From 36965e523b3ca1f20fa0b82993711f092edbad10 Mon Sep 17 00:00:00 2001 From: ColleagueRiley Date: Thu, 2 Jan 2025 18:00:38 -0500 Subject: [PATCH] remove opengl more --- deps/RFont.h | 155 +++++++++++++----- examples/advanced/RSGL_rsoft.h | 5 +- examples/advanced/glfw.c | 1 + examples/advanced/rsoft.c | 4 +- examples/advanced/shader.c | 1 + examples/basics/basic.c | 2 + examples/basics/shapes.c | 2 + examples/basics/text.c | 11 +- examples/basics/textures.c | 3 +- .../custom_render_samples/custom_render_pgl.c | 59 ++----- .../custom_render_samples/custom_render_pgl.h | 49 +++++- .../custom_render_samples/custom_render_rgl.c | 63 ++----- .../custom_render_samples/custom_render_rgl.h | 72 +++++++- .../custom_render_template.h | 18 +- 14 files changed, 294 insertions(+), 151 deletions(-) diff --git a/deps/RFont.h b/deps/RFont.h index 3232aab..43ab898 100644 --- a/deps/RFont.h +++ b/deps/RFont.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2021-24 ColleagueRiley ColleagueRiley@gmail.com +* Copyright (c) 2021-25 ColleagueRiley ColleagueRiley@gmail.com * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -128,7 +128,7 @@ typedef u32 RFont_texture; #endif #ifndef RFONT_MAX_GLYPHS -#define RFONT_MAX_GLYPHS 224 +#define RFONT_MAX_GLYPHS 256 #endif #ifndef RFONT_ATLAS_WIDTH_DEFAULT @@ -148,7 +148,7 @@ typedef u32 RFont_texture; #endif #ifndef RFONT_INIT_VERTS -#define RFONT_INIT_VERTS 1024 * 600 +#define RFONT_INIT_VERTS 20 * RFONT_MAX_GLYPHS #endif #ifndef RFONT_TEXTFORMAT_MAX_SIZE @@ -179,12 +179,20 @@ typedef struct { u32 codepoint; /* the character (for checking) */ size_t size; /* the size of the glyph */ i32 x, x2; /* coords of the character on the texture */ + RFont_font* font; /* the font that the glyph belongs to */ /* source glyph data */ i32 src; float w, h, x1, y1, advance; } RFont_glyph; +/** + * @brief Converts a codepoint to a utf8 string. + * @param codepoint The codepoint to convert to utf8. + * @return The utf8 string. +*/ +inline char* RFont_codepoint_to_utf8(u32 codepoint); + /** * @brief Sets the framebuffer size AND runs the graphics init function. * @param width The framebuffer width. @@ -244,6 +252,9 @@ inline RFont_font* RFont_font_init_data_pro(u8* font_data, b8 auto_free, size_t */ inline void RFont_font_free(RFont_font* font); +typedef RFont_glyph (*RFont_glyph_fallback_callback)(RFont_font* font, u32 codepoint, size_t size); +RFont_glyph_fallback_callback RFont_set_glyph_fallback_callback(RFont_glyph_fallback_callback callback); + /** * @brief Add a character to the font's atlas. * @param font The font to use. @@ -253,6 +264,25 @@ inline void RFont_font_free(RFont_font* font); */ inline RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size); +/** + * @brief Add a codepoint to the font's atlas. + * @param font The font to use. + * @param codepoint The codepoint to add to the atlas. + * @param size The size of the character. + * @return The `RFont_glyph` created from the data and added to the atlas. +*/ +inline RFont_glyph RFont_font_add_codepoint(RFont_font* font, u32 codepoint, size_t size); + +/** + * @brief Add a codepoint to the font's atlas. + * @param font The font to use. + * @param codepoint The codepoint to add to the atlas. + * @param size The size of the character. + * @param fallback If the fallback function should not be called. + * @return The `RFont_glyph` created from the data and added to the atlas. +*/ +inline RFont_glyph RFont_font_add_codepointPro(RFont_font* font, u32 codepoint, size_t size, b8 fallback); + #ifndef RFONT_NO_FMT /** * @brief Formats a string. @@ -481,13 +511,13 @@ struct RFont_font { RFont_texture atlas; /* atlas texture */ size_t atlasWidth, atlasHeight; float atlasX; /* the current x position inside the atlas */ + + float* verts; + float* tcoords; }; size_t RFont_width = 0, RFont_height = 0; -float* RFont_verts; -float* RFont_tcoords; - RFont_font* font2; void RFont_update_framebuffer(size_t width, size_t height) { @@ -497,15 +527,12 @@ void RFont_update_framebuffer(size_t width, size_t height) { } void RFont_init(size_t width, size_t height) { - RFont_update_framebuffer(width, height); - - #ifndef RFONT_NO_GRAPHICS - /* init any rendering stuff that needs to be initalized (eg. vbo objects) */ - RFont_render_init(); - #endif + RFont_update_framebuffer(width, height); - RFont_verts = (float*)RFONT_MALLOC(sizeof(float) * RFONT_INIT_VERTS); - RFont_tcoords = (float*)RFONT_MALLOC(sizeof(float) * RFONT_INIT_VERTS); + #ifndef RFONT_NO_GRAPHICS + /* init any rendering stuff that needs to be initalized (eg. vbo objects) */ + RFont_render_init(); + #endif } #ifndef RFONT_NO_STDIO @@ -537,6 +564,9 @@ RFont_font* RFont_font_init_data(u8* font_data, b8 auto_free) { RFont_font* RFont_font_init_data_pro(u8* font_data, b8 auto_free, size_t atlasWidth, size_t atlasHeight) { RFont_font* font = (RFont_font*)RFONT_MALLOC(sizeof(RFont_font)); + font->verts = (float*)RFONT_MALLOC(sizeof(float) * RFONT_INIT_VERTS); + font->tcoords = (float*)RFONT_MALLOC(sizeof(float) * RFONT_INIT_VERTS); + font->atlasWidth = atlasWidth; font->atlasHeight = atlasHeight; @@ -567,12 +597,14 @@ void RFont_font_free(RFont_font* font) { if (font->free_font_memory) RFONT_FREE(font->info.data); + RFONT_FREE(font->verts); + RFONT_FREE(font->tcoords); + RFONT_FREE (font); } void RFont_close(void) { - RFONT_FREE(RFont_verts); - RFONT_FREE(RFont_tcoords); + } @@ -628,13 +660,28 @@ void RFont_font_add_string_len(RFont_font* font, const char* string, size_t strL RFont_font_add_char(font, *str, sizes[i]); } +RFont_glyph_fallback_callback RFont_glyph_fallback = NULL; +RFont_glyph_fallback_callback RFont_set_glyph_fallback_callback(RFont_glyph_fallback_callback callback) { + RFont_glyph_fallback_callback old = RFont_glyph_fallback; + RFont_glyph_fallback = callback; + return old; +} RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size) { static u32 utf8state = 0, codepoint = 0; - if (RFont_decode_utf8(&utf8state, &codepoint, (u8)ch) != RFONT_UTF8_ACCEPT) - return (RFont_glyph){0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + if (RFont_decode_utf8(&utf8state, &codepoint, (u8)ch) != RFONT_UTF8_ACCEPT) { + return (RFont_glyph){0, 0, 0, 0, (RFont_font*)0, 0, 0, 0, 0, 0, 0}; + } + return RFont_font_add_codepoint(font, codepoint, size); +} + +RFont_glyph RFont_font_add_codepoint(RFont_font* font, u32 codepoint, size_t size) { + return RFont_font_add_codepointPro(font, codepoint, size, 1); +} + +RFont_glyph RFont_font_add_codepointPro(RFont_font* font, u32 codepoint, size_t size, b8 fallback) { u32 i; for (i = 0; i < font->glyph_len; i++) if (font->glyphs[i].codepoint == codepoint && font->glyphs[i].size == size) @@ -643,25 +690,20 @@ RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size) { RFont_glyph* glyph = &font->glyphs[i]; glyph->src = stbtt_FindGlyphIndex(&font->info, codepoint); - - if (glyph->src == 0 && font2 != NULL && font2->info.data != font->info.data) { - stbtt_fontinfo saveInfo = font->info; - - RFont_font* fakeFont = font; - fakeFont->info = font2->info; - - RFont_glyph g = RFont_font_add_char(fakeFont, 't', size); - - fakeFont->info = saveInfo; - - return g; + + if (glyph->src == 0 && fallback && RFont_glyph_fallback) { + RFont_glyph fallback = RFont_glyph_fallback(font, codepoint, size); + if (fallback.codepoint != 0 && fallback.size != 0) { + return fallback; + } } font->glyph_len++; i32 x0, y0, x1, y1, w = 0, h = 0; - if (stbtt_GetGlyphBox(&font->info, glyph->src, &x0, &y0, &x1, &y1) == 0) - return (RFont_glyph){0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + if (stbtt_GetGlyphBox(&font->info, glyph->src, &x0, &y0, &x1, &y1) == 0) { + return (RFont_glyph){0, 0, 0, 0, (RFont_font*)0, 0, 0, 0, 0, 0, 0}; + } float scale = ((float)size) / font->fheight; @@ -676,6 +718,7 @@ RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size) { glyph->x2 = font->atlasX + glyph->w; glyph->x1 = floorf(x0 * scale); glyph->y1 = floor(-y1 * scale); + glyph->font = font; #ifndef RFONT_NO_GRAPHICS @@ -756,9 +799,36 @@ RFont_area RFont_draw_text_spacing(RFont_font* font, const char* text, float x, return RFont_draw_text_len(font, text, 0, x, y, size, spacing); } +char* RFont_codepoint_to_utf8(u32 codepoint) { + static char utf8[5]; + if (codepoint <= 0x7F) { + utf8[0] = codepoint; + utf8[1] = 0; + } else if (codepoint <= 0x7FF) { + utf8[0] = 0xC0 | (codepoint >> 6); + utf8[1] = 0x80 | (codepoint & 0x3F); + utf8[2] = 0; + } else if (codepoint <= 0xFFFF) { + utf8[0] = 0xE0 | (codepoint >> 12); + utf8[1] = 0x80 | ((codepoint >> 6) & 0x3F); + utf8[2] = 0x80 | (codepoint & 0x3F); + utf8[3] = 0; + } else if (codepoint <= 0x10FFFF) { + utf8[0] = 0xF0 | (codepoint >> 18); + utf8[1] = 0x80 | ((codepoint >> 12) & 0x3F); + utf8[2] = 0x80 | ((codepoint >> 6) & 0x3F); + utf8[3] = 0x80 | (codepoint & 0x3F); + utf8[4] = 0; + } else { + utf8[0] = 0; + } + + return utf8; +} + RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, float x, float y, u32 size, float spacing) { - float* verts = RFont_verts; - float* tcoords = RFont_tcoords; + float* verts = font->verts; + float* tcoords = font->tcoords; float startX = x; float startY = y; @@ -772,8 +842,9 @@ RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, f float scale = (((float)size) / font->fheight); float space_adv = (scale * font->space_adv) / 2; - - y -= (-font->descent * scale); + + float descent_offset = (-font->descent * scale); + y -= descent_offset; for (str = (char*)text; (len == 0 || (size_t)(str - text) < len) && *str; str++) { if (*str == '\n') { @@ -792,6 +863,12 @@ RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, f if (glyph.codepoint == 0 && glyph.size == 0) continue; + if (glyph.font != font) { + RFont_draw_text_len(glyph.font, RFont_codepoint_to_utf8(glyph.codepoint), 4, x, y - size + descent_offset, size, spacing); + x += glyph.advance + spacing; + continue; + } + float realX = x + glyph.x1; float realY = y + glyph.y1; @@ -845,14 +922,14 @@ RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, f tcoords[tIndex + 10] = RFONT_GET_TEXPOSX(glyph.x2, font->atlasWidth); tcoords[tIndex + 11] = RFONT_GET_TEXPOSY(glyph.h, font->atlasHeight); + x += glyph.advance + spacing; i += 18; tIndex += 12; - - x += glyph.advance + spacing; } #ifndef RFONT_NO_GRAPHICS - RFont_render_text(font->atlas, verts, tcoords, i / 3); + if (i) + RFont_render_text(font->atlas, verts, tcoords, i / 3); #endif return (RFont_area){(u32)(x - startX), (u32)(y - startY) + (-font->descent * scale)}; diff --git a/examples/advanced/RSGL_rsoft.h b/examples/advanced/RSGL_rsoft.h index 1402c4e..2667cb6 100644 --- a/examples/advanced/RSGL_rsoft.h +++ b/examples/advanced/RSGL_rsoft.h @@ -1,6 +1,4 @@ -#define RSGL_CUSTOM_RENDER -#define RSGL_IMPLEMENTATION - +#ifdef RSGL_IMPLEMENTATION #define RSGL_GET_WORLD_X(x) (float)(x) #define RSGL_GET_WORLD_Y(y) (float)(y) #define RSGL_GET_WORLD_Z(z) (float)(z) @@ -224,3 +222,4 @@ RSGL_programInfo RSGL_renderCreateProgram(const char* VShaderCode, const char* F RSGL_UNUSED(VShaderCode); RSGL_UNUSED(FShaderCode); RSGL_UNUSED(posName); RSGL_UNUSED(texName); RSGL_UNUSED(colorName); return (RSGL_programInfo) {}; } +#endif \ No newline at end of file diff --git a/examples/advanced/glfw.c b/examples/advanced/glfw.c index ad89cc6..0e54955 100644 --- a/examples/advanced/glfw.c +++ b/examples/advanced/glfw.c @@ -2,6 +2,7 @@ #define RSGL_IMPLEMENTATION #include "RSGL.h" +#include "RSGL_gl.h" #define GLFW_UNUSED(x) if(x){} diff --git a/examples/advanced/rsoft.c b/examples/advanced/rsoft.c index 3f50476..54ab91d 100644 --- a/examples/advanced/rsoft.c +++ b/examples/advanced/rsoft.c @@ -1,8 +1,10 @@ -#define RGFW_IMPLEMENTATION #define RGFW_BUFFER #include "RGFW.h" #define RSoft_area RGFW_area +#include "RSGL.h" + +#define RSGL_IMPLEMENTATION #include "RSGL_rsoft.h" #include diff --git a/examples/advanced/shader.c b/examples/advanced/shader.c index 593ae8a..c63444f 100644 --- a/examples/advanced/shader.c +++ b/examples/advanced/shader.c @@ -3,6 +3,7 @@ #define RSGL_IMPLEMENTATION #include "RSGL.h" +#include "RSGL_gl.h" /* this example is designed for Opengl 3.3 GLSL shaders diff --git a/examples/basics/basic.c b/examples/basics/basic.c index 85b07a3..bfe4c5c 100644 --- a/examples/basics/basic.c +++ b/examples/basics/basic.c @@ -5,7 +5,9 @@ #define RSGL_IMPLEMENTATION #define RSGL_NO_X11_CURSOR +#define RSGL_IMPLEMENTATION #include "RSGL.h" +#include "RSGL_gl.h" #include diff --git a/examples/basics/shapes.c b/examples/basics/shapes.c index 2babc15..c81f7f2 100644 --- a/examples/basics/shapes.c +++ b/examples/basics/shapes.c @@ -7,7 +7,9 @@ #undef __linux__ #endif +#define RSGL_IMPLEMENTATION #include "RSGL.h" +#include "RSGL_gl.h" #include diff --git a/examples/basics/text.c b/examples/basics/text.c index 042ca12..dc1510a 100644 --- a/examples/basics/text.c +++ b/examples/basics/text.c @@ -1,8 +1,10 @@ +#define RSGL_RENDER_LEGACY #define RGFW_IMPLEMENTATION #include "RGFW.h" #define RSGL_IMPLEMENTATION #include "RSGL.h" +#include "RSGL_gl.h" int main(void) { RGFW_window* win = RGFW_createWindow("name", (RGFW_rect){500, 500, 500, 500}, RGFW_CENTER); @@ -12,7 +14,7 @@ int main(void) { RSGL_setFont(RSGL_loadFont("Super Easy.ttf")); u32 fps; - +RFONT_UNUSED(fps); for (; RGFW_window_shouldClose(win) == false;) { RGFW_window_checkEvent(win); @@ -21,9 +23,12 @@ int main(void) { RSGL_clear(RSGL_RGB(255, 255, 255)); + //RSGL_drawRect(RSGL_RECT(0, 0, 100, 100), RSGL_RGB(0, 255, 0)); + RSGL_drawText("Text example\nnewlines too", RSGL_CIRCLE(200, 200, 20), RSGL_RGB(255, 0, 0)); - RSGL_drawText(RSGL_strFmt("FPS : %i\nOpenGL %s", fps, RSGL_args.legacy ? "legacy (2-)" : "modern (3.3 +)"), RSGL_CIRCLE(0, 40, 40), RSGL_RGB(255, 0, 0)); - + RSGL_drawText(RSGL_strFmt("FPS : %i\nOpenGL %s", fps, RSGL_GL_legacy ? "legacy (2-)" : "modern (3.3 +)"), RSGL_CIRCLE(0, 40, 40), RSGL_RGB(255, 0, 0)); + RSGL_drawRect(RSGL_RECT(0, 0, 100, 100), RSGL_RGB(0, 255, 0)); + RSGL_drawRect(RSGL_RECT(50, 50, 100, 100), RSGL_RGBA(0, 255, 0, 50)); RSGL_draw(); RGFW_window_swapBuffers(win); diff --git a/examples/basics/textures.c b/examples/basics/textures.c index 7437f01..8e918d2 100644 --- a/examples/basics/textures.c +++ b/examples/basics/textures.c @@ -2,8 +2,9 @@ #include "RGFW.h" #define RSGL_IMPLEMENTATION - #include "RSGL.h" +#include "RSGL_gl.h" + #include unsigned char icon[4 * 3 * 3] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF}; diff --git a/examples/custom_render_samples/custom_render_pgl.c b/examples/custom_render_samples/custom_render_pgl.c index 3a39c4b..e16cbb7 100644 --- a/examples/custom_render_samples/custom_render_pgl.c +++ b/examples/custom_render_samples/custom_render_pgl.c @@ -1,59 +1,26 @@ -#define RSGL_NO_AUDIO /* RSGL uses miniaudio.h, and I don't want to compile it if I'm not using it */ +#define RGFW_BUFFER +#define RGFW_IMPLEMENTATION +#include "RGFW.h" + #define RSGL_IMPLEMENTATION #include "custom_render_pgl.h" int main() { - RGFW_window* win = RGFW_createWindow("name", RSGL_RECT(0, 0, 500, 500), RGFW_CENTER); - - RSGL_init(RSGL_AREA(win->r.w, win->r.h), RSGL_getProcAddress); + RGFW_window* win = RGFW_createWindow("name", RGFW_RECT(0, 0, 500, 500), RGFW_CENTER); - RGFW_area area = RGFW_getScreenSize(); - + RSGL_init(RSGL_AREA(win->r.w, win->r.h), win->buffer); RSGL_setFont(RSGL_loadFont("COMICSANS.ttf")); - /* generic button */ - RSGL_button generic = RSGL_initButton(); - - /* this can be a rect or polygon */ - RSGL_button_setRect(&generic, RSGL_RECT(50, 50, 100, 50)); - - RSGL_button_setText(&generic, "generic", 8, RSGL_CIRCLE(0, 0, 25), RSGL_RGB(100, 100, 100)); - RSGL_button_alignText(&generic, RSGL_ALIGN_CENTER | RSGL_ALIGN_MIDDLE); - - RSGL_button_setStyle(&generic, RSGL_STYLE_DARK | RSGL_STYLE_ROUNDED); - - RSGL_button_setWindow(&generic, win); - - RSGL_textbox* tb = RSGL_initTextbox(0); - RSGL_textbox_setTextInfo(tb, RSGL_CIRCLE(0, 0, 20), RSGL_RGB(100, 100, 100)); - - RSGL_textbox_setRect(tb, RSGL_RECT(20, 20, 200, 30)); - RSGL_textbox_setStyle(tb, RSGL_STYLE_DARK); - RSGL_textbox_alignText(tb, RSGL_ALIGN_CENTER | RSGL_ALIGN_MIDDLE); - - bool running = true; - - while (running) { - while (RSGL_checkEvent(win)) { - if (win->event.type == RSGL_quit) { - running = false; - break; - } - - RSGL_button_update(&generic, win->event); - RSGL_textbox_update(tb, win->event); - } - - RSGL_drawButton(generic); - - RSGL_textbox_draw(tb); - RSGL_drawRect((RSGL_rect){200, 200, 200, 200}, RSGL_RGB(255, 0, 0)); + while (RGFW_window_shouldClose(win)) { + while (RGFW_window_checkEvent(win)); RSGL_clear(RSGL_RGB(255, 255, 255)); + RSGL_drawRect((RSGL_rect){200, 200, 200, 200}, RSGL_RGB(255, 0, 0)); + + RSGL_draw(); RGFW_window_swapBuffers(win); } - RSGL_textbox_free(tb); - - RSGL_window_close(win); + RSGL_free(); + RGFW_window_close(win); } diff --git a/examples/custom_render_samples/custom_render_pgl.h b/examples/custom_render_samples/custom_render_pgl.h index e43435d..0e0acb4 100644 --- a/examples/custom_render_samples/custom_render_pgl.h +++ b/examples/custom_render_samples/custom_render_pgl.h @@ -6,6 +6,8 @@ #include "portableGL.h" +#define RSGL_texture u32 + #ifdef RSGL_CUSTOM_RENDER #define RSGL_IMPLEMENTATION #include "RSGL.h" @@ -77,7 +79,7 @@ void RSGL_renderInit(void* proc, RSGL_RENDER_INFO* info) { glGenBuffers(1, &RSGL_gl.tbo); glGenBuffers(1, &RSGL_gl.cbo); - RSGL_gl.program = RSGL_renderCreateProgram(defaultVShaderCode, defaultFShaderCode, "vertexPosition", "vertexTexCoord", "vertexColor"); + RSGL_gl.program = RSGL_renderCreateProgram((const char*)defaultVShaderCode, (const char*)defaultFShaderCode, "vertexPosition", "vertexTexCoord", "vertexColor"); /* Init default vertex arrays buffers */ /* Initialize CPU (RAM) vertex buffers (position, texcoord, color data and indexes) */ @@ -252,6 +254,20 @@ void RSGL_renderBatch(RSGL_RENDER_INFO* info) { info->vert_len = 0; } +void RSGL_renderScissorStart(RSGL_rectF scissor) { + RSGL_draw(); + glEnable(GL_SCISSOR_TEST); + + glScissor(scissor.x, RSGL_args.currentRect.h - (scissor.y + scissor.h), scissor.w, scissor.h); + glScissor(scissor.x, scissor.y, scissor.w, scissor.h); +} + +void RSGL_renderScissorEnd(void) { + RSGL_draw(); + glDisable(GL_SCISSOR_TEST); +} + + #ifndef GL_RG #define GL_RG 0x8227 #endif @@ -372,7 +388,7 @@ RSGL_programInfo RSGL_renderCreateProgram(const char* VShaderCode, const char* F RSGL_programInfo program; /* create program and link vertex and fragment shaders */ - program.program = pglCreateProgram((vert_func)VShaderCode, (vert_func)FShaderCode, 0, NULL, GL_FALSE); + program.program = pglCreateProgram((vert_func)VShaderCode, (frag_func)FShaderCode, 0, NULL, GL_FALSE); return program; } @@ -452,7 +468,36 @@ u32 RFont_create_atlas(u32 atlasWidth, u32 atlasHeight) { return id; } +b8 RFont_resize_atlas(RFont_texture* atlas, u32 newWidth, u32 newHeight) { + GLuint newAtlas; + glGenTextures(1, &newAtlas); + glBindTexture(GL_TEXTURE_2D, newAtlas); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, newWidth, newHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + glBindTexture(GL_TEXTURE_2D, *atlas); + //glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, newWidth - RFONT_ATLAS_RESIZE_LEN, newHeight); + + glDeleteTextures(1, (u32*)atlas); + + glBindTexture(GL_TEXTURE_2D, newAtlas); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + /* swizzle new atlas */ + glBindTexture(GL_TEXTURE_2D, newAtlas); + static GLint swizzleRgbaParams[4] = {GL_ONE, GL_ONE, GL_ONE, GL_RED}; + glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleRgbaParams); + + glBindTexture(GL_TEXTURE_2D, 0); + + *atlas = newAtlas; + return 1; +} void RFont_push_pixel_values(GLint alignment, GLint rowLength, GLint skipPixels, GLint skipRows); void RFont_push_pixel_values(GLint alignment, GLint rowLength, GLint skipPixels, GLint skipRows) { diff --git a/examples/custom_render_samples/custom_render_rgl.c b/examples/custom_render_samples/custom_render_rgl.c index c1341dd..85fbd7d 100644 --- a/examples/custom_render_samples/custom_render_rgl.c +++ b/examples/custom_render_samples/custom_render_rgl.c @@ -1,58 +1,29 @@ +#define RGFW_IMPLEMENTATION +#include "RGFW.h" + #include "custom_render_rgl.h" int main(void) { - RSGL_window* win = RSGL_createWindow("name", RSGL_RECT(0, 0, 500, 500), RSGL_CENTER); - - u32 easy_font = RSGL_loadFont("Super Easy.ttf"); - RSGL_setFont(easy_font); - - /* generic button */ - RSGL_button generic = RSGL_initButton(); - - /* this can be a rect or polygon */ - RSGL_button_setRect(&generic, RSGL_RECT(50, 50, 100, 50)); - - RSGL_button_setText(&generic, "generic", 8, RSGL_CIRCLE(0, 0, 25), RSGL_RGB(100, 100, 100)); - RSGL_button_alignText(&generic, RSGL_ALIGN_CENTER | RSGL_ALIGN_MIDDLE); - - RSGL_button_setStyle(&generic, RSGL_STYLE_DARK | RSGL_STYLE_ROUNDED); - - RSGL_button_setWindow(&generic, win); + RGFW_window* win = RGFW_createWindow("name", RGFW_RECT(0, 0, 500, 500), RGFW_CENTER); + RSGL_init(RSGL_AREA(win->r.w, win->r.h), RGFW_getProcAddress); u32 comicSans = RSGL_loadFont("COMICSANS.ttf"); - RSGL_textbox* tb = RSGL_initTextbox(0); - RSGL_textbox_setTextInfo(tb, RSGL_CIRCLE(0, 0, 20), RSGL_RGB(100, 100, 100)); - - RSGL_textbox_setRect(tb, RSGL_RECT(20, 20, 200, 30)); - RSGL_textbox_setStyle(tb, RSGL_STYLE_DARK); - RSGL_textbox_alignText(tb, RSGL_ALIGN_CENTER | RSGL_ALIGN_MIDDLE); - - bool running = true; + while (RGFW_window_shouldClose(win)) { + while (RGFW_window_checkEvent(win)); - while (running) { - while (RSGL_window_checkEvent(win)) { - if (win->event.type == RSGL_quit) { - running = false; - break; - } - - RSGL_button_update(&generic, win->event); - RSGL_textbox_update(tb, win->event); - } - - RSGL_setFont(easy_font); - RSGL_drawButton(generic); + RSGL_clear(RSGL_RGB(255, 255, 255)); RSGL_setFont(comicSans); - RSGL_textbox_draw(tb); - - RSGL_drawRect((RSGL_rect){200, 200, 200, 200}, RSGL_RGB(255, 0, 0)); - RSGL_window_clear(win, RSGL_RGB(255, 255, 255)); + RSGL_drawRect((RSGL_rect){200, 200, 200, 200}, RSGL_RGB(255, 0, 0)); + RSGL_drawRect((RSGL_rect){20, 20, 200, 200}, RSGL_RGB(0, 255, 0)); + // RSGL_drawText("text", RSGL_CIRCLE(250, 250, 20), RSGL_RGB(100, 100, 100)); + + RSGL_draw(); + RGFW_window_swapBuffers(win); } - - RSGL_textbox_free(tb); - - RSGL_window_close(win); + + RSGL_free(); + RGFW_window_close(win); } \ No newline at end of file diff --git a/examples/custom_render_samples/custom_render_rgl.h b/examples/custom_render_samples/custom_render_rgl.h index e1c4ba9..c71aa01 100644 --- a/examples/custom_render_samples/custom_render_rgl.h +++ b/examples/custom_render_samples/custom_render_rgl.h @@ -1,5 +1,6 @@ #define RSGL_CUSTOM_RENDER +#define RSGL_texture u32 #ifdef RSGL_CUSTOM_RENDER #define RSGL_IMPLEMENTATION #include "RSGL.h" @@ -8,7 +9,7 @@ #define RGL_IMPLEMENTATION #include "RGL.h" -void RSGL_renderDeleteTexture(u32 tex) { glDeleteTextures(1, &tex); } +void RSGL_renderDeleteTexture(RSGL_texture tex) { glDeleteTextures(1, &tex); } void RSGL_renderViewport(i32 x, i32 y, i32 w, i32 h) { glViewport(x, y, w ,h); } void RSGL_renderClear(float r, float g, float b, float a) { @@ -29,19 +30,28 @@ void RSGL_renderBatch(RSGL_RENDER_INFO* info) { size_t i, j; size_t tIndex = 0, cIndex = 0, vIndex = 0; for (i = 0; i < info->len; i++) { + glEnable(GL_TEXTURE_2D); + rglSetTexture(info->batches[i].tex); + rglLineWidth(info->batches[i].lineWidth); + u32 mode = info->batches[i].type; - if (mode > 0x0100) { glEnable(GL_BLEND); mode -= 0x0100; } else { - #ifndef RGL_MODERN_OPENGL glDisable(GL_BLEND); - #endif } - - rglSetTexture(info->batches[i].tex); - rglLineWidth(info->batches[i].lineWidth); + + if (mode > 0x0010) { + mode -= 0x0010; + glDisable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); + } + else { + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + } + rglBegin(mode); for (j = info->batches[i].start; j < info->batches[i].len; j++) { @@ -66,6 +76,10 @@ void RSGL_renderBatch(RSGL_RENDER_INFO* info) { } rglEnd(); + + if (info->batches[i].type > 0x0010) { + glEnable(GL_DEPTH_TEST); + } } info->len = 0; @@ -77,6 +91,19 @@ void RSGL_renderBatch(RSGL_RENDER_INFO* info) { #define GL_RG 0x8227 #endif +void RSGL_renderScissorStart(RSGL_rectF scissor) { + RSGL_draw(); + glEnable(GL_SCISSOR_TEST); + + glScissor(scissor.x, RSGL_args.currentRect.h - (scissor.y + scissor.h), scissor.w, scissor.h); + glScissor(scissor.x, scissor.y, scissor.w, scissor.h); +} + +void RSGL_renderScissorEnd(void) { + RSGL_draw(); + glDisable(GL_SCISSOR_TEST); +} + /* textures / images */ u32 RSGL_renderCreateTexture(u8* bitmap, RSGL_area memsize, u8 channels) { unsigned int id = 0; @@ -297,6 +324,37 @@ u32 RFont_create_atlas(u32 atlasWidth, u32 atlasHeight) { return id; } +b8 RFont_resize_atlas(RFont_texture* atlas, u32 newWidth, u32 newHeight) { + GLuint newAtlas; + glGenTextures(1, &newAtlas); + glBindTexture(GL_TEXTURE_2D, newAtlas); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, newWidth, newHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + glBindTexture(GL_TEXTURE_2D, *atlas); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, newWidth - RFONT_ATLAS_RESIZE_LEN, newHeight); + + glDeleteTextures(1, (u32*)atlas); + + glBindTexture(GL_TEXTURE_2D, newAtlas); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + /* swizzle new atlas */ + glBindTexture(GL_TEXTURE_2D, newAtlas); + static GLint swizzleRgbaParams[4] = {GL_ONE, GL_ONE, GL_ONE, GL_RED}; + glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleRgbaParams); + + glBindTexture(GL_TEXTURE_2D, 0); + + *atlas = newAtlas; + return 1; +} + #ifndef GL_UNPACK_ROW_LENGTH #define GL_UNPACK_ROW_LENGTH 0x0CF2 #define GL_UNPACK_SKIP_PIXELS 0x0CF4 diff --git a/examples/custom_render_samples/custom_render_template.h b/examples/custom_render_samples/custom_render_template.h index 1a147be..0b88184 100644 --- a/examples/custom_render_samples/custom_render_template.h +++ b/examples/custom_render_samples/custom_render_template.h @@ -49,7 +49,7 @@ u32 RSGL_renderCreateTexture(u8* bitmap, RSGL_area memsize, u8 channels) { return id; } -void RSGL_renderUpdateTexture(u32 texture, u8* bitmap, RSGL_area memsize, u8 channels) { +void RSGL_renderUpdateTexture(RSGL_texture texture, u8* bitmap, RSGL_area memsize, u8 channels) { } @@ -67,11 +67,23 @@ void RSGL_renderDeleteProgram(RSGL_programInfo program) { } -u32 RFont_create_atlas(u32 atlasWidth, u32 atlasHeight) { +void RSGL_renderScissorStart(RSGL_rectF scissor) { + +} + +void RSGL_renderScissorEnd(void) { + +} + +u32 RFont_create_atlas(RSGL_texture atlasWidth, u32 atlasHeight) { u32 id = 0; return id; } -void RFont_bitmap_to_atlas(u32 atlas, u8* bitmap, float x, float y, float w, float h) { +b8 RFont_resize_atlas(RFont_texture* atlas, u32 newWidth, u32 newHeight) { + return 1; +} + +void RFont_bitmap_to_atlas(RSGL_texture atlas, u8* bitmap, float x, float y, float w, float h) { } \ No newline at end of file