PDA

View Full Version : SDL Coding Help?



psiko_scweek
October 10th, 2006, 04:55
can anyone let me know what i did wrong with this code?


void gmp_splashscreenrun(SDL_Surface *splashtemp)
{
Object SplashScreen;
SplashScreen.setGraphic(splashtemp);
SplashScreen.setX((SCR_W/2)-(SplashScreen.getGraphicWidth()/2));
SplashScreen.setY((SCR_H/2)-(SplashScreen.getGraphicHeight()/2));
for(int i = 0;i<255;i++)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 254)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
for(int i = 255;i>0;i--)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 1)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
}

Object Information:

/////////// FUNCTION FOR FLIPPING THE SCREEN FROM THE BUFFER ///////////
void gfx_flipscreen()
{SDL_Flip(gfx_screen);}

/////////// INITIALIZE THE OBJECT STRUCTURES ///////////
class Object
{
protected:
int x, y;
int graphicwidth;
int graphicheight;
int height, width;


public:
Object();
~Object();
void step();
void draw();
void setGraphicHeight(int tempHeigt);
void setGraphicWidth(int tempWidth);
int getGraphicHeight();
int getGraphicWidth();
void setX(int tempX);
void setY(int tempY);
int getX();
int getY();
int direction;
void setGraphic(SDL_Surface *graphic);
SDL_Surface *graphic;
};

/////////// SETUP THE OBJECTS CONSTRUCTOR ///////////
Object::Object()
{
x = 0;
y = 0;
graphic = NULL;
graphicwidth = 0;
graphicheight = 0;
}

/////////// SETUP THE OBJECTS DESTRUCTOR ///////////
Object::~Object()
{
if (graphic != NULL)
{SDL_FreeSurface(graphic);}
}

/////////// SETUP THE OBJECTS DRAW ///////////
void Object::draw()
{gfx_apply_surface(x,y,graphic,gfx_screen);}

/////////// SET THE GRAPHICS FOR THE OBJECT ///////////
void Object::setGraphic(SDL_Surface *setgraphic)
{
graphic = setgraphic;
graphicwidth = graphic->w;
graphicheight = graphic->h;
}

anyone? this code keeps crashing my PSP. Whenever the game has to call SDL_FreeSurface it crashes and I dont know why!! If I dont tell the game to Free the surface the code works fine, but i know I need to free my surfaces or my game will run VERY slowly.

thanks!

yaustar
October 10th, 2006, 13:38
The only possible problem that I can see is that the surface that was passed into gmp_splashscreenrun, is freed elsewhere in the code which means that SplashScreen.graphic would be pointing to garbarge when it tries to 'free' it. In short, you are possiblely trying to free a freed surface.

In strict OO terms, the Object class should be solely responisble for creating a SDL_Surface from an image and also freeing it when the Object is destroyed rather then having a SDL_Surface passed to it.

psiko_scweek
October 10th, 2006, 16:07
hate to sound stupid, but care to explain that last part a bit more for me?



In strict OO terms, the Object class should be solely responisble for creating a SDL_Surface from an image and also freeing it when the Object is destroyed rather then having a SDL_Surface passed to it.


when you say the Object class should be solely respoonsible for creating the SDL_Surface, is that loading it as well? Just curious as im new to this and im really just trying to get my bug sorted out!

in fact here is my entire sourcecode thus far, lemme know if you can see my problem :)
-----------------------------------------------------------------
#include <fstream>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_image.h>

/////////// DEFINE FUNCTIONS ///////////
#define SCR_W 480
#define SCR_H 272
#define SCR_BBP 16

/////////// SDL CONFIGURATION OBJECTS ///////////
SDL_Joystick *stick = NULL;
SDL_Event event;

SDL_Surface *gfx_screen;
SDL_Surface *gfx_psiko;

/////////// INITIALIZE THE SDL VIDEO, SET THE SCREEN MODE ///////////
int InitSDLVideo()
{
gfx_screen = SDL_SetVideoMode(SCR_W,SCR_H,SCR_BBP,SDL_SWSURFACE |SDL_DOUBLEBUF);
if(gfx_screen == NULL)
{
fprintf(stderr,"Error in video %s\n", SDL_GetError());
return 1;
}
return 0;
}

