Licar/game.h

390 lines
11 KiB
C
Raw Normal View History

2023-09-10 14:43:20 +02:00
/**
2023-09-16 22:52:03 +02:00
game: this file implements the backend of a complete, actually playable
game, and is meant to be included and used by specific frontends (which
2023-09-10 14:43:20 +02:00
will handle each platform's hardware details and I/O).
2024-09-06 00:58:32 +02:00
TODO: more documentation
UNITS: There are various kinds of units used to ensure independence of the
game modules. Here is a summary:
- LCR_GameUnit: data type, abstract unit of the game (racing module). One map
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.
2024-11-21 21:33:36 +01:00
Square height is only half of this.
2024-09-06 00:58:32 +02:00
- S3L_Unit: data type, small3dlib's unit. May change with renderer change.
2024-09-09 19:16:51 +02:00
- S3L_F: small3dlib's value representing 1.0.
2024-09-06 00:58:32 +02:00
- 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.
2024-09-09 19:16:51 +02:00
- TPE_F: tinyphysicsengine's value representing value 1.0.
2024-09-06 00:58:32 +02:00
- 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
independent of rendering and physics libraries, but out of convenient adopts
their coordinate system (X right, Y up, Z forward) and rotations (Euler
2024-09-10 15:30:07 +02:00
angles, by Z, then by X, then Y).
2023-09-10 14:43:20 +02:00
*/
2023-07-21 21:17:49 +02:00
#ifndef _LCR_GAME_H
#define _LCR_GAME_H
2023-09-10 14:43:20 +02:00
#define LCR_KEY_UP 0x00
#define LCR_KEY_RIGHT 0x01
#define LCR_KEY_DOWN 0x02
#define LCR_KEY_LEFT 0x03
#define LCR_KEY_A 0x04 ///< confirm, restart race
#define LCR_KEY_B 0x05 ///< cancel, open menu
#define LCR_KEYS_TOTAL 6
/*
FOR FRONTENDS:
- Implement the below described functions according to their description.
- Implement the main program and game loop.
- Call the below described functions as described.
*/
2023-08-08 20:34:21 +02:00
2023-09-10 14:43:20 +02:00
/**
Implement this in your frontend. Returns 1 if given key is pressed or 0
otherwise.
*/
2023-08-08 20:34:21 +02:00
uint8_t LCR_keyPressed(uint8_t key);
2023-09-10 14:43:20 +02:00
/**
Implement this in your frontend. This function pauses program execution for
given amount of milliseconds and relieves the CPU usage. On platforms that
don't support this the function may simply do nothing.
*/
2023-08-08 20:34:21 +02:00
void LCR_sleep(uint16_t timeMs);
2023-09-10 14:43:20 +02:00
/**
Implement this in your frontend. This function draws a pixel of given color
to the screen back buffer (i.e. NOT directly to screen, back buffer shall
only be copied to front buffer once the LCR_gameStep function finishes all
rendering). This function should NOT check for out-of-screen coordinates, this
is handled by the game internals and out-of-screen pixels will never be drawn.
The color value depends on game settings but is normally an RGB565 value.
*/
2024-07-22 01:16:16 +02:00
void LCR_drawPixel(unsigned long index, uint16_t color);
2023-08-08 20:34:21 +02:00
2024-08-02 00:05:03 +02:00
/**
Implement this in your frontend. This function will be called to log what the
program is doing. If you want to ignore logging, simply make the function do
nothing.
*/
void LCR_log(const char *str);
2023-08-08 20:34:21 +02:00
/**
2023-09-10 14:43:20 +02:00
Call this function in your frontend at the start of the program.
2023-08-08 20:34:21 +02:00
*/
void LCR_gameInit(void);
/**
2023-09-10 14:43:20 +02:00
Call this function in your frontend right before program end.
2023-08-08 20:34:21 +02:00
*/
void LCR_gameEnd(void);
/**
2023-09-10 14:43:20 +02:00
Call this function in your frontend repeatedly inside the main loop, pass the
current time as the number of milliseconds since program start. This function
will perform the game step AND other things such as checking the input states,
rendering or sleeping (all using the above functions you should implement).
Returns 0 if program should end, otherwise 1.
2023-08-08 20:34:21 +02:00
*/
uint8_t LCR_gameStep(uint32_t timeMs);
//------------------------------------------------------------------------------
2024-09-27 00:08:52 +02:00
#define LCR_LOG0(s) ;
#define LCR_LOG1(s) ;
#define LCR_LOG2(s) ;
#if LCR_SETTING_LOG_LEVEL > 0
#undef LCR_LOG0
#define LCR_LOG0(s) LCR_log(s);
#if LCR_SETTING_LOG_LEVEL > 1
#undef LCR_LOG1
#define LCR_LOG1(s) LCR_log(s);
#if LCR_SETTING_LOG_LEVEL > 2
#undef LCR_LOG2
#define LCR_LOG2(s) LCR_log(s);
#endif
#endif
#endif
2024-09-09 19:16:51 +02:00
#define LCR_CONTROL_MODE_FREECAM 0x00
#define LCR_CONTROL_MODE_DRIVE 0x01
2024-11-27 20:30:54 +01:00
#define LCR_GAME_STATE_MENU 0x00
#define LCR_GAME_STATE_RUN_STARTING 0x01
#define LCR_GAME_STATE_RUN 0x02
#define LCR_GAME_STATE_RUN_FINISHED 0x03
2024-09-05 22:51:11 +02:00
struct
{
2024-11-27 20:30:54 +01:00
uint8_t state;
uint32_t stateStartTime;
uint32_t time;
2024-09-05 22:51:11 +02:00
uint32_t nextRenderFrameTime;
uint32_t nextRacingTickTime;
2024-09-09 19:16:51 +02:00
uint8_t controlMode;
uint8_t debugDraw;
2024-11-24 21:21:29 +01:00
uint8_t checkpointsTaken;
2024-09-05 22:51:11 +02:00
} LCR_game;
2023-09-17 13:21:19 +02:00
2024-07-22 01:16:16 +02:00
void LCR_drawPixelXYUnsafe(unsigned int x, unsigned int y,
uint16_t color);
2023-09-10 14:43:20 +02:00
/**
2023-09-13 20:51:07 +02:00
Internal pixel drawing function that checks for out-of-screen coordinates. Use
this if the pixel can potentially lie of screen (however if you know it won't,
use the normal unsafe function in sake of performance).
2023-09-10 14:43:20 +02:00
*/
2024-07-22 01:16:16 +02:00
static inline void LCR_drawPixelXYSafe(unsigned int x, unsigned int y,
2023-09-13 20:51:07 +02:00
uint_fast16_t color);
2023-09-16 22:52:03 +02:00
#include "racing.h"
2023-09-13 20:51:07 +02:00
#include "assets.h"
#include "renderer.h"
2024-07-22 01:16:16 +02:00
uint8_t LCR_keyStates[LCR_KEYS_TOTAL]; /**< Assures unchanging key states
2024-09-09 19:16:51 +02:00
during a single frame, holds
number of frames for which the key
has been continuously held. */
2023-09-13 20:51:07 +02:00
2024-07-22 01:16:16 +02:00
void LCR_drawPixelXYUnsafe(unsigned int x, unsigned int y,
uint16_t color)
2023-09-10 14:43:20 +02:00
{
2023-09-11 20:56:04 +02:00
#if LCR_SETTING_RESOLUTION_SUBDIVIDE == 1
2024-07-22 01:16:16 +02:00
LCR_drawPixel(y * LCR_SETTING_RESOLUTION_X + x,color);
2023-09-11 20:56:04 +02:00
#else
2024-07-22 01:16:16 +02:00
// TODO
2023-09-11 20:56:04 +02:00
#endif
}
2024-07-22 01:16:16 +02:00
static inline void LCR_drawPixelXYSafe(unsigned int x, unsigned int y,
2023-09-11 20:56:04 +02:00
uint_fast16_t color)
{
if (x < LCR_EFFECTIVE_RESOLUTION_X && y < LCR_EFFECTIVE_RESOLUTION_Y)
2024-07-22 01:16:16 +02:00
LCR_drawPixelXYUnsafe(x,y,color);
2023-09-10 14:43:20 +02:00
}
2024-11-27 20:30:54 +01:00
void LCR_gameSetState(uint8_t state)
{
LCR_LOG1("changing state");
LCR_game.state = state;
LCR_game.stateStartTime = LCR_game.time;
}
2024-11-24 21:21:29 +01:00
void LCR_gameResetRun(void)
{
LCR_LOG0("resetting run");
LCR_game.checkpointsTaken = 0;
LCR_mapReset();
LCR_rendererUnmarkCPs();
2024-11-27 20:30:54 +01:00
LCR_gameSetState(LCR_GAME_STATE_RUN_STARTING);
}
//void LCR_gameStartRun(const uint8_t *map)
void LCR_gameStartRun(const char *mapStr)
{
LCR_mapLoadFromStr(mapStr);
LCR_rendererLoadMap();
LCR_gameResetRun();
LCR_racingRestart();
2024-11-24 21:21:29 +01:00
}
2023-08-08 20:34:21 +02:00
void LCR_gameInit(void)
{
2024-09-27 00:08:52 +02:00
LCR_LOG0("initializing");
2024-08-02 00:05:03 +02:00
2023-09-10 14:43:20 +02:00
for (int i = 0; i < LCR_KEYS_TOTAL; ++i)
2024-07-22 01:16:16 +02:00
LCR_keyStates[i] = 0;
2023-09-16 20:35:01 +02:00
LCR_rendererInit();
2024-09-04 23:26:05 +02:00
LCR_racingInit();
2024-09-05 22:51:11 +02:00
LCR_game.nextRenderFrameTime = 0;
LCR_game.nextRacingTickTime = 0;
2024-09-09 19:16:51 +02:00
LCR_game.controlMode = LCR_CONTROL_MODE_FREECAM;
LCR_game.debugDraw = 0;
2024-11-24 21:21:29 +01:00
2024-11-27 20:30:54 +01:00
LCR_gameStartRun(LCR_maps[0]);
2023-08-08 20:34:21 +02:00
}
void LCR_gameEnd(void)
{
2024-09-27 00:08:52 +02:00
LCR_LOG0("ending");
2023-08-08 20:34:21 +02:00
}
uint8_t LCR_gameStep(uint32_t time)
{
2024-09-27 00:08:52 +02:00
LCR_LOG2("game step start");
2024-11-27 20:30:54 +01:00
LCR_game.time = time;
2024-11-21 21:33:36 +01:00
LCR_GameUnit carTransform[6];
2023-09-10 14:43:20 +02:00
for (int i = 0; i < LCR_KEYS_TOTAL; ++i)
2024-09-09 19:16:51 +02:00
LCR_keyStates[i] = LCR_keyPressed(i) ?
(LCR_keyStates[i] < 255 ? LCR_keyStates[i] + 1 : 255) : 0;
2023-09-10 14:43:20 +02:00
2024-09-11 00:26:42 +02:00
//S3L_logTransform3D(LCR_renderer.scene.camera.transform);
2024-09-10 21:49:23 +02:00
if ((LCR_racing.tick % 32) == 0)
2024-09-11 00:26:42 +02:00
{
2024-09-10 21:49:23 +02:00
printf("speed: %d\n",LCR_racingGetCarSpeed());
2024-09-11 00:26:42 +02:00
}
2023-09-17 13:21:19 +02:00
uint32_t sleep = 0;
2024-09-09 19:16:51 +02:00
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)
2024-12-03 22:31:19 +01:00
LCR_racingRestart();
else if (LCR_keyStates[LCR_KEY_B] == 60)
2024-09-09 19:16:51 +02:00
LCR_game.debugDraw = !LCR_game.debugDraw;
2024-09-05 22:51:11 +02:00
while (time >= LCR_game.nextRacingTickTime)
2023-09-17 13:21:19 +02:00
{
2024-09-27 00:08:52 +02:00
LCR_LOG2("gonna step racing engine");
2024-09-05 22:51:11 +02:00
unsigned int input = 0;
2024-09-09 19:16:51 +02:00
if (LCR_game.controlMode != LCR_CONTROL_MODE_FREECAM)
2024-09-05 22:51:11 +02:00
input =
(LCR_keyStates[LCR_KEY_UP] ? LCR_RACING_INPUT_FORW : 0) |
(LCR_keyStates[LCR_KEY_RIGHT] ? LCR_RACING_INPUT_RIGHT : 0) |
(LCR_keyStates[LCR_KEY_DOWN] ? LCR_RACING_INPUT_BACK : 0) |
(LCR_keyStates[LCR_KEY_LEFT] ? LCR_RACING_INPUT_LEFT : 0);
LCR_racingStep(input);
2024-11-21 21:33:36 +01:00
LCR_racingGetCarTransform(carTransform,carTransform + 3,0);
carTransform[0] = (carTransform[0] + (LCR_GAME_UNIT * LCR_MAP_SIZE_BLOCKS)
/ 2) / LCR_GAME_UNIT;
carTransform[1] = (carTransform[1] + (LCR_GAME_UNIT * LCR_MAP_SIZE_BLOCKS)
/ 4) / (LCR_GAME_UNIT / 2);
carTransform[2] = (carTransform[2] + (LCR_GAME_UNIT * LCR_MAP_SIZE_BLOCKS)
/ 2) / LCR_GAME_UNIT;
2024-11-24 21:21:29 +01:00
int blockIndex =
LCR_mapGetBlockAt(carTransform[0],carTransform[1],carTransform[2]);
2024-11-21 21:33:36 +01:00
2024-11-24 21:21:29 +01:00
if (blockIndex >= 0)
2024-11-21 21:33:36 +01:00
{
2024-11-24 21:21:29 +01:00
if (LCR_currentMap.blocks[blockIndex * LCR_BLOCK_SIZE] ==
LCR_BLOCK_CHECKPOINT_0)
{
LCR_LOG1("CP taken");
LCR_currentMap.blocks[blockIndex * LCR_BLOCK_SIZE] =
LCR_BLOCK_CHECKPOINT_1;
LCR_rendererMarkTakenCP(
carTransform[0],carTransform[1],carTransform[2]);
LCR_game.checkpointsTaken++;
}
else if (LCR_game.checkpointsTaken == LCR_currentMap.checkpointCount &&
LCR_currentMap.blocks[blockIndex * LCR_BLOCK_SIZE] == LCR_BLOCK_FINISH)
{
LCR_LOG1("finished");
// TODO
}
2024-11-21 21:33:36 +01:00
}
2024-09-05 22:51:11 +02:00
LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS;
}
2024-09-04 23:26:05 +02:00
2024-09-10 21:49:23 +02:00
sleep = (3 * (LCR_game.nextRacingTickTime - time)) / 4;
2024-09-05 22:51:11 +02:00
if (time >= LCR_game.nextRenderFrameTime)
{
2024-09-27 00:08:52 +02:00
LCR_LOG2("gonna render next frame");
2024-09-09 19:16:51 +02:00
LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT -
((LCR_game.nextRacingTickTime - time) * LCR_GAME_UNIT) /
LCR_RACING_TICK_MS;
2024-09-04 23:26:05 +02:00
2024-09-09 19:16:51 +02:00
LCR_racingGetCarTransform(carTransform,carTransform + 3,
physicsInterpolationParam);
2024-09-10 15:30:07 +02:00
2024-09-09 19:16:51 +02:00
LCR_rendererSetCarTransform(carTransform,carTransform + 3);
2024-09-04 23:26:05 +02:00
2024-09-09 19:16:51 +02:00
while (time >= LCR_game.nextRenderFrameTime)
LCR_game.nextRenderFrameTime += 1000 / LCR_SETTING_FPS;
2023-09-17 13:21:19 +02:00
2024-09-04 23:26:05 +02:00
LCR_GameUnit offsets[5];
2023-09-17 13:21:19 +02:00
for (int i = 0; i < 5; ++i)
offsets[i] = 0;
2024-09-09 19:16:51 +02:00
if (LCR_game.controlMode == LCR_CONTROL_MODE_FREECAM)
2023-09-17 13:21:19 +02:00
{
2024-09-09 19:16:51 +02:00
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;
}
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);
2023-09-17 13:21:19 +02:00
}
2024-09-09 19:16:51 +02:00
else
LCR_rendererCameraFollow();
2024-09-10 15:30:07 +02:00
#if LCR_ANIMATE_CAR
LCR_rendererSetWheelState(LCR_racingGetWheelRotation(),
2024-09-20 15:22:18 +02:00
LCR_racingGetWheelSteer() * 2);
2024-09-10 15:30:07 +02:00
#endif
2024-09-09 19:16:51 +02:00
LCR_rendererDraw();
if (LCR_game.debugDraw)
2023-09-17 13:21:19 +02:00
{
2024-09-09 19:16:51 +02:00
LCR_GameUnit camTr[7];
LCR_rendererGetCameraTransform(camTr,camTr + 3,camTr + 6);
LCR_physicsDebugDraw(camTr,camTr + 3,camTr[6]);
2023-09-17 13:21:19 +02:00
}
}
else
2024-09-10 21:49:23 +02:00
{
uint32_t tmp = (3 * (LCR_game.nextRenderFrameTime - time)) / 4;
sleep = tmp < sleep ? tmp : sleep;
}
2023-09-17 13:21:19 +02:00
if (sleep)
LCR_sleep(sleep);
2023-09-10 14:43:20 +02:00
2024-09-27 00:08:52 +02:00
LCR_LOG2("game step end");
2023-09-10 14:43:20 +02:00
return 1;
2023-08-08 20:34:21 +02:00
}
2023-07-21 21:17:49 +02:00
#endif // guard