2017-05-25 1 views
1

に私は白いウィンドウを開くためにテストプログラムを作っ セグメンテーションフォールトがSDL_FillRect

C.

で、SDL2ライブラリを使用していますが、私はエラーがないにもかかわらず、機能SDL_FillRectとセグメンテーションフォールトを取得したり、私がそれを構築するときの警告。ここで

はコードです:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
#include <SDL2/SDL_ttf.h> 

static const int window_width = 1000; 
static const int window_height = 1000; 

int main(int argc, char* argv[]) 
{ 
    //Window 
    SDL_Window *window = NULL; 

    //Window Surface where things will be shown 
    SDL_Surface *surface = NULL; 

    //Inicializar SDL 
    if(SDL_Init(SDL_INIT_VIDEO) == -1) 
    { 
     printf("Failed to initialize SDL2. SDL Error: %s", SDL_GetError()); 
    } 
    else 
    { 
     window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_SHOWN); 
     if(window == NULL) 
      printf("Failed to create SDL2 window. SDL Error: %s", SDL_GetError()); 
     else 
     { 
      //Fill window with white color 
      SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); 
      //Update surface with new changes 
      SDL_UpdateWindowSurface(window); 
      //Wait before closing (parameter in miliseconds) 
      SDL_Delay(4000); 
     } 
    } 
    SDL_DestroyWindow(window); 
    SDL_Quit(); 

    return 0; 
} 
+0

あなたは**サーフェス**(** NULL **に設定されています)をSDL_FillRectの最初のパラメータが問題になる可能性があります。https://wiki.libsdl.org/SDL_FillRect – smcd

+0

Hmm、ok。どのように私はそれを渡す必要がありますか? –

+0

リンク内のサンプルコードに示すようにサーフェスを作成します。 ** SDL_Surface * surface = SDL_CreateRGBSurface(0、window_width、window_height、32、0、0、0、0); ** – smcd

答えて

1

surfaceは、SDLのwiki(https://wiki.libsdl.org/SDL_FillRect)から直接取得この例のコードはSDL_FillRect()

を呼び出す前 SDL_Surfaceを作成する方法を示しまだ NULL

あるので、あなたは、セグメンテーションフォールトを取得します

/* Declaring the surface. */ 
SDL_Surface *s; 

/* Creating the surface. */ 
s = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0); 

/* Filling the surface with red color. */ 
SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 255, 0, 0)); 
+0

助けてくれてありがとう、私はSDL_FillRectの前にSDL_GetWindowSurface関数を忘れてしまったことに気付きました。これとSDL_CreateRGBSurfaceとの違いはわかりません(後者は問題を完全には解決できませんでした:セグメンテーションを終了しましたが、サーフェスは表示されませんでしたが)。 誰かが興味がある場合に備えて、このチュートリアルに従いました: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php –

関連する問題