File: sdl_togglefullscreen.c

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (150 lines) | stat: -rw-r--r-- 4,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright 2015 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <stdio.h>
#include <SDL.h>
#include <emscripten.h>
#include <emscripten/html5.h>

static enum {
    STATE_INITIAL,      /* Initial state, click needed to enter full screen */
    STATE_FS_REQ,       /* After click, expecting to enter full screen */
    STATE_FS,           /* Should remain in full screen */
    STATE_FS_QUIT_REQ,  /* Second click, expecting to leave full screen */
    STATE_FS_QUIT,      /* Left full screen */
    STATE_SUCCESS,      /* Reported success, test finished */
    STATE_ERROR         /* Something went wrong, and an error was reported */
} state = STATE_INITIAL;

int result = 0;

#if SDL_VERSION_ATLEAST(2,0,0)
SDL_Window *window = 0;
SDL_Renderer *renderer = 0;
#else
SDL_Surface *screen = 0;
#endif

static void fail(const char *msg) {
  printf("%s Test failed.\n", msg);
  state = STATE_ERROR;
  result = 0;
}

static EM_BOOL mouseup(int eventType,
                     const EmscriptenMouseEvent *keyEvent, void *userData) {
  if (eventType == EMSCRIPTEN_EVENT_MOUSEUP) {
    switch (state) {
    case STATE_INITIAL:
#if SDL_VERSION_ATLEAST(2,0,0)
      SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
#else
      SDL_WM_ToggleFullScreen(screen);
#endif
      state = STATE_FS_REQ;
      break;
    case STATE_FS:
#if SDL_VERSION_ATLEAST(2,0,0)
      SDL_SetWindowFullscreen(window, 0);
#else
      SDL_WM_ToggleFullScreen(screen);
#endif
      state = STATE_FS_QUIT_REQ;
      break;
    case STATE_FS_QUIT:
    case STATE_SUCCESS:
    case STATE_ERROR:
      break;
    default:
      fail("Unexpected click.");
      break;
    }
  }

  return 0;
}

static void render() {
#if SDL_VERSION_ATLEAST(2,0,0)
  SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
  SDL_RenderClear(renderer);
  SDL_RenderPresent(renderer);
#else
  int width, height;
  emscripten_get_canvas_element_size("#canvas", &width, &height);
  SDL_Rect rect = { 0, 0, width, height };
  SDL_FillRect(screen, &rect, 0xff00ffff);
#endif
}

static void mainloop() {
  render();
  int isInFullscreen = EM_ASM_INT(return !!(document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement));

  switch (state) {
  case STATE_INITIAL:
    if (isInFullscreen) fail("Unexpected full screen.");
    break;
  case STATE_FS_REQ:
    if (isInFullscreen) {
      state = STATE_FS;
      printf("Successfully transitioned to fullscreen mode.\n");
    }
    break;
  case STATE_FS:
    if (!isInFullscreen) fail("Unexpectedly left full screen.");
    break;
  case STATE_FS_QUIT_REQ:
    if (!isInFullscreen) {
      state = STATE_FS_QUIT;
      /* One more render() call is needed to make sure canvas is yellow,
       * so don't quit yet.
       */
    }
    break;
  case STATE_FS_QUIT:
    if (isInFullscreen) fail("Unexpected full screen.");
    state = STATE_SUCCESS;
    printf("Exited fullscreen. Test succeeded.\n");
    result = 1;
    break;
  case STATE_ERROR:
  case STATE_SUCCESS:
#ifdef REPORT_RESULT
    {
      REPORT_RESULT(result);
    }
#endif
    emscripten_cancel_main_loop();
    break;
  }
}

int main() {
  SDL_Init(SDL_INIT_VIDEO);
#if SDL_VERSION_ATLEAST(2,0,0)
  window = SDL_CreateWindow(NULL, 0, 0, 600, 450, SDL_WINDOW_SHOWN);
  renderer = SDL_CreateRenderer(window, -1, 0);
#else
  screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
#endif

  /* SDL 1 limits which events can be used here.
   * Click and mouseup don't work.
   */
  if (emscripten_set_mouseup_callback("#canvas", NULL, 1, mouseup) != EMSCRIPTEN_RESULT_SUCCESS) {
    printf("Couldn't to set mouse callback. Test failed.\n");
    return 1;
  }

  printf("You should see a yellow canvas.\n");
  printf("Click on the canvas to enter full screen, and then click on the canvas again to finish the test.\n");
  printf("When in full screen, you should see the whole screen filled yellow.\n");
  printf("After exiting, the yellow canvas should be restored in the window.\n");
  emscripten_set_main_loop(mainloop, 0, 0);
  return 0;
}