Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. /* Simple program:  Test bitmap blits */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #include "SDL.h"
  9. #include "picture.xbm"
  10.  
  11. SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
  12. {
  13.         SDL_Surface *bitmap;
  14.         Uint8 *line;
  15.  
  16.         /* Allocate the bitmap */
  17.         bitmap = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 1, 0, 0, 0, 0);
  18.         if ( bitmap == NULL ) {
  19.                 fprintf(stderr, "Couldn't allocate bitmap: %s\n",
  20.                                                 SDL_GetError());
  21.                 return(NULL);
  22.         }
  23.  
  24.         /* Copy the pixels */
  25.         line = (Uint8 *)bitmap->pixels;
  26.         w = (w+7)/8;
  27.         while ( h-- ) {
  28.                 memcpy(line, bits, w);
  29.                 /* X11 Bitmap images have the bits reversed */
  30.                 { int i, j; Uint8 *buf, byte;
  31.                         for ( buf=line, i=0; i<w; ++i, ++buf ) {
  32.                                 byte = *buf;
  33.                                 *buf = 0;
  34.                                 for ( j=7; j>=0; --j ) {
  35.                                         *buf |= (byte&0x01)<<j;
  36.                                         byte >>= 1;
  37.                                 }
  38.                         }
  39.                 }
  40.                 line += bitmap->pitch;
  41.                 bits += w;
  42.         }
  43.         return(bitmap);
  44. }
  45.  
  46. void app_main(int argc, char *argv[])
  47. {
  48.         SDL_Surface *screen;
  49.         SDL_Surface *bitmap;
  50.         Uint8  video_bpp;
  51.         Uint32 videoflags;
  52.         Uint8 *buffer;
  53.         int i, done;
  54.         SDL_Event event;
  55.  
  56.         /* Initialize SDL */
  57.         if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  58.                 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
  59.                 exit(1);
  60.         }
  61.         atexit(SDL_Quit);
  62.  
  63.         video_bpp = 0;
  64.         videoflags = SDL_SWSURFACE;
  65.         /* Set 640x480 video mode */
  66.         if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
  67.                 fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
  68.                                                 video_bpp, SDL_GetError());
  69.                 exit(2);
  70.         }
  71.  
  72.         /* Set the surface pixels and refresh! */
  73.         if ( SDL_LockSurface(screen) < 0 ) {
  74.                 fprintf(stderr, "Couldn't lock the display surface: %s\n",
  75.                                                         SDL_GetError());
  76.                 exit(2);
  77.         }
  78.         buffer=(Uint8 *)screen->pixels;
  79.         for ( i=0; i<screen->h; ++i ) {
  80.                 memset(buffer,(i*255)/screen->h, screen->pitch);
  81.                 buffer += screen->pitch;
  82.         }
  83.         SDL_UnlockSurface(screen);
  84.         SDL_UpdateRect(screen, 0, 0, 0, 0);
  85.  
  86.         /* Load the bitmap */
  87.         bitmap = LoadXBM(screen, picture_width, picture_height,
  88.                                         (Uint8 *)picture_bits);
  89.         if ( bitmap == NULL ) {
  90.                 exit(1);
  91.         }
  92.  
  93.         /* Wait for a keystroke */
  94.         done = 0;
  95.         while ( !done ) {
  96.                 /* Check for events */
  97.                 while ( SDL_PollEvent(&event) ) {
  98.                         switch (event.type) {
  99.                                 case SDL_MOUSEBUTTONDOWN: {
  100.                                         SDL_Rect dst;
  101.  
  102.                                         dst.x = event.button.x - bitmap->w/2;
  103.                                         dst.y = event.button.y - bitmap->h/2;
  104.                                         dst.w = bitmap->w;
  105.                                         dst.h = bitmap->h;
  106.                                         SDL_BlitSurface(bitmap, NULL,
  107.                                                                 screen, &dst);
  108.                                         SDL_UpdateRects(screen,1,&dst);
  109.                                         }
  110.                                         break;
  111.                                 case SDL_KEYDOWN:
  112.                                         /* Any key press quits the app... */
  113.                                         done = 1;
  114.                                         break;
  115.                                 case SDL_QUIT:
  116.                                         done = 1;
  117.                                         break;
  118.                                 default:
  119.                                         break;
  120.                         }
  121.                 }
  122.         }
  123.         SDL_FreeSurface(bitmap);
  124.         return(0);
  125. }
  126.