aboutsummaryrefslogtreecommitdiffstats
path: root/src/city.c
blob: 3ba25cb16fc00075adb4c5d2efebadde4fa6da74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
  Example program for small3dlib -- a GTA-like game demo.

  author: Miloslav Ciz
  license: CC0 1.0
*/

#include <SDL2/SDL.h>
#include <SDL2/SDL_scancode.h>
#include <stdio.h>
#include <time.h>

#define S3L_FLAT 0
#define S3L_NEAR_CROSS_STRATEGY 3
#define S3L_PERSPECTIVE_CORRECTION 2
#define S3L_SORT 0
#define S3L_STENCIL_BUFFER 0
#define S3L_Z_BUFFER 2

#define S3L_PIXEL_FUNCTION drawPixel

#define S3L_RESOLUTION_X 1000
#define S3L_RESOLUTION_Y 600

#include "small3dlib.h"

#include "cityModel.h"
#include "cityTexture.h"
#include "carModel.h"

#define TEXTURE_W 256
#define TEXTURE_H 256

#define MAX_VELOCITY 1000
#define ACCELERATION 700
#define TURN_SPEED 300
#define FRICTION 600

S3L_Model3D models[2];

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
};

S3L_Scene scene;

uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];

uint32_t frame = 0;

void clearScreen()
{
  uint32_t index = 0;

  for (uint16_t y = 0; y < S3L_RESOLUTION_Y; ++y)
  {
    S3L_Unit t = S3L_min(S3L_FRACTIONS_PER_UNIT,((y * S3L_FRACTIONS_PER_UNIT) / S3L_RESOLUTION_Y) * 4);

    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++;
    }
  }
}

static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
{
  uint8_t *p = ((uint8_t *) pixels) + (y * S3L_RESOLUTION_X + x) * 4 + 1;

  *p = blue;
  ++p;
  *p = green;
  ++p;
  *p = red;
}

void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b)
{
  u = S3L_clamp(u,0,CITY_TEXTURE_WIDTH - 1);
  v = S3L_clamp(v,0,CITY_TEXTURE_HEIGHT - 1);

  const uint8_t *t = cityTexture + (v * CITY_TEXTURE_WIDTH + u) * 3;

  *r = *t;
  t++;
  *g = *t;
  t++;
  *b = *t;
}

uint32_t previousTriangle = -1;
S3L_Vec4 uv0, uv1, uv2;

void drawPixel(S3L_PixelInfo *p)
{
  if (p->triangleID != previousTriangle)
  {
    const S3L_Index *uvIndices;
    const S3L_Unit *uvs;

    if (p->modelIndex == 0)
    {
      uvIndices = cityUVIndices;
      uvs = cityUVs;
    }
    else
    {
      uvIndices = carUVIndices;
      uvs = carUVs;
    }

    S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2);

    previousTriangle = p->triangleID;
  }

  uint8_t r, g, b;

  S3L_Unit uv[2];

  uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
  uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);

  sampleTexture(uv[0] >> 1,uv[1] >> 1,&r,&g,&b);
  
  setPixel(p->x,p->y,r,g,b); 
}

void draw()
{
  S3L_newFrame();
  clearScreen();
  S3L_drawScene(scene);
}

static inline uint8_t collision(S3L_Vec4 worldPosition)
{
  worldPosition.x /= S3L_FRACTIONS_PER_UNIT;
  worldPosition.z /= -S3L_FRACTIONS_PER_UNIT;    

  uint16_t index = worldPosition.z * 8 + worldPosition.x;

  return collisionMap[index];
}

static inline void handleCollision(S3L_Vec4 *pos, S3L_Vec4 previousPos)
{
  S3L_Vec4 newPos = *pos;
  newPos.x = previousPos.x;
    
  if (collision(newPos))
  {
    newPos = *pos;
    newPos.z = previousPos.z;

    if (collision(newPos))
      newPos = previousPos;
  }

  *pos = newPos;
}

int16_t fps = 0;

