UI core boiler plate

This commit is contained in:
2023-12-20 11:25:01 +01:00
parent ebda550bf2
commit 70cc2eae8c
9 changed files with 2072 additions and 2 deletions

View File

@@ -6,7 +6,8 @@ set(CMAKE_C_STANDARD 11)
if (EMSCRIPTEN)
add_compile_options(-Wall -Wextra -Wpedantic)
else()
add_compile_options(-Wall -Wextra -Wpedantic -std=c11)
#add_compile_options(-Wall -Wextra -Wpedantic -std=c11)
add_compile_options(-Wall -Wextra)
endif()
add_compile_definitions(DEBUG_MODE)
@@ -22,12 +23,14 @@ target_link_libraries(rlImGui cimgui raylib)
set(librarySources
libs/cute_tiled/cute_tiled.c
libs/raygui/raygui.c
libs/stb/stb.c
)
set(libraryDirs
libs/cute_tiled
libs/raygui
libs/rlImGui
libs/stb
)
@@ -41,6 +44,8 @@ set(BreezeSources
breeze/map/map.c
breeze/map/tileset.c
breeze/ui/ui.c
breeze/util/array.c
breeze/util/heap.c
breeze/util/object_pool.c
@@ -59,6 +64,8 @@ set(BreezeHeaders
breeze/map/map.h
breeze/map/tileset.h
breeze/ui/ui.h
breeze/util/array.h
breeze/util/heap.h
breeze/util/object_pool.h

View File

@@ -8,6 +8,8 @@
#include "breeze/memory/memory.h"
#include "breeze/memory/stack_alloc.h"
#include "breeze/ui/ui.h"
#include "breeze/util/array.h"
#include "breeze/util/heap.h"
#include "breeze/util/object_pool.h"

67
engine/breeze/ui/ui.c Normal file
View File

@@ -0,0 +1,67 @@
#include "ui.h"
#include "../memory/memory.h"
#include "../util/object_pool.h"
#include "../util/string.h"
#include <stb_ds.h>
typedef struct BzUI {
struct {
BzUIKey key;
BzUIWidget *value;
} *widgetMap;
BzObjectPool *widgets;
BzUIWidget **widgetStack;
BzUIWidget *root;
} BzUI;
BzUIKey bzUIKeyNull() {
return 0;
}
BzUIKey bzUIKeyFromString(const char *str) {
return bzStringDefaultHash(str);
}
BzUI *bzUICreate() {
BzUI *ui = bzAlloc(sizeof(*ui));
ui->widgetMap = NULL;
ui->widgetStack = NULL;
ui->widgets = bzObjectPoolCreate(&(BzObjectPoolDesc) {
.objectsPerPage=128,
.objectSize=sizeof(BzUIWidget),
});
ui->root = bzObjectPool(ui->widgets);
bzMemSet(ui->root, 0, sizeof(*ui->root));
hmput(ui->widgetMap, bzUIKeyFromString("##root"), ui->root);
return ui;
}
void bzUIDestroy(BzUI *ui) {
hmfree(ui->widgetMap);
ui->widgetMap = NULL;
arrfree(ui->widgetStack);
ui->widgetStack = NULL;
bzObjectPoolDestroy(ui->widgets);
ui->widgets = NULL;
bzFree(ui);
}
void bzUIBegin(BzUI *ui, i32 width, i32 height) {
arrsetlen(ui->widgetStack, 0);
arrpush(ui->widgetStack, ui->root);
ui->root->semanticSize[BZ_UI_AXIS_X] = (BzUISize){
.kind = BZ_UI_SIZE_PIXELS,
.value = width
};
ui->root->semanticSize[BZ_UI_AXIS_Y] = (BzUISize) {
.kind = BZ_UI_SIZE_PIXELS,
.value = height
};
}
void bzUIEnd(BzUI *ui) {
}

98
engine/breeze/ui/ui.h Normal file
View File

@@ -0,0 +1,98 @@
#ifndef BREEZE_UI_CORE_H
#define BREEZE_UI_CORE_H
#include "../defines.h"
#include <raylib.h>
typedef enum BzUISizeKind {
BZ_UI_SIZE_NULL,
BZ_UI_SIZE_PIXELS,
BZ_UI_SIZE_FIT,
BZ_UI_SIZE_REL_PARENT,
} BzUISizeKind;
typedef struct BzUISize {
BzUISizeKind kind;
f32 value;
} BzUISize;
typedef enum BzUIAxis {
BZ_UI_AXIS_X,
BZ_UI_AXIS_Y,
BZ_UI_AXIS_COUNT,
} BzUIAxis;
typedef u32 BzUIWidgetFlags;
enum {
BZ_UI_WIDGET_CLICKABLE = (1 << 0),
BZ_UI_WIDGET_VIEW_SCROLL = (1 << 1),
BZ_UI_WIDGET_DRAW_TEXT = (1 << 2),
BZ_UI_WIDGET_DRAW_BORDER = (1 << 3),
};
typedef u32 BzUIKey;
typedef struct BzUIWidget BzUIWidget;
typedef struct BzUIWidget {
BzUIWidget *first;
BzUIWidget *last;
BzUIWidget *next;
BzUIWidget *prev;
BzUIWidget *parent;
// Key+generation info
BzUIKey key;
u64 lastFrameTouchedIndex;
// Per-frame info provided by builders
BzUIWidgetFlags flags;
const char *string;
BzUISize semanticSize[BZ_UI_AXIS_COUNT];
// recomputed every frame
f32 computedRelPosition[BZ_UI_AXIS_COUNT];
f32 computedSize[BZ_UI_AXIS_COUNT];
Rectangle rect;
} BzUIWidget;
typedef struct BzUIResult {
BzUIWidget *widget;
bool pressed;
bool released;
//bool dragging;
bool hovering;
} BzUIResult;
typedef struct BzUIWidgetDesc {
BzUIWidgetFlags flags;
const char *string;
BzUIKey key;
} BzUIWidgetDesc;
typedef struct BzUI BzUI;
BzUIKey bzUIKeyNull();
BzUIKey bzUIKeyFromString(const char *str);
BzUI *bzUICreate();
void bzUIDestroy(BzUI *ui);
void bzUIBegin(BzUI *ui, i32 width, i32 height);
void bzUIEnd(BzUI *ui);
// Widget construction
BzUIWidget *bzUIWidgetMake(const BzUIWidgetDesc *desc);
BzUIWidget *bzUIPushParent(BzUIWidget *widget);
BzUIWidget *bzUIPopParent();
BzUIResult bzUIGetResult(BzUIWidget *widget);
// UI
bool bzUIButton(const char *string);
#endif //BREEZE_UI_CORE_H

View File

@@ -3218,7 +3218,6 @@ const char *GetApplicationDirectory(void)
}
#elif defined(__linux__)
ssize_t readlink(const char *, const char *, unsigned int);
unsigned int size = sizeof(appDir);
ssize_t len = readlink("/proc/self/exe", appDir, size);

2
engine/libs/stb/stb.c Normal file
View File

@@ -0,0 +1,2 @@
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"

1895
engine/libs/stb/stb_ds.h Normal file

File diff suppressed because it is too large Load Diff