Jump to content
  • Advertisement
Sign in to follow this  
Joshnathan

Dev-C++ Debug error

This topic is 4949 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

Hi all, I am using the default dev-c++ debugger and get this error: "An Acces violation(segment fault) has occured in your program" Anyone knows what it could be? Thanks in advance, Joshua

Share this post


Link to post
Share on other sites
Advertisement
I think there could be a few reasons for this. If a segmentation fault occurs then check your code and make sure you've eliminated all potential bugs. A segmentation fault occurs when your program tries to access memory locations that haven't been allocated for the program's use. If still no solution, then you could e-mail me your code or the part where you think there error is and I can try to help you fix it.

edit: one example would be if you used: scanf("%d",number);
An error would occur because scanf() passes the address of the variable, not the value. Therefore, you'd need the ampersand (&) before it.

Share this post


Link to post
Share on other sites
This is my code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

using namespace std;

struct CSpriteFrame
{
SDL_Surface *image;
int pause;
};

class CSpriteBase
{
public:
int init(char *dir);
CSpriteFrame *mAnim;
int mBuilt, mNumframes, mW, mH;
};

int CSpriteBase::init(char *dir)
{
char buffer[255];
char filename[255];
char name[255];
int pause = 0, r = 0, g = 0, b = 0;
FILE *file;

sprintf(filename, "%s/info", dir);

if((file=fopen(filename, "r")) == NULL) //fopen(filename, "r")
{
cout<< "error opening the file \n\n" << filename;
system("pause");
return -1;
}
fgets(buffer, 255, file);
sscanf(buffer, "FILES: %d", &mNumframes);
mAnim = new CSpriteFrame[mNumframes];
int mBuilt = 1;
int count = 0;

while(!feof(file) && count < mNumframes)
{
fgets(buffer, 255, file);
if(buffer[0] != '#' && buffer[0] != 'r' && buffer[0] != '\0' && buffer[0] != '\n' && strlen(buffer) != 0)
{
sscanf(buffer, "%s %d %d %d %d", &name, &pause, &r, &g, &b);
sprintf(filename, "%s/%s", dir, name);

SDL_Surface *temp;
if((temp = SDL_LoadBMP(filename)) == NULL)
{
system("pause");
return -1;
}
if(r >= 0)
{
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b));
}
mAnim[count].image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
mAnim[count].pause = pause;
if(!mW) mW = mAnim[count].image->w;
if(!mH) mH = mAnim[count].image->h;

count++;
}
}
fclose(file); //fclose(file);
return 0;
}

class CSprite
{
public:
int init(CSpriteBase *base, SDL_Surface *screen);
void draw();
void clearBG();
void updateBG();

void setFrame(int nr) {mFrame = nr;}
int getFrame() {return mFrame;}
void setSpeed(float nr) {mSpeed = nr;}
float getSpeed() {return mSpeed;}
void toggleAnim() { mAnimating = !mAnimating; }
void startAnim() { mAnimating = 1; }
void stopAnim() { mAnimating = 0; }
void rewind() { mFrame = 0; }
void xadd(int nr) { mX+=nr; }
void yadd(int nr) { mY+=nr; }
void xset(int nr) { mX=nr; }
void yset(int nr) { mY=nr; }
void set(int xx, int yy) { mX=xx; mY=yy; }

private:
int mFrame;
int mX, mY, mOldX, mOldY;
int mAnimating;
int mDrawn;
float mSpeed;
long mLastUpdate;
CSpriteBase *mSpriteBase;
SDL_Surface *mBackreplacement;
SDL_Surface *mScreen;
};

int CSprite::init(CSpriteBase *base, SDL_Surface *screen)
{
mSpriteBase = base;
if(mSpriteBase->mBuilt)
{
if(mSpriteBase->mNumframes > 1) mAnimating = 1;
mBackreplacement = SDL_DisplayFormat(mSpriteBase->mAnim[0].image);
}
mScreen = screen;
}

void CSprite::clearBG()
{
if(mDrawn==1)
{
SDL_Rect dest;
dest.x = mOldX;
dest.y = mOldY;
dest.w = mSpriteBase->mW;
dest.h = mSpriteBase->mH;
SDL_BlitSurface(mBackreplacement, NULL, mScreen, &dest);
}
}

