From 0db9c91742a6ab17c3d8a8b86a34392c1797740b Mon Sep 17 00:00:00 2001 From: navewindre Date: Sun, 28 Sep 2025 02:45:10 +0200 Subject: input bettr --- src/game.cpp | 3 +- src/game/camera.h | 9 ++++ src/game/player.cpp | 126 ++++++++++++++++++++++++++++++++++++++-------------- src/game/player.h | 13 +++++- src/util/input.cpp | 11 +++-- src/util/input.h | 15 ++++++- 6 files changed, 137 insertions(+), 40 deletions(-) create mode 100644 src/game/camera.h (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index bd3c4c8..285f288 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -120,6 +120,7 @@ void game_main_loop( GAME_DATA* game ) { gl_beginframe( gl ); gl_2d_frect( gl2d, s_tl(), s_br(), { 0.f, 0.f, 0.f, 1.f } ); + player_input( game, objl->pl ); if( game->state.ingame ) { game_on_tick( game ); @@ -139,7 +140,7 @@ void game_main_loop( GAME_DATA* game ) { void game_on_tick( GAME_DATA* game ) { U64 tick = u_tick(); if( tick - game->state.last_tick > (U64)(TICK_INTERVAL * 10000) ) { - player_input( game, objl->pl ); + player_move( game, objl->pl ); game->state.last_tick = tick; } } diff --git a/src/game/camera.h b/src/game/camera.h new file mode 100644 index 0000000..b9fe402 --- /dev/null +++ b/src/game/camera.h @@ -0,0 +1,9 @@ +#pragma once + +#include "../util/vector.h" + +struct PLAYER_CAMERA { + VEC3 pos; + VEC3 rot; + F32 fov; +}; diff --git a/src/game/player.cpp b/src/game/player.cpp index 993292c..26c03af 100644 --- a/src/game/player.cpp +++ b/src/game/player.cpp @@ -2,6 +2,7 @@ #include "SDL_scancode.h" #include "objlist.h" #include "world/trace.h" +#include #include "player.h" F32 PLAYER_HULL_HEIGHT = 50.f; @@ -30,64 +31,123 @@ PLAYER* player_create( VEC3 origin, F32 yaw ) { return p; } -void player_input( GAME_DATA* game, PLAYER* p ) { - F32* yaw = &p->rot.y; - VEC3* pos = &p->pos; - +void capture_mouse( PLAYER* p ) { + F32* yaw = &p->input.cam.pos.y; + F32* pitch = &p->input.cam.pos.x; if( input.keys[SDL_SCANCODE_F1] ) { input_capture_mouse( false ); } if( input.mouse_captured ) { *yaw += input.mouse.pos_delta.x * input.mouse_sensitivity; - p->rot.x -= input.mouse.pos_delta.y * input.mouse_sensitivity; - - if( p->rot.x > 89.f ) p->rot.x = 89.f; - if( p->rot.x < -89.f ) p->rot.x = -89.f; - } + *pitch -= input.mouse.pos_delta.y * input.mouse_sensitivity; - if( !input.mouse_captured ) { - if( input.keys[(U8)'a'] ) - *yaw -= 360.f * TICK_INTERVAL * 0.5f; - if( input.keys[(U8)'d'] ) - *yaw += 360.f * TICK_INTERVAL * 0.5f; + if( *pitch > 89.f ) *pitch = 89.f; + if( *pitch < -89.f ) *pitch = -89.f; - if( input.keys[SDL_SCANCODE_UP] ) { - if( p->rot.x < 89.f ) - p->rot.x += 360.f * TICK_INTERVAL * 0.5f; - } - if( input.keys[SDL_SCANCODE_DOWN] ) { - if( p->rot.x > -89.f ) - p->rot.x -= 360.f * TICK_INTERVAL * 0.5f; - } + input_reset_mouse_accumulator(); } *yaw = remainderf( *yaw, 360.f ); + p->rot.x = *pitch; + p->rot.y = *yaw; +} +void capture_move_keys( PLAYER* p ) { + VEC2* move = &p->input.move; + if( input.keys[input.binds.fwd] ) { + if( !p->input.fwd_held ) + move->x = 1.f; + p->input.fwd_held = true; + } else { + if( p->input.fwd_held ) { + if( p->input.bk_held ) + move->x = -1.f; + else + move->x = 0.f; + } + p->input.fwd_held = false; + } + + if( input.keys[input.binds.back] ) { + if( !p->input.bk_held ) + move->x = -1.f; + p->input.bk_held = true; + } else { + if( p->input.bk_held ) { + if( p->input.fwd_held ) + move->x = 1.f; + else + move->x = 0.f; + } + p->input.bk_held = false; + } - VEC3 wishmove; - VEC3 dir = m_radial_offset( *yaw, 70.f ); - if( input.keys[(U8)'w'] ) { - wishmove = dir * TICK_INTERVAL; + if( input.keys[input.binds.left] ) { + if( !p->input.left_held ) + move->y = -1.f; + p->input.left_held = true; + } else { + if( p->input.left_held ) { + if( p->input.right_held ) + move->y = 1.f; + else + move->y = 0.f; + } + p->input.left_held = false; } - if( input.keys[(U8)'s'] ) { - wishmove = dir * -TICK_INTERVAL; + + if( input.keys[input.binds.right] ) { + if( !p->input.right_held ) + move->y = 1.f; + p->input.right_held = true; + } else { + if( p->input.right_held ) { + if( p->input.left_held ) + move->y = -1.f; + else + move->y = 0.f; + } + p->input.right_held = false; } +} + +void player_input( GAME_DATA* game, PLAYER* p ) { + if( !p ) + return; + + capture_mouse( p ); + capture_move_keys( p ); +} + +void player_move( GAME_DATA* game, PLAYER* p ) { + VEC2 move = p->input.move; + F32 yawrad = m_deg2rad( p->rot.y ); + + VEC3 wishdir = { + move.x * cosf( yawrad ) - move.y * sinf( yawrad ), + move.y * cosf( yawrad ) + move.x * sinf( yawrad ), + 0 + }; + VEC3 vel = wishdir * 70.f; + p->velocity = vel; + VEC3 wishmove = vel * TICK_INTERVAL; // todo : should never be false if( vec_len( wishmove ) > BSP_TRACE_EPSILON && game->state.map->bsp ) { BSP_TRACE tr{}; AABB aabb{}; - tr.in_start = *pos; - tr.in_end = *pos + wishmove * 2.f; + tr.in_start = p->pos; + tr.in_end = tr.in_start + wishmove; aabb.min = p->mins; aabb.max = p->maxs; bsp_trace( &tr, game->state.map->bsp, aabb ); if( !tr.hit ) - *pos += wishmove; - else - *pos += wishmove * tr.frac; + p->pos += wishmove; + else { + p->pos += wishmove * tr.frac; + } } } diff --git a/src/game/player.h b/src/game/player.h index 71b1c02..6617f01 100644 --- a/src/game/player.h +++ b/src/game/player.h @@ -1,8 +1,16 @@ #pragma once #include "object.h" +#include "camera.h" + struct PLAYER_INPUT { - + PLAYER_CAMERA cam; + struct PLAYER* pobj; + + // for nulls + U8 fwd_held{}, bk_held{}, left_held{}, right_held{}; + + VEC2 move; }; struct PLAYER : OBJECT { @@ -11,6 +19,8 @@ struct PLAYER : OBJECT { F32 fov{72.f}; F32 eyeoffset{40.f}; + PLAYER_INPUT input; + VEC3 mins; VEC3 maxs; @@ -19,4 +29,5 @@ struct PLAYER : OBJECT { extern PLAYER* player_create( VEC3 origin, F32 yaw ); extern void player_input( struct GAME_DATA* game, PLAYER* player ); +extern void player_move( struct GAME_DATA* game, PLAYER* player ); extern VEC3 player_get_view_pos( PLAYER* player ); diff --git a/src/util/input.cpp b/src/util/input.cpp index 7e94c47..225b696 100644 --- a/src/util/input.cpp +++ b/src/util/input.cpp @@ -27,8 +27,8 @@ void input_on_event( SDL_Event* e ) { case SDL_MOUSEMOTION: { input.mouse.pos.x = (F32)e->motion.x; input.mouse.pos.y = (F32)e->motion.y; - input.mouse.pos_delta.x = (F32)e->motion.xrel; - input.mouse.pos_delta.y = (F32)e->motion.yrel; + input.mouse.pos_delta.x += (F32)e->motion.xrel; + input.mouse.pos_delta.y += (F32)e->motion.yrel; } break; case SDL_KEYDOWN: { input.keys[e->key.keysym.sym & 0xff] = 1; @@ -39,6 +39,10 @@ void input_on_event( SDL_Event* e ) { } } +extern void input_reset_mouse_accumulator() { + input.mouse.pos_delta.x = 0; + input.mouse.pos_delta.y = 0; +} void input_on_mouse( I32 type, I32 x, I32 y ) { if( type == MOUSEEV_MOVE ) { @@ -73,11 +77,10 @@ void input_on_mouse( I32 type, I32 x, I32 y ) { void input_frame_end() { input.mouse.wheel = 0; - input.mouse.pos_delta.x = 0; - input.mouse.pos_delta.y = 0; } void input_capture_mouse( bool capture ) { + input_reset_mouse_accumulator(); input.mouse_captured = capture; SDL_SetRelativeMouseMode( capture ? SDL_TRUE : SDL_FALSE ); } diff --git a/src/util/input.h b/src/util/input.h index 4885f0f..ec901ab 100644 --- a/src/util/input.h +++ b/src/util/input.h @@ -23,15 +23,27 @@ struct MOUSE_DATA { U8 wheel; }; +struct INPUT_KEYBINDS { + U8 fwd = 'w'; + U8 back = 's'; + U8 left = 'a'; + U8 right = 'd'; + + U8 jump = ' '; + U8 crouch = SDL_SCANCODE_LCTRL; +}; + using ON_INPUT_FN = std::function; struct INPUT_DATA { MOUSE_DATA mouse; U8 keys[0xff]; bool mouse_captured; - F32 mouse_sensitivity = 1.0f; + F32 mouse_sensitivity = .3f; + INPUT_KEYBINDS binds; LIST on_input; + }; extern INPUT_DATA input; @@ -41,5 +53,6 @@ extern void input_on_event( SDL_Event* e ); extern void input_on_mouse( I32 type, I32 x, I32 y ); extern void input_is_key_down( U32 key ); extern void input_capture_mouse( bool capture ); +extern void input_reset_mouse_accumulator(); #define kb_down( key ) input_is_key_down( key ) -- cgit v1.2.3