/////////// CREATE THE SPECIFICS FOR LOADING GRAPHICS ///////////
SDL_Surface *gfx_load_image(char *filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;

loadedImage = IMG_Load(filename);
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}

/////////// INITIALIZE THE SDL AUDIO///////////
int InitSDLAudio()
{
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 512) < 0)
{
fprintf(stderr,"Error in audio %s\n", Mix_GetError());
return 1;
}
return 0;
}

/////////// FUNCTION FOR BLITTING IMAGES TO THE SCREEN BUFFER ///////////
void gfx_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 );
}

/////////// FUNCTION FOR FLIPPING THE SCREEN FROM THE BUFFER ///////////
void gfx_flipscreen()
{SDL_Flip(gfx_screen);}

/////////// INITIALIZE THE OBJECT STRUCTURES ///////////
class Object
{
protected:
int x, y;
int graphicwidth;
int graphicheight;
int height, width;


public:
Object();
~Object();
void step();
void draw();
void setGraphicHeight(int tempHeigt);
void setGraphicWidth(int tempWidth);
int getGraphicHeight();
int getGraphicWidth();
void setX(int tempX);
void setY(int tempY);
int getX();
int getY();
int direction;
void setGraphic(SDL_Surface *graphic);
SDL_Surface *graphic;
};

/////////// SETUP THE OBJECTS CONSTRUCTOR ///////////
Object::Object()
{
x = 0;
y = 0;
graphic = NULL;
graphicwidth = 0;
graphicheight = 0;
}

/////////// SETUP THE OBJECTS DESTRUCTOR ///////////
Object::~Object()
{
if (graphic)
{SDL_FreeSurface(graphic);graphic = NULL;}
}

/////////// SETUP THE OBJECTS DRAW ///////////
void Object::draw()
{gfx_apply_surface(x,y,graphic,gfx_screen);}

/////////// SET THE GRAPHICS FOR THE OBJECT ///////////
void Object::setGraphic(SDL_Surface* setgraphic)
{
graphic = setgraphic;
graphicwidth = graphic->w;
graphicheight = graphic->h;
}

/////////// SET X AND Y FOR THE OBJECT ///////////
int Object::getX()
{return x;}

int Object::getY()
{return y;}

/////////// SET X AND Y FOR THE OBJECT ///////////
void Object::setX(int tempX)
{x = tempX;}

void Object::setY(int tempY)
{y = tempY;}

/////////// SET THE GRAPHIC HEIGHT AND WIDTH ///////////
void Object::setGraphicHeight(int tempHeight)
{graphicheight = tempHeight;}

void Object::setGraphicWidth(int tempWidth)
{graphicwidth = tempWidth;}

/////////// GET THE GRAPHIC HEIGHT AND WIDTH ///////////
int Object::getGraphicHeight()
{return graphicheight;}

int Object::getGraphicWidth()
{return graphicwidth;}

/////////// COLLISION DETECTION (BOUNDING BOX) ///////////
int obj_IsCollision(Object *object1, Object *object2)
{
int obj_top1, obj_top2;
int obj_bot1, obj_bot2;
int obj_rigt1, obj_rigt2;
int obj_left1, obj_left2;

obj_left1 = object1->getX();
obj_left2 = object2->getX();
obj_top1 = object1->getY();
obj_top2 = object2->getY();

obj_rigt1 = object1->getX() + object1->getGraphicWidth();
obj_rigt2 = object2->getX() + object2->getGraphicWidth();
obj_bot1 = object1->getY() + object1->getGraphicHeight();
obj_bot2 = object2->getY() + object2->getGraphicHeight();

if (obj_bot1 <= obj_top2) return(0);
if (obj_top1 >= obj_bot2) return(0);
if (obj_rigt1 <= obj_left2) return(0);
if (obj_left1 >= obj_rigt2) return(0);

return 1;
}

void gmp_splashscreenrun(SDL_Surface *splashtemp)
{
Object SplashScreen;
SplashScreen.setGraphic(splashtemp);
SplashScreen.setX((SCR_W/2)-(SplashScreen.getGraphicWidth()/2));
SplashScreen.setY((SCR_H/2)-(SplashScreen.getGraphicHeight()/2));
for(int i = 0;i<255;i++)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 254)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
for(int i = 255;i>0;i--)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 1)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
}

