-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDL_Framework.cpp
More file actions
175 lines (154 loc) · 5.1 KB
/
Copy pathSDL_Framework.cpp
File metadata and controls
175 lines (154 loc) · 5.1 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "SDL_Framework.h"
using namespace std;
bool SDL_Framework::Init(const char* title, int x, int y, int width, int height, int flags)
{
int result = SDL_Init(SDL_INIT_EVERYTHING);
if (result != 0)
{
std::cout << "SDL_Init failed, result: " << result << ".\n";
std::getchar();
return false;
}
window_title_ = title;
window_ = SDL_CreateWindow(title, x, y, width, height, flags);
if (window_ == 0) {
std::cout << "SDL_CreateWindow failed.\n";
std::getchar();
return false;
}
SDL_GetWindowSize(window_, &window_width_, &window_height_);
renderer_ = SDL_CreateRenderer(window_, -1, 0);
if (renderer_ == 0) {
std::cout << "SDL_CreateRenderer failed.\n";
std::getchar();
return false;
}
is_running_ = UserInit();
return is_running_;
}
bool SDL_Framework::Init()
{
int result = SDL_Init(SDL_INIT_EVERYTHING);
if (result != 0) {
std::cout << "SDL_Init failed, result: " << result << ".\n";
std::getchar();
return false;
}
window_ = SDL_CreateWindow(window_title_, window_x_, window_y_, window_width_, window_height_, window_flags_);
if (window_ == 0) {
std::cout << "SDL_CreateWindow failed.\n";
std::getchar();
return false;
}
SDL_GetWindowSize(window_, &window_width_, &window_height_);
renderer_ = SDL_CreateRenderer(window_, -1, 0);
if (renderer_ == 0) {
std::cout << "SDL_CreateRenderer failed.\n";
std::getchar();
return false;
}
is_running_ = UserInit();
return is_running_;
}
void SDL_Framework::Run()
{
Uint32 start_time = SDL_GetTicks();
Uint32 timer = start_time;
int frame_count = 0;
while (is_running_) {
Uint32 frame_start = SDL_GetTicks();
Uint32 elapsed_time = frame_start - start_time;
start_time = frame_start;
HandleEvents();
if (!UserRender(elapsed_time)) {
is_running_ = false;
}
SDL_RenderPresent(Renderer());
frame_count++;
if (SDL_GetTicks() - timer > 1000) {
std::string t = window_title_;
t.append(" (");
t.append(std::to_string(frame_count));
t.append(" FPS)");
SDL_SetWindowTitle(window_, t.c_str());
timer += 1000;
frame_count = 0;
}
Uint32 frame_time = SDL_GetTicks() - frame_start;
if (frame_time < kMaxFrameTime) {
SDL_Delay((int)(kMaxFrameTime - frame_time));
}
}
UserClean();
SDL_DestroyWindow(window_);
SDL_DestroyRenderer(renderer_);
SDL_Quit();
}
void SDL_Framework::DrawCircle(SDL_Point center, int radius, SDL_Color color, bool fill)
{
int radius2 = radius * radius;
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, color.a);
for (int w = 0; w <= radius * 2; w++) {
for (int h = 0; h <= radius * 2; h++) {
int dx = radius - w;
int dy = radius - h;
int pos = dx * dx + dy * dy;
if (fill && pos <= radius2) {
SDL_RenderDrawPoint(renderer_, center.x + dx, center.y + dy);
}
int diff = pos - radius2;
if (!fill && abs(diff) <= 10) {
SDL_RenderDrawPoint(renderer_, center.x + dx, center.y + dy);
}
}
}
}
void SDL_Framework::HandleEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
{
auto found = std::find(pressed_keys_.begin(), pressed_keys_.end(), event.key.keysym.sym);
if (found == pressed_keys_.end()) {
pressed_keys_.push_back(event.key.keysym.sym);
}
}
break;
case SDL_KEYUP:
pressed_keys_.remove(event.key.keysym.sym);
break;
case SDL_QUIT:
is_running_ = false;
break;
case SDL_MOUSEMOTION:
mouse_position_.x = event.motion.x;
mouse_position_.y = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
mouse_button_states_[kMouseLeftButton] = true;
if (event.button.button == SDL_BUTTON_MIDDLE)
mouse_button_states_[kMouseMiddleButton] = true;
if (event.button.button == SDL_BUTTON_RIGHT)
mouse_button_states_[kMouseRightButton] = true;
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
mouse_button_states_[kMouseLeftButton] = false;
if (event.button.button == SDL_BUTTON_MIDDLE)
mouse_button_states_[kMouseMiddleButton] = false;
if (event.button.button == SDL_BUTTON_RIGHT)
mouse_button_states_[kMouseRightButton] = false;
break;
default:
break;
}
}
}
bool SDL_Framework::IsKeyPressed(Sint32 key)
{
return std::find(pressed_keys_.begin(), pressed_keys_.end(), key) != pressed_keys_.end();
}
//END