/***************************************************************************
* Copyright (C) 2008 by Lorenzo La Porta *
* lorelapo@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <SDL.h>
#include "SDL_gfxPrimitives.h"
#include <math.h>
#define WIDTH 3000
#define HEIGHT 2000
#define BPP 4
#define DEPTH 32
#define ITERATION_DEPTH 1000
#define ITER_COLOR 25
typedef float precision;
typedef struct complex
{
precision r;
precision i;
}complex;
complex square(complex c)
{
complex res;
res.r=c.r*c.r-c.i*c.i;
res.i=2.0*c.r*c.i;
return res;
}
precision module(complex c)
{
return sqrt(c.r*c.r+c.i*c.i);
}
int iterate(complex c)
{
complex z=c;
int i;
for(i=0;i<ITERATION_DEPTH;i++)
{
if(module(z)>2.0)
return i;
z=square(z);
z.r+=c.r;
z.i+=c.i;
}
return i;
}
void mandelbrot(SDL_Surface *surf)
{
complex c;
register int x,y,i;
for(x=0;x<WIDTH;x++)
for(y=0;y<HEIGHT;y++)
{
/*printf("\rCalcolato : %.2f%%",(float)(x*HEIGHT+y)/(WIDTH*HEIGHT)*100.0);
fflush(stdout);*/
c.r=(precision)(x)*4.0/WIDTH-2.5;
c.i=1.5-(precision)(y)*3.0/HEIGHT;
i=iterate(c);
i=255-(i*255/ITERATION_DEPTH);
pixelRGBA(surf,x,y,i*ITER_COLOR,i*(ITER_COLOR-1),i*(ITER_COLOR-2),255);
}
}
int main(int argc, char* argv[])
{
SDL_Surface *screen;
SDL_Event event;
int keypress = 0;
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
if (!(screen = SDL_CreateRGBSurface(SDL_HWACCEL|SDL_HWSURFACE,WIDTH, HEIGHT, DEPTH,0,0,0,0)))
{
SDL_Quit();
return 1;
}
mandelbrot(screen);
SDL_Flip(screen);
sleep(1);
SDL_SaveBMP(screen,"Mandelbrot.bmp");
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}