I am trying to merge any numeric symbol into a string, and then render that string onto the main window:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <string>
#include "random.h"
// Window variables
int SCR_WIDTH = 0;
int SCR_HEIGHT = 0;
// General global variables
const std::string VERSION = "v.0.0.1-16";
std:: string inputedString;
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
Mix_Music *sMusic = NULL;
TTF_Font *fFont;
int Operator;
void initialise();
void loadAssets();
int getRandOperator();
void quit();
// Texture class
class cTexture {
public:
cTexture(); // Constructor
~cTexture(); // Destructor
void loadFromFile(std::string path);
void loadFromText(std::string text, SDL_Color color);
void free();
void render(int x, int y, int w, int h);
int getWidth();
int getHeight();
private:
SDL_Texture *mTexture;
int mHeight;
int mWidth;
};
// Class objects
cTexture gTestBackground;
cTexture gFontTexture;
cTexture::cTexture() {
mTexture = NULL;
mHeight = 0;
mWidth = 0;
}
cTexture::~cTexture() { free(); }
void cTexture::loadFromFile(std::string path) {
free();
SDL_Surface *oldSurface = IMG_Load(path.c_str());
if (oldSurface != NULL) {
mTexture = SDL_CreateTextureFromSurface(gRenderer, oldSurface);
mWidth = oldSurface->w;
mHeight = oldSurface->h;
SDL_FreeSurface(oldSurface);
}
}
void cTexture::loadFromText(std::string text, SDL_Color color) {
free();
SDL_Surface *textSurface = TTF_RenderText_Solid(fFont, text.c_str(), color);
mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
mWidth = textSurface->w;
mHeight = textSurface->h;
SDL_FreeSurface(textSurface);
}
void cTexture::free() {
if (mTexture != NULL) {
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void cTexture::render(int x, int y, int w, int h) {
SDL_Rect renderQuad = {x, y, w, h};
SDL_RenderCopy(gRenderer, mTexture, NULL, &renderQuad);
}
int cTexture::getWidth() { return mWidth; }
int cTexture::getHeight() { return mHeight; }
int WinMain(int argc, char *argv[]) {
initialise();
loadAssets();
bool stop = false;
SDL_Event e;
const char *pressedKey;
while (!stop) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
stop = true;
} else if (e.type == SDL_KEYDOWN) {
SDL_Keycode pressedKeyRaw =
e.key.keysym.sym; // I have absolutly no idea what this
// does but it kinda works (?)
if (pressedKeyRaw >= SDLK_0 && pressedKeyRaw <= SDLK_9) {
pressedKey = SDL_GetKeyName(pressedKeyRaw);
inputedString += pressedKey; // Accumulate the number as a string
printf("Current number: %sn", inputedString.c_str());
}
printf("Key pressed: %sn", pressedKey);
}
}
// Game logic
/*
Operator = randNum(1,4);
switch (Operator) {
case 1:
printf("+");
break;
case 2:
printf("-");
break;
case 3:
printf("*");
break;
case 4:
printf("/");
break;
}
*/
// Graphical rendering
SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
SDL_RenderClear(gRenderer);
gTestBackground.render(0, 0, SCR_WIDTH, SCR_HEIGHT);
gFontTexture.loadFromText(inputedString, {255, 255, 255});
gFontTexture.render(0, 0, 100, 100);
SDL_RenderPresent(gRenderer);
// Sounds
if (Mix_PlayingMusic() == 0) {
Mix_PlayMusic(sMusic, -1);
}
}
quit();
return 0;
}
void initialise() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
IMG_Init(IMG_INIT_PNG); // Currently only the png format is needed
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
TTF_Init();
// Get the current display mode of the primary display
SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode);
SCR_WIDTH = display_mode.w;
SCR_HEIGHT = display_mode.h;
gWindow = SDL_CreateWindow(("MathOrDeath " + VERSION).c_str(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCR_WIDTH, SCR_HEIGHT, SDL_WINDOW_SHOWN);
gRenderer = SDL_CreateRenderer(
gWindow, -1,
SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC); // Accelerated with VSync activated
SDL_SetWindowFullscreen(gWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
}
void loadAssets() {
// Graphical elements
gTestBackground.loadFromFile("res/img/background/test-0001.png");
// Sounds
sMusic = Mix_LoadMUS("res/sfx/music/test.ogg");
// Fonts
fFont = TTF_OpenFont("res/font/PressStart2P-Regular.ttf", 20);
gFontTexture.loadFromText(
"Als Gregor Samsa eines Morgens aus unruhigen Traeumen erwachte",
{0, 0, 0});
}
void quit() {
// Graphical elements
gTestBackground.free();
// Sounds
Mix_FreeMusic(sMusic);
// SDL Elements
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
/*
* Special attention: If IMG_Quit does NOT work when working with CMake,
* change #include "close_code.h" on line 2191 to #include <close_code.h>.
* This will typically resolve the issue.
*/
IMG_Quit();
Mix_Quit();
SDL_Quit();
Operator = NULL;
}
If you want my setup you can go into the repository I created here: https://github.com/grubbauer/MathOrDeath/tree/dev_0.0.2
I was compiling my code with this command:
clang++ src/main.cpp src/random.cpp -o "buildwindows-x64MathOrDeath_v.0.0.1-16.exe" -Iinclude -Llib -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -v
This was the log:
PS D:sourcereposMathOrDeath> mingw32-make debug
Starting clean process...
Sucess.
Creating build directory...
Sucess.
Building main executable...
clang++ src/main.cpp src/random.cpp -o "buildwindows-x64MathOrDeath_v.0.0.1-16.exe" -Iinclude -Llib -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -v
clang version 18.1.8
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/mingw64/bin
"C:/msys64/mingw64/bin/clang++.exe" -cc1 -triple x86_64-w64-windows-gnu -emit-obj -mrelax-all -dumpdir "build\windows-x64\MathOrDeath_v.0.0.1-16.exe-" -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -mms-bitfields -funwind-tables=2 -fno-use-init-array -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=D:/source/repos/MathOrDeath -v -fcoverage-compilation-dir=D:/source/repos/MathOrDeath -resource-dir C:/msys64/mingw64/lib/clang/18 -I include -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++ -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/backward -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0 -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/backward -internal-isystem C:/msys64/mingw64/include/c++/14.2.0 -internal-isystem C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/include/c++/14.2.0/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++ -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/backward -internal-isystem C:/msys64/mingw64/lib/clang/18/include -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/usr/include -internal-isystem C:/msys64/mingw64/include -fdeprecated-macro -ferror-limit 19 -fmessage-length=234 -femulated-tls -fno-use-cxa-atexit -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -exception-model=seh -fcolor-diagnostics -faddrsig -o C:/Users/Rapha/AppData/Local/Temp/main-f3b781.o -x c++ src/main.cpp
clang -cc1 version 18.1.8 based upon LLVM 18.1.8 default target x86_64-w64-windows-gnu
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/backward"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/backward"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/usr/include"
#include "..." search starts here:
#include <...> search starts here:
include
C:/msys64/mingw64/include/c++/14.2.0
C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32
C:/msys64/mingw64/include/c++/14.2.0/backward
C:/msys64/mingw64/lib/clang/18/include
C:/msys64/mingw64/include
End of search list.
"C:/msys64/mingw64/bin/clang++.exe" -cc1 -triple x86_64-w64-windows-gnu -emit-obj -mrelax-all -dumpdir "build\windows-x64\MathOrDeath_v.0.0.1-16.exe-" -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name random.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -mms-bitfields -funwind-tables=2 -fno-use-init-array -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=D:/source/repos/MathOrDeath -v -fcoverage-compilation-dir=D:/source/repos/MathOrDeath -resource-dir C:/msys64/mingw64/lib/clang/18 -I include -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++ -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/backward -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0 -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/backward -internal-isystem C:/msys64/mingw64/include/c++/14.2.0 -internal-isystem C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/include/c++/14.2.0/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++ -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/backward -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/x86_64-w64-mingw32 -internal-isystem C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/backward -internal-isystem C:/msys64/mingw64/lib/clang/18/include -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/include -internal-isystem C:/msys64/mingw64/x86_64-w64-mingw32/usr/include -internal-isystem C:/msys64/mingw64/include -fdeprecated-macro -ferror-limit 19 -fmessage-length=234 -femulated-tls -fno-use-cxa-atexit -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -exception-model=seh -fcolor-diagnostics -faddrsig -o C:/Users/Rapha/AppData/Local/Temp/random-3a4f93.o -x c++ src/random.cpp
clang -cc1 version 18.1.8 based upon LLVM 18.1.8 default target x86_64-w64-windows-gnu
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/backward"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include/c++/14.2.0/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2.0/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14.2/backward"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/x86_64-w64-mingw32"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/include/g++-v14/backward"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/include"
ignoring nonexistent directory "C:/msys64/mingw64/x86_64-w64-mingw32/usr/include"
#include "..." search starts here:
#include <...> search starts here:
include
C:/msys64/mingw64/include/c++/14.2.0
C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32
C:/msys64/mingw64/include/c++/14.2.0/backward
C:/msys64/mingw64/lib/clang/18/include
C:/msys64/mingw64/include
End of search list.
"C:/msys64/mingw64/bin/ld" -m i386pep -Bdynamic -o "build\windows-x64\MathOrDeath_v.0.0.1-16.exe" C:/msys64/mingw64/lib/crt2.o C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/crtbegin.o -Llib -LC:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0 -LC:/msys64/mingw64/x86_64-w64-mingw32/lib -LC:/msys64/mingw64/x86_64-w64-mingw32/mingw/lib -LC:/msys64/mingw64/lib C:/Users/Rapha/AppData/Local/Temp/main-f3b781.o C:/Users/Rapha/AppData/Local/Temp/random-3a4f93.o -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lkernel32 C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/14.2.0/crtend.o
Sucess
Copying resource files
copy "bin*" buildwindows-x64
binsdl2-config
binSDL2.dll
binSDL2_image.dll
binSDL2_mixer.dll
binSDL2_ttf.dll
5 file(s) copied.
xcopy "srcres" "buildwindows-x64res" /E /I /H /C /Y
srcresfontPressStart2P-Regular.ttf
srcresimgbackgroundtest-0001.png
srcressfxmusictest.ogg
3 File(s) copied
del buildwindows-x64sdl2-config
Sucess.
Build complete.
Now launching executable
cd buildwindows-x64 && .MathOrDeath_v.0.0.1-16.exe
mingw32-make: *** [Makefile:51: debug-1] Error -1073741819