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