summaryrefslogtreecommitdiff
path: root/src/main.c
blob: f2d69441e06dd16438863662b4cc392eba60e934 (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
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "sector.h"
#include "common.h"

int SW = 1200;
int SH =  800;
char *name = "Thingy";
SDL_Surface *surface = NULL;

#define CLEAR_SCREEN(c) for(int i = 0; i < SW-1; i++) vline(i, 0, SH-1, c);

void print_message(void);

void vline(int x, int y1, int y2, uint32_t col)
{
    y1 = clamp(y1, 0, SH-1);
    y2 = clamp(y2, 0, SH-1);

    uint32_t *pixels = (uint32_t*)surface->pixels;
    for(int y = min(y1, y2); y <= max(y1, y2); y++) {
        pixels[y * SW + x] = SDL_MapRGB(surface->format, color(col));
    }
}

int main(void)
{
    // set logging priority
#ifdef DEBUG
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
#else
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_ERROR);
#endif

    print_message();

    // ----------------- START SDL ----------------- //
    int ret = 1;
    SDL_Window *window = NULL;
    map_t map = {0};

    if(SDL_Init(SDL_INIT_VIDEO) != 0) {
        log_critical(LOG_VIDEO, "SDL_Init: %s", SDL_GetError());
        goto exit;
    }

    window = SDL_CreateWindow(name,
                              SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                              SW, SH, 0);
    if(window == NULL) {
        log_critical(LOG_VIDEO, "SDL_CreateWindow: %s", SDL_GetError());
        goto exit;
    }

    surface = SDL_GetWindowSurface(window);
    if(surface == NULL) {
        log_critical(LOG_VIDEO, "SDL_GetWindowSurface %s", SDL_GetError());
        goto exit;
    }

    SDL_SetRelativeMouseMode(SDL_TRUE);
    // --------------------------------------------- //

    if(map_load(&map, "files/map_clear.txt"))
        return 1;

    bool keys[6] = {0};
    bool quit = false;
    while(!quit)
    {
        SDL_Event event;
        while(SDL_PollEvent(&event)) {
            // ----------------- EVENTS ----------------- //
        #define KEY_SWITCH(b)                       \
            switch(event.key.keysym.sym) {          \
            case 'w': keys[0] = b; break;           \
            case 'a': keys[1] = b; break;           \
            case 's': keys[2] = b; break;           \
            case 'd': keys[3] = b; break;           \
            case ' ': keys[4] = b; break;           \
            case SDLK_LSHIFT:                       \
            case SDLK_RSHIFT: keys[5] = b; break; }

            switch(event.type) {
            case SDL_QUIT:
                quit = true;
                break;
            case SDL_KEYDOWN:
                KEY_SWITCH(true);
                break;
            case SDL_KEYUP:
                KEY_SWITCH(false);
                break;
            default: break;
            }
            // ------------------------------------------ //
        }

        // ----------------- GAME LOOP ----------------- //
        int x,y;
        SDL_GetRelativeMouseState(&x,&y);

        player_input(&map.player, keys, x, y);
        map_detect_collision(&map);
        player_update(&map.player);

        CLEAR_SCREEN(COLOR_LIGHTWHITE);
        map_draw(&map, SW, SH);
        // --------------------------------------------- //

        if(SDL_UpdateWindowSurface(window) != 0) {
            log_error(LOG_VIDEO, "SDL_UpdateWindowSurface: %s", SDL_GetError());
        }
    }

    ret = 0;
exit:
    map_unload(&map);
    if(window != NULL) SDL_DestroyWindow(window);
    SDL_Quit();
    return ret;
}

void print_message(void)
{
    puts("--------------- Instructions ---------------");
    puts("Use WASD to move");
    puts("Use the mouse to look");
    puts("Use SPACE to jump");
    puts("Use SHIFT to duck");
    puts("");
    puts("There is collision, but it is very buggy");
    puts("--------------------------------------------");
}