PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/xbmc/visualizations/Goom/goom2k4-0/src/drawmethods.c

http://github.com/xbmc/xbmc
C | 203 lines | 129 code | 9 blank | 65 comment | 37 complexity | 8a9cc5a81ce760f5a54e536e79885d88 MD5 | raw file
  1#include "drawmethods.h"
  2
  3#define DRAWMETHOD_PLUS(_out,_backbuf,_col) \
  4{\
  5      int tra=0,i=0;\
  6      unsigned char *bra = (unsigned char*)&(_backbuf);\
  7      unsigned char *dra = (unsigned char*)&(_out);\
  8      unsigned char *cra = (unsigned char*)&(_col);\
  9      for (;i<4;i++) {\
 10				tra = *cra;\
 11				tra += *bra;\
 12				if (tra>255) tra=255;\
 13				*dra = tra;\
 14				++dra;++cra;++bra;\
 15			}\
 16}
 17
 18#define DRAWMETHOD DRAWMETHOD_PLUS(*p,*p,col)
 19
 20void draw_line (Pixel *data, int x1, int y1, int x2, int y2, int col, int screenx, int screeny)
 21{
 22  int     x, y, dx, dy, yy, xx;
 23  Pixel    *p;
 24
 25  if ((y1 < 0) || (y2 < 0) || (x1 < 0) || (x2 < 0) || (y1 >= screeny) || (y2 >= screeny) || (x1 >= screenx) || (x2 >= screenx)) return;
 26        
 27  /* clip to top edge 
 28     if ((y1 < 0) && (y2 < 0))
 29     return;
 30	
 31     if (y1 < 0) {
 32     x1 += (y1 * (x1 - x2)) / (y2 - y1);
 33     y1 = 0;
 34     }
 35     if (y2 < 0) {
 36     x2 += (y2 * (x1 - x2)) / (y2 - y1);
 37     y2 = 0;
 38     }
 39         
 40     clip to bottom edge 
 41     if ((y1 >= screeny) && (y2 >= screeny))
 42     return;
 43     if (y1 >= screeny) {
 44     x1 -= ((screeny - y1) * (x1 - x2)) / (y2 - y1);
 45     y1 = screeny - 1;
 46     }
 47     if (y2 >= screeny) {
 48     x2 -= ((screeny - y2) * (x1 - x2)) / (y2 - y1);
 49     y2 = screeny - 1;
 50     }
 51     clip to left edge 
 52     if ((x1 < 0) && (x2 < 0))
 53     return;
 54     if (x1 < 0) {
 55     y1 += (x1 * (y1 - y2)) / (x2 - x1);
 56     x1 = 0;
 57     }
 58     if (x2 < 0) {
 59     y2 += (x2 * (y1 - y2)) / (x2 - x1);
 60     x2 = 0;
 61     }
 62     clip to right edge 
 63     if ((x1 >= screenx) && (x2 >= screenx))
 64     return;
 65     if (x1 >= screenx) {
 66     y1 -= ((screenx - x1) * (y1 - y2)) / (x2 - x1);
 67     x1 = screenx - 1;
 68     }
 69     if (x2 >= screenx) {
 70     y2 -= ((screenx - x2) * (y1 - y2)) / (x2 - x1);
 71     x2 = screenx - 1;
 72     }
 73  */
 74  
 75  dx = x2 - x1;
 76  dy = y2 - y1;
 77  if (x1 > x2) {
 78    int     tmp;
 79
 80    tmp = x1;
 81    x1 = x2;
 82    x2 = tmp;
 83    tmp = y1;
 84    y1 = y2;
 85    y2 = tmp;
 86    dx = x2 - x1;
 87    dy = y2 - y1;
 88  }
 89
 90  /* vertical line */
 91  if (dx == 0) {
 92    if (y1 < y2) {
 93      p = &(data[(screenx * y1) + x1]);
 94      for (y = y1; y <= y2; y++) {
 95	DRAWMETHOD;
 96	p += screenx;
 97      }
 98    }
 99    else {
100      p = &(data[(screenx * y2) + x1]);
101      for (y = y2; y <= y1; y++) {
102	DRAWMETHOD;
103	p += screenx;
104      }
105    }
106    return;
107  }
108  /* horizontal line */
109  if (dy == 0) {
110    if (x1 < x2) {
111      p = &(data[(screenx * y1) + x1]);
112      for (x = x1; x <= x2; x++) {
113	DRAWMETHOD;
114	p++;
115      }
116      return;
117    }
118    else {
119      p = &(data[(screenx * y1) + x2]);
120      for (x = x2; x <= x1; x++) {
121	DRAWMETHOD;
122	p++;
123      }
124      return;
125    }
126  }
127  /* 1    */
128  /* \   */
129  /* \  */
130  /* 2 */
131  if (y2 > y1) {
132    /* steep */
133    if (dy > dx) {
134      dx = ((dx << 16) / dy);
135      x = x1 << 16;
136      for (y = y1; y <= y2; y++) {
137	xx = x >> 16;
138	p = &(data[(screenx * y) + xx]);
139	DRAWMETHOD;
140	if (xx < (screenx - 1)) {
141	  p++;
142	  /* DRAWMETHOD; */
143	}
144	x += dx;
145      }
146      return;
147    }
148    /* shallow */
149    else {
150      dy = ((dy << 16) / dx);
151      y = y1 << 16;
152      for (x = x1; x <= x2; x++) {
153	yy = y >> 16;
154	p = &(data[(screenx * yy) + x]);
155	DRAWMETHOD;
156	if (yy < (screeny - 1)) {
157	  p += screeny;
158	  /* DRAWMETHOD; */
159	}
160	y += dy;
161      }
162    }
163  }
164  /* 2 */
165  /* /  */
166  /* /   */
167  /* 1    */
168  else {
169    /* steep */
170    if (-dy > dx) {
171      dx = ((dx << 16) / -dy);
172      x = (x1 + 1) << 16;
173      for (y = y1; y >= y2; y--) {
174	xx = x >> 16;
175	p = &(data[(screenx * y) + xx]);
176	DRAWMETHOD;
177	if (xx < (screenx - 1)) {
178	  p--;
179	  /* DRAWMETHOD; */
180	}
181	x += dx;
182      }
183      return;
184    }
185    /* shallow */
186    else {
187      dy = ((dy << 16) / dx);
188      y = y1 << 16;
189      for (x = x1; x <= x2; x++) {
190	yy = y >> 16;
191	p = &(data[(screenx * yy) + x]);
192	DRAWMETHOD;
193	if (yy < (screeny - 1)) {
194	  p += screeny;
195	  /* DRAWMETHOD; */
196	}
197	y += dy;
198      }
199      return;
200    }
201  }
202}
203