-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1522 lines (1297 loc) · 62.3 KB
/
main.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 Jordan Paladino
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <thread>
#include <fstream>
#include <queue>
#define NOMINMAX 1
#define WINVER 0x0601
#define NOMETAFILE 1
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <glm/glm.hpp>
#include "glm/gtc/matrix_transform.hpp"
#include "glad/glad.h"
#include <glfw/glfw3.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw/glfw3native.h>
#include "bitmap.hpp"
#include "types.hpp"
namespace resource {
#include "resource/background.h"
#include "resource/compass.h"
#include "resource/help.h"
#include "resource/icon_16.h"
#include "resource/icon_32.h"
#include "resource/icon_48.h"
#include "resource/icon_64.h"
#include "resource/shader_frag.h"
#include "resource/shader_vert.h"
#include "resource/texture.h"
}
#define OPENGL_SHADER_TESTS 1
struct __global_type {
// Windows application instance
HINSTANCE hInstance = nullptr;
// Windows global low-level keyboard hook
HHOOK hhook = nullptr;
// Blocks the keys registered in 'global_keys' if true
bool blocking_keys = true;
// Enables global keys
bool enable_global_keys = true;
// If the program is running
bool running = true;
// If the window needs to be redrawn
bool redraw = true;
// If true discard the incoming update and render
bool discard = false;
// Global scale for the window
unsigned global_scale = 1;
// Queues an update
bool queue_update = false;
// If true display the help image
bool show_help = false;
struct __window {
GLFWwindow* handle = nullptr;
HWND hwnd = nullptr;
glm::uvec2 size { 600, 600 };
struct __icons {
bitmap icon_64;
bitmap icon_48;
bitmap icon_32;
bitmap icon_16;
} icons;
} window;
struct __opengl {
struct __shader {
struct __buffers {
GLuint quad_vertices_id = 0;
GLuint quad_indices_id = 0;
GLuint quad_instanced_pos_id = 0;
GLuint screen_info_id = 0;
GLuint translation_id = 0;
} buffers;
struct __arrays {
GLuint quad_id = 0;
} arrays;
struct __rect_attribs {
GLuint vertices_id = 0;
GLuint uv_id = 0;
// Instanced
GLuint position_id = 0;
GLuint size_id = 0;
GLuint uv_position_id = 0;
GLuint uv_size_id = 0;
GLuint uv_tr_id = 0;
} rect_attribs;
struct __uniforms {
GLint border_fade_id = 0;
} uniforms;
struct __uniform_buffers {
const GLuint screen_info_index = 0;
const GLuint translation_info_index = 1;
} uniform_buffers;
GLuint program = 0;
} shader;
struct __textures {
GLuint texture_id = 0;
GLuint background_id = 0;
GLuint compass_id = 0;
GLuint help_id = 0;
} textures;
} opengl;
struct __map {
// Current player position in the map
glm::ivec2 position { 0, 0 };
// Current view position
glm::ivec2 view_position { 0, 0 };
// The targeted view position
glm::ivec2 target_view_position { 0, 0 };
bool view_portal_room = false;
bool show_scale_meter = false;
glm::uint scale = 6;
std::map<point_id_t, room_data> rooms;
bool pick_direction = true;
std::pair<glm::ivec2, path_flag> path[10] { { { 0, 0 }, (path_flag) 0 } };
unsigned path_head = 0;
unsigned path_size = 0;
std::vector<glm::ivec2> portal_path;
} map;
std::set<DWORD> global_keys { VK_DOWN, VK_UP, VK_LEFT, VK_RIGHT, VK_HOME, VK_END, VK_PRIOR, VK_NEXT };
} static global_state;
namespace constants {
template<typename _Tp>
inline constexpr glm::vec<2, _Tp, glm::defaultp> north { 0, -1 };
template<typename _Tp>
inline constexpr glm::vec<2, _Tp, glm::defaultp> south { 0, 1 };
template<typename _Tp>
inline constexpr glm::vec<2, _Tp, glm::defaultp> west { -1, 0 };
template<typename _Tp>
inline constexpr glm::vec<2, _Tp, glm::defaultp> east { 1, 0 };
template<typename _Tp>
inline constexpr glm::vec<2, _Tp, glm::defaultp> zero { 0, 0 };
template<typename _Tp>
inline constexpr glm::vec<2, _Tp, glm::defaultp> one { 1, 1 };
namespace window {
constexpr unsigned icon_count = sizeof(__global_type::__window::__icons) / sizeof(bitmap);
}
namespace opengl {
namespace shader {
constexpr GLuint buffers_count = sizeof(__global_type::__opengl::__shader::__buffers) / sizeof(GLuint);
constexpr GLuint arrays_count = sizeof(__global_type::__opengl::__shader::__arrays) / sizeof(GLuint);
constexpr GLuint rect_attribs_count = sizeof(__global_type::__opengl::__shader::__rect_attribs) / sizeof(GLuint);
}
constexpr GLuint textures_count = sizeof(__global_type::__opengl::__textures) / sizeof(GLuint);
}
namespace map {
template<typename _Tp>
inline constexpr _Tp radius = 200;
inline constexpr unsigned min_scale = 1;
inline constexpr unsigned max_scale = 8;
inline constexpr unsigned path_count = 10;
inline constexpr unsigned room_area = 40;
}
quad_vertex const quad_vertices[] {
{ { 0, 0, 0 }, { 0, 0 } },
{ { 1, 0, 0 }, { 1, 0 } },
{ { 1, -1, 0 }, { 1, 1 } },
{ { 0, -1, 0 }, { 0, 1 } },
};
constexpr GLuint quad_indices[] {
0, 1, 2, 0, 2, 3
};
inline constexpr GLuint quad_indices_count = sizeof(quad_indices) / sizeof(GLuint);
}
namespace __detail {
//region OpenGL debug code translation add callback ...
std::string
gl_get_debug_source(unsigned int source) {
switch(source) {
case GL_DEBUG_SOURCE_API: return "API";
case GL_DEBUG_SOURCE_APPLICATION: return "Application";
case GL_DEBUG_SOURCE_SHADER_COMPILER: return "Shader Compiler";
case GL_DEBUG_SOURCE_THIRD_PARTY: return "Third Party";
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: return "Window System";
case GL_DEBUG_SOURCE_OTHER:
default: return "Undefined";
}
}
std::string
gl_get_debug_type(unsigned int type) {
switch(type) {
case GL_DEBUG_TYPE_ERROR: return "Error";
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: return "Deprecated behavior";
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: return "Undefined behavior";
case GL_DEBUG_TYPE_PORTABILITY: return "Portability";
case GL_DEBUG_TYPE_PERFORMANCE: return "Performance";
case GL_DEBUG_TYPE_MARKER: return "Marker";
case GL_DEBUG_TYPE_PUSH_GROUP: return "Push group";
case GL_DEBUG_TYPE_POP_GROUP: return "Pop group";
case GL_DEBUG_TYPE_OTHER:
default: return "Undefined";
}
}
std::string
gl_get_debug_severity(unsigned int severity) {
switch(severity) {
case GL_DEBUG_SEVERITY_NOTIFICATION: return "Info";
case GL_DEBUG_SEVERITY_LOW: return "Low";
case GL_DEBUG_SEVERITY_MEDIUM: return "Medium";
case GL_DEBUG_SEVERITY_HIGH: return "High";
default: return "Undefined";
}
}
void
gl_debug_callback(unsigned int source, unsigned int type, unsigned int id, unsigned int severity, int, const char* message, const void*) {
if(severity == GL_DEBUG_SEVERITY_NOTIFICATION) return;
std::cout << gl_get_debug_source(source) << " [" << id << "] " << gl_get_debug_severity(severity) << " " << gl_get_debug_type(type) << " "
<< message << std::endl;
}
//endregion
void
glfw_error_callback(int type, const char* message) {
std::cout << type << " " << message << std::endl;
}
}
inline constexpr point_id_t
point_id(const glm::ivec2& p) {
constexpr unsigned size = constants::map::radius<unsigned> * 2 + 1;
constexpr unsigned offset = constants::map::radius<unsigned>;
return (point_id_t) ((p.x + offset) + (p.y + offset) * size);
}
template<bool unbind = true>
void
assign_buffer(GLenum target, GLuint buffer, GLuint size, const void* data, GLenum usage = GL_STATIC_DRAW) {
glBindBuffer(target, buffer);
glBufferData(target, size, data, usage);
if(unbind) glBindBuffer(target, 0);
}
GLuint
load_shader(const std::vector<std::string>& shader, GLenum shader_type) {
GLuint shader_id = glCreateShader(shader_type);
if(shader_id == 0) throw std::runtime_error("Unable to create shader");
std::vector<const char*> shader_ptr;
std::vector<GLsizei> shader_length;
for(const auto& item: shader) {
shader_ptr.push_back(item.data( ));
shader_length.push_back((GLsizei) item.size( ));
}
glShaderSource(shader_id, (GLsizei) shader_ptr.size( ), shader_ptr.data( ), shader_length.data( ));
glCompileShader(shader_id);
#if OPENGL_SHADER_TESTS
GLint compiled;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compiled);
if(compiled == GL_FALSE) {
int length = 0;
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &length);
std::vector<char> message(length);
glGetShaderInfoLog(shader_id, length, nullptr, message.data( ));
glDeleteShader(shader_id);
std::string str = std::string(message.begin( ), message.end( ));
std::cerr << str << std::endl;
throw std::runtime_error(str);
}
#endif
return shader_id;
}
GLuint
load_image(const bitmap& map, GLenum filter = GL_NEAREST, GLenum wrap = GL_CLAMP) {
if(!map.allocated) return 0;
GLuint id = 0;
glGenTextures(1, &id);
if(id == 0) throw std::runtime_error("Unable to allocated image on graphics pipeline");
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLint) map.size.x, (GLint) map.size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, map.bytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLint) wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLint) wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLint) filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLint) filter);
glBindTexture(GL_TEXTURE_2D, 0);
return id;
}
struct attrib_builder {
GLuint gl_next_attribute;
GLuint* buffer_ids;
GLuint buffer_ids_size;
attrib_builder(GLuint* ids, GLuint size) :
gl_next_attribute(0), buffer_ids(ids), buffer_ids_size(size) { }
void
attribute(GLint size, GLenum type, GLint stride, GLsizeiptr offset, bool instanced = false) {
if(gl_next_attribute >= buffer_ids_size) throw std::runtime_error("Assigned outside of bounds");
glEnableVertexAttribArray(gl_next_attribute);
glVertexAttribPointer(gl_next_attribute, size, type, false, stride, (void*) offset);
if(instanced) glVertexAttribDivisor(gl_next_attribute, 1);
*(buffer_ids + gl_next_attribute) = gl_next_attribute;
gl_next_attribute++;
}
void
attribute_i(GLint size, GLenum type, GLint stride, GLsizeiptr offset, bool instanced = false) {
if(gl_next_attribute >= buffer_ids_size) throw std::runtime_error("Assigned outside of bounds");
glEnableVertexAttribArray(gl_next_attribute);
glVertexAttribIPointer(gl_next_attribute, size, type, stride, (void*) offset);
if(instanced) glVertexAttribDivisor(gl_next_attribute, 1);
*(buffer_ids + gl_next_attribute) = gl_next_attribute;
gl_next_attribute++;
}
void
attribute_l(GLint size, GLenum type, GLint stride, GLsizeiptr offset, bool instanced = false) {
if(gl_next_attribute >= buffer_ids_size) throw std::runtime_error("Assigned outside of bounds");
glEnableVertexAttribArray(gl_next_attribute);
glVertexAttribLPointer(gl_next_attribute, size, type, stride, (void*) offset);
if(instanced) glVertexAttribDivisor(gl_next_attribute, 1);
*(buffer_ids + gl_next_attribute) = gl_next_attribute;
gl_next_attribute++;
}
};
GLFWwindow*
create_window( ) {
glfwDefaultWindowHints( );
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWmonitor* monitor = glfwGetPrimaryMonitor( );
GLFWwindow* window_ref = glfwCreateWindow((int) global_state.window.size.x, (int) global_state.window.size.y, "Vault Mapper", nullptr, nullptr);
if(window_ref == nullptr) throw std::runtime_error("Failed to create window.");
glfwSetWindowAttrib(window_ref, GLFW_AUTO_ICONIFY, false);
glfwSetWindowAttrib(window_ref, GLFW_RESIZABLE, false);
glfwSetWindowAttrib(window_ref, GLFW_DECORATED, true);
glfwSetWindowAttrib(window_ref, GLFW_FLOATING, false);
const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
glfwSetWindowPos(window_ref, (int) (videoMode->width - global_state.window.size.x) / 2, (int) (videoMode->height - global_state.window.size.y) / 2);
return window_ref;
}
void
window_icons( ) {
global_state.window.icons.icon_64 = load_image(resource::icon_64, sizeof(resource::icon_64));
global_state.window.icons.icon_48 = load_image(resource::icon_48, sizeof(resource::icon_48));
global_state.window.icons.icon_32 = load_image(resource::icon_32, sizeof(resource::icon_32));
global_state.window.icons.icon_16 = load_image(resource::icon_16, sizeof(resource::icon_16));
GLFWimage icons[] {
{ (int) global_state.window.icons.icon_64.size.x, (int) global_state.window.icons.icon_64.size.y, global_state.window.icons.icon_64.bytes },
{ (int) global_state.window.icons.icon_48.size.x, (int) global_state.window.icons.icon_48.size.y, global_state.window.icons.icon_48.bytes },
{ (int) global_state.window.icons.icon_32.size.x, (int) global_state.window.icons.icon_32.size.y, global_state.window.icons.icon_32.bytes },
{ (int) global_state.window.icons.icon_16.size.x, (int) global_state.window.icons.icon_16.size.y, global_state.window.icons.icon_16.bytes }
};
glfwSetWindowIcon(global_state.window.handle, (int) constants::window::icon_count, (GLFWimage*) &icons);
}
void
build_gl_images( ) {
bitmap texture_bitmap = load_image(resource::texture, sizeof(resource::texture));
global_state.opengl.textures.texture_id = load_image(texture_bitmap);
delete_image(texture_bitmap);
bitmap background_bitmap = load_image(resource::background, sizeof(resource::background));
global_state.opengl.textures.background_id = load_image(background_bitmap, GL_LINEAR, GL_REPEAT);
delete_image(background_bitmap);
bitmap compass_bitmap = load_image(resource::compass, sizeof(resource::compass));
global_state.opengl.textures.compass_id = load_image(compass_bitmap);
delete_image(compass_bitmap);
bitmap help_bitmap = load_image(resource::help, sizeof(resource::help));
global_state.opengl.textures.help_id = load_image(help_bitmap);
delete_image(help_bitmap);
}
void
build_gl_items( ) {
glGenBuffers((GLsizei) constants::opengl::shader::buffers_count, (GLuint*) &global_state.opengl.shader.buffers);
glGenVertexArrays((GLsizei) constants::opengl::shader::arrays_count, (GLuint*) &global_state.opengl.shader.arrays);
glBindVertexArray(0);
assign_buffer(GL_ARRAY_BUFFER, global_state.opengl.shader.buffers.quad_vertices_id, sizeof(constants::quad_vertices), &constants::quad_vertices);
assign_buffer(GL_ARRAY_BUFFER, global_state.opengl.shader.buffers.quad_indices_id, sizeof(constants::quad_indices), &constants::quad_indices);
assign_buffer(GL_ARRAY_BUFFER, global_state.opengl.shader.buffers.quad_instanced_pos_id, sizeof(rect), nullptr, GL_DYNAMIC_DRAW);
/**
* @code
* Attrib parts -------------------- Buf - Attributes
* ==================================================
* vertex, uv-------------------------------- [0] - 0 1
* indices ---------------------------------- [1]
* position, size, uv_position, uv_size - i - [2] - 2 3 4 5
* @endcode
*/
glBindVertexArray(global_state.opengl.shader.arrays.quad_id);
attrib_builder quad_builder((GLuint*) &global_state.opengl.shader.rect_attribs, constants::opengl::shader::rect_attribs_count);
glBindBuffer(GL_ARRAY_BUFFER, global_state.opengl.shader.buffers.quad_vertices_id);
quad_builder.attribute(3, GL_FLOAT, sizeof(quad_vertex), offsetof(quad_vertex, position));
quad_builder.attribute(2, GL_FLOAT, sizeof(quad_vertex), offsetof(quad_vertex, uv));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global_state.opengl.shader.buffers.quad_indices_id);
// Instanced buffer
glBindBuffer(GL_ARRAY_BUFFER, global_state.opengl.shader.buffers.quad_instanced_pos_id);
quad_builder.attribute_i(2, GL_INT, sizeof(rect), offsetof(rect, dimensions.position), true);
quad_builder.attribute_i(2, GL_UNSIGNED_INT, sizeof(rect), offsetof(rect, dimensions.size), true);
quad_builder.attribute(2, GL_FLOAT, sizeof(rect), offsetof(rect, texture.position), true);
quad_builder.attribute(2, GL_FLOAT, sizeof(rect), offsetof(rect, texture.size), true);
quad_builder.attribute_i(1, GL_UNSIGNED_INT, sizeof(rect), offsetof(rect, uv_tr), true);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
assign_buffer<false>(GL_UNIFORM_BUFFER, global_state.opengl.shader.buffers.screen_info_id, sizeof(screen_info), nullptr, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, global_state.opengl.shader.uniform_buffers.screen_info_index, global_state.opengl.shader.buffers.screen_info_id, 0,
sizeof(screen_info));
glBindBuffer(GL_UNIFORM_BUFFER, 0);
assign_buffer<false>(GL_UNIFORM_BUFFER, global_state.opengl.shader.buffers.translation_id, sizeof(translation_info), nullptr, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, global_state.opengl.shader.uniform_buffers.translation_info_index, global_state.opengl.shader.buffers.translation_id, 0,
sizeof(translation_info));
glBindBuffer(GL_UNIFORM_BUFFER, 0);
global_state.opengl.shader.program = glCreateProgram( );
//std::stringstream stream;
//std::ifstream file_stream("res/shader_vert.glsl");
//if(!file_stream.is_open( )) throw std::runtime_error("Unable to load fragment shader file");
//stream << file_stream.rdbuf( );
//file_stream.close( );
GLuint vert_id = load_shader({ resource::shader_vert }, GL_VERTEX_SHADER);
glAttachShader(global_state.opengl.shader.program, vert_id);
//stream.str({ });
//file_stream.open("res/shader_frag.glsl");
//if(!file_stream.is_open( )) throw std::runtime_error("unable to load vertex shader file");
//stream << file_stream.rdbuf( );
//file_stream.close( );
GLuint frag_id = load_shader({ resource::shader_frag }, GL_FRAGMENT_SHADER);
glAttachShader(global_state.opengl.shader.program, frag_id);
glBindAttribLocation(global_state.opengl.shader.program, global_state.opengl.shader.rect_attribs.vertices_id, "vertex");
glBindAttribLocation(global_state.opengl.shader.program, global_state.opengl.shader.rect_attribs.uv_id, "uv");
glLinkProgram(global_state.opengl.shader.program);
#if OPENGL_SHADER_TESTS
GLint linked = GL_FALSE;
glGetProgramiv(global_state.opengl.shader.program, GL_LINK_STATUS, &linked);
if(linked == GL_FALSE) {
int length = 0;
glGetProgramiv(global_state.opengl.shader.program, GL_INFO_LOG_LENGTH, &length);
std::vector<char> message(length);
glGetProgramInfoLog(global_state.opengl.shader.program, length, nullptr, message.data( ));
glDeleteShader(vert_id);
glDeleteShader(frag_id);
throw std::runtime_error(message.data( ));
}
#endif
glDeleteShader(vert_id);
glDeleteShader(frag_id);
glValidateProgram(global_state.opengl.shader.program);
#if OPENGL_SHADER_TESTS
glValidateProgram(global_state.opengl.shader.program);
int valid = GL_FALSE;
glGetProgramiv(global_state.opengl.shader.program, GL_VALIDATE_STATUS, &valid);
if(valid == GL_FALSE) {
int length = 0;
glGetProgramiv(global_state.opengl.shader.program, GL_INFO_LOG_LENGTH, &length);
std::vector<char> message(length);
glGetProgramInfoLog(global_state.opengl.shader.program, length, nullptr, message.data( ));
throw std::runtime_error(message.data( ));
}
#endif
glUseProgram(0);
GLint uniform_image = glGetUniformLocation(global_state.opengl.shader.program, "image");
glProgramUniform1i(global_state.opengl.shader.program, uniform_image, 0);
global_state.opengl.shader.uniforms.border_fade_id = glGetUniformLocation(global_state.opengl.shader.program, "border_fade");
GLint uniform_screen_info = glGetUniformBlockIndex(global_state.opengl.shader.program, "screen_info");
glUniformBlockBinding(global_state.opengl.shader.program, uniform_screen_info, global_state.opengl.shader.uniform_buffers.screen_info_index);
GLint uniform_translation_info = glGetUniformBlockIndex(global_state.opengl.shader.program, "translation_info");
glUniformBlockBinding(global_state.opengl.shader.program, uniform_translation_info, global_state.opengl.shader.uniform_buffers.translation_info_index);
build_gl_images( );
}
void
update( );
void
render( );
void
update_translation_position( ) {
glm::vec2 position = global_state.map.view_position + (glm::ivec2) (global_state.window.size / 2u);
glBindBuffer(GL_UNIFORM_BUFFER, global_state.opengl.shader.buffers.translation_id);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec2), &position);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void
update_translation_scale(unsigned scale) {
scale = global_state.global_scale * scale;
glBindBuffer(GL_UNIFORM_BUFFER, global_state.opengl.shader.buffers.translation_id);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::vec2), sizeof(glm::uint), &scale);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void
enable_translation(large_bool enabled) {
glBindBuffer(GL_UNIFORM_BUFFER, global_state.opengl.shader.buffers.translation_id);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::vec2) + sizeof(glm::uint), sizeof(large_bool), &enabled);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
#define DISCARD { global_state.discard = true; return; }
LRESULT CALLBACK
WindowGlobalKeyboard(int code, WPARAM wParam, LPARAM lParam);
void
keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void
init_map(path_flag paths);
void
reset_map( );
void
add_surrounding_rooms(glm::ivec2 pos) {
path_flag paths = global_state.map.rooms[point_id(pos)].paths;
for(int i = 0; i < 8; i++) {
int index = i + (i > 3);
int x = index % 3 - 1;
int y = index / 3 - 1;
bool corner = x && y;
bool left = x == -1;
bool right = x == 1;
bool down = y == 1;
bool up = y == -1;
glm::ivec2 around { pos.x + x, pos.y + y };
if(std::abs(around.x) > constants::map::radius<unsigned> || std::abs(around.y) > constants::map::radius<unsigned>) continue;
bool edge_up = std::abs(around.y) == constants::map::radius<unsigned> && around.y < 0;
bool edge_left = std::abs(around.x) == constants::map::radius<unsigned> && around.x < 0;
bool edge_down = std::abs(around.y) == constants::map::radius<unsigned> && around.y > 0;
bool edge_right = std::abs(around.x) == constants::map::radius<unsigned> && around.x > 0;
auto p = (unsigned) paths;
unsigned open = (corner ? ((!edge_down << 0) | (!edge_right << 1) | (!edge_up << 2) | (!edge_left << 3)) : (
((((p & 0x4) || !up) && !edge_down) << 0)
| ((((p & 0x8) || !left) && !edge_right) << 1)
| ((((p & 0x1) || !down) && !edge_up) << 2)
| ((((p & 0x2) || !right) && !edge_left) << 3)
));
auto found = global_state.map.rooms.find(point_id(around));
if(found != std::end(global_state.map.rooms)) {
if(!corner) found->second.paths = (path_flag) ((unsigned) found->second.paths & open);
} else {
global_state.map.rooms[point_id(around)] = { around, (path_flag) open, room_flag::none, false };
}
}
}
void
add_room(glm::ivec2 pos, path_flag paths, room_flag flags, bool visited = true) {
if(global_state.map.rooms.find(point_id(pos)) != std::end(global_state.map.rooms)) return;
global_state.map.rooms[point_id(pos)] = room_data { pos, paths, flags, visited };
}
void
push_path(const glm::ivec2& point, path_flag direction) {
unsigned position = global_state.map.path_size;
if(global_state.map.path_size == 10) {
position = global_state.map.path_head;
if(global_state.map.path_head == 9) global_state.map.path_head = 0;
else global_state.map.path_head++;
} else global_state.map.path_size++;
global_state.map.path[position] = { point, direction };
}
unsigned
calc_heuristic(glm::ivec2 p) {
//auto dif = global_state.map.position - p;
return p.x * p.x + p.y * p.y /*+ dif.x * dif.x + dif.y * dif.y*/;
}
void
find_path( ) {
static constexpr glm::ivec2 directions[] { constants::south<int>, constants::east<int>, constants::north<int>, constants::west<int> };
global_state.map.portal_path.clear( );
if(global_state.map.position == constants::zero<int>) return;
std::map<point_id_t, astar_point> point_data;
std::priority_queue<queued_point> points;
point_id_t start_id = point_id(global_state.map.position);
point_id_t end_id = point_id(constants::zero<int>);
unsigned start_heuristic = calc_heuristic(global_state.map.position);
point_data[start_id] = { global_state.map.position, { }, 0, start_heuristic };
points.emplace(start_heuristic, start_id);
bool found_portal = false;
unsigned max_length = 64;
while(!points.empty( )) {
auto pi = points.top( ).point;
auto p = point_data[pi];
points.pop( );
auto found = global_state.map.rooms.find(pi);
path_flag paths = path_flag::all;
if(found != std::end(global_state.map.rooms))
paths = found->second.paths;
if(p.path_length + 1 > max_length) continue;
for(int i = 0; i < 4; i++) {
if(!((unsigned) paths & (1 << i))) continue;
auto dir = directions[i];
auto n = p.position + dir;
auto ni = point_id(n);
if(std::abs(n.x) > constants::map::radius<unsigned> || std::abs(n.y) > constants::map::radius<unsigned>) continue;
auto found_data = point_data.find(ni);
if(found_data != std::end(point_data)) {
if(p.path_length < found_data->second.path_length)
found_data->second.parent_dir.emplace_back(-dir);
continue;
}
float scale = 1;
found = global_state.map.rooms.find(ni);
if(found != std::end(global_state.map.rooms)) {
if((unsigned) found->second.flags & (unsigned) room_flag::avoid)
scale = 5;
else if(!found->second.visited)
scale = 1.5f;
} else scale = 3;
auto point_heuristic = (unsigned) ((float) calc_heuristic(n) * scale);
point_data[ni] = { n, { -dir }, p.path_length + 1, point_heuristic };
points.emplace(point_heuristic, ni);
if((found_portal = ni == end_id))
max_length = p.path_length + 1;
//if((found_portal = ni == end_id)) break;
}
//if(found_portal) break;
}
auto found = point_data.find(end_id);
if(found != std::end(point_data)) {
auto pos = constants::zero<int>;
auto dest = found->second;
while(dest.path_length != 0) {
auto next = dest.parent_dir.front( );
for(const auto& item: dest.parent_dir) {
auto point_test = point_data.at(point_id(pos + item));
auto point_next = point_data.at(point_id(pos + next));
//if(point_test.heuristic < point_next.heuristic)
// next = item;
//else if(point_test.heuristic == point_next.heuristic)
if(point_test.path_length < point_next.path_length)
next = item;
}
global_state.map.portal_path.push_back(next);
pos += next;
dest = point_data.at(point_id(pos));
}
}
}
int
main( ) {
if(!glfwInit( ))
throw std::runtime_error("Unable to initialize GLFW");
glfwSetErrorCallback(__detail::glfw_error_callback);
global_state.hInstance = GetModuleHandle(nullptr);
global_state.hhook = SetWindowsHookEx(WH_KEYBOARD_LL, WindowGlobalKeyboard, nullptr, 0);
global_state.window.handle = create_window( );
global_state.window.hwnd = glfwGetWin32Window(global_state.window.handle);
window_icons( );
glfwMakeContextCurrent(global_state.window.handle);
if(!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
throw std::runtime_error("Unable to load OpenGL context");
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(__detail::gl_debug_callback, nullptr);
glfwSetKeyCallback(global_state.window.handle, keyboard_callback);
build_gl_items( );
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glBindVertexArray(global_state.opengl.shader.arrays.quad_id);
glUseProgram(global_state.opengl.shader.program);
screen_info screen { global_state.window.size };
glBindBuffer(GL_UNIFORM_BUFFER, global_state.opengl.shader.buffers.screen_info_id);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(screen_info), &screen);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
update_translation_position( );
update_translation_scale(global_state.map.scale);
enable_translation(false);
glfwShowWindow(global_state.window.handle);
while(global_state.running) {
glfwWaitEvents( );
if(glfwWindowShouldClose(global_state.window.handle))
break;
if(global_state.discard && !global_state.queue_update) continue;
global_state.queue_update = false;
//std::cout << "Update" << std::endl;
update( );
global_state.discard = true;
if(global_state.queue_update) glfwPostEmptyEvent( );
if(!global_state.redraw) continue;
//std::cout << "Render" << std::endl;
render( );
glfwSwapBuffers(global_state.window.handle);
global_state.redraw = false;
}
global_state.running = false;
glBindVertexArray(0);
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteProgram(global_state.opengl.shader.program);
glDeleteVertexArrays((GLsizei) constants::opengl::shader::arrays_count, (GLuint*) &global_state.opengl.shader.arrays);
glDeleteBuffers((GLsizei) constants::opengl::shader::buffers_count, (GLuint*) &global_state.opengl.shader.buffers);
glDeleteTextures((GLsizei) constants::opengl::textures_count, (GLuint*) &global_state.opengl.textures);
glfwTerminate( );
for(int i = 0; i < constants::window::icon_count; i++) {
auto ptr = ((bitmap*) &global_state.window.icons) + i;
delete_image(*ptr);
}
UnhookWindowsHookEx(global_state.hhook);
return EXIT_SUCCESS;
}
LRESULT CALLBACK
WindowGlobalKeyboard(int code, WPARAM wParam, LPARAM lParam) {
if(code != HC_ACTION) return 0;
if(!global_state.enable_global_keys) return 0;
if(global_state.window.hwnd == GetFocus( )) return 0;
switch((DWORD) wParam) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP: {
auto param = (PKBDLLHOOKSTRUCT) lParam;
bool down = (wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN);
if(global_state.global_keys.find(param->vkCode) != std::end(global_state.global_keys)) {
ULONG l = 1 & 0xFFFF // Repeat count
| (param->scanCode & 0x1FF) << 16 // Scancode
| (param->flags & 1) << 24 // Extended
| ((param->flags >> 5) & 1) << 29 // Context
| (!down) << 30 // Previous state
| ((param->flags >> 7) & 1) << 31; // Transition
SendMessage(global_state.window.hwnd, down ? WM_KEYDOWN : WM_KEYUP, (WPARAM) param->vkCode, (LPARAM) l);
if(global_state.blocking_keys) return 1;
}
break;
}
default: break;
}
return CallNextHookEx(nullptr, code, wParam, lParam);
}
void
keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
global_state.discard = false;
static room_data dummy_room { constants::zero<int>, (path_flag) 0, room_flag::none, false };
room_data* room = &dummy_room;
if(!global_state.map.pick_direction)
room = &global_state.map.rooms.at(point_id(global_state.map.position));
switch(key) {
case GLFW_KEY_UP: {
if(global_state.show_help) DISCARD
if(global_state.map.pick_direction) {
if(mods & GLFW_MOD_ALT) DISCARD
if(action == GLFW_RELEASE) return;
init_map(path_flag::north);
global_state.redraw = true;
break;
}
if(global_state.map.view_portal_room) DISCARD
if(global_state.map.position.y - 1 < -constants::map::radius<int>) DISCARD
if(mods & GLFW_MOD_ALT) {
if(room->flags == room_flag::portal) DISCARD
room_data* facing_room = &global_state.map.rooms[point_id(global_state.map.position + constants::north<int>)];
if(facing_room->flags == room_flag::portal) DISCARD
if(action == GLFW_RELEASE) return;
(unsigned&) (room->paths) ^= (unsigned) path_flag::north;
(unsigned&) (facing_room->paths) ^= (unsigned) path_flag::south;
find_path( );
global_state.redraw = true;
return;
}
if(!((unsigned) room->paths & (unsigned) path_flag::north)) DISCARD
if(action == GLFW_RELEASE) return;
global_state.map.position.y--;
push_path(global_state.map.position, path_flag::south);
find_path( );
global_state.map.rooms.at(point_id(global_state.map.position)).visited = true;
add_surrounding_rooms(global_state.map.position);
global_state.map.target_view_position.y = global_state.map.position.y * -40;
global_state.redraw = true;
break;
}
case GLFW_KEY_DOWN: {
if(global_state.show_help) DISCARD
if(global_state.map.pick_direction) {
if(mods & GLFW_MOD_ALT) DISCARD
if(action == GLFW_RELEASE) return;
init_map(path_flag::south);
global_state.redraw = true;
break;
}
if(global_state.map.view_portal_room) DISCARD
if(global_state.map.position.y + 1 > constants::map::radius<int>) DISCARD
if(mods & GLFW_MOD_ALT) {
if(room->flags == room_flag::portal) DISCARD
room_data* facing_room = &global_state.map.rooms[point_id(global_state.map.position + constants::south<int>)];
if(facing_room->flags == room_flag::portal) DISCARD
if(action == GLFW_RELEASE) return;
(unsigned&) (room->paths) ^= (unsigned) path_flag::south;
(unsigned&) (facing_room->paths) ^= (unsigned) path_flag::north;
find_path( );
global_state.redraw = true;
return;
}
if(!((unsigned) room->paths & (unsigned) (path_flag::south))) DISCARD
if(action == GLFW_RELEASE) return;
global_state.map.position.y++;
push_path(global_state.map.position, path_flag::north);
find_path( );
global_state.map.rooms.at(point_id(global_state.map.position)).visited = true;
add_surrounding_rooms(global_state.map.position);
global_state.map.target_view_position.y = global_state.map.position.y * -40;
global_state.redraw = true;
break;
}
case GLFW_KEY_LEFT: {
if(global_state.show_help) DISCARD
if(global_state.map.pick_direction) {
if(mods & GLFW_MOD_ALT) DISCARD
if(action == GLFW_RELEASE) return;
init_map(path_flag::west);
global_state.redraw = true;
break;
}
if(global_state.map.view_portal_room) DISCARD
if(global_state.map.position.x - 1 < -constants::map::radius<int>) DISCARD
if(mods & GLFW_MOD_ALT) {
if(room->flags == room_flag::portal) DISCARD
room_data* facing_room = &global_state.map.rooms[point_id(global_state.map.position + constants::west<int>)];
if(facing_room->flags == room_flag::portal) DISCARD
if(action == GLFW_RELEASE) return;
(unsigned&) (room->paths) ^= (unsigned) path_flag::west;
(unsigned&) (facing_room->paths) ^= (unsigned) path_flag::east;
find_path( );
global_state.redraw = true;
return;
}
if(!((unsigned) room->paths & (unsigned) path_flag::west)) DISCARD
if(action == GLFW_RELEASE) return;
global_state.map.position.x--;
push_path(global_state.map.position, path_flag::east);
find_path( );
global_state.map.rooms.at(point_id(global_state.map.position)).visited = true;
add_surrounding_rooms(global_state.map.position);
global_state.map.target_view_position.x = global_state.map.position.x * -40;
global_state.redraw = true;
break;
}
case GLFW_KEY_RIGHT: {
if(global_state.show_help) DISCARD
if(global_state.map.pick_direction) {
if(mods & GLFW_MOD_ALT) DISCARD
if(action == GLFW_RELEASE) return;
init_map(path_flag::east);
global_state.redraw = true;
return;
}
if(global_state.map.view_portal_room) DISCARD
if(global_state.map.position.x + 1 > constants::map::radius<int>) DISCARD
if(mods & GLFW_MOD_ALT) {
if(room->flags == room_flag::portal) DISCARD
room_data* facing_room = &global_state.map.rooms[point_id(global_state.map.position + constants::east<int>)];
if(facing_room->flags == room_flag::portal) DISCARD
if(action == GLFW_RELEASE) return;
(unsigned&) (room->paths) ^= (unsigned) path_flag::east;
(unsigned&) (facing_room->paths) ^= (unsigned) path_flag::west;
find_path( );
global_state.redraw = true;
return;
}
if(!((unsigned) room->paths & (unsigned) path_flag::east)) DISCARD
if(action == GLFW_RELEASE) return;
global_state.map.position.x++;
push_path(global_state.map.position, path_flag::west);
find_path( );
global_state.map.rooms.at(point_id(global_state.map.position)).visited = true;
add_surrounding_rooms(global_state.map.position);