与编程计算机游戏和交互式图形/视频直接相关的问题。涉及游戏逻辑或一般游戏开发的问题(这里是偏离主题的)应该在https://gamedev.stackexchange.com/上提出。
为什么 Camera2D(或视口)在 Godot 4 中不移动?
我创建了游戏的主关卡场景,它一直在工作,直到我处理标题场景和过渡。我有一个带有简单脚本的 Camera2D 节点: 扩展Camera2D @export var desCentrar ...
我试图将任何数字符号合并到一个字符串中,然后将该字符串渲染到主窗口上: #包括 #包括 #包括 我正在尝试将任何数字符号合并到一个字符串中,然后将该字符串渲染到主窗口上: #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; }
移动平台 这是我在游戏中使用的跳跃平台。如果我跳上它,角色就无法再次站在上面,因此玩家会从平台上掉下来。 我的代码: ... move_dir = right_key - left...
获取索引“大小”无效(基于:“RigidBody3D (asteroid_shape_generator.gd)”)
我是 Godot 新手,我一直在尝试为我的游戏创建一个小行星生成器。 我已经设法让我的代码在运行游戏时在主场景上运行,但是我想要...
将 javascript 集成到 Mailchimp HTML Builder 中
我想将 dot.vu 中的游戏化代码嵌入到 Mailchimp HTML 构建器中,该代码以 javascript 形式存在。但根据我的阅读,Mailchimp HTML 构建器不支持 javascript。我尝试嵌入它并且...
xh使用正则表达式查找 headerr 的实例,然后使用一些规范编辑下面的行
所以我有下面 .msg 文件的摘录。我想要做的是对于所有 [sel xxx xxx] 标题找到它们,然后阅读它们下面的行。如果任何答案包含 (+3) 或任何 (+x),则
使用函数来操作 Cell<T> 是 Rust 中的代码味道吗?
我花了一些时间用 Rust 开发一个新项目。 我无法理解我是否以正确的方式使用单元。 这是我目前的问题,很普遍。 我需要访问一个结构...
如何检查 collider2d 是否与复合 collider 2d 重叠
有OnTriggerEnter2D函数,正确触发 private void OnTriggerEnter2D(Collider2D other) { if (!assailables.Contains(other.transform) && other.GetComponent 有OnTriggerEnter2D函数,正确触发 private void OnTriggerEnter2D(Collider2D other) { if (!assailables.Contains(other.transform) && other.GetComponent<IAssailable>()!=null) { Debug.Log(other.transform.position); assailables.Add(other.transform); } } 但是当我尝试使用 Collider2D.overlapCollider 在更新函数中再次验证它时,它将返回 0。 CompositeCollider2D groundArea = GetComponent<CompositeCollider2D>(); Debug.Log(groundArea.OverlapCollider(new ContactFilter2D(), new Collider2D[] {assailables[i].GetComponent<Collider2D>() })); 在此输入图片描述 不知道为什么调用OverlapCollider时可以触发OnTriggerEnter2D却返回0,对吗? compositeCollider2D 是动态的,所以我想再次检查它,并在它不在compositeCollider2D 中时将其从可攻击对象中删除。 Unity 默认情况下在执行物理查询时忽略触发器(即 Is Trigger 设置为 true 的碰撞器)。您需要明确要求它检查触发器: CompositeCollider2D groundArea = GetComponent<CompositeCollider2D>(); Debug.Log(groundArea.OverlapCollider(new ContactFilter2D() { useTriggers = true // <-- notice the added line here }, new Collider2D[] {assailables[i].GetComponent<Collider2D>() })); 参见 ContactFilter2D.useTriggers。
我需要一个代码,可以在最小值和最大值(例如 50-70)之间移动相机视野 (FOV),以放大和缩小相机视图 为此,我尝试了 Mathf.MoveTowards 函数,它运行良好:
我正在制作 Connect 4 游戏。目标是以相同的方式对齐四个项目。 我有一个双数组:Player[6][7],由在该插槽上玩的玩家填充。 我正在为胜利而努力
在我按下重新启动按钮后,游戏重新启动,但它不会产生目标 当我第一次启动游戏时它可以工作,但重新启动后它不会生成目标,光标也应该消失并且你...
属性说明符 BlueprintGetter 和 BlueprintSetter 的作用是什么?
私人: // UPROPERTY(BlueprintGetter=GetHealth, BlueprintSetter=SetHealth) ?????? int32 健康; 民众: UFUNCTION(BlueprintPure) int32 GetHealth() const { 返回健康状况; } UFUNC...
我正在制作一个基本的文字游戏。 我试图让玩家输入他们想要做什么以及他们想要做什么。 如果玩家想要拿起铲子,我想检查...
我制作了一个不和谐机器人,我一直在尝试将其连接到我的 gportal 游戏服务器。 没有公共 api,所以 iv 尝试使用 puppeteer 和 selenium 来实现,(selenium 效果最好) 用这个方法...
为什么当 alpha 为 0 时我的对象仍然可见,但只是有时
我正在尝试让一堆对象淡入淡出并移动,以创建一个移动到位的用户界面。 当我以 0 的 alpha 值开始代码并运行代码时,alpha 值返回到 0,但对象...
我正在尝试使用 Steam 示例 SpaceWar 提供的游戏来编写游戏。最近我重命名了 Visual Studio 解决方案,现在我总是收到消息“Steam 必须运行才能玩这个游戏&qu...
我无法在socket.io房间中进行基于回合的逻辑,我可以让他们进行通信,但我不知道如何分配角色并让玩家等待直到其他玩家完成他们的回合 这是
我的项目有一个箱子的资产。我复制了它并在整个地图上与不同的物品一起使用。但当我测试游戏时,副本不在我放置的位置。事实上,它引起了我的注意,...
Libgdx render() 方法在上一次调用完成之前被调用两次
公共类 SplashScreen 扩展 AbstractScreen { 私有浮点数倒计时; 私有最终 GameManager 游戏管理器; 布尔更新= false; 公共闪屏(主要主要){
为什么part:GetTouchingParts()在没有部件接触时返回部件
我正在尝试调试这段代码 本地鸟 = script.Parent 鸟.锚定 = true 本地 uis = 游戏.UserInputService 本地玩家 = game.Players.LocalPlayer 局部重力 = -1 当地零件 = 鸟: