From 738102b6e68c5e664e92e787765f283d659e6b63 Mon Sep 17 00:00:00 2001 From: Linus_Probert Date: Fri, 23 Feb 2018 11:01:25 +0100 Subject: [PATCH] Adds basic skillbar rendering, now we need skills --- CMakeLists.txt | 1 + src/main.c | 5 ++++ src/skillbar.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++--- src/skillbar.h | 4 +++- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e8903d4..b9fd992 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,7 @@ add_executable(breakhack src/mixer src/io_util src/physfsrwops + src/skillbar ) target_link_libraries(breakhack diff --git a/src/main.c b/src/main.c index a63e547..675a899 100644 --- a/src/main.c +++ b/src/main.c @@ -43,6 +43,7 @@ #include "keyboard.h" #include "mixer.h" #include "random.h" +#include "skillbar.h" typedef enum Turn_t { PLAYER, @@ -55,6 +56,7 @@ static Player *gPlayer = NULL; static Map *gMap = NULL; static RoomMatrix *gRoomMatrix = NULL; static Gui *gGui = NULL; +static SkillBar *gSkillBar = NULL; static Pointer *gPointer = NULL; static unsigned int cLevel = 1; static float deltaTime = 1.0; @@ -181,6 +183,7 @@ initGame(void) gCamera.renderer = gRenderer; gRoomMatrix = roommatrix_create(); gGui = gui_create(gRenderer); + gSkillBar = skillbar_create(gRenderer); item_builder_init(gRenderer); gPointer = pointer_create(gRenderer); particle_engine_init(); @@ -481,6 +484,7 @@ run_game(void) RIGHT_GUI_HEIGHT, &gCamera); SDL_RenderSetViewport(gRenderer, &skillBarViewport); + skillbar_render(gSkillBar, &gCamera); SDL_RenderSetViewport(gRenderer, &bottomGuiViewport); gui_render_log(gGui, BOTTOM_GUI_WIDTH, @@ -600,6 +604,7 @@ void close(void) roommatrix_destroy(gRoomMatrix); gui_destroy(gGui); + skillbar_destroy(gSkillBar); pointer_destroy(gPointer); item_builder_close(); particle_engine_close(); diff --git a/src/skillbar.c b/src/skillbar.c index d9e296d..4725916 100644 --- a/src/skillbar.c +++ b/src/skillbar.c @@ -16,16 +16,32 @@ * along with this program. If not, see . */ +#include "defines.h" #include "skillbar.h" #include "texture.h" #include "util.h" +#include "sprite.h" static void load_texture(SkillBar *bar, const char *path, SDL_Renderer *renderer) { + static SDL_Color c_yellow = { 255, 255, 0, 255 }; + Texture *t = texture_create(); texture_load_from_file(t, path, renderer); + t->dim.width = 16; + t->dim.height = 16; ht_set(bar->textures, path, t); + + for (unsigned int i = 0; i < 4; ++i) { + char buffer[1]; + Sprite *s = sprite_create(); + s->pos = (Position) { i * 32 + 20, 20 }; + sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 8); + m_sprintf(buffer, 1, "%u", i+1); + texture_load_from_text(s->textures[0], buffer, c_yellow, renderer); + linkedlist_append(&bar->sprites, s); + } } SkillBar * @@ -33,19 +49,60 @@ skillbar_create(SDL_Renderer *renderer) { SkillBar *bar = ec_malloc(sizeof(SkillBar)); bar->textures = ht_create(10); + bar->sprites = linkedlist_create(); load_texture(bar, "GUI/GUI0.png", renderer); return bar; } -void -render(SkillBar *bar, Camera *cam) +static void +render_frame(SkillBar *bar, Camera *cam) { - // TODO: Implement + static SDL_Rect c_top_left = { 1*16, 10*16, 16, 16 }; + static SDL_Rect c_top_right = { 3*16, 10*16, 16, 16 }; + static SDL_Rect c_bottom_left = { 1*16, 12*16, 16, 16 }; + static SDL_Rect c_bottom_right = { 3*16, 12*16, 16, 16 }; + + Texture *t = ht_get(bar->textures, "GUI/GUI0.png"); + Position p = { 0, 0 }; + + for (unsigned int i = 0; i < MAP_ROOM_WIDTH; ++i) { + p.x = i*32; + p.y = 0; + texture_render_clip(t, &p, &c_top_left, cam); + p.y = 16; + texture_render_clip(t, &p, &c_bottom_left, cam); + + p.x = i*32 + 16; + p.y = 0; + texture_render_clip(t, &p, &c_top_right, cam); + p.y = 16; + texture_render_clip(t, &p, &c_bottom_right, cam); + } +} + +static void +render_sprites(SkillBar *bar, Camera *cam) +{ + LinkedList *sprites = bar->sprites; + + while (sprites) { + sprite_render(sprites->data, cam); + sprites = sprites->next; + } +} + +void +skillbar_render(SkillBar *bar, Camera *cam) +{ + render_frame(bar, cam); + render_sprites(bar, cam); } void skillbar_destroy(SkillBar *bar) { ht_destroy_custom(bar->textures, (void (*)(void *)) texture_destroy); + while (bar->sprites) + sprite_destroy(linkedlist_pop(&bar->sprites)); free(bar); } diff --git a/src/skillbar.h b/src/skillbar.h index 4955919..96813de 100644 --- a/src/skillbar.h +++ b/src/skillbar.h @@ -21,17 +21,19 @@ #include "SDL.h" #include "hashtable.h" +#include "linkedlist.h" #include "camera.h" typedef struct SkillBar_t { Hashtable *textures; + LinkedList *sprites; } SkillBar; SkillBar * skillbar_create(SDL_Renderer*); void -render(SkillBar*, Camera*); +skillbar_render(SkillBar*, Camera*); void skillbar_destroy(SkillBar*);