Migrated remaining event usage from platform to event subsystem.

dev
Shariq Shah 7 years ago
parent 4a7010d1c8
commit 23fae829fb
  1. 50
      src/game/event.c
  2. 78
      src/game/event.h
  3. 87
      src/game/gui.c
  4. 4
      src/game/gui.h
  5. 31
      src/game/input.c
  6. 12
      src/game/renderer.c
  7. 2
      src/system/main.c
  8. 131
      src/system/platform.c
  9. 14
      src/system/platform.h
  10. 5
      todo.txt

@ -148,46 +148,58 @@ void event_manager_poll_events(struct Event_Manager* event_manager, bool* out_qu
new_event->key.mod_shift = (event.key.keysym.mod & KMOD_SHIFT) ? true : false; new_event->key.mod_shift = (event.key.keysym.mod & KMOD_SHIFT) ? true : false;
new_event->key.mod_alt = (event.key.keysym.mod & KMOD_ALT) ? true : false; new_event->key.mod_alt = (event.key.keysym.mod & KMOD_ALT) ? true : false;
event_manager_send_event(event_manager, new_event); event_manager_send_event(event_manager, new_event);
//platform_state->on_keyboard_func(key, scancode, state, repeat, mod_ctrl, mod_shift, mod_alt);
//log_message("Key name : %s", SDL_GetKeyName(key)); //log_message("Key name : %s", SDL_GetKeyName(key));
break; break;
} }
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
{ {
int button = event.button.button; struct Event* new_event = event_manager_create_new_event(event_manager);
int state = event.button.state; new_event->type = event.type == SDL_MOUSEBUTTONDOWN ? EVT_MOUSEBUTTON_PRESSED : EVT_MOUSEBUTTON_RELEASED;
int num_clicks = event.button.clicks; new_event->mousebutton.button = event.button.button;
int x = event.button.x; new_event->mousebutton.state = event.button.state;
int y = event.button.y; new_event->mousebutton.num_clicks = event.button.clicks;
//platform_state->on_mousebutton_func(button, state, x, y, num_clicks); new_event->mousebutton.x = event.button.x;
new_event->mousebutton.y = event.button.y;
event_manager_send_event(event_manager, new_event);
break; break;
} }
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
int xrel = event.motion.xrel; struct Event* new_event = event_manager_create_new_event(event_manager);
int yrel = event.motion.yrel; new_event->type = EVT_MOUSEMOTION;
int x = event.motion.x; new_event->mousemotion.xrel = event.motion.xrel;
int y = event.motion.y; new_event->mousemotion.yrel = event.motion.yrel;
//platform_state->on_mousemotion_func(x, y, xrel, yrel); new_event->mousemotion.x = event.motion.x;
new_event->mousemotion.y = event.motion.y;
event_manager_send_event(event_manager, new_event);
break; break;
} }
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
{ {
int x = event.wheel.x; struct Event* new_event = event_manager_create_new_event(event_manager);
int y = event.wheel.y; new_event->type = EVT_MOUSEWHEEL;
//platform_state->on_mousewheel_func(x, y); new_event->mousewheel.x = event.wheel.x;
new_event->mousewheel.y = event.wheel.y;
event_manager_send_event(event_manager, new_event);
break; break;
} }
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
{ {
//platform_state->on_textinput_func(event.text.text); struct Event* new_event = event_manager_create_new_event(event_manager);
new_event->type = EVT_TEXT_INPUT;
memcpy(new_event->text_input.text, event.text.text, 32);
event_manager_send_event(event_manager, new_event);
break; break;
} }
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
{ {
if(event.window.event == SDL_WINDOWEVENT_RESIZED) if(event.window.event == SDL_WINDOWEVENT_RESIZED)
{ {
//platform_state->on_windowresize_func(event.window.data1, event.window.data2); struct Event* new_event = event_manager_create_new_event(event_manager);
new_event->type = EVT_WINDOW_RESIZED;
new_event->window_resize.width = event.window.data1;
new_event->window_resize.height = event.window.data2;
event_manager_send_event(event_manager, new_event);
} }
} }
break; break;
@ -216,8 +228,6 @@ void event_manager_poll_events(struct Event_Manager* event_manager, bool* out_qu
} }
} }
void event_manager_cleanup(struct Event_Manager* event_manager) void event_manager_cleanup(struct Event_Manager* event_manager)
{ {
@ -235,7 +245,9 @@ const char* event_name_get(int event_type)
case EVT_MOUSEBUTTON_PRESSED: return "Mousebutton Pressed"; case EVT_MOUSEBUTTON_PRESSED: return "Mousebutton Pressed";
case EVT_MOUSEBUTTON_RELEASED: return "Mousebutton Released"; case EVT_MOUSEBUTTON_RELEASED: return "Mousebutton Released";
case EVT_MOUSEMOTION: return "Mouse Motion"; case EVT_MOUSEMOTION: return "Mouse Motion";
case EVT_MOUSEWHEEL: return "Mouse Wheel";
case EVT_WINDOW_RESIZED: return "Window Resized"; case EVT_WINDOW_RESIZED: return "Window Resized";
case EVT_TEXT_INPUT: return "Text Input";
case EVT_MAX: return "Max Number of Events"; case EVT_MAX: return "Max Number of Events";
default: return "Invalid event_type"; default: return "Invalid event_type";
} }

