fix naming conventions, add scissoring

This commit is contained in:
EmmanuelMess 2024-12-26 15:52:21 -03:00
parent 7ce74ba46c
commit b0fe41186c
2 changed files with 82 additions and 19 deletions

View File

@ -37,16 +37,19 @@ Clay_RenderCommandArray CreateLayout() {
} }
int main() { int main() {
const int width = 80;
const int height = 24;
uint64_t totalMemorySize = Clay_MinMemorySize(); uint64_t totalMemorySize = Clay_MinMemorySize();
Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize)); Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
Clay_Initialize(arena, (Clay_Dimensions) { .width = 80, .height = 24 }); // TODO this is wrong, but I have no idea what the actual size of the terminal is in pixels Clay_Initialize(arena, (Clay_Dimensions) { .width = (float) width, .height = (float) height }); // TODO this is wrong, but I have no idea what the actual size of the terminal is in pixels
// Tell clay how to measure text // Tell clay how to measure text
Clay_SetMeasureTextFunction(Console_MeasureText); Clay_SetMeasureTextFunction(Console_MeasureText);
while(true) { while(true) {
Clay_RenderCommandArray layout = CreateLayout(); Clay_RenderCommandArray layout = CreateLayout();
Clay_Console_Render(layout); Clay_Console_Render(layout, width, height);
fflush(stdout); fflush(stdout);
sleep(1); sleep(1);

View File

@ -7,9 +7,18 @@
#include "signal.h" #include "signal.h"
#endif #endif
#define gotoxy(x, y) printf("\033[%d;%dH", y+1, x+1) static inline void Console_MoveCursor(int x, int y) {
printf("\033[%d;%dH", y+1, x+1);
}
static inline void Console_DrawRectangle(int x0, int y0, int width, int height, Clay_Color color) { bool Clay_PointIsInsideRect(Clay_Vector2 point, Clay_BoundingBox rect) {
// TODO this function is a copy of Clay__PointIsInsideRect but that one is internal, I don't know if we want
// TODO to expose Clay__PointIsInsideRect
return point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height;
}
static inline void Console_DrawRectangle(int x0, int y0, int width, int height, Clay_Color color,
Clay_BoundingBox scissorBox) {
if (color.r < 127 || color.g < 127 || color.b < 127 || color.a < 127) { if (color.r < 127 || color.g < 127 || color.b < 127 || color.a < 127) {
// For now there are only two colors, // For now there are only two colors,
return; return;
@ -17,7 +26,11 @@ static inline void Console_DrawRectangle(int x0, int y0, int width, int height,
for (int y = y0; y < height; y++) { for (int y = y0; y < height; y++) {
for (int x = x0; x < width; x++) { for (int x = x0; x < width; x++) {
gotoxy(x, y); if(!Clay_PointIsInsideRect((Clay_Vector2) { .x = x, .y = y }, scissorBox)) {
continue;
}
Console_MoveCursor(x, y);
// TODO there are only two colors actually drawn, the background and white // TODO there are only two colors actually drawn, the background and white
if (color.r < 127 || color.g < 127 || color.b < 127 || color.a < 127) { if (color.r < 127 || color.g < 127 || color.b < 127 || color.a < 127) {
printf(" "); printf(" ");
@ -61,10 +74,19 @@ void Clay_Raylib_Initialize(int width, int height, const char *title, unsigned i
//TODO //TODO
} }
void Clay_Console_Render(Clay_RenderCommandArray renderCommands) void Clay_Console_Render(Clay_RenderCommandArray renderCommands, int width, int height)
{ {
printf("\033[H\033[J"); // Clear printf("\033[H\033[J"); // Clear
const Clay_BoundingBox fullWindow = {
.x = 0,
.y = 0,
.width = (float) width,
.height = (float) height,
};
Clay_BoundingBox scissorBox = fullWindow;
for (int j = 0; j < renderCommands.length; j++) for (int j = 0; j < renderCommands.length; j++)
{ {
Clay_RenderCommand *renderCommand = Clay_RenderCommandArray_Get(&renderCommands, j); Clay_RenderCommand *renderCommand = Clay_RenderCommandArray_Get(&renderCommands, j);
@ -73,48 +95,84 @@ void Clay_Console_Render(Clay_RenderCommandArray renderCommands)
{ {
case CLAY_RENDER_COMMAND_TYPE_TEXT: { case CLAY_RENDER_COMMAND_TYPE_TEXT: {
Clay_String text = renderCommand->text; Clay_String text = renderCommand->text;
int k = 0; int y = 0;
for (int i = 0; i < text.length; i++) { for (int x = 0; x < text.length; x++) {
if(text.chars[i] == '\n') { if(text.chars[x] == '\n') {
k++; y++;
continue; continue;
} }
gotoxy((int) boundingBox.x + i, (int) boundingBox.y + k); int cursorX = (int) boundingBox.x + x;
printf("%c", text.chars[i]); int cursorY = (int) boundingBox.y + y;
if(!Clay_PointIsInsideRect((Clay_Vector2) { .x = cursorX, .y = cursorY }, scissorBox)) {
continue;
}
Console_MoveCursor(cursorX, cursorY);
printf("%c", text.chars[x]);
} }
break; break;
} }
case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: { case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START: {
//TODO scissorBox = boundingBox;
break; break;
} }
case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END: { case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END: {
//TODO scissorBox = fullWindow;
break; break;
} }
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: { case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: {
Clay_RectangleElementConfig *config = renderCommand->config.rectangleElementConfig; Clay_RectangleElementConfig *config = renderCommand->config.rectangleElementConfig;
Console_DrawRectangle((int)boundingBox.x, (int)boundingBox.y, (int)boundingBox.width, (int)boundingBox.height, config->color); Console_DrawRectangle(
(int)boundingBox.x,
(int)boundingBox.y,
(int)boundingBox.width,
(int)boundingBox.height,
config->color,
scissorBox);
break; break;
} }
case CLAY_RENDER_COMMAND_TYPE_BORDER: { case CLAY_RENDER_COMMAND_TYPE_BORDER: {
Clay_BorderElementConfig *config = renderCommand->config.borderElementConfig; Clay_BorderElementConfig *config = renderCommand->config.borderElementConfig;
// Left border // Left border
if (config->left.width > 0) { if (config->left.width > 0) {
Console_DrawRectangle((int)(boundingBox.x), (int)(boundingBox.y + config->cornerRadius.topLeft), (int)config->left.width, (int)(boundingBox.height - config->cornerRadius.topLeft - config->cornerRadius.bottomLeft), config->left.color); Console_DrawRectangle(
(int)(boundingBox.x),
(int)(boundingBox.y + config->cornerRadius.topLeft),
(int)config->left.width,
(int)(boundingBox.height - config->cornerRadius.topLeft - config->cornerRadius.bottomLeft),
config->left.color,
scissorBox);
} }
// Right border // Right border
if (config->right.width > 0) { if (config->right.width > 0) {
Console_DrawRectangle((int)(boundingBox.x + boundingBox.width - config->right.width), (int)(boundingBox.y + config->cornerRadius.topRight), (int)config->right.width, (int)(boundingBox.height - config->cornerRadius.topRight - config->cornerRadius.bottomRight), config->right.color); Console_DrawRectangle(
(int)(boundingBox.x + boundingBox.width - config->right.width),
(int)(boundingBox.y + config->cornerRadius.topRight),
(int)config->right.width,
(int)(boundingBox.height - config->cornerRadius.topRight - config->cornerRadius.bottomRight),
config->right.color,
scissorBox);
} }
// Top border // Top border
if (config->top.width > 0) { if (config->top.width > 0) {
Console_DrawRectangle((int)(boundingBox.x + config->cornerRadius.topLeft), (int)(boundingBox.y), (int)(boundingBox.width - config->cornerRadius.topLeft - config->cornerRadius.topRight), (int)config->top.width, config->top.color); Console_DrawRectangle(
(int)(boundingBox.x + config->cornerRadius.topLeft),
(int)(boundingBox.y),
(int)(boundingBox.width - config->cornerRadius.topLeft - config->cornerRadius.topRight),
(int)config->top.width,
config->top.color,
scissorBox);
} }
// Bottom border // Bottom border
if (config->bottom.width > 0) { if (config->bottom.width > 0) {
Console_DrawRectangle((int)(boundingBox.x + config->cornerRadius.bottomLeft), (int)(boundingBox.y + boundingBox.height - config->bottom.width), (int)(boundingBox.width - config->cornerRadius.bottomLeft - config->cornerRadius.bottomRight), (int)config->bottom.width, config->bottom.color); Console_DrawRectangle(
(int)(boundingBox.x + config->cornerRadius.bottomLeft),
(int)(boundingBox.y + boundingBox.height - config->bottom.width),
(int)(boundingBox.width - config->cornerRadius.bottomLeft - config->cornerRadius.bottomRight),
(int)config->bottom.width,
config->bottom.color,
scissorBox);
} }
break; break;
} }
@ -127,4 +185,6 @@ void Clay_Console_Render(Clay_RenderCommandArray renderCommands)
} }
} }
} }
Console_MoveCursor(-1, -1); // TODO make the user not be able to write
} }