sdl3: add mouse look

This commit is contained in:
erysdren 2024-10-13 09:52:49 -05:00
parent e6556e93fe
commit 35e58e9e13

View file

@ -16,6 +16,8 @@ static SDL_Texture *texture = NULL;
static SDL_Surface *front = NULL; static SDL_Surface *front = NULL;
static SDL_Surface *back = NULL; static SDL_Surface *back = NULL;
static bool focused = true;
static void die(const char *fmt, ...) static void die(const char *fmt, ...)
{ {
static char error[1024]; static char error[1024];
@ -42,6 +44,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv)
if (!window) if (!window)
die(SDL_GetError()); die(SDL_GetError());
SDL_SetWindowRelativeMouseMode(window, focused);
SDL_SetWindowMinimumSize(window, YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT); SDL_SetWindowMinimumSize(window, YETI_FRAMEBUFFER_WIDTH, YETI_FRAMEBUFFER_HEIGHT);
renderer = SDL_CreateRenderer(window, NULL); renderer = SDL_CreateRenderer(window, NULL);
@ -84,8 +88,8 @@ SDL_AppResult SDL_AppIterate(void *appstate)
{ {
const bool *keys = SDL_GetKeyboardState(NULL); const bool *keys = SDL_GetKeyboardState(NULL);
yeti.keyboard.state.up = keys[SDL_SCANCODE_UP]; yeti.keyboard.state.up = keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W];
yeti.keyboard.state.down = keys[SDL_SCANCODE_DOWN]; yeti.keyboard.state.down = keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S];
yeti.keyboard.state.left = keys[SDL_SCANCODE_LEFT]; yeti.keyboard.state.left = keys[SDL_SCANCODE_LEFT];
yeti.keyboard.state.right = keys[SDL_SCANCODE_RIGHT]; yeti.keyboard.state.right = keys[SDL_SCANCODE_RIGHT];
yeti.keyboard.state.a = keys[SDL_SCANCODE_RCTRL]; yeti.keyboard.state.a = keys[SDL_SCANCODE_RCTRL];
@ -114,6 +118,22 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{ {
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
return SDL_APP_SUCCESS; return SDL_APP_SUCCESS;
case SDL_EVENT_KEY_DOWN:
if (event->key.scancode == SDL_SCANCODE_ESCAPE)
{
focused = !focused;
SDL_SetWindowRelativeMouseMode(window, focused);
}
break;
case SDL_EVENT_MOUSE_MOTION:
if (focused)
{
yeti.camera->p += fl2f(event->motion.yrel);
yeti.camera->t += fl2f(event->motion.xrel);
}
break;
} }
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;