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



好吧,所以我正在尝试加载窗口并使用功能在其上显示几个图像。窗口加载和我的错误不会显示出未能加载图像的情况,但是窗口只是白色。有什么想法可能是吗?这是我下面的代码。

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

}

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

最新更新