test/testnative.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Jan 2010 06:21:52 +0000
changeset 3685 64ce267332c6
parent 3601 240a7700a271
child 5062 e8916fe9cfc8
permissions -rw-r--r--
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
slouken@3057
     1
/* Simple program:  Create a native window and attach an SDL renderer */
slouken@3057
     2
slouken@3601
     3
#include <stdio.h>
slouken@3601
     4
slouken@3057
     5
#include "testnative.h"
slouken@3057
     6
slouken@3057
     7
#define WINDOW_W    640
slouken@3057
     8
#define WINDOW_H    480
slouken@3057
     9
#define NUM_SPRITES 100
slouken@3057
    10
#define MAX_SPEED 	1
slouken@3057
    11
slouken@3057
    12
static NativeWindowFactory *factories[] = {
slouken@3057
    13
#ifdef TEST_NATIVE_WIN32
slouken@3057
    14
    &Win32WindowFactory,
slouken@3057
    15
#endif
slouken@3057
    16
#ifdef TEST_NATIVE_X11
slouken@3057
    17
    &X11WindowFactory,
slouken@3057
    18
#endif
slouken@3057
    19
#ifdef TEST_NATIVE_COCOA
slouken@3057
    20
    &CocoaWindowFactory,
slouken@3057
    21
#endif
slouken@3057
    22
    NULL
slouken@3057
    23
};
slouken@3057
    24
static NativeWindowFactory *factory = NULL;
slouken@3057
    25
static void *native_window;
slouken@3057
    26
static SDL_Rect *positions, *velocities;
slouken@3057
    27
slouken@3057
    28
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3057
    29
static void
slouken@3057
    30
quit(int rc)
slouken@3057
    31
{
slouken@3057
    32
    SDL_VideoQuit();
slouken@3057
    33
    if (native_window) {
slouken@3060
    34
        factory->DestroyNativeWindow(native_window);
slouken@3057
    35
    }
slouken@3057
    36
    exit(rc);
slouken@3057
    37
}
slouken@3057
    38
slouken@3685
    39
SDL_Texture *
slouken@3685
    40
LoadSprite(SDL_Window * window, char *file)
slouken@3057
    41
{
slouken@3057
    42
    SDL_Surface *temp;
slouken@3685
    43
    SDL_Texture *sprite;
slouken@3057
    44
slouken@3057
    45
    /* Load the sprite image */
slouken@3057
    46
    temp = SDL_LoadBMP(file);
slouken@3057
    47
    if (temp == NULL) {
slouken@3057
    48
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@3057
    49
        return 0;
slouken@3057
    50
    }
slouken@3057
    51
slouken@3057
    52
    /* Set transparent pixel as the pixel at (0,0) */
slouken@3057
    53
    if (temp->format->palette) {
slouken@3057
    54
        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
slouken@3057
    55
    }
slouken@3057
    56
slouken@3057
    57
    /* Create textures from the image */
slouken@3057
    58
    SDL_SelectRenderer(window);
slouken@3057
    59
    sprite = SDL_CreateTextureFromSurface(0, temp);
slouken@3057
    60
    if (!sprite) {
slouken@3057
    61
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@3057
    62
        SDL_FreeSurface(temp);
slouken@3057
    63
        return 0;
slouken@3057
    64
    }
slouken@3057
    65
    SDL_FreeSurface(temp);
slouken@3057
    66
slouken@3057
    67
    /* We're ready to roll. :) */
slouken@3057
    68
    return sprite;
slouken@3057
    69
}
slouken@3057
    70
slouken@3057
    71
void
slouken@3685
    72
MoveSprites(SDL_Window * window, SDL_Texture * sprite)
slouken@3057
    73
{
slouken@3057
    74
    int i, n;
slouken@3057
    75
    int window_w, window_h;
slouken@3057
    76
    int sprite_w, sprite_h;
slouken@3057
    77
    SDL_Rect *position, *velocity;
slouken@3057
    78
slouken@3057
    79
    SDL_SelectRenderer(window);
slouken@3057
    80
slouken@3057
    81
    /* Query the sizes */
slouken@3057
    82
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@3057
    83
    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
slouken@3057
    84
slouken@3057
    85
    /* Move the sprite, bounce at the wall, and draw */
slouken@3057
    86
    n = 0;
slouken@3057
    87
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
    88
    SDL_RenderClear();
slouken@3057
    89
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3057
    90
        position = &positions[i];
slouken@3057
    91
        velocity = &velocities[i];
slouken@3057
    92
        position->x += velocity->x;
slouken@3057
    93
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@3057
    94
            velocity->x = -velocity->x;
slouken@3057
    95
            position->x += velocity->x;
slouken@3057
    96
        }
slouken@3057
    97
        position->y += velocity->y;
slouken@3057
    98
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@3057
    99
            velocity->y = -velocity->y;
slouken@3057
   100
            position->y += velocity->y;
slouken@3057
   101
        }
slouken@3057
   102
slouken@3057
   103
        /* Blit the sprite onto the screen */
slouken@3057
   104
        SDL_RenderCopy(sprite, NULL, position);
slouken@3057
   105
    }
slouken@3057
   106
slouken@3057
   107
    /* Update the screen! */
slouken@3057
   108
    SDL_RenderPresent();
slouken@3057
   109
}
slouken@3057
   110
slouken@3057
   111
int
slouken@3057
   112