/////////// START THE MAIN FUNCTION ///////////
extern "C" int main(){

/////////// INITIALIZE THE SDL FUNCTIONS ///////////
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_J OYSTICK)<0)
{
fprintf(stderr, "Unable to Init SDL: %s\n",SDL_GetError());
exit(1);
}
InitSDLVideo();
InitSDLAudio();
stick = SDL_JoystickOpen(0);

gmp_splashscreenrun(gfx_load_image("psiko.png"));

while(1)
{

}
SDL_Quit();
return 0;
}

quzar
October 10th, 2006, 18:46
Damn, I had written a much longer thing, but my laptop bluescreened (o_O?). Anyways, a few tidbits that are mostly related to style consistancy or something.

First, your optimizedImage ends up existing only locally in each function. Since all that you'd end up doing is storing extra pointer which is like 4bytes, you would probably want to have some sort of global reference to it. If you don't, you might end up accidentally losing track of it, and near the end of the program you might not free it (memory leak). (yes, i know in this instance you are passing it to the object, but I would say maybe you should consider either having a global var for it, or moving the image loading function to be a function of the class)

Next, when you open the file and create the optimized image, you check if the first thing is null (definetly something you'd want to do since it relies on stuff outside of the program) but you don't check to see if displayformat returns a null to optimized image, which although it shouldn't happen, you should check for it if you want to be safe.

last thing, just wondering, why are you setting an extra delay at the last looping of each of those loops?

yaustar
October 10th, 2006, 19:21
when you say the Object class should be solely respoonsible for creating the SDL_Surface, is that loading it as well? Just curious as im new to this and im really just trying to get my bug sorted out!
Correct, this is more of a design issue then a fix for your code though. Here is my SurfaceObject class that I use for SDL.

I can't see anything wrong with the code that crashes the program. Can you debug it and find the line of code that it crashes on?


// Header file
#ifndef CSURFACEOBJECT_H
#define CSURFACEOBJECT_H

#include <string>

struct SDL_Surface;

namespace Core
{
/// OO class that wraps around SDL_Surface and functions from SDL
class CSurfaceObject
{
private:
/// Pointer to the SDL_Surface
SDL_Surface * m_Surface;

public:
/// Standard constructor. NULLs the Surface
CSurfaceObject(void);

/// Contructor loading an image for the surface
CSurfaceObject(const std::string &Filename);

/// Destructor. Frees the surface if not NULL
~CSurfaceObject(void);

/// Returns pointer, the Surface
/**
\return A pointer to m_Surface
*/
SDL_Surface * GetSurfacePtr(void) const;

/// Load a new image. Frees the surface if not NULL
/**
\param Filename a const string of the file path to the image
\return True if loading the image succeded, false if failed
*/
bool LoadImage(const std::string &Filename);

/// Scale the surface to a defined width and height
/**
\param Width Unsigned integer of the new width in pixels
\param Height Unsigned integer of the new height in pixels
\return True if loading the scaling succeded, false if failed
*/
bool ScaleImage(unsigned int Width, unsigned int Height);

/// Scale the surface by a scaling factor
/**
\param Width Float of the Scaling factor
\return True if loading the scaling succeded, false if failed
*/
bool ScaleImage(float ScalingFactor);

/// Frees the Surface
void FreeSurface(void);
};
}

#endif // CSURFACEOBJECT_H



// Source file
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "../Logger/CLogger.h"
#include "SDL_rotozoom.h"
#include "CSurfaceObject.h"

using namespace Core;

CSurfaceObject::CSurfaceObject(void) : m_Surface(0)
{
ErrLog << "<< Creating a new Surface Object >>\n";
ErrLog << "<< Created a new Surface Object >>\n";
}

CSurfaceObject::CSurfaceObject(const std::string &Filename) : m_Surface(0)
{
ErrLog << "<< Creating a new Surface Object >>\n";
this->LoadImage(Filename);
ErrLog << "<< Created a new Surface Object >>\n";
}

CSurfaceObject::~CSurfaceObject(void)
{
ErrLog << "<< Destroying a Surface Object >>\n";
this->FreeSurface();
ErrLog << "<< Destroyed a Surface Object >>\n";
}

SDL_Surface * CSurfaceObject::GetSurfacePtr(void) const
{
return m_Surface;
}

bool CSurfaceObject::LoadImage(const std::string &Filename)
{
this->FreeSurface();
ErrLog << " Loading image: " << Filename << "\n";
m_Surface = IMG_Load(Filename.c_str() );

if(!m_Surface)
{
ErrLog << " ERROR: Image loading failed\n";
return false;
}

ErrLog << " Image loading succedeed\n";

// Set the Transparency by Colour key and convert the surface to the same format as the screen
SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_Surface->format, 255, 0, 255) );
SDL_Surface * OldSurface = m_Surface;
m_Surface = SDL_DisplayFormat(m_Surface);
SDL_FreeSurface(OldSurface);

