forked from jno6809/jc_reborn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.c
121 lines (95 loc) · 2.99 KB
/
events.c
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
/*
* This file is part of 'Johnny Reborn'
*
* An open-source engine for the classic
* 'Johnny Castaway' screensaver by Sierra.
*
* Copyright (C) 2019 Jeremie GUILLAUME
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include "mytypes.h"
#include "graphics.h"
#include "events.h"
static uint32 lastTicks = 0x00ffffff;
static int paused = 0;
static int maxSpeed = 0;
static int oneFrame = 0;
int evHotKeysEnabled = 0;
static void eventsProcessEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
if (evHotKeysEnabled) {
switch (event.key.keysym.sym) {
case SDLK_SPACE:
paused = !paused;
break;
case SDLK_m:
maxSpeed = !maxSpeed;
break;
case SDLK_RETURN:
if (event.key.keysym.mod & KMOD_LALT) {
grToggleFullScreen();
oneFrame = 1; // to redraw the window // TODO
}
else {
oneFrame = 1;
}
break;
case SDLK_ESCAPE:
graphicsEnd();
exit(255);
break;
}
}
else {
// Normal behaviour : no hot keys, the screen saver
// terminates if any key is pressed
graphicsEnd();
exit(255);
}
break;
case SDL_WINDOWEVENT:
grRefreshDisplay();
break;
case SDL_QUIT:
graphicsEnd();
exit(255);
break;
}
}
}
void eventsInit()
{
lastTicks = SDL_GetTicks();
}
void eventsWaitTick(uint16 delay)
{
delay *= 20;
oneFrame = 0;
eventsProcessEvents();
while ((paused && !oneFrame)
|| (!maxSpeed && (SDL_GetTicks() - lastTicks < delay))) {
SDL_Delay(5);
eventsProcessEvents();
}
lastTicks = SDL_GetTicks();
}