00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00061
00062
00063
00064
00065
00066
00067
00068
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
00087 put_pixel (screen, xb + x, ya - y, color);
00088 put_pixel (screen, xb + y, ya - x, color);
00089
00090
00091 put_pixel (screen, xa - x, ya - y, color);
00092 put_pixel (screen, xa - y, ya - x, color);
00093
00094
00095 put_pixel (screen, xa - x, yb + y, color);
00096 put_pixel (screen, xa - y, yb + x, color);
00097
00098
00099 put_pixel (screen, xb + x, yb + y, color);
00100 put_pixel (screen, xb + y, yb + x, color);
00101 }
00102
00103
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
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
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
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
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 }