test/testblitspeed.c
changeset 1895 c121d94672cb
parent 1231 cf59e7b91ed4
child 2267 c785543d1843
     1.1 --- a/test/testblitspeed.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testblitspeed.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -15,27 +15,30 @@
     1.4  static int testSeconds = 10;
     1.5  
     1.6  
     1.7 -static int percent(int val, int total)
     1.8 +static int
     1.9 +percent(int val, int total)
    1.10  {
    1.11 -    return((int) ((((float) val) / ((float) total)) * 100.0f));
    1.12 +    return ((int) ((((float) val) / ((float) total)) * 100.0f));
    1.13  }
    1.14  
    1.15 -static int randRange(int lo, int hi)
    1.16 +static int
    1.17 +randRange(int lo, int hi)
    1.18  {
    1.19 -    return(lo + (int) (((double) hi)*rand()/(RAND_MAX+1.0)));
    1.20 +    return (lo + (int) (((double) hi) * rand() / (RAND_MAX + 1.0)));
    1.21  }
    1.22  
    1.23 -static void copy_trunc_str(char *str, size_t strsize, const char *flagstr)
    1.24 +static void
    1.25 +copy_trunc_str(char *str, size_t strsize, const char *flagstr)
    1.26  {
    1.27 -    if ( (strlen(str) + strlen(flagstr)) >= (strsize - 1) )
    1.28 +    if ((strlen(str) + strlen(flagstr)) >= (strsize - 1))
    1.29          strcpy(str + (strsize - 5), " ...");
    1.30      else
    1.31          strcat(str, flagstr);
    1.32  }
    1.33  
    1.34 -static void __append_sdl_surface_flag(SDL_Surface *_surface, char *str,
    1.35 -                                      size_t strsize, Uint32 flag,
    1.36 -                                      const char *flagstr)
    1.37 +static void
    1.38 +__append_sdl_surface_flag(SDL_Surface * _surface, char *str,
    1.39 +                          size_t strsize, Uint32 flag, const char *flagstr)
    1.40  {
    1.41      if (_surface->flags & flag)
    1.42          copy_trunc_str(str, strsize, flagstr);
    1.43 @@ -45,22 +48,28 @@
    1.44  #define append_sdl_surface_flag(a, b, c, fl) __append_sdl_surface_flag(a, b, c, fl, " " #fl)
    1.45  #define print_tf_state(str, val) printf("%s: {%s}\n", str, (val) ? "true" : "false" )
    1.46  
    1.47 -static void output_videoinfo_details(void)
    1.48 +static void
    1.49 +output_videoinfo_details(void)
    1.50  {
    1.51      const SDL_VideoInfo *info = SDL_GetVideoInfo();
    1.52      printf("SDL_GetVideoInfo():\n");
    1.53      if (info == NULL)
    1.54          printf("  (null.)\n");
    1.55 -    else
    1.56 -    {
    1.57 +    else {
    1.58          print_tf_state("  hardware surface available", info->hw_available);
    1.59          print_tf_state("  window manager available", info->wm_available);
    1.60 -        print_tf_state("  accelerated hardware->hardware blits", info->blit_hw);
    1.61 -        print_tf_state("  accelerated hardware->hardware colorkey blits", info->blit_hw_CC);
    1.62 -        print_tf_state("  accelerated hardware->hardware alpha blits", info->blit_hw_A);
    1.63 -        print_tf_state("  accelerated software->hardware blits", info->blit_sw);
    1.64 -        print_tf_state("  accelerated software->hardware colorkey blits", info->blit_sw_CC);
    1.65 -        print_tf_state("  accelerated software->hardware alpha blits", info->blit_sw_A);
    1.66 +        print_tf_state("  accelerated hardware->hardware blits",
    1.67 +                       info->blit_hw);
    1.68 +        print_tf_state("  accelerated hardware->hardware colorkey blits",
    1.69 +                       info->blit_hw_CC);
    1.70 +        print_tf_state("  accelerated hardware->hardware alpha blits",
    1.71 +                       info->blit_hw_A);
    1.72 +        print_tf_state("  accelerated software->hardware blits",
    1.73 +                       info->blit_sw);
    1.74 +        print_tf_state("  accelerated software->hardware colorkey blits",
    1.75 +                       info->blit_sw_CC);
    1.76 +        print_tf_state("  accelerated software->hardware alpha blits",
    1.77 +                       info->blit_sw_A);
    1.78          print_tf_state("  accelerated color fills", info->blit_fill);
    1.79          printf("  video memory: (%d)\n", info->video_mem);
    1.80      }
    1.81 @@ -68,63 +77,58 @@
    1.82      printf("\n");
    1.83  }
    1.84  
    1.85 -static void output_surface_details(const char *name, SDL_Surface *surface)
    1.86 +static void
    1.87 +output_surface_details(const char *name, SDL_Surface * surface)
    1.88  {
    1.89      printf("Details for %s:\n", name);
    1.90  
    1.91 -    if (surface == NULL)
    1.92 -    {
    1.93 +    if (surface == NULL) {
    1.94          printf("-WARNING- You've got a NULL surface!");
    1.95 -    }
    1.96 -    else
    1.97 -    {
    1.98 +    } else {
    1.99          char f[256];
   1.100          printf("  width      : %d\n", surface->w);
   1.101          printf("  height     : %d\n", surface->h);
   1.102 -        printf("  depth      : %d bits per pixel\n", surface->format->BitsPerPixel);
   1.103 +        printf("  depth      : %d bits per pixel\n",
   1.104 +               surface->format->BitsPerPixel);
   1.105          printf("  pitch      : %d\n", (int) surface->pitch);
   1.106          printf("  alpha      : %d\n", (int) surface->format->alpha);
   1.107 -        printf("  colorkey   : 0x%X\n", (unsigned int) surface->format->colorkey);
   1.108 +        printf("  colorkey   : 0x%X\n",
   1.109 +               (unsigned int) surface->format->colorkey);
   1.110  
   1.111          printf("  red bits   : 0x%08X mask, %d shift, %d loss\n",
   1.112 -                    (int) surface->format->Rmask,
   1.113 -                    (int) surface->format->Rshift,
   1.114 -                    (int) surface->format->Rloss);
   1.115 +               (int) surface->format->Rmask,
   1.116 +               (int) surface->format->Rshift, (int) surface->format->Rloss);
   1.117          printf("  green bits : 0x%08X mask, %d shift, %d loss\n",
   1.118 -                    (int) surface->format->Gmask,
   1.119 -                    (int) surface->format->Gshift,
   1.120 -                    (int) surface->format->Gloss);
   1.121 +               (int) surface->format->Gmask,
   1.122 +               (int) surface->format->Gshift, (int) surface->format->Gloss);
   1.123          printf("  blue bits  : 0x%08X mask, %d shift, %d loss\n",
   1.124 -                    (int) surface->format->Bmask,
   1.125 -                    (int) surface->format->Bshift,
   1.126 -                    (int) surface->format->Bloss);
   1.127 +               (int) surface->format->Bmask,
   1.128 +               (int) surface->format->Bshift, (int) surface->format->Bloss);
   1.129          printf("  alpha bits : 0x%08X mask, %d shift, %d loss\n",
   1.130 -                    (int) surface->format->Amask,
   1.131 -                    (int) surface->format->Ashift,
   1.132 -                    (int) surface->format->Aloss);
   1.133 +               (int) surface->format->Amask,
   1.134 +               (int) surface->format->Ashift, (int) surface->format->Aloss);
   1.135  
   1.136          f[0] = '\0';
   1.137  
   1.138 -        /*append_sdl_surface_flag(surface, f, sizeof (f), SDL_SWSURFACE);*/
   1.139 +        /*append_sdl_surface_flag(surface, f, sizeof (f), SDL_SWSURFACE); */
   1.140          if ((surface->flags & SDL_HWSURFACE) == 0)
   1.141 -            copy_trunc_str(f, sizeof (f), " SDL_SWSURFACE");
   1.142 +            copy_trunc_str(f, sizeof(f), " SDL_SWSURFACE");
   1.143  
   1.144 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_HWSURFACE);
   1.145 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_ASYNCBLIT);
   1.146 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_ANYFORMAT);
   1.147 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_HWPALETTE);
   1.148 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_DOUBLEBUF);
   1.149 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_FULLSCREEN);
   1.150 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_OPENGL);
   1.151 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_OPENGLBLIT);
   1.152 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_RESIZABLE);
   1.153 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_NOFRAME);
   1.154 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_HWACCEL);
   1.155 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_SRCCOLORKEY);
   1.156 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_RLEACCELOK);
   1.157 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_RLEACCEL);
   1.158 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_SRCALPHA);
   1.159 -        append_sdl_surface_flag(surface, f, sizeof (f), SDL_PREALLOC);
   1.160 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWSURFACE);
   1.161 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_ASYNCBLIT);
   1.162 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_ANYFORMAT);
   1.163 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWPALETTE);
   1.164 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_DOUBLEBUF);
   1.165 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_FULLSCREEN);
   1.166 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_OPENGL);
   1.167 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_RESIZABLE);
   1.168 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_NOFRAME);
   1.169 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWACCEL);
   1.170 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_SRCCOLORKEY);
   1.171 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_RLEACCELOK);
   1.172 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_RLEACCEL);
   1.173 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_SRCALPHA);
   1.174 +        append_sdl_surface_flag(surface, f, sizeof(f), SDL_PREALLOC);
   1.175  
   1.176          if (f[0] == '\0')
   1.177              strcpy(f, " (none)");
   1.178 @@ -135,14 +139,16 @@
   1.179      printf("\n");
   1.180  }
   1.181  
   1.182 -static void output_details(void)
   1.183 +static void
   1.184 +output_details(void)
   1.185  {
   1.186      output_videoinfo_details();
   1.187      output_surface_details("Source Surface", src);
   1.188      output_surface_details("Destination Surface", dest);
   1.189  }
   1.190  
   1.191 -static Uint32 blit(SDL_Surface *dst, SDL_Surface *src, int x, int y)
   1.192 +static Uint32
   1.193 +blit(SDL_Surface * dst, SDL_Surface * src, int x, int y)
   1.194  {
   1.195      Uint32 start = 0;
   1.196      SDL_Rect srcRect;
   1.197 @@ -152,39 +158,41 @@
   1.198      srcRect.y = 0;
   1.199      dstRect.x = x;
   1.200      dstRect.y = y;
   1.201 -    dstRect.w = srcRect.w = src->w;  /* SDL will clip as appropriate. */
   1.202 +    dstRect.w = srcRect.w = src->w;     /* SDL will clip as appropriate. */
   1.203      dstRect.h = srcRect.h = src->h;
   1.204  
   1.205      start = SDL_GetTicks();
   1.206      SDL_BlitSurface(src, &srcRect, dst, &dstRect);
   1.207 -    return(SDL_GetTicks() - start);
   1.208 +    return (SDL_GetTicks() - start);
   1.209  }
   1.210  
   1.211 -static void blitCentered(SDL_Surface *dst, SDL_Surface *src)
   1.212 +static void
   1.213 +blitCentered(SDL_Surface * dst, SDL_Surface * src)
   1.214  {
   1.215      int x = (dst->w - src->w) / 2;
   1.216      int y = (dst->h - src->h) / 2;
   1.217      blit(dst, src, x, y);
   1.218  }
   1.219  
   1.220 -static int atoi_hex(const char *str)
   1.221 +static int
   1.222 +atoi_hex(const char *str)
   1.223  {
   1.224      if (str == NULL)
   1.225          return 0;
   1.226  
   1.227 -    if (strlen(str) > 2)
   1.228 -    {
   1.229 +    if (strlen(str) > 2) {
   1.230          int retval = 0;
   1.231          if ((str[0] == '0') && (str[1] == 'x'))
   1.232              sscanf(str + 2, "%X", &retval);
   1.233 -        return(retval);
   1.234 +        return (retval);
   1.235      }
   1.236  
   1.237 -    return(atoi(str));
   1.238 +    return (atoi(str));
   1.239  }
   1.240  
   1.241  
   1.242 -static int setup_test(int argc, char **argv)
   1.243 +static int
   1.244 +setup_test(int argc, char **argv)
   1.245  {
   1.246      const char *dumpfile = NULL;
   1.247      SDL_Surface *bmp = NULL;
   1.248 @@ -213,8 +221,7 @@
   1.249      int screenSurface = 0;
   1.250      int i = 0;
   1.251  
   1.252 -    for (i = 1; i < argc; i++)
   1.253 -    {
   1.254 +    for (i = 1; i < argc; i++) {
   1.255          const char *arg = argv[i];
   1.256  
   1.257          if (strcmp(arg, "--dstbpp") == 0)
   1.258 @@ -256,63 +263,59 @@
   1.259          else if (strcmp(arg, "--dumpfile") == 0)
   1.260              dumpfile = argv[++i];
   1.261          /* !!! FIXME: set colorkey. */
   1.262 -        else if (0)  /* !!! FIXME: we handle some commandlines elsewhere now */
   1.263 -        {
   1.264 +        else if (0) {           /* !!! FIXME: we handle some commandlines elsewhere now */
   1.265              fprintf(stderr, "Unknown commandline option: %s\n", arg);
   1.266 -            return(0);
   1.267 +            return (0);
   1.268          }
   1.269      }
   1.270  
   1.271 -    if (SDL_Init(SDL_INIT_VIDEO) == -1)
   1.272 -    {
   1.273 +    if (SDL_Init(SDL_INIT_VIDEO) == -1) {
   1.274          fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
   1.275 -        return(0);
   1.276 +        return (0);
   1.277      }
   1.278  
   1.279      bmp = SDL_LoadBMP("sample.bmp");
   1.280 -    if (bmp == NULL)
   1.281 -    {
   1.282 +    if (bmp == NULL) {
   1.283          fprintf(stderr, "SDL_LoadBMP failed: %s\n", SDL_GetError());
   1.284          SDL_Quit();
   1.285 -        return(0);
   1.286 +        return (0);
   1.287      }
   1.288  
   1.289 -    if ((dstflags & SDL_HWSURFACE) == 0) dstflags |= SDL_SWSURFACE;
   1.290 -    if ((srcflags & SDL_HWSURFACE) == 0) srcflags |= SDL_SWSURFACE;
   1.291 +    if ((dstflags & SDL_HWSURFACE) == 0)
   1.292 +        dstflags |= SDL_SWSURFACE;
   1.293 +    if ((srcflags & SDL_HWSURFACE) == 0)
   1.294 +        srcflags |= SDL_SWSURFACE;
   1.295  
   1.296      if (screenSurface)
   1.297          dest = SDL_SetVideoMode(dstw, dsth, dstbpp, dstflags);
   1.298 -    else
   1.299 -    {
   1.300 +    else {
   1.301          dest = SDL_CreateRGBSurface(dstflags, dstw, dsth, dstbpp,
   1.302                                      dstrmask, dstgmask, dstbmask, dstamask);
   1.303      }
   1.304  
   1.305 -    if (dest == NULL)
   1.306 -    {
   1.307 +    if (dest == NULL) {
   1.308          fprintf(stderr, "dest surface creation failed: %s\n", SDL_GetError());
   1.309          SDL_Quit();
   1.310 -        return(0);
   1.311 +        return (0);
   1.312      }
   1.313  
   1.314      src = SDL_CreateRGBSurface(srcflags, srcw, srch, srcbpp,
   1.315                                 srcrmask, srcgmask, srcbmask, srcamask);
   1.316 -    if (src == NULL)
   1.317 -    {
   1.318 +    if (src == NULL) {
   1.319          fprintf(stderr, "src surface creation failed: %s\n", SDL_GetError());
   1.320          SDL_Quit();
   1.321 -        return(0);
   1.322 +        return (0);
   1.323      }
   1.324  
   1.325      /* handle alpha settings... */
   1.326 -    srcalphaflags = (src->flags&SDL_SRCALPHA) | (src->flags&SDL_RLEACCEL);
   1.327 -    dstalphaflags = (dest->flags&SDL_SRCALPHA) | (dest->flags&SDL_RLEACCEL);
   1.328 +    srcalphaflags = (src->flags & SDL_SRCALPHA) | (src->flags & SDL_RLEACCEL);
   1.329 +    dstalphaflags =
   1.330 +        (dest->flags & SDL_SRCALPHA) | (dest->flags & SDL_RLEACCEL);
   1.331      origsrcalphaflags = srcalphaflags;
   1.332      origdstalphaflags = dstalphaflags;
   1.333      srcalpha = src->format->alpha;
   1.334      dstalpha = dest->format->alpha;
   1.335 -    for (i = 1; i < argc; i++)
   1.336 -    {
   1.337 +    for (i = 1; i < argc; i++) {
   1.338          const char *arg = argv[i];
   1.339  
   1.340          if (strcmp(arg, "--srcalpha") == 0)
   1.341 @@ -336,9 +339,11 @@
   1.342          else if (strcmp(arg, "--dstnorleaccel") == 0)
   1.343              dstalphaflags &= ~SDL_RLEACCEL;
   1.344      }
   1.345 -    if ((dstalphaflags != origdstalphaflags) || (dstalpha != dest->format->alpha))
   1.346 +    if ((dstalphaflags != origdstalphaflags)
   1.347 +        || (dstalpha != dest->format->alpha))
   1.348          SDL_SetAlpha(dest, dstalphaflags, (Uint8) dstalpha);
   1.349 -    if ((srcalphaflags != origsrcalphaflags) || (srcalpha != src->format->alpha))
   1.350 +    if ((srcalphaflags != origsrcalphaflags)
   1.351 +        || (srcalpha != src->format->alpha))
   1.352          SDL_SetAlpha(src, srcalphaflags, (Uint8) srcalpha);
   1.353  
   1.354      /* set some sane defaults so we can see if the blit code is broken... */
   1.355 @@ -349,15 +354,16 @@
   1.356      SDL_FreeSurface(bmp);
   1.357  
   1.358      if (dumpfile)
   1.359 -        SDL_SaveBMP(src, dumpfile);  /* make sure initial convert is sane. */
   1.360 +        SDL_SaveBMP(src, dumpfile);     /* make sure initial convert is sane. */
   1.361  
   1.362      output_details();
   1.363  
   1.364 -    return(1);
   1.365 +    return (1);
   1.366  }
   1.367  
   1.368  
   1.369 -static void test_blit_speed(void)
   1.370 +static void
   1.371 +test_blit_speed(void)
   1.372  {
   1.373      Uint32 clearColor = SDL_MapRGB(dest->format, 0, 0, 0);
   1.374      Uint32 iterations = 0;
   1.375 @@ -376,45 +382,43 @@
   1.376      now = SDL_GetTicks();
   1.377      end = now + testms;
   1.378  
   1.379 -    do
   1.380 -    {
   1.381 +    do {
   1.382          /* pump the event queue occasionally to keep OS happy... */
   1.383 -        if (now - last > 1000)
   1.384 -        {
   1.385 +        if (now - last > 1000) {
   1.386              last = now;
   1.387 -            while (SDL_PollEvent(&event)) { /* no-op. */ }
   1.388 +            while (SDL_PollEvent(&event)) {     /* no-op. */
   1.389 +            }
   1.390          }
   1.391  
   1.392          iterations++;
   1.393          elasped += blit(dest, src, randRange(0, wmax), randRange(0, hmax));
   1.394 -        if (isScreen)
   1.395 -        {
   1.396 -            SDL_Flip(dest);  /* show it! */
   1.397 -            SDL_FillRect(dest, NULL, clearColor); /* blank it for next time! */
   1.398 +        if (isScreen) {
   1.399 +            SDL_Flip(dest);     /* show it! */
   1.400 +            SDL_FillRect(dest, NULL, clearColor);       /* blank it for next time! */
   1.401          }
   1.402  
   1.403          now = SDL_GetTicks();
   1.404 -    } while (now < end);
   1.405 +    }
   1.406 +    while (now < end);
   1.407  
   1.408      printf("Non-blitting crap accounted for %d percent of this run.\n",
   1.409 -            percent(testms - elasped, testms));
   1.410 +           percent(testms - elasped, testms));
   1.411  
   1.412      printf("%d blits took %d ms (%d fps).\n",
   1.413 -            (int) iterations,
   1.414 -            (int) elasped,
   1.415 -            (int) (((float)iterations) / (((float)elasped) / 1000.0f)));
   1.416 +           (int) iterations,
   1.417 +           (int) elasped,
   1.418 +           (int) (((float) iterations) / (((float) elasped) / 1000.0f)));
   1.419  }
   1.420  
   1.421 -int main(int argc, char **argv)
   1.422 +int
   1.423 +main(int argc, char **argv)
   1.424  {
   1.425      int initialized = setup_test(argc, argv);
   1.426 -    if (initialized)
   1.427 -    {
   1.428 +    if (initialized) {
   1.429          test_blit_speed();
   1.430          SDL_Quit();
   1.431      }
   1.432 -    return(!initialized);
   1.433 +    return (!initialized);
   1.434  }
   1.435  
   1.436  /* end of testblitspeed.c ... */
   1.437 -