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;
45} // namespace parameters
46
48
49struct color3f {
50 float r = 0.0f;
51 float g = 0.0f;
52 float b = 0.0f;
53};
54
55
56
57struct image {
58 uint8_t* data = nullptr;
59 int32_t size_x = 0;
60 int32_t size_y = 0;
61 int32_t channels = 0;
62
63 image() { }
64
65 image(uint8_t* data, int32_t size_x, int32_t size_y, int32_t channels) {
66 this->data = data;
67 this->size_x = size_x;
68 this->size_y = size_y;
69 this->channels = channels;
70 }
71 image(image const& other) = delete;
72
73 image(image&& other) noexcept {
74 data = other.data;
75 size_x = other.size_x;
76 size_y = other.size_y;
77 channels = other.channels;
78 other.data = nullptr;
79 }
80
81 image& operator=(image&& other) noexcept {
82 data = other.data;
83 size_x = other.size_x;
84 size_y = other.size_y;
85 channels = other.channels;
86 other.data = nullptr;
87 return *this;
88 }
89 image& operator=(image const& other) = delete;
90
92 if(data)
93 free(data);
94 }
95};
96
97#ifndef NDEBUG
98inline void debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity,
99 GLsizei, // length
100 GLchar const* message, void const*) {
101
102 std::string source_str;
103 switch(source) {
104 case GL_DEBUG_SOURCE_API:
105 source_str = "OpenGL API call";
106 break;
107 case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
108 source_str = "Window system API";
109 break;
110 case GL_DEBUG_SOURCE_SHADER_COMPILER:
111 source_str = "Shading language compiler";
112 break;
113 case GL_DEBUG_SOURCE_THIRD_PARTY:
114 source_str = "Application associated with OpenGL";
115 break;
116 case GL_DEBUG_SOURCE_APPLICATION:
117 source_str = "User generated";
118 break;
119 case GL_DEBUG_SOURCE_OTHER:
120 source_str = "Unknown source";
121 break;
122 default:
123 break;
124 }
125 std::string error_type;
126 switch(type) {
127 case GL_DEBUG_TYPE_ERROR:
128 error_type = "General error";
129 break;
130 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
131 error_type = "Deprecated behavior";
132 break;
133 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
134 error_type = "Undefined behavior";
135 break;
136 case GL_DEBUG_TYPE_PORTABILITY:
137 error_type = "Portability issue";
138 break;
139 case GL_DEBUG_TYPE_PERFORMANCE:
140 error_type = "Performance issue";
141 break;
142 case GL_DEBUG_TYPE_MARKER:
143 error_type = "Command stream annotation";
144 break;
145 case GL_DEBUG_TYPE_PUSH_GROUP:
146 error_type = "Error group push";
147 break;
148 case GL_DEBUG_TYPE_POP_GROUP:
149 error_type = "Error group pop";
150 break;
151 case GL_DEBUG_TYPE_OTHER:
152 error_type = "Uknown error type";
153 break;
154 default:
155 break;
156 }
157 std::string severity_str;
158 switch(severity) {
159 case GL_DEBUG_SEVERITY_HIGH:
160 severity_str = "High";
161 break;
162 case GL_DEBUG_SEVERITY_MEDIUM:
163 severity_str = "Medium";
164 break;
165 case GL_DEBUG_SEVERITY_LOW:
166 severity_str = "Low";
167 break;
168 case GL_DEBUG_SEVERITY_NOTIFICATION:
169 severity_str = "Notification";
170 break;
171 default:
172 break;
173 }
174 std::string full_message("OpenGL error ");
175 full_message += std::to_string(id);
176 full_message += " ";
177 full_message += "; Source: ";
178 full_message += source_str;
179 full_message += " ; Type: ";
180 full_message += error_type;
181 full_message += "; Severity: ";
182 full_message += severity_str;
183 full_message += "; ";
184 full_message += message;
185 full_message += "\n";
186
187#ifdef _WIN64
188 OutputDebugStringA(full_message.c_str());
189#else
190 printf("%s", full_message.c_str());
191#endif
192}
193#endif
194
195struct data {
197
198 void* context = nullptr;
199 bool legacy_mode = false;
201
212
220
227
228 GLuint sub_square_buffers[64] = {0};
229
230 GLuint money_icon_tex = 0;
231 GLuint cross_icon_tex = 0;
234 GLuint navy_icon_tex = 0;
235 GLuint army_icon_tex = 0;
236
239 GLuint msaa_rbo = 0;
241 GLuint msaa_texture = 0;
242 GLuint msaa_vao = 0;
243 GLuint msaa_vbo = 0;
247 bool msaa_enabled = false;
248};
249
250void notify_user_of_fatal_opengl_error(std::string message);
251
252void create_opengl_context(sys::state& state); // you shouldn't call this directly; only initialize_opengl should call it
253void initialize_opengl(sys::state& state);
254void shutdown_opengl(sys::state& state);
255
256bool display_tag_is_valid(sys::state& state, char tag[3]);
257
258GLint compile_shader(std::string_view source, GLenum type);
259GLuint create_program(std::string_view vertex_shader, std::string_view fragment_shader);
260void load_shaders(sys::state& state);
262
263class lines {
264private:
265 float* buffer = nullptr;
266 GLuint buffer_handle = 0;
267 bool pending_data_update = true;
268
269public:
271
273 buffer = new float[count * 4];
275 }
276 lines(lines&& o) noexcept
277 : buffer(o.buffer), buffer_handle(o.buffer_handle), pending_data_update(o.pending_data_update), count(o.count) {
278 o.buffer = nullptr;
279 }
280 lines& operator=(lines&& o) noexcept {
281 count = o.count;
282 buffer = o.buffer;
283 buffer_handle = o.buffer_handle;
284 pending_data_update = o.pending_data_update;
285
286 o.buffer = nullptr;
287 return *this;
288 }
290 delete[] buffer;
291 }
292 void set_default_y();
293 void set_y(float* v);
294 void bind_buffer();
295};
296void render_simple_rect(sys::state const& state, float x, float y, float width, float height, ui::rotation r, bool flipped);
297void render_textured_rect(sys::state const& state, color_modification enabled, float x, float y, float width, float height,
298 GLuint texture_handle, ui::rotation r, bool flipped, bool rtl);
299void render_textured_rect_direct(sys::state const& state, float x, float y, float width, float height, uint32_t handle);
300void render_linegraph(sys::state const& state, color_modification enabled, float x, float y, float width, float height, lines& l);
301void 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);
302void render_barchart(sys::state const& state, color_modification enabled, float x, float y, float width, float height,
303 data_texture& t, ui::rotation r, bool flipped, bool rtl);
304void render_piechart(sys::state const& state, color_modification enabled, float x, float y, float size, data_texture& t);
305void render_bordered_rect(sys::state const& state, color_modification enabled, float border_size, float x, float y, float width,
306 float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl);
307void render_masked_rect(sys::state const& state, color_modification enabled, float x, float y, float width, float height,
308 GLuint texture_handle, GLuint mask_texture_handle, ui::rotation r, bool flipped, bool rtl);
309void render_progress_bar(sys::state const& state, color_modification enabled, float progress, float x, float y, float width,
310 float height, GLuint left_texture_handle, GLuint right_texture_handle, ui::rotation r, bool flipped, bool rtl);
311void render_tinted_textured_rect(sys::state const& state, float x, float y, float width, float height, float r, float g, float b,
312 GLuint texture_handle, ui::rotation rot, bool flipped, bool rtl);
313void render_subsprite(sys::state const& state, color_modification enabled, int frame, int total_frames, float x, float y,
314 float width, float height, GLuint texture_handle, ui::rotation r, bool flipped, bool rtl);
315void render_tinted_rect(sys::state const& state, float x, float y, float width, float height,
316 float r, float g, float b, ui::rotation rot, bool flipped, bool rtl);
317void render_tinted_subsprite(sys::state const& state, int frame, int total_frames, float x, float y,
318 float width, float height, float r, float g, float b, GLuint texture_handle, ui::rotation rot, bool flipped, bool rtl);
319void render_new_text(sys::state const& state, text::stored_glyphs const& txt, color_modification enabled, float x,
320 float y, float size, color3f const& c, text::font& f);
321void render_text(sys::state& state, text::stored_glyphs const& txt, color_modification enabled, float x, float y,
322 color3f const& c, uint16_t font_id);
323void 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);
324void 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);
325void 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);
326
327bool msaa_enabled(sys::state const& state);
328void initialize_msaa(sys::state& state, int32_t x, int32_t y);
329void deinitialize_msaa(sys::state& state);
330
332GLuint make_gl_texture(uint8_t* data, uint32_t size_x, uint32_t size_y, uint32_t channels);
333GLuint make_gl_texture(simple_fs::directory const& dir, native_string_view file_name);
334void set_gltex_parameters(GLuint texture_handle, GLuint texture_type, GLuint filter, GLuint wrap);
335GLuint load_texture_array_from_file(simple_fs::file& file, int32_t tiles_x, int32_t tiles_y);
336} // namespace ogl
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
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 use_mask
constexpr GLuint screen_width
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 subrect
constexpr GLuint alternate_tint
constexpr GLuint interactable_disabled
constexpr GLuint border_filter
constexpr GLuint sub_sprite
constexpr GLuint linegraph_color
Definition: color.hpp:6
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)
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_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_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 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)
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_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
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