Skip to content

Commit ae7c5d4

Browse files
committed
Game_Gui a0.4 - Text, energy, blendmode on
- new requirement: SDL2_ttf (for font support, so handling text is easier for me =)) - Game now contains energy ('p' to regenerate energy, because its still beta) - changed SDL_Init(SDL_INIT_VIDEO) to SDL_Init(SDL_INIT_EVERYTHING), Its for the text... I think. - Now SDL_RenderDrawBlendMode for renderer is ON (SDL_BLENDMODE_BLEND), according to someone who helped me, its required Signed-off-by: Hanuko33 <piotr.a.danecki@gmail.com>
1 parent 9ced917 commit ae7c5d4

8 files changed

Lines changed: 94 additions & 9 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ find_package(PkgConfig)
55
pkg_check_modules(SDL sdl2)
66
pkg_check_modules(SDL2_IMAGE SDL2_image>=2.0.0)
77
pkg_check_modules(SDL2_GFX SDL2_gfx)
8+
pkg_check_modules(SDL2_TTF SDL2_ttf)
89

910
set(CMAKE_BUILD_TYPE Debug)
1011

11-
add_executable(game_new window.cpp texture.cpp dungeon.cpp main.cpp)
12+
add_executable(game_new window.cpp text.cpp texture.cpp dungeon.cpp main.cpp)
1213

1314
target_link_libraries(game_new
14-
SDL2 SDL2_gfx SDL2_image
15+
SDL2 SDL2_gfx SDL2_image SDL2_ttf
1516
${SDL_LDFLAGS}
1617
-lm -lpthread
1718
)

main.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
#include <SDL2/SDL2_framerate.h>
99
#include "dungeon.h"
1010
#include "texture.h"
11+
#include "text.h"
1112
#define SIZE 16
1213

1314
using namespace std;
1415

1516
game_tiles screen_list[SIZE][SIZE];
1617
game_tiles terrain_list[SIZE][SIZE];
1718
Dungeon dungeon;
19+
Text text;
1820

1921
class Player
2022
{
@@ -27,6 +29,7 @@ class Player
2729
void interact(char key, Dungeon& dungeon);
2830
bool going_right = false;
2931
bool in_dungeon = false;
32+
int energy = 400;
3033
};
3134

3235
void generator()
@@ -71,8 +74,8 @@ void save(bool with_player, Player* player)
7174
char player_path[11];
7275
sprintf(player_path, "player.txt");
7376
file = fopen(player_path, "w");
74-
char to_write[13];
75-
sprintf(to_write, "%d\n%d\n%d\n%d\n%d\n", player->map_y, player->map_x, player->y, player->x, (int)player->in_dungeon);
77+
char to_write[14];
78+
sprintf(to_write, "%d\n%d\n%d\n%d\n%d\n%d\n", player->map_y, player->map_x, player->y, player->x, (int)player->in_dungeon, player->energy);
7679
fwrite(to_write, sizeof(to_write), 1, file);
7780
fclose(file);
7881
}
@@ -92,7 +95,6 @@ void save(bool with_player, Player* player)
9295
fwrite(terrain_list, sizeof(terrain_list), 1, chunk);
9396
fclose(chunk);
9497
}
95-
9698
}
9799
void load(bool with_player, Player* player)
98100
{
@@ -104,7 +106,7 @@ void load(bool with_player, Player* player)
104106
sprintf(player_path, "player.txt");
105107
if ((file = fopen(player_path, "r")))
106108
{
107-
fscanf(file, "%d%d%d%d%d", &player->map_y, &player->map_x, &player->y, &player->x, &temp);
109+
fscanf(file, "%d%d%d%d%d%d", &player->map_y, &player->map_x, &player->y, &player->x, &temp, &player->energy);
108110
player->in_dungeon=temp;
109111
}
110112
else
@@ -173,6 +175,8 @@ void Player::interact(char key, Dungeon &dungeon)
173175
if (y < SIZE-1) y++;
174176
else {y=0; save(false, this); map_y++; load(false,this);}
175177
}
178+
energy--;
179+
if (running) energy--;
176180
break;
177181
}
178182
case 'w':
@@ -195,6 +199,8 @@ void Player::interact(char key, Dungeon &dungeon)
195199
if (y > 0) y--;
196200
else {y=SIZE-1; save(false, this); map_y--;load(false,this);}
197201
}
202+
energy--;
203+
if (running) energy--;
198204
break;
199205
}
200206
case 'd':
@@ -217,6 +223,8 @@ void Player::interact(char key, Dungeon &dungeon)
217223
if (x < SIZE-1) x++;
218224
else if (!in_dungeon) {x=0; save(false, this); map_x++;load(false,this);}
219225
}
226+
energy--;
227+
if (running) energy--;
220228
going_right = true;
221229
break;
222230
}
@@ -240,6 +248,8 @@ void Player::interact(char key, Dungeon &dungeon)
240248
if (x > 0) x--;
241249
else if (!in_dungeon) {x=SIZE-1; save(false, this); map_x--;load(false,this);}
242250
}
251+
energy--;
252+
if (running) energy--;
243253
going_right = false;
244254
break;
245255
}
@@ -248,6 +258,11 @@ void Player::interact(char key, Dungeon &dungeon)
248258
save(true, this);
249259
break;
250260
}
261+
case 'p':
262+
{
263+
energy = 400;
264+
break;
265+
}
251266
case 'l':
252267
{
253268
load(true, this);
@@ -391,6 +406,17 @@ void draw(Player player, textures texts, Dungeon& dungeon)
391406
SDL_Rect img_rect = {px, py, TILE_SIZE, TILE_SIZE};
392407
if (player.going_right) SDL_RenderCopy(renderer, texts.playerr, &screen_rect, &img_rect);
393408
else SDL_RenderCopy(renderer, texts.playerl, &screen_rect, &img_rect);
409+
410+
SDL_Texture* text_energy_sdl;
411+
char text_energy[20];
412+
sprintf(text_energy, "Energy: %d", player.energy);
413+
if (player.energy > 100) text_energy_sdl = text.create_font(text_energy, false);
414+
else if (player.energy <= 100) text_energy_sdl = text.create_font(text_energy, true);
415+
416+
SDL_Rect energy_text_rect = {10, 10, 200, 40};
417+
418+
SDL_RenderCopy(renderer, text_energy_sdl, NULL, &energy_text_rect);
419+
SDL_DestroyTexture(text_energy_sdl);
394420
}
395421

