Re : Re : Layered Display

Hi Edward,

In your project, what it the usage of “IMGOverlay” ? Reading the source code, I was wondering if you
were using it for moving sprites over a background or something like that, blitting a “mask” at the old
position and then blitting the sprite at the new position, updating the mask with the backgrnd
data ?

Julien

----- Message d’origine ----De : Edward Byard <e_byard at yahoo.co.uk>
? : A list for developers using the SDL library. (includes SDL-announce)
Envoy? le : Jeudi, 12 Juin 2008, 22h47mn 43s
Objet : Re: [SDL] Re : Layered Display

Oh, also…I have implemented a similar system in a game I’ve just done - a very simple approach to produce an “overlay”.
It can be visible or not, moved when visible (or not) etc etc…just as long as you update the background surface.
Quick and dirty but it works.

Here’s the code anyway. C++ isn’t my first language so don’t complain if stuff looks bad :slight_smile:

Don’t know if it’s of any use.

Header:

#ifndef IMGOverlay_included
#define IMGOverlay_included

class IMGOverlay
{
public:
IMGOverlay(int x,int y,char *filePath,SDL_Surface *srcSurface);
~IMGOverlay(); // Destructor

void UpdateBackgroundSurface(SDL_Surface *dest);
void SetAlpha(int,SDL_Surface *dest);
bool IsEnabled(void);
bool IsVisible(void);

void SetVisible(bool flag,SDL_Surface *dest);
void SetEnabled(bool flag);
void SetXY(int x,int y);
 int getX(void);
 int getY(void);

private:
bool olVisible;
bool olEnabled;
int olXPos;
int olYPos;
int olWPos;
int olHPos;
SDL_Surface *olBackSurface;
SDL_Surface *olSurface;
};

#endif

// Create an “overlay”. Ability to
// Saves portion of surface and allow replacing later
#include “stdafx.h”

#include
#include

#include “SDL.h”
#include “SDL_image.h”

#include “GfxSystem.h”
#include “IMGOverlay.h”

#include “ErrorSystem.h”

IMGOverlay::IMGOverlay(int x,int y,char *filePath,SDL_Surface *srcSurface)
{
this->olEnabled = false;
this->olVisible = false;
this->olSurface = GfxSystem::getInstance()->loadGraphic(filePath);
if (this->olSurface != NULL)
{
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
offset.w = this->olSurface->w;
offset.h = this->olSurface->h;
// Grab background surface and save for restoring later
this->olBackSurface = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA,this->olSurface->w,this->olSurface->h,32,RMASK,GMASK,BMASK,0);
SDL_BlitSurface(srcSurface,&offset,this->olBackSurface,NULL);
this->olHPos=this->olSurface->h;
this->olWPos=this->olSurface->w;
this->olXPos = x;
this->olYPos = y;
}
else
{
ErrorSystem::getInstance()->FatalError(ESYS_GRAPHIC_RES_ERROR,filePath);
}
}

IMGOverlay::~IMGOverlay()
{
// Free background and overlay surface
SDL_FreeSurface(this->olSurface);
SDL_FreeSurface(this->olBackSurface);
}

void IMGOverlay::SetEnabled(bool flag)
{
this->olEnabled = flag;
}

bool IMGOverlay::IsEnabled(void)
{
return this->olEnabled;
}

bool IMGOverlay::IsVisible(void)
{
return this->olVisible;
}

void IMGOverlay::SetVisible(bool flag,SDL_Surface *dest)
{
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = this->olXPos;
offset.y = this->olYPos;
offset.h = this->olHPos;
offset.w = this->olWPos;

this->olVisible = flag;

if (this->olVisible)
    SDL_BlitSurface(this->olSurface, NULL, dest, &offset );
else
    SDL_BlitSurface(this->olBackSurface, NULL, dest, &offset );

SDL_UpdateRect(dest,offset.x,offset.y,offset.w,offset.h);

}

void IMGOverlay::UpdateBackgroundSurface(SDL_Surface *dest)
{
SDL_Rect offset;
offset.x = this->olXPos;
offset.y = this->olYPos;
offset.h = this->olHPos;
offset.w = this->olWPos;
// Grab background surface and save for restoring later
SDL_BlitSurface(dest,&offset,this->olBackSurface,NULL);
}

void IMGOverlay::SetAlpha(int level,SDL_Surface *dest)
{
SDL_SetAlpha(this->olSurface,SDL_SRCALPHA|SDL_RLEACCEL,level);
SetVisible(true,dest);
}

void IMGOverlay::SetXY(int x,int y)
{
this->olXPos = x;
this->olYPos = y;
}

int IMGOverlay::getX(void)
{
return this->olXPos;
}
int IMGOverlay::getY(void)
{
return this->olYPos;
}


Sent from Yahoo! Mail.
A Smarter Email.

  _____________________________________________________________________________ 

Envoyez avec Yahoo! Mail. Une boite mail plus intelligente http://mail.yahoo.fr