if(!m_Surface)
{
ErrLog << " ERROR: Conversion of Surface failed\n";
return false;
}

return true;
}

bool CSurfaceObject::ScaleImage(unsigned int Width, unsigned int Height)
{
ErrLog << " Scaling the Surface to Width: " << Width << " Height: " << Height << "\n";

if(!m_Surface)
{
ErrLog << " ERROR: Surface is NULL\n";
return false;
}

SDL_Surface * OldSurface = m_Surface;

m_Surface = zoomSurface
(
m_Surface,
static_cast<float>(Width) / static_cast<float>(m_Surface->w),
static_cast<float>(Height) / static_cast<float>(m_Surface->h),
0
);

if(!m_Surface)
{
ErrLog << " ERROR: Scaling falied. Reverting to orignal surface\n";
m_Surface = OldSurface;
return false;
}

// Set the Transparency by Colour key and convert the surface to the same format as the screen
SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_Surface->format, 255, 0, 255) );
OldSurface = m_Surface;
m_Surface = SDL_DisplayFormat(m_Surface);
SDL_FreeSurface(OldSurface);

if(!m_Surface)
{
ErrLog << " ERROR: Conversion of Surface failed\n";
return false;
}

return true;
}

bool CSurfaceObject::ScaleImage(float ScaleFactor)
{
ErrLog << " Scaling the Surface by a factor of: " << ScaleFactor << "\n";

if(!m_Surface)
{
ErrLog << " ERROR: Surface is NULL\n";
return false;
}

SDL_Surface * OldSurface = m_Surface;

m_Surface = zoomSurface(m_Surface, ScaleFactor, ScaleFactor, 0);

if(!m_Surface)
{
ErrLog << " ERROR: Scaling falied. Reverting to orignal surface\n";
m_Surface = OldSurface;
return false;
}

// Set the Transparency by Colour key and convert the surface to the same format as the screen
SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_Surface->format, 255, 0, 255) );
OldSurface = m_Surface;
m_Surface = SDL_DisplayFormat(m_Surface);
SDL_FreeSurface(OldSurface);

if(!m_Surface)
{
ErrLog << " ERROR: Conversion of Surface failed\n";
return false;
}

return true;
}

void CSurfaceObject::FreeSurface(void)
{
if(m_Surface != 0)
{
ErrLog << " Freeing the current Surface\n";
SDL_FreeSurface(m_Surface);
m_Surface = 0;
ErrLog << " Freed Surface\n";
}
else
{
ErrLog << " Current Surface is NULL\n";
}
}

psiko_scweek
October 11th, 2006, 04:19
alright I modified the code and removed the OptimizedImage code and it no longer crashes when I call SDL_FreeSurface. Any idea as to why that code was caysing the game to crash!? Thanks, btw!

but the problem now is with that code removed the game runs slow as crap. the exact opposite of what i want!

this isnt fair!! lol

#include <fstream>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_image.h>

/////////// DEFINE FUNCTIONS ///////////
#define SCR_W 480
#define SCR_H 272
#define SCR_BBP 16

/////////// SDL CONFIGURATION OBJECTS ///////////
SDL_Joystick *stick = NULL;
SDL_Event event;

SDL_Surface *gfx_screen;
SDL_Surface *gfx_psiko;