void CSprite::updateBG()
{
SDL_Rect srcrect;
srcrect.w = mSpriteBase->mW;
srcrect.h = mSpriteBase->mH;
srcrect.x = mX;
srcrect.y = mY;
mOldX=mX;mOldY=mY;
SDL_BlitSurface(mScreen, &srcrect, mBackreplacement, NULL);
}

void CSprite::draw()
{
if(mAnimating == 1)
{
if(mLastUpdate+mSpriteBase->mAnim[mFrame].pause*mSpeed < SDL_GetTicks())
{
mFrame++;
if(mFrame > mSpriteBase->mNumframes-1) mFrame=0;
mLastUpdate = SDL_GetTicks();
}
}
if(mDrawn==0) mDrawn=1;
SDL_Rect dest;
dest.x = mX; dest.y = mY;
SDL_BlitSurface(mSpriteBase->mAnim[mFrame].image, NULL, mScreen, &dest);
}

SDL_Surface *screen, *back;
CSpriteBase vikingbase;
CSpriteBase sunbase;
CSprite vikings1;
CSprite vikings2;
CSprite sun;

SDL_Surface * ImageLoad(char *file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file);
temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2;
}

int InitImages()
{
back = ImageLoad("data/bg.bmp");
return 0;
}

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 DrawBG()
{
DrawIMG(back, 0, 0);
}

void DrawScene()
{
sun.clearBG();
vikings1.clearBG();
vikings2.clearBG();

sun.updateBG();
vikings1.updateBG();
vikings2.updateBG();

sun.draw();
vikings1.draw();
vikings2.draw();
SDL_Flip(screen);
}


int main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
cout<< "Unable to init SDL: \n" << SDL_GetError();
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
cout<< "Unable to set 640x480 video: \n" << SDL_GetError();
exit(1);
}

Uint8* keys;

vikingbase.init("data/vikings");
sunbase.init("data/sun");

sun.init(&sunbase,screen);
sun.set(480,50);
sun.setSpeed(1);

vikings1.init(&vikingbase,screen);
vikings1.set(150,300);
vikings1.setSpeed(1);

vikings2.init(&vikingbase,screen);
vikings2.set(350,300);
vikings2.setSpeed(1.5);

SDL_ShowCursor(0);

InitImages();
DrawBG();

int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
if ( event.key.keysym.sym ==SDLK_SPACE){sun.toggleAnim();}
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { vikings1.yadd(-1); }
if ( keys[SDLK_DOWN] ) { vikings1.yadd(1); }
if ( keys[SDLK_LEFT] ) { vikings1.xadd(-1); }
if ( keys[SDLK_RIGHT] ) { vikings1.xadd(1); }
DrawScene();
}
system("pause");
return 0;
}


I don't know at all where the problem would be. I hope anyone can help me here.

Share this post


Link to post
Share on other sites
This is my code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

using namespace std;

struct CSpriteFrame
{
SDL_Surface *image;
int pause;
};

class CSpriteBase
{
public:
int init(char *dir);
CSpriteFrame *mAnim;
int mBuilt, mNumframes, mW, mH;
};

int CSpriteBase::init(char *dir)
{
char buffer[255];
char filename[255];
char name[255];
int pause = 0, r = 0, g = 0, b = 0;
FILE *file;

sprintf(filename, "%s/info", dir);

if((file=fopen(filename, "r")) == NULL) //fopen(filename, "r")
{
cout<< "error opening the file \n\n" << filename;
system("pause");
return -1;
}
fgets(buffer, 255, file);
sscanf(buffer, "FILES: %d", &mNumframes);
mAnim = new CSpriteFrame[mNumframes];
int mBuilt = 1;
int count = 0;

while(!feof(file) && count < mNumframes)
{
fgets(buffer, 255, file);
if(buffer[0] != '#' && buffer[0] != 'r' && buffer[0] != '\0' && buffer[0] != '\n' && strlen(buffer) != 0)
{
sscanf(buffer, "%s %d %d %d %d", &name, &pause, &r, &g, &b);
sprintf(filename, "%s/%s", dir, name);

SDL_Surface *temp;
if((temp = SDL_LoadBMP(filename)) == NULL)
{
system("pause");
return -1;
}
if(r >= 0)
{
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b));
}
mAnim[count].image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
mAnim[count].pause = pause;
if(!mW) mW = mAnim[count].image->w;
if(!mH) mH = mAnim[count].image->h;

count++;
}
}
fclose(file); //fclose(file);
return 0;
}

