Sign in to follow this  

SDL trouble with passing SDL_Surface*s i think?

This topic is 3406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

there are two programs i was fiddling with and i would see to be getting very similar errors, when i try to pass a SDL_Surface* in to a function in one and in the other i get the error when i try to pass it in to a method of a class i mad in the other. i decided to post this after i got the error in the one were i was passing it in to the function since the program so far is so simple i cant see what the hell could gone wrong. for now ill just hope the problems are becomes of the same thing. but here is the code and error for the simple program I,m sure I'm just over looking something but after dealing with the constant error of the more compacted program i need little push.
[
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_image.h"
#include <string>



const int SCREENW = 664;
const int SCREENH = 485;

SDL_Surface* screen= NULL;
SDL_Surface* back =NULL;
SDL_Surface* txt = NULL;



Uint8* key;
SDL_Event Event;

void DrawIMG(SDL_Surface* src, int offx, int offy){
SDL_Rect dest;;
dest.h = src->h;
dest.w = src->w;
dest.x = offx;
dest.y = offy;

SDL_BlitSurface(src,NULL,screen,&dest);
}
void initIMG(){
back = IMG_Load("chick.JPEG");
	
};

int main(int argc, char* args[]){
SDL_Init(SDL_INIT_EVERYTHING);
atexit(SDL_Quit);
TTF_Init();
atexit(TTF_Quit);
screen = SDL_SetVideoMode(SCREENW,SCREENH,32,SDL_ANYFORMAT);
key = SDL_GetKeyState(NULL);
initIMG();

DrawIMG(back,0,0);


SDL_Flip(screen);

SDL_Delay(3000);

return 0;
}




and the error :Unhandled exception at 0x010b36d1 in TTF TEST.exe: 0xC0000005: Access violation reading location 0x0000000c.

]
as a side note i believe my surfaces are coming up NULL, for some reason
[Edited by - DyanmiteDave on August 12, 2008 11:12:02 PM]

Share this post


Link to post
Share on other sites
after messing with it the problem was that the file really was a .JPG
but this mean my other problem in the other program is defiantly different.

the blit just does not show up if i try drawing it using the class, my code is kind of ugly right now from all the messing with it i have bin doing and my back up is from before i tried to make the class
[
// libary,h
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <stdio.h>


const int SCREENW = 800;
const int SCREENH = 600;

SDL_Surface* screen= NULL;
SDL_Surface* back =NULL;
SDL_Surface* Pblit= NULL;

int xpos= 0, ypos= 0;

SDL_Event Event;




#include "Player.h"


//player.h

class player {
private:
int HP;
int speed;

int fram;
SDL_Rect sour[4];

public:
int xoff;
int yoff;

player(){

xoff = 2;
yoff = 2;
speed = 1;
HP = 100;

void setframe();
fram=0;
};

~ player(){};

void controle(Uint8* key);
void Drawing(SDL_Surface* IMG,SDL_Surface* screen);
void cleanup(SDL_Surface* cleaning,SDL_Surface* screen);
void setframe();
void play(Uint8* key, SDL_Surface* screen, SDL_Surface* plays, SDL_Surface* backr );

};

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdio.h>
#include <ctime>
#include "Player.h"

//player.ccp
void player::controle(Uint8* key) {


if (key[SDLK_UP] == 1) {yoff -=speed;}
if (key[SDLK_DOWN] == 1) {yoff +=speed;}
if (key[SDLK_RIGHT] == 1) {xoff +=speed;
if (fram != 0 || fram!=1) fram =1;
if (fram = 0 ) fram++;
else fram = 0;}
if (key[SDLK_LEFT] == 1) {xoff -=speed;
if (fram != 2 || fram!=3) fram =2;
if (fram = 2 ) fram++;
else fram = 2;}

};


void player::Drawing(SDL_Surface* IMG, SDL_Surface* screen){


SDL_Rect dest;

dest.h = IMG->h;
dest.w =IMG->w;
dest.x = xoff;
dest.y = yoff;


SDL_BlitSurface(IMG,&sour[fram],screen,&dest);

};



void player::cleanup(SDL_Surface* cleaning, SDL_Surface* screen){
SDL_Rect Src;
SDL_Rect Dest;

Src.h = sour[fram].h+ 4;
Src.w = sour[fram].w +4;

Dest.x =Src.x = xoff -2;
Dest.y =Src.y =yoff -2;

SDL_BlitSurface(cleaning,&Src,screen,&Dest);
};




void player::setframe(){

sour[0].x =0;
sour[0].y =0;
sour[0].w= 80;
sour[0].h= 60;

sour[1].x =80;
sour[1].y =0;
sour[1].w= 80;
sour[1].h= 60;

sour[2].x =0;
sour[2].y =60;
sour[2].w= 80;
sour[2].h= 60;

sour[3].x =80;
sour[3].y =60;
sour[3].w= 80;
sour[3].h= 60;

};





void player::play(Uint8* key, SDL_Surface* screen,SDL_Surface* plays,SDL_Surface* backr ){

controle(key);
cleanup(backr,screen);
Drawing(plays, screen);

};

//main
#include "library.h"


void initImages(){
back = IMG_Load("backround1.PNG");
Pblit =IMG_Load("blipSH.PNG");

}

void Drawimg(SDL_Surface *img, int x, int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img,NULL,screen,&dest);
}
void cleanSurf(SDL_Surface* cleening, SDL_Surface* sloppy,int x , int y ){
SDL_Rect Src;
SDL_Rect Dest;

Src.h = sloppy->h + 4;
Src.w = sloppy->w +4;

Dest.x =Src.x = x -2;
Dest.y =Src.y =y -2;

SDL_BlitSurface(cleening,&Src,screen,&Dest);
}
void updategame(){

cleanSurf(back,Pblit,xpos,ypos);

Drawimg(Pblit,xpos,ypos);

}




