test/testwm2.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
parent 8149 681eb46b8ac4
child 9607 7746ab058d12
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

This port was due to the efforts of several people, including: Charlie Birks,
Sathyanarayanan Gunasekaran, Jukka Jylänki, Alon Zakai, Edward Rudd,
Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
     1 /*
     2   Copyright (C) 1997-2014 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 
    22 static SDLTest_CommonState *state;
    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;
    40 SDL_Cursor *cursor = NULL;
    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
    51 loop()
    52 {
    53     SDL_Event event;
    54         /* Check for events */
    55         while (SDL_PollEvent(&event)) {
    56             SDLTest_CommonEvent(state, &event, &done);
    57 
    58             if (event.type == SDL_WINDOWEVENT) {
    59                 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
    60                     SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
    61                     if (window) {
    62                         SDL_Log("Window %d resized to %dx%d\n",
    63                             event.window.windowID,
    64                             event.window.data1,
    65                             event.window.data2);
    66                     }
    67                 }
    68                 if (event.window.event == SDL_WINDOWEVENT_MOVED) {
    69                     SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
    70                     if (window) {
    71                         SDL_Log("Window %d moved to %d,%d (display %s)\n",
    72                             event.window.windowID,
    73                             event.window.data1,
    74                             event.window.data2,
    75                             SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
    76                     }
    77                 }
    78             }
    79             if (event.type == SDL_KEYUP) {
    80                 SDL_bool updateCursor = SDL_FALSE;
    81 
    82                 if (event.key.keysym.sym == SDLK_LEFT) {
    83                     --system_cursor;
    84                     if (system_cursor < 0) {
    85                         system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
    86                     }
    87                     updateCursor = SDL_TRUE;
    88                 } else if (event.key.keysym.sym == SDLK_RIGHT) {
    89                     ++system_cursor;
    90                     if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
    91                         system_cursor = 0;
    92                     }
    93                     updateCursor = SDL_TRUE;
    94                 }
    95                 if (updateCursor) {
    96                     SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
    97                     SDL_FreeCursor(cursor);
    98                     cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
    99                     SDL_SetCursor(cursor);
   100                 }
   101             }
   102         }
   103 }
   104 
   105 int
   106 main(int argc, char *argv[])
   107 {
   108     int i;
   109 
   110 	/* Enable standard application logging */
   111     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   112 
   113     SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
   114 
   115     /* Initialize test framework */
   116     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   117     if (!state) {
   118         return 1;
   119     }
   120     state->skip_renderer = SDL_TRUE;
   121     for (i = 1; i < argc;) {
   122         int consumed;
   123 
   124         consumed = SDLTest_CommonArg(state, i);
   125         if (consumed == 0) {
   126             consumed = -1;
   127         }
   128         if (consumed < 0) {
   129             SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   130             quit(1);
   131         }
   132         i += consumed;
   133     }
   134     if (!SDLTest_CommonInit(state)) {
   135         quit(2);
   136     }
   137 
   138     /* Main render loop */
   139     done = 0;
   140 #ifdef __EMSCRIPTEN__
   141     emscripten_set_main_loop(loop, 0, 1);
   142 #else
   143     while (!done) {
   144         loop();
   145     }
   146 #endif
   147     SDL_FreeCursor(cursor);
   148 
   149     quit(0);
   150     /* keep the compiler happy ... */
   151     return(0);
   152 }
   153 
   154 /* vi: set ts=4 sw=4 expandtab: */