Thread: mousebutton event sdl

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    mousebutton event sdl

    I have been trying to create an sdl screem amd a ) printf co ordinates of mousebutton event
    to console, b ) if that doesnt work and it doesnt .. try fprintf(stderr
    message the co ordinates but no luck on that .. not sure I have set it
    correctly or c ) create a test file print the co ordinates to the file.

    at present no luck .. can anyone hlep me here ..

    thanks in advance al.


    Code:
    //-lmingw32 -lSDLmain -lSDL
    #include <stdio.h>
    #include <stdlib.h>
    #include <SDL/SDL.h>
    
    struct sdl_screen{
         int x;
         int y;
    } sdlscreen;
    
    SDL_Event event;
    SDL_Surface *screen;
    
    int begin_sdl_screen(){
         struct sdl_screen *p;
         p = &sdlscreen;
         // 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);
         }
    
    }
    int set_screen(){
         struct sdl_screen *p;
         p = &sdlscreen;
    
         p->x = 400;
         p->y = 200;
    
         printf("x %i y %i\n",p->x,p->y );
    }
    int main(int argc, char *argv[])
    {
         FILE *f;
         
         char ch;
         set_screen();
         begin_sdl_screen();
    
         while ( SDL_PollEvent(&event) ) {
            switch (event.type) {
                   case SDL_MOUSEBUTTONDOWN:{
                        //fprintf(stderr, "Mouse button %d pressed at (%d,%d)\n"
                          //   ,event.button.button, event.button.x, event.button.y);
                        f = fopen("test.txt","w");
                        fprintf(f,"Mouse button %d pressed at (%d,%d)\n",event.button.button, event.button.x, event.button.y);
                        fclose( f );
                        }
                   }
              }  //   SDL_Quit();
         ch = getchar();
      return 0;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You will need to set up a main loop for your progam to run it. Also case statements dont have brackets, but end with 'break'. Currently your program starts, does some stuff, polls input oce then quit. Something like:
    Code:
    int quit = 0;
    while(! quit)
    {
        while ( SDL_PollEvent(&event) ) {
            switch (event.type) {
                case SDL_MOUSEBUTTONDOWN:           
                     doSomething();
                     break;
                 case: SDL_KEYDOWN:
                     quit = 1;
                     break;
             } 
        } 
    }

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ...Also case statements dont have brackets,...
    It is perfectly acceptable for case statements to have brackets. In fact it's useful since any variable declared in a case statement is actually visible to all cases. The brackets are a scoping mechanism and are very useful.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh, maybe. In all the examples I have seen they don't use braces, but yeah It adds scoping which could be useful. Its still a good idea important to break out of the switch case.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    got it thanks

    had a play with the code .. got this working ..

    normallly I would put the breaks in must have been tired when I was playing with it ..

    thanks for the help much appreciated al.
    Code:
    while( SDL_WaitEvent(&event)){
         	switch(event.type ){
         		case SDL_MOUSEBUTTONDOWN:{
         			print_result();
         	if( n > 1.0 )
         		SDL_Quit();
    		break;
    		}
    		default: printf("try again\n");
         		}
           	}
    int print_result(){
         printf("Mouse button %i pressed at x %i y %i\n"
        ,event.button.button, event.button.x, event.button.y);
    }

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    next step ..

    this was the next step in my plan ..

    create a sdl window and using two mousebutton events resize the screen. I have a template size so that the screen doesnt just get smaller and smaller ..

    anyways here it is constructive critisim greatly appreciated al.
    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 );
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Names like fun() and fun2() aren't very descriptive. rescale_screen() might be better, for example.

    Both fun() and fun2() are declared to return int, but never return anything. Your compiler should warn you about things like this if you enable warnings. Since it looks like you're using Dev-C++, add -W -Wall to the "extra compiler flags" textbox in the compiler options dialog to enable them.

    Code:
    int main(int argc, char *argv[]){
         char ch;
         /* ... */
         ch = getchar();
         return ( 0 );
    }
    You don't have to save the return value of getchar() in a variable if you're not going to use it.

    Also -- getchar() isn't perhaps the best way to keep a console open for a graphical program . . . .

    Finally, your indentation could use some work. while_away_screen(), for example.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    thanks

    for the comments .. I appreciate it ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. SDL & Direct3D - Event loop clogged up?
    By cboard_member in forum Game Programming
    Replies: 6
    Last Post: 04-11-2006, 02:29 AM
  3. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  4. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM