Migrate to SDL2 and deploy as WASM
All checks were successful
Build & deploy web version / Build WebAssembly & deploy (push) Successful in 1m8s

This commit is contained in:
Andras Schmelczer 2026-08-09 20:00:56 +01:00
commit c9b542eccf

View file

@ -3,12 +3,13 @@
///Indításnál betölti a szükséges betűtípusokat.
void openFonts() {
SDL_EnableUNICODE(1);
TTF_Init();
fontSmaller = TTF_OpenFont("resources/Roboto-Thin.ttf", 40);
fontSmall = TTF_OpenFont("resources/Roboto-Thin.ttf", 48);
fontSmallBold = TTF_OpenFont("resources/Roboto-Light.ttf", 53);
fontLarge = TTF_OpenFont("resources/Roboto-Thin.ttf", 120);
if(!TTF_WasInit()) TTF_Init();
float scale = (float) screen -> h / designHeight;
fontSmaller = TTF_OpenFont("resources/Roboto-Thin.ttf", larger(1, (int)(40 * scale)));
fontSmall = TTF_OpenFont("resources/Roboto-Thin.ttf", larger(1, (int)(48 * scale)));
fontSmallBold = TTF_OpenFont("resources/Roboto-Light.ttf", larger(1, (int)(53 * scale)));
fontLarge = TTF_OpenFont("resources/Roboto-Thin.ttf", larger(1, (int)(120 * scale)));
}
///Ha már nem kellenek, felszabadítja a betűtípusokat.
@ -20,10 +21,10 @@ void freeFonts() {
}
///Itt tárolom a használható karaktereket. Eldönti, hogy a bemenet köztük van-e.
bool isValidChar(Uint16 chr) {
bool isValidChar(wchar_t chr) {
if(('A' <= chr && chr <= 'Z') || ('a' <= chr && chr <= 'z') || ('0' <= chr && chr <= '9')) return true;
Uint16 validChars[] = L"öüóőúéáűíÖÜÓŐÚÉÁŰÍ|€ß$Łł¤<>#&@{}Đ[]đ+!%=×)(.- ";
wchar_t validChars[] = L"öüóőúéáűíÖÜÓŐÚÉÁŰÍ|€ß$Łł¤<>#&@{}Đ[]đ+!%=×)(.- ";
int i;
for(i = 0; validChars[i] != 0; i++) if(chr == validChars[i])
return true;
@ -57,14 +58,14 @@ Sint16 percentageToPx(int percentage, bool isWidth) {
* \param string vége
* \return string konkatenált
*/
Uint16* concatUnicode(const Uint16 a[], const Uint16 b[]) {
wchar_t* concatUnicode(const wchar_t a[], const wchar_t b[]) {
int aLength, bLength;
for(aLength = 0; a[aLength] != 0; aLength++)
;
for(bLength = 0; b[bLength] != 0; bLength++)
;
Uint16* newStr = (Uint16*) malloc((aLength + bLength - 1 + 100) * sizeof(Uint16));
wchar_t* newStr = (wchar_t*) malloc((aLength + bLength - 1 + 100) * sizeof(wchar_t));
int i = 0, ai, bi;
for(ai = 0; ai < aLength; ai++)
newStr[i++] = a[ai];
@ -76,9 +77,79 @@ Uint16* concatUnicode(const Uint16 a[], const Uint16 b[]) {
}
///A megjelenítendő szöveg közepét kell megadni x,y-ként. A szöveget Uint16*-ban, a betűtipust pedig a betűtipus enumból. Ugyan mindent x = 50-nel hívunk meg, ez később változhat, ezért hagyom meg paraméternek.
void unicodePrintCentered(Uint16 textToWrite[], int x, int y, fontType fontType) {
void unicodePrintCentered(wchar_t textToWrite[], int x, int y, fontType fontType) {
char utf8[2048];
wideToUtf8(textToWrite, utf8, sizeof(utf8));
utf8PrintCentered(utf8, x, y, fontType);
}
///Ugyanaz, mint az unicodePrintCentered, csak utf-8-as bemenettel.
void utf8PrintCentered(const char* textToWrite, int x, int y, fontType fontType) {
if(!isRenderingEnabled()) return;
TTF_Font* currentFont = selectFont(fontType);
SDL_FreeSurface(text);
text = TTF_RenderUNICODE_Blended(currentFont, textToWrite, (SDL_Color) {255, 255, 255});
text = TTF_RenderUTF8_Blended(currentFont, textToWrite, (SDL_Color) {255, 255, 255});
if(text == NULL) return;
SDL_BlitSurface(text, NULL, screen, &(SDL_Rect){percentageToPx(x, true) - text -> w / 2, percentageToPx(y, false) - text -> h / 2, 0, 0});
}
int wideToUtf8(const wchar_t* source, char* destination, int destinationSize) {
int written = 0;
for(; *source != 0; source++) {
unsigned int c = (unsigned int) *source;
int needed = c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
if(written + needed >= destinationSize) break;
switch(needed) {
case 1:
destination[written++] = c;
break;
case 2:
destination[written++] = 0xC0 | (c >> 6);
destination[written++] = 0x80 | (c & 0x3F);
break;
case 3:
destination[written++] = 0xE0 | (c >> 12);
destination[written++] = 0x80 | ((c >> 6) & 0x3F);
destination[written++] = 0x80 | (c & 0x3F);
break;
default:
destination[written++] = 0xF0 | (c >> 18);
destination[written++] = 0x80 | ((c >> 12) & 0x3F);
destination[written++] = 0x80 | ((c >> 6) & 0x3F);
destination[written++] = 0x80 | (c & 0x3F);
}
}
destination[written] = 0;
return written;
}
int utf8ToWide(const char* source, wchar_t* destination, int destinationSize) {
int written = 0;
while(*source != 0 && written < destinationSize - 1) {
unsigned char lead = (unsigned char) *source++;
unsigned int c;
int continuations;
if(lead < 0x80) { c = lead; continuations = 0; }
else if((lead & 0xE0) == 0xC0) { c = lead & 0x1F; continuations = 1; }
else if((lead & 0xF0) == 0xE0) { c = lead & 0x0F; continuations = 2; }
else if((lead & 0xF8) == 0xF0) { c = lead & 0x07; continuations = 3; }
else continue;
int i;
for(i = 0; i < continuations; i++) {
if((*source & 0xC0) != 0x80) break;
c = (c << 6) | (*source++ & 0x3F);
}
destination[written++] = (wchar_t) c;
}
destination[written] = 0;
return written;
}