test/threadwin.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
child 1712 931d111e737a
     1.1 --- a/test/threadwin.c	Mon May 29 03:53:21 2006 +0000
     1.2 +++ b/test/threadwin.c	Mon May 29 04:04:35 2006 +0000
     1.3 @@ -16,14 +16,14 @@
     1.4  
     1.5  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.6  static void
     1.7 -quit (int rc)
     1.8 +quit(int rc)
     1.9  {
    1.10 -    SDL_Quit ();
    1.11 -    exit (rc);
    1.12 +    SDL_Quit();
    1.13 +    exit(rc);
    1.14  }
    1.15  
    1.16  SDL_Surface *
    1.17 -LoadIconSurface (char *file, Uint8 ** maskp)
    1.18 +LoadIconSurface(char *file, Uint8 ** maskp)
    1.19  {
    1.20      SDL_Surface *icon;
    1.21      Uint8 *pixels;
    1.22 @@ -33,41 +33,41 @@
    1.23      *maskp = NULL;
    1.24  
    1.25      /* Load the icon surface */
    1.26 -    icon = SDL_LoadBMP (file);
    1.27 +    icon = SDL_LoadBMP(file);
    1.28      if (icon == NULL) {
    1.29 -        fprintf (stderr, "Couldn't load %s: %s\n", file, SDL_GetError ());
    1.30 +        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    1.31          return (NULL);
    1.32      }
    1.33  
    1.34      /* Check width and height */
    1.35      if ((icon->w % 8) != 0) {
    1.36 -        fprintf (stderr, "Icon width must be a multiple of 8!\n");
    1.37 -        SDL_FreeSurface (icon);
    1.38 +        fprintf(stderr, "Icon width must be a multiple of 8!\n");
    1.39 +        SDL_FreeSurface(icon);
    1.40          return (NULL);
    1.41      }
    1.42      if (icon->format->palette == NULL) {
    1.43 -        fprintf (stderr, "Icon must have a palette!\n");
    1.44 -        SDL_FreeSurface (icon);
    1.45 +        fprintf(stderr, "Icon must have a palette!\n");
    1.46 +        SDL_FreeSurface(icon);
    1.47          return (NULL);
    1.48      }
    1.49  
    1.50      /* Set the colorkey */
    1.51 -    SDL_SetColorKey (icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
    1.52 +    SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *) icon->pixels));
    1.53  
    1.54      /* Create the mask */
    1.55      pixels = (Uint8 *) icon->pixels;
    1.56 -    printf ("Transparent pixel: (%d,%d,%d)\n",
    1.57 -            icon->format->palette->colors[*pixels].r,
    1.58 -            icon->format->palette->colors[*pixels].g,
    1.59 -            icon->format->palette->colors[*pixels].b);
    1.60 +    printf("Transparent pixel: (%d,%d,%d)\n",
    1.61 +           icon->format->palette->colors[*pixels].r,
    1.62 +           icon->format->palette->colors[*pixels].g,
    1.63 +           icon->format->palette->colors[*pixels].b);
    1.64      mlen = icon->w * icon->h;
    1.65 -    mask = (Uint8 *) malloc (mlen / 8);
    1.66 +    mask = (Uint8 *) malloc(mlen / 8);
    1.67      if (mask == NULL) {
    1.68 -        fprintf (stderr, "Out of memory!\n");
    1.69 -        SDL_FreeSurface (icon);
    1.70 +        fprintf(stderr, "Out of memory!\n");
    1.71 +        SDL_FreeSurface(icon);
    1.72          return (NULL);
    1.73      }
    1.74 -    memset (mask, 0, mlen / 8);
    1.75 +    memset(mask, 0, mlen / 8);
    1.76      for (i = 0; i < mlen;) {
    1.77          if (pixels[i] != *pixels)
    1.78              mask[i / 8] |= 0x01;
    1.79 @@ -80,7 +80,7 @@
    1.80  }
    1.81  
    1.82  int SDLCALL
    1.83 -FilterEvents (const SDL_Event * event)
    1.84 +FilterEvents(const SDL_Event * event)
    1.85  {
    1.86      static int reallyquit = 0;
    1.87  
    1.88 @@ -88,19 +88,19 @@
    1.89  
    1.90      case SDL_ACTIVEEVENT:
    1.91          /* See what happened */
    1.92 -        printf ("App %s ", event->active.gain ? "gained" : "lost");
    1.93 +        printf("App %s ", event->active.gain ? "gained" : "lost");
    1.94          if (event->active.state & SDL_APPACTIVE)
    1.95 -            printf ("active ");
    1.96 +            printf("active ");
    1.97          if (event->active.state & SDL_APPMOUSEFOCUS)
    1.98 -            printf ("mouse ");
    1.99 +            printf("mouse ");
   1.100          if (event->active.state & SDL_APPINPUTFOCUS)
   1.101 -            printf ("input ");
   1.102 -        printf ("focus\n");
   1.103 +            printf("input ");
   1.104 +        printf("focus\n");
   1.105  
   1.106          /* See if we are iconified or restored */
   1.107          if (event->active.state & SDL_APPACTIVE) {
   1.108 -            printf ("App has been %s\n",
   1.109 -                    event->active.gain ? "restored" : "iconified");
   1.110 +            printf("App has been %s\n",
   1.111 +                   event->active.gain ? "restored" : "iconified");
   1.112          }
   1.113          return (0);
   1.114  
   1.115 @@ -108,10 +108,10 @@
   1.116      case SDL_QUIT:
   1.117          if (!reallyquit) {
   1.118              reallyquit = 1;
   1.119 -            printf ("Quit requested\n");
   1.120 +            printf("Quit requested\n");
   1.121              return (0);
   1.122          }
   1.123 -        printf ("Quit demanded\n");
   1.124 +        printf("Quit demanded\n");
   1.125          return (1);
   1.126  
   1.127          /* Mouse and keyboard events go to threads */
   1.128 @@ -129,7 +129,7 @@
   1.129  }
   1.130  
   1.131  int SDLCALL
   1.132 -HandleMouse (void *unused)
   1.133 +HandleMouse(void *unused)
   1.134  {
   1.135      SDL_Event events[10];
   1.136      int i, found;
   1.137 @@ -140,7 +140,7 @@
   1.138          (SDL_MOUSEMOTIONMASK | SDL_MOUSEBUTTONDOWNMASK |
   1.139           SDL_MOUSEBUTTONUPMASK);
   1.140      while (!done) {
   1.141 -        found = SDL_PeepEvents (events, 10, SDL_GETEVENT, mask);
   1.142 +        found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
   1.143          for (i = 0; i < found; ++i) {
   1.144              switch (events[i].type) {
   1.145                  /* We want to toggle visibility on buttonpress */
   1.146 @@ -148,28 +148,28 @@
   1.147              case SDL_MOUSEBUTTONUP:
   1.148                  if (events[i].button.state == SDL_PRESSED) {
   1.149                      visible = !visible;
   1.150 -                    SDL_ShowCursor (visible);
   1.151 +                    SDL_ShowCursor(visible);
   1.152                  }
   1.153 -                printf ("Mouse button %d has been %s\n",
   1.154 -                        events[i].button.button,
   1.155 -                        (events[i].button.state == SDL_PRESSED) ?
   1.156 -                        "pressed" : "released");
   1.157 +                printf("Mouse button %d has been %s\n",
   1.158 +                       events[i].button.button,
   1.159 +                       (events[i].button.state == SDL_PRESSED) ?
   1.160 +                       "pressed" : "released");
   1.161                  break;
   1.162                  /* Show relative mouse motion */
   1.163              case SDL_MOUSEMOTION:
   1.164 -                printf ("Mouse relative motion: {%d,%d}\n",
   1.165 -                        events[i].motion.xrel, events[i].motion.yrel);
   1.166 +                printf("Mouse relative motion: {%d,%d}\n",
   1.167 +                       events[i].motion.xrel, events[i].motion.yrel);
   1.168                  break;
   1.169              }
   1.170          }
   1.171          /* Give up some CPU to allow events to arrive */
   1.172 -        SDL_Delay (20);
   1.173 +        SDL_Delay(20);
   1.174      }
   1.175      return (0);
   1.176  }
   1.177  
   1.178  int SDLCALL
   1.179 -HandleKeyboard (void *unused)
   1.180 +HandleKeyboard(void *unused)
   1.181  {
   1.182      SDL_Event events[10];
   1.183      int i, found;
   1.184 @@ -178,16 +178,16 @@
   1.185      /* Handle mouse events here */
   1.186      mask = (SDL_KEYDOWNMASK | SDL_KEYUPMASK);
   1.187      while (!done) {
   1.188 -        found = SDL_PeepEvents (events, 10, SDL_GETEVENT, mask);
   1.189 +        found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
   1.190          for (i = 0; i < found; ++i) {
   1.191              switch (events[i].type) {
   1.192                  /* We want to toggle visibility on buttonpress */
   1.193              case SDL_KEYDOWN:
   1.194              case SDL_KEYUP:
   1.195 -                printf ("Key '%c' has been %s\n",
   1.196 -                        events[i].key.keysym.unicode,
   1.197 -                        (events[i].key.state == SDL_PRESSED) ?
   1.198 -                        "pressed" : "released");
   1.199 +                printf("Key '%c' has been %s\n",
   1.200 +                       events[i].key.keysym.unicode,
   1.201 +                       (events[i].key.state == SDL_PRESSED) ?
   1.202 +                       "pressed" : "released");
   1.203  
   1.204                  /* Allow hitting <ESC> to quit the app */
   1.205                  if (events[i].key.keysym.sym == SDLK_ESCAPE) {
   1.206 @@ -200,33 +200,33 @@
   1.207  
   1.208                  if (events[i].key.keysym.sym == SDLK_f) {
   1.209                      int rc = 0;
   1.210 -                    printf ("attempting to toggle fullscreen...\n");
   1.211 -                    rc = SDL_WM_ToggleFullScreen (SDL_GetVideoSurface ());
   1.212 -                    printf ("SDL_WM_ToggleFullScreen returned %d.\n", rc);
   1.213 +                    printf("attempting to toggle fullscreen...\n");
   1.214 +                    rc = SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
   1.215 +                    printf("SDL_WM_ToggleFullScreen returned %d.\n", rc);
   1.216                  }
   1.217  
   1.218                  if (events[i].key.keysym.sym == SDLK_g) {
   1.219                      SDL_GrabMode m;
   1.220 -                    m = SDL_WM_GrabInput (SDL_GRAB_QUERY) ==
   1.221 +                    m = SDL_WM_GrabInput(SDL_GRAB_QUERY) ==
   1.222                          SDL_GRAB_ON ? SDL_GRAB_OFF : SDL_GRAB_ON;
   1.223                      printf
   1.224                          ("attempting to toggle input grab to %s...\n",
   1.225                           m == SDL_GRAB_ON ? "ON" : "OFF");
   1.226 -                    SDL_WM_GrabInput (m);
   1.227 -                    printf ("attempt finished.\n");
   1.228 +                    SDL_WM_GrabInput(m);
   1.229 +                    printf("attempt finished.\n");
   1.230                  }
   1.231  
   1.232                  break;
   1.233              }
   1.234          }
   1.235          /* Give up some CPU to allow events to arrive */
   1.236 -        SDL_Delay (20);
   1.237 +        SDL_Delay(20);
   1.238      }
   1.239      return (0);
   1.240  }
   1.241  
   1.242  int
   1.243 -main (int argc, char *argv[])
   1.244 +main(int argc, char *argv[])
   1.245  {
   1.246      SDL_Surface *screen;
   1.247      SDL_Surface *icon;
   1.248 @@ -250,17 +250,17 @@
   1.249             been compiled with threaded events enabled, then the mouse and
   1.250             keyboard won't respond.
   1.251           */
   1.252 -        if ((argc >= 2) && (strcmp (argv[1], "-threaded") == 0)) {
   1.253 +        if ((argc >= 2) && (strcmp(argv[1], "-threaded") == 0)) {
   1.254              init_flags |= SDL_INIT_EVENTTHREAD;
   1.255              argc -= 1;
   1.256              argv += 1;
   1.257 -            printf ("Running with threaded events\n");
   1.258 -        } else if ((argc >= 2) && (strcmp (argv[1], "-fullscreen") == 0)) {
   1.259 +            printf("Running with threaded events\n");
   1.260 +        } else if ((argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0)) {
   1.261              video_flags |= SDL_FULLSCREEN;
   1.262              argc -= 1;
   1.263              argv += 1;
   1.264 -        } else if ((argc >= 3) && (strcmp (argv[1], "-bpp") == 0)) {
   1.265 -            video_bpp = atoi (argv[2]);
   1.266 +        } else if ((argc >= 3) && (strcmp(argv[1], "-bpp") == 0)) {
   1.267 +            video_bpp = atoi(argv[2]);
   1.268              argc -= 2;
   1.269              argv += 2;
   1.270          } else {
   1.271 @@ -269,38 +269,38 @@
   1.272      }
   1.273  
   1.274      /* Initialize SDL with the requested flags */
   1.275 -    if (SDL_Init (init_flags) < 0) {
   1.276 -        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   1.277 +    if (SDL_Init(init_flags) < 0) {
   1.278 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.279          return (1);
   1.280      }
   1.281  
   1.282      /* Set the icon -- this must be done before the first mode set */
   1.283 -    icon = LoadIconSurface ("icon.bmp", &icon_mask);
   1.284 +    icon = LoadIconSurface("icon.bmp", &icon_mask);
   1.285      if (icon != NULL) {
   1.286 -        SDL_WM_SetIcon (icon, icon_mask);
   1.287 +        SDL_WM_SetIcon(icon, icon_mask);
   1.288      }
   1.289      if (icon_mask != NULL)
   1.290 -        free (icon_mask);
   1.291 +        free(icon_mask);
   1.292  
   1.293      /* Initialize the display */
   1.294 -    screen = SDL_SetVideoMode (640, 480, video_bpp, video_flags);
   1.295 +    screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags);
   1.296      if (screen == NULL) {
   1.297 -        fprintf (stderr, "Couldn't set 640x480x%d video mode: %s\n",
   1.298 -                 video_bpp, SDL_GetError ());
   1.299 -        quit (1);
   1.300 +        fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
   1.301 +                video_bpp, SDL_GetError());
   1.302 +        quit(1);
   1.303      }
   1.304 -    printf ("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
   1.305 -            "fullscreen" : "windowed");
   1.306 +    printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
   1.307 +           "fullscreen" : "windowed");
   1.308  
   1.309      /* Enable printable characters */
   1.310 -    SDL_EnableUNICODE (1);
   1.311 +    SDL_EnableUNICODE(1);
   1.312  
   1.313      /* Set an event filter that discards everything but QUIT */
   1.314 -    SDL_SetEventFilter (FilterEvents);
   1.315 +    SDL_SetEventFilter(FilterEvents);
   1.316  
   1.317      /* Create the event handling threads */
   1.318 -    mouse_thread = SDL_CreateThread (HandleMouse, NULL);
   1.319 -    keybd_thread = SDL_CreateThread (HandleKeyboard, NULL);
   1.320 +    mouse_thread = SDL_CreateThread(HandleMouse, NULL);
   1.321 +    keybd_thread = SDL_CreateThread(HandleKeyboard, NULL);
   1.322  
   1.323      /* Set the surface pixels and refresh! */
   1.324      for (i = 0; i < 256; ++i) {
   1.325 @@ -308,34 +308,34 @@
   1.326          palette[i].g = 255 - i;
   1.327          palette[i].b = 255 - i;
   1.328      }
   1.329 -    SDL_SetColors (screen, palette, 0, 256);
   1.330 -    if (SDL_LockSurface (screen) < 0) {
   1.331 -        fprintf (stderr, "Couldn't lock display surface: %s\n",
   1.332 -                 SDL_GetError ());
   1.333 -        quit (2);
   1.334 +    SDL_SetColors(screen, palette, 0, 256);
   1.335 +    if (SDL_LockSurface(screen) < 0) {
   1.336 +        fprintf(stderr, "Couldn't lock display surface: %s\n",
   1.337 +                SDL_GetError());
   1.338 +        quit(2);
   1.339      }
   1.340      buffer = (Uint8 *) screen->pixels;
   1.341      for (i = 0; i < screen->h; ++i) {
   1.342 -        memset (buffer, (i * 255) / screen->h,
   1.343 -                screen->w * screen->format->BytesPerPixel);
   1.344 +        memset(buffer, (i * 255) / screen->h,
   1.345 +               screen->w * screen->format->BytesPerPixel);
   1.346          buffer += screen->pitch;
   1.347      }
   1.348 -    SDL_UnlockSurface (screen);
   1.349 -    SDL_UpdateRect (screen, 0, 0, 0, 0);
   1.350 +    SDL_UnlockSurface(screen);
   1.351 +    SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.352  
   1.353      /* Loop, waiting for QUIT */
   1.354      while (!done) {
   1.355          if (!(init_flags & SDL_INIT_EVENTTHREAD)) {
   1.356 -            SDL_PumpEvents ();  /* Needed when event thread is off */
   1.357 +            SDL_PumpEvents();   /* Needed when event thread is off */
   1.358          }
   1.359 -        if (SDL_PeepEvents (NULL, 0, SDL_PEEKEVENT, SDL_QUITMASK)) {
   1.360 +        if (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_QUITMASK)) {
   1.361              done = 1;
   1.362          }
   1.363          /* Give up some CPU so the events can accumulate */
   1.364 -        SDL_Delay (20);
   1.365 +        SDL_Delay(20);
   1.366      }
   1.367 -    SDL_WaitThread (mouse_thread, NULL);
   1.368 -    SDL_WaitThread (keybd_thread, NULL);
   1.369 -    SDL_Quit ();
   1.370 +    SDL_WaitThread(mouse_thread, NULL);
   1.371 +    SDL_WaitThread(keybd_thread, NULL);
   1.372 +    SDL_Quit();
   1.373      return (0);
   1.374  }