File: sdl_fullscreen_samecanvassize.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 (87 lines) | stat: -rw-r--r-- 2,543 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright 2014 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 <stdbool.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <assert.h>
#include <emscripten.h>
#include <emscripten/html5.h>

SDL_Surface *screen = 0;

int inFullscreen = 0;

int wasFullscreen = 0;

int finished = 0;

void render() {
  int width, height;
  emscripten_get_canvas_element_size("#canvas", &width, &height);
  SDL_Rect rect = { 0, 0, width, height };
  SDL_FillRect(screen, &rect, 0xff00ffff);
}

void mainloop() {
  assert(!finished);

  render();

  SDL_Event event;
  int isInFullscreen = EM_ASM_INT(return !!(document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement));
  if (isInFullscreen && !wasFullscreen) {
    printf("Successfully transitioned to fullscreen mode!\n");
    wasFullscreen = isInFullscreen;
  }

  if (wasFullscreen && !isInFullscreen) {
    printf("Exited fullscreen. Test succeeded.\n");
    wasFullscreen = isInFullscreen;
    finished = 1;
    emscripten_force_exit(0);
  }

  int haveEvent = SDL_PollEvent(&event);
  if (haveEvent) {
    switch(event.type) {
      case SDL_MOUSEBUTTONDOWN: {
        SDL_WM_ToggleFullScreen(screen);
        inFullscreen = 1 - inFullscreen;
        if (inFullscreen == 0) {
          if (wasFullscreen) {
            printf("Exited fullscreen. Test succeeded.\n");
          } else {
            printf("Exited fullscreen. Test failed, fullscreen transition did not happen!\n");
            assert(false);
          }
          emscripten_force_exit(0);
        } else {
          printf("Entering fullscreen...\n");
        }
        break;
      }
    }
  }
}

int main() {
  int w, h;
  emscripten_get_canvas_element_size("#canvas", &w, &h);
  printf("w:%d,h:%d\n", w,h);

  SDL_Init(SDL_INIT_VIDEO);

  screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE); //set with the same size with canvas. this used to break full screen

  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, and after exiting, the yellow canvas should be restored in the window.\n");
  emscripten_set_main_loop(mainloop, 0, 0);
  return 0;
}