简体   繁体   English

窗口无法加载-c ++ / SDL

[英]Window doesn't load — c++/SDL

I'm very new with SDL. 我对SDL非常陌生。 I'm trying to add an image to a window and I have followed this tutorial: http://lazyfoo.net/SDL_tutorials/lesson02/index.php 我正在尝试将图像添加到窗口,并且已经按照本教程进行操作: http : //lazyfoo.net/SDL_tutorials/lesson02/index.php

I'm doing all the code in xcode and when I run it the window won't load. 我正在执行xcode中的所有代码,当我运行它时,窗口将不会加载。 I just flashes up and then dissapear and I know I've done all the steps I should. 我只是闪烁然后消失,我知道我已经完成了所有应该做的步骤。 This is my code: 这是我的代码:

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image(std::string filename)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;

    loadedImage = SDL_LoadBMP( filename.c_str() );

    if (loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);

    }

    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, NULL, destination, &offset);
}



int main( int argc, char* args[] )
{
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    //Quit SDL
    SDL_Quit();

    if (SDL_Init(SDL_INIT_EVERYTHING)==-1)
    {
        return 1;
    }

    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    if (screen == NULL)
    {
        return 1;
    }

    SDL_WM_SetCaption("Hellow world!", NULL);

    message = load_image("images.bmp");
    background = load_image("images.bmp");

    apply_surface(0, 0, background, screen);

    apply_surface(180, 140, message, screen);

    return 0;

} }

You are prematurely invoking SDL_Quit() in the main function. 您在主函数中过早调用SDL_Quit() This function shuts down all SDL subsystems and should instead be called at the end of your program. 此函数将关闭所有SDL子系统,而应在程序结尾处调用它。

If you wish for the window to remain until you close it explicitly, add a loop such as the following: 如果希望保留该窗口直到显式关闭它,请添加如下所示的循环:

int main() {
  if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
    return 1;
  }

  screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
  if (!screen) {
    return 1;
  }

  bool running = true;

  SDL_Event event;
  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }
  }

  SDL_Quit();
  return 0;
}

You can set running to false when some event occurs, such as when the window is closed. 您可以在某些事件发生时(例如,关闭窗口时)将running设置为false。

you have put SDL_Quit(); 您已经放置了SDL_Quit(); at the very start of your program. 在程序的开始。 This is like putting return 0; 这就像把return 0; before all your code. 在所有代码之前。 It shuts down when it reads that line. 读取该行时它将关闭。 To avoid this you should make a loop that breaks when the 'x' at the top of the window it pressed. 为避免这种情况,您应该在按下窗口顶部的“ x”时使其中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM