You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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.

Step 1: Verify Your Core SDL Window Code

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;
}
Step 2: Double-Check Your Project Settings

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 include folder (where SDL.h is stored) to your project's include paths.
  • Library Directories: Add the path to SDL's lib folder (use x86 for 32-bit builds, x64 for 64-bit).
  • Linker Input:
    • For Debug builds: Link against SDL2d.lib and SDL2main.lib (the d stands for debug)
    • For Release builds: Link against SDL2.lib and SDL2main.lib
  • Copy SDL DLL to Output: Grab SDL2.dll from SDL's lib/x86 or x64 folder 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.
Step 3: Troubleshoot Common Issues

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 main function 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:34:59