Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
marioEditor.h
Go to the documentation of this file.
1#pragma once
2
3#include <gl2d/gl2d.h>
4#include <imgui.h>
5#include <baseContainer.h>
6#include "marioCommon.h"
7#include <safeSave/safeSave.h>
8
9struct MarioEditor: public Container
10{
11 bool collidable = false;
12 bool nonCollidable = false;
13
14 gl2d::Renderer2D renderer;
15 gl2d::Texture tiles;
16 gl2d::TextureAtlasPadding atlas;
17 glm::ivec2 mapSize = {100, 100};
18
19 char path[257] = {};
20
21 glm::vec2 pos = {};
22
23 int currentBlock = 0;
24 bool flip = 0;
25
27
28 Block &getMapBlockUnsafe(int x, int y)
29 {
30 return map[x + y * mapSize.x];
31 }
32
33 //todo user can request imgui ids; shortcut manager context; allocators
35 {
36 ContainerStaticInfo info = {};
38
39 info.requestImguiFbo = true;
40 info.requestImguiIds = 1;
41
42 info.extensionsSuported = {".scene"};
43
44 return info;
45 }
46
47
48 bool create(RequestedContainerInfo &requestedInfo, samurai::StaticString<256> commandLineArgument)
49 {
50
51 renderer.create();
52 //gl2d::setErrorFuncCallback() //tood
53 //samurai::initShortcutApi();
54
55 size_t s = 0;
56 if (requestedInfo.getFileSizeBinary(SAMURAI_RESOURCES_PATH "/mario/1985_tiles.png", s))
57 {
58 void *data = new unsigned char[s];
59 if (requestedInfo.readEntireFileBinary(SAMURAI_RESOURCES_PATH "/mario/1985_tiles.png", data, s))
60 {
61 tiles.createFromFileDataWithPixelPadding((unsigned char*)data, s, 8, true, false);
62
63 }
64 else { return 0; }
65
66 delete[] data;
67 }
68 else { return 0; }
69
70
71 atlas = gl2d::TextureAtlasPadding(8, 10, 8*8, 8*10);
72
73
74 map = new Block[mapSize.x * mapSize.y];
75 Block d{27,0};
76 memset(map, *(int *)(&d), mapSize.x * mapSize.y);
77
78 if (commandLineArgument.size() != 0)
79 {
80 memcpy(path, commandLineArgument.data(), commandLineArgument.size());
81
82 size_t s = 0;
83 if (requestedInfo.getFileSizeBinary(commandLineArgument.to_string().c_str(), s))
84 {
85 if (s == mapSize.x * mapSize.y)
86 {
87 requestedInfo.readEntireFileBinary(commandLineArgument.to_string().c_str(), map, mapSize.x * mapSize.y);
88 }
89 }
90
91 }
92
93 return true;
94 }
95
97 RequestedContainerInfo &requestedInfo)
98 {
99 {
100 glClear(GL_COLOR_BUFFER_BIT);
101 gl2d::enableNecessaryGLFeatures();
102 renderer.updateWindowMetrics(windowState.w, windowState.h);
103 }
104
105 {
106 float wheel = ImGui::GetIO().MouseWheel;
107
108 //todo standard out
109
110 if ((ImGui::GetIO().KeysData[ImGuiKey_LeftCtrl].Down || ImGui::GetIO().KeysData[ImGuiKey_RightCtrl].Down) && input.hasFocus)
111 {
112 renderer.currentCamera.zoom += wheel * 3;
113 }
114
115 renderer.currentCamera.zoom = std::min(renderer.currentCamera.zoom, 200.f);
116 renderer.currentCamera.zoom = std::max(renderer.currentCamera.zoom, 10.f);
117
118 glm::vec2 delta = {};
119
120 if (input.hasFocus)
121 {
122 if (input.buttons[samurai::Button::A].held())
123 {
124 delta.x -= 1;
125 }
126 if (input.buttons[samurai::Button::D].held())
127 {
128 delta.x += 1;
129 }
130 if (input.buttons[samurai::Button::W].held())
131 {
132 delta.y -= 1;
133 }
134 if (input.buttons[samurai::Button::S].held())
135 {
136 delta.y += 1;
137 }
138 }
139
140 float speed = 10;
141
142 delta *= input.deltaTime * speed;
143
144 pos += delta;
145
146 //todo update gl2d this function
147
148 renderer.currentCamera.follow(pos, input.deltaTime * speed * 0.9f, 0.0001, 0.2, windowState.w, windowState.h);
149
150 }
151 auto viewRect = renderer.getViewRect();
152
153 glm::ivec2 minV;
154 glm::ivec2 maxV;
155 //render
156 {
157
158 minV = {viewRect.x-1, viewRect.y-1};
159 maxV = minV + glm::ivec2{viewRect.z+2, viewRect.w+2};
160 minV = glm::max(minV, {0,0});
161 maxV = glm::min(maxV, mapSize);
162
163
164 for (int j = minV.y; j < maxV.y; j++)
165 for (int i = minV.x; i < maxV.x; i++)
166 {
167 auto b = getMapBlockUnsafe(i, j);
168 auto uv = getTileUV(atlas, b.type, b.flipped);
169
170 renderer.renderRectangle({i, j, 1, 1}, {}, {}, tiles, uv);
171
172 }
173 }
174
175 //mouse pos
176 glm::ivec2 blockPosition;
177 {
178 glm::ivec2 mousePos(input.mouseX, input.mouseY);
179
180 auto lerp = [](auto a, auto b, auto c)
181 {
182 return a * (1.f - c) + b * c;
183 };
184
185 blockPosition = lerp(glm::vec2(viewRect.x, viewRect.y),
186 glm::vec2(viewRect.x + viewRect.z, viewRect.y + viewRect.w), glm::vec2(mousePos) / glm::vec2(windowState.w, windowState.h));
187
188 if (blockPosition.x >= maxV.x || blockPosition.y >= maxV.y || blockPosition.x < minV.x || blockPosition.y < minV.y)
189 {
190 blockPosition = {-1,-1};
191 }
192 else
193 {
194 renderer.renderRectangle({blockPosition, 1, 1}, {0.9,0.9,0.9,0.9}, {}, {}, tiles,
196 }
197
198 }
199
200
201 ImGui::Begin("Block picker");
202 {
203
204
205 ImGui::Checkbox("Show Collidable Blocks", &collidable);
206 ImGui::Checkbox("Show Non-Collidable Blocks", &nonCollidable);
207 ImGui::Checkbox("Flip", &flip);
208 ImGui::Text("MousePos: %d, %d", blockPosition.x, blockPosition.y);
209
210 ImGui::InputText("Save file", path, sizeof(path));
211
212 if (ImGui::Button("save"))
213 {
215 sfs::writeEntireFile((void*)map, mapSize.x * mapSize.y, path);
217 }
218
219 ImGui::Separator();
220
221 unsigned short mCount = 0;
222 ImGui::BeginChild("Block Selector");
223 bool inImgui = ImGui::IsWindowHovered();
224
226 {
227 unsigned short localCount = 0;
228 while (mCount < 8*10)
229 {
230 auto uv = getTileUV(atlas, mCount);
231
232 ImGui::PushID(mCount);
233 if (ImGui::ImageButton((void *)(intptr_t)tiles.id,
234 {35,35}, {uv.x, uv.y}, {uv.z, uv.w}))
235 {
236 currentBlock = mCount;
237 }
238
239 ImGui::PopID();
240
241 if (localCount % 10 != 0)
242 {
243 ImGui::SameLine();
244 }
245 localCount++;
246
247 mCount++;
248 }
249 }
250 else
251 {
253 {
254 unsigned short localCount = 0;
255 while (mCount < 8 * 10)
256 {
257 if (isSolid(mCount))
258 {
259 auto uv = getTileUV(atlas, mCount);
260
261 ImGui::PushID(mCount);
262 if (ImGui::ImageButton((void *)(intptr_t)tiles.id,
263 {35,35}, {uv.x, uv.y}, {uv.z, uv.w}));
264 {
265 currentBlock = mCount;
266 }
267 ImGui::PopID();
268
269 if (localCount % 10 != 0)
270 {
271 ImGui::SameLine();
272 }
273 localCount++;
274
275 }
276 mCount++;
277 }
278 }
279 else if (!collidable && nonCollidable)
280 {
281 unsigned short localCount = 0;
282 while (mCount < 8*10)
283 {
284 if (!isSolid(mCount))
285 {
286 auto uv = getTileUV(atlas, mCount);
287
288 ImGui::PushID(mCount);
289 if (ImGui::ImageButton((void *)(intptr_t)tiles.id,
290 {35,35}, {uv.x, uv.y}, {uv.z, uv.w}));
291 {
292 currentBlock = mCount;
293 }
294 ImGui::PopID();
295
296 if (localCount % 10 != 0)
297 {
298 ImGui::SameLine();
299 }
300 localCount++;
301
302 }
303 mCount++;
304 }
305 }
306 }
307 ImGui::EndChild();
308
309
310
311 }
312 ImGui::End();
313
314
315
316 if (input.hasFocus && input.lMouse.held() && blockPosition.x >= 0)
317 {
318 if (input.buttons[samurai::Button::LeftCtrl].held())
319 {
320
321 currentBlock = getMapBlockUnsafe(blockPosition.x, blockPosition.y).type;
322 flip = getMapBlockUnsafe(blockPosition.x, blockPosition.y).flipped;
323
324 }
325 else
326 {
327 getMapBlockUnsafe(blockPosition.x, blockPosition.y).type = currentBlock;
328 getMapBlockUnsafe(blockPosition.x, blockPosition.y).flipped = flip;
329
330 }
331
332 }
333
334
335 renderer.flush();
336
337
338 return true;
339 }
340
341};
342
glm::vec4 getTileUV(gl2d::TextureAtlasPadding atlas, int id, int flip)
bool isSolid(int id)
void setGlobalAllocator(samurai::memory::CustomAllocator *allocator)
void setGlobalAllocatorToStandard()
size_t constexpr MB(size_t x)
Definition Sizes.h:6
unsigned char flipped
Definition marioCommon.h:26
unsigned char type
Definition marioCommon.h:25
samurai::StaticVector< samurai::StaticString< 16 >, 16 > extensionsSuported
unsigned int requestImguiIds
char path[257]
Definition marioEditor.h:19
Block & getMapBlockUnsafe(int x, int y)
Definition marioEditor.h:28
bool nonCollidable
Definition marioEditor.h:12
glm::vec2 pos
Definition marioEditor.h:21
gl2d::Renderer2D renderer
Definition marioEditor.h:14
bool create(RequestedContainerInfo &requestedInfo, samurai::StaticString< 256 > commandLineArgument)
Definition marioEditor.h:48
gl2d::Texture tiles
Definition marioEditor.h:15
bool update(samurai::Input input, samurai::WindowState windowState, RequestedContainerInfo &requestedInfo)
Definition marioEditor.h:96
Block * map
Definition marioEditor.h:26
static ContainerStaticInfo containerInfo()
Definition marioEditor.h:34
gl2d::TextureAtlasPadding atlas
Definition marioEditor.h:16
glm::ivec2 mapSize
Definition marioEditor.h:17
bool collidable
Definition marioEditor.h:11
bool readEntireFileBinary(const char *name, void *buffer, size_t size)
bool getFileSizeBinary(const char *name, size_t &size)
samurai::memory::CustomAllocator * mainAllocator
bool hasFocus
Definition input.h:74
Button lMouse
Definition input.h:62
Button buttons[Button::BUTTONS_COUNT]
Definition input.h:69
float deltaTime
Definition input.h:76
std::string to_string()
size_t size() const