Subversion Repositories Kolibri OS

Rev

Rev 8200 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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