Start main loop

This commit is contained in:
Miloslav Ciz 2023-09-17 13:21:19 +02:00
parent ee15824329
commit e21d5b7825
5 changed files with 86 additions and 24 deletions

View file

@ -1,12 +1,17 @@
#ifndef _LCR_CONSTANTS_H
#define _LCR_CONSTANTS_H
#include "settings.h"
#define LCR_EFFECTIVE_RESOLUTION_X \
(LCR_SETTING_RESOLUTION_X / LCR_SETTING_RESOLUTION_SUBDIVIDE)
#define LCR_EFFECTIVE_RESOLUTION_Y \
(LCR_SETTING_RESOLUTION_Y / LCR_SETTING_RESOLUTION_SUBDIVIDE)
#define LCR_FREE_CAMERA_STEP \
(LCR_SETTING_FREE_CAMERA_SPEED / LCR_SETTING_FPS)
/** Maximum number of triangles of a block shape. */
#define LCR_MAP_BLOCK_SHAPE_MAX_TRIANGLES 32

View file

@ -42,6 +42,7 @@ uint8_t LCR_keyPressed(uint8_t key)
void LCR_sleep(uint16_t timeMs)
{
usleep(timeMs * 1000);
}
void LCR_drawPixel(unsigned int x, unsigned int y, uint_fast16_t color)

63
game.h
View file

@ -67,6 +67,8 @@ uint8_t LCR_gameStep(uint32_t timeMs);
//------------------------------------------------------------------------------
uint32_t LCR_nextRenderFrameTime;
void LCR_drawPixelUnsafe(unsigned int x, unsigned int y,
uint_fast16_t color);
@ -121,43 +123,58 @@ void LCR_gameEnd(void)
{
}
int aaa = 0;
uint8_t LCR_gameStep(uint32_t time)
{
for (int i = 0; i < LCR_EFFECTIVE_RESOLUTION_X; ++i)
for (int j = 0; j < LCR_EFFECTIVE_RESOLUTION_Y; ++j)
LCR_drawPixelUnsafe(i,j,0);
for (int i = 0; i < LCR_KEYS_TOTAL; ++i)
_LCR_keyStates[i] = LCR_keyPressed(i);
LCR_SpaceUnit camPos[3];
uint32_t sleep = 0;
LCR_getCameraPos(camPos);
if (time >= LCR_nextRenderFrameTime)
{
LCR_drawBackground(0);
LCR_drawSkyStrip(0,0); // TODO: do this AFTER 3D rendering
LCR_rendererDraw();
#define N 10
if (_LCR_keyStates[LCR_KEY_UP]) camPos[2] += N;
else if (_LCR_keyStates[LCR_KEY_DOWN]) camPos[2] -= N;
LCR_nextRenderFrameTime += 1000 / LCR_SETTING_FPS;
if (_LCR_keyStates[LCR_KEY_LEFT]) camPos[0] -= N;
else if (_LCR_keyStates[LCR_KEY_RIGHT]) camPos[0] += N;
LCR_SpaceUnit offsets[5];
if (_LCR_keyStates[LCR_KEY_A]) camPos[1] -= N;
else if (_LCR_keyStates[LCR_KEY_B]) camPos[1] += N;
for (int i = 0; i < 5; ++i)
offsets[i] = 0;
LCR_setCameraPos(camPos);
if (_LCR_keyStates[LCR_KEY_A])
{
if (_LCR_keyStates[LCR_KEY_UP])
offsets[4] = 4;
else if (_LCR_keyStates[LCR_KEY_DOWN])
offsets[4] -= 4;
if (_LCR_keyStates[LCR_KEY_RIGHT])
offsets[3] -= 4;
else if (_LCR_keyStates[LCR_KEY_LEFT])
offsets[3] = 4;
}
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_drawBackground(200 + aaa / 10);
LCR_drawSkyStrip(200 + aaa / 10,aaa / 30);
LCR_rendererMoveCamera(offsets,offsets + 3);
}
else
sleep = LCR_nextRenderFrameTime - time;
LCR_render();
aaa = (aaa + 1) % 10000;
if (sleep)
LCR_sleep(sleep);
return 1;
}

View file

@ -65,6 +65,34 @@ uint8_t LCR_rendererInit(void)
return 1;
}
void LCR_rendererMoveCamera(LCR_SpaceUnit forwRightUpOffset[3],
LCR_SpaceUnit yawPitchOffset[2])
{
S3L_Vec4 f, r, u;
S3L_rotationToDirections(LCR_scene3D.camera.transform.rotation,
S3L_FRACTIONS_PER_UNIT,&f,&r,&u);
LCR_scene3D.camera.transform.translation.x +=
((f.x * forwRightUpOffset[0] + r.x * forwRightUpOffset[1] +
u.x * forwRightUpOffset[2]) * S3L_FRACTIONS_PER_UNIT) / LCR_SQUARE_SIDE_LEN;
LCR_scene3D.camera.transform.translation.y +=
((f.y * forwRightUpOffset[0] + r.y * forwRightUpOffset[1] +
u.y * forwRightUpOffset[2]) * S3L_FRACTIONS_PER_UNIT) / LCR_SQUARE_SIDE_LEN;
LCR_scene3D.camera.transform.translation.z +=
((f.z * forwRightUpOffset[0] + r.z * forwRightUpOffset[1] +
u.z * forwRightUpOffset[2]) * S3L_FRACTIONS_PER_UNIT) / LCR_SQUARE_SIDE_LEN;
LCR_scene3D.camera.transform.rotation.y +=
(yawPitchOffset[0] * S3L_FRACTIONS_PER_UNIT) / LCR_SQUARE_SIDE_LEN;
LCR_scene3D.camera.transform.rotation.x +=
(yawPitchOffset[1] * S3L_FRACTIONS_PER_UNIT) / LCR_SQUARE_SIDE_LEN;
}
/*
void LCR_getCameraPos(LCR_SpaceUnit pos[3])
{
pos[0] = (LCR_scene3D.camera.transform.translation.x *
@ -84,8 +112,9 @@ void LCR_setCameraPos(LCR_SpaceUnit pos[3])
LCR_scene3D.camera.transform.translation.z =
(pos[2] * S3L_FRACTIONS_PER_UNIT) / LCR_SQUARE_SIDE_LEN;
}
*/
void LCR_render(void)
void LCR_rendererDraw(void)
{
S3L_newFrame();
S3L_drawScene(LCR_scene3D);

View file

@ -9,10 +9,20 @@
#define LCR_SETTING_RESOLUTION_Y 768
#endif
#ifndef LCR_SETTING_FPS
/** Rendering frames per second. Note this only applies to graphics, NOT
physics. */
#define LCR_SETTING_FPS 30
#endif
#ifndef LCR_SETTING_RESOLUTION_SUBDIVIDE
#define LCR_SETTING_RESOLUTION_SUBDIVIDE 1
#endif
#ifndef LCR_SETTING_FREE_CAMERA_SPEED
#define LCR_SETTING_FREE_CAMERA_SPEED 50
#endif
#ifndef LCR_SETTING_MAX_VERTICES
/** Maximum number of vertices for 3D rendering. Lower number will decrease
RAM usage but will prevent larger maps from being loaded. */