cstool/pen.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2011 by Jorrit Tyberghein 00003 Copyright (C) 2005 by Christopher Nelson 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_CSTOOL_PEN_H__ 00021 #define __CS_CSTOOL_PEN_H__ 00022 00027 #include "csgeom/poly3d.h" 00028 #include "csgeom/polyidx.h" 00029 #include "csgeom/vector4.h" 00030 #include "csgeom/vector2.h" 00031 #include "csutil/cscolor.h" 00032 #include "csutil/dirtyaccessarray.h" 00033 #include "csutil/ref.h" 00034 #include "csutil/refarr.h" 00035 #include "csutil/memfile.h" 00036 00037 #include "ivideo/graph2d.h" 00038 #include "ivideo/graph3d.h" 00039 #include "ivideo/texture.h" 00040 00041 struct iFont; 00042 00043 enum CS_PEN_TEXT_ALIGN 00044 { 00045 CS_PEN_TA_TOP, 00046 CS_PEN_TA_BOT, 00047 CS_PEN_TA_LEFT, 00048 CS_PEN_TA_RIGHT, 00049 CS_PEN_TA_CENTER 00050 }; 00051 00052 enum CS_PEN_FLAGS 00053 { 00054 CS_PEN_FILL = 1, 00055 CS_PEN_SWAPCOLORS = 2, 00056 CS_PEN_TEXTURE_ONLY = 4, 00057 CS_PEN_TEXTURE = 5 /* fill | 4 */ 00058 }; 00059 00063 struct csPenCoordinate 00064 { 00065 int x, y; 00066 csPenCoordinate (int x, int y) : x (x), y (y) { } 00067 }; 00068 00072 struct csPenCoordinatePair 00073 { 00074 csPenCoordinate c1, c2; 00075 csPenCoordinatePair (int x1, int y1, int x2, int y2) : 00076 c1 (x1, y1), c2 (x2, y2) { } 00077 }; 00078 00079 class csPen; 00080 00085 class CS_CRYSTALSPACE_EXPORT csPenCache 00086 { 00087 private: 00088 struct PRMesh 00089 { 00090 csSimpleRenderMesh mesh; 00091 csSimpleMeshFlags flags; 00092 size_t offsetVertices; 00093 size_t offsetIndices; 00094 size_t vertexCount; 00095 size_t indexCount; 00096 PRMesh () : offsetVertices (0), offsetIndices (0), 00097 vertexCount (0), indexCount (0) { } 00098 }; 00099 00100 csArray<PRMesh> meshes; 00101 00103 csDirtyAccessArray<csVector3> vertices; 00104 00106 csDirtyAccessArray<uint> vertexIndices; 00107 00109 csDirtyAccessArray<csVector4> colors; 00110 00112 csDirtyAccessArray<csVector2> texcoords; 00113 00117 bool IsMergeable (csSimpleRenderMesh* mesh1, csSimpleRenderMesh* mesh2); 00118 00122 void MergeMesh (csSimpleRenderMesh* mesh); 00123 00124 public: 00129 void PushMesh (csSimpleRenderMesh* mesh, csSimpleMeshFlags flags); 00130 00134 void Render (iGraphics3D* g3d); 00135 00139 void Clear (); 00140 00144 void SetTransform (const csReversibleTransform& trans); 00145 }; 00146 00147 00152 class CS_CRYSTALSPACE_EXPORT csPen 00153 { 00154 private: 00156 csRef<iGraphics3D> g3d; 00157 00159 csRef<iGraphics2D> g2d; 00160 00162 csSimpleRenderMesh mesh; 00163 00165 csPolyIndexed poly_idx; 00166 00168 csPoly3D poly; 00169 00171 csVector4 color; 00172 00174 csVector4 alt_color; 00175 00177 csRef<iTextureHandle> tex; 00178 00180 csVector3 tt; 00181 00183 csDirtyAccessArray<csVector4> colors; 00184 00186 csDirtyAccessArray<csVector2> texcoords; 00187 00189 csArray<csReversibleTransform> transforms; 00190 00192 csArray<csVector3> translations; 00193 00195 float pen_width; 00196 00198 uint flags; 00199 00201 struct point 00202 { 00203 float x,y; 00204 }; 00205 00207 csArray<point> line_points; 00208 00210 point last[2]; 00211 00214 float sh_w, sh_h; 00215 00218 bool gen_tex_coords; 00219 00221 csPenCache* penCache; 00222 00223 protected: 00227 void Start(); 00228 00236 void AddVertex (float x, float y, bool force_add=false); 00237 00242 inline void AddTexCoord (float x, float y); 00243 00247 void SetupMesh(); 00248 00252 void DrawMesh (csRenderMeshType mesh_type); 00253 00257 void SetAutoTexture (float w, float h); 00258 00264 void AddThickPoints (float x1, float y1, float x2, float y2); 00265 00266 public: 00267 csPen(iGraphics2D *_g2d, iGraphics3D *_g3d); 00268 ~csPen(); 00269 00280 void SetActiveCache (csPenCache* cache) { penCache = cache; } 00281 00286 void SetFlag (uint flag); 00291 void ClearFlag (uint flag); 00292 00297 void SetMixMode (uint mode); 00298 00306 void SetColor (float r, float g, float b, float a); 00307 void SetColor (const csColor4 &color); 00308 00313 void SetTexture (iTextureHandle* tex); 00314 00318 void SwapColors(); 00319 00323 void SetPenWidth(float width); 00324 00328 void ClearTransform(); 00329 00333 void PushTransform(); 00334 00339 void PopTransform(); 00340 00344 void SetOrigin(const csVector3 &o); 00345 00349 void Translate(const csVector3 &t); 00350 00354 void Rotate(const float &a); 00355 00359 void SetTransform (const csReversibleTransform& trans); 00360 00365 bool ClipLine (int& x1, int& y1, int& x2, int& y2); 00366 00370 void DrawLine (int x1, int y1, int x2, int y2); 00371 void DrawLine (const csPenCoordinatePair& coords) 00372 { 00373 DrawLine (coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y); 00374 } 00375 void DrawThickLine (int x1, int y1, int x2, int y2); 00376 00380 void DrawLines (const csArray<csPenCoordinatePair>& pairs); 00381 void DrawThickLines (const csArray<csPenCoordinatePair>& pairs); 00382 00386 void DrawPoint (int x1, int y2); 00387 void DrawPoint (const csPenCoordinate& c) { DrawPoint (c.x, c.y); } 00388 00392 void DrawRect (int x1, int y1, int x2, int y2); 00393 void DrawRect (const csPenCoordinatePair& coords) 00394 { 00395 DrawRect (coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y); 00396 } 00397 00402 void DrawMiteredRect (int x1, int y1, int x2, int y2, 00403 uint miter); 00404 void DrawMiteredRect (const csPenCoordinatePair& coords, 00405 uint miter) 00406 { 00407 DrawMiteredRect (coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y, 00408 miter); 00409 } 00410 00415 void DrawRoundedRect (int x1, int y1, int x2, int y2, 00416 uint roundness); 00417 void DrawRoundedRect (const csPenCoordinatePair& coords, 00418 uint roundness) 00419 { 00420 DrawRoundedRect (coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y, 00421 roundness); 00422 } 00423 00431 void DrawArc (int x1, int y1, int x2, int y2, 00432 float start_angle = 0, float end_angle = 6.2831853); 00433 void DrawArc (const csPenCoordinatePair& coords, 00434 float start_angle = 0, float end_angle = 6.2831853) 00435 { 00436 DrawArc (coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y, 00437 start_angle, end_angle); 00438 } 00439 00443 void DrawTriangle (int x1, int y1, int x2, int y2, int x3, int y3); 00444 void DrawTriangle (const csPenCoordinate& c1, 00445 const csPenCoordinate& c2, const csPenCoordinate& c3) 00446 { 00447 DrawTriangle (c1.x, c1.y, c2.x, c2.y, c3.x, c3.y); 00448 } 00449 00453 void Write (iFont *font, int x1, int y1, const char *text); 00454 void Write (iFont *font, const csPenCoordinate& c, const char *text) 00455 { 00456 Write (font, c.x, c.y, text); 00457 } 00461 void WriteLines (iFont *font, int x1, int y1, const csStringArray& lines); 00462 void WriteLines (iFont *font, const csPenCoordinate& c, 00463 const csStringArray& lines) 00464 { 00465 WriteLines (font, c.x, c.y, lines); 00466 } 00467 00472 void WriteBoxed (iFont *font, int x1, int y1, int x2, int y2, 00473 uint h_align, uint v_align, const char *text); 00474 void WriteBoxed (iFont *font, const csPenCoordinatePair& coords, 00475 uint h_align, uint v_align, const char *text) 00476 { 00477 WriteBoxed (font, coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y, 00478 h_align, v_align, text); 00479 } 00480 00486 void WriteLinesBoxed (iFont *font, int x1, int y1, int x2, int y2, 00487 uint h_align, uint v_align, const csStringArray& lines); 00488 void WriteLinesBoxed (iFont *font, const csPenCoordinatePair& coords, 00489 uint h_align, uint v_align, const csStringArray& lines) 00490 { 00491 WriteLinesBoxed (font, coords.c1.x, coords.c1.y, coords.c2.x, coords.c2.y, 00492 h_align, v_align, lines); 00493 } 00494 }; 00495 00499 struct csPen3DCoordinatePair 00500 { 00501 csVector3 c1, c2; 00502 csPen3DCoordinatePair (const csVector3& c1, const csVector3& c2) : 00503 c1 (c1), c2 (c2) { } 00504 }; 00505 00510 class CS_CRYSTALSPACE_EXPORT csPen3D 00511 { 00512 private: 00514 csRef<iGraphics3D> g3d; 00515 00517 csRef<iGraphics2D> g2d; 00518 00520 csSimpleRenderMesh mesh; 00521 00523 csVector4 color; 00524 00526 csPolyIndexed poly_idx; 00527 00529 csPoly3D poly; 00530 00532 csDirtyAccessArray<csVector4> colors; 00533 00535 csDirtyAccessArray<csVector2> texcoords; 00536 00538 csPenCache* penCache; 00539 00541 csReversibleTransform local2object; 00542 00543 protected: 00547 void Start(); 00548 00553 void AddVertex (const csVector3& v); 00554 00558 void SetupMesh(); 00559 00563 void DrawMesh (csRenderMeshType mesh_type); 00564 00565 public: 00566 csPen3D (iGraphics2D *_g2d, iGraphics3D *_g3d); 00567 ~csPen3D (); 00568 00579 void SetActiveCache (csPenCache* cache) { penCache = cache; } 00580 00585 void SetMixMode (uint mode); 00586 00594 void SetColor (float r, float g, float b, float a); 00595 void SetColor (const csColor4 &color); 00596 00601 void SetTransform (const csReversibleTransform& trans); 00602 00607 void SetLocal2ObjectTransform (const csReversibleTransform& trans) 00608 { 00609 local2object = trans; 00610 } 00611 00613 const csReversibleTransform& GetLocal2ObjectTransform () const 00614 { 00615 return local2object; 00616 } 00617 00621 void DrawLine (const csVector3& v1, const csVector3& v2); 00622 void DrawLine (const csPen3DCoordinatePair& coords) 00623 { 00624 DrawLine (coords.c1, coords.c2); 00625 } 00626 00630 void DrawLines (const csArray<csPen3DCoordinatePair>& pairs); 00631 00635 void DrawBox (const csBox3& box); 00636 00646 void DrawArc (const csVector3& c1, const csVector3& c2, 00647 int axis, float start_angle = 0, float end_angle = 6.2831853); 00648 00653 void DrawCylinder (const csBox3& box, int axis); 00654 }; 00655 00656 00657 #endif
Generated for Crystal Space 2.1 by doxygen 1.6.1
