test/testnative.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    11
*/
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* Simple program:  Create a native window and attach an SDL renderer */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
3601
240a7700a271 Added missing include for stdio
Sam Lantinga <slouken@libsdl.org>
parents: 3596
diff changeset
    14
#include <stdio.h>
7442
a65a1c17af2b Fixed compiler warnings in test programs by adding includes directives.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    15
#include <stdlib.h> /* for srand() */
a65a1c17af2b Fixed compiler warnings in test programs by adding includes directives.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
    16
#include <time.h> /* for time() */
3601
240a7700a271 Added missing include for stdio
Sam Lantinga <slouken@libsdl.org>
parents: 3596
diff changeset
    17
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
#include "testnative.h"
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
#define WINDOW_W    640
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#define WINDOW_H    480
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#define NUM_SPRITES 100
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6256
diff changeset
    23
#define MAX_SPEED   1
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
static NativeWindowFactory *factories[] = {
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    26
#ifdef TEST_NATIVE_WINDOWS
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    27
    &WindowsWindowFactory,
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#endif
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#ifdef TEST_NATIVE_X11
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
    &X11WindowFactory,
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#endif
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
#ifdef TEST_NATIVE_COCOA
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
    &CocoaWindowFactory,
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#endif
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
    NULL
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
};
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
static NativeWindowFactory *factory = NULL;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
static void *native_window;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
static SDL_Rect *positions, *velocities;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
static void
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
quit(int rc)
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
{
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
    SDL_VideoQuit();
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
    if (native_window) {
3060
4cf533f434d8 Implemented Win32 version of the native window test
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
    47
        factory->DestroyNativeWindow(native_window);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
    exit(rc);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
}
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3601
diff changeset
    52
SDL_Texture *
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    53
LoadSprite(SDL_Renderer *renderer, char *file)
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
{
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    SDL_Surface *temp;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3601
diff changeset
    56
    SDL_Texture *sprite;
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
    /* Load the sprite image */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    temp = SDL_LoadBMP(file);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
    if (temp == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    61
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
        return 0;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
    /* Set transparent pixel as the pixel at (0,0) */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    if (temp->format->palette) {
6256
1d905b13b102 Removed the SDL 1.2 compatibility API... we'll see how painful this is.
Sam Lantinga <slouken@libsdl.org>
parents: 6175
diff changeset
    67
        SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
    /* Create textures from the image */
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    71
    sprite = SDL_CreateTextureFromSurface(renderer, temp);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    if (!sprite) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    73
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
        SDL_FreeSurface(temp);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
        return 0;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    SDL_FreeSurface(temp);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
    /* We're ready to roll. :) */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    return sprite;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
}
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
void
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    84
MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
{
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    int sprite_w, sprite_h;
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    87
    int i;
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    88
    SDL_Rect viewport;
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
    SDL_Rect *position, *velocity;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
    /* Query the sizes */
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    92
    SDL_RenderGetViewport(renderer, &viewport);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    95
    /* Draw a gray background */
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    96
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    97
    SDL_RenderClear(renderer);
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    98
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    /* Move the sprite, bounce at the wall, and draw */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    for (i = 0; i < NUM_SPRITES; ++i) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
        position = &positions[i];
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
        velocity = &velocities[i];
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
        position->x += velocity->x;
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   104
        if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
            velocity->x = -velocity->x;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
            position->x += velocity->x;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
        }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
        position->y += velocity->y;
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   109
        if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
            velocity->y = -velocity->y;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
            position->y += velocity->y;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
        }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
        /* Blit the sprite onto the screen */
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   115
        SDL_RenderCopy(renderer, sprite, NULL, position);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
    /* Update the screen! */
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   119
    SDL_RenderPresent(renderer);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
}
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
int
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
main(int argc, char *argv[])
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
{
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
    int i, done;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
    const char *driver;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3601
diff changeset
   127
    SDL_Window *window;
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   128
    SDL_Renderer *renderer;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3601
diff changeset
   129
    SDL_Texture *sprite;
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
    int window_w, window_h;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
    int sprite_w, sprite_h;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
    SDL_Event event;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   134
    /* Enable standard application logging */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   135
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   136
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   137
    if (SDL_VideoInit(NULL) < 0) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   138
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
                SDL_GetError());
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
        exit(1);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
    driver = SDL_GetCurrentVideoDriver();
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
    /* Find a native window driver and create a native window */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
    for (i = 0; factories[i]; ++i) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
        if (SDL_strcmp(driver, factories[i]->tag) == 0) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
            factory = factories[i];
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
            break;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
        }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
    if (!factory) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   152
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
                driver);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
        quit(2);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
    }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   156
    SDL_Log("Creating native window for %s driver\n", driver);
3060
4cf533f434d8 Implemented Win32 version of the native window test
Sam Lantinga <slouken@libsdl.org>
parents: 3057
diff changeset
   157
    native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
    if (!native_window) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   159
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
        quit(3);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
    window = SDL_CreateWindowFrom(native_window);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
    if (!window) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   164
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
        quit(4);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
    SDL_SetWindowTitle(window, "SDL Native Window Test");
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
    /* Create the renderer */
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   170
    renderer = SDL_CreateRenderer(window, -1, 0);
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   171
    if (!renderer) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   172
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
        quit(5);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
    /* Clear the window, load the sprite and go! */
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   177
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   178
    SDL_RenderClear(renderer);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   180
    sprite = LoadSprite(renderer, "icon.bmp");
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
    if (!sprite) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
        quit(6);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    /* Allocate memory for the sprite info */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
    SDL_GetWindowSize(window, &window_w, &window_h);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
    if (!positions || !velocities) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   191
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
        quit(2);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
    srand(time(NULL));
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
    for (i = 0; i < NUM_SPRITES; ++i) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
        positions[i].x = rand() % (window_w - sprite_w);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
        positions[i].y = rand() % (window_h - sprite_h);
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
        positions[i].w = sprite_w;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
        positions[i].h = sprite_h;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
        velocities[i].x = 0;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
        velocities[i].y = 0;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
        while (!velocities[i].x && !velocities[i].y) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
        }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    /* Main render loop */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    done = 0;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    while (!done) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
        /* Check for events */
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
        while (SDL_PollEvent(&event)) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
            switch (event.type) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
            case SDL_WINDOWEVENT:
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
                switch (event.window.event) {
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
                case SDL_WINDOWEVENT_EXPOSED:
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   217
                    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   218
                    SDL_RenderClear(renderer);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
                    break;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
                }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
                break;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
            case SDL_QUIT:
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
                done = 1;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
                break;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
            default:
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
                break;
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
            }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
        }
6175
42c40787f382 Added testnative to the Makefile and fixed building on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   229
        MoveSprites(renderer, sprite);
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
    }
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   231
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
    quit(0);
7443
58c5fcba814d Fixed compiler warnings in test programs by adding return statements.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7442
diff changeset
   233
58c5fcba814d Fixed compiler warnings in test programs by adding return statements.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7442
diff changeset
   234
    return 0; /* to prevent compiler warning */
3057
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
}
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
089a77aebb7d Added test program for SDL_CreateWindowFrom()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
/* vi: set ts=4 sw=4 expandtab: */