summaryrefslogtreecommitdiff
path: root/backup3.c
blob: a3e32387097990de01de536e4e8b5ed2af2409e1 (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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <time.h>

#include "textures/textures.ppm"

#define SPEED 1.5
#define RADSPD 0.05
// #define DELAY 1
#define MAPX 16
#define MAPY 16
#define PI 3.1415926535
#define DEG 0.01745329
#define HEIGHT 512
#define WIDTH 960
#define TILESIZE 32
#define SQRTTILE 5
#define MAPSCALE 2

struct player
{
	float x; 
	float y; 
	float dx; 
	float dy; 
	float th;
};

float dist(float x1, float y1, float x2, float y2)
{
	return ( sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) );
}

int processEvents(SDL_Window *window, struct player *P, int map[], int *mpbool, clock_t *prevframe)
{
	// Вычисление FPS
	clock_t frame;
	float fps;
	
	frame = clock();
	fps = CLOCKS_PER_SEC/(double)(frame - *prevframe);
	*prevframe = frame;
	printf("%d\r", (int)fps);

	int done = 0;
	SDL_Event event;

	int xoff = 0;
	if((P->dx)>0) {xoff = 20;} else {xoff = -20;}

	int yoff = 0;
	if((P->dy)>0) {yoff = 20;} else {yoff = -20;}

	int px64 = (int)(P->x) >> SQRTTILE;
	int py64 = (int)(P->y) >> SQRTTILE;

	int pxadd = (int)(P->x + xoff) >> SQRTTILE, pyadd = (int)(P->y + yoff) >> SQRTTILE;
	int pxsub = (int)(P->x - xoff) >> SQRTTILE, pysub = (int)(P->y - yoff) >> SQRTTILE;

	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{	
			case SDL_WINDOWEVENT_CLOSE:
			{
				if(window)
				{
					SDL_DestroyWindow(window);
					window = NULL;
					done = 1;
				}
			}
			break;
			
			case SDL_KEYDOWN:
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						done = 1;
						break;
					case SDLK_m: // карта
						*mpbool = !*mpbool;
						break;
					case SDLK_e: // взаимодействие
						if(map[pyadd*MAPX + pxadd] == 4) // дверь
							map[pyadd*MAPX + pxadd] = 0;
				}
			}
			break;
			
			case SDL_QUIT:
				done = 1;
			break;
		}
	}

	const Uint8 *state = SDL_GetKeyboardState(NULL);
	if(state[SDL_SCANCODE_A])
		P->th -= RADSPD;


	if(state[SDL_SCANCODE_W]) {
		if(map[py64*MAPX + pxadd]==0) {P->x += P->dx;}
		if(map[pyadd*MAPX + px64]==0) {P->y += P->dy;}
	}
	if(state[SDL_SCANCODE_S]) {
		if(map[py64*MAPX + pxsub]==0) {P->x -= P->dx;}
		if(map[pysub*MAPX + px64]==0) {P->y -= P->dy;}
	}
	if(state[SDL_SCANCODE_D])
		P->th += RADSPD; 

	if(P->th < 0)
		P->th = 2*PI - 0.00001;
	if(P->th > 2*PI)
		P->th = 0.0;

	P->dx = cos(P->th)*(75/fps);
	P->dy = sin(P->th)*(75/fps);

	return done;
}

void drawLevel(SDL_Renderer *renderer, int map[], struct player P)
{
	for(int y = 0; y<MAPY; y++)
		for(int x = 0; x<MAPX; x++)
		{
			if(map[y*MAPX + x] > 0)
			{
				SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
			} else {
				SDL_SetRenderDrawColor(renderer, 0, 0, 0, 128);
			}

				SDL_Rect wall = {x*TILESIZE/MAPSCALE, y*TILESIZE/MAPSCALE, TILESIZE/MAPSCALE-2, TILESIZE/MAPSCALE-2};
				SDL_RenderFillRect(renderer, &wall);
		}		
}

