
Originally Posted by
WoodSTokk
I'm missing the init-function for SDL in your code.
Your code should have the following fuctions as a mimimum.
Code:
…
SDL_Window* gWindow = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
…
gWindow = SDL_CreateWindow( "SDL Tutorial", 0, 0, 1280, 720, SDL_WINDOW_BORDERLESS );
…
if( gWindow == NULL ) // segmentation fault with gcc and not with g++
…
//Destroy window
SDL_DestroyWindow( gWindow );
//Quit SDL subsystems
SDL_Quit();
…
I only posted what seems relevant, as I know where the segmentation fault occur. Here it is in it's entirety, which is nothing really but at standard test case you can get from about any tutorial on the subject:
Code:
#include "SDL2/SDL.h"
#include <stdio.h>
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
const int SCREEN_WIDTH = 1920;
const int SCREEN_HEIGHT = 1080;
int init()
{
//Initialization flag
int success = 1;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = 0;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_BORDERLESS );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = 0;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow ); // SEGMENTATION FAULT!
}
}
return success;
}
int loadMedia()
{
//Loading success flag
int success = 1;
//Load splash image
gHelloWorld = SDL_LoadBMP( "img.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
success = 0;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Free resources and close SDL
close();
return 0;
}
I really wish I could delete this thread or at least edit it, as, once again, I've confused my self and others by posting the wrong stuff.
Where the segmentation fault occur is at the line:
"gScreenSurface = SDL_GetWindowSurface( gWindow );"
now proven with finality, but as I mentioned earlier, it does not occur when compiling with g++, as opposed to gcc, which is very confusing.