summaryrefslogtreecommitdiff
path: root/src/engine.c
blob: d8df9b3cdd1ccd867b39a5cff2797127e61a031a (plain) (blame)
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
#include "engine.h"
#include "block.h"
#include "chunk.h"
#include "window.h"
#include <junk/vector.h>

int engine_init(struct engine *engine) {
    // Setup the Window
    struct window* window = malloc(sizeof(struct window));
    memset(window, 0, sizeof(struct window));
    if (window_init(window) != 0) {
        free(window);
        return -1;
    }
    engine->window = window;

    // Setup Shader pipeline
    struct shader* shader = malloc(sizeof(struct shader));
    memset(shader, 0, sizeof(struct shader));
    if (shader_init(shader)) {
        free(window);
        free(shader);
        return -1;
    };
    engine->shader = shader;

    // Setup Objects to draw
    vector_init(&engine->objects);
    // Setup root chunk
    struct chunk* chunk = malloc(sizeof(struct chunk));
    chunk_gen(NULL, chunk);

    int counter = 0;
    for (int i = 0; i < CHUNK_WIDTH; i++) {
        for (int j = 0; j < CHUNK_LENGTH; j++) {
            for (int k = 0; k < CHUNK_HEIGHT; k++) {
                struct block* blk = chunk->blocks[i][j][k];
                if (blk == NULL) {
                    continue;
                }
                if (VECTOR_INSERT(engine->objects, (void*)blk) == -1) exit(1);
                // block_debug(blk);
                counter += 1;
            }
        }
    }

    engine->game_loop = 1;
    return 0;
}

void engine_draw(struct engine* engine) {
    while (engine->game_loop) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            // Quit game
            if (event.type == SDL_QUIT) {
                engine->game_loop = 0;
            }
        }
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);
        //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
        glUseProgram(engine->shader->program);
        for (int i = 0; i < vector_length(engine->objects); i++) {
            struct block* block = vector_get(engine->objects, i);
            block_draw(block, engine->shader);
        }
        SDL_RenderPresent(engine->window->renderer);
    }
}