diff --git a/src/game.cc b/src/game.cc index 6782b42..529f7d6 100644 --- a/src/game.cc +++ b/src/game.cc @@ -34,6 +34,12 @@ void on_clear_key(int down) { void on_cycle_tile(int axis) { cursor_tile += axis; + if(cursor_tile <= 0) { + cursor_tile = 1; + } + if(cursor_tile > g_tilemap.tileset.set.size()) { + cursor_tile = g_tilemap.tileset.set.size(); + } if(g_tilemap.tileset.set.size() >= cursor_tile && 0 < cursor_tile) { cursor = g_tilemap.tileset.set.at(cursor_tile-1); } @@ -49,6 +55,10 @@ void on_click(int down) { drawing = down; } +void on_erase(int down) { + drawing = -down; +} + void on_drag_world(float dx, float dy) { if(dragging) { g_active_view.x -= dx * g_active_view.width; @@ -60,6 +70,12 @@ void on_middle_mouse(int down) { dragging = down; } +void on_debug_frame(int down) { + if(down) { + d_debug_next_frame = 1; + } +} + void load_game() { g_tilemap.width = g_tilemap.height = 10; @@ -111,7 +127,9 @@ void load_game() { add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_C, &on_clear_key); add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_Q, &on_quit_key); add_listener_for(SDL_SCANCODE_A, SDL_SCANCODE_D, &on_cycle_tile); + add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_TAB, &on_debug_frame); add_mouse_button_listener(SDL_BUTTON_LMASK, &on_click); + add_mouse_button_listener(SDL_BUTTON_RMASK, &on_erase); add_mouse_button_listener(SDL_BUTTON_MMASK, &on_middle_mouse); add_mouse_listener(0, &on_drag_world); @@ -122,6 +140,7 @@ void start_game() { } void update_game() { + if(drawing == 1) drawing = 2; update_input(); mouse_world_position(&cursor.x, &cursor.y); cursor.x += 0.1f; cursor.y += 0.1f; @@ -132,11 +151,15 @@ void update_game() { mouse_world_position(&fx, &fy); x = floorf(fx); y = floorf(fy); - get_tilemap_tile(x, y)->value = cursor_tile; + if(x >= 0 && y >= 0 && x < g_tilemap.width && y < g_tilemap.height) { + if(drawing > 0) { + get_tilemap_tile(x, y)->value = cursor_tile; + } else { + get_tilemap_tile(x, y)->value = 0; + } + } } -} -void draw_game() { rectshape_t world_outline = { g_tilemap.x, g_tilemap.y, float(g_tilemap.width), float(g_tilemap.height), 0.01f, RLAYER_TILEMAP-1, {0,0,0,0}, {255, 255, 255, 255} @@ -152,19 +175,42 @@ void draw_game() { draw_tilemap(); } -void draw_game_ui() { +void update_ui() { rectshape_t shape = (rectshape_t){ - 0.0f, 0.0f, 0.15f, 0.15f, 0, 1, + 0.0f, 0.0f, 1.f, 0.015f, 0, RLAYER_UI, {100,100,100,255}, {0,0,0,0} }; int w = world_sheet.w / world_sheet.tile_width, h = world_sheet.h / world_sheet.tile_height, offset_selected = cursor_tile - 1; rectshape_t selected_tile_rect = (rectshape_t) { - float(offset_selected % w) * 0.015f, float(offset_selected / h) * 0.015f, 0.015f, 0.015f, 0.001f, -1, + offset_selected * 0.015f, 0.f, 0.015f, 0.015f, 0.001f, RLAYER_UI-10, {0,0,0,0}, {255, 255, 255, 255} }; draw_rect(&selected_tile_rect); draw_rect(&shape); - draw_sprite(¤t_tile_display); + + int i = 0; + float mx, my; + mouse_screen_position(&mx, &my); + for(auto& original: g_tilemap.tileset.set) { + // make a copy of the reference sprite and adjust it to fit the ui + sprite_t sprite = original; + sprite.depth = RLAYER_UI-5; + sprite.x = i * 0.015f; sprite.y = 0; + sprite.sx = 0.015f; sprite.sy = 0.015f; + float amx=mx-sprite.x, amy=my-sprite.y; + draw_sprite(&sprite); + + // allow user to select tiles with mouse + if(amx >= 0 && amy >= 0 && amx < sprite.sx && amy < sprite.sy) { + if(drawing == 1) { + drawing = 0; + cursor_tile = i+1; + on_cycle_tile(0); + } + } + + ++i; + } }