396422
int main()
@@ -403,13 +429,16 @@ int main()
403429
char key;
404430
SDL_Event event;
405431
if (init_window()) return 1;
432+
if (text.load_font()) {printf("failed to load font");return 1;};
406433
textures Texture;
407434
Texture.load_textures();
408435
for (;;)
409436
{
410437
clear_window();
411438
update_screen_list(player, dungeon);
439+
412440
draw(player, Texture, dungeon);
441+
413442
while (SDL_PollEvent(&event))
414443
{
415444
if (event.type==SDL_QUIT) {SDL_Quit(); return 0;};
@@ -422,6 +451,7 @@ int main()
422451
case SDLK_d:
423452
case SDLK_e:
424453
case SDLK_k:
454+
case SDLK_p:
425455
case SDLK_l:
426456
case SDLK_r:
427457
key = event.key.keysym.sym;
@@ -435,6 +465,14 @@ int main()
435465
}
436466

437467
}
468+
if (player.energy <= 0)
469+
{
470+
printf("You ran out of energy (death)\n");
471+
printf("This is still in early alpha, the world isn't lost, just run the game and press 'p' to set energy to 400\n");
472+
printf("Thank you for playing =)\n");
473+
SDL_Quit();
474+
return 0;
475+
}
438476
// DEBUG x, y, map_x, map_y, running
439477
//printf("x: %d y: %d\n map_x: %d map_y: %d running: %d\n", player.x, player.y, player.map_x, player.map_y, player.running);
440478

nerdfont.ttf

2.05 MB
Binary file not shown.

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
This is a game made with sdl2 and c++
44
it has (almost) infinite worlds, with 3 types of terrain, stone ground, dirt ground, and tree. In the future there will be propobly a lot more!
55

6+
### This project uses nerd-font: https://github.com/ryanoasis/nerd-fonts
7+
68
### IMPORTANT: New update with dungeons, could get you stuck. I have idea for next update, where dungeons have more sense and can's get you stuck (propobly), if you have any world you are proud of, you should download this version in other directory and play there! Also, dungeons WON'T appear on old chunks.
79

810
This is still early stage (alpha/beta)

text.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "text.h"
2+
#include "window.h"
3+
int Text::load_font()
4+
{
5+
font = TTF_OpenFont("nerdfont.ttf", 500);
6+
if (!(font)) return 1;
7+
else return 0;
8+
}
9+
SDL_Texture* Text::create_font(const char* text, bool warning)
10+
{
11+
if (!(font)) abort();
12+
SDL_Surface* surface;
13+
if (!(warning))
14+
{
15+
surface = TTF_RenderText_Solid(font, text, White);
16+
}
17+
else
18+
{
19+
surface = TTF_RenderText_Solid(font, text, Red);
20+
}
21+
22+
SDL_Texture* text_sdl = SDL_CreateTextureFromSurface(renderer, surface);
23+
24+
SDL_FreeSurface(surface);
25+
return text_sdl;
26+
}

text.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TEXT_H
2+
#define TEXT_H
3+
4+
#include <stdio.h>
5+
#include <SDL2/SDL_ttf.h>
6+
class Text
7+
{
8+
TTF_Font* font;
9+
SDL_Color White = {255, 255, 255};
10+
SDL_Color Red = {255, 0, 0};
11+
public:
12+
SDL_Texture* create_font(const char* test, bool warning);
13+
int load_font();
14+
};
15+
16+
#endif

window.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int init_window()
88
Uint32 flags;
99
flags = SDL_WINDOW_RESIZABLE;
1010

11-
if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError()); }
11+
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError()); }
1212

1313
if (SDL_CreateWindowAndRenderer(0, 0, flags, &main_window, &renderer) < 0) {
1414
SDL_Log("SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
@@ -18,16 +18,17 @@ int init_window()
1818
// app_surface = SDL_GetWindowSurface(main_window);
1919
SDL_SetWindowPosition(main_window, 0,0);
2020
SDL_SetWindowSize(main_window, 600, 600);
21+
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
2122

2223
int imgFlags = IMG_INIT_PNG;
2324
if (!(IMG_Init(imgFlags) & imgFlags))
2425
{
2526
printf("\nUnable to initialize sdl_image: %s\n", IMG_GetError());
2627
return 1;
2728
}
29+
TTF_Init();
2830

29-
30-
return 0;
31+
return 0;
3132
}
3233

3334
void clear_window()

window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define WINDOW_H
33
#include <SDL2/SDL.h>
44
#include <SDL2/SDL_image.h>
5+
#include <SDL2/SDL_ttf.h>
56

67
extern SDL_Renderer *renderer;
78
extern SDL_Window *main_window;

0 commit comments

Comments
 (0)