int main(int argc, char* argv[]){
SDL_Init(SDL_INIT_EVERYTHING);
atexit(SDL_Quit);
TTF_Init();
atexit(TTF_Quit);

screen = SDL_SetVideoMode(SCREENW,SCREENH,0,SDL_ANYFORMAT);
SDL_WM_SetCaption("Super Blip Advencher", NULL);
initImages();

Uint8* key;// make verable to store the key states
key = SDL_GetKeyState(NULL);// have the verable hold all the keys 1 is keu down 0 is up

Drawimg(back,0,0);

//Drawimg(Pblit,xpos,ypos);


player dave;
dave.Drawing(Pblit,screen);

SDL_Flip(screen);
SDL_Delay(3000);
for(;;){
SDL_PollEvent(&Event);
if (Event.type == SDL_QUIT){break;}

/* with no direct key reading makes the sprite move lagy or uncontrolably
if (Event.type == SDL_KEYDOWN){
if (Event.key.keysym.sym == SDLK_ESCAPE) {break;}
if (Event.key.keysym.sym == SDLK_UP){ypos -=1;}
if (Event.key.keysym.sym == SDLK_DOWN){ypos +=1;}
if (Event.key.keysym.sym == SDLK_RIGHT){xpos +=1;}
if (Event.key.keysym.sym == SDLK_LEFT){xpos -=1;}
}

*/

if (key[SDLK_ESCAPE] == 1) {break;}
if (key[SDLK_UP] == 1) {dave.yoff -=1;}
if (key[SDLK_DOWN] == 1) {dave.yoff +=1;}
if (key[SDLK_RIGHT] == 1) {dave.xoff +=1;}
if (key[SDLK_LEFT] == 1) {dave.xoff -=1;}


dave.Drawing(Pblit,screen);




if (ypos > SCREENH-25){ypos = SCREENH-25;}
if (ypos < 0){ypos = 0;}
if (xpos <0){xpos =0;}
if (xpos >SCREENW-40){xpos =SCREENW-40;}



SDL_Delay(1);// change the speed it moves
//updategame();

SDL_Flip(screen);
}
SDL_Delay(1000);
return 0;
}
]


i got the image to move before i tried implementing classes. don't scold me for rocky mistakes most of what i know is self taught.


[Edited by - DyanmiteDave on August 12, 2008 11:49:34 PM]

Share this post


Link to post
Share on other sites
Please enclose your code in source tags to make it readable - see the FAQ for details on how to do this.

You must check the return values of your calls to see if you get the right values. eg. IMG_Load could return null. You must check all functions like this.

Share this post


Link to post
Share on other sites
well hear is a much cleaner code that is is turning up null when passed in to the function and i have no idea why its not working right now.


