22 |
|
|
23 |
#include <vector> |
#include <vector> |
24 |
#include <string> |
#include <string> |
25 |
|
#include <cassert> |
26 |
|
|
27 |
|
namespace display { |
28 |
|
|
29 |
|
class ActiveModel { |
30 |
|
public: |
31 |
|
virtual void tick (double dtime) = 0; |
32 |
|
virtual void move (int x, int y) = 0; |
33 |
|
}; |
34 |
|
|
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
//---------------------------------------- |
//---------------------------------------- |
39 |
// Image |
// Image |
46 |
class Image { |
class Image { |
47 |
public: |
public: |
48 |
// Constructors. |
// Constructors. |
49 |
Image(px::Surface *i, int xoff=0, int yoff=0) |
Image(px::Surface *sfc) |
50 |
: image(i), rect(), refcount(1) |
: surface(sfc), rect(surface->size()), refcount(1) |
51 |
{ |
{} |
52 |
rect.w = i->width(); |
|
53 |
rect.h = i->height(); |
Image(px::Surface *sfc, const px::Rect &r) |
54 |
this->xoff = xoff; |
: surface(sfc), rect(r), refcount(1) |
55 |
this->yoff = yoff; |
{} |
|
} |
|
|
Image(px::Surface *i, px::Rect r, int xoff=0, int yoff=0) |
|
|
: image(i), rect(r), refcount(1) |
|
|
{ |
|
|
this->xoff = xoff; |
|
|
this->yoff = yoff; |
|
|
} |
|
56 |
|
|
57 |
// Destructor. |
// Destructor. |
58 |
~Image() { |
~Image() { |
59 |
// `image' need not be deleted; this is handled in the |
// `surface' need not be deleted; this is handled in the |
60 |
// global image cache. |
// global image cache. |
61 |
} |
} |
62 |
|
|
65 |
void decref() {if (--refcount == 0) delete this;} |
void decref() {if (--refcount == 0) delete this;} |
66 |
|
|
67 |
void draw(px::GC &gc, int x, int y) { |
void draw(px::GC &gc, int x, int y) { |
68 |
blit(gc, x+xoff, y+yoff, image, rect); |
blit(gc, x, y, surface, rect); |
69 |
} |
} |
70 |
|
|
71 |
// Variables. |
// Variables. |
72 |
px::Surface *image; |
px::Surface *surface; |
73 |
px::Rect rect; // location of image inside surface |
px::Rect rect; // location of image inside surface |
74 |
int refcount; |
int refcount; |
|
int xoff, yoff; // relative origin of the image |
|
75 |
}; |
}; |
76 |
|
|
77 |
//---------------------------------------- |
//---------------------------------------- |
80 |
|
|
81 |
class ImageModel : public Model { |
class ImageModel : public Model { |
82 |
Image* image; |
Image* image; |
83 |
|
int xoff, yoff; // relative origin of the image |
84 |
public: |
public: |
85 |
ImageModel(Image *i) : image(i) { |
ImageModel(Image *i, int xo, int yo) |
86 |
|
: image(i), xoff(xo), yoff(yo) { |
87 |
image->incref(); |
image->incref(); |
88 |
} |
} |
89 |
ImageModel(Surface *s, int xoff, int yoff) |
ImageModel(Surface *s, int xo, int yo) |
90 |
: image(new Image(s, xoff, yoff)) |
: image(new Image(s)), xoff(xo), yoff(yo) |
91 |
{} |
{} |
92 |
ImageModel(Surface *s, const Rect &r, int xoff, int yoff) |
ImageModel(Surface *s, const Rect &r, int xo, int yo) |
93 |
: image(new Image(s, r, xoff, yoff)) |
: image(new Image(s, r)), xoff(xo), yoff(yo) |
94 |
{} |
{} |
95 |
~ImageModel() { image->decref(); } |
~ImageModel() { image->decref(); } |
96 |
void draw(px::GC &gc, int x, int y) |
void draw(px::GC &gc, int x, int y) |
97 |
{ |
{ |
98 |
assert(image); |
assert(image); |
99 |
image->draw(gc, x, y); |
image->draw(gc, x+xoff, y+yoff); |
100 |
} |
} |
101 |
|
|
102 |
Image *get_image() { return image; } |
Image *get_image() { return image; } |
103 |
|
|
104 |
void draw_shadow(px::GC &gc, int x, int y) {} |
void draw_shadow(px::GC &gc, int x, int y) {} |
105 |
Model *clone() { return new ImageModel(image); } |
Model *clone() { return new ImageModel(image, xoff, yoff); } |
106 |
}; |
}; |
107 |
|
|
108 |
//---------------------------------------- |
//---------------------------------------- |
220 |
class AnimFrame : public px::Nocopy { |
class AnimFrame : public px::Nocopy { |
221 |
public: |
public: |
222 |
AnimFrame(Model *m, double dur) |
AnimFrame(Model *m, double dur) |
223 |
: model(m), duration(dur) |
: model(m), duration(dur) |
224 |
{} |
{} |
225 |
~AnimFrame() { delete model; } |
~AnimFrame() { delete model; } |
226 |
Model *model; |
Model *model; |
253 |
void reverse() { reversep = !reversep; } |
void reverse() { reversep = !reversep; } |
254 |
|
|
255 |
void expose (ModelLayer *ml, const px::V2 &pos); |
void expose (ModelLayer *ml, const px::V2 &pos); |
256 |
|
void update (ModelLayer *ml, double dtime) { |
257 |
|
tick (dtime); |
258 |
|
Rect r; |
259 |
|
if (has_changed(r)) { |
260 |
|
ml->mark_redraw_area (r); |
261 |
|
} |
262 |
|
} |
263 |
void remove (ModelLayer *ml); |
void remove (ModelLayer *ml); |
264 |
|
|
265 |
void tick(double dtime); |
void tick(double dtime); |