Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (125 loc) · 3.28 KB
/
Copy pathmain.cpp
File metadata and controls
159 lines (125 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Raylib example file.
This is an example main file for a simple raylib project.
Use this as a starting point or replace it with your code.
by Jeffery Myers is marked with CC0 1.0. To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0/
*/
#include "raylib.h"
#include "rlgl.h"
#include <chrono>
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include "GLFW/glfw3.h"
std::mutex TextureLocker;
std::atomic<bool> LoadComplete;
std::atomic<bool> Abort;
GLFWwindow* LoaderWindow = nullptr;
std::vector<Texture2D> Textures;
int TextureCount = 40;
void LoadTextureInThread()
{
if (!LoaderWindow)
return;
glfwMakeContextCurrent(LoaderWindow);
for (int i = 0; i < TextureCount; i++)
{
if (Abort)
break;
// load perlin noise images and rescale them so they take some time
Image perlin = GenImagePerlinNoise(1024, 1024, i*16, i*34, i/(TextureCount / 3.0f));
ImageColorTint(&perlin, Color{ (unsigned char)GetRandomValue(128,255), (unsigned char)GetRandomValue(128,255) ,(unsigned char)GetRandomValue(128,255) , 255 });
ImageResize(&perlin, 128, 128);
auto texture = LoadTextureFromImage(perlin);
glFlush();
{
std::lock_guard<std::mutex> guard(TextureLocker);
Textures.push_back(texture);
}
UnloadImage(perlin);
}
glfwDestroyWindow(LoaderWindow);
LoaderWindow = nullptr;
LoadComplete = true;
}
std::thread LoaderThread;
void StartLoad()
{
Abort = false;
LoadComplete = false;
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // Window initially hidden
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // Window initially hidden
LoaderWindow = glfwCreateWindow(1, 1, "TempWindow", NULL, glfwGetCurrentContext());
glfwHideWindow(LoaderWindow);
LoaderThread = std::thread(LoadTextureInThread);
}
void CleanupLoad()
{
Abort = true;
if (LoaderThread.joinable())
LoaderThread.join();
for (auto& texture : Textures)
{
UnloadTexture(texture);
}
Textures.clear();
}
int main ()
{
// Create the window and OpenGL context
InitWindow(1280, 800, "Hello Raylib");
SetTargetFPS(60);
StartLoad();
float throberPos = 0;
float throberDir = 1;
// game loop
while (!WindowShouldClose()) // run the loop untill the user presses ESCAPE or presses the Close button on the window
{
// drawing
BeginDrawing();
ClearBackground(DARKGRAY);
{
std::lock_guard<std::mutex> guard(TextureLocker);
if (!LoadComplete)
{
// show a progress bar
DrawText(TextFormat("Loading %d / %d", Textures.size() + 1, TextureCount), 10, 30, 20, WHITE);
throberPos += GetFrameTime() * 800 * throberDir;
if (throberPos > GetScreenWidth())
{
throberPos = GetScreenWidth();
throberDir *= -1;
}
else if (throberPos < 0)
{
throberPos = 0;
throberDir *= -1;
}
DrawCircle(throberPos, 80, 20, RED);
}
//else
{
int x = 0;
int y = 140;
for (auto& tx : Textures)
{
DrawTexture(tx, x, y, WHITE);
x += tx.width + 1;
if (x > GetScreenWidth() - tx.width)
{
x = 0;
y += tx.height + 1;
}
}
}
}
DrawFPS(10, 10);
// end the frame and get ready for the next one (display frame, poll input, etc...)
EndDrawing();
}
CleanupLoad();
// destroy the window and cleanup the OpenGL context
CloseWindow();
return 0;
}