-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
165 lines (131 loc) · 4.45 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
#define ENTRY_FUNCTION init
#include "header.h"
ierr init(Arguments args) {
UNUSED(args);
// init random number device
srand48(time(NULL));
f64 rnd = drand48(); UNUSED(rnd);
// init SDL
SDL_Context ctx = {0};
sdl2_init(&ctx);
TTF_Init();
TTF_Font *font = TTF_OpenFont("font.ttf", 16);
if (font == NULL) {
eprintln("doesnae work: %s", TTF_GetError());
return 1;
}
// struct for position and size
// get the dimensions of the texture
SDL_QueryTexture(ctx.player.texture, NULL, NULL, &ctx.player.rect.w, &ctx.player.rect.h);
ctx.player.rect.w /= 12;
ctx.player.rect.h /= 12;
// Initialise player.
Player player = { .name = STRING("Howard"), { 0 }, { 0 } };
// position the sprite
player.pos.x = SCREEN_WIDTH / 2 + 20;
player.pos.y = SCREEN_HEIGHT / 2 + 20;
Input dir = STOP;
bool close_requested = false;
player.dir.theta = 1.5 * PI; // Theta = 0 when y = 0 and x = 1, H === 1
// animation loop
u64 frame = 0;
until (close_requested) {
++frame;
// clears the renderer
SDL_RenderClear(ctx.rend);
// MOVEMENT
keys(&dir, &close_requested);
player.vel.x = 0;
player.vel.y = 0;
if (dir & UP) {
player.vel.y = -SPEED;
player.vel.x = SPEED;
}
if (dir & DOWN) {
player.vel.y = SPEED;
player.vel.x = -SPEED;
}
if (dir & LEFT) {
player.dir.theta -= 0.04;
}
if (dir & RIGHT) {
player.dir.theta += 0.04;
}
correct_theta(&player.dir.theta);
player.dir.x = cos(player.dir.theta);
player.dir.y = -sin(player.dir.theta);
// print("\r%f", hypot(player.dir.x, player.dir.y));
player.pos.x += player.vel.x * player.dir.x / FPS;
player.pos.y += player.vel.y * player.dir.y / FPS;
// sets players true centre, creats rect, player.c
ctx.player.rect = centrePlayer(ctx.player.rect, player.pos);
struct { int x, y; } grid_pos = {(int)player.pos.x/GRID, (int)player.pos.y/GRID};
// checking for collisions
collision(&player.pos);
//modifies player's position's pointer rather than copying over
//variables from a function, much faster!
// printing on screen
char position_text[128];
sprintf(position_text, "(%u, %u) : (%u, %u) : (%lf PI) : (%02u)",
(ufast)player.pos.x, (ufast)player.pos.y,
grid_pos.x, grid_pos.y, player.dir.theta / PI,
(ufast)(frame % FPS));
// sprintf makes position_text a char pointer holding the formatted string
// TODO -------- Make text creation into a function, easier for much text e.g. UI
Sprite message = { 0 };
message.surface = TTF_RenderText_Solid(font, position_text, WHITE);
message.texture = SDL_CreateTextureFromSurface(ctx.rend, message.surface);
message.rect = ((SDL_Rect){100, 0, 600, 100});
SDL_SetRenderDrawColor(ctx.rend, DRAW_COLOUR, SDL_ALPHA_OPAQUE);
// PRINTING THE MAP TO THE SCREEN
SDL_Rect rect_map;
rect_map.w = 100;
rect_map.h = 100;
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
if (WORLD_MAP[i][j] == 0) SDL_SetRenderDrawColor(ctx.rend, FLOOR_COLOUR, SDL_ALPHA_OPAQUE);
if (WORLD_MAP[i][j] == 1) SDL_SetRenderDrawColor(ctx.rend, WALL_COLOUR, SDL_ALPHA_OPAQUE);
rect_map.x = j * 100;
rect_map.y = i * 100;
SDL_RenderDrawRect(ctx.rend, &rect_map);
SDL_RenderFillRect(ctx.rend, &rect_map);
SDL_SetRenderDrawColor(ctx.rend, LINE_COLOUR, 100 + 45 * sin(frame / 20.0));
SDL_RenderDrawRect(ctx.rend, &rect_map);
}
}
struct { int left, right, top, bottom; } wall = {
grid_pos.x * GRID,
(grid_pos.x + 1) * GRID,
grid_pos.y * GRID,
(grid_pos.y + 1) * GRID
};
UNUSED(wall);
// RAY CASTING
int i;
Direction temp_dir = player.dir;
temp_dir.theta -= ((f64)FOV/2) * 0.01;
correct_theta(&temp_dir.theta);
for (i = 0; i <= FOV; i++) {
Ray ray = cast_ray(player.pos, temp_dir);
u8 alpha = 60 + 70 * (1 + sin(frame / 10.0 + i / 46.0)) / 2;
SDL_SetRenderDrawColor(ctx.rend, 255, 228, 180, alpha); //white
SDL_RenderDrawLine(ctx.rend,
player.pos.x, player.pos.y,
ray.pos.x, ray.pos.y);
temp_dir.theta += 0.01;
correct_theta(&temp_dir.theta);
}
//Drawing the pictures
SDL_SetRenderDrawColor(ctx.rend, DRAW_COLOUR, SDL_ALPHA_OPAQUE); //background colour, last thing called
SDL_RenderCopyEx(ctx.rend, ctx.player.texture, NULL, &ctx.player.rect, 90 + player.dir.theta * 180.0/PI,
NULL, SDL_FLIP_NONE);
SDL_RenderCopy(ctx.rend, message.texture, NULL, &message.rect);
// draw the image to the window
SDL_RenderPresent(ctx.rend);
SDL_Delay(1000 / FPS);
}
// Proper closing down
SDL_DestroyWindow(ctx.win);
SDL_Quit();
return OK;
}