test/threadwin.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2117 40888832d785
child 4429 faa9fc8e7f67
permissions -rw-r--r--
Added testresample.c
     1 
     2 /* Test out the multi-threaded event handling functions */
     3 
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6 #include <string.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_thread.h"
    10 
    11 /* Are we done yet? */
    12 static int done = 0;
    13 
    14 /* Is the cursor visible? */
    15 static int visible = 1;
    16 
    17 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    18 static void
    19 quit(int rc)
    20 {
    21     SDL_Quit();
    22     exit(rc);
    23 }
    24 
    25 SDL_Surface *
    26 LoadIconSurface(char *file, Uint8 ** maskp)
    27 {
    28     SDL_Surface *icon;
    29     Uint8 *pixels;
    30     Uint8 *mask;
    31     int mlen, i;
    32 
    33     *maskp = NULL;
    34 
    35     /* Load the icon surface */
    36     icon = SDL_LoadBMP(file);
    37     if (icon == NULL) {
    38         fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    39         return (NULL);
    40     }
    41 
    42     /* Check width and height */
    43     if ((icon->w % 8) != 0) {
    44         fprintf(stderr, "Icon width must be a multiple of 8!\n");
    45         SDL_FreeSurface(icon);
    46         return (NULL);
    47     }
    48     if (icon->format->palette == NULL) {
    49         fprintf(stderr, "Icon must have a palette!\n");
    50         SDL_FreeSurface(icon);
    51         return (NULL);
    52     }
    53 
    54     /* Set the colorkey */
    55     SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
    56 
    57     /* Create the mask */
    58     pixels = (Uint8 *) icon->pixels;
    59     printf("Transparent pixel: (%d,%d,%d)\n",
    60            icon->format->palette->colors[*pixels].r,
    61            icon->format->palette->colors[*pixels].g,
    62            icon->format->palette->colors[*pixels].b);
    63     mlen = icon->w * icon->h;
    64     mask = (Uint8 *) malloc(mlen / 8);
    65     if (mask == NULL) {
    66         fprintf(stderr, "Out of memory!\n");
    67         SDL_FreeSurface(icon);
    68         return (NULL);
    69     }
    70     memset(mask, 0, mlen / 8);
    71     for (i = 0; i < mlen;) {
    72         if (pixels[i] != *pixels)
    73             mask[i / 8] |= 0x01;
    74         ++i;
    75         if ((i % 8) != 0)
    76             mask[i / 8] <<= 1;
    77     }
    78     *maskp = mask;
    79     return (icon);
    80 }
    81 
    82 int SDLCALL
    83 FilterEvents(void *userdata, SDL_Event * event)
    84 {
    85     static int reallyquit = 0;
    86 
    87     switch (event->type) {
    88 
    89     case SDL_ACTIVEEVENT:
    90         /* See what happened */
    91         printf("App %s ", event->active.gain ? "gained" : "lost");
    92         if (event->active.state & SDL_APPACTIVE)
    93             printf("active ");
    94         if (event->active.state & SDL_APPMOUSEFOCUS)
    95             printf("mouse ");
    96         if (event->active.state & SDL_APPINPUTFOCUS)
    97             printf("input ");
    98         printf("focus\n");
    99 
   100         /* See if we are iconified or restored */
   101         if (event->active.state & SDL_APPACTIVE) {
   102             printf("App has been %s\n",
   103                    event->active.gain ? "restored" : "iconified");
   104         }
   105         return (0);
   106 
   107         /* This is important!  Queue it if we want to quit. */
   108     case SDL_QUIT:
   109         if (!reallyquit) {
   110             reallyquit = 1;
   111             printf("Quit requested\n");
   112             return (0);
   113         }
   114         printf("Quit demanded\n");
   115         return (1);
   116 
   117         /* Mouse and keyboard events go to threads */
   118     case SDL_MOUSEMOTION:
   119     case SDL_MOUSEBUTTONDOWN:
   120     case SDL_MOUSEBUTTONUP:
   121     case SDL_KEYDOWN:
   122     case SDL_KEYUP:
   123         return (1);
   124 
   125         /* Drop all other events */
   126     default:
   127         return (0);
   128     }
   129 }
   130 
   131 int SDLCALL
   132 HandleMouse(void *unused)
   133 {
   134     SDL_Event events[10];
   135     int i, found;
   136     Uint32 mask;
   137 
   138     /* Handle mouse events here */
   139     mask =
   140         (SDL_MOUSEMOTIONMASK | SDL_MOUSEBUTTONDOWNMASK |
   141          SDL_MOUSEBUTTONUPMASK);
   142     while (!done) {
   143         found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
   144         for (i = 0; i < found; ++i) {
   145             switch (events[i].type) {
   146                 /* We want to toggle visibility on buttonpress */
   147             case SDL_MOUSEBUTTONDOWN:
   148             case SDL_MOUSEBUTTONUP:
   149                 if (events[i].button.state == SDL_PRESSED) {
   150                     visible = !visible;
   151                     SDL_ShowCursor(visible);
   152                 }
   153                 printf("Mouse button %d has been %s\n",
   154                        events[i].button.button,
   155                        (events[i].button.state == SDL_PRESSED) ?
   156                        "pressed" : "released");
   157                 break;
   158                 /* Show relative mouse motion */
   159             case SDL_MOUSEMOTION:
   160                 printf("Mouse relative motion: {%d,%d}\n",
   161                        events[i].motion.xrel, events[i].motion.yrel);
   162                 break;
   163             }
   164         }
   165         /* Give up some CPU to allow events to arrive */
   166         SDL_Delay(20);
   167     }
   168     return (0);
   169 }
   170 
   171 int SDLCALL
   172 HandleKeyboard(void *unused)
   173 {
   174     SDL_Event events[10];
   175     int i, found;
   176     Uint32 mask;
   177 
   178     /* Handle mouse events here */
   179     mask = (SDL_KEYDOWNMASK | SDL_KEYUPMASK);
   180     while (!done) {
   181         found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
   182         for (i = 0; i < found; ++i) {
   183             switch (events[i].type) {
   184                 /* We want to toggle visibility on buttonpress */
   185             case SDL_KEYDOWN:
   186             case SDL_KEYUP:
   187                 printf("Key '%c' (keysym==%d) has been %s\n",
   188                        events[i].key.keysym.unicode,
   189                        (int) events[i].key.keysym.sym,
   190                        (events[i].key.state == SDL_PRESSED) ?
   191                        "pressed" : "released");
   192 
   193                 /* Allow hitting <ESC> to quit the app */
   194                 if (events[i].key.keysym.sym == SDLK_ESCAPE) {
   195                     done = 1;
   196                 }
   197 
   198                 /* skip events now that aren't KEYUPs... */
   199                 if (events[i].key.state == SDL_PRESSED)
   200                     break;
   201 
   202                 if (events[i].key.keysym.sym == SDLK_f) {
   203                     int rc = 0;
   204                     printf("attempting to toggle fullscreen...\n");
   205                     rc = SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
   206                     printf("SDL_WM_ToggleFullScreen returned %d.\n", rc);
   207                 }
   208 
   209                 if (events[i].key.keysym.sym == SDLK_g) {
   210                     SDL_GrabMode m;
   211                     m = SDL_WM_GrabInput(SDL_GRAB_QUERY) ==
   212                         SDL_GRAB_ON ? SDL_GRAB_OFF : SDL_GRAB_ON;
   213                     printf
   214                         ("attempting to toggle input grab to %s...\n",
   215                          m == SDL_GRAB_ON ? "ON" : "OFF");
   216                     SDL_WM_GrabInput(m);
   217                     printf("attempt finished.\n");
   218                 }
   219 
   220                 break;
   221             }
   222         }
   223         /* Give up some CPU to allow events to arrive */
   224         SDL_Delay(20);
   225     }
   226     return (0);
   227 }
   228 
   229 int
   230 main(int argc, char *argv[])
   231 {
   232     SDL_Surface *screen;
   233     SDL_Surface *icon;
   234     Uint8 *icon_mask;
   235     int i, parsed;
   236     Uint8 *buffer;
   237     SDL_Color palette[256];
   238     Uint32 init_flags;
   239     Uint8 video_bpp;
   240     Uint32 video_flags;
   241     SDL_Thread *mouse_thread;
   242     SDL_Thread *keybd_thread;
   243 
   244     /* Set the options, based on command line arguments */
   245     init_flags = SDL_INIT_VIDEO;
   246     video_bpp = 8;
   247     video_flags = SDL_SWSURFACE;
   248     parsed = 1;
   249     while (parsed) {
   250         /* If the threaded option is enabled, and the SDL library hasn't
   251            been compiled with threaded events enabled, then the mouse and
   252            keyboard won't respond.
   253          */
   254         if ((argc >= 2) && (strcmp(argv[1], "-threaded") == 0)) {
   255             init_flags |= SDL_INIT_EVENTTHREAD;
   256             argc -= 1;
   257             argv += 1;
   258             printf("Running with threaded events\n");
   259         } else if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
   260             video_flags |= SDL_FULLSCREEN;
   261             argc -= 1;
   262             argv += 1;
   263         } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
   264             video_bpp = atoi(argv[2]);
   265             argc -= 2;
   266             argv += 2;
   267         } else {
   268             parsed = 0;
   269         }
   270     }
   271 
   272     /* Initialize SDL with the requested flags */
   273     if (SDL_Init(init_flags) < 0) {
   274         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   275         return (1);
   276     }
   277 
   278     /* Set the icon -- this must be done before the first mode set */
   279     icon = LoadIconSurface("icon.bmp", &icon_mask);
   280     if (icon != NULL) {
   281         SDL_WM_SetIcon(icon, icon_mask);
   282     }
   283     if (icon_mask != NULL)
   284         free(icon_mask);
   285 
   286     /* Initialize the display */
   287     screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags);
   288     if (screen == NULL) {
   289         fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   290                 video_bpp, SDL_GetError());
   291         quit(1);
   292     }
   293     printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
   294            "fullscreen" : "windowed");
   295 
   296     /* Enable printable characters */
   297     SDL_EnableUNICODE(1);
   298 
   299     /* Set an event filter that discards everything but QUIT */
   300     SDL_SetEventFilter(FilterEvents, NULL);
   301 
   302     /* Create the event handling threads */
   303     mouse_thread = SDL_CreateThread(HandleMouse, NULL);
   304     keybd_thread = SDL_CreateThread(HandleKeyboard, NULL);
   305 
   306     /* Set the surface pixels and refresh! */
   307     for (i = 0; i < 256; ++i) {
   308         palette[i].r = 255 - i;
   309         palette[i].g = 255 - i;
   310         palette[i].b = 255 - i;
   311     }
   312     SDL_SetColors(screen, palette, 0, 256);
   313     if (SDL_LockSurface(screen) < 0) {
   314         fprintf(stderr, "Couldn't lock display surface: %s\n",
   315                 SDL_GetError());
   316         quit(2);
   317     }
   318     buffer = (Uint8 *) screen->pixels;
   319     for (i = 0; i < screen->h; ++i) {
   320         memset(buffer, (i * 255) / screen->h,
   321                screen->w * screen->format->BytesPerPixel);
   322         buffer += screen->pitch;
   323     }
   324     SDL_UnlockSurface(screen);
   325     SDL_UpdateRect(screen, 0, 0, 0, 0);
   326 
   327     /* Loop, waiting for QUIT */
   328     while (!done) {
   329         if (!(init_flags & SDL_INIT_EVENTTHREAD)) {
   330             SDL_PumpEvents();   /* Needed when event thread is off */
   331         }
   332         if (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_QUITMASK)) {
   333             done = 1;
   334         }
   335         /* Give up some CPU so the events can accumulate */
   336         SDL_Delay(20);
   337     }
   338     SDL_WaitThread(mouse_thread, NULL);
   339     SDL_WaitThread(keybd_thread, NULL);
   340     SDL_Quit();
   341     return (0);
   342 }