hi, it’s about few hours that i’m using SDL and as a first program i tried to open a window and to draw a fractal in it.
the problem i get is that the cpu goes 100% and i don’t get any output.
here is the code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <SDL.h>
#define FRACT_MANDEL_SET 0
typedef struct _cplane
{
double xMin;
double xMax;
double yMin;
double yMax;
}ComplexPlane;
typedef struct _cnumb
{
double real;
double imm;
}ComplexNumber;
void computeFractal(SDL_Surface *scrn, ComplexPlane cp, int type);
int main(void)
{
SDL_Surface *screen;
SDL_Event event;
ComplexPlane cp = {-1.0, 1.0, -1.0, 1.0};
int quit = 0;
printf("My pid is %d, kill me!\n", getpid()); //used to kill the process
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_putenv("SDL_VIDEO_CENTERED=center");
SDL_WM_SetCaption("sdlFractal", NULL);
if(!(screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE)))
{
fprintf(stderr, "SDL_SetVideoMode(): %s\n", SDL_GetError());
exit(1);
}
computeFractal(screen, cp, 0);
SDL_Flip(screen);
while(!quit)
{
while(SDL_WaitEvent(&event))
if(event.type == SDL_QUIT) { quit = 1; break; }
}
SDL_FreeSurface(screen);
return 0;
}
void computeFractal(SDL_Surface *srfc, ComplexPlane cp, int type)
{
ComplexNumber z_n, c, temp;
uint16_t w, h, n;
uint8_t *buffer, *line, *pixel;
//verify surface
printf("Surface size %d x %d, depth %d \n", srfc -> w, srfc -> h, srfc -> format -> BytesPerPixel);
SDL_LockSurface(srfc);
buffer = (uint8_t *)srfc -> pixels;
line = buffer;
for(h = 0; h < srfc -> h; h++)
{
pixel = line;
for(w = 0; w < srfc -> w; h++)
{
//conversion from discrete screen position to continous complex numbers in the plane
c.real = ((double)w * ((cp.xMax - cp.xMin) / (double)srfc -> w)) + cp.xMin;
c.imm = ((double)h * ((cp.yMax - cp.yMin) / (double)srfc -> h)) + cp.yMin;
z_n = c;
for(n = 0; n < 64; n++)
{
if(((z_n.real * z_n.real) + (z_n.imm * z_n.imm)) > 4.0)
{
switch(srfc -> format -> BytesPerPixel)
{
case 1:
*pixel = n;
break;
case 3://simple coloration - do not care aboit it
pixel[0] = n;
pixel[1] = (n * n) % 256;
pixel[2] = (n * n * n) % 256;
break;
default:
fprintf(stderr, "Pixel depth not yet supported: %d\n", srfc -> format -> BytesPerPixel);
exit(1);
break;
}
break;
}
//else
temp.real = (z_n.real * z_n.real) - (z_n.imm * z_n.imm) + c.real;
temp.imm = 2.0 * z_n.real * z_n.imm + c.imm;
z_n = temp;
}
pixel += srfc -> format -> BytesPerPixel;
}
line += srfc -> pitch;
}
SDL_UnlockSurface(srfc);
}
the procedure of the fractal computation looks correct, so i ask you if i committed some error whit the sdl procedures
bye