diff --git a/frontend_sdl.c b/frontend_sdl.c index cdbaa3d..99346f2 100644 --- a/frontend_sdl.c +++ b/frontend_sdl.c @@ -1,6 +1,8 @@ #include #include +#define LCR_SETTING_LOG_LEVEL 5 + #include "game.h" #include "debug.h" @@ -67,12 +69,30 @@ int main(int argc, char *argv[]) SDL_WINDOWPOS_UNDEFINED, LCR_SETTING_RESOLUTION_X, LCR_SETTING_RESOLUTION_Y, SDL_WINDOW_SHOWN); + if (!window) + { + fputs("ERROR: couldn't create window",stderr); + return 1; + } + renderer = SDL_CreateRenderer(window,-1,0); + if (!renderer) + { + fputs("ERROR: couldn't create renderer",stderr); + return 1; + } + texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB565,SDL_TEXTUREACCESS_STATIC, LCR_SETTING_RESOLUTION_X,LCR_SETTING_RESOLUTION_Y); + if (!texture) + { + fputs("ERROR: couldn't create texture",stderr); + return 1; + } + screenSurface = SDL_GetWindowSurface(window); SDL_ShowCursor(0); diff --git a/game.h b/game.h index 3ad4f74..107a6d2 100644 --- a/game.h +++ b/game.h @@ -94,6 +94,25 @@ uint8_t LCR_gameStep(uint32_t timeMs); //------------------------------------------------------------------------------ +#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 + #define LCR_CONTROL_MODE_FREECAM 0x00 #define LCR_CONTROL_MODE_DRIVE 0x01 @@ -144,7 +163,7 @@ static inline void LCR_drawPixelXYSafe(unsigned int x, unsigned int y, void LCR_gameInit(void) { - LCR_log("initializing"); + LCR_LOG0("initializing"); for (int i = 0; i < LCR_KEYS_TOTAL; ++i) LCR_keyStates[i] = 0; @@ -162,10 +181,13 @@ void LCR_gameInit(void) void LCR_gameEnd(void) { + LCR_LOG0("ending"); } uint8_t LCR_gameStep(uint32_t time) { + LCR_LOG2("game step start"); + for (int i = 0; i < LCR_KEYS_TOTAL; ++i) LCR_keyStates[i] = LCR_keyPressed(i) ? (LCR_keyStates[i] < 255 ? LCR_keyStates[i] + 1 : 255) : 0; @@ -188,6 +210,7 @@ if ((LCR_racing.tick % 32) == 0) while (time >= LCR_game.nextRacingTickTime) { + LCR_LOG2("gonna step racing engine"); unsigned int input = 0; if (LCR_game.controlMode != LCR_CONTROL_MODE_FREECAM) @@ -205,6 +228,8 @@ if ((LCR_racing.tick % 32) == 0) if (time >= LCR_game.nextRenderFrameTime) { + LCR_LOG2("gonna render next frame"); + LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT - ((LCR_game.nextRacingTickTime - time) * LCR_GAME_UNIT) / LCR_RACING_TICK_MS; @@ -278,6 +303,8 @@ LCR_GameUnit physicsInterpolationParam = LCR_GAME_UNIT - if (sleep) LCR_sleep(sleep); + LCR_LOG2("game step end"); + return 1; } diff --git a/racing.h b/racing.h index 06be891..a204abb 100644 --- a/racing.h +++ b/racing.h @@ -246,7 +246,8 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist) } else { -printf("oof\n"); + LCR_LOG1("collision checking all blocks (shouldn't happen often!)"); + const uint8_t *block = LCR_currentMap.blocks; /* Full check of all map blocks, slow, shouldn't happen often! */ @@ -299,6 +300,8 @@ LCR_GameUnit _LCR_racingSmoothRot(LCR_GameUnit angleNew, LCR_GameUnit angleOld) */ void LCR_racingRestart(void) { + LCR_LOG0("restarting race"); + LCR_racing.tick = 0; TPE_bodyActivate(&(LCR_racing.carBody)); @@ -320,7 +323,7 @@ void LCR_racingRestart(void) */ void LCR_racingInit(void) { - LCR_log("initializing racing engine"); + LCR_LOG0("initializing racing engine"); // make the car body: TPE_makeCenterRectFull(LCR_racing.carJoints, @@ -361,6 +364,8 @@ void LCR_racingInit(void) void LCR_racingGetCarTransform(LCR_GameUnit position[3], LCR_GameUnit rotation[3], LCR_GameUnit interpolationParam) { + LCR_LOG2("getting car transform"); + TPE_Vec3 v; #if LCR_SETTING_SMOOTH_ANIMATIONS @@ -435,6 +440,8 @@ void _LCR_racingWheelAccelerate(unsigned int wheel, TPE_Vec3 dir) */ void LCR_racingStep(unsigned int input) { + LCR_LOG2("racing step start"); + TPE_Vec3 carForw, carRight, carUp; carForw = TPE_vec3Normalized(TPE_vec3Plus( @@ -546,7 +553,10 @@ void LCR_racingStep(unsigned int input) TPE_bodyApplyGravity(&(LCR_racing.carBody),LCR_GRAVITY); LCR_racing.wheelCollisions <<= 4; + + LCR_LOG2("gonna step physics engine"); TPE_worldStep(&(LCR_racing.physicsWorld)); + LCR_LOG2("stepping physics engine done"); if (TPE_vec3Dot(carUp,TPE_vec3Minus(LCR_racing.carBody.joints[4].position, LCR_racing.carBody.joints[0].position)) < 0) @@ -588,11 +598,15 @@ void LCR_racingStep(unsigned int input) LCR_racing.carRotations[0] = tmpVec; LCR_racing.tick++; + + LCR_LOG2("racing step end"); } void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2], LCR_GameUnit camFov) { + LCR_LOG2("drawing physics debug overlay"); + TPE_Vec3 cPos, cRot, cView; cPos.x = (camPos[0] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT; diff --git a/renderer.h b/renderer.h index a1bd69c..1f19e8b 100644 --- a/renderer.h +++ b/renderer.h @@ -91,6 +91,8 @@ struct void LCR_rendererSetCarTransform(LCR_GameUnit position[3], LCR_GameUnit rotation[3]) { + LCR_LOG2("setting car transform"); + LCR_renderer.carModel->transform.translation.x = (position[0] * LCR_RENDERER_UNIT) / LCR_GAME_UNIT; LCR_renderer.carModel->transform.translation.y = @@ -271,7 +273,7 @@ S3L_Index _LCR_rendererAddMapVert(S3L_Unit x, S3L_Unit y, S3L_Unit z) return LCR_renderer.mapModel.vertexCount - 1; } - LCR_log("couldn't add map vertex!"); + LCR_LOG0("couldn't add map vertex!"); return 0; } @@ -479,7 +481,7 @@ uint8_t _LCR_rendererCheckMapTriCover(const S3L_Index *t1, */ void _LCR_cullHiddenMapTris(void) { - LCR_log("culling invisible triangles"); + LCR_LOG1("culling invisible triangles"); int n = 0; // number of removed elements int i = 0; @@ -567,7 +569,7 @@ void _LCR_cullHiddenMapTris(void) void _LCR_makeMapChunks(void) { - LCR_log("making map chunks"); + LCR_LOG1("making map chunks"); S3L_Index start = 0; @@ -612,7 +614,7 @@ void _LCR_makeMapChunks(void) */ uint8_t _LCR_buildMapModel(void) { - LCR_log("building map model"); + LCR_LOG1("building map model"); uint8_t blockShapeBytes[LCR_MAP_BLOCK_SHAPE_MAX_BYTES]; uint8_t blockShapeByteCount; @@ -739,14 +741,14 @@ uint8_t _LCR_buildMapModel(void) _LCR_cullHiddenMapTris(); - LCR_log("map model built"); + LCR_LOG1("map model built"); return 1; } void _LCR_rendererComputeLOD(void) { - LCR_log("computing LOD"); + LCR_LOG1("computing LOD"); for (int i = 0; i < LCR_RENDERER_LOD_BLOCKS; ++i) LCR_renderer.gridOfLODs[i] = 0; @@ -767,7 +769,7 @@ void _LCR_rendererComputeLOD(void) uint8_t LCR_rendererInit(void) { - LCR_log("initializing renderer"); + LCR_LOG0("initializing renderer"); LCR_renderer.frame = 0; @@ -834,6 +836,8 @@ LCR_renderer.ghostModel->transform.translation.x -= LCR_GAME_UNIT / 4; LCR_renderer.wheelSteer = 0; #endif + LCR_LOG2("initializing 3D scene"); + S3L_sceneInit( LCR_renderer.models,LCR_RENDERER_MODEL_COUNT,&LCR_renderer.scene); @@ -864,6 +868,8 @@ void LCR_rendererGetCameraTransform(LCR_GameUnit position[3], void LCR_rendererMoveCamera(LCR_GameUnit forwRightUpOffset[3], LCR_GameUnit yawPitchOffset[2]) { + LCR_LOG2("moving camera"); + S3L_Vec4 f, r, u; S3L_rotationToDirections(LCR_renderer.scene.camera.transform.rotation, @@ -963,6 +969,8 @@ void LCR_rendererDrawRect(int x, int y, unsigned int w, unsigned int h, void _LCR_rendererDrawLODBlock(int blockX, int blockY, int blockZ, unsigned int size, uint16_t color, uint8_t variability) { + LCR_LOG2("drawing LOD block"); + S3L_Vec4 p, r; p.x = (blockX - LCR_MAP_SIZE_BLOCKS / 2) * LCR_RENDERER_UNIT @@ -1007,6 +1015,8 @@ void _LCR_rendererDrawLODBlock(int blockX, int blockY, int blockZ, unsigned int */ void LCR_rendererDrawSky(int sky, S3L_Unit offsetH, S3L_Unit offsetV) { + LCR_LOG2("drawing sky"); + int anchorPoint[2], y; unsigned long pixelIndex; unsigned int topColor, bottomColor; @@ -1196,6 +1206,8 @@ S3L_Unit _LCR_rendererSmoothRot(S3L_Unit angleOld, S3L_Unit angleNew, */ void _LCR_rendererLoadMapChunks(void) { + LCR_LOG2("loading map chunks"); + int8_t camChunk[3], chunkOffsets[3]; S3L_Vec4 cp = LCR_renderer.scene.camera.transform.translation; S3L_Vec4 cf; @@ -1236,6 +1248,8 @@ void _LCR_rendererLoadMapChunks(void) */ void LCR_rendererDrawLOD(void) { + LCR_LOG2("drawing LOD"); + #if LCR_SETTING_LOD_DISTANCE < 64 int variability = 0; @@ -1267,6 +1281,8 @@ void LCR_rendererDrawLOD(void) void LCR_drawLevelFloor(void) { + LCR_LOG2("drawing floor"); + #if LCR_SETTING_FLOOR_PARTICLE_SIZE != 0 #define _STEP ((LCR_MAP_SIZE_BLOCKS * LCR_RENDERER_UNIT) / LCR_SETTING_FLOOR_PARTICLE_RESOLUTION) @@ -1302,6 +1318,8 @@ void LCR_drawLevelFloor(void) #if LCR_ANIMATE_CAR void _LCR_rendererAnimateCar(void) { + LCR_LOG2("animating car"); + for (int i = LCR_renderer.frame % LCR_SETTING_CAR_ANIMATION_SUBDIVIDE; i < LCR_CAR_VERTEX_COUNT; i += LCR_SETTING_CAR_ANIMATION_SUBDIVIDE) { @@ -1351,6 +1369,8 @@ void _LCR_rendererAnimateCar(void) void LCR_rendererCameraFollow(void) { + LCR_LOG2("following camera"); + S3L_Transform3D transPrev = LCR_renderer.scene.camera.transform; LCR_renderer.scene.camera.transform.translation.y = @@ -1427,6 +1447,8 @@ void LCR_rendererSetWheelState(LCR_GameUnit rotation, LCR_GameUnit steer) void LCR_rendererDraw(void) { + LCR_LOG2("rendering frame (start)"); + // first make sure rotations are in correct range: LCR_renderer.scene.camera.transform.rotation.y = S3L_wrap( LCR_renderer.scene.camera.transform.rotation.y, S3L_F); @@ -1450,9 +1472,13 @@ void LCR_rendererDraw(void) LCR_drawLevelFloor(); LCR_rendererDrawLOD(); + LCR_LOG2("gonna render 3D scene"); S3L_drawScene(LCR_renderer.scene); + LCR_LOG2("rendering 3D scene done"); LCR_renderer.frame++; + + LCR_LOG2("rendering frame (end)"); } #endif // guard diff --git a/settings.h b/settings.h index 029cbec..6afad11 100644 --- a/settings.h +++ b/settings.h @@ -131,4 +131,11 @@ #define LCR_SETTING_SMOOTH_ANIMATIONS 1 #endif +#ifndef LCR_SETTING_LOG_LEVEL + /** How detailed the console logs should be. 0 turns off logging, 1 means + normal, 2 more detailed etc. Setting high log level may result in spam and + slower game, but is useful for debugging. */ + #define LCR_SETTING_LOG_LEVEL 1 +#endif + #endif // guard