test/threadwin.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
child 5969 3a041d215edc
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

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