world objects and input are now managed in engine.c rather than game.c

pull/4/head
Sara 2023-05-11 19:49:40 +02:00
parent f7696c4ea8
commit 1e184d43c7
2 changed files with 28 additions and 62 deletions

View File

@ -1,4 +1,5 @@
#include "engine.h" #include "engine.h"
#include "world.h"
#include "corelib/assets.h" #include "corelib/assets.h"
#include "corelib/render.h" #include "corelib/render.h"
#include "corelib/input.h" #include "corelib/input.h"
@ -47,7 +48,8 @@ int _engine_run() {
.running = 1, .running = 1,
}; };
load_game(); input_init();
world_clear();
start_game(); start_game();
timespec_get(&start_last_frame, TIME_UTC); timespec_get(&start_last_frame, TIME_UTC);
@ -58,10 +60,13 @@ int _engine_run() {
if(next_time.tv_nsec < start_last_frame.tv_nsec) _delta_time = 0; if(next_time.tv_nsec < start_last_frame.tv_nsec) _delta_time = 0;
start_last_frame = next_time; start_last_frame = next_time;
_handle_events(); _handle_events();
update_input();
_render_mode = 1; _render_mode = 1;
update_ui(); update_ui();
_render_mode = 0; _render_mode = 0;
update_objects(); // update world objects
update_game(); update_game();
draw_objects(); // draw world objects
swap_buffer(); swap_buffer();
} }
return 0; return 0;

View File

@ -1,6 +1,5 @@
#include "engine.h" #include "engine.h"
#include "world.h" #include "world.h"
#include "player.h"
#include "corelib/input.h" #include "corelib/input.h"
#include "corelib/render.h" #include "corelib/render.h"
#include "corelib/assets.h" #include "corelib/assets.h"
@ -8,83 +7,45 @@
#include "SDL2/SDL_mouse.h" #include "SDL2/SDL_mouse.h"
#include "SDL2/SDL_scancode.h" #include "SDL2/SDL_scancode.h"
int dragging = 0;
int drawing = 0;
spritesheet_t world_sheet;
void on_quit_key(int down) { void on_quit_key(int down) {
// Q is pressed or released
if(down) { if(down) {
// stop running when Q is pressed
g_context.running = 0; g_context.running = 0;
} }
} }
void on_horizontal(int axis) {
// when A or D is pressed or released
}
void on_vertical(int axis) {
// W or S is pressed or released
}
void on_click(int down) { void on_click(int down) {
drawing = down; // when the left mouse button is pressed down
} // float mouse_x, mouse_y;
// mouse_world_position(&mouse_x, &mouse_y);
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() {
world_sheet = make_spritesheet("tileset.png", 189, 189);
input_init();
world_clear();
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_Q, &on_quit_key);
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);
create_player();
} }
void start_game() { void start_game() {
// called when the game first run
g_active_view.width = 21; g_active_view.width = 21;
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_Q, &on_quit_key);
add_key_listener(SDL_SCANCODE_S, SDL_SCANCODE_W, &on_horizontal);
add_key_listener(SDL_SCANCODE_A, SDL_SCANCODE_D, &on_vertical);
add_mouse_button_listener(SDL_BUTTON_LMASK, &on_click);
} }
void update_game() { void update_game() {
if(drawing == 1) { // called every frame,
drawing = 2; // render calls made in this function will be in *world* space
}
update_objects();
draw_objects();
} }
void update_ui() { void update_ui() {
update_input(); // called every frame,
// render calls made in this function will be in *screen* space
} }
void game_exit() {} void game_exit() {}