-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.cpp
120 lines (111 loc) · 2.52 KB
/
window.cpp
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
#include <iostream>
#include "window.h"
#include "def.h"
#include "sdlutils.h"
#define KEYHOLD_TIMER_FIRST 6
#define KEYHOLD_TIMER 2
CWindow::CWindow(void):
m_timer(0),
m_lastPressed(SDLK_0),
m_retVal(0)
{
// Add window to the lists for render
Globals::g_windows.push_back(this);
}
CWindow::~CWindow(void)
{
// Remove last window
Globals::g_windows.pop_back();
}
const int CWindow::execute(void)
{
m_retVal = 0;
Uint32 l_time(0);
SDL_Event l_event;
bool l_loop(true);
bool l_render(true);
// Main loop
while (l_loop)
{
l_time = SDL_GetTicks();
// Handle key press
while (SDL_PollEvent(&l_event))
{
if (l_event.type == SDL_KEYDOWN)
{
l_render = this->keyPress(l_event);
if (m_retVal)
l_loop = false;
}
}
// Handle key hold
if (l_loop)
l_render = this->keyHold() || l_render;
// Render if necessary
if (l_render && l_loop)
{
SDL_utils::renderAll();
// Flip twice to avoid graphical glitch on Dingoo
SDL_Flip(Globals::g_screen);
SDL_Flip(Globals::g_screen);
l_render = false;
INHIBIT(std::cout << "Render time: " << SDL_GetTicks() - l_time << "ms"<< std::endl;)
}
// Cap the framerate
l_time = MS_PER_FRAME - (SDL_GetTicks() - l_time);
if (l_time <= MS_PER_FRAME) SDL_Delay(l_time);
}
return m_retVal;
}
const bool CWindow::keyPress(const SDL_Event &p_event)
{
// Reset timer if running
if (m_timer)
m_timer = 0;
m_lastPressed = p_event.key.keysym.sym;
return false;
}
const bool CWindow::keyHold(void)
{
// Default behavior
return false;
}
const bool CWindow::tick(const Uint8 p_held)
{
bool l_ret(false);
if (p_held)
{
if (m_timer)
{
--m_timer;
if (!m_timer)
{
// Trigger!
l_ret = true;
// Timer continues
m_timer = KEYHOLD_TIMER;
}
}
else
{
// Start timer
m_timer = KEYHOLD_TIMER_FIRST;
}
}
else
{
// Stop timer if running
if (m_timer)
m_timer = 0;
}
return l_ret;
}
const int CWindow::getReturnValue(void) const
{
return m_retVal;
}
const bool CWindow::isFullScreen(void) const
{
// Default behavior
return false;
}