Problem with SDL_BlitSurface() and Seg. fault : SDL Parachute Deployed

I’ve got a problem with a SDL_BlitSurface(), located near the end of the
code. if I comment it out the program runs like it’s expected (ie : without
a lot of function right now).

The only error message i’ve got is : Fatal signal: Segmentation Fault (SDL
Parachute Deployed)
The SDL_BlitSurface return no error message, in fact I’m not sure it do
something.

I’m clueless about what to do.

I’m on linux (Slackware 8.1) ,kernel 2.4.18 , SDL-1.2.5
By the way, i’m kinda new to programming (started one week ago) so feel free
to enlight me about my coding style or thing i’m doing wrong. I’m here to
learn after all.

So here’s the code :

#include
#include
#include
#include “main.h” // contain function prototype only.
#include “SDL.h”
#include “SDL_image.h”

// define the block size (X pixel)
#define BSize 20

// define the coord. where the xy matrix start (220px by 58px)
#define XMatrix 220
#define YMatrix 58

// define the size of the matrix in block
#define WMatrix 10
#define HMatrix 20

using namespace std;

SDL_Surface* Screen = NULL; // the main screen
SDL_Surface* SMatrix = NULL; // the matrix where the block will fall
SDL_Surface* Background = NULL; // the background image
SDL_Surface* Block = NULL; // the block

SDL_Rect Src_Rect, Dest_Rect;

// Matrix or grid. A place to store data about the fallen block
vector< vector > Matrix;

int main(int argc, char* argv[])
{

if( SDL_Init(SDL_INIT_VIDEO) ==-1)
{
cerr << "Can’t initialize SDL, " << SDL_GetError() << endl;
exit(-1);
}

//Cleaning SDL
atexit(SDL_Quit);

//Should do a way to change setting on the fly or at least read from a
file
Screen = SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);
if (!Screen)
{
cerr << "Can’t set videomode, " << SDL_GetError() << endl;
exit(-1);
}

Background = IMG_Load(“graphic/background.jpg”);
if(!Background)
cerr << "Can’t load background image, " << SDL_GetError() << endl;

Block = IMG_Load(“graphic/block.jpg”);
if(!Block)
cerr << "Can’t load block image, " << SDL_GetError() << endl;

SMatrix = IMG_Load(“graphic/matrix.jpg”);
if(!SMatrix)
cerr << "Can’t load matrix image, " << SDL_GetError() << endl;

Src_Rect.w=Dest_Rect.w=Background->w;
Src_Rect.h=Dest_Rect.y=Background->h;
Src_Rect.x=Src_Rect.y=0;
Dest_Rect.x=Dest_Rect.y=0;

SDL_BlitSurface(Background, &Src_Rect, Screen, &Dest_Rect);
SDL_Flip(Screen);

//
InitMatrix();
GarbageMatrix(10);
DrawBlock();

}

void InitMatrix()
{
cout << “Initializing the Matrix” << endl;
Matrix.resize(HMatrix);
for(int i = 0; i<Matrix.size(); i++)
{
Matrix[i].resize(WMatrix);
for (int j = 0; j<Matrix[i].size(); j++)
{
Matrix[i][j] = 0;
}
}
}

void GarbageMatrix(int a)
{
for(int i = a; i != 0; i–)
{
for(int j = 0; j<Matrix[i].size(); j++)
{
if(rand()%3 == 0)
{
Matrix[i][j] = 1;
cout << i << “:” << j << “\t” << Matrix[i][j] << endl;
}
}
}
}

void DrawBlock()
{
int x = 0;
int y = 0;
SDL_Rect Dest, Src;
for(int i = Matrix.size(); i != 0; i–)
{
for(int j = 0; j<Matrix[i].size(); j++)
{
if(Matrix[i][j] == 1)
{
Src.w = Src.h = BSize;
Src.x = Src.y = 0;
Dest.x = x;
Dest.y = y;
// i’ve isolated this line, if i commented it out there’s no problem.
// SDL_BlitSurface(Block, &Src, SMatrix, &Dest);
//
}
x += BSize;
}
y += BSize;
}
}_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail

SDL_Rect Dest, Src;
Src.w = Src.h = BSize;
Src.x = Src.y = 0;
Dest.x = x;
Dest.y = y;
// i’ve isolated this line, if i commented it out there’s no problem.
// SDL_BlitSurface(Block, &Src, SMatrix, &Dest);

Where do you initialize Dest.w and Dest.h?
Note that you need to initialize them each time through the loop if there’s
any possibility of clipping, since it’s modified with the actual blit rect.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Finaly it’s nothing related to SDL, the if(Matrix[i][j] == 1) seem to cause
the segmentation fault. I dunno why right now but at least I’m on the way to
find it, so please disregard the previous message as it doesn’t belong to
this list.

Sorry.

Cordialy,
Karl St-Jacques>I’ve got a problem with a SDL_BlitSurface(), located near the end of the

code. if I comment it out the program runs like it’s expected (ie : without
a lot of function right now).

The only error message i’ve got is : Fatal signal: Segmentation Fault (SDL
Parachute Deployed)
The SDL_BlitSurface return no error message, in fact I’m not sure it do
something.

I’m clueless about what to do.

I’m on linux (Slackware 8.1) ,kernel 2.4.18 , SDL-1.2.5
By the way, i’m kinda new to programming (started one week ago) so feel
free to enlight me about my coding style or thing i’m doing wrong. I’m here
to learn after all.

So here’s the code :

#include
#include
#include
#include “main.h” // contain function prototype only.
#include “SDL.h”
#include “SDL_image.h”

// define the block size (X pixel)
#define BSize 20

// define the coord. where the xy matrix start (220px by 58px)
#define XMatrix 220
#define YMatrix 58

// define the size of the matrix in block
#define WMatrix 10
#define HMatrix 20

using namespace std;

SDL_Surface* Screen = NULL; // the main screen
SDL_Surface* SMatrix = NULL; // the matrix where the block will fall
SDL_Surface* Background = NULL; // the background image
SDL_Surface* Block = NULL; // the block

SDL_Rect Src_Rect, Dest_Rect;

// Matrix or grid. A place to store data about the fallen block
vector< vector > Matrix;

int main(int argc, char* argv[])
{

if( SDL_Init(SDL_INIT_VIDEO) ==-1)
{
cerr << "Can’t initialize SDL, " << SDL_GetError() << endl;
exit(-1);
}

//Cleaning SDL
atexit(SDL_Quit);

//Should do a way to change setting on the fly or at least read from a
file
Screen = SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);
if (!Screen)
{
cerr << "Can’t set videomode, " << SDL_GetError() << endl;
exit(-1);
}

Background = IMG_Load(“graphic/background.jpg”);
if(!Background)
cerr << "Can’t load background image, " << SDL_GetError() << endl;

Block = IMG_Load(“graphic/block.jpg”);
if(!Block)
cerr << "Can’t load block image, " << SDL_GetError() << endl;

SMatrix = IMG_Load(“graphic/matrix.jpg”);
if(!SMatrix)
cerr << "Can’t load matrix image, " << SDL_GetError() << endl;

Src_Rect.w=Dest_Rect.w=Background->w;
Src_Rect.h=Dest_Rect.y=Background->h;
Src_Rect.x=Src_Rect.y=0;
Dest_Rect.x=Dest_Rect.y=0;

SDL_BlitSurface(Background, &Src_Rect, Screen, &Dest_Rect);
SDL_Flip(Screen);

//
InitMatrix();
GarbageMatrix(10);
DrawBlock();

}

void InitMatrix()
{
cout << “Initializing the Matrix” << endl;
Matrix.resize(HMatrix);
for(int i = 0; i<Matrix.size(); i++)
{
Matrix[i].resize(WMatrix);
for (int j = 0; j<Matrix[i].size(); j++)
{
Matrix[i][j] = 0;
}
}
}

void GarbageMatrix(int a)
{
for(int i = a; i != 0; i–)
{
for(int j = 0; j<Matrix[i].size(); j++)
{
if(rand()%3 == 0)
{
Matrix[i][j] = 1;
cout << i << “:” << j << “\t” << Matrix[i][j] << endl;
}
}
}
}

void DrawBlock()
{
int x = 0;
int y = 0;
SDL_Rect Dest, Src;
for(int i = Matrix.size(); i != 0; i–)
{
for(int j = 0; j<Matrix[i].size(); j++)
{
if(Matrix[i][j] == 1)
{
Src.w = Src.h = BSize;
Src.x = Src.y = 0;
Dest.x = x;
Dest.y = y;
// i’ve isolated this line, if i commented it out there’s no problem.
// SDL_BlitSurface(Block, &Src, SMatrix, &Dest);
//
}
x += BSize;
}
y += BSize;
}
}


Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

Finaly it’s nothing related to SDL, the if(Matrix[i][j] == 1) seem to cause
the segmentation fault. I dunno why right now but at least I’m on the way to
find it, so please disregard the previous message as it doesn’t belong to
this list.

void DrawBlock()
{
int x = 0;
int y = 0;
SDL_Rect Dest, Src;
for(int i = Matrix.size(); i != 0; i–)

Here’s your problem, i needs to start at Matrix.size() - 1.
You’ll also need to special case for size() == 0.

Ron Steinke> From: “Karl St-Jacques”

Yeah, I’ve just found that 10 minutes ago! I did not know that the first
element was element zero ! now I know :slight_smile:

but thanks for the answer !>From: rsteinke at w-link.net

Reply-To: sdl at libsdl.org
To: sdl at libsdl.org
Subject: Re: [SDL] problem with SDL_BlitSurface() and Seg. fault : SDL
Parachute Deployed
Date: Wed, 23 Jul 2003 10:19:25 -0700

From: “Karl St-Jacques” <@Karl_St-Jacques>

Finaly it’s nothing related to SDL, the if(Matrix[i][j] == 1) seem to
cause
the segmentation fault. I dunno why right now but at least I’m on the
way to
find it, so please disregard the previous message as it doesn’t belong
to
this list.

void DrawBlock()
{
int x = 0;
int y = 0;
SDL_Rect Dest, Src;
for(int i = Matrix.size(); i != 0; i–)

Here’s your problem, i needs to start at Matrix.size() - 1.
You’ll also need to special case for size() == 0.

Ron Steinke


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Help STOP SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail