adding better game world representation #2
|
@ -1,7 +1,10 @@
|
|||
# build dirs
|
||||
bin/
|
||||
release/
|
||||
build/
|
||||
debug/
|
||||
|
||||
compile_commands.json
|
||||
|
||||
# clangd cache
|
||||
.cache/
|
||||
|
|
|
@ -30,4 +30,4 @@ add_executable(${PROJECT}
|
|||
|
||||
target_link_libraries(${PROJECT} SDL2 SDL2_image SDL2_ttf)
|
||||
|
||||
target_include_directories(${PROJECT} PRIVATE "${CMAKE_SOURCE_DIR}/include/" "${CMAKE_SOURCE_DIR}/src/sge/")
|
||||
target_include_directories(${PROJECT} PRIVATE "${CMAKE_SOURCE_DIR}/include/" "${CMAKE_SOURCE_DIR}/src/corelib" "${CMAKE_SOURCE_DIR}/src/ui")
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
[
|
||||
{
|
||||
"directory": "/home/sara/Documents/c-projects/tabletop/release",
|
||||
"command": "/usr/bin/cc -I/home/sara/Documents/c-projects/tabletop/include -I/home/sara/Documents/c-projects/tabletop/src/sge -O3 -DNDEBUG -o CMakeFiles/tabletop.dir/src/assets.c.o -c /home/sara/Documents/c-projects/tabletop/src/assets.c",
|
||||
"file": "/home/sara/Documents/c-projects/tabletop/src/assets.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/sara/Documents/c-projects/tabletop/release",
|
||||
"command": "/usr/bin/cc -I/home/sara/Documents/c-projects/tabletop/include -I/home/sara/Documents/c-projects/tabletop/src/sge -O3 -DNDEBUG -o CMakeFiles/tabletop.dir/src/engine.c.o -c /home/sara/Documents/c-projects/tabletop/src/engine.c",
|
||||
"file": "/home/sara/Documents/c-projects/tabletop/src/engine.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/sara/Documents/c-projects/tabletop/release",
|
||||
"command": "/usr/bin/cc -I/home/sara/Documents/c-projects/tabletop/include -I/home/sara/Documents/c-projects/tabletop/src/sge -O3 -DNDEBUG -o CMakeFiles/tabletop.dir/src/game.c.o -c /home/sara/Documents/c-projects/tabletop/src/game.c",
|
||||
"file": "/home/sara/Documents/c-projects/tabletop/src/game.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/sara/Documents/c-projects/tabletop/release",
|
||||
"command": "/usr/bin/cc -I/home/sara/Documents/c-projects/tabletop/include -I/home/sara/Documents/c-projects/tabletop/src/sge -O3 -DNDEBUG -o CMakeFiles/tabletop.dir/src/render.c.o -c /home/sara/Documents/c-projects/tabletop/src/render.c",
|
||||
"file": "/home/sara/Documents/c-projects/tabletop/src/render.c"
|
||||
}
|
||||
]
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 && cmake --build build && lldb bin/sim-game -o r -b
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=1 && cmake --build build && bin/sim-game
|
|
@ -6,12 +6,12 @@
|
|||
|
||||
#define NUM_ASSETS 256
|
||||
|
||||
void _remove_asset(resource_t*);
|
||||
static void _remove_asset(resource_t*);
|
||||
|
||||
resource_t g_assets[NUM_ASSETS];
|
||||
resource_t* g_assets_endptr = g_assets;
|
||||
|
||||
resource_t* insert_asset(const resource_t* resource) {
|
||||
static resource_t* insert_asset(const resource_t* resource) {
|
||||
*g_assets_endptr = *resource;
|
||||
resource_t* inserted = g_assets_endptr;
|
||||
++g_assets_endptr;
|
||||
|
@ -95,7 +95,7 @@ resource_t* get_asset(const char* file) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
void _delete_referenced_asset(resource_t* res) {
|
||||
static void _delete_referenced_asset(resource_t* res) {
|
||||
free(res->name);
|
||||
switch(res->type) {
|
||||
default:
|
||||
|
@ -139,7 +139,7 @@ void clean_assets() {
|
|||
g_assets_endptr = g_assets;
|
||||
}
|
||||
|
||||
void _remove_asset(resource_t* res) {
|
||||
static void _remove_asset(resource_t* res) {
|
||||
_delete_referenced_asset(res);
|
||||
resource_t* last = g_assets_endptr - 1;
|
||||
memmove(res, last, sizeof(resource_t));
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "SDL2/SDL_render.h"
|
||||
#include "SDL2/SDL_surface.h"
|
||||
#include "SDL2/SDL_ttf.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#define NUM_DRAWCMDS 2048
|
||||
|
||||
|
@ -75,7 +76,8 @@ SDL_FRect get_dest_with_size(SDL_FRect untransformed, int ui) {
|
|||
return r;
|
||||
}
|
||||
|
||||
void exec_sprite_cmd(const drawcmd_t* cmd) {
|
||||
static
|
||||
void _exec_sprite_cmd(const drawcmd_t* cmd) {
|
||||
const sprite_t* sprite = &cmd->sprite;
|
||||
float fw, fh, fwm, fhm;
|
||||
get_scaling_factors(&fw, &fh, &fwm, &fhm, cmd->ui);
|
||||
|
@ -94,7 +96,8 @@ void exec_sprite_cmd(const drawcmd_t* cmd) {
|
|||
&sprite->origin,SDL_FLIP_NONE);
|
||||
}
|
||||
|
||||
void exec_rect_cmd(const drawcmd_t* cmd) {
|
||||
static
|
||||
void _exec_rect_cmd(const drawcmd_t* cmd) {
|
||||
float w, h, wm, hm;
|
||||
get_scaling_factors(&w, &h, &wm, &hm, cmd->ui);
|
||||
SDL_FRect rect = (SDL_FRect) {
|
||||
|
@ -134,7 +137,8 @@ void exec_rect_cmd(const drawcmd_t* cmd) {
|
|||
SDL_RenderFillRectF(g_context.renderer, &r);
|
||||
}
|
||||
|
||||
void exec_sliced_cmd(const drawcmd_t* cmd) {
|
||||
static
|
||||
void _exec_sliced_cmd(const drawcmd_t* cmd) {
|
||||
const nineslice_t* sliced = &cmd->sliced;
|
||||
|
||||
// target rect in world space
|
||||
|
@ -229,7 +233,8 @@ void exec_sliced_cmd(const drawcmd_t* cmd) {
|
|||
SDL_RenderCopyF(g_context.renderer, t, &srcr, &dstr);
|
||||
}
|
||||
|
||||
void exec_text_cmd(const drawcmd_t* cmd) {
|
||||
static
|
||||
void _exec_text_cmd(const drawcmd_t* cmd) {
|
||||
SDL_FRect r = cmd->text.area;
|
||||
int fh = TTF_FontHeight(cmd->text.style.font);
|
||||
int wrap = (int)(fh * r.w / cmd->text.style.size);
|
||||
|
@ -295,14 +300,15 @@ sprite_t render_text(const char* str, SDL_FRect area, text_style_t style) {
|
|||
}
|
||||
|
||||
typedef void(*drawcmd_delegate)(const drawcmd_t*);
|
||||
drawcmd_delegate const drawcmd_funcs[] = {
|
||||
&exec_sprite_cmd,
|
||||
&exec_rect_cmd,
|
||||
&exec_sliced_cmd,
|
||||
&exec_text_cmd,
|
||||
static drawcmd_delegate const drawcmd_funcs[] = {
|
||||
&_exec_sprite_cmd,
|
||||
&_exec_rect_cmd,
|
||||
&_exec_sliced_cmd,
|
||||
&_exec_text_cmd,
|
||||
};
|
||||
|
||||
void exec_buffer() {
|
||||
static inline
|
||||
void _exec_buffer() {
|
||||
if(d_debug_next_frame) printf("debug capture of draw buffer\ncount: %zu\n", (size_t)(g_drawdata_endptr - g_drawdata));
|
||||
for(const drawcmd_t* cmd = g_drawdata; cmd != g_drawdata_endptr; ++cmd) {
|
||||
if(cmd->type > DRAWCMDTYPE_MIN && cmd->type < DRAWCMDTYPE_MAX) {
|
||||
|
@ -320,12 +326,13 @@ void swap_buffer() {
|
|||
SDL_GetRendererOutputSize(g_context.renderer, &iw, &ih);
|
||||
_render_width = (float)iw; _render_height = (float)ih;
|
||||
_aspect_ratio = _render_height/_render_width;
|
||||
exec_buffer();
|
||||
_exec_buffer();
|
||||
SDL_RenderPresent(g_context.renderer);
|
||||
g_drawdata_endptr = g_drawdata;
|
||||
}
|
||||
|
||||
void insert_drawcmd_at(size_t index, const drawcmd_t* cmd) {
|
||||
static inline
|
||||
void _insert_drawcmd_at(size_t index, const drawcmd_t* cmd) {
|
||||
drawcmd_t* insertpoint = g_drawdata + index;
|
||||
drawcmd_t* dest = insertpoint + 1;
|
||||
size_t size = (size_t)(g_drawdata_endptr - g_drawdata);
|
||||
|
@ -337,13 +344,15 @@ void insert_drawcmd_at(size_t index, const drawcmd_t* cmd) {
|
|||
memmove(dest, insertpoint, count*sizeof(drawcmd_t));
|
||||
}
|
||||
}
|
||||
memcpy(insertpoint, cmd, sizeof(drawcmd_t));
|
||||
*insertpoint = *cmd;
|
||||
insertpoint->ui = _render_mode == 1;
|
||||
}
|
||||
|
||||
void draw(const drawcmd_t* cmd) {
|
||||
static inline
|
||||
void _draw(const drawcmd_t* cmd) {
|
||||
if(g_drawdata_endptr == g_drawdata) {
|
||||
insert_drawcmd_at(0, cmd);
|
||||
_insert_drawcmd_at(0, cmd);
|
||||
return;
|
||||
}
|
||||
long top = (size_t)(g_drawdata_endptr - g_drawdata),
|
||||
bot = 0,
|
||||
|
@ -367,7 +376,7 @@ void draw(const drawcmd_t* cmd) {
|
|||
med++;
|
||||
diff = g_drawdata[med].depth - cmd->depth;
|
||||
}
|
||||
insert_drawcmd_at(med, cmd);
|
||||
_insert_drawcmd_at(med, cmd);
|
||||
}
|
||||
|
||||
void draw_sprite(const sprite_t* sprite) {
|
||||
|
@ -376,7 +385,7 @@ void draw_sprite(const sprite_t* sprite) {
|
|||
.depth=sprite->depth,
|
||||
.sprite=*sprite
|
||||
};
|
||||
draw(&d);
|
||||
_draw(&d);
|
||||
}
|
||||
|
||||
void draw_rect(const rectshape_t* rect) {
|
||||
|
@ -385,7 +394,7 @@ void draw_rect(const rectshape_t* rect) {
|
|||
.depth=rect->depth,
|
||||
.rect=*rect
|
||||
};
|
||||
draw(&d);
|
||||
_draw(&d);
|
||||
}
|
||||
|
||||
void draw_sliced(const nineslice_t *sliced) {
|
||||
|
@ -394,7 +403,7 @@ void draw_sliced(const nineslice_t *sliced) {
|
|||
.depth=sliced->depth,
|
||||
.sliced=*sliced
|
||||
};
|
||||
draw(&d);
|
||||
_draw(&d);
|
||||
}
|
||||
|
||||
void draw_text(const char *str, SDL_FRect area, text_style_t style, depth_t depth) {
|
||||
|
@ -410,7 +419,7 @@ void draw_text(const char *str, SDL_FRect area, text_style_t style, depth_t dept
|
|||
.text=t,
|
||||
.depth=depth,
|
||||
};
|
||||
draw(&d);
|
||||
_draw(&d);
|
||||
}
|
||||
|
||||
spritesheet_t make_spritesheet(const char *file, int tile_width, int tile_height) {
|
||||
|
|
|
@ -89,7 +89,6 @@ extern void screen_to_view(float* x, float* y);
|
|||
|
||||
extern void clear_buffer();
|
||||
extern void swap_buffer();
|
||||
extern void draw(const drawcmd_t* cmd);
|
||||
|
||||
extern sprite_t render_text(const char* str, SDL_FRect area, text_style_t style);
|
||||
extern void draw_sprite(const sprite_t* sprite);
|
||||
|
@ -104,9 +103,7 @@ extern text_style_t make_text_style(const char* font, SDL_Color color, int dpi,
|
|||
extern SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index);
|
||||
extern void set_active_view(const view_t* view);
|
||||
|
||||
inline sprite_t no_sprite() {
|
||||
return (sprite_t){NULL, 0.0, 0.0, {0.0, 0.0}, 0.0, 0.0, 0.0, 0, {0, 0, 0, 0}};
|
||||
}
|
||||
#define no_sprite (sprite_t){NULL, 0.0, 0.0, {0.0, 0.0}, 0.0, 0.0, 0.0, 0, {0, 0, 0, 0}}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
34
src/engine.c
34
src/engine.c
|
@ -2,20 +2,31 @@
|
|||
#include "corelib/assets.h"
|
||||
#include "corelib/render.h"
|
||||
#include "corelib/input.h"
|
||||
#include "time.h"
|
||||
|
||||
int engine_start() {
|
||||
static float _delta_time = 0;
|
||||
static struct timespec start_last_frame;
|
||||
|
||||
inline float delta_time() {
|
||||
return _delta_time;
|
||||
}
|
||||
|
||||
static inline
|
||||
int _engine_start() {
|
||||
init_context();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int engine_shutdown() {
|
||||
static inline
|
||||
int _engine_shutdown() {
|
||||
game_exit();
|
||||
clean_assets();
|
||||
close_context();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void handle_events() {
|
||||
static inline
|
||||
void _handle_events() {
|
||||
while(SDL_PollEvent(&g_context.event)) {
|
||||
input_event(g_context.event);
|
||||
switch(g_context.event.type) {
|
||||
|
@ -26,7 +37,8 @@ void handle_events() {
|
|||
}
|
||||
}
|
||||
|
||||
int engine_run() {
|
||||
static inline
|
||||
int _engine_run() {
|
||||
SDL_Window* window = SDL_CreateWindow("Tabletop", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 420, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
||||
|
||||
g_context = (context_t){
|
||||
|
@ -38,8 +50,14 @@ int engine_run() {
|
|||
load_game();
|
||||
start_game();
|
||||
|
||||
timespec_get(&start_last_frame, TIME_UTC);
|
||||
struct timespec next_time;
|
||||
while(g_context.running) {
|
||||
handle_events();
|
||||
timespec_get(&next_time, TIME_UTC);
|
||||
_delta_time = (next_time.tv_nsec - start_last_frame.tv_nsec) * 1E-9;
|
||||
if(next_time.tv_nsec < start_last_frame.tv_nsec) _delta_time = 0;
|
||||
start_last_frame = next_time;
|
||||
_handle_events();
|
||||
_render_mode = 1;
|
||||
update_ui();
|
||||
_render_mode = 0;
|
||||
|
@ -50,7 +68,7 @@ int engine_run() {
|
|||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
engine_start();
|
||||
engine_run();
|
||||
engine_shutdown();
|
||||
_engine_start();
|
||||
_engine_run();
|
||||
_engine_shutdown();
|
||||
}
|
||||
|
|
10
src/engine.h
10
src/engine.h
|
@ -1,6 +1,12 @@
|
|||
#ifndef _engine_h
|
||||
#define _engine_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern float delta_time();
|
||||
|
||||
/* TO BE DEFINED IN GAME */
|
||||
|
||||
extern void load_game();
|
||||
|
@ -9,4 +15,8 @@ extern void start_game();
|
|||
extern void update_game();
|
||||
extern void update_ui();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _engine_h */
|
||||
|
|
161
src/game.c
161
src/game.c
|
@ -1,53 +1,16 @@
|
|||
#include "engine.h"
|
||||
#include "tilemap.h"
|
||||
#include "world.h"
|
||||
#include "player.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) {
|
||||
|
@ -92,51 +55,11 @@ void on_scroll_zoom(float delta) {
|
|||
}
|
||||
|
||||
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);
|
||||
world_clear();
|
||||
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);
|
||||
|
@ -144,94 +67,24 @@ void load_game() {
|
|||
add_mouse_button_listener(SDL_BUTTON_MMASK, &on_middle_mouse);
|
||||
add_mouse_listener(&on_drag_world);
|
||||
add_scroll_listener(&on_scroll_zoom);
|
||||
|
||||
create_player();
|
||||
}
|
||||
|
||||
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);
|
||||
update_objects();
|
||||
draw_objects();
|
||||
}
|
||||
|
||||
void update_ui() {
|
||||
update_input();
|
||||
|
||||
nineslice_t sliced = style.button.active;
|
||||
sliced.rect = (SDL_FRect){0, 0.25f, 0.2, 0.05};
|
||||
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);
|
||||
text_style_t style = make_text_style("ui_font.otf", 100, 0.01);
|
||||
draw_text("M",
|
||||
(SDL_FRect){0.0, 0.0, 10.0, 10.0}, style, (SDL_Color){255, 255, 255, 255}, RLAYER_UI - 100);
|
||||
|
||||
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() {}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
#include "player.h"
|
||||
#include "layers.h"
|
||||
#include "world.h"
|
||||
#include "input.h"
|
||||
#include "engine.h"
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
|
||||
float player_move_x = 0;
|
||||
float player_move_y = 0;
|
||||
float player_xv = 0;
|
||||
float player_yv = 0;
|
||||
object_t* player_instance = NULL;
|
||||
|
||||
void player_update(object_t *object) {
|
||||
float m = sqrtf(player_move_x*player_move_x + player_move_y*player_move_y);
|
||||
if(m == FP_NAN) m = 1;
|
||||
m = 1.f/m;
|
||||
player_xv = player_move_x * 3.f * m,
|
||||
player_yv = player_move_y * 3.f * m;
|
||||
object->sprite.x += player_xv * delta_time();
|
||||
object->sprite.y += player_yv * delta_time();
|
||||
}
|
||||
|
||||
object_t* create_player() {
|
||||
player_instance = make_object();
|
||||
player_instance->evt_update = &player_update;
|
||||
player_instance->sprite = make_sprite("player.png", 0, 0);
|
||||
|
||||
add_key_listener(SDL_SCANCODE_A, SDL_SCANCODE_D, &player_axis_horizontal);
|
||||
add_key_listener(SDL_SCANCODE_S, SDL_SCANCODE_W, &player_axis_vertical);
|
||||
|
||||
return player_instance;
|
||||
}
|
||||
|
||||
void player_axis_horizontal(int axis) {
|
||||
player_move_x = axis;
|
||||
}
|
||||
|
||||
void player_axis_vertical(int axis) {
|
||||
player_move_y = -axis;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef _player_h
|
||||
#define _player_h
|
||||
|
||||
struct object_t;
|
||||
|
||||
extern float player_move_x, player_move_y,
|
||||
player_xv, player_yv;
|
||||
extern struct object_t* player_instance;
|
||||
|
||||
void player_update(struct object_t* object);
|
||||
struct object_t* create_player();
|
||||
|
||||
extern void player_axis_horizontal(int axis);
|
||||
extern void player_axis_vertical(int axis);
|
||||
|
||||
#endif /* _player_h */
|
|
@ -1,82 +0,0 @@
|
|||
#include "tilemap.h"
|
||||
#include "corelib/layers.h"
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "signal.h"
|
||||
#include "stdio.h"
|
||||
|
||||
void draw_tilemap(const tilemap_t* tilemap, const tileset_t* tileset) {
|
||||
sprite_t sprite;
|
||||
for(int y=0;y<tilemap->height;++y) {
|
||||
for(int x=0;x<tilemap->width;++x) {
|
||||
size_t index = get_tile(tilemap, x, y);
|
||||
if(index == 0) continue;
|
||||
const tile_t tile = tileset->set[index-1];
|
||||
sprite = tile.sprite;
|
||||
sprite.depth = RLAYER_TILEMAP;
|
||||
sprite.x = x; sprite.y = y;
|
||||
draw_sprite(&sprite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int save_tilemap(const tilemap_t* map, const char* file) {
|
||||
FILE* fs = fopen(file, "w");
|
||||
if(fs == NULL) {
|
||||
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
|
||||
return 1;
|
||||
}
|
||||
fprintf(fs, "%d,%d,", map->width, map->height);
|
||||
for(int i = 0; i < map->width * map->height; ++i)
|
||||
fprintf(fs,"%zu,", map->tiles[i]);
|
||||
fclose(fs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int load_tilemap(tilemap_t* map, const char* file) {
|
||||
FILE* fs = fopen(file, "r");
|
||||
if(fs == NULL) {
|
||||
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
|
||||
return 1;
|
||||
}
|
||||
char buf[99];
|
||||
char* writer = buf;
|
||||
int n = 0;
|
||||
size_t* tile = map->tiles;
|
||||
int entries=0;
|
||||
while(1) {
|
||||
n = fgetc(fs);
|
||||
const char c = (char)n;
|
||||
switch(n) {
|
||||
case ' ':
|
||||
case '\n':
|
||||
case '\t':
|
||||
break; // ignore
|
||||
case EOF:
|
||||
case ',':
|
||||
*writer = '\0';
|
||||
if(entries == 0) {
|
||||
map->width = atoi(buf);
|
||||
} else if(entries == 1) {
|
||||
map->height = atoi(buf);
|
||||
} else {
|
||||
if(entries-2 >= TILEMAP_MAX_SIZE) goto end_while;
|
||||
*tile = (size_t)atoi(buf);
|
||||
++tile;
|
||||
}
|
||||
writer = buf;
|
||||
++entries;
|
||||
if(n == EOF)
|
||||
goto end_while;
|
||||
else
|
||||
break;
|
||||
default:
|
||||
*writer = c;
|
||||
++writer;
|
||||
break;
|
||||
}
|
||||
}
|
||||
end_while:
|
||||
fclose(fs);
|
||||
return 0;
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
#ifndef _tilemap_h
|
||||
#define _tilemap_h
|
||||
|
||||
#include "corelib/render.h"
|
||||
|
||||
#define TILEMAP_MAX_SIZE 16384
|
||||
#define TILESET_MAX_SIZE 255
|
||||
|
||||
typedef struct tile_t {
|
||||
sprite_t sprite;
|
||||
short walkable;
|
||||
} tile_t;
|
||||
|
||||
typedef struct tileset_t {
|
||||
tile_t* set;
|
||||
size_t size;
|
||||
} tileset_t;
|
||||
|
||||
typedef struct tilemap_t {
|
||||
size_t tiles[TILEMAP_MAX_SIZE];
|
||||
int width, height;
|
||||
float x, y;
|
||||
} tilemap_t;
|
||||
|
||||
static inline int get_tilemap_index(const tilemap_t* tilemap, int x, int y) {
|
||||
return x + y * tilemap->width;
|
||||
}
|
||||
static inline size_t get_tile(const tilemap_t* tilemap, int x, int y) {
|
||||
return *(tilemap->tiles + get_tilemap_index(tilemap, x, y));
|
||||
}
|
||||
static inline void set_tile(tilemap_t* tilemap, int x, int y, size_t value) {
|
||||
*(tilemap->tiles + get_tilemap_index(tilemap, x, y)) = value;
|
||||
}
|
||||
|
||||
extern void draw_tilemap(const tilemap_t* tilemap, const tileset_t* tileset);
|
||||
/*load csv tilemap from file*/
|
||||
extern int load_tilemap(tilemap_t* tilemap, const char* file);
|
||||
/*write csv tilemap to file*/
|
||||
extern int save_tilemap(const tilemap_t* tilemap, const char* file);
|
||||
|
||||
#endif /* _tilemap_h */
|
|
@ -0,0 +1,60 @@
|
|||
#include "world.h"
|
||||
|
||||
object_t g_objects[WORLD_NUM_OBJECTS];
|
||||
|
||||
void world_clear() {
|
||||
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||
g_objects[i].active = 0;
|
||||
g_objects[i].enabled = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void object_draw_sprite(object_t* object) {
|
||||
draw_sprite(&object->sprite);
|
||||
}
|
||||
|
||||
object_t* _find_free_object() {
|
||||
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||
if(g_objects[i].active == 0) {
|
||||
return g_objects + i;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
object_t* make_object() {
|
||||
object_t* o = _find_free_object();
|
||||
o->active = 1;
|
||||
o->enabled = 1;
|
||||
o->evt_draw = &object_draw_sprite;
|
||||
o->evt_update = NULL;
|
||||
memset(&o->sprite, 0, sizeof(sprite_t));
|
||||
return o;
|
||||
}
|
||||
|
||||
object_t* instantiate_object(const object_t *original) {
|
||||
object_t* obj = make_object();
|
||||
*obj = *original;
|
||||
obj->active = 1;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void update_objects() {
|
||||
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||
if(g_objects[i].active == 1
|
||||
&& g_objects[i].enabled == 1
|
||||
&& g_objects[i].evt_update != NULL) {
|
||||
g_objects[i].evt_update(g_objects + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_objects() {
|
||||
for(int i = 0; i < WORLD_NUM_OBJECTS; ++i) {
|
||||
if(g_objects[i].active == 1
|
||||
&& g_objects[i].enabled == 1
|
||||
&& g_objects[i].evt_draw != NULL) {
|
||||
g_objects[i].evt_draw(g_objects + i);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef _world_h
|
||||
#define _world_h
|
||||
|
||||
#include "render.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define WORLD_NUM_OBJECTS 255
|
||||
|
||||
typedef struct object_t object_t;
|
||||
|
||||
typedef void(*tick_fn)(struct object_t*);
|
||||
typedef void(*draw_fn)(struct object_t*);
|
||||
|
||||
struct object_t {
|
||||
sprite_t sprite;
|
||||
int active; // 1 if this object is in use and should not be overriden
|
||||
int enabled; // 1 if this object's events should be triggered
|
||||
|
||||
uintptr_t timer; // free to use for whatever
|
||||
|
||||
tick_fn evt_update;
|
||||
draw_fn evt_draw;
|
||||
};
|
||||
|
||||
extern object_t g_objects[WORLD_NUM_OBJECTS];
|
||||
|
||||
void world_clear();
|
||||
|
||||
object_t* make_object();
|
||||
object_t* instantiate_object(const object_t* original);
|
||||
|
||||
void object_draw_sprite(object_t* object);
|
||||
|
||||
void update_objects();
|
||||
void draw_objects();
|
||||
|
||||
#endif /* _world_h */
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue