wip sw renderer...bsp is borked, also lots of other things
This commit is contained in:
parent
415d424057
commit
c771fa665d
56 changed files with 8151 additions and 1261 deletions
140
src/gui/pxl8_gui.c
Normal file
140
src/gui/pxl8_gui.c
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
#include "pxl8_gui.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_gfx.h"
|
||||
|
||||
pxl8_gui_state* pxl8_gui_state_create(void) {
|
||||
pxl8_gui_state* state = (pxl8_gui_state*)malloc(sizeof(pxl8_gui_state));
|
||||
if (!state) return NULL;
|
||||
|
||||
memset(state, 0, sizeof(pxl8_gui_state));
|
||||
return state;
|
||||
}
|
||||
|
||||
void pxl8_gui_state_destroy(pxl8_gui_state* state) {
|
||||
if (!state) return;
|
||||
free(state);
|
||||
}
|
||||
|
||||
void pxl8_gui_begin_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
|
||||
if (!state) return;
|
||||
state->hot_id = 0;
|
||||
if (gfx) pxl8_gfx_push_target(gfx);
|
||||
}
|
||||
|
||||
void pxl8_gui_end_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
|
||||
if (!state) return;
|
||||
|
||||
if (!state->cursor_down) {
|
||||
state->active_id = 0;
|
||||
}
|
||||
state->cursor_clicked = false;
|
||||
if (gfx) pxl8_gfx_pop_target(gfx);
|
||||
}
|
||||
|
||||
void pxl8_gui_cursor_move(pxl8_gui_state* state, i32 x, i32 y) {
|
||||
if (!state) return;
|
||||
state->cursor_x = x;
|
||||
state->cursor_y = y;
|
||||
}
|
||||
|
||||
void pxl8_gui_cursor_down(pxl8_gui_state* state) {
|
||||
if (!state) return;
|
||||
state->cursor_down = true;
|
||||
}
|
||||
|
||||
void pxl8_gui_cursor_up(pxl8_gui_state* state) {
|
||||
if (!state) return;
|
||||
state->cursor_down = false;
|
||||
state->cursor_clicked = true;
|
||||
}
|
||||
|
||||
static bool is_cursor_over(const pxl8_gui_state* state, i32 x, i32 y, i32 w, i32 h) {
|
||||
return state->cursor_x >= x && state->cursor_x < (x + w) &&
|
||||
state->cursor_y >= y && state->cursor_y < (y + h);
|
||||
}
|
||||
|
||||
bool pxl8_gui_button(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, const char* label) {
|
||||
if (!state || !gfx || !label) return false;
|
||||
|
||||
bool cursor_over = is_cursor_over(state, x, y, w, h);
|
||||
bool is_hot = (state->hot_id == id);
|
||||
bool is_active = (state->active_id == id);
|
||||
|
||||
if (cursor_over) {
|
||||
state->hot_id = id;
|
||||
}
|
||||
|
||||
if (cursor_over && state->cursor_down && state->active_id == 0) {
|
||||
state->active_id = id;
|
||||
}
|
||||
|
||||
bool clicked = is_active && state->cursor_clicked && cursor_over;
|
||||
if (clicked) {
|
||||
state->active_id = 0;
|
||||
}
|
||||
|
||||
u8 bg_color;
|
||||
u8 border_color;
|
||||
i32 offset_x = 0;
|
||||
i32 offset_y = 0;
|
||||
|
||||
if (is_active) {
|
||||
bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
|
||||
border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
|
||||
offset_x = 1;
|
||||
offset_y = 1;
|
||||
} else if (is_hot || cursor_over) {
|
||||
bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
|
||||
border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_FG0);
|
||||
} else {
|
||||
bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
|
||||
border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
|
||||
}
|
||||
|
||||
pxl8_2d_rect_fill(gfx, x, y, w, h, bg_color);
|
||||
pxl8_2d_rect(gfx, x, y, w, h, border_color);
|
||||
|
||||
i32 text_len = (i32)strlen(label);
|
||||
i32 text_x = x + (w / 2) - ((text_len * 8) / 2) + offset_x;
|
||||
i32 text_y = y + (h / 2) - 5 + offset_y;
|
||||
pxl8_2d_text(gfx, label, text_x, text_y, pxl8_gfx_ui_color(gfx, PXL8_UI_FG1));
|
||||
|
||||
return clicked;
|
||||
}
|
||||
|
||||
void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title) {
|
||||
if (!gfx || !title) return;
|
||||
|
||||
u8 title_bg = pxl8_gfx_ui_color(gfx, PXL8_UI_BG1);
|
||||
u8 body_bg = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
|
||||
u8 border = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
|
||||
u8 title_fg = pxl8_gfx_ui_color(gfx, PXL8_UI_FG0);
|
||||
|
||||
pxl8_2d_rect_fill(gfx, x, y, w, 28, title_bg);
|
||||
pxl8_2d_rect_fill(gfx, x, y + 28, w, h - 28, body_bg);
|
||||
pxl8_2d_rect(gfx, x, y, w, h, border);
|
||||
pxl8_2d_rect_fill(gfx, x, y + 28, w, 1, border);
|
||||
|
||||
i32 title_x = x + 10;
|
||||
i32 title_y = y + (28 / 2) - 5;
|
||||
pxl8_2d_text(gfx, title, title_x, title_y, title_fg);
|
||||
}
|
||||
|
||||
void pxl8_gui_label(pxl8_gfx* gfx, i32 x, i32 y, const char* text, u8 color) {
|
||||
if (!gfx || !text) return;
|
||||
pxl8_2d_text(gfx, text, x, y, color);
|
||||
}
|
||||
|
||||
bool pxl8_gui_is_hovering(const pxl8_gui_state* state) {
|
||||
if (!state) return false;
|
||||
return state->hot_id != 0;
|
||||
}
|
||||
|
||||
void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y) {
|
||||
if (!state) return;
|
||||
if (x) *x = state->cursor_x;
|
||||
if (y) *y = state->cursor_y;
|
||||
}
|
||||
38
src/gui/pxl8_gui.h
Normal file
38
src/gui/pxl8_gui.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_gfx.h"
|
||||
#include "pxl8_types.h"
|
||||
|
||||
typedef struct {
|
||||
i32 cursor_x;
|
||||
i32 cursor_y;
|
||||
bool cursor_down;
|
||||
bool cursor_clicked;
|
||||
u32 hot_id;
|
||||
u32 active_id;
|
||||
} pxl8_gui_state;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
pxl8_gui_state* pxl8_gui_state_create(void);
|
||||
void pxl8_gui_state_destroy(pxl8_gui_state* state);
|
||||
|
||||
void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y);
|
||||
bool pxl8_gui_is_hovering(const pxl8_gui_state* state);
|
||||
|
||||
void pxl8_gui_begin_frame(pxl8_gui_state* state, pxl8_gfx* gfx);
|
||||
void pxl8_gui_end_frame(pxl8_gui_state* state, pxl8_gfx* gfx);
|
||||
|
||||
void pxl8_gui_cursor_down(pxl8_gui_state* state);
|
||||
void pxl8_gui_cursor_move(pxl8_gui_state* state, i32 x, i32 y);
|
||||
void pxl8_gui_cursor_up(pxl8_gui_state* state);
|
||||
|
||||
bool pxl8_gui_button(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, const char* label);
|
||||
void pxl8_gui_label(pxl8_gfx* gfx, i32 x, i32 y, const char* text, u8 color);
|
||||
void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
41
src/gui/pxl8_gui_palette.h
Normal file
41
src/gui/pxl8_gui_palette.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_types.h"
|
||||
|
||||
#define PXL8_UI_PALETTE_SIZE 16
|
||||
|
||||
#define PXL8_UI_BG0 0
|
||||
#define PXL8_UI_BG1 1
|
||||
#define PXL8_UI_BG2 2
|
||||
#define PXL8_UI_BG3 3
|
||||
#define PXL8_UI_FG0 4
|
||||
#define PXL8_UI_FG1 5
|
||||
#define PXL8_UI_FG2 6
|
||||
#define PXL8_UI_FG3 7
|
||||
#define PXL8_UI_RED 8
|
||||
#define PXL8_UI_GREEN 9
|
||||
#define PXL8_UI_YELLOW 10
|
||||
#define PXL8_UI_BLUE 11
|
||||
#define PXL8_UI_PURPLE 12
|
||||
#define PXL8_UI_AQUA 13
|
||||
#define PXL8_UI_ORANGE 14
|
||||
#define PXL8_UI_GRAY 15
|
||||
|
||||
static const u32 pxl8_ui_palette[PXL8_UI_PALETTE_SIZE] = {
|
||||
0xFF282828,
|
||||
0xFF3c3836,
|
||||
0xFF504945,
|
||||
0xFF665c54,
|
||||
0xFFc7f1fb,
|
||||
0xFFb2dbeb,
|
||||
0xFFa1c4d5,
|
||||
0xFF93aebd,
|
||||
0xFF3449fb,
|
||||
0xFF26bbb8,
|
||||
0xFF2fbdfa,
|
||||
0xFF98a583,
|
||||
0xFF9b86d3,
|
||||
0xFF7cc08e,
|
||||
0xFF1980fe,
|
||||
0xFF928374,
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue