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