csplugincommon/canvas/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 00027 #include "csextern.h" 00028 00029 #include "csutil/cfgacc.h" 00030 #include "csutil/scf.h" 00031 #include "csutil/scf_implementation.h" 00032 #include "csutil/weakref.h" 00033 00034 #include "iutil/comp.h" 00035 #include "iutil/dbghelp.h" 00036 #include "iutil/eventh.h" 00037 #include "iutil/plugin.h" 00038 #include "iutil/pluginconfig.h" 00039 #include "iutil/string.h" 00040 #include "ivideo/fontserv.h" 00041 #include "ivideo/graph2d.h" 00042 #include "ivideo/natwin.h" 00043 00048 struct iObjectRegistry; 00049 struct iPluginManager; 00050 00051 class csFontCache; 00052 00060 class CS_CRYSTALSPACE_EXPORT csGraphics2D : 00061 public scfImplementation6<csGraphics2D, 00062 iGraphics2D, 00063 iComponent, 00064 iNativeWindow, 00065 iNativeWindowManager, 00066 iPluginConfig, 00067 iDebugHelper> 00068 { 00069 public: 00071 csConfigAccess config; 00072 00074 int ClipX1, ClipX2, ClipY1, ClipY2; 00075 00077 csPixelFormat pfmt; 00078 00080 unsigned char *Memory; 00081 00083 bool is_open; 00084 00086 int *LineAddress; 00087 00089 iObjectRegistry* object_reg; 00091 csWeakRef<iPluginManager> plugin_mgr; 00092 00097 csRef<iOffscreenCanvasCallback> ofscb; 00098 00100 csWeakRef<iFontServer> FontServer; 00102 csFontCache* fontCache; 00103 00105 csString win_title; 00106 00108 int Width, Height, Depth; 00109 00110 int vpWidth, vpHeight; 00111 bool vpSet; 00112 00118 int DisplayNumber; 00120 bool FullScreen; 00122 bool AllowResizing; 00124 csRGBpixel *Palette; 00126 bool PaletteAlloc[256]; 00131 int FrameBufferLocked; 00135 virtual void ChangeDepth (int d); 00139 virtual const char *GetName() const; 00140 00141 protected: 00143 int refreshRate; 00145 bool vsync; 00146 00147 void CreateDefaultFontCache (); 00148 00149 csString name; 00150 private: 00152 int FindRGBPalette (int r, int g, int b); 00157 bool Initialize (iObjectRegistry* r, int width, int height, 00158 int depth, void* memory, iOffscreenCanvasCallback* ofscb); 00159 00160 public: 00162 csGraphics2D (iBase*); 00163 00165 virtual ~csGraphics2D (); 00166 00168 virtual bool Initialize (iObjectRegistry*); 00170 virtual bool HandleEvent (iEvent&); 00171 00173 virtual bool Open (); 00175 virtual void Close (); 00176 00178 virtual void SetClipRect (int xmin, int ymin, int xmax, int ymax); 00180 virtual void GetClipRect (int &xmin, int &ymin, int &xmax, int &ymax); 00181 00186 virtual bool BeginDraw (); 00188 virtual void FinishDraw (); 00189 00191 virtual void Print (csRect const* /*area*/ = 0) { } 00192 00194 virtual int GetPage (); 00196 virtual bool DoubleBuffer (bool Enable); 00198 virtual bool GetDoubleBufferState (); 00199 00201 virtual void Clear (int color); 00203 virtual void ClearAll (int color); 00204 00210 00211 void (*_DrawPixel) (csGraphics2D *This, int x, int y, int color); 00213 virtual void DrawPixel (int x, int y, int color) 00214 { _DrawPixel (this, x, y, color); } 00215 virtual void DrawPixels (csPixelCoord const* pixels, int num_pixels, 00216 int color); 00218 virtual void Blit (int x, int y, int width, int height, 00219 unsigned char const* data); 00220 00222 virtual void DrawLine (float x1, float y1, float x2, float y2, int color); 00224 virtual void DrawBox (int x, int y, int w, int h, int color); 00226 virtual void SetRGB (int i, int r, int g, int b); 00227 virtual int FindRGB (int r, int g, int b, int a = 255) 00228 { 00229 if (r < 0) r = 0; else if (r > 255) r = 255; 00230 if (g < 0) g = 0; else if (g > 255) g = 255; 00231 if (b < 0) b = 0; else if (b > 255) b = 255; 00232 if (a < 0) a = 0; else if (a > 255) a = 255; 00233 if (Depth == 8) 00234 return FindRGBPalette (r, g, b); 00235 return 00236 ((r >> (8 - pfmt.RedBits)) << pfmt.RedShift) | 00237 ((g >> (8 - pfmt.GreenBits)) << pfmt.GreenShift) | 00238 ((b >> (8 - pfmt.BlueBits)) << pfmt.BlueShift) | 00239 ((255 - a) << 24); 00240 /* Alpha is "inverted" so '-1' can be decomposed to a 00241 transparent color. (But alpha not be inverted, '-1' 00242 would be "opaque white". However, -1 is the color 00243 index for "transparent text background". */ 00244 } 00245 virtual void GetRGB (int color, int& r, int& g, int& b); 00246 virtual void GetRGB (int color, int& r, int& g, int& b, int& a); 00248 virtual void Write (iFont *font , int x, int y, int fg, int bg, 00249 const char *text, uint flags = 0); 00250 virtual void WriteBaseline (iFont *font , int x, int y, int fg, int bg, 00251 const char *text); 00253 unsigned char* (*_GetPixelAt) (csGraphics2D *This, int x, int y); 00255 virtual unsigned char *GetPixelAt (int x, int y) 00256 { return _GetPixelAt (this, x, y); } 00257 00265 virtual int GetPalEntryCount () 00266 { return pfmt.PalEntries; } 00267 00273 virtual int GetPixelBytes () 00274 { return pfmt.PixelBytes; } 00275 00279 virtual csPixelFormat const* GetPixelFormat () 00280 { return &pfmt; } 00281 00287 virtual csImageArea *SaveArea (int x, int y, int w, int h); 00289 virtual void RestoreArea (csImageArea *Area, bool Free = true); 00291 virtual void FreeArea (csImageArea *Area); 00292 00293 virtual bool SetGamma (float /*gamma*/) { return false; } 00294 virtual float GetGamma () const { return 1.0; } 00295 00296 virtual csPtr<iGraphics2D> CreateOffscreenCanvas ( 00297 void* memory, int width, int height, int depth, 00298 iOffscreenCanvasCallback* ofscb); 00299 00300 private: 00302 bool CLIPt (float denom, float num, float& tE, float& tL); 00303 public: 00304 00309 virtual bool ClipLine (float &x1, float &y1, float &x2, float &y2, 00310 int xmin, int ymin, int xmax, int ymax); 00311 00313 virtual iFontServer *GetFontServer () 00314 { return FontServer; } 00315 00316 virtual int GetWidth () 00317 { return vpSet ? vpWidth : Width; } 00318 virtual int GetHeight () 00319 { return vpSet ? vpHeight : Height; } 00320 00322 virtual csRGBpixel *GetPalette () 00323 { return pfmt.PalEntries ? Palette : 0; } 00324 00326 virtual void GetPixel (int x, int y, uint8 &oR, uint8 &oG, uint8 &oB); 00328 virtual void GetPixel (int x, int y, uint8 &oR, uint8 &oG, uint8 &oB, uint8 &oA); 00329 00334 virtual bool PerformExtension (char const* command, ...); 00335 00340 virtual bool PerformExtensionV (char const* command, va_list); 00341 00343 virtual csPtr<iImage> ScreenShot (); 00344 00346 virtual void AllowResize (bool /*iAllow*/) { }; 00347 00349 virtual bool Resize (int w, int h); 00350 00352 virtual iNativeWindow* GetNativeWindow (); 00353 00355 virtual bool GetFullScreen () 00356 { return FullScreen; } 00357 00361 virtual void SetFullScreen (bool b); 00362 00364 virtual bool SetMousePosition (int x, int y); 00365 00375 virtual bool SetMouseCursor (csMouseCursorID iShape); 00376 00384 virtual bool SetMouseCursor (iImage *image, const csRGBcolor* keycolor = 0, 00385 int hotspot_x = 0, int hotspot_y = 0, 00386 csRGBcolor fg = csRGBcolor(255,255,255), 00387 csRGBcolor bg = csRGBcolor(0,0,0)); 00388 00389 struct EventHandler : public scfImplementation1<EventHandler, iEventHandler> 00390 { 00391 private: 00392 csGraphics2D* parent; 00393 public: 00394 EventHandler (csGraphics2D* parent) : scfImplementationType (this), 00395 parent(parent) {} 00396 virtual bool HandleEvent (iEvent& e) { return parent->HandleEvent(e); } 00397 CS_EVENTHANDLER_NAMES("crystalspace.graphics2d.common") 00398 CS_EVENTHANDLER_NIL_CONSTRAINTS 00399 } * scfiEventHandler; 00400 00401 protected: 00408 00409 static void DrawPixel8 (csGraphics2D *This, int x, int y, int color); 00411 static unsigned char *GetPixelAt8 (csGraphics2D *This, int x, int y); 00412 00414 static void DrawPixel16 (csGraphics2D *This, int x, int y, int color); 00416 static unsigned char *GetPixelAt16 (csGraphics2D *This, int x, int y); 00417 00419 static void DrawPixel32 (csGraphics2D *This, int x, int y, int color); 00421 static unsigned char *GetPixelAt32 (csGraphics2D *This, int x, int y); 00422 00425 // Virtual Alert function so it can be overridden by subclasses 00426 // of csGraphics2D. 00427 virtual void AlertV (int type, const char* title, const char* okMsg, 00428 const char* msg, va_list args); 00429 virtual void Alert (int type, const char* title, const char* okMsg, 00430 const char* msg, ...); 00435 // Virtual SetTitle function so it can be overridden by subclasses 00436 // of csGraphics2D. 00437 virtual void SetTitle (const char* title); 00442 virtual bool GetOptionDescription (int idx, csOptionDescription*); 00443 virtual bool SetOption (int id, csVariant* value); 00444 virtual bool GetOption (int id, csVariant* value); 00449 virtual bool DebugCommand (const char* cmd); 00450 virtual int GetSupportedTests () const { return 0; } 00451 virtual csPtr<iString> UnitTest () { return 0; } 00452 virtual csPtr<iString> StateTest () { return 0; } 00453 virtual csTicks Benchmark (int /*num_iterations*/) { return 0; } 00454 virtual csPtr<iString> Dump () { return 0; } 00455 virtual void Dump (iGraphics3D* /*g3d*/) { } 00457 }; 00458 00461 #endif // __CS_CSPLUGINCOMMON_CANVAS_GRAPH2D_H__
Generated for Crystal Space by doxygen 1.4.6