diff options
Diffstat (limited to 'src/engine/sdl_linux.c')
-rw-r--r-- | src/engine/sdl_linux.c | 180 |
1 files changed, 105 insertions, 75 deletions
diff --git a/src/engine/sdl_linux.c b/src/engine/sdl_linux.c index 76851df..806b718 100644 --- a/src/engine/sdl_linux.c +++ b/src/engine/sdl_linux.c @@ -1,91 +1,101 @@ #include "GL/glew.h" -#include "SDL2/SDL.h" +#include "SDL3/SDL.h" #include "prge.h" #include "game.h" #include "game.c" -void process_mouse_pos(Input *input, S32 x, S32 y) +void process_mouse_pos(Input *input, SDL_MouseMotionEvent *mouseev) { - V2 pos = v2((F32)x, (F32)y); - if (input->first_mouse) { - input->mouse_pos = pos; + V2 offset; + + input->mouse_pos = v2((F32)mouseev->x, (F32)mouseev->y); + + if (input->first_mouse) input->first_mouse = 0; - } - input->last_mouse_pos = input->mouse_pos; - input->mouse_pos = pos; - input->mouse_offset = v2sub(input->mouse_pos, - input->last_mouse_pos); -} -void process_window_resize() -{ + offset = v2((F32)mouseev->xrel, (F32)mouseev->yrel); + input->mouse_offset = v2add(input->mouse_offset, offset); } -void process_key(U8 state, Key *key) +void process_key(B32 down, Key *key) { - key->state = (state == SDL_PRESSED) ? KeyState_PRESS : KeyState_RELEASE; + key->state = (down ? KeyState_PRESS : KeyState_RELEASE); } -void process_keyboard(Input *input, SDL_KeyboardEvent *key) +void process_keyboard(Input *input, SDL_KeyboardEvent *keyev) { - switch (key->keysym.scancode) { + switch (keyev->scancode) { case SDL_SCANCODE_D: - process_key(key->state, &input->move_right); + process_key(keyev->down, &input->move_right); break; case SDL_SCANCODE_W: - process_key(key->state, &input->move_forward); + process_key(keyev->down, &input->move_forward); break; case SDL_SCANCODE_A: - process_key(key->state, &input->move_left); + process_key(keyev->down, &input->move_left); break; case SDL_SCANCODE_S: - process_key(key->state, &input->move_backward); + process_key(keyev->down, &input->move_backward); break; case SDL_SCANCODE_E: - process_key(key->state, &input->move_up); + process_key(keyev->down, &input->move_up); break; case SDL_SCANCODE_Q: - process_key(key->state, &input->move_down); + process_key(keyev->down, &input->move_down); break; case SDL_SCANCODE_SPACE: - process_key(key->state, &input->jump); + process_key(keyev->down, &input->jump); break; case SDL_SCANCODE_RIGHT: - process_key(key->state, &input->action_right); + process_key(keyev->down, &input->action_right); break; case SDL_SCANCODE_UP: - process_key(key->state, &input->action_up); + process_key(keyev->down, &input->action_up); break; case SDL_SCANCODE_LEFT: - process_key(key->state, &input->action_left); + process_key(keyev->down, &input->action_left); break; case SDL_SCANCODE_DOWN: - process_key(key->state, &input->action_down); + process_key(keyev->down, &input->action_down); break; case SDL_SCANCODE_ESCAPE: - process_key(key->state, &input->exit); + process_key(keyev->down, &input->exit); break; default: break; } } -void handle_events(Input *input) +void process_mouse_buttons(Input *input, SDL_MouseButtonEvent *buttonev) { - SDL_Event event; + if (buttonev->button == SDL_BUTTON_LEFT) + process_key(buttonev->down, &input->mouse_left); + else if (buttonev->button == SDL_BUTTON_RIGHT) + process_key(buttonev->down, &input->mouse_right); +} + +void handle_events(Window *wnd, Input *input) +{ + SDL_Event event; + while (SDL_PollEvent(&event)) { switch (event.type) { - case SDL_KEYDOWN: - case SDL_KEYUP: + case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: process_keyboard(input, &event.key); break; - case SDL_MOUSEMOTION: - process_mouse_pos(input, event.motion.x, event.motion.y); + case SDL_EVENT_MOUSE_MOTION: + process_mouse_pos(input, &event.motion); + break; + case SDL_EVENT_MOUSE_BUTTON_DOWN: + case SDL_EVENT_MOUSE_BUTTON_UP: + process_mouse_buttons(input, &event.button); break; - case SDL_WINDOWEVENT_RESIZED: - process_window_resize(); + case SDL_EVENT_WINDOW_RESIZED: + wnd->width = event.window.data1; + wnd->height = event.window.data2; break; default: break; @@ -95,65 +105,73 @@ void handle_events(Input *input) F32 get_elapsed_seconds(U64 cur, U64 last) { - F32 result = ((F32)cur-(F32)last)/1000.0f; - return result; + F32 res; + res = ((F32)cur-(F32)last)/1000.0f; + return res; } F32 lock_framerate(U64 last, F32 target) { - F32 target_mspf = 1000.0f/target; + F32 target_mspf, elapsed, sleep, dt; - F32 elapsed = get_elapsed_seconds(SDL_GetTicks64(), last)*1000.0f; + elapsed = get_elapsed_seconds(SDL_GetTicks(), last)*1000.0f; + target_mspf = 1000.0f/target; if (elapsed < target_mspf) { - /* F32 sleep = target_mspf-elapsed-1.0f; */ - F32 sleep = target_mspf-elapsed; + sleep = target_mspf-elapsed; if (sleep > 0.0f) SDL_Delay(sleep); /* do { - elapsed = get_elapsed_seconds(SDL_GetTicks64(), last)*1000.0f; + elapsed = get_elapsed_seconds(SDL_GetTicks(), last)*1000.0f; } while (elapsed < target_mspf); */ } - F32 dt = get_elapsed_seconds(SDL_GetTicks64(), last); + dt = get_elapsed_seconds(SDL_GetTicks(), last); return dt; } -B32 sdl_init(S32 width, S32 height, - SDL_Window **window, - SDL_GLContext **context) +B32 sdl_init(Window *wnd, SDL_Window **sdl_window, SDL_GLContext *context) { - if (SDL_Init(SDL_INIT_VIDEO)) { + U32 flags; + GLenum error; + Arena *tmpar; + char *wname; + + if (!SDL_Init(SDL_INIT_VIDEO)) { SDL_Log("[ERROR] : SDL : %s\n", SDL_GetError()); return 0; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, - SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + + tmpar = arena_alloc(0); + wname = str8tocstr(tmpar, wnd->name); + + flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; - U32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; - (*window) = SDL_CreateWindow("prge game example", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - width, height, flags); + (*sdl_window) = SDL_CreateWindow(wname, wnd->width, wnd->height, flags); + + arena_release(tmpar); - if (!(*window)) { + if (!(*sdl_window)) { SDL_Log("[ERROR] : SDL : %s\n", SDL_GetError()); return 0; } - (*context) = SDL_GL_CreateContext((*window)); + SDL_GetWindowSize(*sdl_window, &wnd->width, &wnd->height); + + (*context) = SDL_GL_CreateContext((*sdl_window)); if (!(*context)) { SDL_Log("[ERROR] : SDL : %s\n", SDL_GetError()); return 0; } - GLenum error = glewInit(); + error = glewInit(); if (error != GLEW_OK) { SDL_Log("[ERROR] : GLEW : %s\n", glewGetErrorString(error)); return 0; @@ -165,39 +183,51 @@ B32 sdl_init(S32 width, S32 height, int main(void) { /* NOTE(pryazha): Engine init */ - Input input = input_init(); - S32 width = 800, height = 600; + Input input; + Window wnd; + + input = input_init(); + wnd = window_init(800, 600, str8lit("PRGE example")); - /* NOTE(pryazha): SDL init */ - SDL_Window *window; - SDL_GLContext *context; - if (!sdl_init(width, height, &window, &context)) { + /* NOTE(pryazha): SDL and OpenGL init */ + SDL_Window *sdl_window; + SDL_GLContext context; + U64 last; + + if (!sdl_init(&wnd, &sdl_window, &context)) { SDL_Quit(); return 1; } + glEnable(GL_DEPTH_TEST); + /* NOTE(pryazha): Game init */ - State state = {0}; - game_init(&state); + State state; + MemoryZero(&state, sizeof(State)); + + init(&state); + + last = SDL_GetTicks(); - U64 last = SDL_GetTicks64(); while (input.is_running) { - handle_events(&input); + SDL_SetWindowRelativeMouseMode(sdl_window, input.capture_mouse); + + handle_events(&wnd, &input); input.dt = lock_framerate(last, 60.0f); - last = SDL_GetTicks64(); + last = SDL_GetTicks(); - game_update_and_render(&state, &input); + update_and_render(&wnd, &state, &input); input_update(&input); - SDL_GL_SwapWindow(window); + SDL_GL_SwapWindow(sdl_window); } - game_clear(&state); + clear(&state); - SDL_GL_DeleteContext(context); - SDL_DestroyWindow(window); + SDL_GL_DestroyContext(context); + SDL_DestroyWindow(sdl_window); SDL_Quit(); return 0; } |