Continue mouse

This commit is contained in:
Miloslav Číž 2020-01-18 12:27:20 +01:00
parent b7b6ce3839
commit 1b081bdefb
3 changed files with 78 additions and 22 deletions

37
main.c
View File

@ -34,8 +34,11 @@
#define SFG_KEY_STRAFE_LEFT 8
#define SFG_KEY_STRAFE_RIGHT 9
#define SFG_KEY_MAP 10
#define SFG_KEY_TOGGLE_FREELOOK 11
#define SFG_KEY_NEXT_WEAPON 12
#define SFG_KEY_PREVIOUS_WEAPON 13
#define SFG_KEY_COUNT 10 ///< Number of keys.
#define SFG_KEY_COUNT 14 ///< Number of keys.
/* ============================= PORTING =================================== */
@ -230,6 +233,10 @@ struct
uint32_t lastItemTakenFrame;
uint8_t ammo[SFG_AMMO_TOTAL];
uint8_t freeLook; /**< If on, the vertical looking (shear) does
not automatically shear back. This is mainly
for mouse control. */
} SFG_player;
RCL_RayConstraints SFG_rayConstraints;
@ -1090,6 +1097,8 @@ void SFG_init()
SFG_setAndInitLevel(&SFG_level0);
SFG_lastFrameTimeMs = SFG_getTimeMs();
SFG_player.freeLook = 0;
}
void SFG_getPlayerWeaponInfo(
@ -1099,9 +1108,13 @@ void SFG_getPlayerWeaponInfo(
*projectileCount = SFG_GET_WEAPON_PROJECTILE_COUNT(SFG_player.weapon);
#if SFG_INFINITE_AMMO
*canShoot = 1;
#else
*canShoot =
(*ammoType == SFG_AMMO_NONE ||
SFG_player.ammo[*ammoType] >= *projectileCount);
#endif
}
void SFG_playerRotateWeapon(uint8_t next)
@ -1752,6 +1765,9 @@ void SFG_gameStep()
int8_t shearing = 0;
if (SFG_keyJustPressed(SFG_KEY_TOGGLE_FREELOOK))
SFG_player.freeLook = !SFG_player.freeLook;
#if SFG_PREVIEW_MODE == 0
if (
SFG_keyIsDown(SFG_KEY_B) &&
@ -1865,6 +1881,11 @@ void SFG_gameStep()
} // attack
#endif // SFG_PREVIEW_MODE == 0
if (SFG_keyJustPressed(SFG_KEY_NEXT_WEAPON))
SFG_playerRotateWeapon(1);
else if (SFG_keyJustPressed(SFG_KEY_PREVIOUS_WEAPON))
SFG_playerRotateWeapon(0);
if (SFG_keyIsDown(SFG_KEY_A))
{
if (SFG_keyIsDown(SFG_KEY_UP))
@ -1893,9 +1914,9 @@ void SFG_gameStep()
}
else
{
if (SFG_keyJustPressed(SFG_KEY_LEFT) | SFG_keyJustPressed(SFG_KEY_A))
if (SFG_keyJustPressed(SFG_KEY_LEFT) || SFG_keyJustPressed(SFG_KEY_A))
SFG_playerRotateWeapon(0);
else if (SFG_keyJustPressed(SFG_KEY_RIGHT) | SFG_keyJustPressed(SFG_KEY_B))
else if (SFG_keyJustPressed(SFG_KEY_RIGHT) || SFG_keyJustPressed(SFG_KEY_B))
SFG_playerRotateWeapon(1);
}
}
@ -1931,6 +1952,14 @@ void SFG_gameStep()
SFG_player.camera.direction +=
(mouseX * SFG_MOUSE_SENSITIVITY_HORIZONTAL) / 128;
if (SFG_player.freeLook)
SFG_player.camera.shear =
RCL_max(RCL_min(
SFG_player.camera.shear - (mouseY * SFG_MOUSE_SENSITIVITY_VERTICAL)
/ 128,
SFG_CAMERA_MAX_SHEAR_PIXELS),
-1 * SFG_CAMERA_MAX_SHEAR_PIXELS);
recomputeDirection = 1;
}
@ -1986,7 +2015,7 @@ void SFG_gameStep()
(SFG_player.verticalSpeed - SFG_GRAVITY_SPEED_INCREASE_PER_FRAME);
#endif
if (!shearing && SFG_player.camera.shear != 0)
if (!shearing && SFG_player.camera.shear != 0 && !SFG_player.freeLook)
{
// gradually shear back to zero

View File

@ -35,6 +35,8 @@
const uint8_t *sdlKeyboardState;
int8_t mouseWheel;
uint16_t screen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y]; // RGB565 format
SDL_Window *window;
@ -106,7 +108,7 @@ int8_t SFG_keyPressed(uint8_t key)
break;
case SFG_KEY_B:
return sdlKeyboardState[SDL_SCANCODE_H];
return sdlKeyboardState[SDL_SCANCODE_H] || SDL_GetMouseState(0,0);
break;
case SFG_KEY_C:
@ -131,17 +133,43 @@ int8_t SFG_keyPressed(uint8_t key)
return sdlKeyboardState[SDL_SCANCODE_TAB];
break;
case SFG_KEY_TOGGLE_FREELOOK:
return sdlKeyboardState[SDL_SCANCODE_T];
break;
case SFG_KEY_NEXT_WEAPON:
return sdlKeyboardState[SDL_SCANCODE_M] || (mouseWheel > 0);
break;
case SFG_KEY_PREVIOUS_WEAPON:
return sdlKeyboardState[SDL_SCANCODE_N] || (mouseWheel < 0);
break;
default: return 0; break;
}
}
int running;
#ifdef __EMSCRIPTEN__
void mainLoopIteration()
{
SDL_PumpEvents(); // updates the keyboard state
mouseWheel = 0;
SDL_Event event;
while(SDL_PollEvent(&event))
if(event.type == SDL_MOUSEWHEEL)
{
if(event.wheel.y > 0)
mouseWheel = 1;
else
mouseWheel = -1;
}
if (sdlKeyboardState[SDL_SCANCODE_ESCAPE])
return;
running = 0;
SFG_mainLoopBody();
@ -152,6 +180,7 @@ void mainLoopIteration()
SDL_RenderPresent(renderer);
}
#ifdef __EMSCRIPTEN__
typedef void (*em_callback_func)(void);
void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop);
#endif
@ -221,26 +250,13 @@ int main(int argc, char *argv[])
SFG_init();
int running = 1;
running = 1;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(mainLoopIteration,0,1);
#else
while (running)
{
SDL_PumpEvents(); // updates the keyboard state
if (sdlKeyboardState[SDL_SCANCODE_ESCAPE])
break;
SFG_mainLoopBody();
SDL_UpdateTexture(texture,NULL,screen,SFG_SCREEN_RESOLUTION_X * sizeof(uint16_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
}
mainLoopIteration();
#endif
printf("SDL: freeing SDL\n");

View File

@ -33,6 +33,12 @@
*/
#define SFG_MOUSE_SENSITIVITY_HORIZONTAL 32
/**
Like SFG_MOUSE_SENSITIVITY_HORIZONTAL but for vertical look. 128 means 1
shear pixel per mouse pixel travelled.
*/
#define SFG_MOUSE_SENSITIVITY_VERTICAL 64
/**
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
@ -169,4 +175,9 @@
*/
#define SFG_BACKGROUND_BLUR 0
/**
Developer cheat for having infinite ammo in all weapons.
*/
#define SFG_INFINITE_AMMO 1
#endif // guard