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 #ifdef TEST_NATIVE_OS2
36  &OS2WindowFactory,
37 #endif
38  NULL
39 };
41 static void *native_window;
43 
44 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
45 static void
46 quit(int rc)
47 {
48  SDL_VideoQuit();
49  if (native_window) {
51  }
52  exit(rc);
53 }
54 
57 {
58  SDL_Surface *temp;
60 
61  /* Load the sprite image */
62  temp = SDL_LoadBMP(file);
63  if (temp == NULL) {
64  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
65  return 0;
66  }
67 
68  /* Set transparent pixel as the pixel at (0,0) */
69  if (temp->format->palette) {
70  SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
71  }
72 
73  /* Create textures from the image */
75  if (!sprite) {
76  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
77  SDL_FreeSurface(temp);
78  return 0;
79  }
80  SDL_FreeSurface(temp);
81 
82  /* We're ready to roll. :) */
83  return sprite;
84 }
85 
86 void
88 {
89  int sprite_w, sprite_h;
90  int i;
92  SDL_Rect *position, *velocity;
93 
94  /* Query the sizes */
97 
98  /* Draw a gray background */
99  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
101 
102  /* Move the sprite, bounce at the wall, and draw */
103  for (i = 0; i < NUM_SPRITES; ++i) {
104  position = &positions[i];
105  velocity = &velocities[i];
106  position->x += velocity->x;
107  if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
108  velocity->x = -velocity->x;
109  position->x += velocity->x;
110  }
111  position->y += velocity->y;
112  if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
113  velocity->y = -velocity->y;
114  position->y += velocity->y;
115  }
116 
117  /* Blit the sprite onto the screen */
118  SDL_RenderCopy(renderer, sprite, NULL, position);
119  }
120 
121  /* Update the screen! */
123 }
124 
125 int
126 main(int argc, char *argv[])
127 {
128  int i, done;
129  const char *driver;
133  int window_w, window_h;
134  int sprite_w, sprite_h;
136 
137  /* Enable standard application logging */
139 
140  if (SDL_VideoInit(NULL) < 0) {
141  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
142  SDL_GetError());
143  exit(1);
144  }
145  driver = SDL_GetCurrentVideoDriver();
146 
147  /* Find a native window driver and create a native window */
148  for (i = 0; factories[i]; ++i) {
149  if (SDL_strcmp(driver, factories[i]->tag) == 0) {
150  factory = factories[i];
151  break;
152  }
153  }
154  if (!factory) {
155  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
156  driver);
157  quit(2);
158  }
159  SDL_Log("Creating native window for %s driver\n", driver);
161  if (!native_window) {
162  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
163  quit(3);
164  }
166  if (!window) {
167  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
168  quit(4);
169  }
170  SDL_SetWindowTitle(window, "SDL Native Window Test");
171 
172  /* Create the renderer */
174  if (!renderer) {
175  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
176  quit(5);
177  }
178 
179  /* Clear the window, load the sprite and go! */
180  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
182 
183  sprite = LoadSprite(renderer, "icon.bmp");
184  if (!sprite) {
185  quit(6);
186  }
187 
188  /* Allocate memory for the sprite info */
193  if (!positions || !velocities) {
194  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
195  quit(2);
196  }
197  srand(time(NULL));
198  for (i = 0; i < NUM_SPRITES; ++i) {
199  positions[i].x = rand() % (window_w - sprite_w);
200  positions[i].y = rand() % (window_h - sprite_h);
201  positions[i].w = sprite_w;
202  positions[i].h = sprite_h;
203  velocities[i].x = 0;
204  velocities[i].y = 0;
205  while (!velocities[i].x && !velocities[i].y) {
206  velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
207  velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
208  }
209  }
210 
211  /* Main render loop */
212  done = 0;
213  while (!done) {
214  /* Check for events */
215  while (SDL_PollEvent(&event)) {
216  switch (event.type) {
217  case SDL_WINDOWEVENT:
218  switch (event.window.event) {
220  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
222  break;
223  }
224  break;
225  case SDL_QUIT:
226  done = 1;
227  break;
228  default:
229  break;
230  }
231  }
233  }
234 
235  quit(0);
236 
237  return 0; /* to prevent compiler warning */
238 }
239 
240 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_QueryTexture
#define SDL_RenderPresent
#define SDL_SetColorKey
#define SDL_GetWindowSize
#define SDL_RenderGetViewport
#define SDL_GetError
#define SDL_CreateWindowFrom
#define SDL_GetCurrentVideoDriver
#define SDL_PollEvent
#define SDL_SetRenderDrawColor
#define SDL_CreateTextureFromSurface
#define SDL_VideoInit
#define SDL_CreateRenderer
#define SDL_malloc
#define SDL_LogSetPriority
#define SDL_RenderClear
#define SDL_LogError
#define SDL_VideoQuit
#define SDL_strcmp
#define SDL_RenderCopy
#define SDL_FreeSurface
#define SDL_Log
#define SDL_SetWindowTitle
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_WINDOWEVENT
Definition: SDL_events.h:94
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
struct _cl_event * event
uint8_t Uint8
Definition: SDL_stdinc.h:185
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:203
@ SDL_WINDOWEVENT_EXPOSED
Definition: SDL_video.h:151
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
#define NULL
Definition: begin_code.h:163
int done
Definition: checkkeys.c:28
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
EGLConfig void * native_window
Definition: eglext.h:790
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
void *(* CreateNativeWindow)(int w, int h)
Definition: testnative.h:25
void(* DestroyNativeWindow)(void *window)
Definition: testnative.h:26
SDL_Palette * palette
Definition: SDL_pixels.h:327
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:73
void * pixels
Definition: SDL_surface.h:76
The type used to identify a window.
Definition: SDL_sysvideo.h:75
static SDL_Renderer * renderer
int main(int argc, char *argv[])
Definition: testnative.c:126
static void quit(int rc)
Definition: testnative.c:46
static void * native_window
Definition: testnative.c:41
static SDL_Rect * velocities
Definition: testnative.c:42
#define WINDOW_W
Definition: testnative.c:20
SDL_Texture * LoadSprite(SDL_Renderer *renderer, char *file)
Definition: testnative.c:56
#define NUM_SPRITES
Definition: testnative.c:22
static SDL_Rect * positions
Definition: testnative.c:42
#define MAX_SPEED
Definition: testnative.c:23
static NativeWindowFactory * factories[]
Definition: testnative.c:25
#define WINDOW_H
Definition: testnative.c:21
void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
Definition: testnative.c:87
static NativeWindowFactory * factory
Definition: testnative.c:40
int window_w
Definition: testoverlay2.c:145
int window_h
Definition: testoverlay2.c:146
static int sprite_w
Definition: testsprite2.c:38
static int sprite_h
Definition: testsprite2.c:38
static SDL_Texture * sprite
static SDL_Rect viewport
Definition: testviewport.c:28
General event structure.
Definition: SDL_events.h:592