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