From 9c5767436d48608cb5996dd1a6ccff9465c55a2d Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 27 Jun 2022 19:12:26 +0000 Subject: [PATCH] Add easy log macros; add vec_init; windows managed by vec now --- Makefile | 4 +-- source/engine/font.c | 17 ---------- source/engine/log.h | 4 +++ source/engine/openglrender.c | 5 +++ source/engine/texture.c | 2 +- source/engine/vec.c | 10 ++++++ source/engine/vec.h | 7 ++-- source/engine/window.c | 64 ++++++++++++++++++++++-------------- source/engine/window.h | 1 + 9 files changed, 67 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index e2941b7..f687a7d 100755 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ UNAME_P != uname -m CCACHE = ccache #CC specifies which compiler we're using -CC = $(CCACHE) clang +CC = $(CCACHE) tcc MUSL = /usr/local/musl/include @@ -95,7 +95,7 @@ ifeq ($(UNAME), Windows_NT) CLIBS = glew32 EXT = .exe else - LINKER_FLAGS = -fuse-ld=lld #/usr/local/lib/tcc/bcheck.o /usr/local/lib/tcc/bt-exe.o /usr/local/lib/tcc/bt-log.o + LINKER_FLAGS = -g #/usr/local/lib/tcc/bcheck.o /usr/local/lib/tcc/bt-exe.o /usr/local/lib/tcc/bt-log.o ELIBS = m c engine editor glfw3 CLIBS = EXT = diff --git a/source/engine/font.c b/source/engine/font.c index b2f08fa..d8b9daf 100755 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -46,16 +46,6 @@ struct sFont *MakeFont(const char *fontfile, int height) printf("failed\n"); } - int tw,th; - - unsigned char *testbitmap = stbtt_GetCodepointBitmap(&fontinfo, 0, stbtt_ScaleForPixelHeight(&fontinfo, 100), 'G', &tw, &th, 0,0); - - for (int i = 0; i < th; ++i) { - for (int j = 0; j>5]); - putchar('\n'); - } - float scale = stbtt_ScaleForPixelHeight(&fontinfo, 64); int ascent, descent, linegap; @@ -75,13 +65,6 @@ struct sFont *MakeFont(const char *fontfile, int height) bitmap = stbtt_GetCodepointBitmap(&fontinfo, 0, stbtt_ScaleForPixelHeight(&fontinfo, newfont->height), c, &w, &h, 0, 0); - -for (int i = 0; i < h; ++i) { - for (int j = 0; j>5]); - putchar('\n'); - } - GLuint ftexture; glGenTextures(1, &ftexture); glBindTexture(GL_TEXTURE_2D, ftexture); diff --git a/source/engine/log.h b/source/engine/log.h index fcc707d..3c05d6d 100755 --- a/source/engine/log.h +++ b/source/engine/log.h @@ -9,6 +9,10 @@ #define LOG_CRITICAL 3 #define YughLog(cat, pri, msg, ...) mYughLog(cat, pri, __LINE__, __FILE__, msg, __VA_ARGS__) +#define YughInfo(msg, ...) mYughLog(0, 0, __LINE__, __FILE__, msg, __VA_ARGS__); +#define YughWarn(msg, ...) mYughLog(0, 1, __LINE__, __FILE__, msg, __VA_ARGS__); +#define YughError(msg, ...) mYughLog(0, 2, __LINE__, __FILE__, msg, __VA_ARGS__); +#define YughCritical(msg, ...) mYughLog(0, 3, __LINE__, __FILE__, msg, __VA_ARGS__); void mYughLog(int category, int priority, int line, const char *file, const char *message, ...); diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 0c016fd..b97cde8 100755 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -94,6 +94,11 @@ static unsigned int projUBO; void openglInit() { + if (!mainwin) { + YughError("No window to init OpenGL on.", 1); + abort(); + } + sprite_initialize(); ////// MAKE SHADERS diff --git a/source/engine/texture.c b/source/engine/texture.c index b55e2c3..4785b9d 100755 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -41,7 +41,7 @@ struct Texture *texture_pullfromfile(const char *path) shput(texhash, tex->path, tex); - glGenTextures(1, &tex->id); + //glGenTextures(1, &tex->id); return tex; } diff --git a/source/engine/vec.c b/source/engine/vec.c index 1909604..6008a13 100755 --- a/source/engine/vec.c +++ b/source/engine/vec.c @@ -3,6 +3,16 @@ #include #include +struct vec vec_init(size_t width, int size) +{ + struct vec v; + v.size = size; + v.width = width; + v.len = 0; + v.data = calloc(v.size, v.width); + return v; +} + struct vec *vec_make(size_t width, int size) { struct vec *new = calloc(1, sizeof(struct vec)); diff --git a/source/engine/vec.h b/source/engine/vec.h index d22867d..d123272 100755 --- a/source/engine/vec.h +++ b/source/engine/vec.h @@ -4,12 +4,13 @@ #include struct vec { - int len; - int size; - size_t width; + int len; // How many elements are in the vec + int size; // The capacity of the vec + size_t width; // The size in bytes of an element of the vector void *data; }; +struct vec vec_init(size_t width, int size); struct vec *vec_make(size_t width, int size); void *vec_get(struct vec *vec, int n); void vec_walk(struct vec *vec, void (*fn)(void *data)); diff --git a/source/engine/window.c b/source/engine/window.c index c8254d5..340f8fa 100755 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -8,42 +8,60 @@ #include #include -struct mSDLWindow *mainwin; +#include -static struct mSDLWindow *windows[5]; -static int numWindows = 0; +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) { - struct mSDLWindow *w = calloc(1, sizeof(struct mSDLWindow)); - w->width = width; - w->height = height; - w->window = glfwCreateWindow(width, height, name, NULL, NULL); + struct mSDLWindow *w; - if (!w->window) { - printf("Couldn't make GLFW window\n"); + if (windows.data == NULL) { + windows = vec_init(sizeof(struct mSDLWindow), 5); + w = vec_add(&windows, NULL); + mainwin = w; } else { - glfwMakeContextCurrent(w->window); - gladLoadGL(glfwGetProcAddress); - - glfwSwapInterval(1); - - w->id = numWindows; - - - if (numWindows < 5) - windows[numWindows++] = w; - + w = vec_add(&windows, NULL); } - mainwin = windows[0]; + GLFWwindow *sharewin = mainwin ? NULL : mainwin->window; + + w->width = width; + w->height = height; + printf("NUmber of windows: %d.\n", windows.len); + w->id = windows.len-1; + + w->window = glfwCreateWindow(width, height, name, NULL, sharewin); + + if (!w->window) { + YughError("Couldn't make GLFW window\n", 1); + return w; + } + + if (icon) window_seticon(w, icon); + + glfwMakeContextCurrent(w->window); + gladLoadGL(glfwGetProcAddress); + glfwSwapInterval(1); + + + return w; } +void window_set_icon(const char *png) +{ + icon = texture_pullfromfile(png); +} + void window_destroy(struct mSDLWindow *w) { glfwDestroyWindow(w->window); + vec_delete(&windows, w->id); } void window_handle_event(struct mSDLWindow *w) @@ -131,9 +149,7 @@ void window_handle_event(struct mSDLWindow *w) void window_all_handle_events() { - for (int i = 0; i < numWindows; i++) { - window_handle_event(windows[i]); - } + vec_walk(&windows, window_handle_event); } void window_makefullscreen(struct mSDLWindow *w) diff --git a/source/engine/window.h b/source/engine/window.h index 07aec86..8d20d0b 100755 --- a/source/engine/window.h +++ b/source/engine/window.h @@ -28,6 +28,7 @@ extern struct mSDLWindow *mainwin; 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); void window_all_handle_events();