-
Notifications
You must be signed in to change notification settings - Fork 2
/
sdl_ui.cpp
152 lines (115 loc) · 2.97 KB
/
sdl_ui.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
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
#include "sdl_ui.h"
#include <iostream>
// sdl_ui function definitions
sdl_ui::sdl_ui(int width, int height, int bpp) {
if(!init(width, height, bpp, "Testtesttest")) {
fprintf(stderr, "Couldn't initialize SDL!\n");
} else {
pDrawer = new Drawer(screen);
cout << "sdl_ui created" << endl;
}
}
void sdl_ui::createButton(int x, int y) {
entity *tmp;
tmp = button::generateButton(x, y);
entities.push_back(tmp);
cout << "button created" << endl;
}
void sdl_ui::createPopup(int x, int y) {
entity *tmp;
tmp = popup::generatePopup(x, y, popup_color);
entities.push_back(tmp);
cout << "Popup created" << endl;
}
void sdl_ui::update() {
cout << "updating" << endl;
for(int i = 0; i < entities.size(); i++) {
entities.at(i)->draw(pDrawer);
}
SDL_Flip(screen);
cout << "flipping" << endl;
}
sdl_ui::~sdl_ui() {
delete pDrawer;
for(int i = 0; i < entities.size(); i++) {
delete entities.at(i);
}
SDL_Quit();
}
// entity function definitions
entity::entity(int x, int y) {
offset.x = x;
offset.y = y;
}
bool entity::inRect(int x, int y) const {
if(x < offset.x || x > offset.x+offset.w) // Two if's just to keep it clearer
return false;
if(y < offset.y || y > offset.y+offset.h)
return false;
return true;
}
// picture function definitions
/*picture::picture(int x, int y, string filename) : entity(x, y) {
pic = load_image(filename);
offset.w = pic->w;
offset.h = pic->h;
cout << "A picture has been created!" << endl;
}
void picture::draw(Drawer *pDrawer) {
pDrawer->apply_surface(offset.x, offset.y, pic);
}
entity* picture::generatePicture(int x, int y, string filename) {
entity *pPic = new picture(x, y, filename);
return pPic;
}
picture::~picture() {
SDL_FreeSurface(pic);
}*/
// popup function definitions
popup::popup(int x, int y, color popup_color) : entity(x, y) {
name = "popup";
colors = popup_color;
offset.w = 10;
offset.h = 10;
cout << "A popup window has been created!" << endl;
}
bool popup::addEntity(entity *tmp) {
if(tmp->name != "popup" && inRect(tmp->offset.x, tmp->offset.y)) {
entities.push_back(tmp);
}
}
void popup::draw(Drawer *pDrawer) {
pDrawer->apply_box(offset, colors);
for(int i = 0; i < entities.size(); i++) {
}
}
entity* popup::generatePopup(int x, int y, color popup_color) {
entity *pPup = new popup(x, y, popup_color);
return pPup;
}
popup::~popup() {
}
// button function definitions
button::button(int x, int y) : entity(x, y) {
name = "button";
offset.w = 10;
offset.h = 10;
cout << "A button has been created!" << endl;
}
void button::draw(Drawer *pDrawer) {
cout << "in the draw function" << endl;
int x, y;
SDL_GetMouseState(&x, &y);
if(inRect(x, y)) {
pDrawer->apply_box(offset, pressed_button_color);
} else {
pDrawer->apply_box(offset, button_color);
}
cout << "drawed" << endl;
}
entity* button::generateButton(int x, int y) {
entity *pButton = new button(x, y);
return pButton;
}
button::~button() {
}