Hi, I’m relatively new to SDL and I’ve been following LazyFoo’s guide. I tried programming it in C instead of C++ and it actually compiles without any problems but when I attempt to run it, it just throws me a seg fault. I saved it as C++ and it ran without any problems. What am I doing wrong?
#include<stdio.h>
#include<SDL2/SDL.h>
#define HEIGHT 720
#define WIDTH 1280
int initWindow(SDL_Window **window);
int loadMedia(SDL_Surface **surface);
void update(SDL_Window **window, SDL_Surface **image, SDL_Surface **surface);
void close(SDL_Window **window);
int main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Surface *surface, *image;
window = NULL;
surface = NULL;
if(SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Error: %s", SDL_GetError());
}else{
if(initWindow(&window) != 0){
printf("Error: %s", SDL_GetError());
}else{
surface = SDL_GetWindowSurface(window);
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF));
if(loadMedia(&image) != 0){
printf("Error: %s", SDL_GetError());
}else{
update(&window, &image, &surface);
}
}
}
}
int loadMedia(SDL_Surface **image)
{
*image = SDL_LoadBMP("assets/hello.bmp");
return (*image == NULL) ? 1: 0;
}
void update(SDL_Window **window, SDL_Surface **image, SDL_Surface **surface)
{
int isOver;
SDL_Event e;
isOver = 0;
while(!isOver){
while(SDL_PollEvent(&e) != 0){
if(e.type == SDL_QUIT){
isOver = 1;
}
}
SDL_BlitSurface(*image, NULL, *surface, NULL);
SDL_UpdateWindowSurface(*window);
}
close(window);
}
int initWindow(SDL_Window **window)
{
*window = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
return (*window == NULL) ? 1: 0;
}
void close(SDL_Window **window)
{
SDL_DestroyWindow(*window);
SDL_Quit();
}