CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

graph2d.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
00003     Written by Andrew Zabolotny <bit@eltech.ru>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_CSPLUGINCOMMON_CANVAS_GRAPH2D_H__
00021 #define __CS_CSPLUGINCOMMON_CANVAS_GRAPH2D_H__
00022 
00026 #include "csextern.h"
00027 #include "csutil/scf.h"
00028 #include "csutil/weakref.h"
00029 #include "ivideo/graph2d.h"
00030 #include "ivideo/fontserv.h"
00031 #include "ivideo/natwin.h"
00032 #include "iutil/eventh.h"
00033 #include "iutil/comp.h"
00034 #include "iutil/config.h"
00035 #include "iutil/dbghelp.h"
00036 #include "csutil/cfgacc.h"
00037 #include "iutil/plugin.h"
00038 
00039 #include "fontcache.h"
00040 
00045 struct iObjectRegistry;
00046 struct iPluginManager;
00047 
00055 class CS_CSPLUGINCOMMON_EXPORT csGraphics2D : public iGraphics2D
00056 {
00057 public:
00059   csConfigAccess config;
00060 
00062   int ClipX1, ClipX2, ClipY1, ClipY2;
00063 
00065   csPixelFormat pfmt;
00066 
00068   unsigned char *Memory;
00069 
00071   bool is_open;
00072 
00074   int *LineAddress;
00075 
00077   iObjectRegistry* object_reg;
00079   csWeakRef<iPluginManager> plugin_mgr;
00080 
00085   csRef<iOffscreenCanvasCallback> ofscb;
00086 
00088   csWeakRef<iFontServer> FontServer;
00090   csFontCache* fontCache;
00091 
00093   char* win_title;
00094 
00096   int Width, Height, Depth;
00102   int DisplayNumber;
00104   bool FullScreen;
00106   bool AllowResizing;
00108   csRGBpixel *Palette;
00110   bool PaletteAlloc[256];
00115   int FrameBufferLocked;
00119   virtual void ChangeDepth (int d);
00120 
00121 protected:
00123   int refreshRate;
00125   bool vsync;
00126 
00127   void CreateDefaultFontCache ();
00128 private:
00130   int FindRGBPalette (int r, int g, int b);
00135   bool Initialize (iObjectRegistry* r, int width, int height,
00136         int depth, void* memory, iOffscreenCanvasCallback* ofscb);
00137 
00138 public:
00139   SCF_DECLARE_IBASE;
00140 
00142   csGraphics2D (iBase*);
00144   virtual ~csGraphics2D ();
00145 
00147   virtual bool Initialize (iObjectRegistry*);
00149   virtual bool HandleEvent (iEvent&);
00150 
00152   virtual bool Open ();
00154   virtual void Close ();
00155 
00157   virtual void SetClipRect (int xmin, int ymin, int xmax, int ymax);
00159   virtual void GetClipRect (int &xmin, int &ymin, int &xmax, int &ymax);
00160 
00165   virtual bool BeginDraw ();
00167   virtual void FinishDraw ();
00168 
00170   virtual void Print (csRect const* area = 0) { }
00171 
00173   virtual int GetPage ();
00175   virtual bool DoubleBuffer (bool Enable);
00177   virtual bool GetDoubleBufferState ();
00178 
00180   virtual void Clear (int color);
00182   virtual void ClearAll (int color);
00183 
00189 
00190   void (*_DrawPixel) (csGraphics2D *This, int x, int y, int color);
00192   virtual void DrawPixel (int x, int y, int color)
00193   { _DrawPixel (this, x, y, color); }
00194   virtual void DrawPixels (csPixelCoord const* pixels, int num_pixels,
00195     int color);
00197   virtual void Blit (int x, int y, int width, int height,
00198         unsigned char const* data);
00199 
00201   virtual void DrawLine (float x1, float y1, float x2, float y2, int color);
00203   virtual void DrawBox (int x, int y, int w, int h, int color);
00205   virtual void SetRGB (int i, int r, int g, int b);
00206   virtual int FindRGB (int r, int g, int b, int a = 255)
00207   {
00208     if (r < 0) r = 0; else if (r > 255) r = 255;
00209     if (g < 0) g = 0; else if (g > 255) g = 255;
00210     if (b < 0) b = 0; else if (b > 255) b = 255;
00211     if (a < 0) a = 0; else if (a > 255) a = 255;
00212     if (Depth == 8)
00213       return FindRGBPalette (r, g, b);
00214     return
00215       ((r >> (8 - pfmt.RedBits))   << pfmt.RedShift) |
00216       ((g >> (8 - pfmt.GreenBits)) << pfmt.GreenShift) |
00217       ((b >> (8 - pfmt.BlueBits))  << pfmt.BlueShift) |
00218       ((255 - a) << 24);
00219     /* Alpha is "inverted" so '-1' can be decomposed to a 
00220        transparent color. (But alpha not be inverted, '-1'
00221        would be "opaque white". However, -1 is the color
00222        index for "transparent text background". */
00223   }
00224   virtual void GetRGB (int color, int& r, int& g, int& b);
00225   virtual void GetRGB (int color, int& r, int& g, int& b, int& a);
00227   virtual void Write (iFont *font , int x, int y, int fg, int bg,
00228     const char *text, uint flags = 0);
00229   virtual void WriteBaseline (iFont *font , int x, int y, int fg, int bg,
00230     const char *text);
00232   unsigned char* (*_GetPixelAt) (csGraphics2D *This, int x, int y);
00234   virtual unsigned char *GetPixelAt (int x, int y)
00235   { return _GetPixelAt (this, x, y); }
00236 
00244   virtual int GetPalEntryCount ()
00245   { return pfmt.PalEntries; }
00246 
00252   virtual int GetPixelBytes ()
00253   { return pfmt.PixelBytes; }
00254 
00258   virtual csPixelFormat const* GetPixelFormat ()
00259   { return &pfmt; }
00260 
00266   virtual csImageArea *SaveArea (int x, int y, int w, int h);
00268   virtual void RestoreArea (csImageArea *Area, bool Free = true);
00270   virtual void FreeArea (csImageArea *Area);
00271 
00272   virtual bool SetGamma (float /*gamma*/) { return false; }
00273   virtual float GetGamma () const { return 1.0; }
00274 
00275   virtual csPtr<iGraphics2D> CreateOffscreenCanvas (
00276         void* memory, int width, int height, int depth,
00277         iOffscreenCanvasCallback* ofscb);
00278 
00279 private:
00281   bool CLIPt (float denom, float num, float& tE, float& tL);
00282 public:
00283 
00288   virtual bool ClipLine (float &x1, float &y1, float &x2, float &y2,
00289     int xmin, int ymin, int xmax, int ymax);
00290 
00292   virtual iFontServer *GetFontServer ()
00293   { return FontServer; }
00294 
00296   virtual int GetWidth ()
00297   { return Width; }
00299   virtual int GetHeight ()
00300   { return Height; }
00301 
00303   virtual csRGBpixel *GetPalette ()
00304   { return pfmt.PalEntries ? Palette : 0; }
00305 
00307   virtual void GetPixel (int x, int y, uint8 &oR, uint8 &oG, uint8 &oB);
00309   virtual void GetPixel (int x, int y, uint8 &oR, uint8 &oG, uint8 &oB, uint8 &oA);
00310 
00315   virtual bool PerformExtension (char const* command, ...);
00316 
00321   virtual bool PerformExtensionV (char const* command, va_list);
00322 
00324   virtual csPtr<iImage> ScreenShot ();
00325 
00327   virtual void AllowResize (bool /*iAllow*/) { };
00328 
00330   virtual bool Resize (int w, int h);
00331 
00333   virtual iNativeWindow* GetNativeWindow ();
00334 
00336   virtual bool GetFullScreen ()
00337   { return FullScreen; }
00338 
00342   virtual void SetFullScreen (bool b);
00343 
00345   virtual bool SetMousePosition (int x, int y);
00346 
00356   virtual bool SetMouseCursor (csMouseCursorID iShape);
00357 
00365   virtual bool SetMouseCursor (iImage *image, const csRGBcolor* keycolor = 0, 
00366                                int hotspot_x = 0, int hotspot_y = 0,
00367                                csRGBcolor fg = csRGBcolor(255,255,255),
00368                                csRGBcolor bg = csRGBcolor(0,0,0));
00369 
00370   struct eiComponent : public iComponent
00371   {
00372     SCF_DECLARE_EMBEDDED_IBASE(csGraphics2D);
00373     virtual bool Initialize (iObjectRegistry* p)
00374     { return scfParent->Initialize(p); }
00375   } scfiComponent;
00376   struct EventHandler : public iEventHandler
00377   {
00378   private:
00379     csGraphics2D* parent;
00380   public:
00381     SCF_DECLARE_IBASE;
00382     EventHandler (csGraphics2D* parent)
00383     {
00384       SCF_CONSTRUCT_IBASE (0);
00385       EventHandler::parent = parent;
00386     }
00387     virtual ~EventHandler ()
00388     {
00389       SCF_DESTRUCT_IBASE();
00390     }
00391     virtual bool HandleEvent (iEvent& e) { return parent->HandleEvent(e); }
00392   } * scfiEventHandler;
00393 
00394 protected:
00401 
00402   static void DrawPixel8 (csGraphics2D *This, int x, int y, int color);
00404   static unsigned char *GetPixelAt8 (csGraphics2D *This, int x, int y);
00405 
00407   static void DrawPixel16 (csGraphics2D *This, int x, int y, int color);
00409   static unsigned char *GetPixelAt16 (csGraphics2D *This, int x, int y);
00410 
00412   static void DrawPixel32 (csGraphics2D *This, int x, int y, int color);
00414   static unsigned char *GetPixelAt32 (csGraphics2D *This, int x, int y);
00415 
00416   // Virtual Alert function so it can be overridden by subclasses
00417   // of csGraphics2D.
00418   virtual void AlertV (int type, const char* title, const char* okMsg,
00419     const char* msg, va_list args);
00420   // Virtual SetTitle function so it can be overridden by subclasses
00421   // of csGraphics2D.
00422   virtual void SetTitle (const char* title);
00423 
00424   struct CanvasConfig : public iConfig
00425   {
00426     SCF_DECLARE_EMBEDDED_IBASE (csGraphics2D);
00427     virtual bool GetOptionDescription (int idx, csOptionDescription*);
00428     virtual bool SetOption (int id, csVariant* value);
00429     virtual bool GetOption (int id, csVariant* value);
00430   } scfiConfig;
00431   friend struct CanvasConfig;
00432 
00433   struct NativeWindowManager : public iNativeWindowManager
00434   {
00435     SCF_DECLARE_EMBEDDED_IBASE (csGraphics2D);
00436     virtual void Alert (int type, const char* title, const char* okMsg,
00437         const char* msg, ...);
00438     virtual void AlertV (int type, const char* title, const char* okMsg,
00439         const char* msg, va_list arg)
00440     {
00441       scfParent->AlertV (type, title, okMsg, msg, arg);
00442     }
00443   } scfiNativeWindowManager;
00444   friend struct NativeWindowManager;
00445 
00446   struct NativeWindow : public iNativeWindow
00447   {
00448     SCF_DECLARE_EMBEDDED_IBASE (csGraphics2D);
00449     virtual void SetTitle (const char* title)
00450     {
00451       scfParent->SetTitle (title);
00452     }
00453   } scfiNativeWindow;
00454   friend struct NativeWindow;
00455 
00457   virtual bool DebugCommand (const char* cmd);
00458 
00459   struct eiDebugHelper : public iDebugHelper
00460   {
00461     SCF_DECLARE_EMBEDDED_IBASE(csGraphics2D);
00462     virtual int GetSupportedTests () const
00463     { return 0; }
00464     virtual csPtr<iString> UnitTest ()
00465     { return 0; }
00466     virtual csPtr<iString> StateTest ()
00467     { return 0; }
00468     virtual csTicks Benchmark (int num_iterations)
00469     { return 0; }
00470     virtual csPtr<iString> Dump ()
00471     { return 0; }
00472     virtual void Dump (iGraphics3D* g3d)
00473     { }
00474     virtual bool DebugCommand (const char* cmd)
00475     { return scfParent->DebugCommand (cmd); }
00476   } scfiDebugHelper;
00477   friend struct eiDebugHelper;
00478 };
00479 
00482 #endif // __CS_CSPLUGINCOMMON_CANVAS_GRAPH2D_H__

Generated for Crystal Space by doxygen 1.3.9.1