SDL测试编译期间出错

SDL测试编译期间出错

我正在学习使用SDL2进行编程。目前,我正在进行基本练习。我的程序加载背景图像,并在背景上加载完整的Sprite工作表。但是我在执行程序时遇到问题。当我执行二进制文件时,程序窗口会立即自动关闭;我什么都看不到。我认为这是主循环的问题,但对我来说似乎很合适。这是我的代码

#include "SDL.h"


void main () {

int gameover = 0;


SDL_Init(SDL_INIT_VIDEO);

SDL_Window* TheWindow;
SDL_Renderer* RenderEngine;
SDL_Surface* LoadedSurface;
SDL_Surface* LoadedImage;
SDL_Texture* CharacterImg;
SDL_Texture* BackgroundImg;

TheWindow = SDL_CreateWindow("Character Test",
    SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 580, SDL_WINDOW_RESIZABLE);

RenderEngine = SDL_CreateRenderer( TheWindow, -1, SDL_RENDERER_ACCELERATED);

LoadedSurface= SDL_LoadBMP("./maptest.bmp");
BackgroundImg = SDL_CreateTextureFromSurface( RenderEngine, LoadedSurface);
SDL_FreeSurface(LoadedSurface);

LoadedImage = SDL_LoadBMP("./sprite.bmp");
CharacterImg = SDL_CreateTextureFromSurface( RenderEngine, LoadedImage);
SDL_FreeSurface(LoadedImage);



SDL_Event event;

 while (!gameover)
{

  if (SDL_PollEvent(&event)) {

  switch (event.type) {

        case SDL_QUIT:
         gameover = 1;
        break;

        case SDL_KEYDOWN:
        switch (event.key.keysym.sym) {
        case SDLK_ESCAPE:
        case SDLK_q:
         gameover = 1;
                break;
            }
         }
      break;
      }

    SDL_RenderClear(RenderEngine);
    SDL_RenderCopy(RenderEngine, BackgroundImg, NULL, NULL);
    SDL_RenderCopy(RenderEngine, CharacterImg, 120, 80);
    SDL_RenderPresent(RenderEngine);


   }



    SDL_Quit();



}

最佳答案

如果发生任何while,您的代码将退出SDL_PollEvent -loop:

while (!gameover)
  {
    if (SDL_PollEvent(&event)) //Any SDL_PollEvent that gets into if...
      {
        switch (event.type)
          {
          case SDL_QUIT:
            gameover = 1;
            break;
          case SDL_KEYDOWN:
            switch (event.key.keysym.sym)
              {
              case SDLK_ESCAPE:
              case SDLK_q:
                gameover = 1;
                break;
              }
          }
        break;     //...will eventually hit this break if it didn't break before
      }
    SDL_RenderClear(RenderEngine);
    SDL_RenderCopy(RenderEngine, BackgroundImg, NULL, NULL);
    SDL_RenderCopy(RenderEngine, CharacterImg, 120, 80);
    SDL_RenderPresent(RenderEngine);
  }

关于c++ - SDL测试编译期间出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25819389/

10-11 01:04