@ -17,7 +17,9 @@ enum Event_Types
EVT_MOUSEBUTTON_PRESSED, EVT_MOUSEBUTTON_PRESSED,
EVT_MOUSEBUTTON_RELEASED, EVT_MOUSEBUTTON_RELEASED,
EVT_MOUSEMOTION, EVT_MOUSEMOTION,
EVT_MOUSEWHEEL,
EVT_WINDOW_RESIZED, EVT_WINDOW_RESIZED,
EVT_TEXT_INPUT,
EVT_MAX EVT_MAX
}; };
@ -32,11 +34,38 @@ struct Key_Event
bool mod_alt; bool mod_alt;
}; };
struct Player_Damage_Event struct Mousemotion_Event
{ {
int damage; int x;
int enemy; int y;
vec3 direction; int xrel;
int yrel;
};
struct Mousewheel_Event
{
int x;
int y;
};
struct Mousebutton_Event
{
int button;
int state;
int num_clicks;
int x;
int y;
};
struct Text_Input_Event
{
char text[32];
};
struct Window_Resized_Event
{
int width;
int height;
}; };
struct Event struct Event
@ -44,8 +73,12 @@ struct Event
int type; int type;
union union
{ {
struct Player_Damage_Event player_damage; struct Key_Event key;
struct Key_Event key; struct Mousewheel_Event mousewheel;
struct Mousebutton_Event mousebutton;
struct Mousemotion_Event mousemotion;
struct Text_Input_Event text_input;
struct Window_Resized_Event window_resize;
}; };
}; };
@ -62,39 +95,6 @@ struct Event_Manager
uint32 sdl_event_id; uint32 sdl_event_id;
}; };
////Event subsciption example
//void player_init()
//{
// struct Event_Manager* event_manager = game_state_get()->event_manager;
// event_manager_subscribe(event_manager, EVT_PLAYER_DAMAGE, &on_player_damage);
//}
//
////Event unsubscribe example
//void player_cleanup()
//{
// struct Event_Manager* event_manager = game_state_get()->event_manager;
// event_manager_unsubscribe(event_manager, EVT_PLAYER_DAMANGE, &on_player_damage);
//}
//
////Event recieve example usage
//void on_player_damage(struct Event* event_data)
//{
// struct Player_Damage_Event* player_damage_event = &event_data->player_damage;
// damage_player(player_damage_event->damage, player_damage_event->direction);
//}
//
////Event send example usage
//void enemy_tick()
//{
// struct Event_Manager* event_manager = game_state-get()->event_manager;
// struct Event* new_event = event_manager_create_new_event(event_manager);
// new_event->type = EVT_PLAYER_DAMAGE;
// new_event->player_damage.damage = 20;
// new_event->player_damage.enemy = enemy_id;
// event_manager_send_event(event_manager, new_event);
//}
//
void event_manager_init(struct Event_Manager* event_manager); void event_manager_init(struct Event_Manager* event_manager);
void event_manager_subscribe(struct Event_Manager* event_manager, int event_type, Event_Handler subscriber); void event_manager_subscribe(struct Event_Manager* event_manager, int event_type, Event_Handler subscriber);
void event_manager_unsubscribe(struct Event_Manager* event_manager, int event_type, Event_Handler subscriber); void event_manager_unsubscribe(struct Event_Manager* event_manager, int event_type, Event_Handler subscriber);

