SDL 2.0问题 - 尝试加载图像,但只获得一个白盒子

时间:2014-03-12 16:54:18

标签: image window sdl

好的,所以我试图加载一个窗口并使用一个函数在其上显示几个图像。窗口加载并且我的错误不会显示加载图像失败,但窗口只是保持白色。任何想法为什么会这样?这是我的代码。

 #include "stdafx.h"
 #include "SDL.h"
 #include <iostream>
 #include <string>

 using namespace std;

 const int Window_Width = 640;
 const int Window_Height = 480;

 SDL_Window *window = NULL;
 SDL_Renderer *render = NULL;

 SDL_Texture* loadImage(string imagename) //function that loads the image, useful for handling multiple image imports

 {

SDL_Surface* loadedImage = NULL;
SDL_Texture* texture = NULL;

loadedImage = SDL_LoadBMP(imagename.c_str()); //loads the image with the passed file name

if (loadedImage == NULL) //checks for any errors loading the image
{
    cout<<"The image failed to load.."<<endl;
}

texture = SDL_CreateTextureFromSurface(render, loadedImage);
SDL_FreeSurface(loadedImage);

return texture;
 }


 int main(int argc, char** argv)
 {

  if (SDL_Init(SDL_INIT_EVERYTHING) == -1)

  {       
    cout << SDL_GetError() << endl;
    return 1;
  }

  window = SDL_CreateWindow("Frogger", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Window_Width, Window_Height, SDL_WINDOW_SHOWN); 
  //creates a window in the centre of the screen, it uses const int's to define the size of the window

  if (window == NULL)
  {
    cout << SDL_GetError()<<endl;
    return 1;
  }  

  render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  //this renders the window

  if (render == NULL)
  {
    cout << SDL_GetError()<<endl;
    return 1;
  }

  //loading the images using the function
  SDL_Texture* background = NULL;
  SDL_Texture* frog = NULL;

  background = loadImage("background.bmp");
  frog = loadImage("frogger.bmp");

  SDL_Delay(2000);

  SDL_RenderClear(render);

  SDL_RenderPresent(render);

  SDL_UpdateWindowSurface(window);

  //freeing the memory back up
  SDL_DestroyRenderer(render);
  SDL_DestroyWindow(window);
  SDL_DestroyTexture(background);
  SDL_DestroyTexture(frog);

  SDL_Quit();

return 0;

}

1 个答案:

答案 0 :(得分:0)

你没有渲染任何东西,只需加载一些纹理并结束程序。