Add mouse (horizontal)

This commit is contained in:
Miloslav Číž 2020-01-18 11:10:05 +01:00
parent 88c9b2f226
commit b7b6ce3839
4 changed files with 52 additions and 6 deletions

21
main.c
View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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