From 6c205fb014294eb8966af89cf95eef4803ba6120 Mon Sep 17 00:00:00 2001 From: "Raimon @ quifisraimon" Date: Thu, 24 Sep 2026 14:01:45 +0200 Subject: [PATCH] =?UTF-8?q?-=20[NEW]=20Pasada=20la=20textura=20de=20font?= =?UTF-8?q?=20a=20png=20-=20[FIX]=20debugger=20inicialitzat=20al=20comen?= =?UTF-8?q?=C3=A7ar=20-=20[WIP]=20Noves=20classes=20de=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- font.png | Bin 0 -> 822 bytes source/main.cpp | 1 + source/ui.cpp | 2 +- source/ui/context.cpp | 126 ++++++++++++++++++++++++++++++++++++++++++ source/ui/context.h | 39 +++++++++++++ source/ui/window.cpp | 50 +++++++++++++++++ source/ui/window.h | 38 +++++++++++++ 7 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 font.png create mode 100644 source/ui/context.cpp create mode 100644 source/ui/context.h create mode 100644 source/ui/window.cpp create mode 100644 source/ui/window.h diff --git a/font.png b/font.png new file mode 100644 index 0000000000000000000000000000000000000000..fa3849a26e7dc2622eea7214204926722372559d GIT binary patch literal 822 zcmV-61Ihe}P)p@`K-?i3Wr!&Trad1fN}jKyZuc$RgJu&GXM|=tQs`oYYjU)(B8w> zJ_oAjg1-$&4-hUYt5yr#m(HYn0agp#JGQolgmK$@g47M3zv0oYyB$Em?q0G@zpMVjSGvjyrf_EIsH8=X3& z%Cg3)zw!N0>Ww!Xl2=Q2F;Xm>^Sg<~<1w0I!0ugHGj=h~^b`+X7T1B=-G9I7uL(KH zCdcC~!I%62cc;G2$3t_Irpe^e3zCfJX_fW>U2E~rw?*TVb^aEBi%QP0Jx5=1!3#bg7M5=;oP`#(IUv$0+449vnW(|Y3fpx%d0xD>HbhWBtt23O}NaP zUP0-lro6S)z?y`2bEOJ&m7G-hxun--G?i8XfVF@S@E$>!?eF30zRJ-;)059Xg0O5d|MQovRS*4Z$k9Hfn^L;7=l6RsNK!G}^rjjk- z-*ioAZ{@lE*ixj{{zh{r&y*_i-HdE#2e6X47ohA1vFrQX`3ur*bg;sR!Dg9^x!+i# zA!-%tkwwm?VL^Kkmp!XJc%}YTTaM{uX@BnCB|x>H1=#Gs5rg&7@dDtc{vyZ;GQuky zkvX8eqFDapsIKU5yCojjuj#kzrvN~&@EY0x9AGN|y_P)-U}Wihi#IQ#@`htqJqiuL zfNjpt+@wl-a6Y#?=|z?T-X73hqygy9OeYqwQwdg5GCPUXGjc-@UKLfa@72g0p0MiBqqtdZ{T>t<807*qoM6N<$f+n1W Aj{pDw literal 0 HcmV?d00001 diff --git a/source/main.cpp b/source/main.cpp index c30276c..56379ec 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -17,6 +17,7 @@ int main(int argc, char *argv[]) { m6502dis::symbols::load(); atari2600::init(); atari2600::pause(); + m6502debugger::init(); m6502debugger::show(); int wait = 16; diff --git a/source/ui.cpp b/source/ui.cpp index 4e3abae..e4a56f7 100644 --- a/source/ui.cpp +++ b/source/ui.cpp @@ -33,7 +33,7 @@ namespace ui SDL_Texture * createtexture(SDL_Renderer *renderer) { - SDL_Surface *bmp_surface = SDL_LoadBMP("font.bmp"); + SDL_Surface *bmp_surface = SDL_LoadPNG("font.png"); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp_surface); SDL_DestroySurface(bmp_surface); SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST); diff --git a/source/ui/context.cpp b/source/ui/context.cpp new file mode 100644 index 0000000..9a3346b --- /dev/null +++ b/source/ui/context.cpp @@ -0,0 +1,126 @@ +#include "context.h" + +#define CHR_W 6 +#define CHR_H 13 + +namespace ui +{ + Context::Context(SDL_Window *window) + { + this->win = window; + this->ren = SDL_CreateRenderer(this->win, NULL); + this->fontTex = LoadTexture("font.png"); + + this->offsetX = this->offsetY = 0; + } + + Context::~Context() + { + SDL_DestroyRenderer(this->ren); + SDL_DestroyTexture(this->fontTex); + } + + SDL_Texture *Context::CreateTexture(int width, int height) + { + SDL_Texture *texture = SDL_CreateTexture(this->ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); + SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST); + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + return texture; + } + + SDL_Texture *Context::LoadTexture(const char* filename) + { + SDL_Surface *bmp_surface = SDL_LoadPNG(filename); + SDL_Texture *texture = SDL_CreateTextureFromSurface(this->ren, bmp_surface); + SDL_DestroySurface(bmp_surface); + SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST); + SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); + return texture; + } + + void Context::SetOffset(int x, int y) + { + this->offsetX = x; + this->offsetY = y; + } + + void Context::IncOffset(int x, int y) + { + this->offsetX += x; + this->offsetY += y; + } + + int Context::GetOffsetX() + { + return this->offsetX; + } + + int Context::GetOffsetY() + { + return this->offsetY; + } + + void Context::PushOffset() + { + this->offsetStack.push({this->offsetX, this->offsetY}); + } + + void Context::PopOffset() + { + this->offsetX = this->offsetStack.top().first; + this->offsetY = this->offsetStack.top().second; + this->offsetStack.pop(); + } + + void Context::ClearOffsetStack() + { + this->offsetStack = std::stack>(); + } + + bool Context::IsMouseInside(int x, int y, int w, int h) + { + float tmpf_x, tmpf_y; + SDL_GetMouseState(&tmpf_x, &tmpf_y); + + const int tmp_x = int(tmpf_x) + this->offsetX; + const int tmp_y = int(tmpf_y) + this->offsetY; + + return ( (tmp_x >= x) && (tmp_x < x+w) && (tmp_y >= y) && (tmp_y < y+h) ); + } + + 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_SetRenderDrawColor(this->ren, (color>>16)&0xff, (color>>8)&0xff, color&0xff, (color>>24)&0xff); + SDL_RenderRect(this->ren, &rect); + } + + void Context::FillRect(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_SetRenderDrawColor(this->ren, (color>>16)&0xff, (color>>8)&0xff, color&0xff, (color>>24)&0xff); + SDL_RenderFillRect(this->ren, &rect); + } + + void Context::DrawLine(int x1, int y1, int x2, int y2, uint32_t color) + { + SDL_SetRenderDrawColor(this->ren, (color>>16)&0xff, (color>>8)&0xff, color&0xff, (color>>24)&0xff); + SDL_RenderLine(this->ren, float(this->offsetX+x1), float(this->offsetY+y1), float(this->offsetX+x2), float(this->offsetY+y2)); + } + + void Context::DrawChar(int x, int y, char chr, uint32_t color=0xffffffff) + { + if (color != 255) SDL_SetRenderDrawColor(this->ren, (color>>16)&0xff, (color>>8)&0xff, color&0xff, (color>>24)&0xff); + if (chr==32) return; + if (chr<32 || chr>127) chr = '.'; + SDL_FRect src {float((chr-32)&0xf)*CHR_W, float((chr-32)>>4)*CHR_H, float(CHR_W), float(CHR_H)}; + SDL_FRect dst {float(this->offsetX+x), float(this->offsetY+y), float(CHR_W), float(CHR_H)}; + SDL_RenderTexture(ren, this->fontTex, &src, &dst); + } + + void Context::DrawText(int x, int y, const char *text, uint32_t color) + { + 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]); + } +} \ No newline at end of file diff --git a/source/ui/context.h b/source/ui/context.h new file mode 100644 index 0000000..af29ff1 --- /dev/null +++ b/source/ui/context.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include + +namespace ui +{ + class Context { + public: + Context(SDL_Window *window); + ~Context(); + + SDL_Texture * CreateTexture(int width, int height); + SDL_Texture * LoadTexture(const char* filename); + + void SetOffset(int x, int y); + void IncOffset(int x, int y); + int GetOffsetX(); + int GetOffsetY(); + void PushOffset(); + void PopOffset(); + void ClearOffsetStack(); + + bool IsMouseInside(int x, int y, int w, int h); + + 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 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 DrawText(int x, int y, const char *text, uint32_t color); + + protected: + SDL_Window *win; + SDL_Renderer *ren; + SDL_Texture *fontTex; + + int offsetX, offsetY; + std::stack> offsetStack; + }; +} diff --git a/source/ui/window.cpp b/source/ui/window.cpp new file mode 100644 index 0000000..f0d7c1e --- /dev/null +++ b/source/ui/window.cpp @@ -0,0 +1,50 @@ +#include "window.h" + +namespace ui +{ + Window::Window(const char *title, int width, int height) + { + this->win = SDL_CreateWindow(title, width, height, 0); + this->context = new Context(this->win); + + windows.push_back({SDL_GetWindowID(this->win), this}); + + float x, y; + SDL_GetMouseState(&x, &y); + input.mouse.x = int(x); + input.mouse.y = int(y); + } + + Window::~Window() + { + for (size_t i=0; icontext; + SDL_DestroyWindow(this->win); + } + + bool Window::HandleEvents(SDL_Event *e) + { + switch (e->type) { + case SDL_EVENT_MOUSE_MOTION: + input.mouse.x = e->motion.x; + input.mouse.y = e->motion.y; + + // ... + + default: break; + } + } + + + /* MÈTODES ESTATICS */ + /* ================ */ + + std::vector> Window::windows; + + void Window::DistributeEvent(SDL_Event *e) + { + uint32_t id = SDL_GetWindowID(SDL_GetWindowFromEvent(e)); + for (auto window : Window::windows) if (window.first == id) window.second->HandleEvents(e); + } +} \ No newline at end of file diff --git a/source/ui/window.h b/source/ui/window.h new file mode 100644 index 0000000..bc8c876 --- /dev/null +++ b/source/ui/window.h @@ -0,0 +1,38 @@ +#pragma once +#include "context.h" +#include + +namespace ui +{ + enum MouseButton { mbLeft = 0, mbMiddle = 1, mbRight = 2 }; + enum MouseButtonState { mbsReleased, mbsDown, mbsPressed, mbsUp }; + struct Input { + struct { + int x, y; + MouseButtonState buttons[3] = {mbsReleased, mbsReleased, mbsReleased}; + } mouse; + struct { + uint8_t keyDown = 0; + uint8_t keyUp = 0; + const bool *keys; + } keyboard; + }; + + class Window { + public: + + Window(const char *title, int width, int height); + ~Window(); + + bool HandleEvents(SDL_Event *e); + + static void DistributeEvent(SDL_Event *e); + + protected: + SDL_Window *win; + Context *context; + Input input; + + static std::vector> windows; + }; +} \ No newline at end of file