summaryrefslogtreecommitdiff
path: root/backup.c
blob: c7886861235e3cb8552c74e2d7963eb6530443b5 (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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>

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

#define SPEED 7
#define RADSPD 0.2
#define DELAY 40
#define MAPX 8
#define MAPY 8
#define PI 3.1415926535
#define DEG 0.01745329
#define HEIGHT 512
#define WIDTH 1232
#define TILESIZE 64
//#define SQRTTILE 6

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 done = 0;
	SDL_Event event;
	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;
				}
			}
			break;
			
			case SDL_QUIT:
				done = 1;
			break;
		}
	}

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

	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) >> 6;
	int py64 = (int)(P->y) >> 6;

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

	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)*SPEED;
	P->dy = sin(P->th)*SPEED;

	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])
			{
				SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
			} else {
				SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
			}

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

void drawPlayer(SDL_Renderer *renderer, struct player P)
{
		SDL_SetRenderDrawColor(renderer, 255, 220, 0, 255);
		SDL_Rect rect = {P.x-5, P.y-5, 10, 10};
		SDL_RenderFillRect(renderer, &rect);

		SDL_Vertex arrow[3];
		arrow[0].position.x = P.x+5*cos(P.th-PI/2);
		arrow[0].position.y = P.y+5*sin(P.th-PI/2);
		arrow[0].color.r = 255;
		arrow[0].color.g = 220;
		arrow[0].color.b = 0;
		arrow[0].color.a = 255;

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

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

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

void drawRays(SDL_Renderer *renderer, struct player P, int map[])
{
	int mx, my, mp, dof;
	float rx, ry, ra, xo, yo, px, py, hlen, vlen, sinra, hx, hy, vx, vy, rdist;
	ra=P.th-45*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<90; r++)
	{
		// ----- по горизонтальным -----
		hlen = 100000;
		dof = 0;
		sinra = sin(ra);
		if(sinra<-0.001)	// "вверх"
		{
			antan=-1.0/tan(ra);
			ry = (((int)py>>6)<<6)-0.0001;
			rx = (py-ry)*antan+px;
			yo = -64;
			xo = -yo*antan;
		} else {
		if(sinra>0.001)
		{
			antan=-1.0/tan(ra);
			ry = (((int)py>>6)<<6)+64;
			rx = (py-ry)*antan+px;
			yo = 64;
			xo = -yo*antan;
		}
		else
		{
			rx = px;
			ry = py;
			dof = 8;
		}}

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

			if(mp > 0 && mp < MAPX*MAPY && map[mp]==1){
				hx = rx;
				hy = ry;
				hlen = dist(px, py, hx, hy);
				dof = 8;
			} else {
				rx += xo;
				ry += yo;
				dof++;
			}
		}
		//SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
		//SDL_RenderDrawLineF(renderer, P.x, P.y, rx, ry);
		// ----------

		// ----- по вертикальным -----
		dof = 0;
		vlen = 100000;
		float cosra = cos(ra);
		if(cosra<-0.001)	// "влево"
		{
			antan=-tan(ra);
			rx = (((int)px>>6)<<6)-0.0001;
			ry = (px-rx)*antan+py;
			xo = -64;
			yo = -xo*antan;
		} else {
		if(cosra>0.001)
		{
			antan=-tan(ra);
			rx = (((int)px>>6)<<6)+64;
			ry = (px-rx)*antan+py;
			xo = 64;
			yo = -xo*antan;
		}
		else
		{
			rx = px;
			ry = py;
			dof = 8;
		}}

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

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

		if(vlen < hlen) 
		{ 
			rx = vx; ry = vy; rdist = vlen; 
			SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
		}
		else { rx = hx; ry = hy; rdist = hlen; 
			SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
		}
				
		SDL_RenderDrawLineF(renderer, P.x, P.y, rx, ry);

		// ----- Рисуем стены -----
		float corran = P.th - ra;
		if(corran<0)
			corran += 2*PI;
		if(corran>2*PI)
			corran -= 2*PI;
		rdist *= cos(corran); // рыбий глаз
		//rdist += 20;

		float lineh = (64*HEIGHT)/rdist;
		if(lineh>HEIGHT)
			lineh=HEIGHT;
		float lineo = HEIGHT/2 - lineh/2;

		SDL_Rect line = {64*MAPX+r*8, lineo, 8, lineh};
		SDL_RenderFillRect(renderer, &line);

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

void Render(SDL_Renderer *renderer, int map[], struct player P)
{
	SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255); // фон
	SDL_RenderClear(renderer);
	SDL_SetRenderDrawColor(renderer, 90, 90, 100, 255); // пол
	SDL_Rect floor = {0, HEIGHT/2, WIDTH, HEIGHT/2};
	SDL_RenderFillRect(renderer, &floor);
	drawLevel(renderer, map, P);
	drawRays(renderer, P, map);
	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);
	struct player P = {100, 100, 0, 0, PI/2};
	// int mapT = 64;
	int layout[MAPX*MAPY] = 
	{
 	1,1,1,1,1,1,1,1,
 	1,0,1,0,0,0,0,1,
 	1,0,1,0,0,0,0,1,
 	1,0,1,0,0,0,0,1,
 	1,0,0,0,0,0,0,1,
 	1,0,0,0,0,1,0,1,
 	1,0,0,0,0,0,0,1,
 	1,1,1,1,1,1,1,1
	};

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

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

	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(renderer);

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