diff --git a/src/corelib/render.c b/src/corelib/render.c index 2e6eaf7..c7338a4 100644 --- a/src/corelib/render.c +++ b/src/corelib/render.c @@ -209,9 +209,7 @@ sprite_t make_sprite(const char* file, float x, float y) { .sx=1.0,.sy=1.0, .rot=0, .depth=RLAYER_SPRITES, - .uv=(SDL_Rect){ - .x=0,.y=0,.w=0,.h=0 - } + .uv=(SDL_Rect){0,0,0,0} }; SDL_QueryTexture(sprite.texture, NULL, NULL, &sprite.uv.w, &sprite.uv.h); sprite.origin.x = -(float)sprite.uv.h/2.f; sprite.origin.y = -(float)sprite.uv.h/2.f; @@ -233,8 +231,9 @@ sprite_t sprite_from_spritesheet(spritesheet_t *sheet, int index) { SDL_Rect get_srcrect_from(spritesheet_t *sheet, int index) { int pixels = index * sheet->tile_width; + int w = sheet->w / sheet->tile_width; return (SDL_Rect) { - pixels%sheet->w, pixels/sheet->w*sheet->tile_height, + pixels%sheet->w, index/w * sheet->tile_height, sheet->tile_width, sheet->tile_height, }; } diff --git a/src/game.cc b/src/game.cc index 35b4d75..4a23388 100644 --- a/src/game.cc +++ b/src/game.cc @@ -1,18 +1,16 @@ #include "engine.hpp" +#include "tilemap.hpp" #include "corelib/input.h" #include "corelib/render.h" #include "corelib/assets.h" #include "corelib/layers.h" -#include "tilemap.hpp" #include "SDL2/SDL_mouse.h" #include -void on_cycle_tile(int); - int cursor_tile = 1; int dragging = 0; int drawing = 0; -sprite_t player; +sprite_t cursor; sprite_t current_tile_display; spritesheet_t world_sheet; @@ -36,7 +34,9 @@ void on_clear_key(int down) { void on_cycle_tile(int axis) { cursor_tile += axis; - player.uv = get_srcrect_from(&world_sheet, cursor_tile); + if(g_tilemap.tileset.set.size() >= cursor_tile && 0 < cursor_tile) { + cursor = g_tilemap.tileset.set.at(cursor_tile-1); + } } void on_quit_key(int down) { @@ -63,20 +63,28 @@ void on_middle_mouse(int down) { void load_game() { g_tilemap.width = g_tilemap.height = 10; - player = make_sprite("tilemap.png", 0, 0); + cursor = make_sprite("tilemap.png", 0, 0); current_tile_display = make_sprite("tilemap.png", 0, 0); current_tile_display.depth = 0; current_tile_display.sx = 0.15f; current_tile_display.sy = 0.15f; - world_sheet = make_spritesheet("tilemap.png", 16, 16); + world_sheet = make_spritesheet("tilemap.png", 189, 189); g_tilemap.tileset.set = { + sprite_from_spritesheet(&world_sheet, 0), sprite_from_spritesheet(&world_sheet, 1), sprite_from_spritesheet(&world_sheet, 2), - sprite_from_spritesheet(&world_sheet, 3), - sprite_from_spritesheet(&world_sheet, 4), + sprite_from_spritesheet(&world_sheet, 10), + sprite_from_spritesheet(&world_sheet, 11), + sprite_from_spritesheet(&world_sheet, 12), + sprite_from_spritesheet(&world_sheet, 20), + sprite_from_spritesheet(&world_sheet, 21), + sprite_from_spritesheet(&world_sheet, 22), + sprite_from_spritesheet(&world_sheet, 30), + sprite_from_spritesheet(&world_sheet, 31), + sprite_from_spritesheet(&world_sheet, 32), }; for(int i=0;ivalue == 0) continue; + tile_t* tile = get_tilemap_tile(x, y); + if(tile->value <= 0) continue; sprite = g_tilemap.tileset.set[tile->value-1]; sprite.depth = RLAYER_TILEMAP; sprite.x = x; sprite.y = y; diff --git a/tilemap.csv b/tilemap.csv index 0f9141b..9bc758b 100644 --- a/tilemap.csv +++ b/tilemap.csv @@ -1 +1 @@ -10,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,1,1,1,1,1,3,3,3,2,2,1,1,1,1,3,3,2,2,2,2,1,1,3,3,3,2,2,2,2,2,1,3,3,3,2,2,2,2,2,2,3,3,3,2,2,2,2,2,2,2,3,3,2,2,2,2,2,2,2,2, \ No newline at end of file +10,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,3,0,0,0,0,0,0,4,5,5,6,0,0,0,0,0,0,7,8,8,9,0,0,0,0,0,0,7,8,8,9,0,0,0,0,0,0,10,11,11,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, \ No newline at end of file