Project Alice
Loading...
Searching...
No Matches
opengl_wrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <string_view>
5
6#ifndef GLEW_STATIC
7#define GLEW_STATIC
8#endif
9#include "glew.h"
10
11#include "container_types.hpp"
12#include "texture.hpp"
13#include "fonts.hpp"
14
15namespace ogl {
16namespace parameters {
17
18inline constexpr GLuint screen_width = 0;
19inline constexpr GLuint screen_height = 1;
20inline constexpr GLuint drawing_rectangle = 2;
21
22inline constexpr GLuint border_size = 6;
23inline constexpr GLuint inner_color = 7;
24inline constexpr GLuint subrect = 10;
25
26inline constexpr GLuint enabled = 4;
27inline constexpr GLuint disabled = 3;
28inline constexpr GLuint border_filter = 0;
29inline constexpr GLuint filter = 1;
30inline constexpr GLuint no_filter = 2;
31inline constexpr GLuint sub_sprite = 5;
32inline constexpr GLuint use_mask = 6;
33inline constexpr GLuint progress_bar = 7;
34inline constexpr GLuint frame_stretch = 8;
35inline constexpr GLuint piechart = 9;
36inline constexpr GLuint barchart = 10;
37inline constexpr GLuint linegraph = 11;
38inline constexpr GLuint tint = 12;
39inline constexpr GLuint interactable = 13;
40inline constexpr GLuint interactable_disabled = 14;
41inline constexpr GLuint subsprite_b = 15;
42inline constexpr GLuint alternate_tint = 16;
43inline constexpr GLuint linegraph_color = 17;
44inline constexpr GLuint transparent_color = 18;
45inline constexpr GLuint solid_color = 19;
46inline constexpr GLuint alpha_color = 20;
47inline constexpr GLuint subsprite_c = 21;
48inline constexpr GLuint linegraph_acolor = 22;
49inline constexpr GLuint stripchart = 23;
50} // namespace parameters
51
53
54struct color3f {
55 float r = 0.0f;
56 float g = 0.0f;
57 float b = 0.0f;
58};
59
60
61
62struct image {
63 uint8_t* data = nullptr;
64 int32_t size_x = 0;
65 int32_t size_y = 0;
66 int32_t channels = 0;
67
68 image() { }
69
70 image(uint8_t* data, int32_t size_x, int32_t size_y, int32_t channels) {
71 this->data = data;
72 this->size_x = size_x;
73 this->size_y = size_y;
74 this->channels = channels;
75 }
76 image(image const& other) = delete;
77
78 image(image&& other) noexcept {
79 data = other.data;
80 size_x = other.size_x;
81 size_y = other.size_y;
82 channels = other.channels;
83 other.data = nullptr;
84 }
85
86 image& operator=(image&& other) noexcept {
87 data = other.data;
88 size_x = other.size_x;
89 size_y = other.size_y;
90 channels = other.channels;
91 other.data = nullptr;
92 return *this;
93 }
94 image& operator=(image const& other) = delete;
95
97 if(data)
98 free(data);
99 }
100};
101
102#ifndef NDEBUG
103inline void debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity,
104 GLsizei, // length
105 GLchar const* message, void const*) {
106
107 std::string source_str;
108 switch(source) {
109 case GL_DEBUG_SOURCE_API:
110 source_str = "OpenGL API call";
111 break;
112 case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
113 source_str = "Window system API";
114 break;
115 case GL_DEBUG_SOURCE_SHADER_COMPILER:
116 source_str = "Shading language compiler";
117 break;
118 case GL_DEBUG_SOURCE_THIRD_PARTY:
119 source_str = "Application associated with OpenGL";
120 break;
121 case GL_DEBUG_SOURCE_APPLICATION:
122 source_str = "User generated";
123 break;
124 case GL_DEBUG_SOURCE_OTHER:
125 source_str = "Unknown source";
126 break;
127 default:
128 break;
129 }
130 std::string error_type;
131 switch(type) {
132 case GL_DEBUG_TYPE_ERROR:
133 error_type = "General error";
134 break;
135 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
136 error_type = "Deprecated behavior";
137 break;
138 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
139 error_type = "Undefined behavior";
140 break;
141 case GL_DEBUG_TYPE_PORTABILITY:
142 error_type = "Portability issue";
143 break;
144 case GL_DEBUG_TYPE_PERFORMANCE:
145 error_type = "Performance issue";
146 break;
147 case GL_DEBUG_TYPE_MARKER:
148 error_type = "Command stream annotation";
149 break;
150 case GL_DEBUG_TYPE_PUSH_GROUP:
151 error_type = "Error group push";
152 break;
153 case GL_DEBUG_TYPE_POP_GROUP:
154 error_type = "Error group pop";
155 break;
156 case GL_DEBUG_TYPE_OTHER:
157 error_type = "Uknown error type";
158 break;
159 default:
160 break;
161 }
162 std::string severity_str;
163 switch(severity) {
164 case GL_DEBUG_SEVERITY_HIGH:
165 severity_str = "High";
166 break;
167 case GL_DEBUG_SEVERITY_MEDIUM:
168 severity_str = "Medium";
169 break;
170 case GL_DEBUG_SEVERITY_LOW:
171 severity_str = "Low";
172 break;
173 case GL_DEBUG_SEVERITY_NOTIFICATION:
174 severity_str = "Notification";
175 break;
176 default:
177 break;
178 }
179 std::string full_message("OpenGL error ");
180 full_message += std::to_string(id);
181 full_message += " ";
182 full_message += "; Source: ";
183 full_message += source_str;
184 full_message += " ; Type: ";
185 full_message += error_type;
186 full_message += "; Severity: ";
187 full_message += severity_str;
188 full_message += "; ";
189 full_message += message;
190 full_message += "\n";
191
192#ifdef _WIN64
193 OutputDebugStringA(full_message.c_str());
194#else
195 printf("%s", full_message.c_str());
196#endif
197}
198#endif
199
200struct data {
202 ankerl::unordered_dense::map<std::string, dcon::texture_id> late_loaded_map;
203
204 void* context = nullptr;
205 bool legacy_mode = false;
207
218
226
233
234 GLuint sub_square_buffers[64] = {0};
235
236 GLuint money_icon_tex = 0;
237 GLuint cross_icon_tex = 0;
240 GLuint navy_icon_tex = 0;
241 GLuint army_icon_tex = 0;
242
245 GLuint msaa_rbo = 0;
247 GLuint msaa_texture = 0;
248 GLuint msaa_vao = 0;
249 GLuint msaa_vbo = 0;
253 bool msaa_enabled = false;
254};
255
256void notify_user_of_fatal_opengl_error(std::string message);
257
258void create_opengl_context(sys::state& state); // you shouldn't call this directly; only initialize_opengl should call it
259void initialize_opengl(sys::state& state);
260void shutdown_opengl(sys::state& state);
261
262bool display_tag_is_valid(sys::state& state, char tag[3]);
263
264GLint compile_shader(std::string_view source, GLenum type);
265GLuint create_program(std::string_view vertex_shader, std::string_view fragment_shader);
266void load_shaders(sys::state& state);
268
269class lines {
270private:
271 float* buffer = nullptr;
272 GLuint buffer_handle = 0;
273 bool pending_data_update = true;
274
275public:
277
279 buffer = new float[count * 4];
281 }
282 lines(lines&& o) noexcept
283 : buffer(o.buffer), buffer_handle(o.buffer_handle), pending_data_update(o.pending_data_update), count(o.count) {
284 o.buffer = nullptr;
285 }
286 lines& operator=(lines&& o) noexcept {
287 count = o.count;
288 buffer = o.buffer;
289 buffer_handle = o.buffer_handle;
290 pending_data_update = o.pending_data_update;
291
292 o.buffer = nullptr;
293 return *this;
294 }
296 delete[] buffer;
297 }
298 void set_default_y();
299 void set_y(float* v);
300 void bind_buffer();
301};
302void render_colored_rect(sys::state const& state, float x, float y, float width, float height, float red, float green, float blue, ui::rotation r, bool flipped, bool rtl);
303void render_alpha_colored_rect(sys::state const& state, float x, float y, float width, float height, float red, float green, float blue, float alpha);
304void render_simple_rect(sys::state const& state, float x, float y, float width, float height, ui::rotation r, bool flipped, bool rtl);
305void render_textured_rect(sys::state const& state, color_modification enabled, float x, float y, float width, float height,
306 GLuint texture_handle, ui::rotation r, bool flipped, bool rtl);
307void render_textured_rect_direct(sys::state const& state, float x, float y, float width, float height, uint32_t handle);
308void render_linegraph(sys::state const& state, color_modification enabled, float x, float y, float width, float height, lines& l);
309void render_linegraph(sys::state const& state, color_modification enabled, float x, float y, float width, float height, float r, float g, float b, lines& l);
310void render_linegraph(sys::state const& state, color_modification enabled, float x, float y, float width, float height, float r, float g, float b, float a, lines& l);
311void render_barchart(sys::state const& state, color_modification enabled, float x, float y, float width, float height,
312 data_texture& t, ui::rotation r, bool flipped, bool rtl);
313void render_piechart(sys::state const& state, color_modification enabled, float x, float y, float size, data_texture& t);
314void render_stripchart(sys::state const& state, color_modification enabled, float x, float y, float sizex, float sizey, data_texture& t);
315void render_bordered_rect(sys::state const& state, color_modification enabled, float border_size, float x, float y, float width,
316 float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl);
317void render_masked_rect(sys::state const& state, color_modification enabled, float x, float y, float width, float height,
318 GLuint texture_handle, GLuint mask_texture_handle, ui::rotation r, bool flipped, bool rtl);
319void render_progress_bar(sys::state const& state, color_modification enabled, float progress, float x, float y, float width,
320 float height, GLuint left_texture_handle, GLuint right_texture_handle, ui::rotation r, bool flipped, bool rtl);
321void render_tinted_textured_rect(sys::state const& state, float x, float y, float width, float height, float r, float g, float b,
322 GLuint texture_handle, ui::rotation rot, bool flipped, bool rtl);
323void render_subsprite(sys::state const& state, color_modification enabled, int frame, int total_frames, float x, float y,
324 float width, float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl);
325void render_tinted_rect(sys::state const& state, float x, float y, float width, float height,
326 float r, float g, float b, ui::rotation rot, bool flipped, bool rtl);
327void render_tinted_subsprite(sys::state const& state, int frame, int total_frames, float x, float y,
328 float width, float height, float r, float g, float b, GLuint texture_handle, ui::rotation rot, bool flipped, bool rtl);
329void render_new_text(sys::state const& state, text::stored_glyphs const& txt, color_modification enabled, float x,
330 float y, float size, color3f const& c, text::font& f);
331void render_text(sys::state& state, text::stored_glyphs const& txt, color_modification enabled, float x, float y,
332 color3f const& c, uint16_t font_id);
333void render_text_icon(sys::state& state, text::embedded_icon ico, float x, float baseline_y, float font_size, text::font& f, ogl::color_modification = ogl::color_modification::none);
334void render_text_flag(sys::state& state, text::embedded_flag ico, float x, float baseline_y, float font_size, text::font& f, ogl::color_modification = ogl::color_modification::none);
335void render_text_unit_icon(sys::state& state, text::embedded_unit_icon ico, float x, float baseline_y, float font_size, text::font& f, ogl::color_modification = ogl::color_modification::none);
337 sys::state& state, dcon::commodity_id cid,
338 float x, float y, float w, float h
339);
342 float x, float baseline_y,
343 float font_size, text::font& f
344);
345
346bool msaa_enabled(sys::state const& state);
347void initialize_msaa(sys::state& state, int32_t x, int32_t y);
348void deinitialize_msaa(sys::state& state);
349
351GLuint make_gl_texture(uint8_t* data, uint32_t size_x, uint32_t size_y, uint32_t channels);
352GLuint make_gl_texture(simple_fs::directory const& dir, native_string_view file_name);
353void set_gltex_parameters(GLuint texture_handle, GLuint texture_type, GLuint filter, GLuint wrap);
354GLuint load_texture_array_from_file(simple_fs::file& file, int32_t tiles_x, int32_t tiles_y);
355
356class animation;
357
359private:
360 GLuint framebuffer = 0;
361 GLuint texture_handle = 0;
362 int32_t max_x = 0;
363 int32_t max_y = 0;
364public:
365 void ready(sys::state& state);
366 void finish(sys::state& state);
367 GLuint get();
369
370 friend class animation;
371};
372
373void render_subrect(sys::state const& state, float target_x, float target_y, float target_width, float target_height, float source_x, float source_y, float source_width, float source_height, GLuint texture_handle);
374
376public:
377 enum class type {
384 };
385private:
386 render_capture start_state;
387 render_capture end_state;
388 decltype(std::chrono::steady_clock::now()) start_time;
389 int32_t ms_run_time = 0;
390 int32_t x_pos = 0;
391 int32_t y_pos = 0;
392 int32_t x_size = 0;
393 int32_t y_size = 0;
394 type ani_type;
395 bool running = false;
396public:
397 void start_animation(sys::state& state, int32_t x, int32_t y, int32_t w, int32_t h, type t, int32_t runtime);
398 void post_update_frame(sys::state& state);
399 void render(sys::state& state);
400};
401
402
403} // namespace ogl
void start_animation(sys::state &state, int32_t x, int32_t y, int32_t w, int32_t h, type t, int32_t runtime)
void post_update_frame(sys::state &state)
void render(sys::state &state)
uint32_t count
lines(uint32_t c)
void set_y(float *v)
lines(lines &&o) noexcept
void set_default_y()
lines & operator=(lines &&o) noexcept
void ready(sys::state &state)
void finish(sys::state &state)
constexpr GLuint piechart
constexpr GLuint tint
constexpr GLuint interactable
constexpr GLuint frame_stretch
constexpr GLuint no_filter
constexpr GLuint transparent_color
constexpr GLuint progress_bar
constexpr GLuint alpha_color
constexpr GLuint use_mask
constexpr GLuint screen_width
constexpr GLuint stripchart
constexpr GLuint linegraph_acolor
constexpr GLuint subsprite_b
constexpr GLuint enabled
constexpr GLuint filter
constexpr GLuint barchart
constexpr GLuint inner_color
constexpr GLuint disabled
constexpr GLuint screen_height
constexpr GLuint drawing_rectangle
constexpr GLuint border_size
constexpr GLuint linegraph
constexpr GLuint subsprite_c
constexpr GLuint subrect
constexpr GLuint alternate_tint
constexpr GLuint interactable_disabled
constexpr GLuint border_filter
constexpr GLuint solid_color
constexpr GLuint sub_sprite
constexpr GLuint linegraph_color
void render_tinted_subsprite(sys::state const &state, int frame, int total_frames, float x, float y, float width, float height, float r, float g, float b, GLuint texture_handle, ui::rotation rot, bool flipped, bool rtl)
color_modification
void initialize_opengl(sys::state &state)
GLuint make_gl_texture(uint8_t *data, uint32_t size_x, uint32_t size_y, uint32_t channels)
void create_opengl_context(sys::state &state)
void render_barchart(sys::state const &state, color_modification enabled, float x, float y, float width, float height, data_texture &t, ui::rotation r, bool flipped, bool rtl)
void initialize_msaa(sys::state &state, int32_t size_x, int32_t size_y)
void render_colored_rect(sys::state const &state, float x, float y, float width, float height, float red, float green, float blue, ui::rotation r, bool flipped, bool rtl)
bool msaa_enabled(sys::state const &state)
void load_shaders(sys::state &state)
void render_bordered_rect(sys::state const &state, color_modification enabled, float border_size, float x, float y, float width, float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl)
void render_alpha_colored_rect(sys::state const &state, float x, float y, float width, float height, float red, float green, float blue, float alpha)
void render_piechart(sys::state const &state, color_modification enabled, float x, float y, float size, data_texture &t)
void render_masked_rect(sys::state const &state, color_modification enabled, float x, float y, float width, float height, GLuint texture_handle, GLuint mask_texture_handle, ui::rotation r, bool flipped, bool rtl)
void load_global_squares(sys::state &state)
void deinitialize_msaa(sys::state &state)
void render_new_text(sys::state &state, text::stored_glyphs const &txt, color_modification enabled, float x, float y, float size, color3f const &c, text::font &f)
void render_text_commodity_icon(sys::state &state, text::embedded_commodity_icon ico, float x, float baseline_y, float font_size, text::font &f)
void render_subsprite(sys::state const &state, color_modification enabled, int frame, int total_frames, float x, float y, float width, float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl)
void render_text(sys::state &state, text::stored_glyphs const &txt, color_modification enabled, float x, float y, color3f const &c, uint16_t font_id)
void render_linegraph(sys::state const &state, color_modification enabled, float x, float y, float width, float height, lines &l)
void render_text_flag(sys::state &state, text::embedded_flag ico, float x, float baseline_y, float font_size, text::font &f, ogl::color_modification cmod)
GLuint load_texture_array_from_file(simple_fs::file &file, int32_t tiles_x, int32_t tiles_y)
void render_simple_rect(sys::state const &state, float x, float y, float width, float height, ui::rotation r, bool flipped, bool rtl)
void render_subrect(sys::state const &state, float target_x, float target_y, float target_width, float target_height, float source_x, float source_y, float source_width, float source_height, GLuint texture_handle)
void shutdown_opengl(sys::state &state)
void debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei, GLchar const *message, void const *)
void set_gltex_parameters(GLuint texture_handle, GLuint texture_type, GLuint filter, GLuint wrap)
void render_commodity_icon(sys::state &state, dcon::commodity_id cid, float x, float y, float w, float h)
image load_stb_image(simple_fs::file &file)
GLuint create_program(std::string_view vertex_shader, std::string_view fragment_shader)
void render_textured_rect(sys::state const &state, color_modification enabled, float x, float y, float width, float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl)
bool display_tag_is_valid(sys::state &state, char tag[3])
void render_tinted_rect(sys::state const &state, float x, float y, float width, float height, float r, float g, float b, ui::rotation rot, bool flipped, bool rtl)
void render_stripchart(sys::state const &state, color_modification enabled, float x, float y, float sizex, float sizey, data_texture &t)
void render_text_icon(sys::state &state, text::embedded_icon ico, float x, float baseline_y, float font_size, text::font &f, ogl::color_modification cmod)
GLint compile_shader(std::string_view source, GLenum type)
void render_tinted_textured_rect(sys::state const &state, float x, float y, float width, float height, float r, float g, float b, GLuint texture_handle, ui::rotation rot, bool flipped, bool rtl)
void render_text_unit_icon(sys::state &state, text::embedded_unit_icon ico, float x, float baseline_y, float font_size, text::font &f, ogl::color_modification cmod)
void render_textured_rect_direct(sys::state const &state, float x, float y, float width, float height, uint32_t handle)
void notify_user_of_fatal_opengl_error(std::string message)
void render_progress_bar(sys::state const &state, color_modification enabled, float progress, float x, float y, float width, float height, GLuint left_texture_handle, GLuint right_texture_handle, ui::rotation r, bool flipped, bool rtl)
embedded_icon
Definition: text.hpp:781
std::string_view native_string_view
uint uint32_t
uchar uint8_t
GLuint global_square_flipped_buffer
GLuint color_blind_cross_icon_tex
GLuint ui_shader_screen_height_uniform
GLuint global_square_right_buffer
tagged_vector< texture, dcon::texture_id > asset_textures
GLuint navy_icon_tex
GLuint checkmark_icon_tex
GLuint global_rtl_square_left_flipped_buffer
GLuint ui_shader_border_size_uniform
GLuint msaa_texture
GLuint army_icon_tex
GLuint msaa_uniform_gaussian_blur
GLuint money_icon_tex
GLuint ui_shader_subroutines_index_uniform
GLuint ui_shader_inner_color_uniform
GLuint global_rtl_square_left_buffer
ankerl::unordered_dense::map< std::string, dcon::texture_id > late_loaded_map
GLuint ui_shader_d_rect_uniform
GLuint global_rtl_square_right_buffer
GLuint global_rtl_square_buffer
GLuint global_rtl_square_flipped_buffer
GLuint cross_icon_tex
GLuint msaa_uniform_screen_size
GLuint msaa_shader_program
GLuint ui_shader_secondary_texture_sampler_uniform
GLuint sub_square_buffers[64]
GLuint global_square_left_flipped_buffer
GLuint global_square_left_buffer
GLuint ui_shader_screen_width_uniform
GLuint ui_shader_program
GLuint global_square_buffer
GLuint ui_shader_texture_sampler_uniform
GLuint ui_shader_subrect_uniform
GLuint global_square_right_flipped_buffer
GLuint ui_shader_gamma_uniform
GLuint global_rtl_square_right_flipped_buffer
GLuint msaa_texcolorbuffer
GLuint msaa_framebuffer
GLuint msaa_interbuffer
GLuint global_square_vao
image(image &&other) noexcept
image & operator=(image &&other) noexcept
image(uint8_t *data, int32_t size_x, int32_t size_y, int32_t channels)
int32_t channels
image & operator=(image const &other)=delete
int32_t size_y
image(image const &other)=delete
int32_t size_x
uint8_t * data
Holds important data about the game world, state, and other data regarding windowing,...