Source/Gorgon/Main.cpp

Tue, 10 Aug 2021 09:31:50 +0300

author
cemkalyoncu
date
Tue, 10 Aug 2021 09:31:50 +0300
branch
4.x-dev
changeset 1728
92bb193f0a6e
parent 1584
4606aeb9823a
child 1729
06588a526327
permissions
-rw-r--r--

* Run once is now guarded and allows any function with any number of parameters

764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
1 #include <thread>
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
2
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
3 #include "Main.h"
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
4
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
5 #include "Filesystem.h"
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
6 #include "WindowManager.h"
784
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
7 #include "Window.h"
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
8 #include "OS.h"
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
9 #include "Time.h"
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
10 #include "Animation.h"
797
5d31c4836303 * Loader for Resource::Data
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 789
diff changeset
11 #include "Resource.h"
811
e6af71f983cc * Pulse audio connection
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 797
diff changeset
12 #include "Audio.h"
1578
fff352023162 #14 Wav files can now be streamed
cemkalyoncu
parents: 1577
diff changeset
13 #include "Multimedia.h"
863
c6321c51594e * Fixed fullscreen support for windows
cemkalyoncu
parents: 839
diff changeset
14 #include "Graphics.h"
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
15
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
16 #ifdef SCRIPTING
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
17 # include "Scripting.h"
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
18 #endif
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
19
1387
2e91fa117d0f * Widget registry
cemkalyoncu
parents: 1379
diff changeset
20 #ifdef ENABLE_UI
2e91fa117d0f * Widget registry
cemkalyoncu
parents: 1379
diff changeset
21 # include "UI.h"
2e91fa117d0f * Widget registry
cemkalyoncu
parents: 1379
diff changeset
22 #endif
1236
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
23
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
24 /**
1416
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
25 * @page programminghelpers Programming Utilities
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
26 *
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
27 * Debugging Help
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
28 * ==============
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
29 *
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
30 * GDB Pretty Printing
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
31 * -------------------
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
32 *
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
33 * For GDB pretty printing to work with Gorgon objects simply copy `Resource/Gorgon-gdb.py`
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
34 * file to the startup directory of your program. You may need to allow script execution
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
35 * in GDB, when GDB is run, it will show you options on how to allow autoload scripts.
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
36 *
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
37 * Adding the following to `~/.gdbinit` will allow script autoload in everywhere:
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
38 *
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
39 * @code
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
40 * set auto-load safe-path /
ce292ba2937c * DataEffect tests
cemkalyoncu
parents: 1387
diff changeset
41 * @endcode
1236
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
42 */
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
43
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
44 #ifdef __GNUC__
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
45 //For GDB pretty printing
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
46 #define DEFINE_GDB_SCRIPT(name) \
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
47 asm("\
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
48 .pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n\
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
49 .byte 1\n\
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
50 .asciz \"" name "\"\n\
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
51 .popsection \n\
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
52 ");
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
53
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
54 DEFINE_GDB_SCRIPT ("Gorgon-gdb.py")
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
55 #endif
1816e647b817 * Polyfill problem is fixed for AA>1 cases
cemkalyoncu
parents: 928
diff changeset
56
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
57 namespace Gorgon {
789
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
58
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
59 Event<> BeforeFrameEvent;
1288
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
60
1728
92bb193f0a6e * Run once is now guarded and allows any function with any number of parameters
cemkalyoncu
parents: 1584
diff changeset
61 std::mutex once_mtx;
1288
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
62 std::vector<std::function<void()>> once;
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
63
928
c46672472f39 * Audio now joins its thread
cemkalyoncu
parents: 909
diff changeset
64 bool exiting = false;
c46672472f39 * Audio now joins its thread
cemkalyoncu
parents: 909
diff changeset
65
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
66 namespace internal {
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
67 std::string systemname;
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
68 }
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
69
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
70 namespace Animation {
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
71 void Animate();
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
72 }
1577
78a9e4944144 * Design for streaming audio class
cemkalyoncu
parents: 1523
diff changeset
73
78a9e4944144 * Design for streaming audio class
cemkalyoncu
parents: 1523
diff changeset
74 void gorgon_exit() {
78a9e4944144 * Design for streaming audio class
cemkalyoncu
parents: 1523
diff changeset
75 exiting = true;
78a9e4944144 * Design for streaming audio class
cemkalyoncu
parents: 1523
diff changeset
76 }
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
77
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
78 void Initialize(const std::string &name) {
1577
78a9e4944144 * Design for streaming audio class
cemkalyoncu
parents: 1523
diff changeset
79 atexit(&gorgon_exit);
78a9e4944144 * Design for streaming audio class
cemkalyoncu
parents: 1523
diff changeset
80
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
81 internal::systemname=name;
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
82
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
83 Filesystem::Initialize();
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
84 WindowManager::Initialize();
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
85
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
86 #ifdef SCRIPTING
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
87 Scripting::Initialize();
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
88 #endif
839
ddfaef71313f * Current state of the graphics module is done
cemkalyoncu
parents: 811
diff changeset
89 #ifdef AUDIO
811
e6af71f983cc * Pulse audio connection
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 797
diff changeset
90 Audio::Initialize();
839
ddfaef71313f * Current state of the graphics module is done
cemkalyoncu
parents: 811
diff changeset
91 #endif
1578
fff352023162 #14 Wav files can now be streamed
cemkalyoncu
parents: 1577
diff changeset
92 Multimedia::Initialize();
797
5d31c4836303 * Loader for Resource::Data
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 789
diff changeset
93 Resource::Initialize();
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
94 }
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
95
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
96 void Tick() {
784
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
97 static bool inited=false;
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
98 auto ctime=Time::GetTime();
784
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
99 if(!inited) {
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
100 Time::internal::framestart=ctime;
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
101 inited=true;
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
102 }
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
103
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
104 Time::internal::deltatime=ctime-Time::internal::framestart;
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
105 Time::internal::framestart=ctime;
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
106
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
107 Animation::Animate();
1288
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
108
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
109 for(auto &fn : once) {
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
110 fn();
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
111 }
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
112
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
113 once.clear();
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
114 }
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
115
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
116 void Render() {
784
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
117 for(auto &w : Window::Windows) {
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
118 w.Render();
0fe865b9e032 * Animations are now working
cemkalyoncu
parents: 764
diff changeset
119 }
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
120 }
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
121
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
122 void NextFrame() {
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
123 Render();
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
124
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
125 auto ctime=Time::GetTime();
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
126 auto currentdelta=ctime-Time::FrameStart();
1324
eb8d1585e8ec * Several bug fixes
cemkalyoncu
parents: 1288
diff changeset
127
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
128 if(currentdelta<16) {
909
7fd61951afa1 * Fixed shaders
cemkalyoncu
parents: 863
diff changeset
129 std::this_thread::sleep_for(std::chrono::duration<unsigned long, std::milli>(15-currentdelta));
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
130 }
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
131
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
132 Tick();
789
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
133
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
134 BeforeFrameEvent();
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
135
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
136 OS::processmessages();
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
137 }
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
138
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
139 void UpdateFrame() {
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
140 Render();
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
141
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
142 Tick();
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
143
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
144 BeforeFrameEvent();
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
145
4de9ca4d7da0 * HTTP transfer using CURL
cemkalyoncu <cemkalyoncu@gmail.com>
parents: 784
diff changeset
146 OS::processmessages();
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
147 }
1288
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
148
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
149 void RegisterOnce(std::function<void()> fn) {
1728
92bb193f0a6e * Run once is now guarded and allows any function with any number of parameters
cemkalyoncu
parents: 1584
diff changeset
150 std::lock_guard<std::mutex> grd(once_mtx);
92bb193f0a6e * Run once is now guarded and allows any function with any number of parameters
cemkalyoncu
parents: 1584
diff changeset
151
1379
0c9e6ca3674b * IconButton now resizes automatically if no size is given
cemkalyoncu
parents: 1324
diff changeset
152 once.push_back(fn);
1288
27ad8c8a1fa8 * Press/Release events for buttons
cemkalyoncu
parents: 1236
diff changeset
153 }
1728
92bb193f0a6e * Run once is now guarded and allows any function with any number of parameters
cemkalyoncu
parents: 1584
diff changeset
154
764
7c71952bd75b Gorgon code base has been moved to Source/Gorgon
cengizkandemir
parents:
diff changeset
155 }

mercurial