00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <math.h>
00020 #include <SDL/SDL.h>
00021 #include <time.h>
00022
00023
00024 #define PI 3.14
00025
00026 void rectangulo(SDL_Surface *, int, int, int, int, int, Uint32);
00027 void Recta2Puntos(SDL_Surface *, int, int ,int, int, Uint32);
00028 void PutPixel(SDL_Surface *, int, int, Uint32);
00029 void Arco(SDL_Surface *, int, int, float, float, int, Uint32);
00030
00031
00032
00033 int main (int narg, char *argv[])
00034 {
00035 SDL_Surface *screen = NULL;
00036 SDL_Event suceso;
00037
00038 char *msg;
00039 int salir, x0, y0, ancho, alto, color, angulo=0;
00040
00041
00042 if (SDL_Init (SDL_INIT_VIDEO) < 0)
00043 {
00044 sprintf (msg, "Error, error en -> SDL: %s\n", SDL_GetError ());
00045 free (msg);
00046 exit (1);
00047 }
00048
00049 atexit(SDL_Quit);
00050
00051
00052 screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
00053 if (screen == NULL)
00054 {
00055 sprintf (msg, "Error en la ejecución del modo -> 640x480x16: %s\n",
00056 SDL_GetError ());
00057 free (msg);
00058 exit (2);
00059 }
00060 SDL_WM_SetCaption ("Prueba SDL: Rectángulo Redondeado. ESC para salir. :)", NULL);
00061
00062 salir = 0;
00063
00064 srand(time(NULL));
00065
00066
00067
00068 if (narg==6) {
00069 x0=atoi(argv[1]);
00070 y0=atoi(argv[2]);
00071 alto=atoi(argv[3]);
00072 ancho=atoi(argv[4]);
00073 angulo=atoi(argv[5]);
00074 }
00075 else {
00076 x0=60;
00077 y0=60;
00078 ancho=500;
00079 alto=300;
00080 angulo=40;
00081 }
00082
00083 while (!salir) {
00084 while (SDL_PollEvent (&suceso))
00085 {
00086 switch (suceso.type)
00087 {
00088 case SDL_KEYDOWN:
00089 switch(suceso.key.keysym.sym) {
00090 case SDLK_ESCAPE:
00091 salir=1;
00092 break;
00093 }
00094 break;
00095 default:
00096 break;
00097 }
00098 }
00099 color=SDL_MapRGB(screen->format, rand()%0xff, rand()%0xff, rand()%0xff);
00100 rectangulo(screen, x0, y0, alto, ancho, angulo, color);
00101
00102 SDL_Flip (screen);
00103 if (!salir) {
00104 SDL_Delay(50);
00105 }
00106 }
00107
00108 return 0;
00109 }
00110
00111
00112
00113
00114
00115
00116 void rectangulo(SDL_Surface *destino, int x0, int y0, int h, int w, int radio, Uint32 color) {
00117
00118 int x1=x0+w, y1=y0+h;
00119
00120 Recta2Puntos(destino, x0+radio, y0, x1-radio, y0, color);
00121 Recta2Puntos(destino, x0+radio, y1, x1-radio, y1, color);
00122 Recta2Puntos(destino, x1, y0+radio, x1, y1-radio, color);
00123 Recta2Puntos(destino, x0, y0+radio, x0, y1-radio, color);
00124
00125
00126 Arco(destino, x0+radio, y0+radio, 90.0, 180.0, radio+1, color);
00127 Arco(destino, x0+radio, y1-radio, 180.0, 270.0, radio+1, color);
00128 Arco(destino, x1-radio, y1-radio, 270.0, 360.0, radio+1, color);
00129 Arco(destino, x1-radio, y0+radio, 0.0, 90.0, radio+1, color);
00130
00131 }
00132
00133 void Recta2Puntos(SDL_Surface *pantalla, int x0, int y0, int x1, int y1, Uint32 color)
00134 {
00135 int x=0, y=0, incx=0, incy=0, temp=0, mayorY=0;
00136
00137 if (x0 > x1)
00138 {
00139 temp=x0;
00140 x0=x1;
00141 x1=temp;
00142 temp=y0;
00143 y0=y1;
00144 y1=temp;
00145 }
00146
00147 if ((x1-x0)==0)
00148 {
00149 if(y0 > y1)
00150 {
00151 temp=x0;
00152 x0=x1;
00153 x1=temp;
00154 temp=y0;
00155 y0=y1;
00156 y1=temp;
00157 }
00158 for(y=y0; y<=y1; y++)
00159 PutPixel(pantalla, x0+incx, y+incy, color);
00160 }
00161 else
00162 {
00163 for (x=x0; x<=x1; x++)
00164 {
00165 y=(((y1-y0)*(x-x0))/(x1-x0))+y0;
00166 PutPixel(pantalla, x+incx, y+incy, color);
00167 }
00168 }
00169 }
00170
00171 void PutPixel(SDL_Surface *pantalla, int x, int y, Uint32 pixel)
00172 {
00173 int bpp=pantalla->format->BytesPerPixel;
00174 Uint8 *p=(Uint8 *)pantalla->pixels + y * pantalla->pitch + x * bpp;
00175
00176 switch (bpp)
00177 {
00178 case 1:
00179 *p=pixel;
00180 break;
00181 case 2:
00182 *(Uint16 *)p=pixel;
00183 break;
00184 case 3:
00185 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00186 {
00187 p[0]=(pixel >> 16) & 0xff;
00188 p[1]=(pixel >> 8) & 0xff;
00189 p[2]=pixel & 0xff;
00190 }
00191 else
00192 {
00193 p[0]=pixel & 0xff;
00194 p[1]=(pixel >> 8) & 0xff;
00195 p[2]=(pixel >> 16) & 0xff;
00196 }
00197 break;
00198 case 4:
00199 *(Uint32 *)p=pixel;
00200 break;
00201 }
00202 }
00203
00204 void Arco(SDL_Surface *pantalla, int x0, int y0, float inicio, float fin, int radio, Uint32 color)
00205 {
00206 int x, y;
00207 float angulo=0, res;
00208
00209 res=(2*PI)/360.0;
00210
00211 for(angulo=inicio; angulo<=fin; angulo+=0.1)
00212 {
00213 x=radio*cos(res*angulo);
00214 y=radio*sin(res*angulo);
00215 PutPixel(pantalla, x+x0, y0-y, color);
00216 }
00217 }