home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue2 / SDL.ARC / rwsdlex1 / c / main
Encoding:
Text File  |  2006-07-10  |  7.3 KB  |  191 lines

  1.  
  2. // main.c part of a RISC OS SDL tutorial by Neil White
  3.  
  4. // This line tells the conmpiler we will be using the SDL library
  5. #include <SDL/SDL.h>
  6. // This includes the C standard library, for basic maths i/o functions etc.
  7. #include <stdlib.h>
  8. // This Includes string.h which has basic string functions.
  9. #include <string.h>
  10. // All images, including the actual screen area are defined as surfaces, here we define a poinetr to 'screen' which we will use as the main windows image. All other images are plotted to this surface to be displayed on screen.
  11. SDL_Surface *screen;
  12. // This sets aside a surface for where we will keep the Risc World logo. And a surface we can use for the background.
  13. SDL_Surface *background;
  14. SDL_Surface *rwlogo;
  15. // This sets the variable we will use to get mouse events.
  16. SDL_MouseMotionEvent mmevent;
  17. // int defines an integer, here for the x and y positions of the mouse.
  18. int mousex,mousey;
  19. // This defines unsigned 32 bit integers needed, in this case 'secs' and 'cents' for holding information about time for control of the speed of the program.
  20. Uint32 sec, cents;
  21. // This sets a pointer we can use for collecting data from a file on disk.
  22. FILE *file_ptr;
  23. // Some more integers, in this case if we set quit to 1 the code will quit, bounce factor is how much the logo will move, rwx and rwy are the coordinates of the logo and bouncing is used to tell the program if the logo is bouncing or not.
  24. int quit = 0,bouncefactor=0,rwy=60,rwx=60,bouncing=0;
  25.  
  26. // This function is used when we want to plot an image to the screen surface SDL_Surface (in this function called image) is the image we are sending to the function, x and y are the co-ordinates we sent to the function.
  27.  
  28. void imageplot(SDL_Surface *image, int x, int y)
  29. {
  30. // An SDL_Rect holds the width height and x,y positions of a rectangle, here we define a SDL rectangle called pos.
  31.     SDL_Rect pos;
  32. // Set the x and y value of the rectangle to the x and y value sent to the function
  33.     pos.x = x;
  34.     pos.y = y ;
  35. // Now we blit (plot) the surface image into the screen surface at the x and y positions we sent to the function
  36.     SDL_BlitSurface(image, NULL, screen, &pos);
  37. // Note- all SDL co-ordinates origin from the top left of the surface
  38. }
  39.  
  40. // This funtion will load the images we want when called
  41. SDL_Surface *loadimage(char *name)
  42. {
  43. // Define some surfaces for use within this function
  44.     SDL_Surface *surface;
  45.     SDL_Surface *image;
  46. // Define a string to hold the location of the file we want to load
  47.     char path[256];
  48. // In the Makefile we defined DATA_PREFIX as <rwsdlex1$dir>/ this line makes that the start opf our files location
  49.     strcpy(path, DATA_PREFIX);
  50. // Now we add gfx/, the directory the images are stored
  51.     strcat(path, "gfx/");
  52. // Here we add the name of the file that was sent to the function
  53.     strcat(path, name);
  54. // And we add the exstention of the file
  55.     strcat(path,".bmp");
  56. // so now our string 'char' contains '<rwsdlex1$dir>/gfx/image.bmp'
  57. // The main SDL library has support for windows bitmap files (bmp) so we can load bmp's without using any other libraries. This line loads the bmp into the temprary surface we defined for this function.
  58.     surface = SDL_LoadBMP(path);
  59. // this is a little error trap for if we cant find the file.
  60.     if (surface == NULL)
  61.     {
  62.         fprintf(stderr, "Can't load image %s", path);
  63.         return NULL;
  64.     }
  65. // this tells SDL what colour to treat as transparent.
  66.     SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, 255,255,255));
  67. // now we have set the transparency colour as white, we send the new image to another temporary surface.
  68.     image = SDL_DisplayFormat(surface);
  69. // free up some  memory it is a good idea to use lots of error traps and make sure all memory is accounted for.
  70.     SDL_FreeSurface(surface);
  71. // now we return our loaded image to the surface we used the function for.
  72.     return image;
  73. }
  74.  
  75. // This function is used to load all the images we want using the loadimage function above.
  76. static int loadimages()
  77. {
  78.  
  79.         rwlogo = loadimage( "rwlogo" );
  80.         background     = loadimage( "background" );
  81. // by returing 1 we can tell if the function as worked or not.
  82.     return 1;
  83. }
  84.  
  85. // a function used for checking events, called checkkeys, but at this stage it only contains code for detecting mouse events.
  86. static void checkkeys(void)
  87. {
  88. // define the approprate variable type to hold event information.
  89.     SDL_Event event;
  90. // this is a polling loop
  91.     while (SDL_PollEvent(&event) != 0)
  92.     {
  93. // ask SDL if the mouse button has been pressed, if it has, set the bouncefactor to 8, which will start the logo bouncing.
  94.       if(SDL_GetMouseState(NULL,NULL)&SDL_BUTTON(1)){bouncefactor=8;}
  95.         switch(event.type)
  96.         {
  97. // SDL_QUIT is if either the x on the window has been clicked or escape has been pressed, if this happens we set quit to 1 and the code will quit.
  98.         case SDL_QUIT:
  99.             quit = 1;
  100. // This breaks from the loop so we can quit.
  101.             break;
  102.         }
  103.     }
  104. }
  105.  
  106. // This function is the workings for the bounce mechanism.
  107. static void bouncer(void)
  108. {
  109. // bouncefactor is how many pixels the logo will move each step.
  110.   if ( bouncefactor>0 )
  111.   {
  112. // Here the logo will move up until it is less than 2 pixels from the top of the screen, then switch to going down.
  113.   if ( bouncing==1 )
  114.   {
  115.     rwy-=bouncefactor;
  116.     if (rwy<2) { bouncing=0; bouncefactor-=1; }
  117.   }
  118. // here it will move down until the logo is over 100 pixels down the screen, then switch to going up.
  119.   if ( bouncing==0 )
  120.   {
  121.     rwy+=bouncefactor;
  122.     if (rwy>100) { bouncing=1; bouncefactor-=1; }
  123.   }
  124.   }
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. // All C programs require a main() function to run, this is where all the previously defined functions are called.
  132.  
  133. // argc and argv are for collecting any extra information on the command line, usually used for setting nosound or fullscreen, which does not apply here.
  134.  
  135. int main(int argc, char *argv[])
  136. {
  137.  
  138.     // Initialize SDL
  139.     if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 ) {
  140.         fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  141.         return EXIT_FAILURE;
  142.     }
  143.     atexit(SDL_Quit);
  144.  
  145. // Set video mode ( x width , y height , Bits per pixel and pallette information )
  146.     if ( (screen=SDL_SetVideoMode(320,320,8, SDL_HWSURFACE|SDL_HWPALETTE)) == NULL )
  147.     {
  148.         return EXIT_FAILURE;
  149.     }
  150. // Calling the loadimage function to load all the images.
  151.     if (!loadimages()) return EXIT_FAILURE;
  152. // Set the title of the SDL window.
  153.         SDL_WM_SetCaption("Risc World SDL Example 1", 0);
  154. // Tell SDL if we want the cursor or not, in this case we will leave it on ( 1 ).
  155.     SDL_ShowCursor( 1 );
  156.  
  157. // Collect time information for controlling the speed of the program.
  158.     sec = SDL_GetTicks();
  159. // The beginning of the main loop.
  160.     do
  161.     {
  162. // more time controll stuff, here we get the time and wait 80 centeseconds then continue with the program.
  163.                 cents = SDL_GetTicks();
  164.                 if (cents < sec + 80) SDL_Delay(sec + 80 - cents);
  165.                 sec = SDL_GetTicks();
  166. // Calling the checkkeys function to see if the mouse has been clicked.
  167.                                 checkkeys();
  168. // Plot the background image.
  169.                                 imageplot(background,0,0);
  170. // Plot the Risc World Logo.
  171.                                 imageplot(rwlogo,rwx,rwy);
  172. // Move the image if clicked.
  173.                                 bouncer();
  174. // SDL_Flip(screen) will redraw the entire screen.
  175.                 SDL_Flip(screen);
  176.  
  177. // The end of the main loop, if quit has been detected the program will stop.
  178.     }
  179.     while (quit == 0);
  180. // We assume everything went ok and the code quits.
  181.     return EXIT_SUCCESS;
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.