Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
mario.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 <fileChanged.h>
8
9#include "Sizes.h"
10#include "input/input.h"
13
15{
16 glm::vec2 position = {};
17 glm::vec2 size = {};
18
19 glm::vec2 getTopLeftCorner();
20 glm::vec2 getCenter();
21 glm::vec2 getBottomLeftCorner();
22 glm::vec2 getBottomCenter();
23
24};
25
26#define PLAYER_SIZE glm::vec2(6.f / 8.f, 1.f)
27
28struct Player
29{
30 Transform position = {glm::vec2(0,0), PLAYER_SIZE};
31
32 glm::vec2 lastPos{};
33
34 glm::vec2 velocity = {};
35
36 bool movingRight = 0;
37 bool grounded = 0;
38
39 void move(glm::vec2 dir);
40
41 void moveVelocityX(float dir);
42
43 void jump(float power);
44
45 void applyGravity(float gravity);
46
47 bool movingThisFrame = false;
48
49
50 //should be called only once per frame last
51 void updateMove();
52
53 //this should be called before collisions
54 void updatePhisics(float deltaTime);
55
56
57 void resolveConstrains(Block *map);
58
59 void checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos,
60 Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch);
61
62 glm::vec2 Player::performCollision(Block *map, glm::vec2 pos, glm::vec2 size,
63 glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch);
64
65 int input = 0;
66};
67
68struct Mario: public Container
69{
70
71 gl2d::Renderer2D renderer;
72 gl2d::Texture tiles;
73 gl2d::Texture marioTexture;
74 gl2d::TextureAtlasPadding atlas;
75
77 samurai::FileChanged fileChanged;
78
79 //todo user can request imgui ids; shortcut manager context; allocators
81 {
82 ContainerStaticInfo info = {};
84
85 info.requestImguiFbo = true; //todo this should not affect the compatibility of input recording
86
87
88 return info;
89 }
90
92 glm::ivec2 mapSize = {100, 100};
93
94 Block &getMapBlockUnsafe(int x, int y)
95 {
96 return map[x + y * mapSize.x];
97 }
98
99 bool loadMap(RequestedContainerInfo &requestedInfo)
100 {
101 size_t s = 0;
102 if (requestedInfo.getFileSizeBinary(SAMURAI_RESOURCES_PATH "/mario/map1.scene", s))
103 {
104 if (s == mapSize.x * mapSize.y)
105 {
106 requestedInfo.readEntireFileBinary(SAMURAI_RESOURCES_PATH "/mario/map1.scene", map, mapSize.x * mapSize.y);
107 }
108 else { return 0; }
109 }
110 else { return 0; }
111
112 return 1;
113 }
114
115 bool create(RequestedContainerInfo &requestedInfo, samurai::StaticString<256> commandLineArgument)
116 {
117 player.position.position = {1,1};
118
119 renderer.create();
120 //gl2d::setErrorFuncCallback() //tood
121 //samurai::initShortcutApi();
122
123 size_t s = 0;
124 if (requestedInfo.getFileSizeBinary(SAMURAI_RESOURCES_PATH "/mario/1985_tiles.png", s))
125 {
126 void *data = new unsigned char[s];
127 if (requestedInfo.readEntireFileBinary(SAMURAI_RESOURCES_PATH "/mario/1985_tiles.png", data, s))
128 {
129 tiles.createFromFileDataWithPixelPadding((unsigned char*)data, s, 8, true, false);
130
131 }
132 else { return 0; }
133
134 delete[] data;
135 }
136 else { return 0; }
137
138 //todo push pop or sthing
140 marioTexture.loadFromFile(SAMURAI_RESOURCES_PATH "/mario/mario.png", true, false);
141 marioTexture.loadFromFile(SAMURAI_RESOURCES_PATH "/mario/mario.png", true, false);
143
144
145 atlas = gl2d::TextureAtlasPadding(8, 10, 8*8, 8*10);
146
147 map = new Block[mapSize.x * mapSize.y];
148 Block d{27,0};
149 memset(map, *(int *)(&d), mapSize.x * mapSize.y);
150
151 renderer.currentCamera.zoom = 60.f;
152
153 bool rez = loadMap(requestedInfo);
154
155 fileChanged.setFile(SAMURAI_RESOURCES_PATH "/mario/map1.scene");
156
157 return rez;
158 }
159
161 RequestedContainerInfo &requestedInfo)
162 {
163
164 {
165 glClear(GL_COLOR_BUFFER_BIT);
166 gl2d::enableNecessaryGLFeatures();
167 renderer.updateWindowMetrics(windowState.w, windowState.h);
168 }
169
170 if (fileChanged.changed())
171 {
172 loadMap(requestedInfo);
173 }
174
175 {
176 float wheel = ImGui::GetIO().MouseWheel;
177
178 //todo standard out
179
180 if ((ImGui::GetIO().KeysData[ImGuiKey_LeftCtrl].Down || ImGui::GetIO().KeysData[ImGuiKey_RightCtrl].Down) && input.hasFocus)
181 {
182 renderer.currentCamera.zoom += wheel * 3;
183 }
184
185 renderer.currentCamera.zoom = std::min(renderer.currentCamera.zoom, 70.f);
186 renderer.currentCamera.zoom = std::max(renderer.currentCamera.zoom, 50.f);
187
188 int delta = 0;
189
190 if (input.hasFocus)
191 {
192 if (input.buttons[samurai::Button::A].held())
193 {
194 delta -= 1;
195 }
196 if (input.buttons[samurai::Button::D].held())
197 {
198 delta += 1;
199 }
200
201 }
202
203 player.input = delta;
204
205 if (input.buttons[samurai::Button::Space].pressed() && player.grounded)
206 {
207 player.jump(75);
208 }
209
210 //phisics
211 {
213
214 player.applyGravity(280.f * input.deltaTime);
215
217
218 player.grounded = false;
220
221 //player.playerAnimation.grounded = i.second.grounded;
222
224
225 //player.playerAnimation.update(input.deltaTime);
226 }
227
228 //todo update gl2d this function
229 renderer.currentCamera.follow(player.position.getCenter(), input.deltaTime * 3, 0.0001, 0.2, windowState.w, windowState.h);
230
231 }
232 auto viewRect = renderer.getViewRect();
233
234 glm::ivec2 minV;
235 glm::ivec2 maxV;
236 //render
237 {
238
239 minV = {viewRect.x - 2, viewRect.y - 2};
240 maxV = minV + glm::ivec2{viewRect.z + 4, viewRect.w + 4};
241 minV = glm::max(minV, {0,0});
242 maxV = glm::min(maxV, mapSize);
243
244
245 for (int j = minV.y; j < maxV.y; j++)
246 for (int i = minV.x; i < maxV.x; i++)
247 {
248 auto b = getMapBlockUnsafe(i, j);
249 auto uv = getTileUV(atlas, b.type, b.flipped);
250
251 renderer.renderRectangle({i, j, 1, 1}, {}, {}, tiles, uv);
252
253 }
254 }
255
256 glm::vec4 pos(player.position.position, 1, 1);
257 //pos.y -= 1 / 8.f;
258 pos.x -= 1 / 8.f;
259
260 renderer.renderRectangle(pos, {}, {}, marioTexture,
261 player.movingRight ? glm::vec4(0,1,1,0) : glm::vec4(1, 1, 0, 0));
262
263
264 renderer.flush();
265
266
267 return true;
268 }
269
270};
271
272//todo flag to clear screen from engine
273//todo error popup
274//todo error popup disable in release
#define PLAYER_SIZE
Definition mario.h:26
glm::vec4 getTileUV(gl2d::TextureAtlasPadding atlas, int id, int flip)
void setGlobalAllocator(samurai::memory::CustomAllocator *allocator)
void setGlobalAllocatorToStandard()
size_t constexpr MB(size_t x)
Definition Sizes.h:6
Definition mario.h:69
gl2d::Texture marioTexture
Definition mario.h:73
bool update(samurai::Input input, samurai::WindowState windowState, RequestedContainerInfo &requestedInfo)
Definition mario.h:160
Block & getMapBlockUnsafe(int x, int y)
Definition mario.h:94
glm::ivec2 mapSize
Definition mario.h:92
gl2d::Renderer2D renderer
Definition mario.h:71
Block * map
Definition mario.h:91
static ContainerStaticInfo containerInfo()
Definition mario.h:80
gl2d::Texture tiles
Definition mario.h:72
Player player
Definition mario.h:76
bool create(RequestedContainerInfo &requestedInfo, samurai::StaticString< 256 > commandLineArgument)
Definition mario.h:115
samurai::FileChanged fileChanged
Definition mario.h:77
bool loadMap(RequestedContainerInfo &requestedInfo)
Definition mario.h:99
gl2d::TextureAtlasPadding atlas
Definition mario.h:74
Definition mario.h:29
bool movingRight
Definition mario.h:36
void jump(float power)
Definition mario.cpp:94
void updatePhisics(float deltaTime)
Definition mario.cpp:147
void move(glm::vec2 dir)
Definition mario.cpp:28
void resolveConstrains(Block *map)
Definition mario.cpp:283
Transform position
Definition mario.h:30
glm::vec2 performCollision(Block *map, glm::vec2 pos, glm::vec2 size, glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
Definition mario.cpp:206
int input
Definition mario.h:65
bool movingThisFrame
Definition mario.h:47
bool grounded
Definition mario.h:37
glm::vec2 velocity
Definition mario.h:34
void applyGravity(float gravity)
Definition mario.cpp:101
void moveVelocityX(float dir)
Definition mario.cpp:40
void checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos, Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
Definition mario.cpp:180
void updateMove()
Definition mario.cpp:117
glm::vec2 lastPos
Definition mario.h:32
bool readEntireFileBinary(const char *name, void *buffer, size_t size)
bool getFileSizeBinary(const char *name, size_t &size)
samurai::memory::CustomAllocator * mainAllocator
glm::vec2 getBottomLeftCorner()
Definition mario.cpp:16
glm::vec2 size
Definition mario.h:17
glm::vec2 getCenter()
Definition mario.cpp:11
glm::vec2 getTopLeftCorner()
Definition mario.cpp:6
glm::vec2 getBottomCenter()
Definition mario.cpp:21
glm::vec2 position
Definition mario.h:16
bool hasFocus
Definition input.h:74
Button buttons[Button::BUTTONS_COUNT]
Definition input.h:69
float deltaTime
Definition input.h:76