summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKasullian <tomkasull@gmail.com>2025-09-10 12:08:59 -0400
committerKasullian <tomkasull@gmail.com>2025-09-10 12:08:59 -0400
commitae694bc0da98e45c5def20ac1d92f9d8aad65fd5 (patch)
treefec648ec2ee3c7ab44ad555c837653ade8e7ac34 /src
parente2366afb57d69f3952bbb4d1894d82293cd1cb9d (diff)
3d view mouse control
Diffstat (limited to 'src')
-rw-r--r--src/editor/view3d.cpp21
-rw-r--r--src/game/player.cpp36
-rw-r--r--src/util/input.cpp9
-rw-r--r--src/util/input.h4
4 files changed, 59 insertions, 11 deletions
diff --git a/src/editor/view3d.cpp b/src/editor/view3d.cpp
index f9bd60e..61bcc3e 100644
--- a/src/editor/view3d.cpp
+++ b/src/editor/view3d.cpp
@@ -31,6 +31,16 @@ void gui_editor_3dview_draw_showpos( GUI_EDITOR_3DVIEW* view ) {
objl->pl->rot.y,
objl->pl->rot.x
);
+
+ if( input.mouse_captured ) {
+ gui_draw_str(
+ x + 2, y + 2,
+ ALIGN_L,
+ FNT_JPN12,
+ ui_clr.txt,
+ "[capturing mouse]"
+ );
+ }
}
void gui_editor_3dview_draw_fn( void* ptr ) {
@@ -60,9 +70,20 @@ void gui_editor_3dview_draw_fn( void* ptr ) {
}
void gui_editor_3dview_input_fn( void* ptr ) {
+ GUI_EDITOR_3DVIEW* view = (GUI_EDITOR_3DVIEW*)ptr;
+
if( !objl->pl )
return;
+ if( input.mouse.left && !input.mouse_captured ) {
+ I32 view_x = gui_relx( view );
+ I32 view_y = gui_rely( view ) + EDITORVIEW_TITLE_OFFSET;
+ if( input.mouse.pos.x >= view_x && input.mouse.pos.x < view_x + view->w &&
+ input.mouse.pos.y >= view_y && input.mouse.pos.y < view_y + view->h ) {
+ input_capture_mouse( true );
+ }
+ }
+
game_on_tick( editor->game );
}
diff --git a/src/game/player.cpp b/src/game/player.cpp
index 87786df..993292c 100644
--- a/src/game/player.cpp
+++ b/src/game/player.cpp
@@ -34,18 +34,32 @@ void player_input( GAME_DATA* game, PLAYER* p ) {
F32* yaw = &p->rot.y;
VEC3* pos = &p->pos;
- 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( 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_F1] ) {
+ input_capture_mouse( false );
}
- if( input.keys[SDL_SCANCODE_DOWN] ) {
- if( p->rot.x > -89.f )
- p->rot.x -= 360.f * TICK_INTERVAL * 0.5f;
+
+ 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;
+ }
+
+ 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( 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;
+ }
}
*yaw = remainderf( *yaw, 360.f );
diff --git a/src/util/input.cpp b/src/util/input.cpp
index 4a786aa..7e94c47 100644
--- a/src/util/input.cpp
+++ b/src/util/input.cpp
@@ -27,6 +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;
} break;
case SDL_KEYDOWN: {
input.keys[e->key.keysym.sym & 0xff] = 1;
@@ -71,4 +73,11 @@ 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.mouse_captured = capture;
+ SDL_SetRelativeMouseMode( capture ? SDL_TRUE : SDL_FALSE );
}
diff --git a/src/util/input.h b/src/util/input.h
index e174fe6..4885f0f 100644
--- a/src/util/input.h
+++ b/src/util/input.h
@@ -16,6 +16,7 @@ const U32 MOUSE_WHEEL = 0x4;
struct MOUSE_DATA {
VEC2 pos;
+ VEC2 pos_delta;
U8 left;
U8 right;
U8 middle;
@@ -27,6 +28,8 @@ using ON_INPUT_FN = std::function<void( SDL_Event* )>;
struct INPUT_DATA {
MOUSE_DATA mouse;
U8 keys[0xff];
+ bool mouse_captured;
+ F32 mouse_sensitivity = 1.0f;
LIST<ON_INPUT_FN> on_input;
};
@@ -37,5 +40,6 @@ extern void input_frame_end();
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 );
#define kb_down( key ) input_is_key_down( key )