test/testnative.c
changeset 3057 089a77aebb7d
child 3060 4cf533f434d8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testnative.c	Mon Feb 09 05:32:12 2009 +0000
     1.3 @@ -0,0 +1,219 @@
     1.4 +/* Simple program:  Create a native window and attach an SDL renderer */
     1.5 +
     1.6 +#include "testnative.h"
     1.7 +
     1.8 +#define WINDOW_W    640
     1.9 +#define WINDOW_H    480
    1.10 +#define NUM_SPRITES 100
    1.11 +#define MAX_SPEED 	1
    1.12 +
    1.13 +static NativeWindowFactory *factories[] = {
    1.14 +#ifdef TEST_NATIVE_WIN32
    1.15 +    &Win32WindowFactory,
    1.16 +#endif
    1.17 +#ifdef TEST_NATIVE_X11
    1.18 +    &X11WindowFactory,
    1.19 +#endif
    1.20 +#ifdef TEST_NATIVE_COCOA
    1.21 +    &CocoaWindowFactory,
    1.22 +#endif
    1.23 +    NULL
    1.24 +};
    1.25 +static NativeWindowFactory *factory = NULL;
    1.26 +static void *native_window;
    1.27 +static SDL_Rect *positions, *velocities;
    1.28 +
    1.29 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.30 +static void
    1.31 +quit(int rc)
    1.32 +{
    1.33 +    SDL_VideoQuit();
    1.34 +    if (native_window) {
    1.35 +        factory->DestroyWindow(native_window);
    1.36 +    }
    1.37 +    exit(rc);
    1.38 +}
    1.39 +
    1.40 +SDL_TextureID
    1.41 +LoadSprite(SDL_WindowID window, char *file)
    1.42 +{
    1.43 +    SDL_Surface *temp;
    1.44 +    SDL_TextureID sprite;
    1.45 +
    1.46 +    /* Load the sprite image */
    1.47 +    temp = SDL_LoadBMP(file);
    1.48 +    if (temp == NULL) {
    1.49 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    1.50 +        return 0;
    1.51 +    }
    1.52 +
    1.53 +    /* Set transparent pixel as the pixel at (0,0) */
    1.54 +    if (temp->format->palette) {
    1.55 +        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
    1.56 +    }
    1.57 +
    1.58 +    /* Create textures from the image */
    1.59 +    SDL_SelectRenderer(window);
    1.60 +    sprite = SDL_CreateTextureFromSurface(0, temp);
    1.61 +    if (!sprite) {
    1.62 +        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    1.63 +        SDL_FreeSurface(temp);
    1.64 +        return 0;
    1.65 +    }
    1.66 +    SDL_FreeSurface(temp);
    1.67 +
    1.68 +    /* We're ready to roll. :) */
    1.69 +    return sprite;
    1.70 +}
    1.71 +
    1.72 +void
    1.73 +MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
    1.74 +{
    1.75 +    int i, n;
    1.76 +    int window_w, window_h;
    1.77 +    int sprite_w, sprite_h;
    1.78 +    SDL_Rect *position, *velocity;
    1.79 +
    1.80 +    SDL_SelectRenderer(window);
    1.81 +
    1.82 +    /* Query the sizes */
    1.83 +    SDL_GetWindowSize(window, &window_w, &window_h);
    1.84 +    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
    1.85 +
    1.86 +    /* Move the sprite, bounce at the wall, and draw */
    1.87 +    n = 0;
    1.88 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    1.89 +    SDL_RenderFill(NULL);
    1.90 +    for (i = 0; i < NUM_SPRITES; ++i) {
    1.91 +        position = &positions[i];
    1.92 +        velocity = &velocities[i];
    1.93 +        position->x += velocity->x;
    1.94 +        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
    1.95 +            velocity->x = -velocity->x;
    1.96 +            position->x += velocity->x;
    1.97 +        }
    1.98 +        position->y += velocity->y;
    1.99 +        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   1.100 +            velocity->y = -velocity->y;
   1.101 +            position->y += velocity->y;
   1.102 +        }
   1.103 +
   1.104 +        /* Blit the sprite onto the screen */
   1.105 +        SDL_RenderCopy(sprite, NULL, position);
   1.106 +    }
   1.107 +
   1.108 +    /* Update the screen! */
   1.109 +    SDL_RenderPresent();
   1.110 +}
   1.111 +
   1.112 +int
   1.113 +main(int argc, char *argv[])
   1.114 +{
   1.115 +    int i, done;
   1.116 +    const char *driver;
   1.117 +    SDL_WindowID window;
   1.118 +    SDL_TextureID sprite;
   1.119 +    int window_w, window_h;
   1.120 +    int sprite_w, sprite_h;
   1.121 +    SDL_Event event;
   1.122 +
   1.123 +    if (SDL_VideoInit(NULL, 0) < 0) {
   1.124 +        fprintf(stderr, "Couldn't initialize SDL video: %s\n",
   1.125 +                SDL_GetError());
   1.126 +        exit(1);
   1.127 +    }
   1.128 +    driver = SDL_GetCurrentVideoDriver();
   1.129 +
   1.130 +    /* Find a native window driver and create a native window */
   1.131 +    for (i = 0; factories[i]; ++i) {
   1.132 +        if (SDL_strcmp(driver, factories[i]->tag) == 0) {
   1.133 +            factory = factories[i];
   1.134 +            break;
   1.135 +        }
   1.136 +    }
   1.137 +    if (!factory) {
   1.138 +        fprintf(stderr, "Couldn't find native window code for %s driver\n",
   1.139 +                driver);
   1.140 +        quit(2);
   1.141 +    }
   1.142 +    printf("Creating native window for %s driver\n", driver);
   1.143 +    native_window = factory->CreateWindow(WINDOW_W, WINDOW_H);
   1.144 +    if (!native_window) {
   1.145 +        fprintf(stderr, "Couldn't create native window\n");
   1.146 +        quit(3);
   1.147 +    }
   1.148 +    window = SDL_CreateWindowFrom(native_window);
   1.149 +    if (!window) {
   1.150 +        fprintf(stderr, "Couldn't create SDL window: %s\n", SDL_GetError());
   1.151 +        quit(4);
   1.152 +    }
   1.153 +    SDL_SetWindowTitle(window, "SDL Native Window Test");
   1.154 +
   1.155 +    /* Create the renderer */
   1.156 +    if (SDL_CreateRenderer(window, -1, 0) < 0) {
   1.157 +        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
   1.158 +        quit(5);
   1.159 +    }
   1.160 +
   1.161 +    /* Clear the window, load the sprite and go! */
   1.162 +    SDL_SelectRenderer(window);
   1.163 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.164 +    SDL_RenderFill(NULL);
   1.165 +
   1.166 +    sprite = LoadSprite(window, "icon.bmp");
   1.167 +    if (!sprite) {
   1.168 +        quit(6);
   1.169 +    }
   1.170 +
   1.171 +    /* Allocate memory for the sprite info */
   1.172 +    SDL_GetWindowSize(window, &window_w, &window_h);
   1.173 +    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
   1.174 +    positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
   1.175 +    velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
   1.176 +    if (!positions || !velocities) {
   1.177 +        fprintf(stderr, "Out of memory!\n");
   1.178 +        quit(2);
   1.179 +    }
   1.180 +    srand(time(NULL));
   1.181 +    for (i = 0; i < NUM_SPRITES; ++i) {
   1.182 +        positions[i].x = rand() % (window_w - sprite_w);
   1.183 +        positions[i].y = rand() % (window_h - sprite_h);
   1.184 +        positions[i].w = sprite_w;
   1.185 +        positions[i].h = sprite_h;
   1.186 +        velocities[i].x = 0;
   1.187 +        velocities[i].y = 0;
   1.188 +        while (!velocities[i].x && !velocities[i].y) {
   1.189 +            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.190 +            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.191 +        }
   1.192 +    }
   1.193 +
   1.194 +    /* Main render loop */
   1.195 +    done = 0;
   1.196 +    while (!done) {
   1.197 +        /* Check for events */
   1.198 +        while (SDL_PollEvent(&event)) {
   1.199 +            switch (event.type) {
   1.200 +            case SDL_WINDOWEVENT:
   1.201 +                switch (event.window.event) {
   1.202 +                case SDL_WINDOWEVENT_EXPOSED:
   1.203 +                    SDL_SelectRenderer(event.window.windowID);
   1.204 +                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.205 +                    SDL_RenderFill(NULL);
   1.206 +                    break;
   1.207 +                }
   1.208 +                break;
   1.209 +            case SDL_QUIT:
   1.210 +                done = 1;
   1.211 +                break;
   1.212 +            default:
   1.213 +                break;
   1.214 +            }
   1.215 +        }
   1.216 +        MoveSprites(window, sprite);
   1.217 +    }
   1.218 +
   1.219 +    quit(0);
   1.220 +}
   1.221 +
   1.222 +/* vi: set ts=4 sw=4 expandtab: */