csplugincommon/canvas/draw_line.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2004 by Jorrit Tyberghein 00003 (C) 2004 by Frank Richter 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_DRAW_LINE_H__ 00021 #define __CS_CSPLUGINCOMMON_CANVAS_DRAW_LINE_H__ 00022 00027 #include "csplugincommon/canvas/draw_common.h" 00028 #include "csqint.h" 00029 00034 00035 template<class Tpixel, class Tpixmixer> 00036 class csG2DDrawLine 00037 { 00038 public: 00040 static void DrawLine (csGraphics2D* G2D, float x1, float y1, float x2, float y2, 00041 Tpixel color, uint8 alpha) 00042 { 00043 Tpixmixer mixer (G2D, color, alpha); 00044 unsigned char* Memory = G2D->Memory; 00045 const int* LineAddress = G2D->LineAddress; 00046 00047 int fx1 = csQint (x1), fx2 = csQint (x2), 00048 fy1 = csQint (y1), fy2 = csQint (y2); 00049 00050 if (fy1 == fy2) 00051 { 00052 if (fx2 - fx1) 00053 { 00054 if (fx1 > fx2) { int tmp = fx1; fx1 = fx2; fx2 = tmp; } 00055 int count = fx2 - fx1 + 1; 00056 register Tpixel* dest = (Tpixel*)G2D->GetPixelAt (fx1, fy1); 00057 while (count--) mixer.Mix (*dest++); 00058 } 00059 else 00060 mixer.Mix (*((Tpixel*)G2D->GetPixelAt (fx1, fy1))); 00061 } 00062 else if (abs (fx2 - fx1) > abs (fy2 - fy1)) 00063 { 00064 // Transform floating-point format to 16.16 fixed-point 00065 fy1 = csQfixed16 (y1); fy2 = csQfixed16 (y2); 00066 00067 if (fx1 > fx2) 00068 { 00069 int tmp = fx1; fx1 = fx2; fx2 = tmp; 00070 tmp = fy1; fy1 = fy2; fy2 = tmp; 00071 } 00072 00073 // delta Y can be negative 00074 int deltay = (fy2 - fy1) / (fx2 - fx1 + 1); 00075 00076 { 00077 int x, y; 00078 for (x = fx1, y = fy1 + deltay / 2; x <= fx2; x++) 00079 { 00080 Tpixel* p = (Tpixel*)(Memory + 00081 (x * sizeof (Tpixel) + LineAddress [y >> 16])); 00082 mixer.Mix (*p); y += deltay; 00083 } 00084 } 00085 } 00086 else 00087 { 00088 // Transform floating-point format to 16.16 fixed-point 00089 fx1 = csQfixed16 (x1); fx2 = csQfixed16 (x2); 00090 00091 if (fy1 > fy2) 00092 { 00093 int tmp = fy1; fy1 = fy2; fy2 = tmp; 00094 tmp = fx1; fx1 = fx2; fx2 = tmp; 00095 } 00096 00097 // delta X can be negative 00098 int deltax = (fx2 - fx1) / (fy2 - fy1 + 1); 00099 00100 { 00101 int x, y; 00102 for (x = fx1 + deltax / 2, y = fy1; y <= fy2; y++) 00103 { 00104 Tpixel* p = (Tpixel*)(Memory + 00105 ((x >> 16) * sizeof (Tpixel) + LineAddress [y])); 00106 mixer.Mix (*p); x += deltax; 00107 } 00108 } 00109 } 00110 00111 } 00112 }; 00113 00116 #endif // __CS_CSPLUGINCOMMON_CANVAS_DRAW_LINE_H__
Generated for Crystal Space by doxygen 1.4.7