source: buchla-emu/emu/sdl.c@ bdd5a63

Last change on this file since bdd5a63 was bdd5a63, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Separate thread for rendering.

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 * Copyright (C) 2017 The Contributors
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or (at
7 * your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * A copy of the GNU General Public License can be found in the file
15 * "gpl.txt" in the top directory of this repository.
16 */
17
18#include <all.h>
19
20int32_t sdl_verbose = false;
21
22#define ver(...) _ver(sdl_verbose, 0, __VA_ARGS__)
23#define ver2(...) _ver(sdl_verbose, 1, __VA_ARGS__)
24#define ver3(...) _ver(sdl_verbose, 2, __VA_ARGS__)
25
26void sdl_init(void)
27{
28 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) < 0) {
29 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
30 exit(1);
31 }
32
33 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_VERBOSE);
34
35 if (SDLNet_Init() < 0) {
36 fail("SDLNet_Init() failed: %s", SDLNet_GetError());
37 }
38
39 if (TTF_Init() < 0) {
40 fail("TTF_Init() failed: %s", TTF_GetError());
41 }
42
43 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
44 SDL_StartTextInput();
45}
46
47void sdl_quit(void)
48{
49 TTF_Quit();
50 SDLNet_Quit();
51 SDL_Quit();
52}
53
54void sdl_loop(void)
55{
56 inf("entering SDL loop");
57
58#if defined EMU_LINUX
59 SDL_Scancode down = SDL_SCANCODE_UNKNOWN;
60#endif
61
62 while (SDL_AtomicGet(&run) != 0) {
63 ser_sdl();
64
65 SDL_Event ev;
66
67 while (SDL_PollEvent(&ev) > 0) {
68#if defined EMU_LINUX
69 // Work around duplicate key-down events on Linux.
70
71 if (ev.type == SDL_KEYDOWN) {
72 if (down == ev.key.keysym.scancode) {
73 continue;
74 }
75
76 down = ev.key.keysym.scancode;
77 }
78 else if (ev.type == SDL_KEYUP) {
79 down = SDL_SCANCODE_UNKNOWN;
80 }
81#endif
82
83 if (ev.type == SDL_QUIT ||
84 (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE)) {
85 ver("quit");
86 SDL_AtomicSet(&run, 0);
87 continue;
88 }
89
90 if (ev.type == SDL_TEXTINPUT) {
91 ser_text(&ev.text);
92 continue;
93 }
94
95 if (ev.type == SDL_KEYDOWN) {
96 ser_key(&ev.key);
97 continue;
98 }
99
100 SDL_Delay(50);
101 }
102 }
103
104 inf("leaving SDL loop");
105}
Note: See TracBrowser for help on using the repository browser.