GlistEngine
Loading...
Searching...
No Matches
gAssetLoader.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Nitra Games Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef UTILS_GASSETLOADER_H_
18#define UTILS_GASSETLOADER_H_
19
20#include <map>
21#include <queue>
22#include "gThread.h"
23#include "gImage.h"
24#include "gModel.h"
25#include "gFont.h"
26#include "gBaseSound.h"
27
28
29class gAssetLoader: public gThread {
30public:
31 static const short SIGNAL_NONE = 0;
32 static const int TYPE_IMAGE = 0, TYPE_TEXTURE = 1, TYPE_MODEL = 2, TYPE_FONT = 3, TYPE_SOUND = 4;
33
35 virtual ~gAssetLoader();
36
37 void load(gImage& image, std::string fullPath, short signal = SIGNAL_NONE);
38 void loadImage(gImage& image, std::string imagePath, short signal = SIGNAL_NONE);
39
41
43 short getSignal();
46 void reset();
48
49 void update();
50
51
52private:
53
54 struct assetToLoad {
55 assetToLoad() {
56 image = nullptr;
57 texture = nullptr;
58 model = nullptr;
59 font = nullptr;
60 sound = nullptr;
61 filename = "";
62 name = "";
63 assetsignal = SIGNAL_NONE;
64 fontsize = 0;
65 modeloptimize = false;
66 type = TYPE_IMAGE;
67 }
68
69 assetToLoad(gImage& image, std::string filename, short signal) {
70 this->image = &image;
71 this->filename = filename;
72 this->name = filename;
73 type = TYPE_IMAGE;
74 assetsignal = signal;
75 }
76
77 assetToLoad(gTexture& texture, std::string filename, short signal) {
78 this->texture = &texture;
79 this->filename = filename;
80 this->name = filename;
81 type = TYPE_TEXTURE;
82 assetsignal = signal;
83 }
84
85 assetToLoad(gModel& model, std::string filename, bool optimize, short signal) {
86 this->model = &model;
87 this->filename = filename;
88 this->name = filename;
89 this->modeloptimize = optimize;
90 type = TYPE_MODEL;
91 assetsignal = signal;
92 }
93
94 assetToLoad(gFont& font, std::string filename, int size, short signal) {
95 this->font = &font;
96 this->filename = filename;
97 this->name = filename;
98 this->fontsize = size;
99 type = TYPE_FONT;
100 assetsignal = signal;
101 }
102
103 assetToLoad(gBaseSound& sound, std::string filename, short signal) {
104 this->sound = &sound;
105 this->filename = filename;
106 this->name = filename;
107 type = TYPE_SOUND;
108 assetsignal = signal;
109 }
110
111 gImage* image;
112 gTexture* texture;
113 gModel* model;
114 gFont* font;
115 gBaseSound* sound;
116
117 std::string filename;
118 std::string name;
119 int type;
120 bool modeloptimize;
121 int fontsize;
122
123 short assetsignal;
124 };
125
126
127 int loadedassetnum;
128 short signal;
129 int counter;
130
131 std::queue<assetToLoad> queue, queue2;
132 bool transferfinished;
133 bool isupdateneeded;
134
135
136 template<typename T>
137 bool send(T& value) {
138 std::unique_lock<std::mutex> lock(mutex);
139 queue.push(std::move(value));
140 conditionvariable.notify_one();
141 return true;
142 }
143
144 template<typename T>
145 bool sendToUpdate(T& value) {
146 std::unique_lock<std::mutex> lock(mutex);
147 queue2.push(std::move(value));
148 conditionvariable.notify_one();
149 return true;
150 }
151
152 template<typename T>
153 bool receive(T& sentValue){
154 std::unique_lock<std::mutex> lock(mutex);
155 while(queue.empty() && !transferfinished){
157 }
158 if(!transferfinished){
159 std::swap(sentValue, queue.front());
160 queue.pop();
161 return true;
162 }else{
163 return false;
164 }
165 }
166
167 template<typename T>
168 bool receiveToUpdate(T& sentValue){
169 std::unique_lock<std::mutex> lock(mutex);
170 while(queue2.empty() && !transferfinished){
172 }
173 if(!transferfinished){
174 std::swap(sentValue, queue2.front());
175 queue2.pop();
176 return true;
177 }else{
178 return false;
179 }
180 }
181
182
183};
184
185#endif /* UTILS_GASSETLOADER_H_ */
Definition gAssetLoader.h:29
static const int TYPE_IMAGE
Definition gAssetLoader.h:32
int getLoadedAssetNum()
bool isUpdateNeeded()
static const int TYPE_TEXTURE
Definition gAssetLoader.h:32
static const int TYPE_SOUND
Definition gAssetLoader.h:32
void resetSignal()
short getSignal()
virtual ~gAssetLoader()
static const int TYPE_MODEL
Definition gAssetLoader.h:32
void loadImage(gImage &image, std::string imagePath, short signal=SIGNAL_NONE)
void threadFunction()
static const int TYPE_FONT
Definition gAssetLoader.h:32
void load(gImage &image, std::string fullPath, short signal=SIGNAL_NONE)
static const short SIGNAL_NONE
Definition gAssetLoader.h:31
int getCounter()
Definition gBaseSound.h:15
Definition gFont.h:47
Definition gImage.h:34
Definition gModel.h:32
Definition gTexture.h:17
Definition gThread.h:28
std::mutex mutex
Definition gThread.h:56
bool lock()
std::condition_variable conditionvariable
Definition gThread.h:55