void drawPlayer(SDL_Renderer *renderer, struct player P)
{
		SDL_Vertex arrow[3];
		arrow[0].position.x = (P.x+7*cos(P.th-PI/2))/MAPSCALE;
		arrow[0].position.y = (P.y+7*sin(P.th-PI/2))/MAPSCALE;
		arrow[0].color.r = 255;
		arrow[0].color.g = 220;
		arrow[0].color.b = 0;
		arrow[0].color.a = 128;

		arrow[1].position.x = (P.x+7*cos(P.th+PI/2))/MAPSCALE;
		arrow[1].position.y = (P.y+7*sin(P.th+PI/2))/MAPSCALE;
		arrow[1].color.r = 255;
		arrow[1].color.g = 220;
		arrow[1].color.b = 0;
		arrow[1].color.a = 128;

		arrow[2].position.x = (P.x+20*cos(P.th))/MAPSCALE;
		arrow[2].position.y = (P.y+20*sin(P.th))/MAPSCALE;
		arrow[2].color.r = 255;
		arrow[2].color.g = 220;
		arrow[2].color.b = 0;
		arrow[2].color.a = 128;

		SDL_RenderGeometry(renderer, NULL, arrow, 3, NULL, 0);
}

void drawRays(SDL_Renderer *renderer, struct player P, int map[], int mapF[], int mapC[])
{
	int mx, my, mp, dof, texture_iv, texture_ih, texture_i;
	float rx, ry, ra, xo, yo, px, py, hlen, vlen, sinra, hx, hy, vx, vy, rdist;
	ra=P.th-30*DEG;
	if(ra<0)
		ra += 2*PI;
	if(ra>2*PI)
		ra -= 2*PI;

	px = P.x;
	py = P.y;
	float antan;
	for(int r = 0; r<240; r++)
	{
		// ----- по горизонтальным -----
		hlen = 100000;
		dof = 0;
		sinra = sin(ra);
		if(sinra<-0.001)	// "вверх"
		{
			antan=-1.0/tan(ra);
			ry = (((int)py>>SQRTTILE)<<SQRTTILE)-0.0001;
			rx = (py-ry)*antan+px;
			yo = -TILESIZE;
			xo = -yo*antan;
		} else {
		if(sinra>0.001)
		{
			antan=-1.0/tan(ra);
			ry = (((int)py>>SQRTTILE)<<SQRTTILE)+TILESIZE;
			rx = (py-ry)*antan+px;
			yo = TILESIZE;
			xo = -yo*antan;
		}
		else
		{
			rx = px;
			ry = py;
			dof = 16;
		}}

		while(dof<16)
		{	
			mx = (int)(rx)>>SQRTTILE;
			my = (int)(ry)>>SQRTTILE;
			mp = my*MAPX+mx;

			if(mp > 0 && mp < MAPX*MAPY && map[mp]>0){
				hx = rx;
				hy = ry;
				hlen = dist(px, py, hx, hy);
				texture_ih = map[mp]-1;
				dof = 16;
			} else {
				rx += xo;
				ry += yo;
				dof++;
			}
		}
		// ----- по вертикальным -----
		dof = 0;
		vlen = 100000;
		float cosra = cos(ra);
		if(cosra<-0.001)	// "влево"
		{
			antan=-tan(ra);
			rx = (((int)px>>SQRTTILE)<<SQRTTILE)-0.0001;
			ry = (px-rx)*antan+py;
			xo = -TILESIZE;
			yo = -xo*antan;
		} else {
		if(cosra>0.001)
		{
			antan=-tan(ra);
			rx = (((int)px>>SQRTTILE)<<SQRTTILE)+TILESIZE;
			ry = (px-rx)*antan+py;
			xo = TILESIZE;
			yo = -xo*antan;
		}
		else
		{
			rx = px;
			ry = py;
			dof = 16;
		}}

		while(dof<16)
		{	
			mx = (int)(rx)>>SQRTTILE;
			my = (int)(ry)>>SQRTTILE;
			mp = my*MAPX+mx;

			if(mp > 0 && mp < MAPX*MAPY && map[mp]>0){
				vx = rx;
				vy = ry;
				vlen = dist(px, py, vx, vy);
				texture_iv = map[mp]-1;
				dof = 16;
			} else {
				rx += xo;
				ry += yo;
				dof++;
			}
		}

		float shade = 1;
		if(vlen < hlen) 
		{ 
			rx = vx; ry = vy; rdist = vlen; texture_i = texture_iv;
		}
		else { rx = hx; ry = hy; rdist = hlen; shade = 0.6; texture_i = texture_ih;
		}

		// ----- Рисуем стены -----

		float corran = P.th - ra;
		if(corran<0)
			corran += 2*PI;
		if(corran>2*PI)
			corran -= 2*PI;
		rdist *= cos(corran); 						// рыбий глаз

		float lineh = (TILESIZE*HEIGHT)/rdist;
		float ty_step = 32.0 / lineh;
		float ty_offset = 0;
		
		if(lineh>HEIGHT){
			ty_offset = (lineh-HEIGHT)/2.0;
			lineh=HEIGHT;
		}
		float lineo = HEIGHT/2 - lineh/2;

		
		float ty = ty_offset*ty_step; // Текстурирование
		float tx;
		if(vlen > hlen) {
			tx = (int)(rx/(TILESIZE/32)) % 32;
			if(ra < 180*DEG) {tx = 31-tx;}
		} else {
			tx = (int)(ry/(TILESIZE/32)) % 32;
			if(ra>90*DEG && ra < 270*DEG) {tx = 31-tx;}
		}

		/*int y;
		for(y = 0; y<lineh; y++, ty+=ty_step)
		{
			int c = textures[(int)(ty)*32+(int)tx]*255*shade;
			// SDL_SetRenderDrawColor(renderer, c/rdist*15, c/rdist*15, c/rdist*15, 255);
			SDL_SetRenderDrawColor(renderer, c, c, c, 255);
			SDL_Rect line = {r*4, lineo+y, 4, 4};
			SDL_RenderFillRect(renderer, &line);
		}*/
		int y;
		for(y = 0; y<lineh; y++, ty+=ty_step)
		{
			int index = ((int)ty*32 + (int)tx)*3+(texture_i*32*32*3);
			int red = textures[index]*shade;
			int green = textures[index+1]*shade;
			int blue = textures[index+2]*shade;
	
			SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
			SDL_Rect px = {r*4, lineo+y, 4, 4};
			SDL_RenderFillRect(renderer, &px);
		}

		//   ----- рисуем пол -----
		for(y = lineo+lineh; y<HEIGHT; y++)
		{
			float dy = y - (HEIGHT/2.0);
			tx = P.x  + cos(ra)*(0.4941406*HEIGHT)*32/(dy*cos(corran));
			ty = P.y  + sin(ra)*(0.4941406*HEIGHT)*32/(dy*cos(corran));
			int mp = mapF[(int)(ty/32.0)*MAPX + (int)(tx/32.0)]*32*32;

			int index = (((int)(ty)&31)*32 + ((int)(tx)&31))*3 + mp*3;
			int red = textures[index]*0.7;
			int green = textures[index+1]*0.7;
			int blue = textures[index+2]*0.7;

			// float c = textures[index]*0.7;
			// SDL_SetRenderDrawColor(renderer, c*dy/255, c*dy/255, c*dy/255, 255);
			SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
			SDL_Rect line = {r*4, y, 4, 4};
			SDL_RenderFillRect(renderer, &line);

			mp = mapC[(int)(ty/32.0)*MAPX + (int)(tx/32.0)]*32*32;
			index = (((int)(ty)&31)*32 + ((int)(tx)&31))*3 + mp*3;
			red = textures[index]*0.7;
			green = textures[index+1]*0.7;
			blue = textures[index+2]*0.7;
			// SDL_SetRenderDrawColor(renderer, c*dy/255, c*dy/255, c*dy/255, 255);
			SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
			SDL_Rect line_c = {r*4, HEIGHT-y, 4, 4};
			SDL_RenderFillRect(renderer, &line_c);

		}

		ra += 0.25*DEG;
		if(ra<0)
			ra += 2*PI;
		if(ra>2*PI)
			ra -= 2*PI;
	}
}

