test/testspriteminimal.c
author David Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 21:39:48 -0500
changeset 8563 c0e68f3b6bbb
parent 7639 9406b7dd2f2d
child 8149 681eb46b8ac4
permissions -rw-r--r--
WinRT: compiled the d3d11 renderer's shaders into SDL itself

Previously, the shaders would get compiled separately, the output of which would need to be packaged into the app. This change should make SDL's dll be the only binary needed to include SDL in a WinRT app.
     1 /*
     2   Copyright (C) 1997-2013 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 /* Simple program:  Move N sprites around on the screen as fast as possible */
    13 
    14 #include <stdlib.h>
    15 #include <stdio.h>
    16 #include <time.h>
    17 
    18 #include "SDL.h"
    19 
    20 #define WINDOW_WIDTH    640
    21 #define WINDOW_HEIGHT   480
    22 #define NUM_SPRITES     100
    23 #define MAX_SPEED       1
    24 
    25 static SDL_Texture *sprite;
    26 static SDL_Rect positions[NUM_SPRITES];
    27 static SDL_Rect velocities[NUM_SPRITES];
    28 static int sprite_w, sprite_h;
    29 
    30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    31 static void
    32 quit(int rc)
    33 {
    34     exit(rc);
    35 }
    36 
    37 int
    38 LoadSprite(char *file, SDL_Renderer *renderer)
    39 {
    40     SDL_Surface *temp;
    41 
    42     /* Load the sprite image */
    43     temp = SDL_LoadBMP(file);
    44     if (temp == NULL) {
    45         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
    46         return (-1);
    47     }
    48     sprite_w = temp->w;
    49     sprite_h = temp->h;
    50 
    51     /* Set transparent pixel as the pixel at (0,0) */
    52     if (temp->format->palette) {
    53         SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    54     } else {
    55         switch (temp->format->BitsPerPixel) {
    56         case 15:
    57             SDL_SetColorKey(temp, SDL_TRUE,
    58                             (*(Uint16 *) temp->pixels) & 0x00007FFF);
    59             break;
    60         case 16:
    61             SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    62             break;
    63         case 24:
    64             SDL_SetColorKey(temp, SDL_TRUE,
    65                             (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    66             break;
    67         case 32:
    68             SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    69             break;
    70         }
    71     }
    72 
    73     /* Create textures from the image */
    74     sprite = SDL_CreateTextureFromSurface(renderer, temp);
    75     if (!sprite) {
    76         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    77         SDL_FreeSurface(temp);
    78         return (-1);
    79     }
    80     SDL_FreeSurface(temp);
    81 
    82     /* We're ready to roll. :) */
    83     return (0);
    84 }
    85 
    86 void
    87 MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
    88 {
    89     int i;
    90     int window_w = WINDOW_WIDTH;
    91     int window_h = WINDOW_HEIGHT;
    92     SDL_Rect *position, *velocity;
    93 
    94     /* Draw a gray background */
    95     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    96     SDL_RenderClear(renderer);
    97 
    98     /* Move the sprite, bounce at the wall, and draw */
    99     for (i = 0; i < NUM_SPRITES; ++i) {
   100         position = &positions[i];
   101         velocity = &velocities[i];
   102         position->x += velocity->x;
   103         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   104             velocity->x = -velocity->x;
   105             position->x += velocity->x;
   106         }
   107         position->y += velocity->y;
   108         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   109             velocity->y = -velocity->y;
   110             position->y += velocity->y;
   111         }
   112 
   113         /* Blit the sprite onto the screen */
   114         SDL_RenderCopy(renderer, sprite, NULL, position);
   115     }
   116 
   117     /* Update the screen! */
   118     SDL_RenderPresent(renderer);
   119 }
   120 
   121 int
   122 main(int argc, char *argv[])
   123 {
   124     SDL_Window *window;
   125     SDL_Renderer *renderer;
   126     int i, done;
   127     SDL_Event event;
   128 
   129 	/* Enable standard application logging */
   130     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   131 
   132     if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
   133         quit(2);
   134     }
   135 
   136     if (LoadSprite("icon.bmp", renderer) < 0) {
   137         quit(2);
   138     }
   139 
   140     /* Initialize the sprite positions */
   141     srand(time(NULL));
   142     for (i = 0; i < NUM_SPRITES; ++i) {
   143         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   144         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   145         positions[i].w = sprite_w;
   146         positions[i].h = sprite_h;
   147         velocities[i].x = 0;
   148         velocities[i].y = 0;
   149         while (!velocities[i].x && !velocities[i].y) {
   150             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   151             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   152         }
   153     }
   154 
   155     /* Main render loop */
   156     done = 0;
   157     while (!done) {
   158         /* Check for events */
   159         while (SDL_PollEvent(&event)) {
   160             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   161                 done = 1;
   162             }
   163         }
   164         MoveSprites(renderer, sprite);
   165     }
   166 
   167     quit(0);
   168 
   169     return 0; /* to prevent compiler warning */
   170 }
   171 
   172 /* vi: set ts=4 sw=4 expandtab: */