~nch/gamelog

ref: 75432213d3972f26c03ef636da68ed482f0ac908 gamelog/gamelog.cc -rw-r--r-- 6.8 KiB View raw
75432213 — nc refactoring debug code out a bit 5 months ago
                                                                                
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
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HANDMADE_MATH_IMPLEMENTATION
#include "HandmadeMath.h"
#include <string.h>
#include <typeinfo>
#include <lua5.3/lua.hpp>

typedef unsigned int uint;

void print(hmm_mat4 m) {
    printf("(");
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++)
            printf("%f ", m[j][i]);
        printf("\n");
    }
    printf(")\n");
}

static void show_info_log(
        GLuint object,
        PFNGLGETSHADERIVPROC glGet__iv,
        PFNGLGETSHADERINFOLOGPROC glGet__InfoLog
        )
{
    GLint log_length;
    char *log;

    glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length);
    log = (char *) malloc(log_length);
    glGet__InfoLog(object, log_length, NULL, log);
    fprintf(stderr, "%s", log);
    free(log);
}

int compile_shader(const char *source, uint type) {
    int len = strlen(source);
    uint shader = glCreateShader(type);
    glShaderSource(shader, 1, &source, &len);
    glCompileShader(shader);

    int status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if(status == GL_FALSE) {
        if(type == GL_VERTEX_SHADER) {
            fprintf(stderr, "Error while compiling VERTEX_SHADER: ");
        } else if (type == GL_FRAGMENT_SHADER) {
            fprintf(stderr, "Error while compiling FRAGMENT_SHADER: ");
        }
        show_info_log(shader, glGetShaderiv, glGetShaderInfoLog);
        glDeleteShader(shader);
        return 0; // failed.
    }
    return shader;
}

uint make_program(uint v_shader, uint f_shader) {
    uint p = glCreateProgram();
    glAttachShader(p, v_shader);
    glAttachShader(p, f_shader);
    glLinkProgram(p);
    glDeleteShader(v_shader);
    glDeleteShader(f_shader);

    int program_ok;
    glGetProgramiv(p, GL_LINK_STATUS, &program_ok);
    if(!program_ok) { // lovely. More bloated error checking.
        show_info_log(p, glGetProgramiv, glGetProgramInfoLog);
        glDeleteProgram(p);
        return 0;
    }

    return p;
}

void message_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, GLchar const* message, void const* user_param)
{
    switch (source)
    {
        case GL_DEBUG_SOURCE_API: fprintf(stderr, "API"); break;
        case GL_DEBUG_SOURCE_WINDOW_SYSTEM: fprintf(stderr, "WINDOW SYSTEM"); break;
        case GL_DEBUG_SOURCE_SHADER_COMPILER: fprintf(stderr, "SHADER COMPILER"); break;
        case GL_DEBUG_SOURCE_THIRD_PARTY: fprintf(stderr, "THIRD PARTY"); break;
        case GL_DEBUG_SOURCE_APPLICATION: fprintf(stderr, "APPLICATION"); break;
        case GL_DEBUG_SOURCE_OTHER: fprintf(stderr, "OTHER"); break;
    }

    fprintf(stderr, " ");

    switch (type)
    {
        case GL_DEBUG_TYPE_ERROR: fprintf(stderr, "ERROR"); break;
        case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: fprintf(stderr, "DEPRECATED_BEHAVIOR"); break;
        case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: fprintf(stderr, "UNDEFINED_BEHAVIOR"); break;
        case GL_DEBUG_TYPE_PORTABILITY: fprintf(stderr, "PORTABILITY"); break;
        case GL_DEBUG_TYPE_PERFORMANCE: fprintf(stderr, "PERFORMANCE"); break;
        case GL_DEBUG_TYPE_MARKER: fprintf(stderr, "MARKER"); break;
        case GL_DEBUG_TYPE_OTHER: fprintf(stderr, "OTHER"); break;
    }

    fprintf(stderr, " ");

    switch (severity) {
        case GL_DEBUG_SEVERITY_NOTIFICATION: fprintf(stderr, "NOTIFICATION"); break;
        case GL_DEBUG_SEVERITY_LOW: fprintf(stderr, "LOW"); break;
        case GL_DEBUG_SEVERITY_MEDIUM: fprintf(stderr, "MEDIUM"); break;
        case GL_DEBUG_SEVERITY_HIGH: fprintf(stderr, "HIGH"); break;
    }

    fprintf(stderr, "%u: %s\n", id, message);
}

template<typename T> void pass_to_shader(uint id, const char *binding, T v) {
    uint binding_id = glGetUniformLocation(id, binding);
    if (typeid(T) == typeid(hmm_m4)) {
        glUniformMatrix4fv(binding_id, 1, GL_FALSE, &v.Elements[0][0]);
    }
}

template<typename T> struct vector {
    T* data;
    uint capacity;
    uint len = 0;
    ~vector() {
        delete[] data;
    }
    vector(T d[]) {
        capacity = sizeof(data) / sizeof(T);
        data = new T[capacity];
        memcpy(data, d, sizeof(data));
        data = d;
    }
    vector(uint max_capacity) {
        capacity = max_capacity;
        data = new T[capacity];
    }
    T operator[](uint i) {
        assert(i < capacity);
        return data[i];
    }
    void append(T elem) {
        assert(len + 1 < capacity);
        data[len] = elem;
        len++;
    }
};

GLFWwindow *window;
static int is_down(lua_State *L) {
    const char *keystr = lua_tostring(L, 1);
    lua_pushboolean(L, glfwGetKey(window, keystr[0]) == GLFW_PRESS);
    return 1;
}

int traceback(lua_State *L) {
    const char *message = lua_tostring(L, 1);
    luaL_traceback(L, L, message, 1);
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
    return 1;
}

// ripped from https://www.lua.org/source/5.2/lua.c.html
static int docall(lua_State *L, int narg, int nres) {
    int status;
    int base = lua_gettop(L) - narg;  /* function index */
    lua_pushcfunction(L, traceback);  /* push traceback function */
    lua_insert(L, base);  /* put it under chunk and args */
    status = lua_pcall(L, narg, nres, base);
    lua_remove(L, base);  /* remove traceback function */
    return status;
}

int main() {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    lua_register(L, "is_down", is_down);

    if(luaL_loadfile(L, "./main.lua") != LUA_OK) {
        docall(L, 0, 0);
        exit(1);
    }

    { // init window
        assert(glfwInit());
        glfwWindowHint(GLFW_SAMPLES, 4);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        window = glfwCreateWindow(800, 600, "game", NULL, NULL);
        glfwMakeContextCurrent(window);
    }

    glewExperimental = 1;
    assert(glewInit() == GLEW_OK);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
    glEnable(GL_DEBUG_OUTPUT);
    glEnable(GL_CULL_FACE);
    glEnable(GL_PROGRAM_POINT_SIZE);
    glDebugMessageCallback(message_callback, NULL);
    glDepthFunc(GL_LESS);

    // load lua module
    if(docall(L, 0, 0) != LUA_OK) exit(1);

    lua_getglobal(L, "init");
    assert(!lua_isnil(L, -1) && "draw is nil");
    if(docall(L, 0, 0) != LUA_OK) exit(1);

    do {
        glViewport(0, 0, 800, 600);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.3, 0.3, 0.3, 1.0);

        lua_getglobal(L, "draw");
        assert(!lua_isnil(L, -1) && "draw is nil");
        if(docall(L, 0, 0) != LUA_OK) exit(1);

        glfwSwapBuffers((GLFWwindow *) window);
        glfwPollEvents();
    } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
}