/////////// INITIALIZE THE SDL VIDEO, SET THE SCREEN MODE ///////////
int InitSDLVideo()
{
gfx_screen = SDL_SetVideoMode(SCR_W,SCR_H,SCR_BBP,SDL_SWSURFACE |SDL_DOUBLEBUF);
if(gfx_screen == NULL)
{
fprintf(stderr,"Error in video %s\n", SDL_GetError());
return 1;
}
return 0;
}

/////////// INITIALIZE THE SDL AUDIO///////////
int InitSDLAudio()
{
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 512) < 0)
{
fprintf(stderr,"Error in audio %s\n", Mix_GetError());
return 1;
}
return 0;
}

/////////// FUNCTION FOR BLITTING IMAGES TO THE SCREEN BUFFER ///////////
void gfx_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 );
}

/////////// FUNCTION FOR FLIPPING THE SCREEN FROM THE BUFFER ///////////
void gfx_flipscreen()
{SDL_Flip(gfx_screen);}

/////////// INITIALIZE THE OBJECT STRUCTURES ///////////
class Object
{
protected:
int x, y;
int graphicwidth;
int graphicheight;
int height, width;


public:
Object();
~Object();
void step();
void draw();
void setGraphicHeight(int tempHeigt);
void setGraphicWidth(int tempWidth);
int getGraphicHeight();
int getGraphicWidth();
void setX(int tempX);
void setY(int tempY);
int getX();
int getY();
int direction;
void setGraphic(SDL_Surface *graphic);
SDL_Surface* loadImage(char *file);
SDL_Surface *graphic;
};

/////////// SETUP THE OBJECTS CONSTRUCTOR ///////////
Object::Object()
{
x = 0;
y = 0;
graphic = NULL;
graphicwidth = 0;
graphicheight = 0;
}

/////////// SETUP THE OBJECTS DESTRUCTOR ///////////
Object::~Object()
{
if (graphic != NULL)
{SDL_FreeSurface(graphic);graphic = NULL;}
}

SDL_Surface* Object::loadImage(char *filename)
{
SDL_Surface* loadedImage = NULL;
//SDL_Surface* optimizedImage = NULL;

loadedImage = IMG_Load(filename);
// if( loadedImage != NULL )
//{
// optimizedImage = SDL_DisplayFormat( loadedImage );
// SDL_FreeSurface( loadedImage );
// }
//return optimizedImage;
return loadedImage;
}

/////////// SETUP THE OBJECTS DRAW ///////////
void Object::draw()
{gfx_apply_surface(x,y,graphic,gfx_screen);}

/////////// SET THE GRAPHICS FOR THE OBJECT ///////////
void Object::setGraphic(SDL_Surface* setgraphic)
{
graphic = setgraphic;
graphicwidth = graphic->w;
graphicheight = graphic->h;
}

/////////// SET X AND Y FOR THE OBJECT ///////////
int Object::getX()
{return x;}

int Object::getY()
{return y;}

/////////// SET X AND Y FOR THE OBJECT ///////////
void Object::setX(int tempX)
{x = tempX;}

void Object::setY(int tempY)
{y = tempY;}

/////////// SET THE GRAPHIC HEIGHT AND WIDTH ///////////
void Object::setGraphicHeight(int tempHeight)
{graphicheight = tempHeight;}

void Object::setGraphicWidth(int tempWidth)
{graphicwidth = tempWidth;}

/////////// GET THE GRAPHIC HEIGHT AND WIDTH ///////////
int Object::getGraphicHeight()
{return graphicheight;}

int Object::getGraphicWidth()
{return graphicwidth;}

/////////// COLLISION DETECTION (BOUNDING BOX) ///////////
int obj_IsCollision(Object *object1, Object *object2)
{
int obj_top1, obj_top2;
int obj_bot1, obj_bot2;
int obj_rigt1, obj_rigt2;
int obj_left1, obj_left2;

obj_left1 = object1->getX();
obj_left2 = object2->getX();
obj_top1 = object1->getY();
obj_top2 = object2->getY();

obj_rigt1 = object1->getX() + object1->getGraphicWidth();
obj_rigt2 = object2->getX() + object2->getGraphicWidth();
obj_bot1 = object1->getY() + object1->getGraphicHeight();
obj_bot2 = object2->getY() + object2->getGraphicHeight();

if (obj_bot1 <= obj_top2) return(0);
if (obj_top1 >= obj_bot2) return(0);
if (obj_rigt1 <= obj_left2) return(0);
if (obj_left1 >= obj_rigt2) return(0);

return 1;
}

