Add surface test suite; minor improvements to render suite; refactor image saving into test lib compare function; fix for Haiku build
2 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
12 /* Simple program: Move N sprites around on the screen as fast as possible */
21 #define WINDOW_WIDTH 256
22 #define WINDOW_HEIGHT (2*192)
24 #define WINDOW_WIDTH 640
25 #define WINDOW_HEIGHT 480
27 #define NUM_SPRITES 100
30 static SDL_Texture *sprite;
31 static SDL_Rect positions[NUM_SPRITES];
32 static SDL_Rect velocities[NUM_SPRITES];
33 static int sprite_w, sprite_h;
35 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
43 LoadSprite(char *file, SDL_Renderer *renderer)
47 /* Load the sprite image */
48 temp = SDL_LoadBMP(file);
50 fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
56 /* Set transparent pixel as the pixel at (0,0) */
57 if (temp->format->palette) {
58 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
60 switch (temp->format->BitsPerPixel) {
62 SDL_SetColorKey(temp, SDL_TRUE,
63 (*(Uint16 *) temp->pixels) & 0x00007FFF);
66 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
69 SDL_SetColorKey(temp, SDL_TRUE,
70 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
73 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
78 /* Create textures from the image */
79 sprite = SDL_CreateTextureFromSurface(renderer, temp);
81 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
82 SDL_FreeSurface(temp);
85 SDL_FreeSurface(temp);
87 /* We're ready to roll. :) */
92 MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
95 int window_w = WINDOW_WIDTH;
96 int window_h = WINDOW_HEIGHT;
97 SDL_Rect *position, *velocity;
99 /* Draw a gray background */
100 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
101 SDL_RenderClear(renderer);
103 /* Move the sprite, bounce at the wall, and draw */
104 for (i = 0; i < NUM_SPRITES; ++i) {
105 position = &positions[i];
106 velocity = &velocities[i];
107 position->x += velocity->x;
108 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
109 velocity->x = -velocity->x;
110 position->x += velocity->x;
112 position->y += velocity->y;
113 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
114 velocity->y = -velocity->y;
115 position->y += velocity->y;
118 /* Blit the sprite onto the screen */
119 SDL_RenderCopy(renderer, sprite, NULL, position);
122 /* Update the screen! */
123 SDL_RenderPresent(renderer);
127 main(int argc, char *argv[])
130 SDL_Renderer *renderer;
134 if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
138 if (LoadSprite("icon.bmp", renderer) < 0) {
142 /* Initialize the sprite positions */
144 for (i = 0; i < NUM_SPRITES; ++i) {
145 positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
146 positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
147 positions[i].w = sprite_w;
148 positions[i].h = sprite_h;
151 while (!velocities[i].x && !velocities[i].y) {
152 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
153 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
157 /* Main render loop */
160 /* Check for events */
161 while (SDL_PollEvent(&event)) {
162 if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
166 MoveSprites(window, renderer, sprite);
172 /* vi: set ts=4 sw=4 expandtab: */