在Microsoft Visual Studio C++中创建游戏窗口的问题求助
Hey there! Since you're only a month into C++ and stuck on SDL window creation, let's break this down step by step—no confusing jargon, just straightforward checks and fixes to get your code running.
First, let's make sure your basic SDL setup is correct. You mentioned having a Graphics class, so here's a minimal, working version you can use as a starting point:
Graphics.h
#pragma once #include <SDL.h> class Graphics { public: Graphics(); ~Graphics(); // Creates a game window and renderer bool initWindow(const char* windowTitle, int posX, int posY, int width, int height, Uint32 windowFlags); // Getters to access window/renderer (for drawing later) SDL_Window* getWindow() const; SDL_Renderer* getRenderer() const; private: SDL_Window* _gameWindow; SDL_Renderer* _gameRenderer; };
Graphics.cpp
#include "Graphics.h" #include <iostream> Graphics::Graphics() : _gameWindow(nullptr), _gameRenderer(nullptr) {} Graphics::~Graphics() { // Clean up SDL resources when the class is destroyed if (_gameRenderer != nullptr) { SDL_DestroyRenderer(_gameRenderer); } if (_gameWindow != nullptr) { SDL_DestroyWindow(_gameWindow); } SDL_Quit(); } bool Graphics::initWindow(const char* windowTitle, int posX, int posY, int width, int height, Uint32 windowFlags) { // Initialize SDL's video subsystem (required for windows) if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::cout << "SDL failed to initialize! Error: " << SDL_GetError() << std::endl; return false; } // Create the actual window _gameWindow = SDL_CreateWindow(windowTitle, posX, posY, width, height, windowFlags); if (_gameWindow == nullptr) { std::cout << "Window failed to create! Error: " << SDL_GetError() << std::endl; return false; } // Create a hardware-accelerated renderer (makes drawing faster) _gameRenderer = SDL_CreateRenderer(_gameWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (_gameRenderer == nullptr) { std::cout << "Renderer failed to create! Error: " << SDL_GetError() << std::endl; SDL_DestroyWindow(_gameWindow); _gameWindow = nullptr; return false; } return true; } SDL_Window* Graphics::getWindow() const { return _gameWindow; } SDL_Renderer* Graphics::getRenderer() const { return _gameRenderer; }
main.cpp
#include "Graphics.h" int main(int argc, char* argv[]) { Graphics gameGraphics; // Create an 800x600 window centered on the screen if (!gameGraphics.initWindow("My First SDL Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN)) { return 1; // Exit if window setup fails } // Main game loop (keeps the window open) bool isGameRunning = true; SDL_Event gameEvent; while (isGameRunning) { // Handle user input (like closing the window) while (SDL_PollEvent(&gameEvent)) { if (gameEvent.type == SDL_QUIT) { isGameRunning = false; } } // Clear the screen to black SDL_SetRenderDrawColor(gameGraphics.getRenderer(), 0, 0, 0, 255); SDL_RenderClear(gameGraphics.getRenderer()); // Draw a simple red rectangle to test rendering SDL_Rect testRect = { 150, 150, 200, 200 }; SDL_SetRenderDrawColor(gameGraphics.getRenderer(), 255, 0, 0, 255); SDL_RenderFillRect(gameGraphics.getRenderer(), &testRect); // Update the screen with what we drew SDL_RenderPresent(gameGraphics.getRenderer()); } return 0; }
Even if your code is right, incorrect project settings will break things. Here's what to verify (adjust based on your IDE—Visual Studio, Code::Blocks, etc.):
- Include Directories: Add the path to SDL's
includefolder (whereSDL.his stored) to your project's include paths. - Library Directories: Add the path to SDL's
libfolder (usex86for 32-bit builds,x64for 64-bit). - Linker Input:
- For Debug builds: Link against
SDL2d.libandSDL2main.lib(thedstands for debug) - For Release builds: Link against
SDL2.libandSDL2main.lib
- For Debug builds: Link against
- Copy SDL DLL to Output: Grab
SDL2.dllfrom SDL'slib/x86orx64folder and paste it into your project's Debug/Release output directory. If this is missing, your program will crash immediately when you try to run it.
If you still hit problems, check these common pitfalls:
- SDL_Init Fails: Always use
SDL_GetError()to get a specific error message—it'll tell you exactly what's wrong (e.g., missing DLL, wrong subsystem initialized). - Window Closes Immediately: Make sure you have a main loop like the one above. If your
mainfunction exits right after creating the window, it'll close instantly. - Linker Errors: "Unresolved external symbol" errors mean you didn't link the SDL libraries correctly. Double-check your linker settings and make sure your build architecture (32/64-bit) matches the SDL libraries you're using.
- Header Path Issues: If you get "SDL.h not found" errors, double-check your include directory settings and make sure you spelled the include path correctly.
Take it slow—since you're new to C++, start with this minimal working example, get it running, then build on it from there. If you hit specific error messages (like linker logs or SDL error outputs), share those details and we can narrow it down further!
内容的提问来源于stack exchange,提问作者Lachie Power

