[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Enigma-cvs] enigma/src d_models.hh, 1.11, 1.12 d_models.cc, 1.15, 1.16
From: |
Daniel Heck <address@hidden> |
Subject: |
[Enigma-cvs] enigma/src d_models.hh, 1.11, 1.12 d_models.cc, 1.15, 1.16 |
Date: |
Sun, 16 Nov 2003 19:16:11 +0000 |
Update of /cvsroot/enigma/enigma/src
In directory subversions:/tmp/cvs-serv2917/src
Modified Files:
d_models.hh d_models.cc
Log Message:
* src/d_models.cc: Adapt to px::Cache changes. Disabled
ReloadModels(). Added CropSurface(), GetSurface(),
RegisterImage(), LookupImage().
* src/d_models.hh: Added CropSurface(), GetSurface(),
RegisterImage(), LookupImage(), DefineImageModel().
Index: d_models.hh
===================================================================
RCS file: /cvsroot/enigma/enigma/src/d_models.hh,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** d_models.hh 3 Jul 2003 21:46:32 -0000 1.11
--- d_models.hh 16 Nov 2003 19:16:08 -0000 1.12
***************
*** 21,60 ****
#define D_MODELS_HH
#include <vector>
#include <string>
#include <cassert>
- //----------------------------------------
- // Image
- //----------------------------------------
namespace display
{
using std::string;
using std::vector;
class Image {
public:
// Constructors.
! Image(px::Surface *sfc)
! : surface(sfc), rect(surface->size()), refcount(1)
! {}
! Image(px::Surface *sfc, const px::Rect &r)
! : surface(sfc), rect(r), refcount(1)
! {}
! // Destructor.
! ~Image() {
! // `surface' need not be deleted; this is handled in the
! // global image cache.
! }
- // Functions.
void incref() { ++refcount; }
void decref() {if (--refcount == 0) delete this;}
! void draw(px::GC &gc, int x, int y) {
! blit(gc, x, y, surface, rect);
! }
// Variables.
--- 21,53 ----
#define D_MODELS_HH
+ #include "px/pxfwd.hh"
#include <vector>
#include <string>
#include <cassert>
namespace display
{
using std::string;
using std::vector;
+ using px::Surface;
+
+ /* -------------------- Image -------------------- */
class Image {
public:
// Constructors.
! Image(px::Surface *sfc);
! Image(px::Surface *sfc, const px::Rect &r);
! ~Image();
! // Functions.
! Image *duplicate();
! void overlay (Image *other);
void incref() { ++refcount; }
void decref() {if (--refcount == 0) delete this;}
! void draw(px::GC &gc, int x, int y);
// Variables.
***************
*** 64,70 ****
};
! //----------------------------------------
! // ImageModel
! //----------------------------------------
class ImageModel : public Model {
--- 57,61 ----
};
! /* -------------------- ImageModel -------------------- */
class ImageModel : public Model {
***************
*** 79,83 ****
: image(new Image(s)), xoff(xo), yoff(yo)
{}
! ImageModel(Surface *s, const Rect &r, int xo, int yo)
: image(new Image(s, r)), xoff(xo), yoff(yo)
{}
--- 70,74 ----
: image(new Image(s)), xoff(xo), yoff(yo)
{}
! ImageModel(Surface *s, const px::Rect &r, int xo, int yo)
: image(new Image(s, r)), xoff(xo), yoff(yo)
{}
***************
*** 94,100 ****
};
! //----------------------------------------
! // ShadowModel
! //----------------------------------------
class ShadowModel : public Model {
public:
--- 85,90 ----
};
! /* -------------------- ShadowModel -------------------- */
!
class ShadowModel : public Model {
public:
***************
*** 130,141 ****
};
! //----------------------------------------------------------------------
! // CompositeModel
! //
! // This kind of model consists of two layers: one background layer and
! // some foreground layer that is drawn over the background. Both
! // layers may be animated. This model make it easy to compose complex
! // animations from simple ones; see models-2d.lua for a few examples.
! //----------------------------------------------------------------------
class CompositeModel : public Model {
Model *bg, *fg;
--- 120,125 ----
};
! /* -------------------- CompositeModel -------------------- */
!
class CompositeModel : public Model {
Model *bg, *fg;
***************
*** 178,196 ****
};
! //----------------------------------------
! // RandomModel
! //----------------------------------------
class RandomModel : public Model {
std::vector<std::string> modelnames;
public:
void add_model(const std::string &name) {modelnames.push_back(name);}
-
- // Model interface
Model *clone();
};
! //----------------------------------------
! // Alias
! //----------------------------------------
class AliasModel : public Model {
string name;
--- 162,177 ----
};
! /* -------------------- RandomModel -------------------- */
!
! /* Creates new models randomly from a set of template models. */
class RandomModel : public Model {
std::vector<std::string> modelnames;
public:
void add_model(const std::string &name) {modelnames.push_back(name);}
Model *clone();
};
! /* -------------------- AliasModel -------------------- */
!
class AliasModel : public Model {
string name;
***************
*** 200,206 ****
};
! //----------------------------------------
! // Animation
! //----------------------------------------
class AnimFrame : public px::Nocopy {
public:
--- 181,186 ----
};
! /* -------------------- Animations -------------------- */
!
class AnimFrame : public px::Nocopy {
public:
***************
*** 239,253 ****
void expose (ModelLayer *ml, const px::V2 &pos);
! void update (ModelLayer *ml, double dtime) {
! tick (dtime);
! Rect r;
! if (has_changed(r)) {
! ml->mark_redraw_area (r);
! }
! }
void remove (ModelLayer *ml);
void tick(double dtime);
! bool has_changed(Rect &changed_region);
bool is_garbage() const { return finishedp; }
--- 219,227 ----
void expose (ModelLayer *ml, const px::V2 &pos);
! void update (ModelLayer *ml, double dtime);
void remove (ModelLayer *ml);
void tick(double dtime);
! bool has_changed(px::Rect &changed_region);
bool is_garbage() const { return finishedp; }
***************
*** 263,267 ****
*/
AnimRep *rep;
! unsigned curframe; // Current frame number.
double frametime; // Elapsed time since frame was activated
bool finishedp; // Animation has finished
--- 237,241 ----
*/
AnimRep *rep;
! unsigned curframe; // Current frame number
double frametime; // Elapsed time since frame was activated
bool finishedp; // Animation has finished
***************
*** 273,276 ****
--- 247,255 ----
ModelCallback *callback;
};
+
+ px::Surface *GetSurface (const std::string &filename);
+ px::Surface *CropSurface (const px::Surface *s, px::Rect r);
+ void DefineModel (const char *name, Model *m);
+ void DefineImageModel (const char *name, px::Surface *s);
}
Index: d_models.cc
===================================================================
RCS file: /cvsroot/enigma/enigma/src/d_models.cc,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** d_models.cc 24 Oct 2003 14:55:19 -0000 1.15
--- d_models.cc 16 Nov 2003 19:16:08 -0000 1.16
***************
*** 47,115 ****
#include "px-lua.hh"
! //----------------------------------------
! // Image cache
! //----------------------------------------
namespace
{
! class SurfaceAlloc_Alpha {
public:
Surface *acquire(const std::string &name) {
return enigma::LoadImage(name.c_str());
}
- void release(Surface *s) { delete s; }
};
! class SurfaceAlloc {
public:
! Surface * acquire(const std::string &name) {
! string fname = enigma::FindDataFile(string("gfx/") + name+".png");
! SDL_Surface *s = IMG_Load(fname.c_str());
! if (s) {
! SDL_Surface *img = 0;
! if (options::BitsPerPixel == 8) {
! SDL_SetColorKey(s, SDL_SRCCOLORKEY|SDL_RLEACCEL,
! SDL_MapRGB(s->format, 255,0,255));
! img = SDL_DisplayFormat(s);
! } else {
! if (s->flags & SDL_SRCALPHA) {
! img = SDL_DisplayFormatAlpha(s);
! } else {
! SDL_SetColorKey(s, SDL_SRCCOLORKEY|SDL_RLEACCEL,
! SDL_MapRGB(s->format, 255,0,255));
! img = SDL_DisplayFormat(s);
! }
! }
! if (img) {
! SDL_FreeSurface(s);
! return Surface::make_surface(img);
! }
! return Surface::make_surface(s);
! }
! return 0;
! }
! void release(Surface *s) {delete s;}
};
-
-
- typedef Cache<Surface*, SurfaceAlloc> SurfaceCache;
- typedef Cache<Surface*, SurfaceAlloc_Alpha> SurfaceCache_Alpha;
- }
-
- //======================================================================
- // DEFINING AND CREATING MODELS
- //======================================================================
-
- namespace
- {
- SurfaceCache surface_cache;
- SurfaceCache_Alpha surface_cache_alpha;
-
- vector<Surface *> image_pile;
- }
-
- namespace
- {
class ModelManager {
public:
--- 47,66 ----
#include "px-lua.hh"
! /* -------------------- Types -------------------- */
namespace
{
! class SurfaceCache_Alpha : public PtrCache<Surface> {
public:
Surface *acquire(const std::string &name) {
return enigma::LoadImage(name.c_str());
}
};
! class SurfaceCache : public PtrCache<Surface> {
public:
! Surface * acquire(const std::string &name);
};
class ModelManager {
public:
***************
*** 138,163 ****
};
ModelManager *modelmgr = 0;
}
ModelManager::ModelManager()
: m_templates (1069)
{}
! ModelManager::~ModelManager()
! {
delete_map (m_templates.begin(), m_templates.end());
delete_sequence (image_pile.begin(), image_pile.end());
}
! void
! ModelManager::define (const std::string name, Model *m)
! {
m_templates.insert (name, m);
}
! Model *
! ModelManager::create (const std::string &name)
! {
ModelMap::iterator i = m_templates.find(name);
--- 89,148 ----
};
+ }
+
+ /* -------------------- Variables -------------------- */
+
+ namespace
+ {
+ SurfaceCache surface_cache;
+ SurfaceCache_Alpha surface_cache_alpha;
ModelManager *modelmgr = 0;
+
+ vector<Surface *> image_pile;
+
+ string anim_templ_name;
+ Anim2d *anim_templ = 0;
}
+
+ /* -------------------- SurfaceCache -------------------- */
+
+ Surface *SurfaceCache::acquire(const std::string &name) {
+ string fname = enigma::FindDataFile(string("gfx/") + name+".png");
+ SDL_Surface *s = IMG_Load(fname.c_str());
+ if (s) {
+ SDL_Surface *img = 0;
+ if (s->flags & SDL_SRCALPHA) {
+ img = SDL_DisplayFormatAlpha(s);
+ } else {
+ SDL_SetColorKey(s, SDL_SRCCOLORKEY, //|SDL_RLEACCEL,
+ SDL_MapRGB(s->format, 255,0,255));
+ img = SDL_DisplayFormat(s);
+ }
+ if (img) {
+ SDL_FreeSurface(s);
+ return Surface::make_surface(img);
+ }
+ return Surface::make_surface(s);
+ }
+ return 0;
+ }
+
+ /* -------------------- ModelManager -------------------- */
+
ModelManager::ModelManager()
: m_templates (1069)
{}
! ModelManager::~ModelManager() {
delete_map (m_templates.begin(), m_templates.end());
delete_sequence (image_pile.begin(), image_pile.end());
}
! void ModelManager::define (const std::string name, Model *m) {
m_templates.insert (name, m);
}
! Model * ModelManager::create (const std::string &name) {
ModelMap::iterator i = m_templates.find(name);
***************
*** 168,174 ****
}
! void
! ModelManager::remove (const std::string &name)
! {
ModelMap::iterator i = m_templates.find(name);
if (i != m_templates.end()) {
--- 153,157 ----
}
! void ModelManager::remove (const std::string &name) {
ModelMap::iterator i = m_templates.find(name);
if (i != m_templates.end()) {
***************
*** 178,188 ****
}
! //----------------------------------------------------------------------
! // MODELS
! //----------------------------------------------------------------------
!
! void display::InitModels()
! {
modelmgr = new ModelManager;
--- 161,168 ----
}
+
+ /* -------------------- Functions -------------------- */
! void display::InitModels() {
modelmgr = new ModelManager;
***************
*** 206,228 ****
}
! void display::ShutdownModels()
! {
delete modelmgr;
}
! void display::ReloadModels()
! {
! NewWorld(0,0);
! surface_cache.clear();
! surface_cache_alpha.clear();
! delete modelmgr;
! modelmgr = new ModelManager;
! lua::Dofile("models-2d.lua");
}
/* Register a new model template `m' under the name `name'. */
! void
! display::DefineModel (const char *name, Model *m)
! {
if (modelmgr->has_model (name)) {
enigma::Log << "Redefining model '" << name << "'\n";
--- 186,208 ----
}
! void display::ShutdownModels() {
delete modelmgr;
}
! void display::ReloadModels() {
! // NewWorld(0,0);
! // surface_cache.clear();
! // surface_cache_alpha.clear();
! // delete modelmgr;
! // modelmgr = new ModelManager;
! // lua::Dofile("models-2d.lua");
! }
!
! Surface *display::CropSurface (const Surface *s, Rect r) {
! return px::Grab(s, r);
}
/* Register a new model template `m' under the name `name'. */
! void display::DefineModel (const char *name, Model *m) {
if (modelmgr->has_model (name)) {
enigma::Log << "Redefining model '" << name << "'\n";
***************
*** 232,238 ****
}
! Model *
! display::MakeModel (const string &name)
! {
if (Model *m = modelmgr->create (name))
return m;
--- 212,216 ----
}
! Model * display::MakeModel (const string &name) {
if (Model *m = modelmgr->create (name))
return m;
***************
*** 243,250 ****
}
!
! void
! display::DefineImage(const char *name, const char *fname,
! int xoff, int yoff)
{
if (px::Surface *sfc = surface_cache.get(fname))
--- 221,226 ----
}
! void display::DefineImage(const char *name, const char *fname,
! int xoff, int yoff)
{
if (px::Surface *sfc = surface_cache.get(fname))
***************
*** 254,260 ****
}
! void
! display::DefineSubImage(const char *name, const char *fname,
! int xoff, int yoff, px::Rect subrect)
{
if (px::Surface *sfc = surface_cache.get(fname))
--- 230,240 ----
}
! void display::DefineImageModel (const char *name, px::Surface *s)
! {
! DefineModel (name, new ImageModel(s, 0, 0));
! }
!
! void display::DefineSubImage(const char *name, const char *fname,
! int xoff, int yoff, px::Rect subrect)
{
if (px::Surface *sfc = surface_cache.get(fname))
***************
*** 264,269 ****
}
! void
! display::DefineRandModel(const char *name, int n, const char **names)
{
RandomModel *m = new RandomModel();
--- 244,248 ----
}
! void display::DefineRandModel(const char *name, int n, const char **names)
{
RandomModel *m = new RandomModel();
***************
*** 273,278 ****
}
! void
! display::DefineShadedModel (
const char *name,
const char *model,
--- 252,256 ----
}
! void display::DefineShadedModel (
const char *name,
const char *model,
***************
*** 287,296 ****
`images' is the name of the background image, the following images are
drawn on top of it. */
! void
! display::DefineOverlayImage (const char *name, int n, const char **images)
{
Surface *sfc = Duplicate(surface_cache.get(images[0]));
! if (sfc)
! {
GC gc(sfc);
for (int i=1; i<n; i++)
--- 265,273 ----
`images' is the name of the background image, the following images are
drawn on top of it. */
! void display::DefineOverlayImage (const char *name, int n,
! const char **images)
{
Surface *sfc = Duplicate(surface_cache.get(images[0]));
! if (sfc) {
GC gc(sfc);
for (int i=1; i<n; i++)
***************
*** 301,308 ****
}
! void
! display::DefineComposite (const char *name,
! const char *bgname,
! const char *fgname)
{
DefineModel(name, new CompositeModel(MakeModel(bgname),
--- 278,284 ----
}
! void display::DefineComposite (const char *name,
! const char *bgname,
! const char *fgname)
{
DefineModel(name, new CompositeModel(MakeModel(bgname),
***************
*** 310,322 ****
}
! namespace
! {
! string anim_templ_name;
! Anim2d *anim_templ = 0;
! }
!
! void
! display::DefineAnim (const char *name, bool loop_p)
! {
anim_templ = new Anim2d(loop_p);
DefineModel(name, anim_templ);
--- 286,290 ----
}
! void display::DefineAnim (const char *name, bool loop_p) {
anim_templ = new Anim2d(loop_p);
DefineModel(name, anim_templ);
***************
*** 324,329 ****
}
! void
! display::AddFrame (const char *name, const char *model, double time)
{
if (anim_templ_name != name)
--- 292,296 ----
}
! void display::AddFrame (const char *name, const char *model, double time)
{
if (anim_templ_name != name)
***************
*** 334,339 ****
}
! void
! display::DefineAlias (const char *name, const char *othername)
{
DefineModel(name, new AliasModel(othername));
--- 301,305 ----
}
! void display::DefineAlias (const char *name, const char *othername)
{
DefineModel(name, new AliasModel(othername));
***************
*** 342,351 ****
! //======================================================================
! // MODEL IMPLEMENTATION
! //======================================================================
! Model *
! RandomModel::clone()
! {
if (!modelnames.empty()) {
int r = enigma::IntegerRand(0, modelnames.size()-1);
--- 308,343 ----
! /* -------------------- Image -------------------- */
!
! Image::Image(px::Surface *sfc)
! : surface(sfc), rect(surface->size()), refcount(1)
! {}
!
! Image::Image(px::Surface *sfc, const px::Rect &r)
! : surface(sfc), rect(r), refcount(1)
! {}
!
! // Destructor.
! Image::~Image() {
! // `surface' need not be deleted; this is handled in the
! // global image cache.
! }
!
! void Image::draw(px::GC &gc, int x, int y) {
! blit(gc, x, y, surface, rect);
! }
!
! Image *Image::duplicate() {
! return 0;
! }
!
! void Image::overlay (Image *other) {
!
! }
!
!
! /* -------------------- RandomModel -------------------- */
!
! Model * RandomModel::clone() {
if (!modelnames.empty()) {
int r = enigma::IntegerRand(0, modelnames.size()-1);
***************
*** 357,366 ****
}
! Model *
! AliasModel::clone()
! {
return MakeModel(name);
}
Anim2d::Anim2d(AnimRep *r)
: rep(r), curframe(0), frametime(0),
--- 349,360 ----
}
! /* -------------------- RandomModel -------------------- */
!
! Model * AliasModel::clone() {
return MakeModel(name);
}
+ /* -------------------- Anim2d -------------------- */
+
Anim2d::Anim2d(AnimRep *r)
: rep(r), curframe(0), frametime(0),
***************
*** 372,388 ****
}
! Anim2d::~Anim2d()
! {
if (--rep->refcount == 0)
delete rep;
}
! void Anim2d::add_frame(Model *m, double duration)
! {
rep->frames.push_back(new AnimFrame(m, duration));
}
! void Anim2d::draw(px::GC &gc, int x, int y)
! {
if (!finishedp) {
AnimFrame *f =rep->frames[curframe];
--- 366,379 ----
}
! Anim2d::~Anim2d() {
if (--rep->refcount == 0)
delete rep;
}
! void Anim2d::add_frame(Model *m, double duration) {
rep->frames.push_back(new AnimFrame(m, duration));
}
! void Anim2d::draw(px::GC &gc, int x, int y) {
if (!finishedp) {
AnimFrame *f =rep->frames[curframe];
***************
*** 392,398 ****
}
! void
! Anim2d::draw_shadow (px::GC &gc, int x, int y)
! {
if (!finishedp) {
AnimFrame *f =rep->frames[curframe];
--- 383,387 ----
}
! void Anim2d::draw_shadow (px::GC &gc, int x, int y) {
if (!finishedp) {
AnimFrame *f =rep->frames[curframe];
***************
*** 401,407 ****
}
! void
! Anim2d::expose (ModelLayer *ml, const px::V2 &pos)
! {
ml->activate (this);
posx = pos[0];
--- 390,394 ----
}
! void Anim2d::expose (ModelLayer *ml, const px::V2 &pos) {
ml->activate (this);
posx = pos[0];
***************
*** 410,424 ****
}
! void
! Anim2d::remove (ModelLayer *ml)
! {
ml->deactivate (this);
}
! bool
! Anim2d::has_changed(Rect &changed_region)
! {
! if (changedp)
! {
if (is_sprite)
changed_region = Rect(int(posx-1), int(posy-1), 3, 3);
--- 397,406 ----
}
! void Anim2d::remove (ModelLayer *ml) {
ml->deactivate (this);
}
! bool Anim2d::has_changed(Rect &changed_region) {
! if (changedp) {
if (is_sprite)
changed_region = Rect(int(posx-1), int(posy-1), 3, 3);
***************
*** 431,437 ****
}
! void
! Anim2d::tick (double dtime)
! {
assert(curframe < rep->frames.size());
frametime += dtime;
--- 413,417 ----
}
! void Anim2d::tick (double dtime) {
assert(curframe < rep->frames.size());
frametime += dtime;
***************
*** 461,463 ****
--- 441,472 ----
callback->animcb();
}
+ }
+
+ void Anim2d::update (ModelLayer *ml, double dtime) {
+ tick (dtime);
+ px::Rect r;
+ if (has_changed(r)) {
+ ml->mark_redraw_area (r);
+ }
+ }
+
+
+ /* -------------------- Functions -------------------- */
+
+ namespace display
+ {
+ Surface *GetSurface (const std::string &filename)
+ {
+ return surface_cache.get(filename);
+ }
+
+ void RegisterImage (const std::string &name, Image *img)
+ {
+ }
+
+ Image *LookupImage (const std::string &name)
+ {
+ return 0;
+ }
+
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Enigma-cvs] enigma/src d_models.hh, 1.11, 1.12 d_models.cc, 1.15, 1.16,
Daniel Heck <address@hidden> <=