void gmp_splashscreenrun(char *filename)
{
Object SplashScreen;
SplashScreen.setGraphic(SplashScreen.loadImage(fil ename));
SplashScreen.setX((SCR_W/2)-(SplashScreen.getGraphicWidth()/2));
SplashScreen.setY((SCR_H/2)-(SplashScreen.getGraphicHeight()/2));
for(int i = 0;i<255;i++)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 254)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
for(int i = 255;i>0;i--)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 1)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
}

/////////// START THE MAIN FUNCTION ///////////
extern "C" int main(){

/////////// INITIALIZE THE SDL FUNCTIONS ///////////
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_J OYSTICK)<0)
{
fprintf(stderr, "Unable to Init SDL: %s\n",SDL_GetError());
exit(1);
}
InitSDLVideo();
InitSDLAudio();
stick = SDL_JoystickOpen(0);

gmp_splashscreenrun("psiko.png");

SDL_Quit();
return 0;
}

yaustar
October 11th, 2006, 13:34
The reason that it is slow is because you commented out the optimised image code and it's having to convert the Surface to the same format as the screen every frame.

I take a closer look at it when I get back home and get my development environment setup and track the problem of the crash.

psiko_scweek
October 11th, 2006, 15:57
I realized that it was running slow due to the missing OptimizedImage code because the game's graphics was no longer set to the same format as the screen (right?)

I appreciate your help!

yaustar
October 11th, 2006, 22:25
I have just tried the code and it doesn't crash. It runs and exits normally.

psiko_scweek
October 12th, 2006, 00:29
which code have you tried? If you tried the lastest write of code, it does not crash but just runs slowly.

if you used the first build....then what else could my problem be?

thanks again for taking your time to help me.

yaustar
October 12th, 2006, 10:05
This code

#include <fstream>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_image.h>

/////////// DEFINE FUNCTIONS ///////////
#define SCR_W 480
#define SCR_H 272
#define SCR_BBP 16

/////////// SDL CONFIGURATION OBJECTS ///////////
SDL_Joystick *stick = NULL;
SDL_Event event;

SDL_Surface *gfx_screen;
SDL_Surface *gfx_psiko;

/////////// INITIALIZE THE SDL VIDEO, SET THE SCREEN MODE ///////////
int InitSDLVideo()
{
gfx_screen = SDL_SetVideoMode(SCR_W,SCR_H,SCR_BBP,SDL_SWSURFACE |SDL_DOUBLEBUF);
if(gfx_screen == NULL)
{
fprintf(stderr,"Error in video %s\n", SDL_GetError());
return 1;
}
return 0;
}

/////////// INITIALIZE THE SDL AUDIO///////////
int InitSDLAudio()
{
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 512) < 0)
{
fprintf(stderr,"Error in audio %s\n", Mix_GetError());
return 1;
}
return 0;
}

/////////// FUNCTION FOR BLITTING IMAGES TO THE SCREEN BUFFER ///////////
void gfx_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 );
}

/////////// FUNCTION FOR FLIPPING THE SCREEN FROM THE BUFFER ///////////
void gfx_flipscreen()
{SDL_Flip(gfx_screen);}

/////////// INITIALIZE THE OBJECT STRUCTURES ///////////
class Object
{
protected:
int x, y;
int graphicwidth;
int graphicheight;
int height, width;


public:
Object();
~Object();
void step();
void draw();
void setGraphicHeight(int tempHeigt);
void setGraphicWidth(int tempWidth);
int getGraphicHeight();
int getGraphicWidth();
void setX(int tempX);
void setY(int tempY);
int getX();
int getY();
int direction;
void setGraphic(SDL_Surface *graphic);
SDL_Surface* loadImage(char *file);
SDL_Surface *graphic;
};

/////////// SETUP THE OBJECTS CONSTRUCTOR ///////////
Object::Object()
{
x = 0;
y = 0;
graphic = NULL;
graphicwidth = 0;
graphicheight = 0;
}

/////////// SETUP THE OBJECTS DESTRUCTOR ///////////
Object::~Object()
{
if (graphic != NULL)
{SDL_FreeSurface(graphic);graphic = NULL;}
}

