- [WIP] nova UI

This commit is contained in:
raimon@prometeo committed 2026-09-25 06:45:32 +02:00
1 parent 6c205fb014
commit dedd6b6285
4 files changed
+57 -2

No files matched your search

+12 -1
View File
@@ -16,8 +16,8 @@ namespace ui
Context::~Context() Context::~Context()
{ {
SDL_DestroyRenderer(this->ren);
SDL_DestroyTexture(this->fontTex); SDL_DestroyTexture(this->fontTex);
SDL_DestroyRenderer(this->ren);
} }
SDL_Texture *Context::CreateTexture(int width, int height) SDL_Texture *Context::CreateTexture(int width, int height)
@@ -88,6 +88,12 @@ namespace ui
return ( (tmp_x >= x) && (tmp_x < x+w) && (tmp_y >= y) && (tmp_y < y+h) ); return ( (tmp_x >= x) && (tmp_x < x+w) && (tmp_y >= y) && (tmp_y < y+h) );
} }
void Context::Clear(uint32_t color)
{
SDL_SetRenderDrawColor(ren, (color>>16)&0xff, (color>>8)&0xff, color&0xff, (color>>24)&0xff);
SDL_RenderClear(ren);
}
void Context::DrawRect(int x, int y, int w, int h, uint32_t color) void Context::DrawRect(int x, int y, int w, int h, uint32_t color)
{ {
SDL_FRect rect {float(this->offsetX+x), float(this->offsetY+y), float(w), float(h)}; SDL_FRect rect {float(this->offsetX+x), float(this->offsetY+y), float(w), float(h)};
@@ -123,4 +129,9 @@ namespace ui
SDL_SetTextureColorMod(this->fontTex, (color>>16)&0xff, (color>>8)&0xff, color&0xff); SDL_SetTextureColorMod(this->fontTex, (color>>16)&0xff, (color>>8)&0xff, color&0xff);
for (int i=0; i<(int)strlen(text);++i) if (text[i]!=32) DrawChar(x+i*CHR_W, y, text[i]); for (int i=0; i<(int)strlen(text);++i) if (text[i]!=32) DrawChar(x+i*CHR_W, y, text[i]);
} }
void Context::Present()
{
SDL_RenderPresent(this->ren);
}
} }
+3
View File
@@ -22,12 +22,15 @@ namespace ui
bool IsMouseInside(int x, int y, int w, int h); bool IsMouseInside(int x, int y, int w, int h);
void Clear(uint32_t color);
void DrawRect(int x, int y, int w, int h, uint32_t color); void DrawRect(int x, int y, int w, int h, uint32_t color);
void FillRect(int x, int y, int w, int h, uint32_t color); void FillRect(int x, int y, int w, int h, uint32_t color);
void DrawLine(int x1, int y1, int x2, int y2, uint32_t color); void DrawLine(int x1, int y1, int x2, int y2, uint32_t color);
void DrawChar(int x, int y, char chr, uint32_t color=0xffffffff); void DrawChar(int x, int y, char chr, uint32_t color=0xffffffff);
void DrawText(int x, int y, const char *text, uint32_t color); void DrawText(int x, int y, const char *text, uint32_t color);
void Present();
protected: protected:
SDL_Window *win; SDL_Window *win;
SDL_Renderer *ren; SDL_Renderer *ren;
+40 -1
View File
@@ -26,16 +26,55 @@ namespace ui
bool Window::HandleEvents(SDL_Event *e) bool Window::HandleEvents(SDL_Event *e)
{ {
switch (e->type) { switch (e->type) {
case SDL_EVENT_WINDOW_EXPOSED:
case SDL_EVENT_WINDOW_SHOWN:
this->Update();
break;
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION:
input.mouse.x = e->motion.x; input.mouse.x = e->motion.x;
input.mouse.y = e->motion.y; input.mouse.y = e->motion.y;
this->Update();
break;
// ... case SDL_EVENT_MOUSE_BUTTON_DOWN:
input.mouse.buttons[e->button.button-1] = mbsDown;
this->Update();
input.mouse.buttons[e->button.button-1] = mbsPressed;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
input.mouse.buttons[e->button.button-1] = mbsUp;
this->Update();
input.mouse.buttons[e->button.button-1] = mbsReleased;
break;
case SDL_EVENT_MOUSE_WHEEL:
input.mouse.wheel = e->wheel.integer_y;
this->Update();
input.mouse.wheel = 0;
break;
case SDL_EVENT_KEY_DOWN:
input.keyboard.keyDown = e->key.scancode;
this->Update();
input.keyboard.keyDown = 0;
break;
case SDL_EVENT_KEY_UP:
input.keyboard.keyUp = e->key.scancode;
this->Update();
input.keyboard.keyUp = 0;
break;
default: break; default: break;
} }
} }
void Window::Update()
{
this->context->Clear(0xff1e1e1e);
}
/* MÈTODES ESTATICS */ /* MÈTODES ESTATICS */
/* ================ */ /* ================ */
+2
View File
@@ -10,6 +10,7 @@ namespace ui
struct { struct {
int x, y; int x, y;
MouseButtonState buttons[3] = {mbsReleased, mbsReleased, mbsReleased}; MouseButtonState buttons[3] = {mbsReleased, mbsReleased, mbsReleased};
int wheel = 0;
} mouse; } mouse;
struct { struct {
uint8_t keyDown = 0; uint8_t keyDown = 0;
@@ -25,6 +26,7 @@ namespace ui
~Window(); ~Window();
bool HandleEvents(SDL_Event *e); bool HandleEvents(SDL_Event *e);
virtual void Update();
static void DistributeEvent(SDL_Event *e); static void DistributeEvent(SDL_Event *e);