Code:
/* objective create screem amd a ) use two mousebutton events to redraw
screen. the mousebutton events must take place high left to low right.
all printf statements appear in a stdout.txt once the sdl window closes.
Im using getchar() to close the program.
-lmingw32 -lSDLmain -lSDL */
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
/* screen size. Im using sscreen for most of the program as sdl screen size
while vfscreen is very first screen */
struct sdl_screen{
int x;
int y;
int z;
}vfscreen, sscreen;
/* records the two mousebutton events */
struct mousebutton{
int x;
int xx;
int y;
int yy;
}button;
SDL_Surface *screen;
SDL_Event event;
/* finds the new screen legnths resizes screen writes to ssdcreen
calls draw function again. fun and fun2 are for whether the x or y
direction is bigger. the buigger is used for the rezizing */
int fun( int nsdlx,int nsdly ){
struct sdl_screen *p;
struct sdl_screen *pp;
p = &vfscreen;
pp = &sscreen;
int t;
printf("function 1\n");
printf("x > y\n");
printf("x = %i y = %i\n",nsdlx,nsdly);
t = p->x / nsdlx;
printf("t = %i\n",t);
pp->x = nsdlx * t;
pp->y = nsdly * t;
printf("function 1 new screen lengths\n \
%i %i\n",pp->x,pp->y);
draw();
}
int fun2( int nsdlx,int nsdly ){
struct sdl_screen *p;
struct sdl_screen *pp;
p = &vfscreen;
pp = &sscreen;
int t;
printf("function 2\n");
printf("y > x\n");
printf("x = %i y = %i\n",nsdlx,nsdly);
t = p->y / nsdly;
printf("t = %i\n",t);
pp->x = nsdlx * t;
pp->y = nsdly * t;
printf("function 2 new screen lengths\n \
%i %i\n",pp->x,pp->y);
draw();
}
/* could have done it easier but I already had the code. trying to make things
easier by using two functions to do two jobs rather than one doing many */
int new_length( int nsdlx, int nsdly ){
printf("new length function\n");
if( nsdlx > nsdly ) fun( nsdlx, nsdly );
else fun2( nsdlx, nsdly );
}
/* records the mousebutton events finds the difference between the two
mousebutton events. I am resizing the screen so the screen stays a
relative size */
int co_ordinates_of_mousebutton(){
struct mousebutton *p;
p = &button;
int nsdlx,nsdly;
printf("co ordinates of mousebutton function\n");
printf("finding new lengths in mousebutton function\n");
printf("x %i y %i xx %i yy %i\n",p->x,p->y,p->xx,p->yy);
if( p->xx > p->x && p->yy > p->y ){
nsdlx = p->xx - p->x;
nsdly = p->yy - p->y;
printf("lengths nsdlx %i and nsdly %i\n",nsdlx,nsdly );
}
else{
printf("numbers selected for sdl_new_screen outside bounds\n");
}
new_length( nsdlx,nsdly );
}
/* originally a check in tp.c proof but now a way of entering the two mousebutton events
into a struct. */
int print_result(){
struct mousebutton *p;
struct sdl_screen *pp;
p = &button;
pp = &sscreen;
printf("print_result function\n");
printf("%i )mouse button %i pressed at x %i y %i\n",pp->z, event.button.button, event.button.x, event.button.y);
if( pp->z % 2 == 0 ){
p->xx = event.button.x;
p->yy = event.button.y;
co_ordinates_of_mousebutton();
}
else{
p->x = event.button.x;
p->y = event.button.y;
}
printf("mouse button pressed at x %i y %i and xx %i yy %i\n"
, p->x,p->y,p->xx,p->yy);
}
/* draw what it says ..:p */
/* uaing one pointer to point to two seperate structures vfscreen the first
sdl screen only and sscreen for every one after. used an if statement and
an extra element in sscreen to provide a choose mechanism */
int draw(){
struct sdl_screen *p;
p = &sscreen;
if( p->z == 1 ){
p = &vfscreen;
}
printf("draw_screen\n");
printf("p->x %i p->y %i\n",p->x,p->y );
// initialize SDL's subsystems - in this case, only video.
if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0 ){
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_WM_SetCaption ("MANDELBROT SET", NULL);
// attempt to create a 200X200 window with 32bit pixels.
screen = SDL_SetVideoMode(p->x, p->y, 32, SDL_SWSURFACE );
// if we fail, return error.
if ( screen == NULL ){
fprintf(stderr, "Unable to set 200X200 video: %s\n", SDL_GetError());
exit(1);
}
SDL_Delay( 5000 );
}
/* provides original sdl screen size. I want to make program interactive later
thats why I havent hardwired it in. */
int sdl_screen( ){
struct sdl_screen *p;
p = &vfscreen;
printf("set_sdl_screen\n");
p->x = 800;
p->y = 500;
p->z = 1;
printf("screen size x %i y %i\n",p->x,p->y);
draw();
}
/* event type to recognise mousebutton events and a sdl exit point.
the structure is a counter .. Im learning so its more fun than using
a global int count */
int while_away_screen(){
struct sdl_screen *p;
p = &sscreen;
p->z = 1;
while( SDL_WaitEvent(&event)){
switch(event.type ){
case SDL_MOUSEBUTTONDOWN:{
print_result();
p->z = p->z + 1;
break;
}
case SDL_KEYDOWN:{
if ( event.key.keysym.sym == SDLK_ESCAPE ){
SDL_Quit();
break;
}
}}}
}
int main(int argc, char *argv[]){
char ch;
printf("main function\n");
sdl_screen();
while_away_screen();
ch = getchar();
return ( 0 );
}