From ef950c43d477104d92f014d83b5c03ad5de2ae0a Mon Sep 17 00:00:00 2001 From: ColleagueRiley Date: Wed, 1 Jan 2025 14:13:15 -0500 Subject: [PATCH] update --- examples/RSGL_rendering/GLFW_windowing/main.c | 2 + examples/RSGL_rendering/RGFW_windowing/main.c | 6 +- renderers/RSGL/RSGL.h | 18 ++++- renderers/RSGL/RSGL_gl.h | 13 +++ renderers/RSGL/clay_renderer_RSGL.c | 81 +++++++++++++------ 5 files changed, 89 insertions(+), 31 deletions(-) diff --git a/examples/RSGL_rendering/GLFW_windowing/main.c b/examples/RSGL_rendering/GLFW_windowing/main.c index 8768959..420ffc8 100644 --- a/examples/RSGL_rendering/GLFW_windowing/main.c +++ b/examples/RSGL_rendering/GLFW_windowing/main.c @@ -311,6 +311,8 @@ int main(void) { RSGL_clear(RSGL_RGB(0, 0, 0)); Clay_RenderCommandArray renderCommands = CreateLayout(); Clay_RSGL_Render(renderCommands); + RSGL_draw(); + glfwSwapBuffers(window); } diff --git a/examples/RSGL_rendering/RGFW_windowing/main.c b/examples/RSGL_rendering/RGFW_windowing/main.c index 9a9736c..d88c20f 100644 --- a/examples/RSGL_rendering/RGFW_windowing/main.c +++ b/examples/RSGL_rendering/RGFW_windowing/main.c @@ -295,13 +295,15 @@ int main(void) { deltaTime = NOW - LAST; Clay_Vector2 scrollDelta = {}; - while (RGFW_window_checkEvent(win)); { + while (RGFW_window_checkEvent(win)) { clay_RGFW_update(win, deltaTime); } - + RSGL_clear(RSGL_RGB(0, 0, 0)); Clay_RenderCommandArray renderCommands = CreateLayout(); Clay_RSGL_Render(renderCommands); + RSGL_draw(); + RGFW_window_swapBuffers(win); } diff --git a/renderers/RSGL/RSGL.h b/renderers/RSGL/RSGL.h index 97cc8d8..937e409 100644 --- a/renderers/RSGL/RSGL.h +++ b/renderers/RSGL/RSGL.h @@ -264,6 +264,7 @@ RSGLDEF void RSGL_init( void* loader /* opengl prozc address ex. wglProcAddress */ ); RSGLDEF void RSGL_updateSize(RSGL_area r); +RSGLDEF void RSGL_draw(void); /* draw current batch */ RSGLDEF void RSGL_clear(RSGL_color c); RSGLDEF void RSGL_free(void); @@ -403,6 +404,10 @@ RSGLDEF RSGL_texture RSGL_renderCreateTexture(u8* bitmap, RSGL_area memsize, u8 RSGLDEF void RSGL_renderUpdateTexture(RSGL_texture texture, u8* bitmap, RSGL_area memsize, u8 channels); /* delete a texture */ RSGLDEF void RSGL_renderDeleteTexture(RSGL_texture tex); +/* starts scissoring */ +RSGLDEF void RSGL_renderScissorStart(RSGL_rectF scissor); +/* stops scissoring */ +RSGLDEF void RSGL_renderScissorEnd(void); /* custom shader program */ typedef struct RSGL_programInfo { @@ -498,6 +503,7 @@ struct RFont_font; RSGLDEF void RSGL_setRFont(struct RFont_font* font); RSGLDEF void RSGL_drawText_len(const char* text, size_t len, RSGL_circle c, RSGL_color color); +RSGLDEF void RSGL_drawText_pro(const char* text, size_t len, float spacing, RSGL_circle c, RSGL_color color); RSGLDEF void RSGL_drawText(const char* text, RSGL_circle c, RSGL_color color); #define RSGL_drawTextF(text, font, c, color) \ RSGL_setFont(font);\ @@ -854,10 +860,12 @@ void RSGL_init(RSGL_area r, void* loader) { } } +void RSGL_draw(void) { + RSGL_renderBatch(&RSGL_renderInfo); +} + void RSGL_clear(RSGL_color color) { RSGL_renderClear(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f); - - RSGL_renderBatch(&RSGL_renderInfo); } void RSGL_updateSize(RSGL_area r) { @@ -1389,11 +1397,15 @@ void RSGL_setRFont(RFont_font* font) { } void RSGL_drawText_len(const char* text, size_t len, RSGL_circle c, RSGL_color color) { + RSGL_drawText_pro(text, len, 0.0f, c, color); +} + +void RSGL_drawText_pro(const char* text, size_t len, float spacing, RSGL_circle c, RSGL_color color) { if (text == NULL || RSGL_font.f == NULL) return; RFont_set_color(color.r / 255.0f, color.b / 255.0f, color.g / 255.0f, color.a / 255.0f); - RFont_draw_text_len(RSGL_font.f, text, len, c.x, c.y, c.d, 0.0f); + RFont_draw_text_len(RSGL_font.f, text, len, c.x, c.y, c.d, spacing); } void RSGL_drawText(const char* text, RSGL_circle c, RSGL_color color) { diff --git a/renderers/RSGL/RSGL_gl.h b/renderers/RSGL/RSGL_gl.h index 3e73cea..d64feea 100644 --- a/renderers/RSGL/RSGL_gl.h +++ b/renderers/RSGL/RSGL_gl.h @@ -497,6 +497,19 @@ 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 diff --git a/renderers/RSGL/clay_renderer_RSGL.c b/renderers/RSGL/clay_renderer_RSGL.c index 902b85a..150c5e1 100644 --- a/renderers/RSGL/clay_renderer_RSGL.c +++ b/renderers/RSGL/clay_renderer_RSGL.c @@ -3,37 +3,35 @@ #include +#define RFONT_FONT_SCALE 1 + +#define CLAY_COLOR_TO_RSGL_COLOR(color) \ + RSGL_RGBA((unsigned char)roundf(color.r), (unsigned char)roundf(color.g), (unsigned char)roundf(color.b), (unsigned char)roundf(color.a)) + static Clay_Dimensions RSGL_MeasureText(Clay_String *text, Clay_TextElementConfig *config) { - RSGL_area area = RSGL_textArea(text->chars, config->fontSize, text->length); + RSGL_area area = RSGL_textArea(text->chars, config->fontSize / RFONT_FONT_SCALE, text->length); return (Clay_Dimensions) { .width = (float)area.w, .height = (float)area.h, }; } -RSGL_rectF currentClippingRectangle; - static void Clay_RSGL_Render(Clay_RenderCommandArray renderCommands) { for (uint32_t i = 0; i < renderCommands.length; i++) { Clay_RenderCommand *renderCommand = Clay_RenderCommandArray_Get(&renderCommands, i); - Clay_BoundingBox boundingBox = renderCommand->boundingBox; + RSGL_rectF boundingBox = RSGL_RECTF(renderCommand->boundingBox.x, + renderCommand->boundingBox.y, + renderCommand->boundingBox.width, + renderCommand->boundingBox.height); + switch (renderCommand->commandType) { case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: { Clay_RectangleElementConfig *config = renderCommand->config.rectangleElementConfig; - RSGL_rectF rect = RSGL_RECTF( - boundingBox.x, - boundingBox.y, - boundingBox.width, - boundingBox.height); - RSGL_color color = RSGL_RGBA( (u8)(config->color.r), - (u8)(config->color.g), - (u8)(config->color.b), - (u8)(config->color.a)); - RSGL_drawRectF(rect, color); + RSGL_drawRectF(boundingBox, CLAY_COLOR_TO_RSGL_COLOR(config->color)); break; } case CLAY_RENDER_COMMAND_TYPE_TEXT: { @@ -47,26 +45,57 @@ static void Clay_RSGL_Render(Clay_RenderCommandArray renderCommands) (u8)(config->textColor.a)); RSGL_circle destination = RSGL_CIRCLE( - boundingBox.x, - boundingBox.y, - boundingBox.height); - RSGL_drawText_len(text.chars, text.length, destination, color); + boundingBox.x, + boundingBox.y, + config->fontSize / RFONT_FONT_SCALE); + RSGL_drawText_pro(text.chars, text.length, config->letterSpacing, destination, color); + break; + } + case CLAY_RENDER_COMMAND_TYPE_IMAGE: + Clay_ImageElementConfig* config = renderCommand->config.imageElementConfig; + RSGL_setTexture((RSGL_texture)config->imageData); + RSGL_drawRectF(boundingBox, RSGL_RGBA(255, 255, 255, 255)); + break; + case CLAY_RENDER_COMMAND_TYPE_BORDER: { + Clay_BorderElementConfig *config = renderCommand->config.borderElementConfig; + if (config->left.width > 0) { + RSGL_drawRectF(RSGL_RECTF(boundingBox.x, + boundingBox.y + config->cornerRadius.topLeft, + config->left.width, + boundingBox.h - config->cornerRadius.topLeft - config->cornerRadius.bottomLeft), + CLAY_COLOR_TO_RSGL_COLOR(config->left.color)); + } + if (config->right.width > 0) { + RSGL_drawRectF(RSGL_RECTF(boundingBox.x + boundingBox.w - config->right.width, + boundingBox.y + config->cornerRadius.topRight, + config->right.width, + boundingBox.h - config->cornerRadius.topRight - config->cornerRadius.bottomRight), + CLAY_COLOR_TO_RSGL_COLOR(config->right.color)); + } + if (config->top.width > 0) {RSGL_drawRectF(RSGL_RECTF(boundingBox.x + config->cornerRadius.topLeft, boundingBox.y, + boundingBox.w - config->cornerRadius.topLeft - config->cornerRadius.topRight, + config->top.width), + CLAY_COLOR_TO_RSGL_COLOR(config->top.color)); + } + if (config->bottom.width > 0) {RSGL_drawRectF(RSGL_RECTF(boundingBox.x + config->cornerRadius.bottomLeft, \ + boundingBox.y + boundingBox.h - config->bottom.width, + boundingBox.w - config->cornerRadius.bottomLeft - config->cornerRadius.bottomRight, + config->bottom.width), + CLAY_COLOR_TO_RSGL_COLOR(config->bottom.color)); + } break; } case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: { - currentClippingRectangle = (RSGL_rectF) { - boundingBox.x, - boundingBox.y, - boundingBox.width, - boundingBox.height, - }; - //printf("Clipping rectangle has not been implemented yet\n"); + RSGL_renderScissorStart(boundingBox); break; } case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END: { - //printf("Clipping rectangle has not been implemented yet\n"); + RSGL_renderScissorEnd(); break; } + case CLAY_RENDER_COMMAND_TYPE_CUSTOM: + printf("Custom render commands have not been implemented yet\n"); + break; default: { fprintf(stderr, "Error: unhandled render command: %d\n", renderCommand->commandType); }