From 2bc9bb65af7bad3054f308bda641c6d2886e9d2e Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 30 Jun 2022 15:31:23 +0000 Subject: [PATCH] X --- Makefile | 2 +- source/editor/Nuklear/nuklear.h | 1 + source/engine/engine.c | 4 +-- source/engine/font.c | 38 ++++++++++------------- source/engine/font.h | 12 +++----- source/engine/openglrender.c | 46 ++++++++++++++-------------- source/engine/shader.c | 36 ++++++++-------------- source/engine/sound.c | 4 ++- source/engine/sprite.c | 14 +++------ source/engine/sprite.h | 6 ++-- source/engine/timer.c | 8 +++++ source/engine/vec.c | 15 ++++++--- source/engine/vec.h | 4 +-- source/engine/window.c | 54 +++++++++++++++++++++++++++++++-- source/engine/window.h | 5 ++- 15 files changed, 143 insertions(+), 106 deletions(-) diff --git a/Makefile b/Makefile index 94ff0ac..43fe02c 100755 --- a/Makefile +++ b/Makefile @@ -158,7 +158,7 @@ bin/libglfw3.a: $(objprefix)/%.o:%.c @mkdir -p $(@D) @echo Making C object $@ - -@$(CC) $(COMPILER_FLAGS) + @$(CC) $(COMPILER_FLAGS) tags: $(ALLFILES) @echo Making tags diff --git a/source/editor/Nuklear/nuklear.h b/source/editor/Nuklear/nuklear.h index aef8f7f..0de0d00 100644 --- a/source/editor/Nuklear/nuklear.h +++ b/source/editor/Nuklear/nuklear.h @@ -6078,6 +6078,7 @@ NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_prop #define STB_RECT_PACK_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION +#define STBTT_STATIC /* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */ #ifndef STBTT_malloc diff --git a/source/engine/engine.c b/source/engine/engine.c index 86a32e0..f5f522b 100755 --- a/source/engine/engine.c +++ b/source/engine/engine.c @@ -3,8 +3,8 @@ #define STB_DS_IMPLEMENTATION #include -//#define STB_TRUETYPE_IMPLEMENTATION -//#include +#define STB_TRUETYPE_IMPLEMENTATION +#include #define PL_MPEG_IMPLEMENTATION #include diff --git a/source/engine/font.c b/source/engine/font.c index d8b9daf..0aad613 100755 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -9,8 +9,6 @@ #include #include -#define STB_TRUETYPE_IMPLEMENTATION -#define STBTT_STATIC #include static uint32_t VBO = 0; @@ -22,13 +20,12 @@ unsigned char temp_bitmap[512 * 512]; struct sFont *font; static struct mShader *shader; -void font_init() { - shader = MakeShader("textvert.glsl", "textfrag.glsl"); +void font_init(struct mShader *textshader) { + shader = textshader; } void font_frame(struct mSDLWindow *w) { shader_use(shader); - shader_setmat4(shader, "projection", w->projection); } struct sFont *MakeFont(const char *fontfile, int height) @@ -90,8 +87,7 @@ struct sFont *MakeFont(const char *fontfile, int height) glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 4, NULL, - GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 4, NULL, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); @@ -102,8 +98,7 @@ struct sFont *MakeFont(const char *fontfile, int height) return newfont; } -void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, - struct mShader *shader, float color[3]) +void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct mShader *shader, float color[3]) { float xpos = cursor[0] + c.Bearing[0] * scale; float ypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale; @@ -118,6 +113,8 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, xpos + w, ypos, 1.f, 1.f }; + +////// Outline calculation // float outlineWidth = 1.1; // float ow = c.Size[0] * scale * outlineWidth; @@ -126,8 +123,6 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, // float oxpos = cursor[0] + c.Bearing[0] * scale * outlineWidth - ((ow-w)/2); // float oypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale * outlineWidth - ((oh-h)/2); - - // float overts[4*4] = { // oxpos, oypos + oh, 0.f, 0.f, // oxpos, oypos, 0.f, 1.f, @@ -135,12 +130,13 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, // oxpos + ow, oypos, 1.f, 1.f // }; + +/////////// Shadow calculation +/* + float shadowOffset = 6.f; - float sxpos = - cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset); - float sypos = - cursor[1] - (c.Size[1] - c.Bearing[1]) * scale - - (scale * shadowOffset); + float sxpos = cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset); + float sypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale - (scale * shadowOffset); float sverts[4 * 4] = { sxpos, sypos + h, 0.f, 0.f, @@ -148,22 +144,20 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, sxpos + w, sypos + h, 1.f, 0.f, sxpos + w, sypos, 1.f, 1.f }; - +*/ glBindTexture(GL_TEXTURE_2D, c.TextureID); //// Shadow pass + /* float black[3] = { 0, 0, 0 }; shader_setvec3(shader, "textColor", black); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(sverts), sverts); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + */ - //// Outline pass - - - + //// Character pass shader_setvec3(shader, "textColor", color); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); diff --git a/source/engine/font.h b/source/engine/font.h index dfa825c..b3230a8 100755 --- a/source/engine/font.h +++ b/source/engine/font.h @@ -4,6 +4,7 @@ #include "mathc.h" struct mShader; +struct mSDLWindow; /// Holds all state information relevant to a character as loaded using FreeType struct Character { @@ -19,17 +20,14 @@ struct sFont { struct Character Characters[127]; }; -struct mSDLWindow; -void font_init(); + +void font_init(struct mShader *s); void font_frame(struct mSDLWindow *w); struct sFont *MakeFont(const char *fontfile, int height); -void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, - struct mShader *shader, float color[3]); +void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct mShader *shader, float color[3]); void text_settype(struct sFont *font); -void renderText( - const char *text, mfloat_t pos[2], float scale, - mfloat_t color[3], float lw); +void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw); #endif diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index b97cde8..b21ecf1 100755 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -96,28 +96,25 @@ void openglInit() { if (!mainwin) { YughError("No window to init OpenGL on.", 1); - abort(); + exit(1); } - sprite_initialize(); - ////// MAKE SHADERS outlineShader = MakeShader("outlinevert.glsl", "outline.glsl"); - spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl"); - animSpriteShader = - MakeShader("animspritevert.glsl", "animspritefrag.glsl"); + animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl"); textShader = MakeShader("textvert.glsl", "textfrag.glsl"); + + + font_init(textShader); + sprite_initialize(); debugdraw_init(); - stdFont = MakeFont("notosans.ttf", 300); - text_settype(stdFont); //glEnable(GL_STENCIL_TEST); - glClearColor(editorClearColor[0], editorClearColor[1], - editorClearColor[2], editorClearColor[3]); + glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -131,8 +128,7 @@ void openglInit() glGenBuffers(1, &projUBO); glBindBuffer(GL_UNIFORM_BUFFER, projUBO); glBufferData(GL_UNIFORM_BUFFER, 64, NULL, GL_DYNAMIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 0, projUBO, 0, - sizeof(float) * 16); + glBindBufferRange(GL_UNIFORM_BUFFER, 0, projUBO, 0, sizeof(float) * 16); glBindBuffer(GL_UNIFORM_BUFFER, 0); shader_setUBO(spriteShader, "Projection", 0); @@ -154,33 +150,37 @@ void openglRender(struct mSDLWindow *window, struct mCamera *mcamera) glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection); - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - glCullFace(GL_BACK); + + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glCullFace(GL_BACK); // Clear color and depth - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | - GL_STENCIL_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); ////// TEXT && GUI glDepthFunc(GL_ALWAYS); - //shader_use(textShader); + shader_use(textShader); //shader_setmat4(textShader, "projection", window->projection); - //mfloat_t fontpos[2] = { 25.f, 25.f }; - //mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f }; - //renderText("Sample text", fontpos, 0.4f, fontcolor, -1.f); + mfloat_t fontpos[2] = { 25.f, 25.f }; + mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f }; + renderText("Sample text", fontpos, 4.f, fontcolor, -1.f); -/* + ///// Sprites + + shader_use(spriteShader); + shader_setmat4(spriteShader, "projection", window->projection); for (int i = 0; i < numSprites; i++) { sprite_draw(sprites[i]); } - */ + //glDepthFunc(GL_LESS); diff --git a/source/engine/shader.c b/source/engine/shader.c index e7b75d9..e17fd2f 100755 --- a/source/engine/shader.c +++ b/source/engine/shader.c @@ -75,7 +75,7 @@ GLuint load_shader_from_file(const char *path, int type) const char *code = buf; glShaderSource(id, 1, &code, NULL); glCompileShader(id); - if ( shader_compile_error(id)) + if (shader_compile_error(id)) return 0; return id; @@ -116,51 +116,39 @@ void shader_setfloat(struct mShader *shader, const char *name, float val) glUniform1f(glGetUniformLocation(shader->id, name), val); } -void shader_setvec2(struct mShader *shader, const char *name, - mfloat_t val[2]) +void shader_setvec2(struct mShader *shader, const char *name, mfloat_t val[2]) { glUniform2fv(glGetUniformLocation(shader->id, name), 1, val); } -void shader_setvec3(struct mShader *shader, const char *name, - mfloat_t val[3]) +void shader_setvec3(struct mShader *shader, const char *name, mfloat_t val[3]) { glUniform3fv(glGetUniformLocation(shader->id, name), 1, val); } -void shader_setvec4(struct mShader *shader, const char *name, - mfloat_t val[4]) +void shader_setvec4(struct mShader *shader, const char *name, mfloat_t val[4]) { glUniform4fv(glGetUniformLocation(shader->id, name), 1, val); } -void shader_setmat2(struct mShader *shader, const char *name, - mfloat_t val[4]) +void shader_setmat2(struct mShader *shader, const char *name, mfloat_t val[4]) { - glUniformMatrix2fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, - val); + glUniformMatrix2fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val); } -void shader_setmat3(struct mShader *shader, const char *name, - mfloat_t val[9]) +void shader_setmat3(struct mShader *shader, const char *name, mfloat_t val[9]) { - glUniformMatrix3fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, - val); + glUniformMatrix3fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val); } -void shader_setmat4(struct mShader *shader, const char *name, - mfloat_t val[16]) +void shader_setmat4(struct mShader *shader, const char *name, mfloat_t val[16]) { - shader_use(shader); - glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, - val); + glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val); } -void shader_setUBO(struct mShader *shader, const char *name, - unsigned int index) +void shader_setUBO(struct mShader *shader, const char *name, unsigned int index) { - glUniformBlockBinding(shader->id, - glGetUniformBlockIndex(shader->id, name), index); + glUniformBlockBinding(shader->id, glGetUniformBlockIndex(shader->id, name), index); } diff --git a/source/engine/sound.c b/source/engine/sound.c index 0a0522d..b499c7e 100755 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -1,6 +1,7 @@ #include "sound.h" #include "resources.h" #include +#include "log.h" ma_engine engine; @@ -34,7 +35,8 @@ void sound_init() ma_result result = ma_engine_init(NULL, &engine); if (result != MA_SUCCESS) { - printf("UHOH!!!"); + YughError("Miniaudio did not start properly.",1); + exit(1); } ma_sound_group_init(&engine, 0, NULL, &mus_grp); diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 88aa12c..bfa8709 100755 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -95,8 +95,7 @@ void sprite_initialize() glBindVertexArray(quadVAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, - GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); @@ -111,7 +110,7 @@ void sprite_draw(struct mSprite *sprite) { if (sprite->tex != NULL) { - shader_use(spriteShader); + //shader_use(spriteShader); mfloat_t model[16] = { 0.f }; @@ -126,10 +125,8 @@ void sprite_draw(struct mSprite *sprite) mfloat_t t_move[2] = { 0.f }; mfloat_t t_scale[2] = { 0.f }; - t_scale[0] = - sprite->size[0] * sprite->tex->width * sprite->go->scale; - t_scale[1] = - sprite->size[1] * sprite->tex->height * sprite->go->scale; + t_scale[0] = sprite->size[0] * sprite->tex->width * sprite->go->scale; + t_scale[1] = sprite->size[1] * sprite->tex->height * sprite->go->scale; t_move[0] = sprite->pos[0] * t_scale[0]; t_move[1] = sprite->pos[1] * t_scale[1]; @@ -184,8 +181,7 @@ sprite->size[1] * sprite->anim.dimensions[1] }; } -void video_draw(struct datastream *stream, mfloat_t position[2], - mfloat_t size[2], float rotate, mfloat_t color[3]) +void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3]) { shader_use(spriteShader); diff --git a/source/engine/sprite.h b/source/engine/sprite.h index c9610b3..2f555cb 100755 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -35,14 +35,12 @@ extern int numSprites; struct mSprite *MakeSprite(struct mGameObject *go); void sprite_init(struct mSprite *sprite, struct mGameObject *go); void sprite_loadtex(struct mSprite *sprite, const char *path); -void sprite_loadanim(struct mSprite *sprite, const char *path, - struct Anim2D anim); +void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim); void sprite_settex(struct mSprite *sprite, struct Texture *tex); void sprite_initialize(); void sprite_draw(struct mSprite *sprite); void spriteanim_draw(struct mSprite *sprite); -void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], - float rotate, mfloat_t color[3]); +void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]); unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite); struct mSprite *gui_makesprite(); diff --git a/source/engine/timer.c b/source/engine/timer.c index 8ea7c6e..ddcd9f7 100644 --- a/source/engine/timer.c +++ b/source/engine/timer.c @@ -15,6 +15,14 @@ struct timer *timer_make(int interval, void *(*callback)(void *param), void *par return new; } +void timer_pause(struct timer *t) { + +} + +void timer_start(struct timer *t) { + +} + void timer_remove(struct timer *t) { free(t); } \ No newline at end of file diff --git a/source/engine/vec.c b/source/engine/vec.c index 6008a13..1ce148c 100755 --- a/source/engine/vec.c +++ b/source/engine/vec.c @@ -27,9 +27,7 @@ void *vec_get(struct vec *vec, int n) if (n < vec->len) return (char *) vec->data + (n * vec->width); - YughLog(0, 4, - "Attempted to access element %d of a vec with %d elements.", n, - vec->len); + YughLog(0, 4,"Attempted to access element %d of a vec with %d elements.", n, vec->len); return NULL; } @@ -71,8 +69,7 @@ void *vec_add(struct vec *vec, const void *data) return vec_get(vec, vec->len - 1); } -void *vec_add_sort(struct vec *vec, void *data, - int (*sort)(void *a, void *b)) +void *vec_add_sort(struct vec *vec, void *data, int (*sort)(void *a, void *b)) { if(vec->size == vec->len) vec_expand(vec); @@ -84,6 +81,14 @@ void *vec_add_sort(struct vec *vec, void *data, return vec_insert(vec, data, n); } +void *vec_find(struct vec *v, int (*valid)(void *a, void *data), void *data) +{ + for (int i = 0; i < v->len; i++) { + if (valid(vec_p(v, i), data)) + return vec_p(v, i); + } +} + void *vec_insert(struct vec *vec, void *data, int n) { if (vec->size == vec->len) diff --git a/source/engine/vec.h b/source/engine/vec.h index d123272..3b95630 100755 --- a/source/engine/vec.h +++ b/source/engine/vec.h @@ -18,10 +18,10 @@ void vec_clear(struct vec *vec); void *vec_add(struct vec *vec, const void *data); /* sort returns 0 for a<=b, 1 for a>b */ -void *vec_add_sort(struct vec *vec, void *data, - int (*sort)(void *a, void *b)); +void *vec_add_sort(struct vec *vec, void *data, int (*sort)(void *a, void *b)); void *vec_insert(struct vec *vec, void *data, int n); void *vec_set(struct vec *vec, int n, void *data); +void *vec_find(struct vec *v, int (*valid)(void *a, void *data), void *data); void vec_delete(struct vec *vec, int n); void vec_del_order(struct vec *vec, int n); void vec_store(struct vec *vec, FILE *f); diff --git a/source/engine/window.c b/source/engine/window.c index 85380d1..e1ce7a1 100755 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -5,14 +5,56 @@ #include #include #include +#include "input.h" static struct mSDLWindow *mainwin; static struct vec windows; struct Texture *icon = NULL; -struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, - uint32_t flags) +int is_win(struct mSDLWindow *s, GLFWwindow *w) +{ + return s->window == w; +} + +void window_size_callback(GLFWwindow *w) +{ + +} + +void window_iconify_callback(GLFWwindow *w, int iconified) +{ + struct mSDLWindow *win = vec_find(&windows, is_win, w); + win->iconified = iconified; +} + +void window_focus_callback(GLFWwindow *w, int focused) +{ + struct mSDLWindow *win = vec_find(&windows, is_win, w); + win->keyboardFocus = focused; +} + +void window_maximize_callback(GLFWwindow *w, int maximized) +{ + struct mSDLWindow *win = vec_find(&windows, is_win, w); + win->minimized = !maximized; +} + +void window_framebuffer_size_cb(GLFWwindow *w, int width, int height) +{ + struct mSDLWindow *win = vec_find(&windows, is_win, w); + win->width = width; + win->height = height; + window_makecurrent(win); + win->render = 1; +} + +void window_close_callback(GLFWwindow *w) +{ + quit = 1; +} + +struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags) { struct mSDLWindow *w; @@ -44,7 +86,11 @@ struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, gladLoadGL(glfwGetProcAddress); glfwSwapInterval(1); - + // Set callbacks + glfwSetWindowCloseCallback(w->window, window_close_callback); + glfwSetWindowSizeCallback(w->window, window_size_callback); + glfwSetFramebufferSizeCallback(w->window, window_framebuffer_size_cb); + glfwSetWindowFocusCallback(w->window, window_focus_callback); return w; } @@ -60,6 +106,8 @@ void window_destroy(struct mSDLWindow *w) vec_delete(&windows, w->id); } + + void window_handle_event(struct mSDLWindow *w) { /* diff --git a/source/engine/window.h b/source/engine/window.h index 8d20d0b..e696ca1 100755 --- a/source/engine/window.h +++ b/source/engine/window.h @@ -16,6 +16,7 @@ struct mSDLWindow { bool keyboardFocus; bool fullscreen; bool minimized; + bool iconified; bool shown; float projection[16]; }; @@ -25,9 +26,7 @@ struct Texture; extern struct mSDLWindow *mainwin; - -struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, - uint32_t flags); +struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags); void window_set_icon(const char *png); void window_destroy(struct mSDLWindow *w); void window_handle_event(struct mSDLWindow *w);