SDL - 简单的DirectMedia Layer是一个跨平台的多媒体库,旨在通过OpenGL和2D视频帧缓冲提供对音频,键盘,鼠标,操纵杆,3D硬件的低级访问。它被MPEG播放软件,模拟器和许多流行的游戏使用,包括获奖的Linux端口“文明:呼唤权力”。不应与安全开发生命周期混淆。
我一直在尝试寻找 SDL 1.2 的完整文档。我无法使用 SDL 2 有几个原因,我现在不想讨论这些原因,但基本上,我找不到 SDL 1.2 的任何 wiki 备份...
当我用 C 编译并运行一个使用 SDL 库(版本 2)的简单应用程序时,窗口似乎打开了两次;首先我看到一个打开和关闭速度非常快的窗口,然后是窗口
基本上,我有一个使用 SDL 2.0 渲染的精灵,我可以使用 SDL_RenderCopyEx() 围绕纹理的中心原点顺时针旋转可变的量。我想根据...
我试图将任何数字符号合并到一个字符串中,然后将该字符串渲染到主窗口上: #包括 #包括 #包括 我正在尝试将任何数字符号合并到一个字符串中,然后将该字符串渲染到主窗口上: #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: %s\n", inputedString.c_str()); } printf("Key pressed: %s\n", 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; } 如果您想要我的设置,可以进入我在此处创建的存储库:https://github.com/grubbauer/MathOrDeath/tree/dev_0.0.2 我正在使用以下命令编译我的代码: clang++ src/main.cpp src/random.cpp -o "build\windows-x64\MathOrDeath_v.0.0.1-16.exe" -Iinclude -Llib -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -v 这是日志: PS D:\source\repos\MathOrDeath> mingw32-make debug Starting clean process... Sucess. Creating build directory... Sucess. Building main executable... clang++ src/main.cpp src/random.cpp -o "build\windows-x64\MathOrDeath_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\*" build\windows-x64 bin\sdl2-config bin\SDL2.dll bin\SDL2_image.dll bin\SDL2_mixer.dll bin\SDL2_ttf.dll 5 file(s) copied. xcopy "src\res" "build\windows-x64\res" /E /I /H /C /Y src\res\font\PressStart2P-Regular.ttf src\res\img\background\test-0001.png src\res\sfx\music\test.ogg 3 File(s) copied del build\windows-x64\sdl2-config Sucess. Build complete. Now launching executable cd build\windows-x64 && .\MathOrDeath_v.0.0.1-16.exe mingw32-make: *** [Makefile:51: debug-1] Error -1073741819 因此,我使用调试器运行代码,发现每一个刻度都调用 gFontTexture.loadFromText(inputedString, {255, 255, 255});。我纠正了我的错误,现在代码可以轻松运行。这是完整的代码: #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: %s\n", inputedString.c_str()); gFontTexture.loadFromText(inputedString, {255, 255, 255}); } printf("Key pressed: %s\n", 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.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; }
我在 Windows 11 上使用 SDL2 版本 2.28.3,该问题发生在所有后端(DirectX、opengl、软件)上,当我直接绘制到屏幕上时,与绘制 t 相比,我得到不同的结果...
我有这个代码: #包括 #include“SDL/SDL.h” SDL_Surface *屏幕= NULL; int main(int argc, char* args[]) { SDL_Init(SDL_INIT_EVERYTHING); 屏幕 = SDL_SetVideoMode(...
当我这样做(动态链接)时,一切都很好: $ gcc -o beta.bin ./game.c -lSDL -lSDLmain 但是当我尝试静态链接时: $ gcc -static -o beta.bin ./game.c -lSDL -SDLmain 存在链接器问题: /usr/
我一直在尝试更改 TTF 文本的颜色,但我不知道该怎么做。我有: SDL_Color color_text = { 0, 0, 0 }; //文字颜色 SDL_Surface * 表面 = TTF_RenderText_Solid(字体, ...
我正在尝试遵循 SDL 教程并编译使用 SDL 的 C++ 文件。我使用的是 Windows 10,最初使用的是 MinGW。 过了某个时刻,我意识到我的 MinGW 安装出现了问题......
我正在尝试在 SDL2 C++ 中创建一个体素空间引擎。我接下来的教程读取高度图图像的 X 和 Y 像素。但我找不到办法做到这一点。 我正在关注本教程:...
我尝试在带有 SDL_WINDOW_RESIZABLE 标志的窗口中创建和渲染大量纹理,但是当我调整窗口大小时,某些纹理有时会损坏。如果我是的话,我还没有注意到这个问题
我想知道如何从 C 函数返回对象数组。 这是一个例子: SDL_Vertex initTriangle (struct Point2D point1, struct Point2D point2, struct Point2D point3) { SDL_顶点
这个问题对于你们中的一些人来说可能非常愚蠢,但我对 C 编程很陌生。我目前正在用纯 C 创建一个 3D 引擎,我想知道如何从 C 返回对象数组
这个问题对你们中的一些人来说可能非常愚蠢,但我对 C 编程很陌生。我目前正在用纯 C 创建一个 3D 引擎,我想知道如何使用 C 函数返回一个对象。 他...
我正在尝试使用类的私有变量来制作SDL窗口。为了从私有变量创建一个窗口,我尝试使用 getter 和 setter。但是,我收到此错误: 优秀...
这个在SDL中画线的程序不起作用: #include“SDL.h” int main(int argc, char** argv) { SDL_Window* window = SDL_CreateWindow("一行", 10, 10, 1200, 600, fal...
使用 SDL 和 OpenGL 在 MacOS 上正确处理 HighDPI
我尝试在 MacOS 上为我的游戏《Bitfighter》进行高 DPI 渲染,结果总是看起来很糟糕,就像低分辨率游戏的放大版本。 游戏使用SDL2 + OpenGL并且我已经正确启用...
(源代码和问题行在底部) 我编写了一个简单的程序来将透明 PNG 加载到 SDL2 上。然而,它作为图像弹出,背景非常不稳定,不断闪烁。 我苏...
我使用 SLD2 箱来创建一个简单的物理模拟/引擎。我想使用“gfx”功能来获取帧速率和绘图基元。我明白我需要
尝试加载 OpenGL 4.1,但在 MacOS 上只能获取 OpenGL 2.1
我正在尝试在 Macbook Pro 14(M2 Pro CPU、Macos Sonoma 14.5)中设置基于 OpenGL 的项目。我基本上已经设法让一切正常工作,但无论我做什么,我都无法使程序...