SDL  2.0
testwm2.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2019 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 
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 #ifdef __EMSCRIPTEN__
17 #include <emscripten/emscripten.h>
18 #endif
19 
20 #include "SDL_test_common.h"
21 
23 int done;
24 
25 static const char *cursorNames[] = {
26  "arrow",
27  "ibeam",
28  "wait",
29  "crosshair",
30  "waitarrow",
31  "sizeNWSE",
32  "sizeNESW",
33  "sizeWE",
34  "sizeNS",
35  "sizeALL",
36  "NO",
37  "hand",
38 };
39 int system_cursor = -1;
41 
42 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
43 static void
44 quit(int rc)
45 {
46  SDLTest_CommonQuit(state);
47  exit(rc);
48 }
49 
50 void
52 {
53  int i;
55  /* Check for events */
56  while (SDL_PollEvent(&event)) {
57  SDLTest_CommonEvent(state, &event, &done);
58 
59  if (event.type == SDL_WINDOWEVENT) {
60  if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
62  if (window) {
63  SDL_Log("Window %d resized to %dx%d\n",
64  event.window.windowID,
65  event.window.data1,
66  event.window.data2);
67  }
68  }
69  if (event.window.event == SDL_WINDOWEVENT_MOVED) {
71  if (window) {
72  SDL_Log("Window %d moved to %d,%d (display %s)\n",
73  event.window.windowID,
74  event.window.data1,
75  event.window.data2,
77  }
78  }
79  }
80  if (event.type == SDL_KEYUP) {
81  SDL_bool updateCursor = SDL_FALSE;
82 
83  if (event.key.keysym.sym == SDLK_LEFT) {
84  --system_cursor;
85  if (system_cursor < 0) {
87  }
88  updateCursor = SDL_TRUE;
89  } else if (event.key.keysym.sym == SDLK_RIGHT) {
90  ++system_cursor;
92  system_cursor = 0;
93  }
94  updateCursor = SDL_TRUE;
95  }
96  if (updateCursor) {
97  SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
98  SDL_FreeCursor(cursor);
99  cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
100  SDL_SetCursor(cursor);
101  }
102  }
103  }
104 
105  for (i = 0; i < state->num_windows; ++i) {
106  SDL_Renderer *renderer = state->renderers[i];
107  SDL_RenderClear(renderer);
108  SDL_RenderPresent(renderer);
109  }
110 #ifdef __EMSCRIPTEN__
111  if (done) {
112  emscripten_cancel_main_loop();
113  }
114 #endif
115 }
116 
117 int
118 main(int argc, char *argv[])
119 {
120  int i;
121 
122  /* Enable standard application logging */
124 
126 
127  /* Initialize test framework */
129  if (!state) {
130  return 1;
131  }
132 
133  if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
134  SDLTest_CommonQuit(state);
135  return 1;
136  }
137 
140 
141  for (i = 0; i < state->num_windows; ++i) {
142  SDL_Renderer *renderer = state->renderers[i];
143  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
144  SDL_RenderClear(renderer);
145  }
146 
147  /* Main render loop */
148  done = 0;
149 #ifdef __EMSCRIPTEN__
150  emscripten_set_main_loop(loop, 0, 1);
151 #else
152  while (!done) {
153  loop();
154  }
155 #endif
156  SDL_FreeCursor(cursor);
157 
158  quit(0);
159  /* keep the compiler happy ... */
160  return(0);
161 }
162 
163 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_PollEvent
#define SDL_GetWindowDisplayIndex
SDL_bool SDLTest_CommonDefaultArgs(SDLTest_CommonState *state, const int argc, char **argv)
Easy argument handling when test app doesn&#39;t need any custom args.
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
#define SDL_CreateSystemCursor
#define SDL_GetDisplayName
#define SDL_ENABLE
Definition: SDL_events.h:759
static const char * cursorNames[]
Definition: testwm2.c:25
#define SDL_SetCursor
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
static SDLTest_CommonState * state
Definition: testwm2.c:22
SDL_WindowEvent window
Definition: SDL_events.h:562
#define SDL_Log
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
static SDL_Renderer * renderer
struct _cl_event * event
void loop()
Definition: testwm2.c:51
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor().
Definition: SDL_mouse.h:46
SDL_Keysym keysym
Definition: SDL_events.h:220
SDL_Cursor * cursor
Definition: testwm2.c:40
#define SDL_GetWindowFromID
SDL_Renderer ** renderers
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 SDL_assert(condition)
Definition: SDL_assert.h:169
int main(int argc, char *argv[])
Definition: testwm2.c:118
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:167
SDL_bool
Definition: SDL_stdinc.h:161
int done
Definition: testwm2.c:23
#define SDL_RenderClear
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_KeyboardEvent key
Definition: SDL_events.h:563
static void quit(int rc)
Definition: testwm2.c:44
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_EventState
SDL_Keycode sym
Definition: SDL_keyboard.h:50
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
General event structure.
Definition: SDL_events.h:557
#define SDL_SetRenderDrawColor
int system_cursor
Definition: testwm2.c:39
#define SDL_FreeCursor
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:559