-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathui_manager.cpp
More file actions
280 lines (196 loc) · 7.9 KB
/
ui_manager.cpp
File metadata and controls
280 lines (196 loc) · 7.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "ui_manager.hpp"
#include "hologram.hpp"
#include <boost/compute/source.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/algorithm/iota.hpp>
#include <boost/compute/interop/opengl.hpp>
#include <SFML/Graphics.hpp>
#include "clstate.h"
#include "engine.hpp"
#include <map>
#include "vec.hpp"
#include "proj.hpp"
namespace compute = boost::compute;
int ui_element::gid = 0;
std::vector<ui_element*> ui_manager::ui_elems;
std::map<std::string, cl_float2> ui_manager::offset_from_minimum;
std::vector<std::pair<cl_float4, int>> ship_screen::ship_render_positions;
int ui_manager::selected_value = -1;
int ui_manager::last_selected_value = -1;
void ui_element::set_pos(cl_float2 pos)
{
finish = {pos.x + initial.x, pos.y + initial.y};
}
/*void ui_element::finalise()
{
initial = finish;
}*/
void ui_element::load(int _ref_id, std::string file, std::string _name, cl_float2 _initial, cl_float2 _xbounds, cl_float2 _ybounds)
{
sf::Image img;
img.loadFromFile(file);
GLuint gl_id = get_texture_from_sfml(img);
g_ui = clCreateFromGLTexture2D(cl::context, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, gl_id, NULL);
ref_id = _ref_id;
//int real_id = hologram_manager::get_real_id(ref_id);
initial = _initial;
finish = _initial;
xbounds = _xbounds;
ybounds = _ybounds;
name = _name;
w = img.getSize().x;
h = img.getSize().y;
id = gid++;
//finish = initial;
///blit id with buffer here? All at once? Who? What?
}
void ship_screen::ship_load(int _ref_id, std::string file, std::string selected_file, std::string _name)
{
load(_ref_id, file, _name, {0, 0}, {0, 0}, {0, 0});
sf::Image img;
img.loadFromFile(selected_file.c_str());
GLuint gl_id = get_texture_from_sfml(img);
selected_tex = clCreateFromGLTexture2D(cl::context, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, gl_id, NULL);
}
void ui_element::update_offset()
{
float minx = initial.x + xbounds.x;
float maxx = initial.x + xbounds.y;
float miny = initial.y + ybounds.x;
float maxy = initial.y + ybounds.y;
float cx = finish.x;
float cy = finish.y;
float xfrac = (cx - minx) / (maxx - minx);
float yfrac = (cy - miny) / (maxy - miny);
ui_manager::offset_from_minimum[name] = {xfrac, 1.0f - yfrac};
}
void correct_bounds(ui_element& e)
{
float minx = e.initial.x + e.xbounds.x;
float maxx = e.initial.x + e.xbounds.y;
float miny = e.initial.y + e.ybounds.x;
float maxy = e.initial.y + e.ybounds.y;
if(e.finish.x < minx)
e.finish.x = minx;
if(e.finish.x > maxx)
e.finish.x = maxx;
if(e.finish.y < miny)
e.finish.y = miny;
if(e.finish.y > maxy)
e.finish.y = maxy;
}
///need to create memory for this ui object too
void ui_element::tick()
{
correct_bounds(*this);
int r_id = hologram_manager::get_real_id(ref_id);
hologram_manager::acquire(r_id);
clEnqueueAcquireGLObjects(cl::cqueue, 1, &g_ui, 0, NULL, NULL);
//cl::cqueue.finish();
cl_uint global[2] = {(cl_uint)w, (cl_uint)h};
cl_uint local[2] = {16, 8};
cl_float2 offset = {finish.x - w/2.0f, finish.y - h/2.0f};
compute::buffer coords = compute::buffer(cl::context, sizeof(cl_float2), CL_MEM_COPY_HOST_PTR, &offset);
compute::buffer g_id = compute::buffer(cl::context, sizeof(cl_uint), CL_MEM_COPY_HOST_PTR, &id);
arg_list id_arg_list;
id_arg_list.push_back(&hologram_manager::g_tex_mem_base[r_id]);
id_arg_list.push_back(&hologram_manager::g_tex_mem[r_id]);
id_arg_list.push_back(&g_ui);
id_arg_list.push_back(&coords);
id_arg_list.push_back(&hologram_manager::g_id_bufs[r_id]);
id_arg_list.push_back(&g_id);
run_kernel_with_list(cl::blit_with_id, global, local, 2, id_arg_list, true);
clEnqueueReleaseGLObjects(cl::cqueue, 1, &g_ui, 0, NULL, NULL);
hologram_manager::release(r_id);
update_offset();
//time.stop();
}
void ship_screen::tick()
{
int r_id = hologram_manager::get_real_id(ref_id);
hologram_manager::acquire(r_id);
clEnqueueAcquireGLObjects(cl::cqueue, 1, &g_ui, 0, NULL, NULL);
clEnqueueAcquireGLObjects(cl::cqueue, 1, &selected_tex, 0, NULL, NULL);
//cl::cqueue.finish();
cl_uint global[2] = {(cl_uint)w, (cl_uint)h};
cl_uint local[2] = {16, 16};
compute::buffer wrap_first = compute::buffer(hologram_manager::g_tex_mem_base[r_id]);
compute::buffer wrap_second = compute::buffer(hologram_manager::g_tex_mem[r_id]);
compute::buffer wrap_id_buf = compute::buffer(hologram_manager::g_id_bufs[r_id]);
///the ids written will get confused with ui_ids...
for(int i=0; i<ship_screen::ship_render_positions.size(); i++)
{
compute::buffer wrap_write = compute::buffer(g_ui);
///currently selected, do different colour or something
if(ship_screen::ship_render_positions[i].second == (ui_manager::last_selected_value & (~MINIMAP_BITFLAG)))
{
wrap_write = compute::buffer(selected_tex);
}
cl_int bit_hack = ship_screen::ship_render_positions[i].second | MINIMAP_BITFLAG;
compute::buffer g_id = compute::buffer(cl::context, sizeof(cl_int), CL_MEM_COPY_HOST_PTR | CL_MEM_READ_ONLY, &bit_hack);
cl_float4 pos = ship_screen::ship_render_positions[i].first;
cl_float4 holo_pos = hologram_manager::parents[r_id]->pos;
cl_float2 offset = {pos.x - holo_pos.x, pos.z - holo_pos.z};
offset.x /= 1000;
offset.y /= 1000;
offset.x += hologram_manager::tex_size[r_id].first/2.0f;
offset.y += hologram_manager::tex_size[r_id].second/2.0f;
compute::buffer coords = compute::buffer(cl::context, sizeof(cl_float2), CL_MEM_COPY_HOST_PTR, &offset);
arg_list blit_arg_list;
blit_arg_list.push_back(&wrap_first);
blit_arg_list.push_back(&wrap_second);
blit_arg_list.push_back(&wrap_write);
blit_arg_list.push_back(&coords);
blit_arg_list.push_back(&wrap_id_buf);
blit_arg_list.push_back(&g_id);
run_kernel_with_list(cl::blit_with_id, global, local, 2, blit_arg_list, true);
}
clEnqueueReleaseGLObjects(cl::cqueue, 1, &g_ui, 0, NULL, NULL);
clEnqueueReleaseGLObjects(cl::cqueue, 1, &selected_tex, 0, NULL, NULL);
hologram_manager::release(r_id);
}
ui_element* ui_manager::make_new(int _ref_id, std::string file, std::string name, cl_float2 _offset, cl_float2 _xbounds, cl_float2 _ybounds)
{
ui_element* elem = new ui_element;
elem->load(_ref_id, file, name, _offset, _xbounds, _ybounds);
ui_elems.push_back(elem);
return elem;
}
ship_screen* ui_manager::make_new_ship_screen(int _ref_id, std::string file, std::string selected_file, std::string name)
{
ship_screen* elem = new ship_screen;
elem->ship_load(_ref_id, file, selected_file, name);
ui_elems.push_back(elem);
return elem;
}
///make this sticky?
void ui_manager::update_selected_values(int mx, int my)
{
my = engine::height - my;
sf::Mouse mouse;
if(!mouse.isButtonPressed(sf::Mouse::Left))
{
selected_value = -1;
return;
}
cl_int id;
if(!(mx >= 0 && mx < engine::width && my >= 0 && my < engine::height))
{
return;
}
cl::cqueue.enqueue_read_buffer(engine::g_ui_id_screen, sizeof(cl_int)*(my*engine::width + mx), sizeof(cl_int), &id);
selected_value = id;
last_selected_value = id;
}
void ui_manager::tick_all()
{
cl_uint global[2] = {engine::width, engine::height};
cl_uint local[2] = {16, 8};
arg_list id_clear_arg_list;
id_clear_arg_list.push_back(&engine::g_ui_id_screen);
run_kernel_with_list(cl::clear_id_buf, global, local, 2, id_clear_arg_list, true);
for(auto& i : ui_elems)
{
i->tick();
}
}