@ -10,6 +10,7 @@
#include "../common/string_utils.h" #include "../common/string_utils.h"
#include "../system/platform.h" #include "../system/platform.h"
#include "../system/file_io.h" #include "../system/file_io.h"
#include "event.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -23,9 +24,14 @@ struct Gui_Vertex
#define MAX_GUI_VERTEX_MEMORY 512 * 1024 #define MAX_GUI_VERTEX_MEMORY 512 * 1024
#define MAX_GUI_ELEMENT_MEMORY 128 * 1024 #define MAX_GUI_ELEMENT_MEMORY 128 * 1024
static void gui_handle_clipbard_copy(nk_handle usr, const char *text, int len); static void gui_on_clipbard_copy(nk_handle usr, const char *text, int len);
static void gui_handle_clipbard_paste(nk_handle usr, struct nk_text_edit *edit); static void gui_on_clipbard_paste(nk_handle usr, struct nk_text_edit *edit);
static void gui_handle_textinput_event(const char* text); static void gui_on_textinput(const struct Event* event);
static void gui_on_mousewheel(const struct Event* event);
static void gui_on_mousemotion(const struct Event* event);
static void gui_on_mousebutton(const struct Event* event);
static void gui_on_key(const struct Event* event);
static void gui_upload_atlas(const void *image, int width, int height); static void gui_upload_atlas(const void *image, int width, int height);
static void gui_font_set_default(void); static void gui_font_set_default(void);
@ -43,8 +49,8 @@ bool gui_init(void)
nk_init_default(&gui_state->context, 0); nk_init_default(&gui_state->context, 0);
nk_buffer_init_default(&gui_state->cmds); nk_buffer_init_default(&gui_state->cmds);
gui_state->context.clip.copy = gui_handle_clipbard_copy; gui_state->context.clip.copy = gui_on_clipbard_copy;
gui_state->context.clip.paste = gui_handle_clipbard_paste; gui_state->context.clip.paste = gui_on_clipbard_paste;
gui_state->context.clip.userdata = nk_handle_ptr(0); gui_state->context.clip.userdata = nk_handle_ptr(0);
gui_state->current_font = NULL; gui_state->current_font = NULL;
gui_state->shader = shader_create("gui.vert", "gui.frag"); gui_state->shader = shader_create("gui.vert", "gui.frag");
@ -63,9 +69,9 @@ bool gui_init(void)
{ {
/* buffer setup */ /* buffer setup */
GLsizei vs = sizeof(struct Gui_Vertex); GLsizei vs = sizeof(struct Gui_Vertex);
size_t vp = offsetof(struct Gui_Vertex, pos); size_t vp = offsetof(struct Gui_Vertex, pos);
size_t vt = offsetof(struct Gui_Vertex, uv); size_t vt = offsetof(struct Gui_Vertex, uv);
size_t vc = offsetof(struct Gui_Vertex, col); size_t vc = offsetof(struct Gui_Vertex, col);
glGenBuffers(1, &gui_state->vbo); glGenBuffers(1, &gui_state->vbo);
glGenBuffers(1, &gui_state->ebo); glGenBuffers(1, &gui_state->ebo);
@ -89,12 +95,21 @@ bool gui_init(void)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
platform_textinput_callback_set(&gui_handle_textinput_event);
//gui_font_set("Ubuntu-R.ttf", 14); //gui_font_set("Ubuntu-R.ttf", 14);
//gui_font_set("FiraSans-Regular.ttf", 14); //gui_font_set("FiraSans-Regular.ttf", 14);
gui_font_set("roboto_condensed.ttf", 18); gui_font_set("roboto_condensed.ttf", 18);
// gui_theme_set(GT_RED); // gui_theme_set(GT_RED);
gui_theme_set(GT_DEFAULT); gui_theme_set(GT_DEFAULT);
struct Event_Manager* event_manager = game_state_get()->event_manager;
event_manager_subscribe(event_manager, EVT_KEY_PRESSED, &gui_on_key);
event_manager_subscribe(event_manager, EVT_KEY_RELEASED, &gui_on_key);
event_manager_subscribe(event_manager, EVT_MOUSEBUTTON_PRESSED, &gui_on_mousebutton);
event_manager_subscribe(event_manager, EVT_MOUSEBUTTON_RELEASED, &gui_on_mousebutton);
event_manager_subscribe(event_manager, EVT_MOUSEMOTION, &gui_on_mousemotion);
event_manager_subscribe(event_manager, EVT_MOUSEWHEEL, &gui_on_mousewheel);
event_manager_subscribe(event_manager, EVT_TEXT_INPUT, &gui_on_textinput);
success = true; success = true;
return success; return success;
} }
@ -114,6 +129,14 @@ void gui_upload_atlas(const void *image, int width, int height)
void gui_cleanup(void) void gui_cleanup(void)
{ {
struct Event_Manager* event_manager = game_state_get()->event_manager;
event_manager_unsubscribe(event_manager, EVT_KEY_PRESSED, &gui_on_key);
event_manager_unsubscribe(event_manager, EVT_KEY_RELEASED, &gui_on_key);
event_manager_unsubscribe(event_manager, EVT_MOUSEBUTTON_PRESSED, &gui_on_mousebutton);
event_manager_unsubscribe(event_manager, EVT_MOUSEBUTTON_RELEASED, &gui_on_mousebutton);
event_manager_unsubscribe(event_manager, EVT_MOUSEMOTION, &gui_on_mousemotion);
event_manager_unsubscribe(event_manager, EVT_MOUSEWHEEL, &gui_on_mousewheel);
nk_font_atlas_clear(&gui_state->atlas); nk_font_atlas_clear(&gui_state->atlas);
nk_free(&gui_state->context); nk_free(&gui_state->context);
shader_remove(gui_state->shader); shader_remove(gui_state->shader);
@ -225,7 +248,7 @@ void gui_render(enum nk_anti_aliasing AA)
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
} }
void gui_handle_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) void gui_on_clipbard_paste(nk_handle usr, struct nk_text_edit *edit)
{ {
char *text = platform_clipboard_text_get(); char *text = platform_clipboard_text_get();
if(text) if(text)
@ -236,7 +259,7 @@ void gui_handle_clipbard_paste(nk_handle usr, struct nk_text_edit *edit)
(void)usr; (void)usr;
} }
void gui_handle_clipbard_copy(nk_handle usr, const char *text, int len) void gui_on_clipbard_copy(nk_handle usr, const char *text, int len)
{ {
char *str = 0; char *str = 0;
(void)usr; (void)usr;
@ -249,10 +272,15 @@ void gui_handle_clipbard_copy(nk_handle usr, const char *text, int len)
free(str); free(str);
} }
void gui_handle_keyboard_event(int key, int state, int mod_ctrl, int mod_shift) void gui_on_key(const struct Event* event)
{ {
struct nk_context *ctx = &gui_state->context; assert(event->type == EVT_KEY_PRESSED || event->type == EVT_KEY_RELEASED);
int down = (state == KS_PRESSED);
int key = event->key.key;
bool mod_ctrl = event->key.mod_ctrl;
struct nk_context* ctx = &gui_state->context;
int down = event->type == EVT_KEY_PRESSED ? 1 : 0;
if (key == KEY_RSHIFT || key == KEY_LSHIFT) if (key == KEY_RSHIFT || key == KEY_LSHIFT)
nk_input_key(ctx, NK_KEY_SHIFT, down); nk_input_key(ctx, NK_KEY_SHIFT, down);
@ -315,18 +343,29 @@ void gui_handle_keyboard_event(int key, int state, int mod_ctrl, int mod_shift)
} }
} }
void gui_handle_mousebutton_event(int button, int state, int x, int y) void gui_on_mousebutton(const struct Event* event)
{ {
int down = state == KS_PRESSED; assert(event->type == EVT_MOUSEBUTTON_PRESSED || event->type == EVT_MOUSEBUTTON_RELEASED);
struct nk_context *ctx = &gui_state->context;
int button = event->mousebutton.button;
int x = event->mousebutton.x;
int y = event->mousebutton.y;
int down = event->type == EVT_MOUSEBUTTON_PRESSED ? 1 : 0;
struct nk_context* ctx = &gui_state->context;
if(button == MSB_LEFT) nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); if(button == MSB_LEFT) nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
if(button == MSB_MIDDLE) nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); if(button == MSB_MIDDLE) nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
if(button == MSB_RIGHT) nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); if(button == MSB_RIGHT) nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
} }
void gui_handle_mousemotion_event(int x, int y, int xrel, int yrel) void gui_on_mousemotion(const struct Event* event)
{ {
struct nk_context *ctx = &gui_state->context; int x = event->mousemotion.x;
int y = event->mousemotion.y;
int xrel = event->mousemotion.xrel;
int yrel = event->mousemotion.yrel;
struct nk_context* ctx = &gui_state->context;
if(ctx->input.mouse.grabbed) if(ctx->input.mouse.grabbed)
{ {
int prev_x = (int)ctx->input.mouse.prev.x, prev_y = (int)ctx->input.mouse.prev.y; int prev_x = (int)ctx->input.mouse.prev.x, prev_y = (int)ctx->input.mouse.prev.y;
@ -338,17 +377,19 @@ void gui_handle_mousemotion_event(int x, int y, int xrel, int yrel)
} }
} }
void gui_handle_textinput_event(const char* text) void gui_on_textinput(const struct Event* event)
{ {
struct nk_context *ctx = &gui_state->context; struct nk_context *ctx = &gui_state->context;
nk_glyph glyph; nk_glyph glyph;
memcpy(glyph, text, NK_UTF_SIZE); memcpy(glyph, event->text_input.text, NK_UTF_SIZE);
nk_input_glyph(ctx, glyph); nk_input_glyph(ctx, glyph);
} }
void gui_handle_mousewheel_event(int x, int y) void gui_on_mousewheel(const struct Event* event)
{ {
struct nk_context *ctx = &gui_state->context; int x = event->mousewheel.x;
int y = event->mousewheel.y;
struct nk_context* ctx = &gui_state->context;
nk_input_scroll(ctx, nk_vec2(x, y)); nk_input_scroll(ctx, nk_vec2(x, y));
} }

