SDL  2.0
testnative.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 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 #include <stdlib.h> /* for srand() */
16 #include <time.h> /* for time() */
17 
18 #include "testnative.h"
19 
20 #define WINDOW_W 640
21 #define WINDOW_H 480
22 #define NUM_SPRITES 100
23 #define MAX_SPEED 1
24 
26 #ifdef TEST_NATIVE_WINDOWS
27  &WindowsWindowFactory,
28 #endif
29 #ifdef TEST_NATIVE_X11
30  &X11WindowFactory,
31 #endif
32 #ifdef TEST_NATIVE_COCOA
33  &CocoaWindowFactory,
34 #endif
35  NULL
36 };
38 static void *native_window;
40 
41 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
42 static void
43 quit(int rc)
44 {
45  SDL_VideoQuit();
46  if (native_window) {
48  }
49  exit(rc);
50 }
51 
54 {
55  SDL_Surface *temp;
57 
58  /* Load the sprite image */
59  temp = SDL_LoadBMP(file);
60  if (temp == NULL) {
61  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
62  return 0;
63  }
64 
65  /* Set transparent pixel as the pixel at (0,0) */
66  if (temp->format->palette) {
67  SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
68  }
69 
70  /* Create textures from the image */
72  if (!sprite) {
73  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
74  SDL_FreeSurface(temp);
75  return 0;
76  }
77  SDL_FreeSurface(temp);
78 
79  /* We're ready to roll. :) */
80  return sprite;
81 }
82 
83 void
85 {
86  int sprite_w, sprite_h;
87  int i;
89  SDL_Rect *position, *velocity;
90 
91  /* Query the sizes */
94 
95  /* Draw a gray background */
96  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
98 
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;
107  }
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;
112  }
113 
114  /* Blit the sprite onto the screen */
115  SDL_RenderCopy(renderer, sprite, NULL, position);
116  }
117 
118  /* Update the screen! */
120 }
121 
122 int
123 main(int argc, char *argv[])
124 {
125  int i, done;
126  const char *driver;
130  int window_w, window_h;
131  int sprite_w, sprite_h;
133 
134  /* Enable standard application logging */
136 
137  if (SDL_VideoInit(NULL) < 0) {
138  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
139  SDL_GetError());
140  exit(1);
141  }
142  driver = SDL_GetCurrentVideoDriver();
143 
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];
148  break;
149  }
150  }
151  if (!factory) {
152  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
153  driver);
154  quit(2);
155  }
156  SDL_Log("Creating native window for %s driver\n", driver);
158  if (!native_window) {
159  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
160  quit(3);
161  }
163  if (!window) {
164  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
165  quit(4);
166  }
167  SDL_SetWindowTitle(window, "SDL Native Window Test");
168 
169  /* Create the renderer */
171  if (!renderer) {
172  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
173  quit(5);
174  }
175 
176  /* Clear the window, load the sprite and go! */
177  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
179 
180  sprite = LoadSprite(renderer, "icon.bmp");
181  if (!sprite) {
182  quit(6);
183  }
184 
185  /* Allocate memory for the sprite info */
190  if (!positions || !velocities) {
191  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
192  quit(2);
193  }
194  srand(time(NULL));
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;
200  velocities[i].x = 0;
201  velocities[i].y = 0;
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;
205  }
206  }
207 
208  /* Main render loop */
209  done = 0;
210  while (!done) {
211  /* Check for events */
212  while (SDL_PollEvent(&event)) {
213  switch (event.type) {
214  case SDL_WINDOWEVENT:
215  switch (event.window.event) {
217  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
219  break;
220  }
221  break;
222  case SDL_QUIT:
223  done = 1;
224  break;
225  default:
226  break;
227  }
228  }
230  }
231 
232  quit(0);
233 
234  return 0; /* to prevent compiler warning */
235 }
236 
237 /* vi: set ts=4 sw=4 expandtab: */
native_window
EGLConfig void * native_window
Definition: eglext.h:790
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
quit
static void quit(int rc)
Definition: testnative.c:43
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
WINDOW_H
#define WINDOW_H
Definition: testnative.c:21
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:70
NativeWindowFactory::DestroyNativeWindow
void(* DestroyNativeWindow)(void *window)
Definition: testnative.h:26
time
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
sprite
static SDL_Texture * sprite
Definition: testspriteminimal.c:29
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
window_w
int window_w
Definition: testoverlay2.c:145
NULL
#define NULL
Definition: begin_code.h:167
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:76
SDL_CreateWindowFrom
#define SDL_CreateWindowFrom
Definition: SDL_dynapi_overrides.h:515
viewport
static SDL_Rect viewport
Definition: testviewport.c:28
SDL_Rect::x
int x
Definition: SDL_rect.h:79
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_Rect::w
int w
Definition: SDL_rect.h:80
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
NativeWindowFactory::CreateNativeWindow
void *(* CreateNativeWindow)(int w, int h)
Definition: testnative.h:25
factory
static NativeWindowFactory * factory
Definition: testnative.c:37
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
SDL_CreateTextureFromSurface
#define SDL_CreateTextureFromSurface
Definition: SDL_dynapi_overrides.h:307
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2652
SDL_Renderer
Definition: SDL_sysrender.h:109
done
int done
Definition: checkkeys.c:28
sprite_w
static int sprite_w
Definition: testsprite2.c:38
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
window_h
int window_h
Definition: testoverlay2.c:146
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_Rect::y
int y
Definition: SDL_rect.h:79
SDL_Rect::h
int h
Definition: SDL_rect.h:80
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
velocities
static SDL_Rect * velocities
Definition: testnative.c:39
SDL_RenderGetViewport
#define SDL_RenderGetViewport
Definition: SDL_dynapi_overrides.h:325
SDL_QueryTexture
#define SDL_QueryTexture
Definition: SDL_dynapi_overrides.h:308
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
sprite_h
static int sprite_h
Definition: testsprite2.c:38
SDL_WINDOWEVENT_EXPOSED
@ SDL_WINDOWEVENT_EXPOSED
Definition: SDL_video.h:150
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
SDL_PixelFormat::palette
SDL_Palette * palette
Definition: SDL_pixels.h:321
SDL_SetColorKey
#define SDL_SetColorKey
Definition: SDL_dynapi_overrides.h:453
SDL_VideoInit
#define SDL_VideoInit
Definition: SDL_dynapi_overrides.h:498
MAX_SPEED
#define MAX_SPEED
Definition: testnative.c:23
NUM_SPRITES
#define NUM_SPRITES
Definition: testnative.c:22
SDL_SetWindowTitle
#define SDL_SetWindowTitle
Definition: SDL_dynapi_overrides.h:519
SDL_LoadBMP
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:201
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_GetCurrentVideoDriver
#define SDL_GetCurrentVideoDriver
Definition: SDL_dynapi_overrides.h:500
testnative.h
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
main
int main(int argc, char *argv[])
Definition: testnative.c:123
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:77
SDL_Texture
Definition: SDL_sysrender.h:36
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
MoveSprites
void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
Definition: testnative.c:84
SDL_VideoQuit
#define SDL_VideoQuit
Definition: SDL_dynapi_overrides.h:499
positions
static SDL_Rect * positions
Definition: testnative.c:39
SDL_Event
General event structure.
Definition: SDL_events.h:558
SDL_WINDOWEVENT
@ SDL_WINDOWEVENT
Definition: SDL_events.h:92
WINDOW_W
#define WINDOW_W
Definition: testnative.c:20
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_CreateRenderer
#define SDL_CreateRenderer
Definition: SDL_dynapi_overrides.h:301
native_window
static void * native_window
Definition: testnative.c:38
SDL_strcmp
#define SDL_strcmp
Definition: SDL_dynapi_overrides.h:417
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:73
LoadSprite
SDL_Texture * LoadSprite(SDL_Renderer *renderer, char *file)
Definition: testnative.c:53
factories
static NativeWindowFactory * factories[]
Definition: testnative.c:25
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
NativeWindowFactory
Definition: testnative.h:22
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179