- [NEW] Pasada la textura de font a png
- [FIX] debugger inicialitzat al començar - [WIP] Noves classes de UI
This commit is contained in:
1 parent
1dddc11722
commit
6c205fb014
7 files changed
+255
-1
No files matched your search
@@ -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<std::pair<int, int>>();
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user