@ -45,10 +45,6 @@ enum Gui_Theme
bool gui_init(void); bool gui_init(void);
void gui_cleanup(void); void gui_cleanup(void);
void gui_render(enum nk_anti_aliasing AA); void gui_render(enum nk_anti_aliasing AA);
void gui_handle_mousewheel_event(int x, int y);
void gui_handle_mousemotion_event(int x, int y, int xrel, int yrel);
void gui_handle_mousebutton_event(int button, int state, int x, int y);
void gui_handle_keyboard_event(int key, int state, int mod_ctrl, int mod_shift);
void gui_input_begin(void); void gui_input_begin(void);
void gui_input_end(void); void gui_input_end(void);
void gui_font_set(const char* font_name, float font_height); void gui_font_set(const char* font_name, float font_height);

@ -16,9 +16,6 @@
//static void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift, int mod_alt); //static void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift, int mod_alt);
static void input_on_key(const struct Event* event); static void input_on_key(const struct Event* event);
static void input_on_mousebutton(int button, int state, int x, int y, int8 num_clicks);
static void input_on_mousemotion(int x, int y, int xrel, int yrel);
static void input_on_mousewheel(int x, int y);
static struct Hashmap* key_bindings = NULL; static struct Hashmap* key_bindings = NULL;
@ -27,10 +24,6 @@ void input_init(void)
struct Event_Manager* event_manager = game_state_get()->event_manager; struct Event_Manager* event_manager = game_state_get()->event_manager;
event_manager_subscribe(event_manager, EVT_KEY_PRESSED, &input_on_key); event_manager_subscribe(event_manager, EVT_KEY_PRESSED, &input_on_key);
event_manager_subscribe(event_manager, EVT_KEY_RELEASED, &input_on_key); event_manager_subscribe(event_manager, EVT_KEY_RELEASED, &input_on_key);
//platform_keyboard_callback_set(&input_on_key);
platform_mousebutton_callback_set(&input_on_mousebutton);
platform_mousemotion_callback_set(&input_on_mousemotion);
platform_mousewheel_callback_set(&input_on_mousewheel);
key_bindings = hashmap_new(); key_bindings = hashmap_new();
@ -256,18 +249,6 @@ bool input_keybinds_save(const char* filename, int directory_type)
return write_success; return write_success;
} }
void input_on_mousemotion(int x, int y, int xrel, int yrel)
{
/* TODO: This is temporary. After proper event loop is added this code should not be here */
gui_handle_mousemotion_event(x, y, xrel, yrel);
}
void input_on_mousewheel(int x, int y)
{
/* TODO: This is temporary. After proper event loop is added this code should not be here */
gui_handle_mousewheel_event(x, y);
}
void input_mouse_pos_get(int* xpos, int* ypos) void input_mouse_pos_get(int* xpos, int* ypos)
{ {
assert(xpos && ypos); assert(xpos && ypos);
@ -313,18 +294,6 @@ void input_on_key(const struct Event* event)
break; break;
} }
} }
/* TODO: This is temporary. After proper event loop is added this code should not be here */
gui_handle_keyboard_event(key, state, mod_ctrl, mod_shift);
}
void input_on_mousebutton(int button, int state, int x, int y, int8 num_clicks)
{
/* Probably add 'mouse maps', same as input maps for keyboard but with buttons.
Do we even need that?
*/
/* TODO: This is temporary. After proper event loop is added this code should not be here */
gui_handle_mousebutton_event(button, state, x, y);
} }
void input_mouse_mode_set(enum Mouse_Mode mode) void input_mouse_mode_set(enum Mouse_Mode mode)

