2017-08-11 361 views
1

我一直試圖在Visual Studio 2017中創建一個簡單的C++遊戲,但我甚至無法獲得簡單的黑屏。窗口出現白色且無響應,是否有人能夠提供幫助?我一直在從Udemy的免費課程中學習,直到現在它一直在努力。我的代碼如下。Visual Studio 2017空白窗口,無響應

#include <iostream> 
#include <SDL.h> 
#include <string> 
using namespace std; 
int main(int argc, char* argv[]) { 
    const int screenWidth = 800; 
    const int screenLength = 600; 
    if (SDL_Init(SDL_INIT_VIDEO) < 0) { 
     cout << "SDL init faliure" << endl; 
     return 0; 

    } 
    SDL_Window *window = SDL_CreateWindow("Particle Fire", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenLength, SDL_WINDOW_SHOWN); 
    SDL_Delay(100000); 

    if (window == NULL) { 
     SDL_Quit(); 
     return 2; 
    } 
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC); 
    SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, screenWidth, screenLength); 

     if (renderer == NULL) { 
      cout << "Could not produce renderer"; 
      SDL_DestroyWindow(window); 
       SDL_Quit(); 
      return 3; 

     } 
    if (texture == NULL) { 
     cout << "Could not produce texture"; 
     SDL_DestroyRenderer(renderer); 
     SDL_DestroyWindow(window); 
      SDL_Quit(); 
     return 3; 
    } 
    Uint32 *buffer = new Uint32[screenWidth*screenLength]; 
    memset(buffer, 0xFF, screenWidth*screenLength*sizeof(Uint32)); 
    for (int i=0; i < screenWidth*screenLength; i++) { 

     buffer[i = 0xFFFF0000]; 
    } 
    SDL_UpdateTexture(texture, NULL, buffer, screenWidth * sizeof(Uint32)); 
    SDL_RenderClear(renderer); 
    SDL_RenderCopy(renderer, texture, NULL, NULL); 
    SDL_RenderPresent(renderer); 
    bool quit = false; 
    SDL_UpdateTexture(texture, NULL, buffer, screenWidth * sizeof(Uint32)); 
     SDL_RenderClear(renderer); 
     SDL_RenderCopy(renderer,texture , NULL, NULL); 
     SDL_RenderPresent(renderer); 
    SDL_Event event; 
    while (!quit) { 

     while (SDL_PollEvent(&event)) { 
      if (event.type == SDL_QUIT) { 
       quit = true; 
      } } 

    } 
    delete buffer; 
    SDL_DestroyTexture(texture); 
     SDL_DestroyRenderer(renderer); 
    SDL_DestroyWindow(window); 
    SDL_Quit(); 
} 

我試過改變一切,但它不起作用。

+0

我不知道sdl,所以我很好奇:你真的需要所有的代碼才能獲得黑屏嗎?如果確實如此,我肯定會使用其他的東西 – user463035818

+0

@ tobi303這不是SDL,幾乎所有C++庫都是如此。 – cdo256

+0

@ tobi303 SDL的意思是低層次的,所以這是很正常的,它需要很多代碼 – litelite

回答

3

在創建窗口SDL_Delay(100000);後立即有100秒延遲。

此外,您需要將buffer[i = 0xFFFF0000]更改爲buffer[i] = 0xFFFF0000。第一個僅設置i並保持緩衝區不變。第二個使像素變成黃色。