-[NEW] reorganització de códi
-[NEW] mòdul display operatiu -[NEW] mòdul tia pràcticament acabat (falta el reset) -[NEW] glue code para poder compilar. Primera compilació -[NEW] Afegides classes de UI -[NEW] Afegides classes de debug desensamblador del z80, a convertir a 6502
This commit is contained in:
+172
@@ -0,0 +1,172 @@
|
||||
#include "ui.h"
|
||||
|
||||
namespace ui
|
||||
{
|
||||
uint8_t colors[16][3] = {
|
||||
{0,0,0},
|
||||
{0,0,128},
|
||||
{0,128,0},
|
||||
{0,128,128},
|
||||
{128,0,0},
|
||||
{128,0,128},
|
||||
{255,128,0},
|
||||
{128,128,128},
|
||||
{30,30,30},
|
||||
{0,0,255},
|
||||
{0,255,0},
|
||||
{0,255,255},
|
||||
{255,0,0},
|
||||
{255,0,255},
|
||||
{255,255,0},
|
||||
{255,255,255},
|
||||
};
|
||||
|
||||
SDL_Renderer *ren = nullptr;
|
||||
SDL_Texture *tex = nullptr;
|
||||
|
||||
uint8_t offset_x = 0;
|
||||
uint8_t offset_y = 0;
|
||||
|
||||
bool clicked = false;
|
||||
|
||||
void (*dialog)(void) = nullptr;
|
||||
|
||||
SDL_Texture * createtexture(SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("font.bmp"));
|
||||
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
|
||||
return texture;
|
||||
}
|
||||
|
||||
void setrenderer(SDL_Renderer *renderer, SDL_Texture *texture)
|
||||
{
|
||||
ren = renderer;
|
||||
tex = texture;
|
||||
offset_x = offset_y = 0;
|
||||
}
|
||||
|
||||
void setoffset(uint8_t x, uint8_t y)
|
||||
{
|
||||
offset_x = x;
|
||||
offset_y = y;
|
||||
}
|
||||
|
||||
void incoffset(uint8_t x, uint8_t y)
|
||||
{
|
||||
offset_x += x;
|
||||
offset_y += y;
|
||||
}
|
||||
|
||||
uint8_t getOffsetX()
|
||||
{
|
||||
return offset_x;
|
||||
}
|
||||
|
||||
uint8_t getOffsetY()
|
||||
{
|
||||
return offset_y;
|
||||
}
|
||||
|
||||
bool mouseInside(int x, int y, int w, int h)
|
||||
{
|
||||
float mxf, myf;
|
||||
SDL_GetMouseState(&mxf, &myf);
|
||||
int mx = int(mxf)/CHR_W;
|
||||
int my = int(myf)/CHR_H;
|
||||
mx -= offset_x;
|
||||
my -= offset_y;
|
||||
return (mx >= x) && (mx < x+w) && (my >= y) && (my < y+h);
|
||||
}
|
||||
|
||||
void panel(int x, int y, int w, int h, const char *title)
|
||||
{
|
||||
ui::setoffset(x, y);
|
||||
|
||||
ui::box(0,0,w,h,COLOR_WHITE);
|
||||
ui::printrect(2,0, strlen(title)+2,1, COLOR_DARK);
|
||||
ui::printtxt(3,0, title, COLOR_WHITE);
|
||||
|
||||
ui::incoffset(1, 1);
|
||||
}
|
||||
|
||||
void box(int x, int y, int w, int h, uint8_t color)
|
||||
{
|
||||
SDL_FRect rect {float((offset_x+x)*CHR_W)+3, float((offset_y+y)*CHR_H)+6, float(w*CHR_W-6), float(h*CHR_H-13)};
|
||||
SDL_SetRenderDrawColor(ren, colors[color][0], colors[color][1], colors[color][2], 255);
|
||||
SDL_RenderRect(ren, &rect);
|
||||
}
|
||||
|
||||
void printrect(int x, int y, int w, int h, uint8_t color)
|
||||
{
|
||||
SDL_FRect rect {float(offset_x+x)*CHR_W, float(offset_y+y)*CHR_H, float(w*CHR_W), float(h*CHR_H)};
|
||||
SDL_SetRenderDrawColor(ren, colors[color][0], colors[color][1], colors[color][2], 255);
|
||||
SDL_RenderFillRect(ren, &rect);
|
||||
}
|
||||
|
||||
void printvoidrect(int x, int y, int w, int h, uint8_t color)
|
||||
{
|
||||
SDL_FRect rect {float(offset_x+x)*CHR_W, float(offset_y+y)*CHR_H, float(w*CHR_W), float(h*CHR_H)};
|
||||
SDL_SetRenderDrawColor(ren, colors[color][0], colors[color][1], colors[color][2], 255);
|
||||
SDL_RenderRect(ren, &rect);
|
||||
}
|
||||
|
||||
void printchar(int x, int y, char chr, uint8_t color)
|
||||
{
|
||||
if (color != 255) SDL_SetRenderDrawColor(ren, colors[color][0], colors[color][1], colors[color][2], 255);
|
||||
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(offset_x+x)*CHR_W, float(offset_y+y)*CHR_H, float(CHR_W), float(CHR_H)};
|
||||
SDL_RenderTexture(ren, tex, &src, &dst);
|
||||
}
|
||||
|
||||
void printtxt(int x, int y, const char *text, uint8_t color)
|
||||
{
|
||||
SDL_SetTextureColorMod(tex, colors[color][0], colors[color][1], colors[color][2]);
|
||||
for (int i=0; i<strlen(text);++i) if (text[i]!=32) printchar(x+i, y, text[i]);
|
||||
}
|
||||
|
||||
void placechar(int x, int y, char chr, uint8_t color)
|
||||
{
|
||||
if (color != 255) SDL_SetRenderDrawColor(ren, colors[color][0], colors[color][1], colors[color][2], 255);
|
||||
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(x), float(y), float(CHR_W*2), float(CHR_H*2)};
|
||||
SDL_RenderTexture(ren, tex, &src, &dst);
|
||||
}
|
||||
|
||||
void placetxt(int x, int y, const char *text, uint8_t color)
|
||||
{
|
||||
SDL_SetTextureColorMod(tex, colors[color][0], colors[color][1], colors[color][2]);
|
||||
for (int i=0; i<strlen(text);++i) if (text[i]!=32) placechar(x+i*CHR_W*2, y, text[i]);
|
||||
}
|
||||
|
||||
void setClicked(const bool value)
|
||||
{
|
||||
clicked = value;
|
||||
}
|
||||
|
||||
const bool getClicked()
|
||||
{
|
||||
return clicked;
|
||||
}
|
||||
|
||||
void setDialog(void(*new_dialog)(void))
|
||||
{
|
||||
ui:dialog = new_dialog;
|
||||
}
|
||||
|
||||
bool hasDialog()
|
||||
{
|
||||
return dialog != nullptr;
|
||||
}
|
||||
|
||||
void callDialog()
|
||||
{
|
||||
if (dialog) dialog();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user