[SDL] SDL_Flip() is causing an error

Started by
1 comment, last by WaleedH95 12 years, 8 months ago
Ok, so I just started SDL yesterday or before yesterday, either way I'm using Microsoft Visual Studio 2010, and I'm following the tutorial on http://www.lazyfoo.net/, and when I build my program I get this error

Unhandled exception at 0x6812a21b in SDL.exe: 0xC0000005: Access violation reading location 0x00000004.[/quote]

and I know that SDL_Flip() is causing this because there's a green arrow pointed at it when I build it.
Advertisement
One of your pointers is null. This probably means that you aren't checking for errors in one of your SDL calls, or you are ignoring the error if you do check it.

Can you post your code?
Um sure, I don't have any comments on it though so it's gonna be a bit clustered, sorry, and I checked but I couldn't find the null pointers.
Um sure, I don't have any comments on it though so it's gonna be a bit clustered, sorry, and I checked but I couldn't find the null pointers.

Headers:

#include "SDL.h"
#include "SDL_image.h"
#include <string>

using namespace std;

int SCREEN_WIDTH = 640;
int SCREEN_HEIGHT = 480;
int SCREEN_BPP = 32;

SDL_Surface*screen = NULL;
SDL_Surface*background = NULL;
SDL_Surface*foo = NULL;

SDL_Event event;[/quote]

Functions:

SDL_Surface*load_image(string filename){
SDL_Surface*loadedImage = NULL;
SDL_Surface*optimizedImage = NULL;

loadedImage = IMG_Load(filename.c_str());
if(loadedImage != NULL){
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}[/quote]

void apply_surface(int x, int y, SDL_Surface*source, SDL_Surface*destination){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,NULL,destination,&offset);
}[/quote]

bool init(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
return 1;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);

if(screen = NULL){
return false;
}

SDL_WM_SetCaption("Foo says hi!", NULL);

return true;
}[/quote]



bool load_files(){
background = load_image("background.png");
foo = load_image("foo.png");

if(background = NULL){
return false;
}
if(foo = NULL){
return false;
}

return true;
}[/quote]



void clean_up(){
SDL_FreeSurface(background);
SDL_FreeSurface(foo);

SDL_Quit();
}[/quote]

Main:


int main(int argc,char*args[]){
bool quit = false;

if(init() == false){
return true;
}

if(load_files() == -1){
return 1;
}

apply_surface(0,0,background,screen);
apply_surface(280,140,foo,screen);

SDL_Flip(screen);
SDL_GetError();

SDL_Delay(2000);

while(quit = false){
while(SDL_PollEvent(&event)){
if(event.type = SDL_QUIT){
quit = true;
}
}
}
clean_up();
return 0;
}[/quote]
Figured it out, I just forgot to put two = signs in each if statement, thanks for replying.

This topic is closed to new replies.

Advertisement