From b7b6ce38395876589b1bcc3f08fd9a9cb9ef2545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 18 Jan 2020 11:10:05 +0100 Subject: [PATCH] Add mouse (horizontal) --- main.c | 21 ++++++++++++++++++++- platform_pokitto.h | 6 ++++++ platform_sdl.h | 25 ++++++++++++++++++++----- settings.h | 6 ++++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 301b346..b168b68 100755 --- a/main.c +++ b/main.c @@ -60,6 +60,14 @@ */ int8_t SFG_keyPressed(uint8_t key); +/** + Optinal function for mouse/analog controls, gets mouse x and y offset in + pixels from the game screen center (to achieve classic FPS mouse controls the + platform should center the mouse at the end). If the platform isn't using a + mouse, this function should simply return [0,0] offets at each call. +*/ +void SFG_getMouseOffset(int16_t *x, int16_t *y); + /** Returns time in ms sice program start. */ @@ -1745,7 +1753,6 @@ void SFG_gameStep() int8_t shearing = 0; #if SFG_PREVIEW_MODE == 0 - if ( SFG_keyIsDown(SFG_KEY_B) && !SFG_keyIsDown(SFG_KEY_C) && @@ -1915,6 +1922,18 @@ void SFG_gameStep() SFG_playerRotateWeapon(1); } + int16_t mouseX, mouseY; + + SFG_getMouseOffset(&mouseX,&mouseY); + + if (mouseX != 0 || mouseY != 0) + { + SFG_player.camera.direction += + (mouseX * SFG_MOUSE_SENSITIVITY_HORIZONTAL) / 128; + + recomputeDirection = 1; + } + if (recomputeDirection) SFG_recompurePLayerDirection(); diff --git a/platform_pokitto.h b/platform_pokitto.h index 666d150..3b90074 100644 --- a/platform_pokitto.h +++ b/platform_pokitto.h @@ -76,6 +76,12 @@ int8_t SFG_keyPressed(uint8_t key) } } +void SFG_getMouseOffset(int16_t *x, int16_t *y) +{ + *x = 0; + *y = 0; +} + int main() { pokitto.begin(); diff --git a/platform_sdl.h b/platform_sdl.h index 384372d..de34521 100644 --- a/platform_sdl.h +++ b/platform_sdl.h @@ -37,6 +37,11 @@ const uint8_t *sdlKeyboardState; uint16_t screen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y]; // RGB565 format +SDL_Window *window; +SDL_Renderer *renderer; +SDL_Texture *texture; +SDL_Surface *screenSurface; + void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex) { screen[y * SFG_SCREEN_RESOLUTION_X + x] = paletteRGB565[colorIndex]; @@ -54,6 +59,19 @@ void SFG_sleepMs(uint16_t timeMs) #endif } +void SFG_getMouseOffset(int16_t *x, int16_t *y) +{ + int mX, mY; + + SDL_GetMouseState(&mX,&mY); + + *x = mX - SFG_SCREEN_RESOLUTION_X / 2; + *y = mY - SFG_SCREEN_RESOLUTION_Y / 2; + + SDL_WarpMouseInWindow(window, + SFG_SCREEN_RESOLUTION_X / 2, SFG_SCREEN_RESOLUTION_Y / 2); +} + int8_t SFG_keyPressed(uint8_t key) { switch (key) @@ -117,11 +135,6 @@ int8_t SFG_keyPressed(uint8_t key) } } -SDL_Window *window; -SDL_Renderer *renderer; -SDL_Texture *texture; -SDL_Surface *screenSurface; - #ifdef __EMSCRIPTEN__ void mainLoopIteration() { @@ -204,6 +217,8 @@ int main(int argc, char *argv[]) sdlKeyboardState = SDL_GetKeyboardState(NULL); + SDL_ShowCursor(0); + SFG_init(); int running = 1; diff --git a/settings.h b/settings.h index b100c28..54a2201 100644 --- a/settings.h +++ b/settings.h @@ -27,6 +27,12 @@ */ #define SFG_FPS 60 +/** + On platforms with mouse this sets its horizontal sensitivity. 128 means 1 + RCL_Unit per mouse pixel travelled. +*/ +#define SFG_MOUSE_SENSITIVITY_HORIZONTAL 32 + /** Width of the screen in pixels. Set this to ACTUAL resolution. If you want the game to run at smaller resolution (with bigger pixels), do his using