basic-game-framework/src/game.c

235 lines
6.6 KiB
C
Raw Normal View History

#include "engine.h"
#include "tilemap.h"
#include "corelib/input.h"
#include "corelib/render.h"
#include "corelib/assets.h"
#include "corelib/layers.h"
#include "SDL2/SDL_mouse.h"
#include "SDL2/SDL_scancode.h"
#include "ui.h"
int cursor_tile = 1;
int dragging = 0;
int drawing = 0;
sprite_t cursor;
spritesheet_t world_sheet;
tileset_t tileset;
tilemap_t tilemap = {
.width = 0,.height = 0,
.x=0,.y=0
};
void on_save_key(int down) {
if(down) save_tilemap(&tilemap, "tilemap.csv");
}
void on_load_key(int down) {
if(down) load_tilemap(&tilemap, "tilemap.csv");
}
void on_clear_key(int down) {
if(down) {
for(int i = 0; i < tilemap.height * tilemap.width; ++i) {
tilemap.tiles[i] = 0;
}
}
}
void on_cycle_tile(int axis) {
cursor_tile += axis;
if(cursor_tile <= 0) {
cursor_tile = 1;
}
int size = tileset.size;
if(cursor_tile >= size) {
cursor_tile = size-1;
}
if(tileset.size >= cursor_tile && 0 < cursor_tile) {
cursor = tileset.set[cursor_tile-1].sprite;
}
}
void on_quit_key(int down) {
if(down) {
g_context.running = 0;
}
}
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;
g_active_view.y -= dy * g_active_view.width;
}
}
void on_middle_mouse(int down) {
dragging = down;
}
void on_debug_frame(int down) {
if(down) {
d_debug_next_frame = 1;
}
}
void on_button_zoom(int axis) {
if(axis != 0) {
g_active_view.width += axis;
}
}
void on_scroll_zoom(float delta) {
g_active_view.width -= delta * 0.5f;
}
void load_game() {
style.button.button = style.button.active = make_nineslice("button.png", 126, 0.01);
world_sheet = make_spritesheet("tileset.png", 189, 189);
tileset.set = (tile_t*)calloc(21, sizeof(tile_t));
tileset.size = 21;
tileset.set[0] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 0),.walkable=0};
tileset.set[1] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 1),.walkable=0};
tileset.set[2] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 2),.walkable=0};
tileset.set[3] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 10),.walkable=0};
tileset.set[4] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 11),.walkable=0};
tileset.set[5] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 12),.walkable=0};
tileset.set[6] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 20),.walkable=0};
tileset.set[7] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 21),.walkable=0};
tileset.set[8] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 22),.walkable=0};
tileset.set[9] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 30),.walkable=1};
tileset.set[10]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 31),.walkable=1};
tileset.set[11]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 32),.walkable=1};
tileset.set[12]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 3),.walkable=1};
tileset.set[13]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 4),.walkable=1};
tileset.set[14]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 5),.walkable=1};
tileset.set[15]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 13),.walkable=1};
tileset.set[16]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 14),.walkable=1};
tileset.set[17]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 15),.walkable=1};
tileset.set[18]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 23),.walkable=1};
tileset.set[19]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 24),.walkable=1};
tileset.set[20]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 25),.walkable=1};
// register calloc'd array to be deleted with free(...)
add_arbitrary_asset(tileset.set, &free);
tilemap.width = tilemap.height = 128;
on_cycle_tile(0);
input_init();
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_W, &on_save_key);
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_R, &on_load_key);
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_C, &on_clear_key);
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_Q, &on_quit_key);
add_key_listener(SDL_SCANCODE_A, SDL_SCANCODE_D, &on_cycle_tile);
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_TAB, &on_debug_frame);
add_key_listener(SDL_SCANCODE_MINUS, SDL_SCANCODE_EQUALS, &on_button_zoom);
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(&on_drag_world);
add_scroll_listener(&on_scroll_zoom);
}
void start_game() {
g_active_view.width = 21;
on_clear_key(1);
}
void update_game() {
if(drawing == 1) {
drawing = 2;
}
float fx, fy;
mouse_world_position(&fx, &fy);
cursor.x = floor(fx); cursor.y = floor(fy);
if(drawing) {
int x, y;
x = floorf(fx);
y = floorf(fy);
if(x >= 0 && y >= 0 && x < tilemap.width && y < tilemap.height) {
if(drawing > 0) {
set_tile(&tilemap, x, y, cursor_tile);
} else {
set_tile(&tilemap, x, y, 0);
}
}
}
rectshape_t world_outline = {
tilemap.x, tilemap.y, (float)tilemap.width, (float)tilemap.height,
0.01f, RLAYER_TILEMAP-1, {0,0,0,0}, {255, 255, 255, 255}
};
rectshape_t cursor_outline = {
cursor.x, cursor.y, 1.0f, 1.0f, 0.01f, RLAYER_SPRITES-1,
{0, 0, 0, 0}, {255, 255, 255, 255}
};
draw_rect(&world_outline);
draw_rect(&cursor_outline);
draw_sprite(&cursor);
draw_tilemap(&tilemap, &tileset);
}
void update_ui() {
update_input();
nineslice_t sliced = style.button.active;
sliced.rect = (SDL_FRect){0, 0, 0.1, 0.1};
sliced.depth = RLAYER_UI - 20;
draw_sliced(&sliced);
const int offset_selected = cursor_tile - 1;
rectshape_t selected_tile_rect = (rectshape_t) {
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);
rectshape_t shape = (rectshape_t) {
0.0f, 0.0f, 1.f, 0.015f, 0, RLAYER_UI,
{100,100,100,255}, {0,0,0,0}
};
draw_rect(&shape);
float mx, my;
mouse_screen_position(&mx, &my);
for(int i = 0; i < tileset.size; ++i) {
// make a copy of the reference sprite and adjust it to fit the ui
sprite_t sprite = tileset.set[i].sprite;
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);
}
}
}
}
void game_exit() {}