Main Page   File List   Globals  

FBdraw.c

Go to the documentation of this file.
00001 
00010 /* This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; version 2 of the License. */
00013  
00014 #include <math.h>
00015 #include "FBlib.h"
00016 #include "FBpriv.h"
00017 #include <stdlib.h> // for abort()
00018 
00019 //
00020 // TODO: Should really rename this to something else
00021 //
00022 int GETPOS(int a, int b)
00023 {
00024         int pos = 0;
00025         switch(FB_getrotation())
00026         {
00027                 case FB_ROTATE_NONE:
00028                         pos = (a*inc_x) + (b*inc_y);
00029                         break;
00030                 case FB_ROTATE_90:
00031                         pos = (b*inc_x) + ((work_var_info.yres-a-1)*inc_y);
00032                         break;
00033                 case FB_ROTATE_180:
00034                         pos = screensize-((a*inc_x) + (b*inc_y));
00035                         break;
00036                 case FB_ROTATE_270:
00037                         pos = ((work_var_info.xres-b-1)*inc_x) + (a*inc_y);
00038                         break;
00039                 default:
00040                         printf("Trying to create rotation coordinate with invalid rotation: FB_getrotation()=%d\n",FB_getrotation());
00041                         abort();
00042         }
00043 
00044         // sanity checking
00045         if(pos>screensize || pos<0)
00046                 pos = 0;
00047         return pos;
00048 }
00049 
00050 inline void setpixel(char *pos, FB_pixel col)
00051 {
00052         char *abs_pos;
00053         
00054         abs_pos = fb_visp + (long int)pos;
00055         if(FB_getbpp() == 16)
00056         {
00057                 *abs_pos = (u_char)col;
00058                 *(abs_pos + 1) = (u_char)(col>>8);
00059         }
00060         else if (FB_getbpp() == 32)
00061         {
00062                 *abs_pos = (u_char)col;
00063                 *(abs_pos + 1) = (col>>8);
00064                 *(abs_pos + 2) = (u_char)(col>>16);
00065                 *(abs_pos + 3) = (u_char)(col>>24);
00066         }
00067 
00068         return;
00069 }
00070 
00071 inline void FB_putpixel(int x, int y, FB_pixel color)
00072 {
00073         char *pos;
00074         
00075         if(x < 0 || x > FB_getxres())
00076                 return;
00077         if(y < 0 || y > FB_getyres())
00078                 return;
00079         
00080         pos = (char*)(GETPOS(x,y));
00081         setpixel(pos, color);
00082 
00083         return;
00084 }
00085 
00086 FB_pixel FB_getpixel(int x, int y)
00087 {
00088         long int location;
00089         FB_pixel tmp;
00090         
00091         location=x * (FB_getbpp()/8) + y * (fb_fix_info.line_length);
00092         if(location > visiblesize)
00093                 return 0; /* return black if out of bounds (weird) */
00094         tmp = *(fb_visp + location) | (*(fb_visp + location + 1)<<8);
00095         if(FB_getbpp() == 32)
00096                 tmp |= (*(fb_visp + location + 2)<<16) + (*(fb_visp + location + 3)<<24);
00097         return tmp;
00098 }
00099 
00100 //
00101 // TODO: replace this with something independent of inc_*
00102 //
00103 void FB_vline(int x, int sy, int ey, FB_pixel color)
00104 {
00105         int i;
00106 
00107         /* Parameter sanity checks */
00108         if(x < 0 || x > FB_getxres())
00109                 return;
00110         
00111         if(sy > ey)
00112         {
00113                 i=sy;
00114                 sy=ey;
00115                 ey=i;
00116         }
00117 
00118         if(sy < 0)
00119                 sy = 0;
00120 
00121         if(ey > FB_getyres())
00122                 ey = FB_getyres();
00123         /* end of checks */
00124 
00125         for(i = sy; i <= ey; i++)
00126         {
00127                 FB_putpixel(x,i,color);
00128         }
00129         return;
00130 }
00131 
00132 //
00133 // TODO: replace this with something independent of inc_*
00134 //
00135 void FB_hline(int sx, int ex, int y, FB_pixel color)
00136 {
00137         int i;
00138         
00139         if(y < 0 || y > FB_getyres())
00140                 return;
00141         
00142         if(sx > ex)
00143         {
00144                 i=sx;
00145                 sx=ex;
00146                 ex=i;
00147         }
00148 
00149         if(sx < 0)
00150                 sx = 0;
00151         if(ex > FB_getxres())
00152                 ex = FB_getxres();
00153         
00154         for(i = sx; i <= ex; i++)
00155         {
00156                 FB_putpixel(i,y,color);
00157         }
00158         return;
00159 }
00160 
00161 //
00162 // TODO: replace this with something independent of inc_*
00163 //
00164 /* This is Bresenham's line algorithm */
00165 void FB_line(int sx, int sy, int ex, int ey, FB_pixel color)
00166 {
00167         int dx,dy;
00168         int x,y,incx,incy;
00169         int balance;
00170         
00171         if(sx == ex)
00172                 FB_vline(sx,sy,ey,color);
00173         if(sy == ey)
00174                 FB_hline(sx,ex,sy,color);
00175         
00176         if (ex >= sx)
00177         {
00178                 dx = ex - sx;
00179                 incx = 1;
00180         }
00181         else
00182         {
00183                 dx = sx - ex;
00184                 incx = -1;
00185         }
00186 
00187         if (ey >= sy)
00188         {
00189                 dy = ey - sy;
00190                 incy = 1;
00191         }
00192         else
00193         {
00194                 dy = sy - ey;
00195                 incy = -1;
00196         }
00197 
00198         x = sx;
00199         y = sy;
00200 
00201         if (dx >= dy)
00202         {
00203                 dy <<= 1;
00204                 balance = dy - dx;
00205                 dx <<= 1;
00206 
00207                 while (x != ex)
00208                 {
00209                         FB_putpixel(x, y, color);
00210                         if (balance >= 0)
00211                         {
00212                                 y += incy;
00213                                 balance -= dx;
00214                         }
00215                         balance += dy;
00216                         x += incx;
00217                 } FB_putpixel(x, y, color);
00218         }
00219         else
00220         {
00221                 dx <<= 1;
00222                 balance = dx - dy;
00223                 dy <<= 1;
00224 
00225                 while (y != ey)
00226                 {
00227                         FB_putpixel(x, y, color);
00228                         if (balance >= 0)
00229                         {
00230                                 x += incx;
00231                                 balance -= dy;
00232                         }
00233                         balance += dx;
00234                         y += incy;
00235                 } FB_putpixel(x, y, color);
00236         }
00237 
00238         return;
00239 }
00240 
00241 void FB_rect(int sx, int sy, int ex, int ey, FB_pixel color)
00242 {
00243         FB_hline(sx,ex,sy,color);
00244         FB_hline(sx,ex,ey,color);
00245         FB_vline(sx,sy,ey,color);
00246         FB_vline(ex,sy,ey,color);
00247         return;
00248 }
00249 
00250 //
00251 // TODO: replace this with something independent of inc_*
00252 //
00253 void FB_rectfill(int sx, int sy, int ex, int ey, FB_pixel color)
00254 {
00255         int i,j;
00256         
00257         if(sx > ex)
00258         {
00259                 i=sx;
00260                 sx=ex;
00261                 ex=i;
00262         }
00263         if(sy > ey)
00264         {
00265                 i=sy;
00266                 sy=ey;
00267                 ey=i;
00268         }
00269         if(sx < 0)
00270                 sx = 0;
00271         if(sy < 0)
00272                 sy = 0;
00273         if(ex > FB_getxres())
00274                 ex = FB_getxres();
00275         if(ey > FB_getyres())
00276                 ey = FB_getyres();
00277         
00278         for(i = sy; i <= ey; i++)
00279         {
00280                 for(j = sx; j <= ex; j++ )
00281                 {
00282                         FB_putpixel(j,i,color);
00283                 }
00284         }
00285         return;
00286 }
00287 
00288 void FB_triangle(int x1,int y1,int x2,int y2,int x3,int y3,FB_pixel col)
00289 {
00290         FB_line(x1, y1, x2, y2, col);
00291         FB_line(x3, y3, x2, y2, col);
00292         FB_line(x1, y1, x3, y3, col);
00293         
00294         return;
00295 }
00296 
00297 void FB_circle(int cx, int cy, int radius, FB_pixel color)
00298 {
00299         double cosval;
00300         double sinval;
00301         double inc = (M_PI)/(radius*3.6); // Is this ok for all circles ?
00302         double angle=0;
00303         int x,y;
00304 
00305         while(angle <= M_PI)
00306         {
00307                 cosval = radius*cos(angle);
00308                 sinval = radius*sin(angle);
00309                 x = (int)(cosval + cx);
00310                 y = (int)(sinval + cy);
00311                 FB_putpixel(x, y, color);
00312                 x = (int)(-cosval + cx);
00313                 y = (int)(-sinval + cy);
00314                 FB_putpixel(x, y, color);
00315                 angle+=inc;
00316         }
00317         return;
00318 }
00319 
00320 void FB_clear_screen(FB_pixel color)
00321 {
00322         int i,j;
00323         
00324         for(i = 0; i <= FB_getyres(); i++)
00325         {
00326                 for(j = 0; j <= FB_getxres(); j++ )
00327                 {
00328                         FB_putpixel(j,i,color);
00329                 }
00330         }
00331 
00332         return;
00333 }

Generated on Sat Jul 5 09:04:51 2003 for LibFB by doxygen1.2.18