test/testsprite2.c
changeset 1917 3f54b3ec5a07
parent 1916 c773b0c0ac89
child 1965 a788656ca29a
equal deleted inserted replaced
1916:c773b0c0ac89 1917:3f54b3ec5a07
    15 static SDL_TextureID *sprites;
    15 static SDL_TextureID *sprites;
    16 static SDL_Rect *positions;
    16 static SDL_Rect *positions;
    17 static SDL_Rect *velocities;
    17 static SDL_Rect *velocities;
    18 static int sprite_w, sprite_h;
    18 static int sprite_w, sprite_h;
    19 static SDL_TextureBlendMode blendMode = SDL_TextureBlendMode_Mask;
    19 static SDL_TextureBlendMode blendMode = SDL_TextureBlendMode_Mask;
       
    20 static SDL_TextureScaleMode scaleMode = SDL_TextureScaleMode_None;
    20 
    21 
    21 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    22 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    22 static void
    23 static void
    23 quit(int rc)
    24 quit(int rc)
    24 {
    25 {
    85     SDL_GetWindowSize(window, &window_w, &window_h);
    86     SDL_GetWindowSize(window, &window_w, &window_h);
    86 
    87 
    87     /* Move the sprite, bounce at the wall, and draw */
    88     /* Move the sprite, bounce at the wall, and draw */
    88     n = 0;
    89     n = 0;
    89     SDL_RenderFill(NULL, BACKGROUND);
    90     SDL_RenderFill(NULL, BACKGROUND);
    90     /*
       
    91        for (i = 0; i < num_sprites; ++i) {
       
    92        position = &positions[i];
       
    93        SDL_RenderFill(position, BACKGROUND);
       
    94        }
       
    95      */
       
    96     for (i = 0; i < num_sprites; ++i) {
    91     for (i = 0; i < num_sprites; ++i) {
    97         position = &positions[i];
    92         position = &positions[i];
    98         velocity = &velocities[i];
    93         velocity = &velocities[i];
    99         position->x += velocity->x;
    94         position->x += velocity->x;
   100         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
    95         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   106             velocity->y = -velocity->y;
   101             velocity->y = -velocity->y;
   107             position->y += velocity->y;
   102             position->y += velocity->y;
   108         }
   103         }
   109 
   104 
   110         /* Blit the sprite onto the screen */
   105         /* Blit the sprite onto the screen */
   111         SDL_RenderCopy(sprite, NULL, position, blendMode,
   106         SDL_RenderCopy(sprite, NULL, position, blendMode, scaleMode);
   112                        SDL_TextureScaleMode_None);
       
   113     }
   107     }
   114 
   108 
   115     /* Update the screen! */
   109     /* Update the screen! */
   116     SDL_RenderPresent();
   110     SDL_RenderPresent();
   117 }
   111 }
   140             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   134             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   141                 if (argv[i + 1]) {
   135                 if (argv[i + 1]) {
   142                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   136                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   143                         blendMode = SDL_TextureBlendMode_None;
   137                         blendMode = SDL_TextureBlendMode_None;
   144                         consumed = 2;
   138                         consumed = 2;
       
   139                     } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
       
   140                         blendMode = SDL_TextureBlendMode_Mask;
       
   141                         consumed = 2;
   145                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   142                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   146                         blendMode = SDL_TextureBlendMode_Blend;
   143                         blendMode = SDL_TextureBlendMode_Blend;
   147                         consumed = 2;
   144                         consumed = 2;
   148                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   145                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   149                         blendMode = SDL_TextureBlendMode_Add;
   146                         blendMode = SDL_TextureBlendMode_Add;
   150                         consumed = 2;
   147                         consumed = 2;
   151                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   148                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   152                         blendMode = SDL_TextureBlendMode_Mod;
   149                         blendMode = SDL_TextureBlendMode_Mod;
       
   150                         consumed = 2;
       
   151                     }
       
   152                 }
       
   153             } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
       
   154                 if (argv[i + 1]) {
       
   155                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
       
   156                         scaleMode = SDL_TextureScaleMode_None;
       
   157                         consumed = 2;
       
   158                     } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
       
   159                         scaleMode = SDL_TextureScaleMode_Fast;
       
   160                         consumed = 2;
       
   161                     } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
       
   162                         scaleMode = SDL_TextureScaleMode_Slow;
       
   163                         consumed = 2;
       
   164                     } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
       
   165                         scaleMode = SDL_TextureScaleMode_Best;
   153                         consumed = 2;
   166                         consumed = 2;
   154                     }
   167                     }
   155                 }
   168                 }
   156             } else if (SDL_isdigit(*argv[i])) {
   169             } else if (SDL_isdigit(*argv[i])) {
   157                 num_sprites = SDL_atoi(argv[i]);
   170                 num_sprites = SDL_atoi(argv[i]);
   158                 consumed = 1;
   171                 consumed = 1;
   159             }
   172             }
   160         }
   173         }
   161         if (consumed < 0) {
   174         if (consumed < 0) {
   162             fprintf(stderr, "Usage: %s %s [--blend none|blend|add|mod]",
   175             fprintf(stderr,
       
   176                     "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best]",
   163                     argv[0], CommonUsage(state));
   177                     argv[0], CommonUsage(state));
   164             quit(1);
   178             quit(1);
   165         }
   179         }
   166         i += consumed;
   180         i += consumed;
   167     }
   181     }
   190     if (!positions || !velocities) {
   204     if (!positions || !velocities) {
   191         fprintf(stderr, "Out of memory!\n");
   205         fprintf(stderr, "Out of memory!\n");
   192         quit(2);
   206         quit(2);
   193     }
   207     }
   194     srand(time(NULL));
   208     srand(time(NULL));
       
   209     if (scaleMode != SDL_TextureScaleMode_None) {
       
   210         sprite_w += sprite_w / 2;
       
   211         sprite_h += sprite_h / 2;
       
   212     }
   195     for (i = 0; i < num_sprites; ++i) {
   213     for (i = 0; i < num_sprites; ++i) {
   196         positions[i].x = rand() % (state->window_w - sprite_w);
   214         positions[i].x = rand() % (state->window_w - sprite_w);
   197         positions[i].y = rand() % (state->window_h - sprite_h);
   215         positions[i].y = rand() % (state->window_h - sprite_h);
   198         positions[i].w = sprite_w;
   216         positions[i].w = sprite_w;
   199         positions[i].h = sprite_h;
   217         positions[i].h = sprite_h;