← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 767108ac6b016cc31cafe393d3b5ad6a7005b060 (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
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdbool.h>
#include <math.h>
// TODO FIRSST get rendering and movement around map working. DONE DONE DONE

// Improving movement with strafing.

// TODO Enemies, Load maps. Weapons

// Map loading - needs to save maps to files. Maybe implement a map editor

// Enemies - Some kind of AI? gotta shoot at you and do damage. Maybe less chance to hit when moving

// Weapons - need some sort of graphic for different weapon types. Have to shoot and be able to hit things. Hitboxes for enemies and such.

struct Tile
{
  int type;
  bool discovered;
};

struct Player
{
  float x;
  float y;
  float angle;
  int health;
};

struct Ray
{
  float angle;
  float xDir;
  float yDir;
  float distance;
  int type;
};

float shootRay(float xDir, float yDir, float x, float y, float angle, float baseAngle, float map_depth, int map_w, int map_h, struct Tile map[map_w][map_h], int *type)
  {
    float stepX, stepY, vx, vy, hx, hy;
    bool hit;
    //Checking Vertical Intersections
    if(xDir < 0.0)
      {
        stepX = -1;
        vx = floor(x);
      }
    else
      {
        stepX = 1;
        vx = ceil(x);
      }
    if(yDir < 0.0)
      vy = y - (fabs(vx - x) * fabs(tan(angle)));
    else
      vy = y + (fabs(vx - x) * fabs(tan(angle)));

    //printf("xDir: %f yDir: %f\n", xDir, yDir);
    //printf("--- Vertical check ---\n");
    //printf("x: %f, y: %f", vx, vy);
    if(sqrt(pow(fabs(vx - x), 2) + pow(fabs(vy - y), 2)) > map_depth)
      hit = true;
    else if(xDir < 0.0 && map[(int)vx - 1][(int)vy].type != 0)
        hit = true;
    else if(map[(int)vx][(int)vy].type != 0)
      hit = true;
    else
      hit = false;
    //printf(" hit: %b\n", hit);
    while(!hit)
      {
        vx += stepX;
        if(yDir < 0.0)
          vy -= fabs(tan(angle));
        else
          vy += fabs(tan(angle));
        //printf("x: %f, y: %f", vx, vy);
        if(sqrt(pow(fabs(vx - x), 2) + pow(fabs(vy - y), 2)) > map_depth)
          hit = true;
        else if(xDir < 0.0 && map[(int)vx - 1][(int)vy].type != 0)
            hit = true;
        else if(map[(int)vx][(int)vy].type != 0)
          hit = true;
        //printf(" hit: %b\n", hit);
      }
    //printf("distance: %f\n", sqrt(pow(fabs(vx - x), 2) + pow(fabs(vy - y), 2)));
    //printf("--- Done ---\n\n");

    //Checking Horizontal Intersections
    if(yDir < 0.0)
      {
        stepY = -1;
        hy = floor(y);
      }
    else
      {
        stepY = 1;
        hy = ceil(y);
      }

    if(xDir < 0.0)
      hx = x - (fabs(hy - y) / fabs(tan(angle)));
    else
      hx = x + (fabs(hy - y) / fabs(tan(angle)));

    //printf("--- Horizontal check ---\n");
    //printf("x: %f, y: %f", hx, hy);
    if(sqrt(pow(fabs(hx - x), 2) + pow(fabs(y - hy), 2)) > map_depth)
      hit = true;
    else if(yDir < 0.0 && map[(int)hx][(int)hy - 1].type != 0)
        hit = true;
    else if(map[(int)hx][(int)hy].type != 0)
      hit = true;
    else
      hit = false;
    //printf(" hit: %b\n", hit);
    while(!hit)
      {
        hy += stepY;
        if(xDir < 0.0)
          hx -= 1/fabs(tan(angle));
        else
          hx += 1/fabs(tan(angle));
        //printf("x: %f, y: %f", hx, hy);
        if(sqrt(pow(fabs(hx - x), 2) + pow(fabs(hy - y), 2)) > map_depth)
            hit = true;
        else if(yDir < 0.0 && map[(int)hx][(int)hy - 1].type != 0)
            hit = true;
        else if(map[(int)hx][(int)hy].type != 0)
          hit = true;
        //printf(" hit: %b\n", hit);
      }
    //printf("distance: %f\n", sqrt(pow(fabs(hx - x), 2) + pow(fabs(hy - y), 2)));
    //printf("--- Done ---\n\n");

    float vdistance = sqrt(pow(fabs(vx - x), 2) + pow(fabs(vy - y), 2)), hdistance = sqrt(pow(fabs(hx - x), 2) + pow(fabs(hy - y), 2));
    if(vdistance < hdistance)
      {
        //if((fabs(ceil(vx) - vx) < 0.05) && (fabs(ceil(vy) - vy) < 0.05))
        //  *type = 0;
        if(xDir < 0.0)
          *type = map[(int)vx - 1][(int)vy].type;
        else
          *type = map[(int)vx][(int)vy].type;
        return(vdistance * cos(angle - baseAngle));
      }
    else
      {
        //if((fabs(ceil(hx) -hx) < 0.05) && (fabs(ceil(hy) - hy) < 0.05))
        //  *type = 0;
        if(yDir < 0.0)
          *type = map[(int)hx][(int)hy - 1].type;
        else
          *type = map[(int)hx][(int)hy].type;
        return(hdistance * cos(angle - baseAngle));
      }
  }

bool gameLoop(int map_w, int map_h, SDL_Renderer *renderer, SDL_Event event, SDL_Window* window, struct Tile map[map_w][map_h])
  {
    int framerate = 30, ticksPerFrame = 1000/framerate, startFrame = 0, ray_n = 150;
    float fov = 90 * (M_PI/180), angle_step = fov/ray_n, map_depth = sqrt(pow(map_w, 2) + pow(map_h, 2)), acceleration = 0.0, sideAcceleration = 0.0, mouseMovement;
    bool end = false;

    SDL_Rect floor = {0, 240, 640, 240};

    // Make minimap squares 60x60 pixels. DONE
    SDL_Rect minimap[map_w][map_h];
    for(int y = 0;y < map_h;y++)
      {
        for(int x = 0;x < map_w;x++)
          {
            minimap[x][y].x = x * 60 + 640;
            minimap[x][y].y = y * 60;
            minimap[x][y].w = 60;
            minimap[x][y].h = 60;
          }
      }

    struct Player player;
    player.x = 3.95;
    player.y = 3.95;
    player.angle = 0.0;
    player.health = 10;

    //struct Ray ray[ray_n];
    struct Ray ray[ray_n + 1];
    SDL_Rect rayDisplay[ray_n + 1];

    while(!end)
      {
        startFrame = SDL_GetTicks();
        SDL_RenderClear(renderer);
        SDL_WarpMouseInWindow(window, 320, 240);

        // Make player shoot out rays from position with an FoV of 60 degrees DONE
        for(int i = 0;i <= ray_n;i++)
          {
            ray[i].angle = player.angle - (fov / 2) + (angle_step * i);
            ray[i].xDir = cos(ray[i].angle);
            ray[i].yDir = sin(ray[i].angle);

            ray[i].distance = shootRay(ray[i].xDir, ray[i].yDir, player.x, player.y, ray[i].angle, player.angle, map_depth, map_w, map_h, map, &ray[i].type);
          }

        /*          --- CONTROLS ---          */
        while(SDL_PollEvent(&event))
          {
            switch(event.type)
              {
                case SDL_MOUSEMOTION:
                  mouseMovement = (float)event.motion.xrel;
                  break;
                case SDL_QUIT:
                  return true;
                  break;
              }
          }
        if(mouseMovement > 10.0 || mouseMovement < -10.0)
          player.angle += mouseMovement / 500;
        const Uint8* keys = SDL_GetKeyboardState(NULL);
        if(keys[SDL_SCANCODE_W])
          {
            if(acceleration < 0.5)
              acceleration += 0.1;
           }
        if(keys[SDL_SCANCODE_S])
          {
            if(acceleration > -0.5)
              acceleration -= 0.1;
          }
        if(keys[SDL_SCANCODE_A])
          {
            if(sideAcceleration < 0.5)
              sideAcceleration -= 0.1;
          }
        if(keys[SDL_SCANCODE_D])
          {
            if(sideAcceleration > -0.5)
              sideAcceleration += 0.1;
          }
        if(keys[SDL_SCANCODE_Q])
          {
            player.angle -= 5 * angle_step;
            if(player.angle < 0.0)
              player.angle += 2 * M_PI;
          }
        if(keys[SDL_SCANCODE_E])
          {
            player.angle += 5 * angle_step;
            if(player.angle > 2 * M_PI)
              player.angle -= 2 * M_PI;
          }

        if(map[(int)(player.x + acceleration * cos(player.angle))][(int)(player.y + acceleration * sin(player.angle))].type == 0)
          {
            player.x += acceleration * cos(player.angle);
            player.y += acceleration * sin(player.angle);
          }
        if(map[(int)(player.x + sideAcceleration * cos(player.angle + M_PI_2))][(int)(player.y + sideAcceleration * sin(player.angle + M_PI_2))].type == 0)
          {
            player.x += sideAcceleration * cos(player.angle + M_PI_2);
            player.y += sideAcceleration * sin(player.angle + M_PI_2);
          }
        if(acceleration > 0.0)
          acceleration -= 0.1;
        else if(acceleration < 0.0)
          acceleration += 0.1;
        if(acceleration < 0.01 && acceleration > -0.01)
          acceleration = 0.0;

        if(sideAcceleration > 0.0)
          sideAcceleration -= 0.1;
        else if(sideAcceleration < 0.0)
          sideAcceleration += 0.1;
        if(sideAcceleration < 0.01 && sideAcceleration > -0.01)
          sideAcceleration = 0.0;

        /*          --- RENDERING ---          */
        SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
        SDL_RenderFillRect(renderer, &floor);
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        for(int y = 0;y < map_h;y++)
          {
            for(int x = 0;x < map_w;x++)
              {
                if(map[x][y].type == 0)
                  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                if(map[x][y].type == 1)
                  SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
                if(map[x][y].type == 2)
                  SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
                if(map[x][y].type == 3)
                  SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
                SDL_RenderFillRect(renderer, &minimap[x][y]);
                SDL_SetRenderDrawColor(renderer, 100, 100, 100, 50);
                SDL_RenderDrawLine(renderer, (x * 60) + 640, y * 60, (x * 60) + 700, y * 60);
                SDL_RenderDrawLine(renderer, (x * 60) + 640, y * 60, (x * 60) + 640, (y * 60) + 60);
                SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
              }
          }

        for(int i = 0;i <= ray_n;i++)
          {
            rayDisplay[i].w = 640 / ray_n;
            rayDisplay[i].h = 400 / ray[i].distance;
            rayDisplay[i].x = i * (640 / ray_n);
            rayDisplay[i].y = 240 - (rayDisplay[i].h / 2);
            if(ray[i].type == 0)
              SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
            else if(ray[i].type == 1)
              SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
            else if(ray[i].type == 2)
              SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
            else if(ray[i].type == 3)
              SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
            SDL_RenderFillRect(renderer, &rayDisplay[i]);
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
            SDL_RenderDrawLine(renderer, (player.x * 60) + 640, player.y * 60, ((player.x + cos(ray[i].angle) * (ray[i].distance / cos(ray[i].angle - player.angle))) * 60) + 640, (player.y + sin(ray[i].angle) * (ray[i].distance / cos(ray[i].angle - player.angle))) * 60);
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
          }

        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
        SDL_RenderDrawLine(renderer, (player.x * 60) + 640, player.y * 60, ((player.x + cos(player.angle)) * 60) + 640, (player.y + sin(player.angle)) * 60);
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderPresent(renderer);
        int ticks = startFrame - SDL_GetTicks();
        if(ticks < ticksPerFrame)
          SDL_Delay(ticksPerFrame - ticks);
      }
    return true;
  }

int main()
  {
    int map_w = 8, map_h = 8;
    struct Tile map[map_w][map_h];
    bool quit = false;
    for(int y = 0;y < map_h;y++)
      {
        for(int x = 0;x < map_w;x++)
          {
            if(x == 0 || x == map_w - 1 || y == 0 || y == map_h - 1)
              map[x][y].type = 1;
            else if(x == 3 && y == 5)
              map[x][y].type = 2;
            else if(x == 2 && y >= 2 && y <= 6)
              map[x][y].type = 3;
            else
              map[x][y].type = 0;
          }
      }

    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();

    window = SDL_CreateWindow("Doom", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640 + 480, 480, SDL_WINDOW_OPENGL);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRelativeMouseMode(SDL_TRUE);

    while(!quit)
      {
        quit = gameLoop(map_w, map_h, renderer, event, window, map);
      }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
  }