1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| #include <SDL/SDL.h>
#include <algorithm>
using namespace std;
int pycode(int c_1,int c_2,int c_3)
{
return c_3+c_2*256+c_1*256*256;
}
void definirPixel(SDL_Surface *surface, int x, int y, Uint32 pixel,int nbOctetsParPixel)
{
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;
switch(nbOctetsParPixel)
{
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
}
else
{
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
Uint32 lirePixel(SDL_Surface *surface, int x, int y,int nbOctetsParPixel)
{
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;
switch(nbOctetsParPixel)
{
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
void decale(SDL_Surface *ecran,int x,int y,int nbOctetsParPixel)
{
Uint32 temp[480][600];
for(int i=max(0,x);i<min(600,600+x);i++)
{
for(int j=max(0,y);j<min(480,480+y);j++)
{
temp[j][i]=lirePixel(ecran,i,j,nbOctetsParPixel);
}
}
for(int i=max(0,x);i<min(600,600+x);i++)
{
for(int j=max(0,y);j<min(480,480+y);j++)
{
definirPixel(ecran,i-x,j-y,temp[j][i],nbOctetsParPixel);
}
}
}
int main(int argc, char *argv[])
{
int tx1=0,ty1=0;
int tx2=0;
int ty2=0;
int ri=250;
int vi=0;
int bi=0;
int i;
int couleur[150];
for (i=0;i<25;i++)
{
couleur[i]=pycode(ri,vi,bi);
vi+=10;
}
for (;i<50;i++)
{
couleur[i]=pycode(ri,vi,bi);
ri-=10;
}
for (;i<75;i++)
{
couleur[i]=pycode(ri,vi,bi);
bi+=10;
}
for (;i<100;i++)
{
couleur[i]=pycode(ri,vi,bi);
vi-=10;
}
for (;i<125;i++)
{
couleur[i]=pycode(ri,vi,bi);
ri+=10;
}
for (;i<150;i++)
{
couleur[i]=pycode(ri,vi,bi);
bi-=10;
}
double xa=0,ya=0;
SDL_Event event;
int continuer = 1;
int tempsPrecedent = 0, tempsActuel = 0;
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *ecran =NULL,*Fond = NULL;
SDL_Rect positionFond;
positionFond.x = 0;
positionFond.y = 0;
ecran = SDL_SetVideoMode(600, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Mandelbrot", NULL);
int nbOctetsParPixel = ecran->format->BytesPerPixel;
Fond = SDL_LoadBMP("debut.bmp");
SDL_BlitSurface(Fond, NULL, ecran, &positionFond);
int iteration_max=150;
double zoom=200;
SDL_EnableKeyRepeat(400,200);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_u:
iteration_max+=50;
break;
case SDLK_t:
SDL_SaveBMP(ecran,"mandelbrot.bmp");
break;
case SDLK_RALT:
zoom/=1.5;
break;
case SDLK_r:
zoom=200;
break;
case SDLK_LEFT:
xa-=50/zoom;
tx2=550;
decale(ecran,-50,0,nbOctetsParPixel);
break;
case SDLK_RIGHT:
xa+=50/zoom;
tx1=550;
decale(ecran,50,0,nbOctetsParPixel);
break;
case SDLK_UP:
ya-=50/zoom;
ty2=430;
decale(ecran,0,-50,nbOctetsParPixel);
break;
case SDLK_DOWN:
ya+=50/zoom;
ty1=430;
decale(ecran,0,50,nbOctetsParPixel);
break;
case SDLK_SPACE:
zoom*=1.5;
break;
case SDLK_BACKSPACE:
iteration_max+=20;
break;
case SDLK_e:
iteration_max-=50;
break;
}
long double eca=zoom/200;
long double x1 =xa-1.5/eca;
long double x2 =xa+1.5/eca;
long double y1 =ya-1.2/eca;
long double y2 =ya+1.2/eca;
#pragma omp parallel for schedule(dynamic, 1)
for(int x=tx1;x<600-tx2;x++)
{
for(int y=ty1;y<480-ty2;y++)
{
long double c_r = x / zoom + x1,c_i = y / zoom + y1,z_r = 0,z_i = 0;
int i = 0;
while(z_r*z_r + z_i*z_i <4 && i < iteration_max)
{
long double tmp = z_r;
z_r = z_r*z_r - z_i*z_i + c_r;
z_i = 2*z_i*tmp + c_i;
i++;
}
if (i < iteration_max)
definirPixel(ecran,x,y,couleur[i%150],nbOctetsParPixel);
else
definirPixel(ecran,x,y,0,nbOctetsParPixel);
}
}
tx1=0;
ty1=0;
tx2=0;
ty2=0;
}
SDL_Flip(ecran);
}
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager