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