2 Copyright (C) 1997-2016 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: Create a native window and attach an SDL renderer */
15 #include <stdlib.h> /* for srand() */
16 #include <time.h> /* for time() */
18 #include "testnative.h"
22 #define NUM_SPRITES 100
25 static NativeWindowFactory *factories[] = {
26 #ifdef TEST_NATIVE_WINDOWS
27 &WindowsWindowFactory,
29 #ifdef TEST_NATIVE_X11
32 #ifdef TEST_NATIVE_COCOA
37 static NativeWindowFactory *factory = NULL;
38 static void *native_window;
39 static SDL_Rect *positions, *velocities;
41 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
47 factory->DestroyNativeWindow(native_window);
53 LoadSprite(SDL_Renderer *renderer, char *file)
58 /* Load the sprite image */
59 temp = SDL_LoadBMP(file);
61 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
65 /* Set transparent pixel as the pixel at (0,0) */
66 if (temp->format->palette) {
67 SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
70 /* Create textures from the image */
71 sprite = SDL_CreateTextureFromSurface(renderer, temp);
73 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
74 SDL_FreeSurface(temp);
77 SDL_FreeSurface(temp);
79 /* We're ready to roll. :) */
84 MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
86 int sprite_w, sprite_h;
89 SDL_Rect *position, *velocity;
92 SDL_RenderGetViewport(renderer, &viewport);
93 SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
95 /* Draw a gray background */
96 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
97 SDL_RenderClear(renderer);
99 /* Move the sprite, bounce at the wall, and draw */
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 >= (viewport.w - sprite_w))) {
105 velocity->x = -velocity->x;
106 position->x += velocity->x;
108 position->y += velocity->y;
109 if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
110 velocity->y = -velocity->y;
111 position->y += velocity->y;
114 /* Blit the sprite onto the screen */
115 SDL_RenderCopy(renderer, sprite, NULL, position);
118 /* Update the screen! */
119 SDL_RenderPresent(renderer);
123 main(int argc, char *argv[])
128 SDL_Renderer *renderer;
130 int window_w, window_h;
131 int sprite_w, sprite_h;
134 /* Enable standard application logging */
135 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
137 if (SDL_VideoInit(NULL) < 0) {
138 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
142 driver = SDL_GetCurrentVideoDriver();
144 /* Find a native window driver and create a native window */
145 for (i = 0; factories[i]; ++i) {
146 if (SDL_strcmp(driver, factories[i]->tag) == 0) {
147 factory = factories[i];
152 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
156 SDL_Log("Creating native window for %s driver\n", driver);
157 native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
158 if (!native_window) {
159 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
162 window = SDL_CreateWindowFrom(native_window);
164 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
167 SDL_SetWindowTitle(window, "SDL Native Window Test");
169 /* Create the renderer */
170 renderer = SDL_CreateRenderer(window, -1, 0);
172 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
176 /* Clear the window, load the sprite and go! */
177 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
178 SDL_RenderClear(renderer);
180 sprite = LoadSprite(renderer, "icon.bmp");
185 /* Allocate memory for the sprite info */
186 SDL_GetWindowSize(window, &window_w, &window_h);
187 SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
188 positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
189 velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
190 if (!positions || !velocities) {
191 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
195 for (i = 0; i < NUM_SPRITES; ++i) {
196 positions[i].x = rand() % (window_w - sprite_w);
197 positions[i].y = rand() % (window_h - sprite_h);
198 positions[i].w = sprite_w;
199 positions[i].h = sprite_h;
202 while (!velocities[i].x && !velocities[i].y) {
203 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
204 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
208 /* Main render loop */
211 /* Check for events */
212 while (SDL_PollEvent(&event)) {
213 switch (event.type) {
214 case SDL_WINDOWEVENT:
215 switch (event.window.event) {
216 case SDL_WINDOWEVENT_EXPOSED:
217 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
218 SDL_RenderClear(renderer);
229 MoveSprites(renderer, sprite);
234 return 0; /* to prevent compiler warning */
237 /* vi: set ts=4 sw=4 expandtab: */