int main()
{
  SDL_Window *window = SDL_CreateWindow("city demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN); 
  SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
  SDL_Texture *textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
  SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
  SDL_Event event;

  cityModelInit();
  carModelInit();

  models[0] = cityModel;
  models[1] = carModel;

  S3L_sceneInit(models,2,&scene);

  S3L_transform3DSet(1909,16,-3317,0,-510,0,512,512,512,&(models[1].transform));

  int running = 1;

  clock_t nextPrintT;

  nextPrintT = clock();

  S3L_Vec4 carDirection;

  S3L_vec4Init(&carDirection);
  
  scene.camera.transform.translation.x = 0;
  scene.camera.transform.translation.y = S3L_FRACTIONS_PER_UNIT / 4;
  scene.camera.transform.rotation.x = 1;

  int16_t velocity = 0;

  while (running) // main loop
  {
    clock_t frameStartT = clock();

    models[1].transform.rotation.y += models[1].transform.rotation.z; // overturn the car for the rendering

    draw();

    models[1].transform.rotation.y -= models[1].transform.rotation.z; // turn the car back for the physics

    fps++;

    SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));

    clock_t nowT = clock();

    double timeDiff = ((double) (nowT - nextPrintT)) / CLOCKS_PER_SEC;
    double frameDiff = ((double) (nowT - frameStartT)) / CLOCKS_PER_SEC;
    int16_t frameDiffMs = frameDiff * 1000;

    if (timeDiff >= 1.0)
    {
      nextPrintT = nowT;
      printf("FPS: %d\n",fps);
      fps = 0;
    }

    while (SDL_PollEvent(&event))
      if (event.type == SDL_QUIT)
        running = 0;

    const uint8_t *state = SDL_GetKeyboardState(NULL);

    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);

    if (stepRotation == 0 && S3L_abs(velocity) >= 200)
      stepRotation = 1;

    if (velocity < 0)
      stepRotation *= -1;

    if (state[SDL_SCANCODE_LEFT] || state[SDL_SCANCODE_A])
    {
      models[1].transform.rotation.y += stepRotation;
      models[1].transform.rotation.z =
        S3L_min(S3L_abs(velocity) / 64, models[1].transform.rotation.z + 1);
    }
    else if (state[SDL_SCANCODE_RIGHT] || state[SDL_SCANCODE_D])
    {
      models[1].transform.rotation.y -= stepRotation;
      models[1].transform.rotation.z =
        S3L_max(-S3L_abs(velocity) / 64, models[1].transform.rotation.z - 1);
    }
    else
      models[1].transform.rotation.z = (models[1].transform.rotation.z * 3) / 4;

    S3L_rotationToDirections(models[1].transform.rotation,S3L_FRACTIONS_PER_UNIT,&carDirection,0,0);

    S3L_Vec4 previousCarPos = models[1].transform.translation;

    int16_t friction = 0;

    if (state[SDL_SCANCODE_UP] || state[SDL_SCANCODE_W])
      velocity = S3L_min(MAX_VELOCITY,velocity + (velocity < 0 ? (2 * stepVelocity) : stepVelocity));
    else if (state[SDL_SCANCODE_DOWN] || state[SDL_SCANCODE_S])
      velocity = S3L_max(-MAX_VELOCITY,velocity - (velocity > 0 ? (2 * stepVelocity) : stepVelocity));
    else
      friction = 1;

    models[1].transform.translation.x += (carDirection.x * step) / S3L_FRACTIONS_PER_UNIT;
    models[1].transform.translation.z += (carDirection.z * step) / S3L_FRACTIONS_PER_UNIT;

    uint8_t coll = collision(models[1].transform.translation);

    if (coll != 0)
    {
      if (coll == 1)
      {
        handleCollision(&(models[1].transform.translation),previousCarPos);
        friction = 2;
      }
      else if (coll == 2)
      {
        // teleport the car
        models[1].transform.translation.x += 5 * S3L_FRACTIONS_PER_UNIT;
        models[1].transform.translation.z += 2 * S3L_FRACTIONS_PER_UNIT;
      }
      else
      {
        // teleport the car
        models[1].transform.translation.x -= 5 * S3L_FRACTIONS_PER_UNIT;
        models[1].transform.translation.z -= 2 * S3L_FRACTIONS_PER_UNIT;
      }
    }

    if (velocity > 0)
      velocity = S3L_max(0,velocity - stepFriction * friction);
    else
      velocity = S3L_min(0,velocity + stepFriction * friction);

    scene.camera.transform.translation.x =
      scene.models[1].transform.translation.x - (carDirection.x) / S3L_FRACTIONS_PER_UNIT;

    scene.camera.transform.translation.z =
      scene.models[1].transform.translation.z - (carDirection.z) / S3L_FRACTIONS_PER_UNIT;

    scene.camera.transform.rotation.y = models[1].transform.rotation.y;

    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
    SDL_RenderPresent(renderer);

    frame++;
  }

  return 0;
}