-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.cpp
More file actions
205 lines (173 loc) · 5.42 KB
/
oop.cpp
File metadata and controls
205 lines (173 loc) · 5.42 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Anda tidak perlu melakukan perubahan pada file ini untuk memenuhi spek
// wajib. Namun, hal tersebut diperbolehkan.
#include "oop.hpp"
#include <map>
#include <iostream>
#include <chrono>
using namespace std::chrono;
high_resolution_clock::time_point start = high_resolution_clock::now();
double time_since_start()
{
high_resolution_clock::time_point now = high_resolution_clock::now();
return duration_cast<duration<double>>(now - start).count();
}
SDL_Window* sdlWindow;
std::map<std::string, SDL_Surface*> loadedSurfaces;
std::map<int, TTF_Font*> loadedFontSizes;
SDL_Surface* gScreenSurface = NULL;
bool init()
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
if(TTF_Init() == -1) {
printf("TTF_Init: %s\n", TTF_GetError());
success = false;
}
sdlWindow = SDL_CreateWindow( "ArkavQuarium", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( sdlWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
gScreenSurface = SDL_GetWindowSurface( sdlWindow );
}
}
return success;
}
void close()
{
for (auto const& x : loadedSurfaces)
{
SDL_FreeSurface( x.second );
}
for (auto const& x : loadedFontSizes)
{
TTF_CloseFont( x.second );
}
SDL_DestroyWindow( sdlWindow );
sdlWindow = NULL;
SDL_Quit();
}
SDL_Surface* loadSurface( std::string path )
{
SDL_Surface* loadedSurface = NULL;
if (loadedSurfaces.count(path) < 1) {
loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
loadedSurfaces[path] = loadedSurface;
} else {
loadedSurface = loadedSurfaces[path];
}
if (loadedSurface == NULL) {
loadedSurface = loadSurface("unknown.png");
}
return loadedSurface;
}
void draw_image(std::string filename, int x, int y) {
SDL_Surface* s = loadSurface(filename);
SDL_Rect dest;
dest.x = x - s->w/2;
dest.y = y - s->h/2;
dest.w = s->w;
dest.h = s->h;
SDL_BlitSurface(s, NULL, gScreenSurface, &dest);
}
void draw_text(std::string text, int font_size, int x, int y, unsigned char r, unsigned char g, unsigned char b) {
if (loadedFontSizes.count(font_size) < 1) {
loadedFontSizes[font_size] = TTF_OpenFont(FONT_NAME, font_size);
}
TTF_Font* font = loadedFontSizes[font_size];
SDL_Surface* result = TTF_RenderText_Blended(font, text.c_str(), {r, g, b});
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = result->w;
dest.h = result->h;
SDL_BlitSurface(result, NULL, gScreenSurface, &dest);
SDL_FreeSurface(result);
}
void clear_screen() {
SDL_FillRect(gScreenSurface, NULL, SDL_MapRGB(gScreenSurface->format, 255, 255, 255));
}
void update_screen() {
SDL_UpdateWindowSurface(sdlWindow);
}
bool quit = false;
std::set<SDL_Keycode> pressedKeys;
std::set<SDL_Keycode> tappedKeys;
bool leftButtonClicked = false;
bool leftButtonPressed = false;
bool leftButtonReleased = false;
int mouseX = 0;
int mouseY = 0;
void handle_input() {
SDL_Event e;
if (!tappedKeys.empty()) tappedKeys.clear();
if (leftButtonReleased) leftButtonReleased = false;
if (leftButtonClicked) leftButtonClicked = false;
while( SDL_PollEvent( &e ) != 0 )
{
if ( e.type == SDL_QUIT ) {
quit = true;
} else if (e.type == SDL_KEYDOWN && !e.key.repeat) {
pressedKeys.insert(e.key.keysym.sym);
tappedKeys.insert(e.key.keysym.sym);
} else if (e.type == SDL_KEYUP) {
pressedKeys.erase(e.key.keysym.sym);
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
if (e.button.button == SDL_BUTTON_LEFT) {
leftButtonClicked = true;
leftButtonPressed = true;
mouseX = e.button.x;
mouseY = e.button.y;
}
} else if (e.type == SDL_MOUSEBUTTONUP) {
if (e.button.button == SDL_BUTTON_LEFT) {
leftButtonReleased = true;
leftButtonPressed = false;
mouseX = e.button.x;
mouseY = e.button.y;
}
} else if (e.type == SDL_MOUSEMOTION) {
if (leftButtonPressed) {
mouseX = e.motion.x;
mouseY = e.motion.y;
}
}
}
}
bool quit_pressed() {
return quit;
}
const std::set<SDL_Keycode>& get_pressed_keys() {
return pressedKeys;
}
const std::set<SDL_Keycode>& get_tapped_keys() {
return tappedKeys;
}
bool isLeftButtonClicked() {
return leftButtonClicked;
}
bool isLeftButtonPressed() {
return leftButtonPressed;
}
bool isLeftButtonReleased() {
return leftButtonReleased;
}
int getMouseX() {
return mouseX;
}
int getMouseY() {
return mouseY;
}