2014-03-12 22 views
0

好吧,所以我想加載一個窗口並使用函數在其上顯示幾個圖像。窗口加載和我的錯誤不顯示加載圖像失敗,但窗口只是保持白色。任何想法,爲什麼這可能是?這是我的代碼如下。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; 

}

回答

0

你是不是渲染什麼,你只加載一些紋理和結束程序。

+0

哦,當然。對不起,我沒有看到!謝謝,我會試試 – Peter

相關問題