main(int argc, char *argv[])
slouken@3057
   113
{
slouken@3057
   114
    int i, done;
slouken@3057
   115
    const char *driver;
slouken@3685
   116
    SDL_Window *window;
slouken@3685
   117
    SDL_Texture *sprite;
slouken@3057
   118
    int window_w, window_h;
slouken@3057
   119
    int sprite_w, sprite_h;
slouken@3057
   120
    SDL_Event event;
slouken@3057
   121
slouken@3057
   122
    if (SDL_VideoInit(NULL, 0) < 0) {
slouken@3057
   123
        fprintf(stderr, "Couldn't initialize SDL video: %s\n",
slouken@3057
   124
                SDL_GetError());
slouken@3057
   125
        exit(1);
slouken@3057
   126
    }
slouken@3057
   127
    driver = SDL_GetCurrentVideoDriver();
slouken@3057
   128
slouken@3057
   129
    /* Find a native window driver and create a native window */
slouken@3057
   130
    for (i = 0; factories[i]; ++i) {
slouken@3057
   131
        if (SDL_strcmp(driver, factories[i]->tag) == 0) {
slouken@3057
   132
            factory = factories[i];
slouken@3057
   133
            break;
slouken@3057
   134
        }
slouken@3057
   135
    }
slouken@3057
   136
    if (!factory) {
slouken@3057
   137
        fprintf(stderr, "Couldn't find native window code for %s driver\n",
slouken@3057
   138
                driver);
slouken@3057
   139
        quit(2);
slouken@3057
   140
    }
slouken@3057
   141
    printf("Creating native window for %s driver\n", driver);
slouken@3060
   142
    native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
slouken@3057
   143
    if (!native_window) {
slouken@3057
   144
        fprintf(stderr, "Couldn't create native window\n");
slouken@3057
   145
        quit(3);
slouken@3057
   146
    }
slouken@3057
   147
    window = SDL_CreateWindowFrom(native_window);
slouken@3057
   148
    if (!window) {
slouken@3057
   149
        fprintf(stderr, "Couldn't create SDL window: %s\n", SDL_GetError());
slouken@3057
   150
        quit(4);
slouken@3057
   151
    }
slouken@3057
   152
    SDL_SetWindowTitle(window, "SDL Native Window Test");
slouken@3057
   153
slouken@3057
   154
    /* Create the renderer */
slouken@3057
   155
    if (SDL_CreateRenderer(window, -1, 0) < 0) {
slouken@3057
   156
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
slouken@3057
   157
        quit(5);
slouken@3057
   158
    }
slouken@3057
   159
slouken@3057
   160
    /* Clear the window, load the sprite and go! */
slouken@3057
   161
    SDL_SelectRenderer(window);
slouken@3057
   162
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
   163
    SDL_RenderClear();
slouken@3057
   164
slouken@3057
   165
    sprite = LoadSprite(window, "icon.bmp");
slouken@3057
   166
    if (!sprite) {
slouken@3057
   167
        quit(6);
slouken@3057
   168
    }
slouken@3057
   169
slouken@3057
   170
    /* Allocate memory for the sprite info */
slouken@3057
   171
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@3057
   172
    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
slouken@3057
   173
    positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
slouken@3057
   174
    velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
slouken@3057
   175
    if (!positions || !velocities) {
slouken@3057
   176
        fprintf(stderr, "Out of memory!\n");
slouken@3057
   177
        quit(2);
slouken@3057
   178
    }
slouken@3057
   179
    srand(time(NULL));
slouken@3057
   180
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3057
   181
        positions[i].x = rand() % (window_w - sprite_w);
slouken@3057
   182
        positions[i].y = rand() % (window_h - sprite_h);
slouken@3057
   183
        positions[i].w = sprite_w;
slouken@3057
   184
        positions[i].h = sprite_h;
slouken@3057
   185
        velocities[i].x = 0;
slouken@3057
   186
        velocities[i].y = 0;
slouken@3057
   187
        while (!velocities[i].x && !velocities[i].y) {
slouken@3057
   188
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3057
   189
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3057
   190
        }
slouken@3057
   191
    }
slouken@3057
   192
slouken@3057
   193
    /* Main render loop */
slouken@3057
   194
    done = 0;
slouken@3057
   195
    while (!done) {
slouken@3057
   196
        /* Check for events */
slouken@3057
   197
        while (SDL_PollEvent(&event)) {
slouken@3057
   198
            switch (event.type) {
slouken@3057
   199
            case SDL_WINDOWEVENT:
slouken@3057
   200
                switch (event.window.event) {
slouken@3057
   201
                case SDL_WINDOWEVENT_EXPOSED:
slouken@3057
   202
                    SDL_SelectRenderer(event.window.windowID);
slouken@3057
   203
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
   204
                    SDL_RenderClear();
slouken@3057
   205
                    break;
slouken@3057
   206
                }
slouken@3057
   207
                break;
slouken@3057
   208
            case SDL_QUIT:
slouken@3057
   209
                done = 1;
slouken@3057
   210
                break;
slouken@3057
   211
            default:
slouken@3057
   212
                break;
slouken@3057
   213
            }
slouken@3057
   214
        }
slouken@3057
   215
        MoveSprites(window, sprite);
slouken@3057
   216
    }
slouken@3057
   217
slouken@3057
   218
    quit(0);
slouken@3057
   219
}
slouken@3057
   220
slouken@3057
   221
/* vi: set ts=4 sw=4 expandtab: */