void Render(SDL_Renderer *renderer, int map[], int mapF[], int mapC[], struct player P, int *mpbool)
{	
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // фон
	SDL_RenderClear(renderer);
	drawRays(renderer, P, map, mapF, mapC);
	
	if(*mpbool) {
		drawLevel(renderer, map, P);
		drawPlayer(renderer, P);
	}

	SDL_RenderPresent(renderer);
	// SDL_Delay(DELAY);
}

int main(void)
{
	SDL_Window *window;
	SDL_Renderer *renderer;
	
	SDL_Init(SDL_INIT_VIDEO);

	window = SDL_CreateWindow("Raycast",
				  SDL_WINDOWPOS_UNDEFINED,
				  SDL_WINDOWPOS_UNDEFINED,
				  WIDTH,
				  HEIGHT,
				  0);

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
	struct player P = {50, 50, 0, 0, PI/2};
	clock_t prevframe = clock();
	clock_t *pnt_pframe = &prevframe;

	int layoutW[MAPX*MAPY] = 
	{
	  /*				    10  12  14  */
		3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,//0
		3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,3,//1
		3,0,0,3,3,0,3,0,0,3,0,0,3,0,0,3,//2
		3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,3,//3
		3,0,0,3,3,0,3,0,0,3,0,0,3,0,0,3,//4
		3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,3,//5
		3,0,0,3,3,3,3,3,3,3,3,3,3,0,0,3,//6
		3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,//7
		3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,//8
		3,3,4,3,3,0,2,0,2,0,2,0,2,0,0,3,//9
		3,0,0,0,3,3,3,3,3,4,3,3,3,0,0,3,//10
		3,3,0,3,3,0,0,0,0,0,0,3,3,0,0,3,//11
		3,0,0,0,3,0,3,3,3,3,0,3,3,0,0,3,//12
		3,3,0,3,3,0,3,3,3,3,3,3,3,0,0,3,//13
		3,0,0,0,3,0,0,0,0,0,0,0,4,0,0,3,//14
		3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,//15
	  /*0,1,2,3,4,5,6,7,8,9,  11  13  15*/
	};

	int layoutF[MAPX*MAPY] = 
	{
	  /*                    10  12  14  */
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//0
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//1
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//2
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//3
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//4
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//5
		0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,//6
		1,1,1,0,0,0,0,0,0,0,0,0,0,2,2,2,//7
		1,1,1,0,0,0,0,0,0,0,0,0,0,2,2,2,//8
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//9
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//10
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//11
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//12
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//13
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//14
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//15
	  /*0,1,2,3,4,5,6,7,8,9,  11  13  15*/
	};

	int layoutC[MAPX*MAPY] = 
	{
	  /*                    10  12  14  */
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//0
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//1
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//2
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//3
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//4
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//5
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//6
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//7
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//8
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//9
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//10
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//11
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//12
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//13
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//14
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//15
	  /*0,1,2,3,4,5,6,7,8,9,  11  13  15*/
	};

	int done = 0;	// bool завершения программы
	int mpbool = 0; // bool вкл/выкл карты

	while(!done)
	{
	// проверка событий
		if(processEvents(window, &P, layoutW, &mpbool, pnt_pframe) == 1)
			done = 1;
	//rendering 
		Render(renderer, layoutW, layoutF, layoutC, P, &mpbool);
	}

	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(renderer);

	SDL_Quit();
	printf("!%d!", errno);
	return 0;
}