test/testnative.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5062 e8916fe9cfc8
child 6175 42c40787f382
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     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_Window * window, 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, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
    66     }
    67 
    68     /* Create textures from the image */
    69     SDL_SelectRenderer(window);
    70     sprite = SDL_CreateTextureFromSurface(0, temp);
    71     if (!sprite) {
    72         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    73         SDL_FreeSurface(temp);
    74         return 0;
    75     }
    76     SDL_FreeSurface(temp);
    77 
    78     /* We're ready to roll. :) */
    79     return sprite;
    80 }
    81 
    82 void
    83 MoveSprites(SDL_Window * window, SDL_Texture * sprite)
    84 {
    85     int i, n;
    86     int window_w, window_h;
    87     int sprite_w, sprite_h;
    88     SDL_Rect *position, *velocity;
    89 
    90     SDL_SelectRenderer(window);
    91 
    92     /* Query the sizes */
    93     SDL_GetWindowSize(window, &window_w, &window_h);
    94     SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
    95 
    96     /* Move the sprite, bounce at the wall, and draw */
    97     n = 0;
    98     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    99     SDL_RenderClear();
   100     for (i = 0; i < NUM_SPRITES; ++i) {
   101         position = &positions[i];
   102         velocity = &velocities[i];
   103         position->x += velocity->x;
   104         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   105             velocity->x = -velocity->x;
   106             position->x += velocity->x;
   107         }
   108         position->y += velocity->y;
   109         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   110             velocity->y = -velocity->y;
   111             position->y += velocity->y;
   112         }
   113 
   114         /* Blit the sprite onto the screen */
   115         SDL_RenderCopy(sprite, NULL, position);
   116     }
   117 
   118     /* Update the screen! */
   119     SDL_RenderPresent();
   120 }
   121 
   122 int
   123 main(int argc, char *argv[])
   124 {
   125     int i, done;
   126     const char *driver;
   127     SDL_Window *window;
   128     SDL_Texture *sprite;
   129     int window_w, window_h;
   130     int sprite_w, sprite_h;
   131     SDL_Event event;
   132 
   133     if (SDL_VideoInit(NULL, 0) < 0) {
   134         fprintf(stderr, "Couldn't initialize SDL video: %s\n",
   135                 SDL_GetError());
   136         exit(1);
   137     }
   138     driver = SDL_GetCurrentVideoDriver();
   139 
   140     /* Find a native window driver and create a native window */
   141     for (i = 0; factories[i]; ++i) {
   142         if (SDL_strcmp(driver, factories[i]->tag) == 0) {
   143             factory = factories[i];
   144             break;
   145         }
   146     }
   147     if (!factory) {
   148         fprintf(stderr, "Couldn't find native window code for %s driver\n",
   149                 driver);
   150         quit(2);
   151     }
   152     printf("Creating native window for %s driver\n", driver);
   153     native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
   154     if (!native_window) {
   155         fprintf(stderr, "Couldn't create native window\n");
   156         quit(3);
   157     }
   158     window = SDL_CreateWindowFrom(native_window);
   159     if (!window) {
   160         fprintf(stderr, "Couldn't create SDL window: %s\n", SDL_GetError());
   161         quit(4);
   162     }
   163     SDL_SetWindowTitle(window, "SDL Native Window Test");
   164 
   165     /* Create the renderer */
   166     if (SDL_CreateRenderer(window, -1, 0) < 0) {
   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_SelectRenderer(window);
   173     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   174     SDL_RenderClear();
   175 
   176     sprite = LoadSprite(window, "icon.bmp");
   177     if (!sprite) {
   178         quit(6);
   179     }
   180 
   181     /* Allocate memory for the sprite info */
   182     SDL_GetWindowSize(window, &window_w, &window_h);
   183     SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
   184     positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
   185     velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
   186     if (!positions || !velocities) {
   187         fprintf(stderr, "Out of memory!\n");
   188         quit(2);
   189     }
   190     srand(time(NULL));
   191     for (i = 0; i < NUM_SPRITES; ++i) {
   192         positions[i].x = rand() % (window_w - sprite_w);
   193         positions[i].y = rand() % (window_h - sprite_h);
   194         positions[i].w = sprite_w;
   195         positions[i].h = sprite_h;
   196         velocities[i].x = 0;
   197         velocities[i].y = 0;
   198         while (!velocities[i].x && !velocities[i].y) {
   199             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   200             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   201         }
   202     }
   203 
   204     /* Main render loop */
   205     done = 0;
   206     while (!done) {
   207         /* Check for events */
   208         while (SDL_PollEvent(&event)) {
   209             switch (event.type) {
   210             case SDL_WINDOWEVENT:
   211                 switch (event.window.event) {
   212                 case SDL_WINDOWEVENT_EXPOSED:
   213                     SDL_SelectRenderer(event.window.windowID);
   214                     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   215                     SDL_RenderClear();
   216                     break;
   217                 }
   218                 break;
   219             case SDL_QUIT:
   220                 done = 1;
   221                 break;
   222             default:
   223                 break;
   224             }
   225         }
   226         MoveSprites(window, sprite);
   227     }
   228 
   229     quit(0);
   230 }
   231 
   232 /* vi: set ts=4 sw=4 expandtab: */