Atras

roundrect.c

00001 
00005 /*
00006  * Ejercicio : roundrect
00007  * Copyright (C) 2005 Hugo Ruscitti : hugoruscitti@yahoo.com.ar
00008  *
00009  * Este programa es software libre. Puede redistribuirlo y/o 
00010  * modificarlo bajo los términos de la Licencia Pública General de 
00011  * GNU según es publicada por la Free Software Foundation, bien de 
00012  * la versión 2 de dicha Licencia o bien (según su elección) de 
00013  * cualquier versión posterior.
00014  * 
00015  * Este programa se distribuye con la esperanza de que sea útil, pero
00016  * SIN NINGUNA GARANTÍA, incluso sin la garantía MERCANTIL implícita 
00017  * o sin garantizar la CONVENIENCIA PARA UN PROPÓSITO PARTICULAR. 
00018  * Véase la Licencia Pública General de GNU para más detalles.
00019  * 
00020  * Debería haber recibido una copia de la Licencia Pública General 
00021  * junto con este programa. Si no ha sido así, escriba a la Free 
00022  * Software Foundation, Inc., en 675 Mass Ave, Cambridge, 
00023  * MA 02139, EEUU.
00024  */
00025 
00026 #include <SDL/SDL.h>
00027 #include <math.h>
00028 
00029 int iniciar_sdl (SDL_Surface ** screen);
00030 void esperar (void);
00031 void put_pixel (SDL_Surface * screen, int x, int y, Uint32 color);
00032 void roundrect (SDL_Surface *screen, int x0, int y0, int w, int h, int radio, Uint32 color);
00033 
00034 
00035 /*
00036  * 
00037  */
00038 int main (int argc, char * argv [])
00039 {
00040         SDL_Surface * screen;
00041         Uint32 color;
00042         int i;
00043 
00044         if (iniciar_sdl (&screen))
00045                 return 1;
00046 
00047         color = SDL_MapRGB (screen->format, 0xFF, 0xFF, 0xFF);
00048         roundrect (screen, 50, 50, 220, 140, 10, color);
00049         
00050         SDL_Flip (screen);
00051         esperar ();
00052         SDL_Quit ();    
00053 
00054         return 0;
00055 }
00056 
00057 
00058 
00059 /*
00060  * Imprime un rectángulo con puntas redondeadas.
00061  *
00062  * screen: superficie donde se imprimirá el rectángulo.
00063  * x0: esquina superior izquierda
00064  * y0: esquina superior izquierda
00065  * w: ancho total
00066  * h: alto total
00067  * radio: radio de difuminación
00068  * color: color del borde
00069  */
00070 void roundrect (SDL_Surface *screen, int x0, int y0, int w, int h, int radio, Uint32 color)
00071 {
00072         int x;
00073         int y=0;
00074         int xa = x0 + radio;
00075         int ya = y0 + radio;
00076         int xb = x0 + w - radio;
00077         int yb = y0 + h - radio;
00078 
00079         if (SDL_MUSTLOCK (screen))
00080                 SDL_LockSurface (screen);
00081 
00082         for (x = 0; x <= y; x ++)
00083         {
00084                 y = (int) sqrt (radio * radio - x * x);
00085 
00086                 /* primer cuadrante */
00087                 put_pixel (screen, xb + x, ya - y, color);
00088                 put_pixel (screen, xb + y, ya - x, color);
00089         
00090                 /* segundo cuadrante */
00091                 put_pixel (screen, xa - x, ya - y, color);
00092                 put_pixel (screen, xa - y, ya - x, color);
00093         
00094                 /* tercer cuadrante */
00095                 put_pixel (screen, xa - x, yb + y, color);
00096                 put_pixel (screen, xa - y, yb + x, color);
00097 
00098                 /* cuarto cuadrante */
00099                 put_pixel (screen, xb + x, yb + y, color);
00100                 put_pixel (screen, xb + y, yb + x, color);
00101         }
00102 
00103         /* lineas horizontales */
00104         for (x = xa; x < xb; x ++)
00105         {
00106                 put_pixel (screen, x, y0, color);
00107                 put_pixel (screen, x, yb + radio, color);
00108         }
00109 
00110         /* lineas verticales */
00111         for (y = ya; y < yb; y ++)
00112         {
00113                 put_pixel (screen, x0, y, color);
00114                 put_pixel (screen, xb + radio, y, color);
00115         }
00116 
00117         if (SDL_MUSTLOCK (screen))
00118                 SDL_UnlockSurface (screen);
00119 }
00120 
00121 
00122 
00123 /*
00124  * Inicia la biblioteca.
00125  */
00126 int iniciar_sdl (SDL_Surface ** screen)
00127 {
00128         if (SDL_Init (0))
00129         {
00130                 printf ("No se puede inicializar SDL: %s\n", SDL_GetError ());
00131                 return 1;
00132         }
00133 
00134         * screen = SDL_SetVideoMode (320, 240, 32, SDL_HWSURFACE);
00135 
00136         if (* screen == NULL)
00137         {
00138                 printf ("Falló al iniciar el video: %s\n", SDL_GetError ());
00139                 return 1;
00140         }
00141 
00142         SDL_WM_SetCaption ("Ejercicio roundrect", NULL);
00143         
00144         return 0;
00145 }
00146 
00147 
00148 
00149 /*
00150  * Imprime un punto en la pantalla.
00151  */
00152 void put_pixel (SDL_Surface *screen, int x, int y, Uint32 color)
00153 {
00154         int bpp = screen->format->BytesPerPixel;
00155         Uint8 *p = (Uint8 *) screen->pixels + y * screen->pitch + x * bpp;
00156 
00157         if (x > screen->w || y > screen->h || y < 0 || x < 0)
00158                 return;
00159 
00160         switch (bpp)
00161         {
00162                 case 1:
00163                         *p = color;
00164                         break;
00165                 
00166                 case 2:
00167                         *(Uint16 *)p = color;
00168                         break;
00169 
00170                 case 3:
00171                         if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
00172                         {
00173                                 p[0] = (color >> 16) & 0xff;
00174                                 p[1] = (color >> 8) & 0xff;
00175                                 p[2] = color & 0xff;
00176                         }
00177                         else
00178                         {
00179                                 p[0] = color & 0xff;
00180                                 p[1] = (color >> 8) & 0xff;
00181                                 p[2] = (color >> 16) & 0xff;
00182                         }
00183                         break;
00184                 
00185                 case 4:
00186                         * (Uint32 *)p  = color;
00187                         break;
00188         }
00189 
00190 }
00191 
00192 
00193 
00194 /*
00195  * Detiene el programa hasta que el usuario realiza algún evento.
00196  */
00197 void esperar (void)
00198 {
00199         SDL_Event evento;
00200 
00201         while (SDL_WaitEvent (&evento))
00202         {
00203                 if (evento.type == SDL_QUIT)
00204                         break;
00205 
00206                 if (evento.type == SDL_KEYDOWN)
00207                         break;
00208         }
00209 }

Generated on Sat Sep 3 13:36:39 2005 for roundrect - hugoruscitti by  Doxygen 1.4.2