[#include "SDL\SDL.h"
#include "SDL\SDL_image.h"
//#include "SDL\SDL_mixer.h"
#define left 0
#define right 1
Uint8* key ;
SDL_Event Event;

class Cplayer{

int pHeight; // 80
int pWidth; //80//

public:
int xoff;
int yoff;
int frame;
SDL_Rect clipl[2];
SDL_Rect clipr[2];
int speed;
int dir;
int fram;
Cplayer(){
yoff = 0;
xoff = 0;
frame = 0;
speed = 1;
pHeight= 60;
pWidth= 50;
setframe();
fram = 0;
dir = right;
};
void handle_event();//
void setframe();

void move();// change off set based on input
void draw(SDL_Surface* guy,SDL_Surface* screen,SDL_Surface* back);// blit and cleen all in one funtons... i hope
};




void initSDL()// add initulizatons
{
SDL_Init(SDL_INIT_EVERYTHING);
atexit(SDL_Quit);

};
/*
void Loadimage()// add any images that need loading to this list
{
back = IMG_Load("backround1.PNG");
player = IMG_Load("BlipSH.PMG");

};
*/


int main(int argc, char* argv[]){
initSDL();
SDL_Surface* screen;
SDL_Surface* back;
SDL_Surface* playerIMG;// keep surfes out of classes since more then one thing may use then
back = IMG_Load("backround1.PNG");
if (back = NULL)exit(1);
playerIMG = IMG_Load("BlipSH.PMG");
if (playerIMG = NULL)exit(1);


SDL_PumpEvents();
key = SDL_GetKeyState(NULL);//
int Sheight = 480;
int Swidth = 640;

screen = SDL_SetVideoMode(Swidth,Sheight,32,SDL_ANYFORMAT);
Cplayer player;
player.draw(playerIMG,screen,back);

SDL_Flip(screen);
SDL_Delay(2000);
for(;;){
SDL_PollEvent(&Event);
if (Event.type == SDL_QUIT){break;}

player.move();
player.draw(playerIMG,screen,back);

SDL_Flip(screen);
}



//SDL_Delay(4000);
return 0;
}


void Cplayer::move(){
if (key[SDLK_LEFT] ==1)
{dir = left;
xoff -= speed;}

if (key[SDLK_UP] ==1)
{yoff -= speed;}
if (key[SDLK_DOWN] ==1)
{yoff += speed;}
if (key[SDLK_RIGHT] ==1)
{dir = right;
xoff += speed;}

};

void Cplayer::draw(SDL_Surface *guy, SDL_Surface *screen, SDL_Surface *back){
SDL_Rect temp;
temp.x = xoff;
temp.y =yoff;
temp.h = pHeight;
temp.w = pWidth;

if(guy== NULL){
exit(1);}// see if it makes it to the functon
if(back== NULL){
exit(1);}// see if it makes it to the functon

if(fram <1){fram =0;}

if(dir == left){
SDL_BlitSurface(guy,&clipl[fram],screen,&temp);
fram++;

}
if(dir == right){
SDL_BlitSurface(guy,&clipr[fram],screen,&temp);
fram++;

}


};



void Cplayer::setframe(){


clipr[0].x =0;
clipr[0].y =0;
clipr[0].h =pHeight;
clipr[0].w =pWidth;

clipr[1].x = pWidth * 2;
clipr[1].y =0;
clipr[1].h =pHeight;
clipr[1].w =pWidth;

clipl[0].x =0;
clipl[0].y =pHeight * 2;
clipl[0].h =pHeight;
clipl[0].w =pWidth;

clipl[1].x =pWidth * 2;
clipl[1].y =pHeight * 2;
clipl[1].h =pHeight;
clipl[1].w =pWidth;


};

]

Share this post


Link to post
Share on other sites
if (back = NULL)exit(1);


Should be:

if (back == NULL)exit(1);


Same goes for the test with playerIMG.

EDIT: Actually the compiler should warn you about this. I suggest that you set your compiler's warning level to be as high as possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
if (back = NULL)exit(1);


Should be:

if (back == NULL)exit(1);


Same goes for the test with playerIMG.

EDIT: Actually the compiler should warn you about this. I suggest that you set your compiler's warning level to be as high as possible.


That makes me so angry, i always do that and it takes me forever to spot the error, i spose i should actually read the warnings more often^^

Share this post


Link to post
Share on other sites
yeah i fixed it and thanks i feel so dumb things are working now it was a miss named image i had PMG not PNG but thats fixed i hope i have this doing what i want it to do soon but i should be well on my way

Share this post


Link to post
Share on other sites
One other way to reduce this type of errors is, if you are making a comparison with a constant (such as NULL), put it on the left hand side of the comparison. That is, instead of this:

if (ptr == NULL) ...

Do this:

if (NULL == ptr) ...

Share this post


Link to post
Share on other sites

This topic is 3406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this