Add animation smoothing

This commit is contained in:
Miloslav Ciz 2024-09-09 19:16:51 +02:00
parent e3d0b3219f
commit 03b703d454
5 changed files with 326 additions and 182 deletions

106
game.h
View file

@ -12,10 +12,10 @@
square is LCR_GAME_UNITs long, a full angle is also LCR_GAME_UNITs.
- LCR_GAME_UNIT: Size of one game square and full angle in LCR_GameUnits.
- S3L_Unit: data type, small3dlib's unit. May change with renderer change.
- S3L_FRACTIONS_PER_UNIT: small3dlib's value representing 1.0.
- S3L_F: small3dlib's value representing 1.0.
- LCR_RENDERER_UNIT: for the renderer one map square is this many S3L_Units.
- TPE_Unit: tinyphysicsengine's unit. May change with phys. engine change.
- TPE_FRACTIONS_PER_UNIT: tinyphysicsengine's value representing value 1.0.
- TPE_F: tinyphysicsengine's value representing value 1.0.
- LCR_PHYSICS_UNIT: for the phys. eng. one map square is this many TPE_Units.
COORDINATE SYSTEM AND ROTATIONS: The game itself (racing module) is
@ -96,10 +96,15 @@ uint8_t LCR_gameStep(uint32_t timeMs);
//------------------------------------------------------------------------------
#define LCR_CONTROL_MODE_FREECAM 0x00
#define LCR_CONTROL_MODE_DRIVE 0x01
struct
{
uint32_t nextRenderFrameTime;
uint32_t nextRacingTickTime;
uint8_t controlMode;
uint8_t debugDraw;
} LCR_game;
void LCR_drawPixelXYUnsafe(unsigned int x, unsigned int y,
@ -118,7 +123,9 @@ static inline void LCR_drawPixelXYSafe(unsigned int x, unsigned int y,
#include "renderer.h"
uint8_t LCR_keyStates[LCR_KEYS_TOTAL]; /**< Assures unchanging key states
during a single frame. */
during a single frame, holds
number of frames for which the key
has been continuously held. */
void LCR_drawPixelXYUnsafe(unsigned int x, unsigned int y,
uint16_t color)
@ -150,6 +157,9 @@ void LCR_gameInit(void)
LCR_game.nextRenderFrameTime = 0;
LCR_game.nextRacingTickTime = 0;
LCR_game.controlMode = LCR_CONTROL_MODE_FREECAM;
LCR_game.debugDraw = 0;
}
void LCR_gameEnd(void)
@ -159,15 +169,22 @@ void LCR_gameEnd(void)
uint8_t LCR_gameStep(uint32_t time)
{
for (int i = 0; i < LCR_KEYS_TOTAL; ++i)
LCR_keyStates[i] = LCR_keyPressed(i);
LCR_keyStates[i] = LCR_keyPressed(i) ?
(LCR_keyStates[i] < 255 ? LCR_keyStates[i] + 1 : 255) : 0;
uint32_t sleep = 0;
if (LCR_keyStates[LCR_KEY_B] == 1)
LCR_game.controlMode = LCR_game.controlMode == LCR_CONTROL_MODE_FREECAM ?
LCR_CONTROL_MODE_DRIVE : LCR_CONTROL_MODE_FREECAM;
else if (LCR_keyStates[LCR_KEY_B] == 30)
LCR_game.debugDraw = !LCR_game.debugDraw;
while (time >= LCR_game.nextRacingTickTime)
{
unsigned int input = 0;
if (LCR_keyStates[LCR_KEY_B])
if (LCR_game.controlMode != LCR_CONTROL_MODE_FREECAM)
input =
(LCR_keyStates[LCR_KEY_UP] ? LCR_RACING_INPUT_FORW : 0) |
(LCR_keyStates[LCR_KEY_RIGHT] ? LCR_RACING_INPUT_RIGHT : 0) |
@ -178,56 +195,67 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS;
}
if (time >= LCR_game.nextRenderFrameTime)
{
LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT -
((LCR_game.nextRacingTickTime - time) * LCR_GAME_UNIT) /
LCR_RACING_TICK_MS;
LCR_GameUnit carTransform[6];
LCR_racingGetCarTransform(carTransform,carTransform + 3);
LCR_racingGetCarTransform(carTransform,carTransform + 3,
physicsInterpolationParam);
LCR_rendererSetCarTransform(carTransform,carTransform + 3);
LCR_rendererDraw();
LCR_GameUnit sss[7];
LCR_rendererGetCameraTransform(sss,sss + 3,sss + 6);
LCR_physicsDebugDraw(sss,sss + 3,sss[6]);
LCR_game.nextRenderFrameTime += 1000 / LCR_SETTING_FPS;
while (time >= LCR_game.nextRenderFrameTime)
LCR_game.nextRenderFrameTime += 1000 / LCR_SETTING_FPS;
LCR_GameUnit offsets[5];
for (int i = 0; i < 5; ++i)
offsets[i] = 0;
if (LCR_keyStates[LCR_KEY_A])
if (LCR_game.controlMode == LCR_CONTROL_MODE_FREECAM)
{
if (LCR_keyStates[LCR_KEY_UP])
offsets[4] = LCR_FREE_CAMERA_TURN_STEP;
else if (LCR_keyStates[LCR_KEY_DOWN])
offsets[4] -= LCR_FREE_CAMERA_TURN_STEP;
if (LCR_keyStates[LCR_KEY_A])
{
if (LCR_keyStates[LCR_KEY_UP])
offsets[4] = LCR_FREE_CAMERA_TURN_STEP;
else if (LCR_keyStates[LCR_KEY_DOWN])
offsets[4] -= LCR_FREE_CAMERA_TURN_STEP;
if (LCR_keyStates[LCR_KEY_RIGHT])
offsets[3] -= LCR_FREE_CAMERA_TURN_STEP;
else if (LCR_keyStates[LCR_KEY_LEFT])
offsets[3] = LCR_FREE_CAMERA_TURN_STEP;
if (LCR_keyStates[LCR_KEY_RIGHT])
offsets[3] -= LCR_FREE_CAMERA_TURN_STEP;
else if (LCR_keyStates[LCR_KEY_LEFT])
offsets[3] = LCR_FREE_CAMERA_TURN_STEP;
}
else
{
if (LCR_keyStates[LCR_KEY_UP])
offsets[0] = LCR_FREE_CAMERA_STEP;
else if (LCR_keyStates[LCR_KEY_DOWN])
offsets[0] -= LCR_FREE_CAMERA_STEP;
if (LCR_keyStates[LCR_KEY_RIGHT])
offsets[1] = LCR_FREE_CAMERA_STEP;
else if (LCR_keyStates[LCR_KEY_LEFT])
offsets[1] -= LCR_FREE_CAMERA_STEP;
}
LCR_rendererMoveCamera(offsets,offsets + 3);
}
else if (!LCR_keyStates[LCR_KEY_B])
else
LCR_rendererCameraFollow();
LCR_rendererDraw();
if (LCR_game.debugDraw)
{
if (LCR_keyStates[LCR_KEY_UP])
offsets[0] = LCR_FREE_CAMERA_STEP;
else if (LCR_keyStates[LCR_KEY_DOWN])
offsets[0] -= LCR_FREE_CAMERA_STEP;
if (LCR_keyStates[LCR_KEY_RIGHT])
offsets[1] = LCR_FREE_CAMERA_STEP;
else if (LCR_keyStates[LCR_KEY_LEFT])
offsets[1] -= LCR_FREE_CAMERA_STEP;
LCR_GameUnit camTr[7];
LCR_rendererGetCameraTransform(camTr,camTr + 3,camTr + 6);
LCR_physicsDebugDraw(camTr,camTr + 3,camTr[6]);
}
LCR_rendererMoveCamera(offsets,offsets + 3);
}
else
sleep = LCR_game.nextRenderFrameTime - time;