编译SDL测试时出错

时间:2014-09-13 02:53:20

标签: c++ c sdl sdl-2

我正在学习使用SDL2进行编程。目前,我正在做一个基本的练习。我的程序加载背景图像,并在背景上加载完整的精灵表。但是我在执行程序时遇到了问题。当我执行二进制文件时,程序窗口会自动关闭;我看不到任何东西。我认为这是主循环的一个问题,但它看起来适合我。这是我的代码

#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();



}

1 个答案:

答案 0 :(得分:3)

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

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); 
  }