SDL_Surface* Object::loadImage(char *filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;

loadedImage = IMG_Load(filename);
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}

/////////// SETUP THE OBJECTS DRAW ///////////
void Object::draw()
{gfx_apply_surface(x,y,graphic,gfx_screen);}

/////////// SET THE GRAPHICS FOR THE OBJECT ///////////
void Object::setGraphic(SDL_Surface* setgraphic)
{
graphic = setgraphic;
graphicwidth = graphic->w;
graphicheight = graphic->h;
}

/////////// SET X AND Y FOR THE OBJECT ///////////
int Object::getX()
{return x;}

int Object::getY()
{return y;}

/////////// SET X AND Y FOR THE OBJECT ///////////
void Object::setX(int tempX)
{x = tempX;}

void Object::setY(int tempY)
{y = tempY;}

/////////// SET THE GRAPHIC HEIGHT AND WIDTH ///////////
void Object::setGraphicHeight(int tempHeight)
{graphicheight = tempHeight;}

void Object::setGraphicWidth(int tempWidth)
{graphicwidth = tempWidth;}

/////////// GET THE GRAPHIC HEIGHT AND WIDTH ///////////
int Object::getGraphicHeight()
{return graphicheight;}

int Object::getGraphicWidth()
{return graphicwidth;}

/////////// COLLISION DETECTION (BOUNDING BOX) ///////////
int obj_IsCollision(Object *object1, Object *object2)
{
int obj_top1, obj_top2;
int obj_bot1, obj_bot2;
int obj_rigt1, obj_rigt2;
int obj_left1, obj_left2;

obj_left1 = object1->getX();
obj_left2 = object2->getX();
obj_top1 = object1->getY();
obj_top2 = object2->getY();

obj_rigt1 = object1->getX() + object1->getGraphicWidth();
obj_rigt2 = object2->getX() + object2->getGraphicWidth();
obj_bot1 = object1->getY() + object1->getGraphicHeight();
obj_bot2 = object2->getY() + object2->getGraphicHeight();

if (obj_bot1 <= obj_top2) return(0);
if (obj_top1 >= obj_bot2) return(0);
if (obj_rigt1 <= obj_left2) return(0);
if (obj_left1 >= obj_rigt2) return(0);

return 1;
}

void gmp_splashscreenrun(char *filename)
{
Object SplashScreen;
SplashScreen.setGraphic(SplashScreen.loadImage(fil ename));
SplashScreen.setX((SCR_W/2)-(SplashScreen.getGraphicWidth()/2));
SplashScreen.setY((SCR_H/2)-(SplashScreen.getGraphicHeight()/2));
for(int i = 0;i<255;i++)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 254)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
for(int i = 255;i>0;i--)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 1)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
}

/////////// START THE MAIN FUNCTION ///////////
extern "C" int main(){

/////////// INITIALIZE THE SDL FUNCTIONS ///////////
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_J OYSTICK)<0)
{
fprintf(stderr, "Unable to Init SDL: %s\n",SDL_GetError());
exit(1);
}
InitSDLVideo();
InitSDLAudio();
stick = SDL_JoystickOpen(0);

gmp_splashscreenrun("psiko.png");

SDL_Quit();
return 0;
}

psiko_scweek
October 12th, 2006, 15:40
i tried that exact code and it crashed on me at the same exact spot. (when calling SDL_FreeSurface)

could it be my Makefile!? or, perhaps my installation of SDL?

i copied your code and it did not work. however, if I remove SDL_DOUBLEBUF from the SDL_Video it works.

im absolutely baffled.

yaustar
October 12th, 2006, 18:30
What hardware are you running it on?

Try the flags SDL_HWSURFACE | SDL_DOUBLEBUF

psiko_scweek
October 13th, 2006, 17:58
Im running it on the PSP. (which i believe is the greatest device ever made) eitherway, ill try that. I removed the SDL_DOUBLEBUF and it started to work, but Ill try your suggestion. And again, thanks a lot! Ill be sure to give credit where it is due whenever I finish my game :-)

yaustar
October 13th, 2006, 18:17
That figures, I been trying this code on the PC which explains why I am not getting the same errors.