I wrote a mandelbrot set progam in c a while back and posted it here at cprogramming .. one of the comments I got was there were too many global variables ..
having problems with a new install of linux as well as trying to fix my program I rewrote it and it works like it used too .. Im using debian lenny amd64 2.6.26-2-amd64 kernel and gcc ( debian 4.3.2-1.1 ) 4.3.2
however 1 ) Im not sure what I wrote is an improvement and .. 2 ) I would like to use color smoothing but even after reading a lot of forums I still not sure how to implement it ..
so heres my code any hints appreciated regards improvements even if it means starting from scratch ..
first program
Code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
struct foo{
int xscreen,yscreen,Nxscreen,Nyscreen;
int base,max,a,b,c,d,x,y,z;
Uint32 color;
long double s,t,u,v;
long double real,imaginary,ximage,yimage,xoff,yoff;
}bar;
SDL_Surface *screen;
SDL_Event event;
int number_base();
void change_color ( int count );
void sdl_screen_size();
void image();
void mousebutton_event();
void putpixel();
void iterate();
void real_imaginary();
void lock_screen();
void unlock_screen();
void set_sdl();
void set_struct();
void sdl_screen_size(){
struct foo *p;
p = &bar;
int t;
p->Nxscreen = p->a - p->c;
p->Nyscreen = p->b - p->d;
if( p->Nxscreen > p->Nyscreen ){
t = p->xscreen / p->Nxscreen;
}
else{
t = p->yscreen / p->Nyscreen;
}
if( t > 1 ){
p->Nxscreen = p->Nxscreen * t;
p->Nyscreen = p->Nyscreen * t;
}
printf("sdl_screen_size\n Nxscreen = %i Nyscreen %i\n"
,p->Nxscreen,p->Nyscreen );
}
void image(){
struct foo *p;
p = &bar;
p->s = (( p->ximage * p->c ) / p->Nxscreen ) - p->xoff;
p->u = (( p->ximage * p->a ) / p->Nxscreen ) - p->xoff;
p->t = p->yoff - (( p->yimage * p->d ) / p->Nyscreen );
p->v = p->yoff - (( p->yimage * p->b ) / p->Nyscreen );
p->ximage = p->u - p->s;
p->yimage = p->t - p->v;
p->xoff = -p->s;
p->yoff = p->t;
printf("s = %Lf\n t = %Lf\n u = %Lf\n v = %Lf\n"
,p->s,p->t,p->u,p->v );
printf("ximage = %Lf\n yimage = %Lf\n xoff = %Lf\n yoff %Lf\n"
,p->ximage,p->yimage,p->xoff, p->yoff );
}
void mousebutton_event(){
struct foo *p;
p = &bar;
if( p->z % 2 == 1 ){
p->c = event.button.x;
p->d = event.button.y;
printf("1 ) mousebutton_event z = %i\n",p->z );
}
if( p->z % 2 == 0 ){
p->a = event.button.x;
p->b = event.button.y;
printf("2 ) mousebutton_event z = %i\n",p->z );
}
printf("%i %i %i %i\n",p->c,p->d,p->a,p->b );
}
void putpixel(){
struct foo *p;
p = &bar;
Uint32 *bufp;
bufp = ( Uint32 *)screen->pixels + p->y*screen->pitch / 4 + p->x;
*bufp = p->color;
}
int number_base( int c ){
struct foo *p;
p = &bar;
int d, r, red = 0, green = 0, blue = 0, count = 1;
while(( c % p->base ) != 0 ){
d = c / p->base;
r = c % p->base;
switch( count ){
case 1: red = r;
break;
case 2: green = r;
break;
case 3: blue = r;
break;
default:printf("........ happens\n");
}
c = d;
++count;
}
p->color = SDL_MapRGB(screen->format,
(( red ) * 16 ),(( green ) * 32 ),(( blue ) * 64 ));
}
void change_color( int count ){
struct foo *p;
p = &bar;
if ( count < p->max ){
putpixel( p->color = number_base( count ));
}
if ( count == p->max ){
putpixel( p->color = SDL_MapRGB(screen->format,0,0,0));
}
}
void iterate(){
struct foo *p;
p = &bar;
int count;
long double xx,yy,tempr = p->real,tempi = p->imaginary;
for( count = 1; count < p->max &&
abs( tempr * tempr + tempi * tempi ) < 4;
count++){
xx = ( tempr * tempr ) - ( tempi * tempi );
yy = 2 * tempr * tempi;
tempr = p->real + xx;
tempi = p->imaginary + yy;
}
change_color( count );
}
void real_imaginary(){
struct foo *p;
p = &bar;
printf("real_imaginary\n");
for( p->x = 1; p->x < p->Nxscreen; p->x = p->x + 1 ){
for( p->y = 1; p->y < p->Nyscreen; p->y = p->y + 1 ){
p->real = (( p->x * p->ximage ) / p->Nxscreen
- p->xoff);
p->imaginary = p->yoff - (( p->y * p->yimage )
/ p->Nyscreen );
iterate();
}SDL_UpdateRect( screen,0,0,p->Nxscreen,p->Nyscreen );
}
}
void lock_screen(){
printf("lock_screen\n");
if( SDL_MUSTLOCK( screen )){
if( SDL_LockSurface( screen ) < 0 ){
return;
}
}
real_imaginary();
printf("unlock_screen\n");
if( SDL_MUSTLOCK( screen )){
SDL_UnlockSurface( screen );
}
}
void set_sdl(){
printf("set_sdl\n");
struct foo *p;
p = &bar;
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
SDL_WM_SetCaption("tp.c",NULL);
screen = SDL_SetVideoMode( p->Nxscreen,p->Nyscreen,
32,SDL_SWSURFACE );
}
void set_struct(){
struct foo *p;
p = &bar;
p->xscreen = 809;
p->yscreen = 500;
p->Nxscreen = 809;
p->Nyscreen = 500;
p->max = 32768;
p->base = 32;
p->z = 1;
p->ximage = 3.2;
p->yimage = 2.0;
p->xoff = 2.2;
p->yoff = 1.0;
}
int main( int argc, char * argv[] ){
struct foo *p;
p = &bar;
printf("main\n");
set_struct();
set_sdl();
lock_screen();
while( SDL_WaitEvent( &event )){
switch( event.type ){
case SDL_MOUSEBUTTONDOWN:{
mousebutton_event();
if( p->z % 2 == 0 ){
image();
sdl_screen_size();
set_sdl();
lock_screen();
}
p->z = p->z + 1;
break;
}
case SDL_KEYDOWN:{
if( event.key.keysym.sym == SDLK_ESCAPE ){
SDL_Quit();
}
if( event.key.keysym.sym == SDLK_s ){
SDL_SaveBMP( screen, "shot.bmp" );
}
break;
}
// default: printf("........ happens\n");
}
}
return ( 0 );
}
and the second time around
Code:
#include<stdio.h>
#include<stdlib.h>
#include<SDL/SDL.h>
#define xscreen 1618
#define yscreen 1000
#define MAX 4096
#define BASE 16
SDL_Surface *screen;
SDL_Event event;
struct sdl_window{
int Nxscreen, Nyscreen;
};
struct sdl_image{
long double ximage,yimage,xoff,yoff;
};
struct sdl_button{
int a,b,c,d;
};
int image_size( struct sdl_button* button,
struct sdl_window* window,struct sdl_image* image );
int window_size( struct sdl_button* button, struct sdl_window* window );
int mouse( int z, struct sdl_button* button );
void putpixel( int x, int y, Uint32 colors );
Uint32 color( int count );
int multiply(long double real, long double imaginary );
int multiply(long double real, long double imaginary );
void numbers( struct sdl_window* window, struct sdl_image* image );
void unlock();
void lock();
void sdl( struct sdl_window* window );
// image size used to find new images size so it fits new sdl window
int image_size( struct sdl_button* button,
struct sdl_window* window,struct sdl_image* image ){
printf("image_size\n");
long double s,t,u,v; // four corners
s = (( image->ximage * button->c ) / window->Nxscreen ) - image->xoff;
u = (( image->ximage * button->a ) / window->Nxscreen ) - image->xoff;
t = image->yoff - (( image->yimage * button->d ) / window->Nyscreen );
v = image->yoff - (( image->yimage * button->b ) / window->Nyscreen );
image->ximage = u - s;
image->yimage = t - v;
image->xoff = -s;
image->yoff = t;
printf("s = %Lf\n t = %Lf\n u = %Lf\n v = %Lf\n"
,s,t,u,v);
printf("ximage = %Lf\n yimage = %Lf\n xoff = %Lf\n yoff %Lf\n"
,image->ximage,image->yimage,image->xoff, image->yoff );
return image;
}
// window size used to find new dimensions of sdl window when zooming in
int window_size( struct sdl_button* button, struct sdl_window* window ){
printf("window_size\n");
int t;
window->Nxscreen = button->a - button->c;
window->Nyscreen = button->b - button->d;
if( window->Nxscreen > window->Nyscreen ){
t = xscreen / window->Nxscreen;
}
else{
t = yscreen / window->Nyscreen;
}
if( t > 1 ){
window->Nxscreen = window->Nxscreen * t;
window->Nyscreen = window->Nyscreen * t;
}
printf(" Nxscreen = %i Nyscreen %i\n",window->Nxscreen,window->Nyscreen );
return window;
}
// mouse uses sdl to return mousebutton events. I am using two events high left // to low right so I can zoom in.
int mouse( int z,struct sdl_button* button ){
printf("mouse\n");
if( z == 1 ){
button->c = event.button.x;
button->d = event.button.y;
printf("c = %i d = %i\n",button->c,button->d);
}
if( z != 1 ){
button->a = event.button.x;
button->b = event.button.y;
printf("a = %i b = %i\n",button->a,button->b);
}
return button;
}
void putpixel( int x, int y, Uint32 colors ){
Uint32 *bufp;
bufp = ( Uint32 *)screen->pixels + y*screen->pitch / 4 + x;
*bufp = colors;
}
Uint32 color( int count ){
int d, r, c = 1, red = 0, green = 0, blue = 0;
Uint32 colors;
while(( count % BASE ) != 0 ){
d = count / BASE;
r = count % BASE;
switch( c ){
case 1: red = r;
break;
case 2: green = r;
break;
case 3: blue = r;
break;
default:printf("........ happens\n");
}
count = d;
++c;
}
return colors = SDL_MapRGB(screen->format,
( red * 16 ),( green * 32 ),( blue * 64 ));
}
// multiply is an iterative process returns a count which is
// converted into a color.
int multiply(long double real, long double imaginary ){
int count;
long double xx,yy,tempr = real,tempi = imaginary;
for( count = 1; count < MAX &&
abs( tempr * tempr + tempi * tempi ) < 4;
count++){
xx = ( tempr * tempr ) - ( tempi * tempi );
yy = 2 * tempr * tempi;
tempr = real + xx;
tempi = imaginary + yy;
}
return count;
}
// numbers takes sdl window and gives an imaginary number for each pixel.
void numbers( struct sdl_window* window, struct sdl_image* image ){
int x,y;
long double real,imaginary;
printf("numbers\n");
for( x = 1; x < window->Nxscreen; x++ ){
for( y = 1; y < window->Nyscreen; y++ ){
real = (( x * image->ximage ) / window->Nxscreen
- image->xoff);
imaginary = image->yoff - (( y * image->yimage )
/ window->Nyscreen );
putpixel(x, y, ( color( multiply(real,imaginary ))));
}SDL_UpdateRect( screen,0,0,window->Nxscreen,window->Nyscreen );
}
}
void unlock(){
printf("unlock\n");
if( SDL_MUSTLOCK( screen )){
SDL_UnlockSurface( screen );
}
}
void lock(){
printf("lock\n");
if( SDL_MUSTLOCK( screen )){
if( SDL_LockSurface( screen ) < 0 ){
return;
}
}
}
void sdl( struct sdl_window* window ){
printf("sdl\n");
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
SDL_WM_SetCaption("schmatic-4.c",NULL);
screen = SDL_SetVideoMode
( window->Nxscreen,window->Nyscreen,
32,SDL_SWSURFACE );
}
int main( int argc, char * argv[] ){
printf("main\n");
struct sdl_window wdw;
struct sdl_window *window;
window = &wdw;
struct sdl_image img;
struct sdl_image *image;
image = &img;
struct sdl_button bttn;
struct sdl_button *button;
button = &bttn;
int z = 1;
window->Nxscreen = xscreen;
window->Nyscreen = yscreen;
image->ximage = 3.2;
image->yimage = 2.0;
image->xoff = 2.2;
image->yoff = 1.0;
printf("start\n");
sdl( window );
lock();
numbers( window,image );
unlock();
while( SDL_WaitEvent( &event )){
switch( event.type ){
case SDL_MOUSEBUTTONDOWN:{
mouse( z,button );
if( z != 1 ){
image_size( button,window,image );
window_size( button,window );
sdl( window );
lock();
numbers( window,image);
unlock();
}
z = -z;
break;
}
case SDL_KEYDOWN:{
if( event.key.keysym.sym == SDLK_ESCAPE ){
SDL_Quit();
}
if( event.key.keysym.sym == SDLK_s ){
SDL_SaveBMP( screen, "shot.bmp" );
}
break;
}
}}
getchar();
return;
}
thanks for taking the time to read al.