2016-02-02 42 views
0

我試圖讓SDL窗口出現,但似乎沒有工作。該程序將運行,並顯示窗口的功能將運行沒有錯誤,但沒有顯示在我的屏幕上。我在碼頭只有一個圖標,表示程序沒有響應。這裏是我的代碼:未出現SDL窗口

int main(int argc, const char * argv[]) { 

    MainComponent mainComponent; 
    mainComponent.init(); 

    char myVar; 

    cout << "Enter any key to quit..."; 
    cin >> myVar; 

    return 0; 
} 

void MainComponent::init() { 
    //Initialize SDL 
    SDL_Init(SDL_INIT_EVERYTHING); 

    window = SDL_CreateWindow("My Game Window", 100, 100, 100, 100, SDL_WINDOW_SHOWN); 

    cout << screenWidth << " " << screenHeight << endl; 

    if(window == nullptr) { 
     cout << "Error could not create window" << SDL_GetError() << endl; 
    } 

    SDL_Delay(5000); 

} 

這裏的圖標的屏幕截圖上了被告席https://www.dropbox.com/s/vc01iqp0z07zs25/Screenshot%202016-02-02%2017.26.44.png?dl=0 讓我知道如果有什麼我做錯了,謝謝!

回答

0

SDL_Renderer應該被初始化來處理渲染。這在這裏詳細解釋What is a SDL renderer?

下面是一個經過初始化的渲染器的修改代碼;

#include <SDL2/SDL.h> 
#include <iostream> 

using namespace std; 

class MainComponent 
{ 
public: 
    void init(); 
    ~MainComponent(); 

private: 
    SDL_Window *window; 
    SDL_Renderer* renderer; 
}; 

MainComponent::~MainComponent() 
{ 
    SDL_DestroyRenderer(renderer); 
    SDL_DestroyWindow(window); 
} 

void MainComponent::init() { 
    //Initialize SDL 
    SDL_Init(SDL_INIT_EVERYTHING); 

    int screenWidth = 400; 
    int screenHeight = 300; 

    window = SDL_CreateWindow("My Game Window", 100, 100, screenWidth, screenHeight, SDL_WINDOW_SHOWN); 
    renderer = SDL_CreateRenderer(window, -1, 0); 

    cout << screenWidth << " " << screenHeight << endl; 

    if(window == nullptr) { 
     cout << "Error could not create window" << SDL_GetError() << endl; 
    } 

    //change the background color 
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); 

    // Clear the entire screen to our selected color. 
    SDL_RenderClear(renderer); 

    // Up until now everything was drawn behind the scenes. 
    // This will show the new, red contents of the window. 
    SDL_RenderPresent(renderer); 


    SDL_Delay(5000); 

} 


int main(int argc, const char * argv[]) { 

    MainComponent mainComponent; 
    mainComponent.init(); 

    char myVar; 

    cout << "Enter any key to quit..."; 
    cin >> myVar; 

    SDL_Quit(); 
    return 0; 
} 

這應該編譯並正常運行。 希望有所幫助。