Add SDL2 support

This commit is contained in:
2025-06-07 02:41:14 -05:00
parent 8dc8b9ea86
commit dd4527645e
9 changed files with 188 additions and 4 deletions

69
src/SDL2/FontCache.cpp Normal file
View File

@@ -0,0 +1,69 @@
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "TessesFramework/SDL2/FontCache.hpp"
namespace Tesses::Framework::SDL2 {
void FontCache::Load(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color)
{
this->mw=0;
this->mh=0;
this->ps=ps;
for(size_t i = 0; i < this->font_chrs.size();i++)
{
SDL_Surface* surf = TTF_RenderGlyph_Blended(font,(Uint16)(i+32),color);
if(surf->w > this->mw) mw = surf->w;
if(surf->h > this->mh) mh = surf->h;
this->font_chrs[i] = SDL_CreateTextureFromSurface(renderer,surf);
SDL_FreeSurface(surf);
}
}
FontCache::FontCache(SDL_Renderer* renderer,std::string font,int sz,const SDL_Color& color)
{
TTF_Font* f = TTF_OpenFont(font.c_str(),sz);
Load(renderer,f,color);
TTF_CloseFont(f);
}
FontCache::FontCache(SDL_Renderer* renderer,const uint8_t* mem,size_t cnt,int sz,const SDL_Color& color)
{
TTF_Font* f = TTF_OpenFontRW(SDL_RWFromConstMem(mem,cnt),1,sz);
Load(renderer,f,color);
TTF_CloseFont(f);
}
FontCache::FontCache(SDL_Renderer* renderer,const std::vector<uint8_t>& v,int sz,const SDL_Color& color) : FontCache(renderer,v.data(),v.size(),sz,color)
{
}
FontCache::FontCache(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color)
{
this->Load(renderer,font,color);
}
SDL_Texture* FontCache::operator[](char c)
{
if(c >= 32 && c <= 126)
{
return this->font_chrs[c-32];
}
return this->font_chrs[95];
}
FontCache::~FontCache()
{
for(auto item : this->font_chrs)
SDL_DestroyTexture(item);
}
int FontCache::MaxWidth()
{
return this->mw;
}
int FontCache::MaxHeight()
{
return this->mh;
}
int FontCache::PointSize()
{
return this->ps;
}
}
#endif

View File

@@ -33,6 +33,9 @@ static GXRModeObj *rmode = NULL;
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
#include "TessesFramework/Threading/Mutex.hpp"
#endif
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include <SDL2/SDL.h>
#endif
namespace Tesses::Framework
{
@@ -59,6 +62,11 @@ namespace Tesses::Framework
static void _sigInt(int c)
{
isRunningSig=false;
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
SDL_Event quitEvent;
quitEvent.type = SDL_QUIT;
SDL_PushEvent(&quitEvent);
#endif
}
void TF_RunEventLoop()
{
@@ -108,16 +116,27 @@ namespace Tesses::Framework
#endif
}
void TF_SetIsRunning(bool _isRunning)
{
isRunning = _isRunning;
}
void TF_Quit()
{
isRunning=false;
#if defined(TESSESFRAMEWORK_ENABLE_THREADING) && (defined(GEKKO) || defined(__SWITCH__))
Tesses::Framework::Threading::JoinAllThreads();
#endif
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
SDL_Quit();
#endif
}
void TF_Init()
{
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
//SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS,"1");
SDL_Init(SDL_INIT_EVERYTHING);
#endif
tzset();
#if defined(_WIN32)
system(" ");