@ -24,13 +24,14 @@
#include "../system/platform.h" #include "../system/platform.h"
#include "../system/config_vars.h" #include "../system/config_vars.h"
#include "scene.h" #include "scene.h"
#include "event.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
void on_framebuffer_size_change(int width, int height); static void renderer_on_framebuffer_size_changed(const struct Event* event);
void renderer_init(struct Renderer* renderer) void renderer_init(struct Renderer* renderer)
{ {
@ -40,7 +41,7 @@ void renderer_init(struct Renderer* renderer)
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
platform_windowresize_callback_set(on_framebuffer_size_change); event_manager_subscribe(game_state_get()->event_manager, EVT_WINDOW_RESIZED, &renderer_on_framebuffer_size_changed);
struct Hashmap* cvars = config_vars_get(); struct Hashmap* cvars = config_vars_get();
renderer->settings.fog.mode = hashmap_int_get(cvars, "fog_mode"); renderer->settings.fog.mode = hashmap_int_get(cvars, "fog_mode");
@ -448,8 +449,11 @@ void renderer_cleanup(struct Renderer* renderer)
texture_remove(renderer->def_depth_tex); texture_remove(renderer->def_depth_tex);
} }
void on_framebuffer_size_change(int width, int height) void renderer_on_framebuffer_size_changed(const struct Event* event)
{ {
int width = event->window_resize.width;
int height = event->window_resize.height;
struct Scene* scene = game_state_get()->scene; struct Scene* scene = game_state_get()->scene;
float aspect = (float)width / (float)height; float aspect = (float)width / (float)height;
for(int i = 0; i < MAX_CAMERAS; i++) for(int i = 0; i < MAX_CAMERAS; i++)
@ -467,7 +471,7 @@ void renderer_clearcolor_set(float red, float green, float blue, float alpha)
glClearColor(red, green, blue, alpha); glClearColor(red, green, blue, alpha);
} }
struct Material * renderer_material_get(int material_type) struct Material* renderer_material_get(int material_type)
{ {
return NULL; return NULL;
} }

