-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
432 lines (330 loc) · 12.3 KB
/
main.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <unistd.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 360
#define new_max(x,y) (((x) >= (y)) ? (x) : (y))
#define new_min(x,y) (((x) <= (y)) ? (x) : (y))
#define FRAME_LENGHT_FLOOR 0.017
#define LOG_ENABLED 1
#define LOG_FRAME_LENGHT 0
#define LOG_SPEED 0
#define LOG_TIMING 60
const float screenScale = 1;
float lapTime = 9999999999;
int notInStart = 1;
struct Float3{
float x;
float y;
float z;
};
struct ORC_PlayerState{
struct Float3 pos;
struct Float3 velo;
float rot;
float sinRot;
float cosRot;
};
struct ORC_Input{
int xA;
int yA;
int zA;
int rot;
float sinRot;
float cosRot;
bool quit;
};
struct ORC_Settings{
float scaleX;
float scaleY;
float rotSpeed;
float moveSpeed;
float playerDistance;
float forwardPlayerDrag;
float lateralPlayerDrag;
float playerAcceleration;
float speedCap;
};
struct ORC_CamState{
struct Float3 pos;
float rot;
float sinRot;
float cosRot;
};
bool init(SDL_Window ** gWindow, SDL_Surface ** gScreenSurface);
void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
void applyPhysics(struct ORC_PlayerState * playerState, struct ORC_Settings * gameSettings, struct ORC_Input * input, SDL_Surface * gameMaps[4]);
Uint32 getPixel(SDL_Surface *surface, int x, int y);
void getInput(struct ORC_Input * inputOut,SDL_Event * event);
void updateCam(struct ORC_CamState * camState, struct ORC_PlayerState * playerState, struct ORC_Settings * gameSettings);
float dot(float x1, float y1, float x2, float y2){
return x1*x2 + y1*y2;
}
float lenght(float x, float y){
return sqrt(x*x + y*y);
}
float lenghtSquared(float x, float y){
return (x*x + y*y);
}
float lerp(float v0, float v1, float t) {
return (1 - t) * v0 + t * v1;
}
void startLoop(SDL_Window * window, SDL_Surface * screenSurface, SDL_Surface * gameMaps[4], struct ORC_Settings * gameSettings, struct ORC_CamState * camState, struct ORC_PlayerState * playerState, SDL_Renderer * renderer)
{
SDL_Event e;
struct ORC_Input input = {0, 0, 0, false};
SDL_PixelFormat * pFormat = (*screenSurface).format;
if (pFormat == NULL){
printf( "SDL Error: %s\n", SDL_GetError() );
}
Uint64 NOW = SDL_GetPerformanceCounter();
Uint64 LAST = 0;
double deltaTime = 0;
Uint16 frame = 0;
float pImgScale = SCREEN_HEIGHT * 4 / 720;
SDL_Surface * kartSurf = SDL_LoadBMP("kart.bmp");
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_BITSPERPIXEL(8), SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_Texture * kartTex = SDL_CreateTextureFromSurface(renderer, kartSurf);
if( texture == NULL ){printf( "SDL Error: %s\n", SDL_GetError() );}
SDL_Rect dst = {SCREEN_WIDTH * screenScale / 2 - pImgScale * kartSurf->w / 2,3 * SCREEN_HEIGHT * screenScale / 4 - pImgScale * kartSurf->h / 2,pImgScale * kartSurf->w * screenScale,pImgScale * kartSurf->h * screenScale};
while (!input.quit){
frame++;
LAST = NOW;
NOW = SDL_GetPerformanceCounter();
deltaTime = (double)((NOW - LAST) / (double)SDL_GetPerformanceFrequency() );
usleep(new_max(0, 1000000 * (FRAME_LENGHT_FLOOR - deltaTime)));
NOW = SDL_GetPerformanceCounter();
SDL_LockTextureToSurface(texture, NULL, &screenSurface);
getInput(&input, &e);
applyPhysics(playerState, gameSettings, &input, gameMaps);
updateCam(camState, playerState, gameSettings);
//Uint8 rComp;
//SDL_GetRGB(keyPixel, rComp, NULL, NULL, NULL);
//printf("%i\n", keyPixel);
for (int y = 0; y < SCREEN_HEIGHT / 2; y++){
for (int x = 0; x < SCREEN_WIDTH; x++){
if (y != 0){
float mappedX = camState->pos.z * ((x - SCREEN_WIDTH / 2) * gameSettings->scaleX) / y;
float mappedY = camState->pos.z * gameSettings->scaleY / y;
int rotatedX = camState->cosRot * mappedX - camState->sinRot * mappedY + camState->pos.x;
int rotatedY = camState->sinRot * mappedX + camState->cosRot * mappedY + camState->pos.y;
if (rotatedX > gameMaps[0]->w || rotatedX <= 0 || rotatedY > gameMaps[0]->h || rotatedY < 0){
setPixel(screenSurface, x, y + SCREEN_HEIGHT / 2, SDL_MapRGB(screenSurface->format, 0, 40, 0));
}
else{
Uint32 pixel = getPixel(gameMaps[0], rotatedX, rotatedY);
setPixel(screenSurface, x, y + SCREEN_HEIGHT / 2, pixel);
}
}
}
}
SDL_UnlockTexture(texture);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL , NULL);
SDL_RenderCopy(renderer, kartTex, NULL, &dst);
SDL_RenderPresent(renderer);
#if LOG_ENABLED == 1
if (frame%LOG_TIMING == 0){
//printf("%f", camState->rot);
#if LOG_FRAME_LENGHT == 1
printf("Calculation length: %f ms, %f %% Time ulitlisation\n", deltaTime * 1000, 100 * (deltaTime) / frameTimeCap);
#endif
#if LOG_SPEED == 1
printf("Speed: %f\n", lenght(playerState->velo.x,playerState->velo.y));
#endif
}
#endif
}
}
int mainF()
{
SDL_Window * gWindow = NULL;
SDL_Surface * gScreenSurface = NULL;
gScreenSurface = SDL_CreateRGBSurface(0,SCREEN_WIDTH, SCREEN_HEIGHT, 8, 0,0,0,0);
if (gScreenSurface == NULL){
printf( "SDL Error: %s\n", SDL_GetError() );
}
bool launchSuccess = init(&gWindow, &gScreenSurface);
SDL_Renderer *renderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED);
if( renderer == NULL )
{
printf( "Renderer got himself absolutely demolished! SDL Error: %s\n", SDL_GetError() );
}
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_Surface * loadedMaps[4];
loadedMaps[0] = SDL_LoadBMP("marioKartMap.bmp");
loadedMaps[1] = SDL_LoadBMP("marioKartMapKey.bmp");
struct ORC_Settings settings = {1 ,400, -0.018, 0,720 * 25 / SCREEN_HEIGHT, 0.997, 0.983, 0.03, 4.0}; // cam scale x, cam scale y, rot speed, move speed, player distance, forward player drag ,lateral player drag, player accel, top speed
struct ORC_CamState camState = {0,0,14}; // x - horizontal, y - horizontal, fake "z" vertical , rottation
struct Float3 pPos = {150 , 150 ,14};
struct Float3 pVelo = {0.0 , 0.0 ,0.0};
struct ORC_PlayerState pState = {pPos, pVelo};
pState.velo = pVelo;
startLoop(gWindow, gScreenSurface,loadedMaps, &settings, &camState, &pState, renderer);
SDL_DestroyWindow( gWindow );
SDL_Quit();
return 0;
}
int main( int argc, char * args[] ){
mainF();
}
bool init(SDL_Window ** gWindow, SDL_Surface ** gScreenSurface)
{
bool success = true;
printf("----------------------\n");
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
printf( "SDL INIT FAILED : %s\n", SDL_GetError() );
success = false;
}
else{
*gWindow = SDL_CreateWindow( "OGRACER", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * screenScale, SCREEN_HEIGHT * screenScale, SDL_WINDOW_SHOWN );
printf("SDL INIT OK\n");
if( *gWindow == NULL ){
printf( "SDL WINDOW IS COOKED: %s\n", SDL_GetError() );
success = false;
}
}
#if LOG_ENABLED == 1
printf("GAME LOG ENABLED\n");
printf("SPEED LOG : %i\n", LOG_SPEED);
printf("FRAME TIMES LOG : %i\n", LOG_FRAME_LENGHT);
printf("----------------------\n");
#endif
return success;
}
void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels
+ y * surface->pitch
+ x * surface->format->BytesPerPixel);
*target_pixel = pixel;
}
Uint32 getPixel(SDL_Surface *surface, int x, int y)
{
Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels
+ y * surface->pitch
+ x * surface->format->BytesPerPixel);
return *target_pixel;
}
void applyPhysics(struct ORC_PlayerState * playerState, struct ORC_Settings * gameSettings, struct ORC_Input * input, SDL_Surface * gameMaps[4]){
// PRE COMPUTING
float sn = sin(playerState->rot);
float cs = cos(playerState->rot);
float fSpeed = dot(playerState->velo.x, playerState->velo.y, sn, -cs);
float lSpeed = dot(playerState->velo.x, playerState->velo.y, cs, sn);
float speed = lenght(playerState->velo.x, playerState->velo.y);
playerState->sinRot = sn;
playerState->cosRot = cs;
// DRAG
float nFwD = 1.0 - gameSettings->forwardPlayerDrag;
float nLtD = 1.0 - gameSettings->lateralPlayerDrag;
if (input->yA > 0){
nLtD /= 2;
nFwD /= 2;
}
Uint32 keyPixel = getPixel(gameMaps[1], playerState->pos.x, playerState->pos.y);
if (keyPixel == -65536){
nFwD *= 13;
nLtD *= 13;
}
if (keyPixel == -16711936){
if (notInStart > 2){
printf("Lap time was: %f\n", notInStart * FRAME_LENGHT_FLOOR);
}
notInStart = 0;
}
else{
notInStart += 1;
}
nFwD = 1.0 - nFwD;
nLtD = 1.0 - nLtD;
fSpeed *= nFwD;
lSpeed *= nLtD;
// reasemble
playerState->velo.x = sn * fSpeed + cs * lSpeed;
playerState->velo.y = -cs * fSpeed + sn * lSpeed;
// ACCELERATION
float nPlayerAcceleration = new_min(gameSettings->playerAcceleration, gameSettings->playerAcceleration / fabs(fSpeed));
float moveX = input->xA * nPlayerAcceleration;
float moveY = input->yA * nPlayerAcceleration;
playerState->velo.x += cs * moveX - sn * moveY;
playerState->velo.y += sn * moveX + cs * moveY;
// MOVEMENT
Uint32 keyPixelAhead = getPixel(gameMaps[1], playerState->pos.x + playerState->velo.x, playerState->pos.y + playerState->velo.y);
if (keyPixelAhead != -1){
playerState->pos.x += playerState->velo.x;
playerState->pos.y += playerState->velo.y;
//playerState->pos.z += input->zA * gameSettings->moveSpeed;
}
else{
playerState->velo.x = 0;
playerState->velo.x = 0;
}
// ROTATION
float nRotSpeed = new_min(gameSettings->rotSpeed,-gameSettings->rotSpeed / speed);
playerState->rot += input->rot * nRotSpeed;
// SPEED CAPPING
float nSpeedCap = gameSettings->speedCap;
if (lenghtSquared(playerState->velo.x,playerState->velo.y) > nSpeedCap * nSpeedCap){
float l = lenght(playerState->velo.x,playerState->velo.y);
playerState->velo.x = nSpeedCap * playerState->velo.x / l;
playerState->velo.y = nSpeedCap * playerState->velo.y / l;
}
}
void updateCam(struct ORC_CamState * camState, struct ORC_PlayerState * playerState, struct ORC_Settings * gameSettings){
camState->pos = playerState->pos;
float l = lenght(playerState->velo.x , playerState->velo.y);
//float difX = playerState->velo.x / l;
//float difY = playerState->velo.y / l;
//printf("%f\n", lenght(difX, difY));
camState->pos.x += /*-difX * gameSettings->playerDistance;//*/playerState->sinRot * gameSettings->playerDistance;
camState->pos.y -= /*-difY * gameSettings->playerDistance ;//*/playerState->cosRot * gameSettings->playerDistance;
//camState->rot = asin(-difX / difY);
//camState->rot = 0;
camState->sinRot = playerState->sinRot;
camState->cosRot = playerState->cosRot;
}
void getInput(struct ORC_Input * inputOut,SDL_Event * event){
Uint8* keystate = SDL_GetKeyboardState(NULL);
(inputOut)->xA = 0;
(inputOut)->zA = 0;
(inputOut)->yA = 0;
(inputOut)->rot = 0;
(inputOut)->quit = false;
if(keystate[SDL_SCANCODE_A] == 1){
//(inputOut)->xA -= 1;
}
if(keystate[SDL_SCANCODE_D]){
//(inputOut)->xA += 1;
}
if(keystate[SDL_SCANCODE_UP]){
(inputOut)->yA += 1;
}
if(keystate[SDL_SCANCODE_DOWN]){
(inputOut)->yA -= 1;
}
if(keystate[SDL_SCANCODE_LEFT]){
(inputOut)->rot -= 1;
}
if(keystate[SDL_SCANCODE_RIGHT]){
(inputOut)->rot += 1;
}
if(keystate[SDL_SCANCODE_SPACE]){
//(inputOut)->zA += 1;
}
if(keystate[SDL_SCANCODE_LSHIFT]){
//(inputOut)->zA -= 1;
}
while(SDL_PollEvent(event)){
switch ((*event).type){
case SDL_QUIT:
inputOut->quit = true;
case SDL_KEYDOWN:
if((*event).key.keysym.sym == SDLK_ESCAPE)
inputOut->quit = true;
break;
}
}
}