Tille - I can see you, read those man pages!   Tille's Site

Adding functionality

In the following example, we make use of the Show_BMP() function from the libsdl.org tutor.

/* Program name: testscreen3.c */
/* SDL screen test display image */
 
/* ALways needed for SDL. */
# include <SDL.h>
 
/* For standard functions. */
# include <stdlib.h>
 
/* More standard functions. */
# include <unistd.h>

/* Functions are declared before main. */

/* Begin ShowBMP. */ 
void ShowBMP(char *file, SDL_Surface *screen, int x, int y)
{
    SDL_Surface *image;
    SDL_Rect dest;
 
    /* Load the BMP file into a surface */
    image = SDL_LoadBMP(file);
    if ( image == NULL ) {
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
        return;
    }
 
    /* Blit onto the screen surface.
       The surfaces should not be locked at this point.
     */
    dest.x = x;
    dest.y = y;
    dest.w = image->w;
    dest.h = image->h;
    SDL_BlitSurface(image, NULL, screen, &dest);
 
    /* Update the changed portion of the screen */
    SDL_UpdateRects(screen, 1, &dest);
 
    SDL_FreeSurface(image);
}
/* End Show_BMP. */ 

int main()
{
  SDL_Surface *screen;
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
  }
 
  atexit(SDL_Quit);
 
  screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
  if ( screen == NULL ) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
  }
 
  printf("Set window of 640x480 at %d bits per pixel mode\n",
        screen->format->BitsPerPixel);

/* Call to the Show_BMP function. */
/* Make this point to a test.bmp file on your system, use the convert
command to create a bmp file from anything else, should you not have one. */ 
  ShowBMP("/nethome/tille/C/SDL/screen/screen3/ooktille.bmp", screen, 0, 0);
  ShowBMP("/nethome/tille/C/SDL/screen/screen3/ooktille.bmp", screen, 100,100);
 
  sleep(5);
 
  printf("Quitting SDL.\n");
   
  return(0);
}

After running make on the edited Makefile, this is the sort of thing you should see:

X window displaying an image.

Testing and using arguments

The following is an example similar to the one before, but now you give the image to display as an argument on the command line. If more than one argument is given, or none, the program exits. Else, it displays the specified file.

/* Program name: testscreen4.c */
/* SDL screen test display image */
                                                                                
/* ALways needed for SDL. */
# include <SDL.h>

/* For standard functions. */
# include <stdlib.h>

/* More standard functions. */
# include <unistd.h>

void ShowBMP(char *file, SDL_Surface *screen, int x, int y)
{
    SDL_Surface *image;
    SDL_Rect dest;
                                                                                
    /* Load the BMP file into a surface */
    image = SDL_LoadBMP(file);
    if ( image == NULL ) {
        fprintf(stderr, "Error loading %s: %s\n", file, SDL_GetError());
        return;
    }
                                                                                
    /* Blit onto the screen surface.
       The surfaces should not be locked at this point.
     */
    dest.x = x;
    dest.y = y;
    dest.w = image->w;
    dest.h = image->h;
    SDL_BlitSurface(image, NULL, screen, &dest);
                                                                                
    /* Update the changed portion of the screen */
    SDL_UpdateRects(screen, 1, &dest);
                                                                                
    SDL_FreeSurface(image);
}

/* telling main not to ignore arguments: */
int main(int argc, char *argv[])
{

/* If the number of arguments is not exactly one, quit program. */

  if (argc != 2) {
    printf("Number of arguments:%d\n", argc - 1);
    printf("Usage: testscreen4 [file]\n");
    exit(1);
  }
                                                                                
  SDL_Surface *screen;
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
  }
                                                                                
  atexit(SDL_Quit);
                                                                                
  screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
  if ( screen == NULL ) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
  }
                                                                                
  printf("Set window of 640x480 at %d bits per pixel mode\n",
        screen->format->BitsPerPixel);

/* Show the file specified on the command line. */
              
  ShowBMP(argv[1], screen, 0, 0);
                                                                                
  sleep(5);
                                                                                
  printf("Quitting SDL.\n");
                                                                                
  return(0);
}

Exercise

Use the above example for including a function, and include the Draw_Pixel function from the libsdl tutor to draw a cross on the screen.

Click here for the solution.

And here for the next chapter.

Home
© 1995-2010 Machtelt Garrels - tille - Powered by vIm - Best viewed with your eyes - Validated by W3C - Last update 20100511