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

View File

@ -41,6 +41,7 @@ src/Crypto/MbedHelpers.cpp
src/TF_Init.cpp
src/wrapper.cpp
src/HiddenField.cpp
src/SDL2/FontCache.cpp
)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
@ -57,6 +58,7 @@ option(TESSESFRAMEWORK_ENABLE_STATIC "Enable Tesses Framework Static Libraries"
option(TESSESFRAMEWORK_ENABLE_SHARED "Enable Tesses Framework Shared Libraries" ON)
option(TESSESFRAMEWORK_ENABLE_SETDATE "Enable setting date to file" ON)
option(TESSESFRAMEWORK_LOGTOFILE "TessesFramework Log to file" OFF)
option(TESSESFRAMEWORK_ENABLE_SDL2 "Enable SDL2" OFF)
option(TESSESFRAMEWORK_FETCHCONTENT "TessesFramework fetchcontent" OFF)
if(TESSESFRAMEWORK_FETCHCONTENT)
set(TESSESFRAMEWORK_CERT_BUNDLE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/ca-certificates.crt" CACHE FILEPATH "Path to ca-chain")
@ -67,6 +69,14 @@ endif()
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)
if(TESSESFRAMEWORK_ENABLE_SDL2)
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_image REQUIRED)
endif()
if(TESSESFRAMEWORK_ENABLE_MBED)
if(TESSESFRAMEWORK_EMBED_CERT_BUNDLE)
include(cmake/bin2h.cmake)
@ -89,8 +99,8 @@ endif()
FetchContent_Declare(
mbedtls
GIT_REPOSITORY https://github.com/Mbed-TLS/mbedtls.git
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(mbedtls)
else()
set(MBEDTLS_DIR "" CACHE PATH "Mbed tls directory")
@ -120,6 +130,10 @@ endif()
if(TESSESFRAMEWORK_ENABLE_NETWORKING)
target_compile_definitions(${TessesFramework_TARGET} PUBLIC TESSESFRAMEWORK_ENABLE_NETWORKING)
endif()
if(TESSESFRAMEWORK_ENABLE_SDL2)
target_link_libraries(${TessesFramework_TARGET} PUBLIC SDL2::SDL2 SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf)
target_compile_definitions(${TessesFramework_TARGET} PUBLIC TESSESFRAMEWORK_ENABLE_SDL2)
endif()
if(TESSESFRAMEWORK_ENABLE_MBED)
target_compile_definitions(${TessesFramework_TARGET} PUBLIC TESSESFRAMEWORK_ENABLE_MBED)
if(TESSESFRAMEWORK_EMBED_CERT_BUNDLE)
@ -170,6 +184,7 @@ if(TESSESFRAMEWORK_ENABLE_STATIC)
add_library(tessesframework STATIC ${TESSESFRAMEWORK_SOURCE})
TESSESFRAMEWORK_LINKDEPS(tessesframework)
if(TESSESFRAMEWORK_FETCHCONTENT AND TESSESFRAMEWORK_ENABLE_MBED)
target_link_libraries(tessesframework PUBLIC mbedtls mbedx509 mbedcrypto everest p256m)
@ -189,6 +204,7 @@ else()
endif()
add_library(tessesframework_shared SHARED ${TESSESFRAMEWORK_SOURCE})
TESSESFRAMEWORK_LINKDEPS(tessesframework_shared)
if(TESSESFRAMEWORK_FETCHCONTENT AND TESSESFRAMEWORK_ENABLE_MBED)
target_link_libraries(tessesframework_shared PUBLIC mbedtls mbedx509 mbedcrypto everest p256m)
@ -235,6 +251,10 @@ DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFramework)
endif()
if(TESSESFRAMEWORK_ENABLE_EXAMPLES)
if(TESSESFRAMEWORK_ENABLE_SDL2)
add_executable(sdl2 examples/sdl2.cpp)
target_link_libraries(sdl2 PUBLIC tessesframework)
endif()
add_executable(copyfile examples/copyfile.cpp)
target_link_libraries(copyfile PUBLIC tessesframework)

View File

@ -17,5 +17,13 @@ if(${TESSESFRAMEWORK_ENABLE_LIBWEBCAM})
find_package(libwebcam)
endif()
set(TESSESFRAMEWORK_ENABLE_SDL2 @TESSESFRAMEWORK_ENABLE_SDL2@)
if(${TESSESFRAMEWORK_ENABLE_SDL2})
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_image REQUIRED)
endif()
set(TESSESFRAMEWORK_ENABLE_NETWORKING @TESSESFRAMEWORK_ENABLE_NETWORKING@)
set(TESSESFRAMEWORK_ENABLE_THREADING @TESSESFRAMEWORK_ENABLE_THREADING@)

View File

@ -16,5 +16,8 @@
#if TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_LOGTOFILE@
#define TESSESFRAMEWORK_LOGTOFILE
#endif
#if TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_SDL2@
#define TESSESFRAMEWORK_ENABLE_SDL2
#endif
#undef TESSES_FRAMEWORK_FLAG_OFF
#undef TESSES_FRAMEWORK_FLAG_ON

28
examples/sdl2.cpp Normal file
View File

@ -0,0 +1,28 @@
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include <SDL2/SDL.h>
#endif
int main(int argc,char** argv)
{
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("My SDL Window",0,0,320,240,0);
SDL_Renderer* renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
while(true)
{
SDL_Event event;
if(SDL_PollEvent(&event) && event.type == SDL_QUIT) break;
SDL_SetRenderDrawColor(renderer,255,0,0,255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
#endif
return 0;
}

View File

@ -8,7 +8,7 @@
#include <map>
#include <vector>
#include <functional>
#include "Threading/Mutex.hpp"
class TextException : public std::exception {
std::string error_message;
@ -52,18 +52,22 @@ namespace Tesses::Framework
template<typename...TArgs>
class EventList : public Event<TArgs...> {
Threading::Mutex mtx;
std::vector<std::shared_ptr<Event<TArgs...>>> items;
public:
void operator+=(std::shared_ptr<Event<TArgs...>> event)
{
mtx.Lock();
for(std::shared_ptr<Event<TArgs...>>& item : this->items)
{
if(item.get() == event.get()) return;
}
this->items.push_back(event);
mtx.Unlock();
}
void operator-=(std::shared_ptr<Event<TArgs...>> event)
{
mtx.Lock();
for(auto i = this->items.begin(); i != this->items.end(); i++)
{
if(i->get() == event.get())
@ -72,13 +76,16 @@ namespace Tesses::Framework
return;
}
}
mtx.Lock();
}
void Invoke(TArgs... args)
{
mtx.Lock();
for(auto& item : this->items)
{
item->Invoke(args...);
}
mtx.Unlock();
}
};
@ -89,6 +96,7 @@ namespace Tesses::Framework
void TF_RunEventLoop();
void TF_RunEventLoopItteration();
bool TF_IsRunning();
void TF_SetIsRunning(bool _isRunning);
void TF_Quit();
bool TF_GetConsoleEventsEnabled();
void TF_SetConsoleEventsEnabled(bool flag);

View File

@ -0,0 +1,28 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include <array>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <string>
#include <vector>
namespace Tesses::Framework::SDL2
{
class FontCache
{
std::array<SDL_Texture*,96> font_chrs;
int mw,mh,ps;
void Load(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color);
public:
FontCache(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,std::string font,int sz,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,const uint8_t* mem,size_t cnt,int sz,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,const std::vector<uint8_t>& v,int sz,const SDL_Color& color);
SDL_Texture* operator[](char c);
int MaxWidth();
int MaxHeight();
int PointSize();
~FontCache();
};
}
#endif

View File

@ -27,4 +27,5 @@
#include "Lazy.hpp"
#include "Mail/Smtp.hpp"
#include "HiddenField.hpp"
#include "Serialization/Json.hpp"
#include "Serialization/Json.hpp"
#include "SDL2/FontCache.hpp"

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(" ");