Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

cvideo.h

Go to the documentation of this file.
00001 #ifndef CVIDEO_H
00002 #define CVIDEO_H
00003 
00011 #include "globals.h"
00012 
00013 #include "csingletonmanager.h"
00014 
00015 #include <list>
00016 #include <assert.h>
00017 #include <stdio.h>
00018 
00020 typedef unsigned char uint8;
00021 
00023 typedef unsigned long uint32;
00024 
00032 class CPoint {
00033 public:
00035   int x, y;
00036 
00037   /* Constructors... */
00038   CPoint() : x(0), y(0) { }
00039   CPoint(int x, int y) : x(x), y(y) { }
00040 
00042   void set(int x, int y) { this->x = x; this->y = y; }
00043 
00045   CPoint &operator+=(CPoint &point) {
00046     x += point.x;
00047     y += point.y;
00048     return *this;
00049   }
00050 
00052   friend const CPoint operator+(const CPoint &left, const CPoint &right);
00053 
00055   int operator==(const CPoint &right) const {
00056     return (x == right.x && y == right.y);
00057   }
00058 
00060   int operator!=(const CPoint &right) const {
00061     return !(x == right.x && y == right.y);
00062   }
00063 };
00064 
00069 class CRect {
00070 public:
00072   CPoint pos;
00073 
00076   CPoint size;
00077 
00078   CRect() { }
00079 
00082   CRect(int x, int y, int w, int h) { set(x, y, w, h); }
00083 
00085   void set(int x, int y, int w, int h) {
00086     pos.x = x;
00087     pos.y = y;
00088     size.x = w;
00089     size.y = h;
00090   }
00091 
00093   void set(CPoint &pos, CPoint &size) { this->pos = pos; this->size = size; }
00094 
00097   int x1() const { return this->pos.x; }
00098 
00101   int y1() const { return this->pos.y; }
00102 
00105   int x2() const { return this->pos.x + this->size.x; }
00106 
00109   int y2() const { return this->pos.y + this->size.y; }
00110 
00113   int intersects_with(const CRect &r) const {
00114     return ( (this->x2() >= r.x1()) &&
00115               (r.x2() >= this->x1()) &&
00116               (this->y2() >= r.y1()) &&
00117               (r.y2() >= this->y1()) );
00118   }
00119 
00121   int operator==(const CRect &right) const {
00122     return (pos == right.pos && size == right.size);
00123   }
00124 
00126   int operator!=(const CRect &right) const {
00127     return (pos != right.pos || size != right.size);
00128   }
00129 };
00130 
00132 typedef std::list<CRect> CRectList;
00133 
00134 class CColor;
00135 class CSurfaceImpl;
00136 class CScreen;
00137 
00142 class CVideoSystem {
00143 private:
00144   /* Outlaw copy constructor and '=' operator. */
00145   CVideoSystem(CVideoSystem &);
00146   CVideoSystem &operator=(CVideoSystem &);
00147 
00148 protected:
00149   friend class CColor;
00150   friend class CSurface;
00151   friend class CSurfaceImpl;
00152 
00155   virtual CSurfaceImpl *make_csurface_impl(int w, int h) = 0;
00156   
00160   virtual CSurfaceImpl *make_csurface_impl(const char *filename) = 0;
00161 
00165   virtual uint32 make_color(uint8 r, uint8 g, uint8 b) = 0;
00166 
00167  public:
00168   CVideoSystem() { }
00169   virtual ~CVideoSystem() { }
00170 
00177   virtual CScreen &get_screen() = 0;
00178 };
00179 
00181 class CVideoSystemManager : public CSingletonManager<CVideoSystem> { };
00182 
00184 typedef CSingletonFactory<CVideoSystem> CVideoSystemFactory;
00185 
00198 class CColor {
00199 private:
00200   /* The surface-specific color value. */
00201   uint32 color;
00202 public:
00207   CColor(uint32 n_color = 0) : color(n_color) { }
00208 
00212   CColor(uint8 r, uint8 g, uint8 b) { color = CVideoSystemManager::get_instance().make_color(r,g,b); }
00213 
00215   operator uint32 () const { return color; }
00216 };
00217 
00229 class CSurfaceImpl {
00230 public:
00233   virtual ~CSurfaceImpl() { }
00234 
00237   virtual void inc_ref_count() = 0;
00238 
00240   virtual void dec_ref_count() = 0;
00241 
00243   virtual int is_empty() = 0;
00244 
00246   virtual void set_transparent_color(CColor color) = 0;
00247 
00249   virtual void blit(CSurfaceImpl &source) = 0;
00250 
00253   virtual void blit(CSurfaceImpl &source, CPoint &dest_pos) = 0;
00254 
00257   virtual void blit(CSurfaceImpl &source, CPoint &dest_pos, CRect &src_rect) = 0;
00258   
00260   virtual void fill_rect(const CRect &rect, CColor color) = 0;
00261 
00263   virtual void set_clipping_rect(CRect &rect) = 0;
00264 
00266   virtual void clear_clipping_rect() = 0;
00267 
00269   virtual int get_depth() = 0;
00270 
00272   virtual int get_width() = 0;
00273 
00275   virtual int get_height() = 0;
00276 
00280   virtual void set_video_mode(int width, int height, int depth, int fullscreen) = 0;
00281 
00285   virtual void flip() = 0;
00286 
00289   virtual void update_rects(CRectList &rects) = 0;
00290 };
00291 
00310 class CSurface {
00311 protected:
00313   CSurfaceImpl *impl;
00314 
00317   void attach(CSurface &cs) {
00318     impl = cs.impl;
00319     impl->inc_ref_count();
00320   }
00321 
00325   void detach() {
00326     if (impl) {
00327       impl->dec_ref_count();
00328       if (impl->is_empty()) delete impl;
00329       impl = 0;
00330     }
00331   }
00332 
00333 public:
00334   CSurface() : impl(0) { }
00335 
00340   CSurface(int w, int h) { impl = CVideoSystemManager::get_instance().make_csurface_impl(w, h); }
00341 
00346   CSurface(const char *filename) { impl = CVideoSystemManager::get_instance().make_csurface_impl(filename); }
00347 
00352   CSurface(CSurface &copy) { attach(copy); }
00353 
00355   ~CSurface() { detach(); }
00356 
00360   CSurface &operator=(CSurface &cs) {
00361     if (this != &cs) {
00362       detach();
00363       attach(cs);
00364     }
00365     return *this;
00366   }
00367 
00369   void set_transparent_color(CColor color) { impl->set_transparent_color(color); }
00370 
00372   void blit(CSurface &source) { impl->blit( *source.impl ); }
00373 
00375   void blit(CSurface &source, CPoint &dest_pos) { impl->blit( *source.impl, dest_pos); }
00376 
00378   void blit(CSurface &source, CPoint &dest_pos, CRect &src_rect) { impl->blit( *source.impl, dest_pos, src_rect); }
00379   
00381   void fill_rect(const CRect &rect, CColor color) { impl->fill_rect(rect, color); }
00382 
00384   void set_clipping_rect(CRect &rect) { impl->set_clipping_rect(rect); }
00385 
00387   void clear_clipping_rect() { impl->clear_clipping_rect(); }
00388 
00390   int get_depth() { return impl->get_depth(); }
00391 
00393   int get_width() { return impl->get_width(); }
00394 
00396   int get_height() { return impl->get_height(); }
00397 
00401   CPoint get_size() { return CPoint(get_width(), get_height()); }
00402 };
00403 
00418 class CScreen : public CSurface {
00419 private:
00420   /* Whether the screen is currently fullscreen or windowed. */
00421   int is_fullscreen;
00422 
00423   /* Outlaw copy and '=' operators. */
00424   CScreen(CScreen &copy);
00425   CScreen &operator=(CScreen &);
00426 
00427 public:
00430   CScreen(CSurfaceImpl *n_impl) : is_fullscreen(0) { impl = n_impl; }
00431 
00433   void set_video_mode(int width, int height, int depth) {
00434     impl->set_video_mode(width, height, depth, is_fullscreen);
00435   }
00436 
00438   void flip() { impl->flip(); }
00439 
00441   void update_rects(CRectList &rects) { impl->update_rects(rects); }
00442 
00444   void set_fullscreen_mode(int mode) {
00445     is_fullscreen = mode;
00446     set_video_mode(get_width(), get_height(), get_depth());
00447   }
00448 
00450   int get_fullscreen_mode() {
00451     return is_fullscreen;
00452   }
00453 };
00454 
00455 #endif

Generated on Wed Aug 27 11:58:41 2003 for GFW by doxygen 1.3.3