class CSprite
{
public:
int init(CSpriteBase *base, SDL_Surface *screen);
void draw();
void clearBG();
void updateBG();

void setFrame(int nr) {mFrame = nr;}
int getFrame() {return mFrame;}
void setSpeed(float nr) {mSpeed = nr;}
float getSpeed() {return mSpeed;}
void toggleAnim() { mAnimating = !mAnimating; }
void startAnim() { mAnimating = 1; }
void stopAnim() { mAnimating = 0; }
void rewind() { mFrame = 0; }
void xadd(int nr) { mX+=nr; }
void yadd(int nr) { mY+=nr; }
void xset(int nr) { mX=nr; }
void yset(int nr) { mY=nr; }
void set(int xx, int yy) { mX=xx; mY=yy; }

private:
int mFrame;
int mX, mY, mOldX, mOldY;
int mAnimating;
int mDrawn;
float mSpeed;
long mLastUpdate;
CSpriteBase *mSpriteBase;
SDL_Surface *mBackreplacement;
SDL_Surface *mScreen;
};

int CSprite::init(CSpriteBase *base, SDL_Surface *screen)
{
mSpriteBase = base;
if(mSpriteBase->mBuilt)
{
if(mSpriteBase->mNumframes > 1) mAnimating = 1;
mBackreplacement = SDL_DisplayFormat(mSpriteBase->mAnim[0].image);
}
mScreen = screen;
}

void CSprite::clearBG()
{
if(mDrawn==1)
{
SDL_Rect dest;
dest.x = mOldX;
dest.y = mOldY;
dest.w = mSpriteBase->mW;
dest.h = mSpriteBase->mH;
SDL_BlitSurface(mBackreplacement, NULL, mScreen, &dest);
}
}

void CSprite::updateBG()
{
SDL_Rect srcrect;
srcrect.w = mSpriteBase->mW;
srcrect.h = mSpriteBase->mH;
srcrect.x = mX;
srcrect.y = mY;
mOldX=mX;mOldY=mY;
SDL_BlitSurface(mScreen, &srcrect, mBackreplacement, NULL);
}

void CSprite::draw()
{
if(mAnimating == 1)
{
if(mLastUpdate+mSpriteBase->mAnim[mFrame].pause*mSpeed < SDL_GetTicks())
{
mFrame++;
if(mFrame > mSpriteBase->mNumframes-1) mFrame=0;
mLastUpdate = SDL_GetTicks();
}
}
if(mDrawn==0) mDrawn=1;
SDL_Rect dest;
dest.x = mX; dest.y = mY;
SDL_BlitSurface(mSpriteBase->mAnim[mFrame].image, NULL, mScreen, &dest);
}

SDL_Surface *screen, *back;
CSpriteBase vikingbase;
CSpriteBase sunbase;
CSprite vikings1;
CSprite vikings2;
CSprite sun;

SDL_Surface * ImageLoad(char *file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file);
temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2;
}

int InitImages()
{
back = ImageLoad("data/bg.bmp");
return 0;
}

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 DrawBG()
{
DrawIMG(back, 0, 0);
}

void DrawScene()
{
sun.clearBG();
vikings1.clearBG();
vikings2.clearBG();

sun.updateBG();
vikings1.updateBG();
vikings2.updateBG();

sun.draw();
vikings1.draw();
vikings2.draw();
SDL_Flip(screen);
}


int main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
cout<< "Unable to init SDL: \n" << SDL_GetError();
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
cout<< "Unable to set 640x480 video: \n" << SDL_GetError();
exit(1);
}

Uint8* keys;

vikingbase.init("data/vikings");
sunbase.init("data/sun");

sun.init(&sunbase,screen);
sun.set(480,50);
sun.setSpeed(1);

vikings1.init(&vikingbase,screen);
vikings1.set(150,300);
vikings1.setSpeed(1);

vikings2.init(&vikingbase,screen);
vikings2.set(350,300);
vikings2.setSpeed(1.5);

SDL_ShowCursor(0);

InitImages();
DrawBG();

int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
if ( event.key.keysym.sym ==SDLK_SPACE){sun.toggleAnim();}
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { vikings1.yadd(-1); }
if ( keys[SDLK_DOWN] ) { vikings1.yadd(1); }
if ( keys[SDLK_LEFT] ) { vikings1.xadd(-1); }
if ( keys[SDLK_RIGHT] ) { vikings1.xadd(1); }
DrawScene();
}
system("pause");
return 0;
}


I don't know at all where the problem would be. I hope anyone can help me here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!