1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-20 20:29:58 +01:00
small3dlib/programs/city.c

306 lines
6.8 KiB
C
Raw Permalink Normal View History

2019-06-09 20:30:18 +02:00
/*
2019-06-23 17:32:53 +02:00
Example program for small3dlib -- a GTA-like game demo.
2019-06-09 20:30:18 +02:00
author: Miloslav Ciz
2019-06-23 17:32:53 +02:00
license: CC0 1.0
2019-06-09 20:30:18 +02:00
*/
#include <stdio.h>
#include <time.h>
#define S3L_FLAT 0
2022-04-16 22:29:16 +02:00
#define S3L_NEAR_CROSS_STRATEGY 3
2019-06-11 15:37:40 +02:00
#define S3L_PERSPECTIVE_CORRECTION 2
2019-06-11 15:45:50 +02:00
#define S3L_SORT 0
#define S3L_STENCIL_BUFFER 0
2019-06-20 16:12:00 +02:00
#define S3L_Z_BUFFER 2
2019-06-09 20:30:18 +02:00
#define S3L_PIXEL_FUNCTION drawPixel
2019-06-29 20:44:39 +02:00
#define S3L_RESOLUTION_X 640
#define S3L_RESOLUTION_Y 480
2019-06-09 20:30:18 +02:00
#include "../small3dlib.h"
#include "cityModel.h"
#include "cityTexture.h"
2019-06-18 01:41:02 +02:00
#include "carModel.h"
2019-06-09 20:30:18 +02:00
#define TEXTURE_W 256
#define TEXTURE_H 256
2022-05-28 20:19:50 +02:00
#include "sdl_helper.h"
2019-06-20 21:05:04 +02:00
#define MAX_VELOCITY 1000
#define ACCELERATION 700
#define TURN_SPEED 300
#define FRICTION 600
2019-06-20 00:17:46 +02:00
2019-06-18 01:41:02 +02:00
S3L_Model3D models[2];
2019-06-09 20:30:18 +02:00
2019-06-19 18:48:08 +02:00
const uint8_t collisionMap[8 * 10] =
{
1,1,1,1,1,1,1,1,
1,1,1,1,0,0,0,1,
1,1,1,1,0,1,0,1,
2,2,1,0,0,0,0,3,
1,2,1,0,1,1,3,1,
2,0,0,0,1,1,3,3,
1,0,1,0,0,1,1,1,
1,0,0,0,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
};
2019-06-09 20:30:18 +02:00
S3L_Scene scene;
uint32_t frame = 0;
2022-05-28 20:19:50 +02:00
void clearScreenBlue()
2019-06-09 20:30:18 +02:00
{
2019-06-20 18:33:38 +02:00
uint32_t index = 0;
for (uint16_t y = 0; y < S3L_RESOLUTION_Y; ++y)
{
2022-09-21 15:53:10 +02:00
S3L_Unit t = S3L_min(S3L_F,((y * S3L_F) / S3L_RESOLUTION_Y) * 4);
2019-06-20 18:33:38 +02:00
uint32_t r = S3L_interpolateByUnit(200,242,t);
uint32_t g = S3L_interpolateByUnit(102,255,t);
uint32_t b = S3L_interpolateByUnit(255,230,t);
uint32_t color = (r << 24) | (g << 16 ) | (b << 8);
for (uint16_t x = 0; x < S3L_RESOLUTION_X; ++x)
{
pixels[index] = color;
index++;
}
}
2019-06-09 20:30:18 +02:00
}
2019-06-18 22:52:12 +02:00
uint32_t previousTriangle = -1;
2019-06-21 23:47:30 +02:00
S3L_Vec4 uv0, uv1, uv2;
2019-06-09 20:30:18 +02:00
void drawPixel(S3L_PixelInfo *p)
{
2019-06-18 18:26:17 +02:00
if (p->triangleID != previousTriangle)
2019-06-18 01:41:02 +02:00
{
2019-07-01 01:42:26 +02:00
const S3L_Index *uvIndices;
const S3L_Unit *uvs;
2019-06-11 00:24:45 +02:00
2019-06-18 01:41:02 +02:00
if (p->modelIndex == 0)
{
uvIndices = cityUVIndices;
uvs = cityUVs;
}
else
{
uvIndices = carUVIndices;
uvs = carUVs;
}
2019-06-11 00:24:45 +02:00
2019-06-21 23:47:30 +02:00
S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2);
2019-06-09 20:30:18 +02:00
2019-06-18 18:26:17 +02:00
previousTriangle = p->triangleID;
2019-06-09 20:30:18 +02:00
}
2019-06-23 17:32:53 +02:00
uint8_t r, g, b;
2019-06-09 20:30:18 +02:00
S3L_Unit uv[2];
2019-06-21 23:47:30 +02:00
uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
2019-06-09 20:30:18 +02:00
2022-05-28 20:19:50 +02:00
sampleTexture(cityTexture,uv[0] >> 1,uv[1] >> 1,&r,&g,&b);
2019-06-09 20:30:18 +02:00
setPixel(p->x,p->y,r,g,b);
}
void draw()
{
S3L_newFrame();
2022-05-28 20:19:50 +02:00
clearScreenBlue();
2019-06-09 20:30:18 +02:00
S3L_drawScene(scene);
}
2019-06-29 20:44:39 +02:00
static inline uint8_t collision(S3L_Vec4 worldPosition)
2019-06-19 18:48:08 +02:00
{
2022-09-21 15:53:10 +02:00
worldPosition.x /= S3L_F;
worldPosition.z /= -S3L_F;
2019-06-19 18:48:08 +02:00
uint16_t index = worldPosition.z * 8 + worldPosition.x;
return collisionMap[index];
}
2019-06-23 17:32:53 +02:00
static inline void handleCollision(S3L_Vec4 *pos, S3L_Vec4 previousPos)
2019-06-20 16:12:00 +02:00
{
2019-06-23 17:32:53 +02:00
S3L_Vec4 newPos = *pos;
newPos.x = previousPos.x;
if (collision(newPos))
{
newPos = *pos;
newPos.z = previousPos.z;
2019-06-20 16:12:00 +02:00
2019-06-23 17:32:53 +02:00
if (collision(newPos))
newPos = previousPos;
}
2019-06-20 16:12:00 +02:00
2019-06-23 17:32:53 +02:00
*pos = newPos;
2019-06-20 16:12:00 +02:00
}
2019-06-09 20:30:18 +02:00
int16_t fps = 0;
2022-05-28 20:19:50 +02:00
int main(void)
2019-06-09 20:30:18 +02:00
{
2022-05-28 20:19:50 +02:00
sdlInit();
2019-06-09 20:30:18 +02:00
2019-06-18 01:41:02 +02:00
cityModelInit();
carModelInit();
models[0] = cityModel;
models[1] = carModel;
2019-06-09 20:30:18 +02:00
2021-12-28 23:25:22 +01:00
S3L_sceneInit(models,2,&scene);
2019-06-09 20:30:18 +02:00
2021-12-28 23:25:22 +01:00
S3L_transform3DSet(1909,16,-3317,0,-510,0,512,512,512,&(models[1].transform));
2019-06-09 20:30:18 +02:00
int running = 1;
clock_t nextPrintT;
nextPrintT = clock();
2019-06-18 22:52:12 +02:00
S3L_Vec4 carDirection;
2021-12-28 23:25:22 +01:00
S3L_vec4Init(&carDirection);
2019-06-20 00:17:46 +02:00
2022-09-21 15:53:10 +02:00
scene.camera.transform.translation.y = S3L_F / 2;
scene.camera.transform.rotation.x = -S3L_F / 16;
2019-06-20 00:17:46 +02:00
int16_t velocity = 0;
2019-06-18 22:52:12 +02:00
2019-06-23 17:32:53 +02:00
while (running) // main loop
2019-06-09 20:30:18 +02:00
{
clock_t frameStartT = clock();
2019-06-22 04:04:16 +02:00
models[1].transform.rotation.y += models[1].transform.rotation.z; // overturn the car for the rendering
2019-06-09 20:30:18 +02:00
draw();
2019-06-22 04:04:16 +02:00
models[1].transform.rotation.y -= models[1].transform.rotation.z; // turn the car back for the physics
2019-06-09 20:30:18 +02:00
fps++;
clock_t nowT = clock();
double timeDiff = ((double) (nowT - nextPrintT)) / CLOCKS_PER_SEC;
double frameDiff = ((double) (nowT - frameStartT)) / CLOCKS_PER_SEC;
2019-06-29 20:44:39 +02:00
int16_t frameDiffMs = frameDiff * 1000;
2019-06-09 20:30:18 +02:00
if (timeDiff >= 1.0)
{
nextPrintT = nowT;
printf("FPS: %d\n",fps);
fps = 0;
}
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
running = 0;
2019-07-01 01:42:26 +02:00
const uint8_t *state = SDL_GetKeyboardState(NULL);
2019-06-09 20:30:18 +02:00
2019-06-29 20:44:39 +02:00
int16_t step = (velocity * frameDiffMs) / 1000;
int16_t stepFriction = (FRICTION * frameDiffMs) / 1000;
int16_t stepRotation = TURN_SPEED * frameDiffMs * S3L_max(0,velocity - 200) / (MAX_VELOCITY * 1000);
int16_t stepVelocity = S3L_nonZero((ACCELERATION * frameDiffMs) / 1000);
2019-06-09 20:30:18 +02:00
2019-06-20 02:03:46 +02:00
if (stepRotation == 0 && S3L_abs(velocity) >= 200)
stepRotation = 1;
if (velocity < 0)
stepRotation *= -1;
2022-05-28 20:19:50 +02:00
if (state[SDL_SCANCODE_ESCAPE])
running = 0;
else if (state[SDL_SCANCODE_LEFT])
2019-06-20 02:03:46 +02:00
{
2019-06-18 22:52:12 +02:00
models[1].transform.rotation.y += stepRotation;
2019-06-22 04:15:10 +02:00
models[1].transform.rotation.z =
S3L_min(S3L_abs(velocity) / 64, models[1].transform.rotation.z + 1);
2019-06-20 02:03:46 +02:00
}
2019-06-18 22:52:12 +02:00
else if (state[SDL_SCANCODE_RIGHT])
2019-06-20 02:03:46 +02:00
{
2019-06-18 22:52:12 +02:00
models[1].transform.rotation.y -= stepRotation;
2019-06-22 04:15:10 +02:00
models[1].transform.rotation.z =
S3L_max(-S3L_abs(velocity) / 64, models[1].transform.rotation.z - 1);
2019-06-20 02:03:46 +02:00
}
else
models[1].transform.rotation.z = (models[1].transform.rotation.z * 3) / 4;
2019-06-18 22:58:37 +02:00
2022-09-21 15:53:10 +02:00
S3L_rotationToDirections(models[1].transform.rotation,S3L_F,&carDirection,0,0);
2019-06-18 22:52:12 +02:00
2019-06-20 16:12:00 +02:00
S3L_Vec4 previousCarPos = models[1].transform.translation;
2019-06-19 18:48:08 +02:00
2019-06-20 02:03:46 +02:00
int16_t friction = 0;
2019-06-18 22:52:12 +02:00
if (state[SDL_SCANCODE_UP])
2019-06-20 02:03:46 +02:00
velocity = S3L_min(MAX_VELOCITY,velocity + (velocity < 0 ? (2 * stepVelocity) : stepVelocity));
2019-06-18 22:52:12 +02:00
else if (state[SDL_SCANCODE_DOWN])
2019-06-20 02:03:46 +02:00
velocity = S3L_max(-MAX_VELOCITY,velocity - (velocity > 0 ? (2 * stepVelocity) : stepVelocity));
2019-06-20 00:17:46 +02:00
else
2019-06-20 02:03:46 +02:00
friction = 1;
2019-06-09 20:30:18 +02:00
2022-09-21 15:53:10 +02:00
models[1].transform.translation.x += (carDirection.x * step) / S3L_F;
models[1].transform.translation.z += (carDirection.z * step) / S3L_F;
2019-06-20 00:17:46 +02:00
2019-06-19 18:48:08 +02:00
uint8_t coll = collision(models[1].transform.translation);
if (coll != 0)
{
if (coll == 1)
{
2019-06-20 16:12:00 +02:00
handleCollision(&(models[1].transform.translation),previousCarPos);
2019-06-20 21:05:04 +02:00
friction = 2;
2019-06-19 18:48:08 +02:00
}
else if (coll == 2)
{
2019-06-22 04:38:57 +02:00
// teleport the car
2022-09-21 15:53:10 +02:00
models[1].transform.translation.x += 5 * S3L_F;
models[1].transform.translation.z += 2 * S3L_F;
2019-06-19 18:48:08 +02:00
}
else
{
2019-06-22 04:38:57 +02:00
// teleport the car
2022-09-21 15:53:10 +02:00
models[1].transform.translation.x -= 5 * S3L_F;
models[1].transform.translation.z -= 2 * S3L_F;
2019-06-19 18:48:08 +02:00
}
}
2019-06-20 02:03:46 +02:00
if (velocity > 0)
velocity = S3L_max(0,velocity - stepFriction * friction);
else
velocity = S3L_min(0,velocity + stepFriction * friction);
2019-06-20 15:43:19 +02:00
S3L_Unit cameraDistance =
2022-09-21 15:53:10 +02:00
S3L_interpolate(S3L_F / 2,(3 * S3L_F) / 4,S3L_abs(velocity),MAX_VELOCITY);
2019-06-20 15:43:19 +02:00
2019-06-20 00:17:46 +02:00
scene.camera.transform.translation.x =
2022-09-21 15:53:10 +02:00
scene.models[1].transform.translation.x - (carDirection.x * cameraDistance) / S3L_F;
2019-06-19 19:05:18 +02:00
2019-06-20 00:17:46 +02:00
scene.camera.transform.translation.z =
2022-09-21 15:53:10 +02:00
scene.models[1].transform.translation.z - (carDirection.z * cameraDistance) / S3L_F;
2019-06-18 23:17:37 +02:00
2019-06-20 00:17:46 +02:00
scene.camera.transform.rotation.y = models[1].transform.rotation.y;
2019-06-18 22:58:37 +02:00
2022-05-28 20:19:50 +02:00
sdlUpdate();
2019-06-09 20:30:18 +02:00
frame++;
}
return 0;
}