diff --git a/CMakeLists.txt b/CMakeLists.txt index 42d2918..9de0c9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ add_executable(breakhack src/position src/monster src/stats + src/actiontext ) target_link_libraries(breakhack diff --git a/src/actiontext.c b/src/actiontext.c new file mode 100644 index 0000000..5ed4655 --- /dev/null +++ b/src/actiontext.c @@ -0,0 +1,52 @@ +#include "actiontext.h" +#include "util.h" + +ActionText* +actiontext_create() +{ + ActionText *t = ec_malloc(sizeof(ActionText)); + t->pos = (Position) { 0, 0 }; + t->texture = texture_create(); + t->timer = timer_create(); + t->active = false; + t->color = (SDL_Color) { 255, 255, 255 }; + return t; +} + +void +actiontext_load_font(ActionText *t, const char *path, unsigned int size) +{ + texture_load_font(t->texture, path, size); +} + +void +actiontext_set_text(ActionText *t, const char *text, SDL_Renderer *renderer) +{ + texture_load_from_text(t->texture, text, t->color, renderer); +} + +void +actiontext_render(ActionText *t, Camera *cam) +{ + if (!t->active) + return; + + if (t->active && !timer_started(t->timer)) + timer_start(t->timer); + + Position cameraPos = camera_to_camera_position(cam, &t->pos); + if (timer_get_ticks(t->timer) < 100) { + texture_render(t->texture, &cameraPos, cam); + } else { + timer_stop(t->timer); + t->active = false; + } +} + +void +actiontext_destroy(ActionText *t) +{ + texture_destroy(t->texture); + timer_destroy(t->timer); + free(t); +} diff --git a/src/actiontext.h b/src/actiontext.h new file mode 100644 index 0000000..7030642 --- /dev/null +++ b/src/actiontext.h @@ -0,0 +1,28 @@ +#ifndef ACTIONTEXT_H_ +#define ACTIONTEXT_H_ + +#include + +#include "position.h" +#include "texture.h" +#include "timer.h" + +typedef struct { + Position pos; + Texture *texture; + bool active; + Timer *timer; + SDL_Color color; +} ActionText; + +ActionText* actiontext_create(); + +void actiontext_load_font(ActionText*, const char *path, unsigned int size); + +void actiontext_set_text(ActionText*, const char *text, SDL_Renderer*); + +void actiontext_render(ActionText*, Camera*); + +void actiontext_destroy(ActionText*); + +#endif // ACTIONTEXT_H_ diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..f05d193 --- /dev/null +++ b/src/globals.h @@ -0,0 +1,9 @@ +#ifndef GLOBALS_H_ +#define GLOBALS_H_ + +#include + +TTF_Font *gFontLarge = NULL; +TTF_Font *gFontSmall = NULL; + +#endif // GLOBALS_H_ diff --git a/src/main.c b/src/main.c index 52ef5d3..87f29e5 100644 --- a/src/main.c +++ b/src/main.c @@ -65,12 +65,20 @@ bool initSDL() } if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0) { - printf("[!!] Unable to initiate scaling: %s\n", SDL_GetError()); + printf("[!!] Unable to initiate scaling: %s\n", + SDL_GetError()); return false; } if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) { - printf("[!!] Unable to initiate img loading: %s\n", IMG_GetError()); + printf("[!!] Unable to initiate img loading: %s\n", + IMG_GetError()); + return false; + } + + if ( TTF_Init() == -1 ) { + printf("[!!] Unable to initiate ttf library: %s\n", + TTF_GetError()); return false; } diff --git a/src/map.c b/src/map.c index 3516921..5f22657 100644 --- a/src/map.c +++ b/src/map.c @@ -68,7 +68,8 @@ map_add_monster_texture(Map *map, const char *path, SDL_Renderer *renderer) t = ht_get(map->monsterTextures, path); if (!t) { - t = texture_create(path, renderer); + t = texture_create(); + texture_load_from_file(t, path, renderer); ht_set(map->monsterTextures, path, t); } @@ -111,7 +112,8 @@ map_add_monster(Map *map, Monster *m) int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer) { - Texture *t = texture_create(path, renderer); + Texture *t = texture_create(); + texture_load_from_file(t, path, renderer); linkedlist_append(&map->textures, t); return linkedlist_size(map->textures) - 1; } diff --git a/src/map_lua.c b/src/map_lua.c index f51b4b4..bd9c17c 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -181,9 +181,9 @@ l_add_monster(lua_State *L) lua_pop(L, 4); - monster = monster_create(); + monster = monster_create(renderer); monster->sprite->clip = (SDL_Rect) { clip_x, clip_y, 16, 16 }; - monster->sprite->pos = (Position) { x, y }; + monster_update_pos(monster, (Position) { x, y }); sprite_set_texture(monster->sprite, texture1, 0); sprite_set_texture(monster->sprite, texture2, 1); diff --git a/src/monster.c b/src/monster.c index a8798d7..be4fe66 100644 --- a/src/monster.c +++ b/src/monster.c @@ -3,25 +3,65 @@ #include "player.h" #include "monster.h" +static void +monster_load_texts(Monster *m, SDL_Renderer *renderer) +{ + ActionText *t = actiontext_create(); + actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); + t->color = (SDL_Color) { 255, 100, 0 }; + actiontext_set_text(t, "HIT", renderer); + t->pos = m->sprite->pos; + m->hitText = t; + + t = actiontext_create(); + actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); + t->color = (SDL_Color) { 255, 255, 0 }; + actiontext_set_text(t, "MISS", renderer); + t->pos = m->sprite->pos; + m->missText = t; +} + Monster* -monster_create() +monster_create(SDL_Renderer *renderer) { Monster *m = ec_malloc(sizeof(Monster)); m->sprite = sprite_create(); m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 }; m->stats = (Stats) { 11, 1, 0, 0, 1 }; + + monster_load_texts(m, renderer); + return m; } +void +monster_update_pos(Monster *m, Position pos) +{ + m->sprite->pos = pos; + + Position textPos = pos; + textPos.y += 10; + m->hitText->pos = textPos; + m->missText->pos = textPos; +} + void monster_render(Monster *m, Camera *cam) { sprite_render(m->sprite, cam); + if (m->hitText) + actiontext_render(m->hitText, cam); + if (m->missText) + actiontext_render(m->missText, cam); } void monster_destroy(Monster *m) { sprite_destroy(m->sprite); + if (m->hitText) + actiontext_destroy(m->hitText); + if (m->missText) + actiontext_destroy(m->missText); free(m); } diff --git a/src/monster.h b/src/monster.h index 39a3232..ce72af7 100644 --- a/src/monster.h +++ b/src/monster.h @@ -4,13 +4,18 @@ #include #include "sprite.h" #include "stats.h" +#include "actiontext.h" typedef struct Monster_t { Sprite *sprite; + ActionText *hitText; + ActionText *missText; Stats stats; } Monster; -Monster* monster_create(); +Monster* monster_create(SDL_Renderer*); + +void monster_update_pos(Monster*, Position); void monster_render(Monster*, Camera*); diff --git a/src/player.c b/src/player.c index 1e84431..5081aa3 100644 --- a/src/player.c +++ b/src/player.c @@ -25,7 +25,12 @@ has_collided(Player *player, RoomMatrix *matrix) collided = space->occupied; if (space->monster != NULL) { - stats_fight(&player->stats, &space->monster->stats); + unsigned int hit = stats_fight(&player->stats, + &space->monster->stats); + if (hit > 0) + space->monster->hitText->active = true; + else + space->monster->missText->active = true; } return collided; diff --git a/src/sprite.c b/src/sprite.c index 12eedfe..34b57d7 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -39,7 +39,8 @@ sprite_load_texture(Sprite *sprite, sprite->textures[index] = NULL; } - sprite->textures[index] = texture_create(path, renderer); + sprite->textures[index] = texture_create(); + texture_load_from_file(sprite->textures[index], path, renderer); sprite->destroyTextures = true; } diff --git a/src/stats.c b/src/stats.c index b2735cc..e36c92f 100644 --- a/src/stats.c +++ b/src/stats.c @@ -2,9 +2,11 @@ #include #include #include +#include + #include "stats.h" -void +unsigned int stats_fight(Stats *attacker, Stats *defender) { unsigned int atkRoll, defRoll, dmgRoll; @@ -16,13 +18,16 @@ stats_fight(Stats *attacker, Stats *defender) critical = true; atkRoll += attacker->atk; defRoll = (rand() % 20) + defender->def; + dmgRoll = 0; - printf("Attacking: %d, Defending: %d\n", atkRoll, defRoll); + //printf("Attacking: %u, Defending: %u\n", atkRoll, defRoll); if (atkRoll > defRoll) { dmgRoll = (rand() % 8) + attacker->dmg; defender->hp -= dmgRoll; if (critical) defender->hp -= dmgRoll; } - printf("Attacker hp: %d, Defender hp: %d\n", attacker->hp, defender->hp); + //printf("Attacker hp: %u, Defender hp: %u\n", attacker->hp, defender->hp); + + return dmgRoll; } diff --git a/src/stats.h b/src/stats.h index cf8dc8a..ccac3c3 100644 --- a/src/stats.h +++ b/src/stats.h @@ -9,7 +9,7 @@ typedef struct { int speed; /* Speed */ } Stats; -void +unsigned int stats_fight(Stats *attacker, Stats *defender); #endif // STATS_H_ diff --git a/src/texture.c b/src/texture.c index 767c2c3..14e1dee 100644 --- a/src/texture.c +++ b/src/texture.c @@ -2,38 +2,102 @@ #include "texture.h" #include "util.h" -Texture* texture_create(const char *path, SDL_Renderer *renderer) +Texture* +texture_create() +{ + Texture *t = ec_malloc(sizeof(Texture)); + t->dim.height = 0; + t->dim.width = 0; + t->texture = NULL; + t->font = NULL; + return t; +} + +void +texture_load_from_file(Texture *texture, + const char *path, + SDL_Renderer *renderer) { - Texture *newTexture = NULL; SDL_Surface *surface = IMG_Load(path); if (surface == NULL) { - printf("[!!] Failed to load texture (%s): %s\n", path, IMG_GetError()); + printf("[!!] Failed to load texture (%s): %s\n", + path, IMG_GetError()); + return; } - else + + if (texture->texture) { + SDL_DestroyTexture(texture->texture); + texture->texture = NULL; + } + + texture->dim.height = surface->h; + texture->dim.width = surface->w; + texture->texture = SDL_CreateTextureFromSurface(renderer, + surface); + if (texture->texture == NULL) { - newTexture = ec_malloc(sizeof(Texture)); - newTexture->dim.height = surface->h; - newTexture->dim.width = surface->w; - newTexture->texture = SDL_CreateTextureFromSurface(renderer, - surface); - if (newTexture->texture == NULL) - { - printf("[!!] Failed to create texture (%s): %s\n", - path, - SDL_GetError()); - } - - SDL_FreeSurface(surface); + printf("[!!] Failed to create texture (%s): %s\n", + path, + SDL_GetError()); } - return newTexture; + SDL_FreeSurface(surface); +} + +void +texture_load_font(Texture *t, const char *path, unsigned int size) +{ + if (t->font) + TTF_CloseFont(t->font); + t->font = TTF_OpenFont(path, size); + if (t->font == NULL) { + fprintf(stderr, "[!!] Failed to load font %s: %s\n", + path, + TTF_GetError()); + return; + } +} + +void +texture_load_from_text(Texture *t, + const char *text, + SDL_Color c, + SDL_Renderer *renderer) +{ + SDL_Surface *surface = TTF_RenderText_Solid( t->font, text, c ); + if (surface == NULL) + { + printf("[!!] Unable to create texture from rendered text: %s\n", + IMG_GetError()); + return; + } + + if (t->texture) { + SDL_DestroyTexture(t->texture); + t->texture = NULL; + } + + t->texture = SDL_CreateTextureFromSurface( renderer, surface ); + if (t->texture == NULL) { + printf("[!!] Failed to create texture from text: %s\n", + SDL_GetError()); + return; + } + + t->dim.width = surface->w; + t->dim.height = surface->h; + + SDL_FreeSurface(surface); } void texture_render(Texture *texture, Position *p, Camera *cam) { + if (!texture->texture) + return; + SDL_Rect draw_box = (SDL_Rect) { p->x, p->y, @@ -72,6 +136,9 @@ texture_render_clip(Texture *texture, Position *p, SDL_Rect *clip, Camera *cam) void texture_destroy(Texture *texture) { - SDL_DestroyTexture(texture->texture); + if (texture->texture) + SDL_DestroyTexture(texture->texture); + if (texture->font) + TTF_CloseFont(texture->font); free(texture); } diff --git a/src/texture.h b/src/texture.h index a1249e6..a6983e6 100644 --- a/src/texture.h +++ b/src/texture.h @@ -2,16 +2,27 @@ #define TEXTURE_H_ #include +#include #include "dimension.h" #include "position.h" #include "camera.h" typedef struct { SDL_Texture *texture; + TTF_Font *font; Dimension dim; } Texture; -Texture* texture_create(const char *path, SDL_Renderer *renderer); +Texture* texture_create(); + +void texture_load_from_file(Texture*, const char *path, SDL_Renderer*); + +void texture_load_font(Texture*, const char *path, unsigned int size); + +void texture_load_from_text(Texture*, + const char *text, + SDL_Color, + SDL_Renderer*); void texture_render(Texture*, Position*, Camera*);