testwm2.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#include <stdlib.h>
#include <stdio.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "SDL_test_common.h"
static SDLTest_CommonState *state;
int done;
static const char *cursorNames[] = {
"arrow",
"ibeam",
"wait",
"crosshair",
"waitarrow",
"sizeNWSE",
"sizeNESW",
"sizeWE",
"sizeNS",
"sizeALL",
"NO",
"hand",
};
int system_cursor = -1;
SDL_Cursor *cursor = NULL;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDLTest_CommonQuit(state);
exit(rc);
}
void
loop()
{
int i;
SDL_Event event;
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
if (window) {
SDL_Log("Window %d resized to %dx%d\n",
event.window.windowID,
event.window.data1,
event.window.data2);
}
}
if (event.window.event == SDL_WINDOWEVENT_MOVED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
if (window) {
SDL_Log("Window %d moved to %d,%d (display %s)\n",
event.window.windowID,
event.window.data1,
event.window.data2,
SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
}
}
}
if (event.type == SDL_KEYUP) {
SDL_bool updateCursor = SDL_FALSE;
if (event.key.keysym.sym == SDLK_LEFT) {
--system_cursor;
if (system_cursor < 0) {
system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
}
updateCursor = SDL_TRUE;
} else if (event.key.keysym.sym == SDLK_RIGHT) {
++system_cursor;
if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
system_cursor = 0;
}
updateCursor = SDL_TRUE;
}
if (updateCursor) {
SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
SDL_FreeCursor(cursor);
cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
SDL_SetCursor(cursor);
}
}
}
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int
main(int argc, char *argv[])
{
int i;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 1;
}
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
SDL_EventState(SDL_DROPTEXT, SDL_ENABLE);
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
/* Main render loop */
done = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
loop();
}
#endif
SDL_FreeCursor(cursor);
quit(0);
/* keep the compiler happy ... */
return(0);
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_GetWindowDisplayIndex
#define SDL_GetWindowDisplayIndex
Definition: SDL_dynapi_overrides.h:510
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_NUM_SYSTEM_CURSORS
@ SDL_NUM_SYSTEM_CURSORS
Definition: SDL_mouse.h:60
SDL_Cursor
Definition: SDL_mouse_c.h:30
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
NULL
#define NULL
Definition: begin_code.h:167
SDL_KEYUP
@ SDL_KEYUP
Definition: SDL_events.h:97
SDLTest_CommonState
Definition: SDL_test_common.h:51
SDL_GetDisplayName
#define SDL_GetDisplayName
Definition: SDL_dynapi_overrides.h:502
cursorNames
static const char * cursorNames[]
Definition: testwm2.c:25
SDL_WINDOWEVENT_RESIZED
@ SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:155
SDL_CreateSystemCursor
#define SDL_CreateSystemCursor
Definition: SDL_dynapi_overrides.h:253
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:759
SDL_WINDOWEVENT_MOVED
@ SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:153
SDL_SetCursor
#define SDL_SetCursor
Definition: SDL_dynapi_overrides.h:254
SDLK_LEFT
@ SDLK_LEFT
Definition: SDL_keycode.h:152
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
state
static SDLTest_CommonState * state
Definition: testwm2.c:22
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:59
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1832
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_Renderer
Definition: SDL_sysrender.h:122
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDLTest_CommonDefaultArgs
SDL_bool SDLTest_CommonDefaultArgs(SDLTest_CommonState *state, const int argc, char **argv)
Easy argument handling when test app doesn't need any custom args.
Definition: SDL_test_common.c:516
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_test_common.h
SDL_DROPTEXT
@ SDL_DROPTEXT
Definition: SDL_events.h:142
loop
void loop()
Definition: testwm2.c:51
SDL_GetWindowFromID
#define SDL_GetWindowFromID
Definition: SDL_dynapi_overrides.h:517
SDL_SystemCursor
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor().
Definition: SDL_mouse.h:46
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
cursor
SDL_Cursor * cursor
Definition: testwm2.c:40
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:764
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
main
int main(int argc, char *argv[])
Definition: testwm2.c:118
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
done
int done
Definition: testwm2.c:23
quit
static void quit(int rc)
Definition: testwm2.c:44
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
system_cursor
int system_cursor
Definition: testwm2.c:39
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
SDL_EventState
#define SDL_EventState
Definition: SDL_dynapi_overrides.h:131
SDL_Event
General event structure.
Definition: SDL_events.h:557
SDL_WINDOWEVENT
@ SDL_WINDOWEVENT
Definition: SDL_events.h:92
SDLK_RIGHT
@ SDLK_RIGHT
Definition: SDL_keycode.h:151
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1501
SDL_FreeCursor
#define SDL_FreeCursor
Definition: SDL_dynapi_overrides.h:257
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
SDL_DROPFILE
@ SDL_DROPFILE
Definition: SDL_events.h:141
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161