Matthieu Fertré wrote:
Now, I think, it's time to choose what library we should use. I prefer
SDL_gfx which seems really really stable! But we need to code some
additionnal functions like collision detection, rotation around a
point, and animation if we decide to use it!
I think the same, but don't accord too much credit to my point of view
as I have no practical experience with theese libraries
2) I have written a preliminary ressource_manager class that load
images and convert them for efficient blittering, should I send
theese files on the list, in patches ?
Of course, send on the list !
Here are the files
In order to verify that they work/are suitable for wormux I have tried
to progress in the port
- map: map/tile/sky/ground
- team: team/character
- object: physics/physical_object
- interface: interface/keyboard/mouse
- weapon: only mine and dynamite
... and alot of other things in order to be able to compile
(Dependencies,dependencies,dependencies....)
It's a lot of work but the result is encouraging
Here is a screenshot:
http://jeanchristophe.duber.free.fr/pub/Capture-9.png
And an archive is here (temporary, until I turn off my computer)
http://jcduberga.homelinux.net/apache2-default/pub/wormux/
--
Jean-Christophe Duberga - http://jeanchristophe.duber.free.fr
webmaster du site MNEA - http://www.mnea.fr
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Distance.cpp: Comppute distances between different geometrics things
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
******************************************************************************/
#include "Distance.h"
#include "Point.h"
#include "Rectangle.h"
int Distance( const Point2i &p1, const Point2i &p2)
{
return ( (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y)) >> 1;
}
float Distance( const Point2f &p1, const Point2f &p2)
{
return sqrtf( (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
}
float Distance( const Point2d &p1, const Point2d &p2)
{
return sqrt( (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
}
bool IsInside( const Rectanglei &r, const Point2i &p)
{
return ( p.x >= r.x && p.x <= r.x+r.w && p.y >= r.y && p.y <= r.y+r.h );
}
bool IsInside( const Rectanglef &r, const Point2f &p)
{
return ( p.x >= r.x && p.x <= r.x+r.w && p.y >= r.y && p.y <= r.y+r.h );
}
bool IsInside( const Rectangled &r, const Point2d &p)
{
return ( p.x >= r.x && p.x <= r.x+r.w && p.y >= r.y && p.y <= r.y+r.h );
}
bool Intersect( const Rectanglei &r1, const Rectanglei &r2)
{
return IsInside( r1, Point2i( r2.x, r2.y))
|| IsInside( r1, Point2i( r2.x, r2.y+r2.h))
|| IsInside( r1, Point2i( r2.x+r2.w, r2.y+r2.h))
|| IsInside( r1, Point2i( r2.x+r2.w, r2.y));
}
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Distance.h: Comppute distances between different geometrics things
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
*****************************************************************************/
#ifndef _DISTANCE_H
#define _DISTANCE_H
#include "Point.h"
#include "Rectangle.h"
extern int Distance( const Point2i &p1, const Point2i &p2);
extern float Distance( const Point2f &p1, const Point2f &p2);
extern float Distance( const Point2d &p1, const Point2d &p2);
extern bool IsInside( const Rectanglei &r, const Point2i &p);
extern bool IsInside( const Rectanglef &r, const Point2f &p);
extern bool IsInside( const Rectangled &r, const Point2d &p);
extern bool Intersect( const Rectanglei &r1, const Rectanglei &r2);
#endif // _DISTANCE_H
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Point.h: Standard C++ 2D Point template
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
*****************************************************************************/
#ifndef _POINT_H
#define _POINT_H
#include <cmath>
template<class T> class Point2
{
public:
inline Point2(){}
inline Point2(T x, T y)
{
this->x = x;
this->y = y;
}
T x, y;
};
typedef Point2<int> Point2i;
typedef Point2<float> Point2f;
typedef Point2<double> Point2d;
#endif // _POINT_H
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Rectangle.h: Standard C++ Rectangle template
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
*****************************************************************************/
#ifndef _RECTANGLE_H
#define _RECTANGLE_H
#include <cmath>
template<class T> class Rectangle
{
public:
inline Rectangle(){}
inline Rectangle(T ox, T oy, T width, T height)
{
this->x = ox;
this->y = oy;
this->w = width;
this->h = height;
}
inline void Clip( const Rectangle &cr)
{
T left = ( x < cr.x ) ? cr.x : ( x > cr.x+cr.w ) ? cr.x+cr.w : x;
T right = ( x+w < cr.x ) ? cr.x : ( x+w > cr.x+cr.w ) ? cr.x+cr.w : x+w;
T top = ( y < cr.y ) ? cr.y : ( y > cr.y+cr.h ) ? cr.y+cr.h : y;
T bottom = ( y+h < cr.y ) ? cr.y : ( y+h > cr.y+cr.h ) ? cr.y+cr.h : y+h;
x = left;
w = right-left;
y = top;
h = bottom-top;
}
T x, y, w, h;
};
typedef Rectangle<int> Rectanglei;
typedef Rectangle<float> Rectanglef;
typedef Rectangle<double> Rectangled;
#endif // _RECTANGLE_H
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Ressource Manager: Load resources (images/sprites) suitable for SDL
* Load directly or from refernece in xml resource profile
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
*
* TODO: Keep reference to resources, better exceptions
*****************************************************************************/
#include "ressource_manager.h"
#include <string>
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include "sprite.h"
#include "xml_document.h"
struct Profile
{
LitDocXml *doc;
std::string filename;
std::string relative_path;
};
RessourceManager::RessourceManager()
{
AddDataPath( std::string("./data/"));
}
RessourceManager::~RessourceManager()
{
}
void RessourceManager::AddDataPath( std::string base_path)
{
this->base_path = base_path;
}
SDL_Surface * RessourceManager::LoadImage( std::string ressource_str, bool alpha, bool set_colorkey, Uint32 colorkey)
{
SDL_Surface *pre_surface = NULL;
SDL_Surface *end_surface = NULL;
if ( ( pre_surface = IMG_Load( (base_path+ressource_str).c_str())) == NULL )
{
// TODO raise an "can't load file" exception
throw std::string("RessourceManager: can't load image ")+ressource_str;
return NULL;
}
// Convert the image to the same format as the one of the main surface
// So as to make blittering efficient
if ( ! alpha )
{
end_surface = SDL_DisplayFormat( pre_surface);
}
else
{
end_surface = SDL_DisplayFormatAlpha( pre_surface);
}
if ( end_surface == NULL )
{
SDL_FreeSurface( pre_surface);
throw std::string( "RessourceManager: error converting image " + ressource_str + std::string(": ") + std::string(SDL_GetError()));
return NULL;
}
SDL_FreeSurface( pre_surface);
// Set the transparency colorkey if requested
if ( set_colorkey )
{
SDL_SetColorKey( end_surface, SDL_SRCCOLORKEY,
colorkey /*SDL_MapRGB( end_surface->format, 255, 255, 255)*/) ;
}
#ifdef _DEBUG
std::cerr << "Image " << ressource_str << " loaded !" << std::endl;
#endif
return end_surface;
}
Profile *RessourceManager::LoadXMLProfile( std::string xml_filename)
{
LitDocXml *doc = new LitDocXml;
// Load the XML
if ( ! doc->Charge ( base_path + xml_filename ) )
{
// TODO raise an "can't load file" exception
throw std::string("RessourceManager: can't load profile ")+xml_filename;
return NULL;
}
Profile *profile = new Profile;
profile->doc = doc;
profile->filename = xml_filename;
if ( xml_filename.rfind("/") != xml_filename.npos)
{
profile->relative_path = xml_filename.substr(0,xml_filename.rfind("/")+1);
}
else
{
profile->relative_path = "";
}
return profile;
}
xmlpp::Element * RessourceManager::GetElement( Profile *profile, std::string ressource_type, std::string resource_name)
{
xmlpp::Element *elem = profile->doc->Access ( profile->doc->racine(),
ressource_type,
resource_name);
if ( elem == NULL )
{
std::string r_name = resource_name;
xmlpp::Element *cur_elem = profile->doc->racine();
while ( ( r_name.find("/") != r_name.npos ) && (cur_elem != NULL))
{
cur_elem = profile->doc->Access ( cur_elem, "section", r_name.substr(0, r_name.find("/")));
r_name = r_name.substr( r_name.find("/")+1, r_name.length());
}
if ( cur_elem)
{
elem = profile->doc->Access ( cur_elem, ressource_type, r_name);
}
}
return elem;
}
SDL_Surface *RessourceManager::LoadImage( Profile *profile, std::string resource_name)
{
xmlpp::Element *elem = GetElement ( profile, "surface", resource_name);
if ( elem == NULL)
{
// TODO raise an "requested resource is not present in the profile" exception
throw std::string("RessourceManager: can't find image resource \"")+resource_name+"\" in profile "+profile->filename;
return NULL;
}
std::string filename;
if ( ! profile->doc->LitAttrString( elem, "file", filename) )
{
// TODO raise an "resource is malformed in the profile" exception
throw std::string("RessourceManager: image resource \"")+resource_name+"\" has no file field in profile "+profile->filename;
return NULL;
}
// TODO load more properties in xml : alpha, colorkey....
// By now force alpha and no colorkey
bool alpha = true;
return LoadImage( profile->relative_path+filename, alpha);
}
Sprite *RessourceManager::LoadSprite( Profile *profile, std::string resource_name)
{
xmlpp::Element *elem_sprite = GetElement( profile, "sprite", resource_name);
if ( elem_sprite == NULL)
{
// TODO raise an "requested resource is not present in the profile" exception
throw std::string("RessourceManager: can't find sprite resource \"")+resource_name+"\" in profile "+profile->filename;;
return NULL;
}
xmlpp::Element *elem_image = profile->doc->AccesBalise ( elem_sprite, "image");
if ( elem_image == NULL )
{
// TODO raise an "requested resource is not present in the profile" exception
throw std::string("RessourceManager: can't load (sprite) resource ")+resource_name;
return NULL;
}
std::string image_filename;
if ( ! profile->doc->LitAttrString( elem_image, "file", image_filename) )
{
// TODO raise an "resource is malformed in the profile" exception
throw std::string("RessourceManager: can't load (sprite) resource ")+resource_name;
return NULL;
}
// TODO load more properties in xml : alpha, colorkey....
// By now force alpha and no colorkey
bool alpha = true;
Sprite *sprite = new Sprite();
sprite->surface = LoadImage( profile->relative_path+image_filename, alpha);
xmlpp::Element *elem_grid = profile->doc->AccesBalise ( elem_image, "grid");
if ( elem_grid == NULL )
{
// No grid element, Load the Sprite like a normal image
sprite->nb_frames = 1;
sprite->frame_width_pix = sprite->surface->w;
sprite->frame_height_pix = sprite->surface->h;
#ifdef DEBUG
std::cout << "RessourceManager: sprite ressource \"" << resource_name << "\" has no grid element" << std::endl;
#endif
}
else
{
std::string size;
if ( ! profile->doc->LitAttrString( elem_grid, "size", size) )
{
// TODO raise an "resource is malformed in the profile" exception
throw std::string("RessourceManager: can't load sprite resource \"")+resource_name+"\" has no attribute size";
return NULL;
}
if ( size.find(",") != size.npos)
{
sprite->frame_width_pix = atoi( (size.substr(0,size.find(","))).c_str());
sprite->frame_height_pix = atoi( (size.substr(size.find(",")+1,size.length())).c_str());
}
else
{
throw std::string("RessourceManager: can't load sprite resource \"")+resource_name+"\" has malformed size attribute";
return NULL;
}
std::string array;
if ( ! profile->doc->LitAttrString( elem_grid, "array", array) )
{
// TODO raise an "resource is malformed in the profile" exception
throw std::string("RessourceManager: can't load sprite resource \"")+resource_name+"\" has no attribute array";
return NULL;
}
if ( array.find(",") != array.npos)
{
sprite->nb_frames = atoi( (array.substr(0,array.find(","))).c_str());
if ( sprite->nb_frames <= 0 )
sprite->nb_frames = 1;
}
else
{
throw std::string("RessourceManager: can't load (sprite) resource ")+resource_name;
return NULL;
}
/* en exemple....
<sprite name="walking">
<image file="poulpe-anim.png">
<grid pos="0,0" size="42,42" array="10,1" />
</image>
<collision_rect dx="10" top="0" bottom="8" />
<wormux repetition="2" />
*/
}
return sprite;
}
RessourceManager ressource_manager;
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Ressource Manager: Load resources (images/sprites) suitable for SDL
* Load directly or from refernece in xml resource profile
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
*
* TODO: Keep reference to resources, better exceptions
*****************************************************************************/
#ifndef _RESSOURCE_MANAGER_H
#define _RESSOURCE_MANAGER_H
#include <vector>
#include <string>
#include <map>
#include <SDL.h>
#include "xml_document.h"
struct Profile;
class Sprite;
struct Element;
class RessourceManager
{
public:
RessourceManager();
~RessourceManager();
void AddDataPath( std::string base_path);
SDL_Surface *LoadImage( std::string ressource_str, bool alpha = false, bool set_colorkey = false, Uint32 colorkey = 0);
Profile *LoadXMLProfile( std::string xml_filename);
Profile *UnLoadXMLProfile( Profile *profile);
SDL_Surface *LoadImage( Profile *profile, std::string resource_name);
Sprite *LoadSprite( Profile *profile, std::string resource_name);
private:
std::string base_path;
// std::vector< std::string> data_pathes;
xmlpp::Element * GetElement( Profile *profile, std::string ressource_type, std::string ressource_name);
// map < string, void *> ressources;
// std::map< std::string, DataFile> index;
// unsigned int start_of_data;
};
extern RessourceManager ressource_manager;
#endif /* _RESSOURCE_MANAGER_H */
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Sprite: Simple sprite management
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
* TODO: Scale,Rotation...
*****************************************************************************/
#include "sprite.h"
#include <SDL.h>
Sprite::Sprite()
{
surface = NULL;
frame_width_pix = 0;
frame_height_pix = 0;
nb_frames = 0;
scale_x = 1.0f;
scale_y = 1.0f;
rotation_deg = 0.0f;
}
Sprite::~Sprite()
{
if ( surface )
SDL_FreeSurface( surface);
}
unsigned int Sprite::GetWidth()
{
return frame_width_pix;
}
unsigned int Sprite::GetHeight()
{
return frame_height_pix;
}
unsigned int Sprite::GetFrameCount()
{
return nb_frames;
}
void Sprite::SetCurrentFrame( unsigned int frame_no)
{
current_frame = ( frame_no < nb_frames ) ? frame_no : nb_frames-1;
}
unsigned int Sprite::GetCurrentFrame()
{
return current_frame;
}
void Sprite::Scale( float scale_x, float scale_y)
{
this->scale_x = scale_x;
this->scale_y = scale_y;
}
void Sprite::GetScaleFactors( float &scale_x, float &scale_y)
{
scale_x = this->scale_x;
scale_y = this->scale_y;
}
void Sprite::SetAlpha( float alpha)
{
this->alpha = alpha;
}
float Sprite::GetAlpha()
{
return alpha;
}
void Sprite::Start()
{
current_frame = 0;
}
void Sprite::StartLoop()
{
current_frame = 0;
}
void Sprite::Finish()
{
current_frame = nb_frames-1;
}
void Sprite::Blit( SDL_Surface *dest, unsigned int pox_x, unsigned int pos_y)
{
//current_frame;
SDL_Rect sr = { current_frame*frame_width_pix, 0, frame_width_pix, frame_height_pix};
SDL_Rect dr = { pox_x, pos_y, frame_width_pix, frame_height_pix};
SDL_BlitSurface( surface, &sr, dest, &dr);
}
void Sprite::Update()
{
current_frame = ( current_frame + 1 ) % nb_frames;
}
/******************************************************************************
* Wormux, a free clone of the game Worms from Team17.
* Copyright (C) 2001-2004 Lawrence Azzoug.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Sprite: Simple sprite management
******************************************************************************
* 2005/09/21: Jean-Christophe Duberga ([EMAIL PROTECTED])
* Initial version
* TODO: Scale,Rotation...
*****************************************************************************/
#ifndef _SPRITE_H
#define _SPRITE_H
struct SDL_Surface;
class RessourceManager;
class Sprite
{
public:
Sprite();
~Sprite();
// Get physical characterisics
unsigned int GetWidth();
unsigned int GetHeight();
unsigned int GetFrameCount();
// Get/Set sprite parameters
void SetCurrentFrame( unsigned int frame_no);
unsigned int GetCurrentFrame();
void Scale( float scale_x, float scale_y);
void GetScaleFactors( float &scale_x, float &scale_y);
void SetAlpha( float alpha); // Can't be combined with per pixel alpha
float GetAlpha();
void Start();
void StartLoop();
void Finish();
void Blit( SDL_Surface *dest, unsigned int pox_x, unsigned int pos_y);
void Update();
private:
SDL_Surface *surface;
int frame_width_pix;
int frame_height_pix;
int nb_frames;
float scale_x;
float scale_y;
float rotation_deg;
float alpha;
int current_frame;
friend class RessourceManager;
};
#endif /* _SPRITE_H */