@ -18,7 +18,7 @@ void cleanup(void);
int main(int argc, char** args) int main(int argc, char** args)
{ {
if(!init(window)) if(!init())
{ {
log_to_stdout("ERR:(Main) Could not initialize"); log_to_stdout("ERR:(Main) Could not initialize");
} }

@ -18,19 +18,6 @@ struct Window
bool is_fullscreen; bool is_fullscreen;
}; };
struct Platform_State
{
Keyboard_Event_Func on_keyboard_func;
Mousebutton_Event_Func on_mousebutton_func;
Mousemotion_Event_Func on_mousemotion_func;
Mousewheel_Event_Func on_mousewheel_func;
Windowresize_Event_Func on_windowresize_func;
Textinput_Event_Func on_textinput_func;
};
/* TODO: Find a better way to handle internal state */
static struct Platform_State* platform_state = NULL;
struct Window* window_create(const char* title, int width, int height, int msaa, int msaa_levels) struct Window* window_create(const char* title, int width, int height, int msaa, int msaa_levels)
{ {
struct Window* new_window = NULL; struct Window* new_window = NULL;
@ -183,25 +170,7 @@ bool platform_init(void)
if(SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL Init failed", SDL_GetError(), NULL) != 0) if(SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "SDL Init failed", SDL_GetError(), NULL) != 0)
log_to_stdout("platform_init", "SDL Init failed : %s", SDL_GetError()); log_to_stdout("platform_init", "SDL Init failed : %s", SDL_GetError());
} }
else
{
platform_state = malloc(sizeof(*platform_state));
if(!platform_state)
{
if(SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Allocation Failure", "Memory allocation failed, out of memory!", NULL) != 0)
log_to_stdout("platform_init", "Could not create platform state, out of memory");
success = false;
}
else
{
platform_state->on_keyboard_func = NULL;
platform_state->on_mousebutton_func = NULL;
platform_state->on_mousemotion_func = NULL;
platform_state->on_mousewheel_func = NULL;
platform_state->on_textinput_func = NULL;
platform_state->on_windowresize_func = NULL;
}
}
return success; return success;
} }
@ -234,108 +203,10 @@ bool platform_init_video()
void platform_cleanup(void) void platform_cleanup(void)
{ {
if(platform_state) free(platform_state);
platform_state = NULL;
SDL_VideoQuit(); SDL_VideoQuit();
SDL_Quit(); SDL_Quit();
} }
void platform_poll_events(bool* out_quit)
{
static SDL_Event event;
while(SDL_PollEvent(&event) != 0)
{
switch(event.type)
{
case SDL_QUIT:
*out_quit = 1;
break;
case SDL_KEYDOWN: case SDL_KEYUP:
{
int scancode = event.key.keysym.scancode;
int key = event.key.keysym.sym;
int state = event.key.state;
int repeat = event.key.repeat;
int mod_ctrl = (event.key.keysym.mod & KMOD_CTRL) ? 1 : 0;
int mod_shift = (event.key.keysym.mod & KMOD_SHIFT) ? 1 : 0;
int mod_alt = (event.key.keysym.mod & KMOD_ALT) ? 1 : 0;
platform_state->on_keyboard_func(key, scancode, state, repeat, mod_ctrl, mod_shift, mod_alt);
//log_message("Key name : %s", SDL_GetKeyName(key));
break;
}
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
{
int button = event.button.button;
int state = event.button.state;
int num_clicks = event.button.clicks;
int x = event.button.x;
int y = event.button.y;
platform_state->on_mousebutton_func(button, state, x, y, num_clicks);
break;
}
case SDL_MOUSEMOTION:
{
int xrel = event.motion.xrel;
int yrel = event.motion.yrel;
int x = event.motion.x;
int y = event.motion.y;
platform_state->on_mousemotion_func(x, y, xrel, yrel);
break;
}
case SDL_MOUSEWHEEL:
{
int x = event.wheel.x;
int y = event.wheel.y;
platform_state->on_mousewheel_func(x, y);
break;
}
case SDL_TEXTINPUT:
{
platform_state->on_textinput_func(event.text.text);
break;
}
case SDL_WINDOWEVENT:
{
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
{
platform_state->on_windowresize_func(event.window.data1, event.window.data2);
}
}
break;
}
}
}
void platform_keyboard_callback_set(Keyboard_Event_Func func)
{
platform_state->on_keyboard_func = func;
}
void platform_mousebutton_callback_set(Mousebutton_Event_Func func)
{
platform_state->on_mousebutton_func = func;
}
void platform_mousemotion_callback_set(Mousemotion_Event_Func func)
{
platform_state->on_mousemotion_func = func;
}
void platform_mousewheel_callback_set(Mousewheel_Event_Func func)
{
platform_state->on_mousewheel_func = func;
}
void platform_textinput_callback_set(Textinput_Event_Func func)
{
platform_state->on_textinput_func = func;
}
void platform_windowresize_callback_set(Windowresize_Event_Func func)
{
platform_state->on_windowresize_func = func;
}
int platform_is_key_pressed(int key) int platform_is_key_pressed(int key)
{ {
/* Returns 1 if key is pressed, 0 otherwise */ /* Returns 1 if key is pressed, 0 otherwise */

@ -4,13 +4,6 @@
#include <stdbool.h> #include <stdbool.h>
#include "../common/num_types.h" #include "../common/num_types.h"
typedef void(*Keyboard_Event_Func) (int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift, int mod_alt);
typedef void(*Mousebutton_Event_Func) (int button, int state, int x, int y, int8 num_clicks);
typedef void(*Mousemotion_Event_Func) (int x, int y, int xrel, int yrel);
typedef void(*Mousewheel_Event_Func) (int x, int y);
typedef void(*Windowresize_Event_Func) (int x, int y);
typedef void(*Textinput_Event_Func) (const char* text);
enum Video_Drivers_Linux enum Video_Drivers_Linux
{ {
VD_X11 = 0, VD_X11 = 0,
@ -37,13 +30,6 @@ bool window_fullscreen_set(struct Window* window, bool fullscreen);
bool platform_init(void); bool platform_init(void);
bool platform_init_video(void); bool platform_init_video(void);
void platform_cleanup(void); void platform_cleanup(void);
void platform_poll_events(bool *out_quit);
void platform_keyboard_callback_set(Keyboard_Event_Func func);
void platform_mousebutton_callback_set(Mousebutton_Event_Func func);
void platform_mousemotion_callback_set(Mousemotion_Event_Func func);
void platform_mousewheel_callback_set(Mousewheel_Event_Func func);
void platform_windowresize_callback_set(Windowresize_Event_Func func);
void platform_textinput_callback_set(Textinput_Event_Func func);
int platform_is_key_pressed(int key); int platform_is_key_pressed(int key);
int platform_mousebutton_state_get(uint button); int platform_mousebutton_state_get(uint button);
void platform_mouse_position_get(int* x, int* y); void platform_mouse_position_get(int* x, int* y);

@ -51,15 +51,15 @@ Todo:
- Validate necessary assets at game launch - Validate necessary assets at game launch
- Gamma correctness - Gamma correctness
- Log and debug/stats output in gui - Log and debug/stats output in gui
- Event Subsystem
- Textual/Binary format for data serialization and persistance - Textual/Binary format for data serialization and persistance
- Array based string type comptible with cstring(char*) - Array based string type comptible with cstring(char*)
- Reduce fps on window focus loss or minimization
- ??? - ???
- Profit! - Profit!
Improvements: Improvements:
- Better naming semantics for examples, init/deinit for initialization and cleanup and create/destroy when memory is allocated or deallocated - Better naming semantics for examples, init/deinit for initialization and cleanup and create/destroy when memory is allocated or deallocated
- Reset mouse cursor position to the center of the screen in editor mode after the right click is released
Bugs: Bugs:
- Better handling of wav format checking at load time - Better handling of wav format checking at load time
@ -195,3 +195,4 @@ Done:
* Fixed Console bug when enabled in editor mode * Fixed Console bug when enabled in editor mode
* Migrated from bitbucket to github and from mercurial back to git * Migrated from bitbucket to github and from mercurial back to git
* Removed the game executable and game library split. * Removed the game executable and game library split.
* Event Subsystem

Loading…
Cancel
Save