adding gfx and ui stuff

This commit is contained in:
Daniel Ledda
2026-06-04 18:22:30 +02:00
parent d676c50961
commit 4dfac3f82f
48 changed files with 26734 additions and 3 deletions

131
examples/gfx.c Normal file
View File

@@ -0,0 +1,131 @@
#define DJSTD_BASIC_ENTRY
#include "../core.c"
#include "../gfx/gfx.c"
typedef struct WindowData WindowData;
struct WindowData {
Renderer *renderer;
};
void windowOnEvent(OS_WindowEvent *e, OS_Window *window) {
switch (e->kind) {
case OS_WindowEventKind_Resize: {
WindowData *data = (WindowData*)window->userData;
data->renderer->width = e->resize.width;
data->renderer->height = e->resize.height;
break;
};
default: {
break;
};
}
}
void ui_GreenBox(UI_Context *ui, int32 width, int32 height) {
UI(.width=width, .height=height, .color=COLOR_GREEN, .padding=UI_PadUniform(10)) {
UI(.flags=UI_Flag_HeightGrow | UI_Flag_WidthGrow | UI_Flag_Vertical, .childGap=10) {
UI_SetTxtAttr(.fontSize=40, .alignment=UI_TxtAlign_Center);
UI_Rect box = UI_RectAttr(.flags=UI_Flag_WidthGrow | UI_Flag_HeightGrow, .color=COLOR_RED);
UI_Rect boxHover = box;
boxHover.color = COLOR_BLUE;
UI_AttachTxtAttr(&box, UI_TxtAttr(.color=COLOR_CYAN));
UI_AttachTxtAttr(&boxHover, UI_TxtAttr(.color=COLOR_YELLOW));
UI(.flags=UI_Flag_HeightGrow | UI_Flag_WidthGrow, .childGap=10) {
UI_FromRect(ui_HoverRect(ui, box, boxHover)) {
UI_Txt(s("A"));
}
UI_FromRect(ui_HoverRect(ui, box, boxHover)) {
UI_Txt(s("B"));
}
}
UI(.flags=UI_Flag_HeightGrow | UI_Flag_WidthGrow, .childGap=10) {
UI_FromRect(ui_HoverRect(ui, box, boxHover)) {
UI_Txt(s("C"));
}
UI_FromRect(ui_HoverRect(ui, box, boxHover)) {
UI_Txt(s("D"));
}
}
}
}
}
void ui_Main(UI_Context *ui, int32 width, int32 height) {
UI(.flags=UI_Flag_HeightGrow | UI_Flag_WidthGrow, .padding=UI_PadUniform(10), .childGap=10) {
ui_GreenBox(ui, width, height);
ui_GreenBox(ui, width, height);
ui_GreenBox(ui, width, height);
ui_GreenBox(ui, width, height);
ui_GreenBox(ui, width, height);
}
}
int djstd_entry(Arena *arena, StringList args) {
WindowData windowData = {
.renderer=NULL,
};
Arena *uiArena = arenaAlloc(Megabytes(128));
OS_WindowInitResult windowInit = os_windowInit(arena, (OS_WindowInitParams){
.name=s("Hello DJ Stdlib Graphics"),
.width=800,
.height=600,
.data=&windowData,
.eventHandler=windowOnEvent,
});
if (!windowInit.valid) {
return 1;
}
OS_Window *window = windowInit.result;
Scene mainScene = createScene(arena);
Camera cam = createCamera(window->width, window->height);
Font kodeMono = createFont(arena, s("./gfx/assets/fonts/KodeMono.ttf"), 96);
Shader phongShader = createShader(s("./gfx/assets/shaders/phong.vertex.glsl"), s("./gfx/assets/shaders/phong.fragment.glsl"));
Shader solidShader = createShader(s("./gfx/assets/shaders/rect.vertex.glsl"), s("./gfx/assets/shaders/rect.fragment.glsl"));
Shader textShader = createShader(s("./gfx/assets/shaders/text.vertex.glsl"), s("./gfx/assets/shaders/text.fragment.glsl"));
Renderer renderer = createRenderer(arena, &mainScene, &cam, createSceneGraphNode(&mainScene));
UI_Context ui = ui_initContext(uiArena, &renderer);
renderer.phongShader = &phongShader;
renderer.solidShader = &solidShader;
renderer.textShader = &textShader;
renderer.activeFont = &kodeMono;
windowData.renderer = &renderer;
OS_Input currInput = {0};
OS_Input prevInput = {0};
while (!os_windowShouldClose(window)) {
os_windowFrameBegin(window);
prevInput = currInput;
os_windowGetInput(window, &currInput);
ui.input = &currInput;
Render(&renderer) {
// Render graphics shit
UI_Pass(&ui) {
ui_Main(&ui);
}
}
if (currInput.keyboard.escape) {
break;
}
os_windowSwapBuffers(window);
os_windowFrameEnd(window);
}
return 0;
}