FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
extra_function.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
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 _FGE_EXTRA_FUNCTION_HPP_INCLUDED
18#define _FGE_EXTRA_FUNCTION_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_event.hpp"
22#include "FastEngine/C_rect.hpp"
23#include "FastEngine/graphic/C_color.hpp"
24#include "FastEngine/graphic/C_renderTarget.hpp"
25#include "FastEngine/graphic/C_view.hpp"
26#include "SDL_mouse.h"
27#include "json.hpp"
28#define GLM_ENABLE_EXPERIMENTAL
29#include "glm/gtx/rotate_vector.hpp"
30#include <array>
31#include <filesystem>
32#include <list>
33
34#define FGE_MATH_SQRT2 1.41421356237309504880
35#define FGE_MATH_PI 3.14159265358979323846
36
37namespace fge
38{
39
40class ObjectData;
41using ObjectDataShared = std::shared_ptr<fge::ObjectData>;
42
43struct Line
44{
45 inline Line() = default;
46 inline Line(fge::Vector2f const& start, fge::Vector2f const& end) :
47 _start(start),
48 _end(end)
49 {}
50 inline Line(fge::Vector2f const& origin, fge::Vector2f const& direction, float length) :
51 _start(origin),
52 _end(origin + direction * length)
53 {}
54
55 [[nodiscard]] inline fge::Vector2f getDirection() const { return glm::normalize(this->_end - this->_start); }
56 [[nodiscard]] inline float getLength() const { return glm::length(this->_end - this->_start); }
57
58 fge::Vector2f _start;
59 fge::Vector2f _end;
60};
61
63{
64 fge::Vector2f _point;
65 float _normA;
66 float _normB;
67};
68
69enum TurnMode
70{
71 TURN_CLOCKWISE,
72 TURN_ANTICLOCKWISE,
73
74 TURN_AUTO
75};
76
78[[nodiscard]] inline char UnicodeToChar(uint32_t unicode);
79
80[[nodiscard]] FGE_API bool IsEngineBuiltInDebugMode();
81
82FGE_API bool SetSystemCursor(SDL_SystemCursor id);
83
84FGE_API std::size_t GetFilesInFolder(std::list<std::string>& buffer,
85 std::filesystem::path const& path,
86 std::string const& regexFilter = ".+",
87 bool ignoreDirectory = true,
88 bool onlyFilename = true,
89 bool recursive = false);
90
91FGE_API bool SetVirtualTerminalSequenceSupport();
92FGE_API void SetConsoleCmdTitle(char const* title);
93
94[[nodiscard]] FGE_API void* AlignedAlloc(std::size_t size, std::size_t alignment);
95FGE_API void AlignedFree(void* data);
96
97FGE_API void Sleep(std::chrono::microseconds time);
98
100{
101 void operator()(void* p) const { AlignedFree(p); };
102};
103
104/*
105Implementation of Austin Appleby MurmurHash2 algorithm.
106https://sites.google.com/site/murmurhash/
107public domain
108*/
109[[nodiscard]] FGE_API std::size_t Hash(void const* key, std::size_t len, std::size_t seed = 0xc70f6907UL);
110
111template<typename TFloat>
112[[nodiscard]] inline TFloat LimitRangeAngle(TFloat angleDegree);
113
115#ifndef FGE_DEF_SERVER
116[[nodiscard]] FGE_API bool IsMouseOn(fge::RenderTarget const& target, fge::RectFloat const& zone);
117[[nodiscard]] FGE_API bool IsMouseOn(fge::Vector2f const& mousePos, fge::RectFloat const& zone);
118
119[[nodiscard]] FGE_API bool IsPressed(fge::Event const& evt,
120 fge::Vector2f const& mouse_pos,
121 fge::RectFloat const& zone,
122 uint8_t button = SDL_BUTTON_LEFT);
123#endif //FGE_DEF_SERVER
124
125enum class IntersectionOptions
126{
127 I_NORM_LIMITS,
128 I_STRICT_NORM_LIMITS,
129 I_NO_NORM_LIMITS,
130
131 I_DEFAULT = I_NORM_LIMITS
132};
133
134[[nodiscard]] FGE_API std::optional<fge::Intersection>
135CheckIntersection(fge::Line const& lineA,
136 fge::Line const& lineB,
137 IntersectionOptions option = IntersectionOptions::I_DEFAULT);
138[[nodiscard]] FGE_API std::optional<fge::Intersection>
139CheckIntersection(fge::Vector2f const& position,
140 fge::Vector2f const& direction,
141 fge::Line const& line,
142 IntersectionOptions option = IntersectionOptions::I_DEFAULT);
143
144[[nodiscard]] inline bool IsVertexInCone(fge::Line const& line1,
145 fge::Line const& line2,
146 fge::Vector2f const& origin,
147 fge::Vector2f const& vertex);
148[[nodiscard]] inline bool IsVertexInCone(float coneAngle,
149 fge::Vector2f const& direction,
150 fge::Vector2f const& origin,
151 fge::Vector2f const& vertex);
152
154template<typename T>
155[[nodiscard]] fge::Rect<T> ToRect(fge::Vector2<T> const& pos1, fge::Vector2<T> const& pos2);
156template<typename T>
157[[nodiscard]] fge::Rect<T> ToRect(std::vector<fge::Vector2<T>> const& pos);
158template<typename T>
159[[nodiscard]] fge::Rect<T> ToRect(fge::Vector2<T> const* pos, std::size_t size);
160
162[[nodiscard]] inline fge::Color SetAlpha(fge::Color color, uint8_t alpha);
163[[nodiscard]] inline fge::Color SetRed(fge::Color color, uint8_t red);
164[[nodiscard]] inline fge::Color SetGreen(fge::Color color, uint8_t green);
165[[nodiscard]] inline fge::Color SetBlue(fge::Color color, uint8_t blue);
166
168[[nodiscard]] FGE_API fge::Vector2f
169ReachVector(fge::Vector2f const& position, fge::Vector2f const& target, float speed, float deltaTime);
170[[nodiscard]] FGE_API float
171ReachRotation(float rotation, float target, float speed, float deltaTime, fge::TurnMode turnMode);
172
173template<typename T>
174[[nodiscard]] T ReachValue(T value, T target, T speed, float deltaTime);
175
177[[nodiscard]] inline constexpr float Cross2d(fge::Vector2f const& vec1, fge::Vector2f const& vec2);
178[[nodiscard]] inline fge::Vector2f GetSegmentNormal(fge::Vector2f const& vec1, fge::Vector2f const& vec2);
179[[nodiscard]] inline constexpr float GetAngle(fge::Vector2f const& vec);
180[[nodiscard]] inline constexpr float GetAngleBetween(fge::Vector2f const& vec1, fge::Vector2f const& vec2);
181[[nodiscard]] inline float GetDistanceBetween(fge::Vector2f const& vec1, fge::Vector2f const& vec2);
182[[nodiscard]] inline float
183GetShortestDistanceBetween(fge::Vector2f const& point, fge::Vector2f const& lineStart, fge::Vector2f const& lineEnd);
184
185template<typename TIterator>
186[[nodiscard]] TIterator
187GetNearestPoint(fge::Vector2f const& point, TIterator const& pointsBegin, TIterator const& pointsEnd);
188
189[[nodiscard]] inline constexpr fge::Vector2f GetForwardVector(float angle);
190[[nodiscard]] inline constexpr fge::Vector2f GetBackwardVector(float angle);
191[[nodiscard]] inline constexpr fge::Vector2f GetLeftVector(float angle);
192[[nodiscard]] inline constexpr fge::Vector2f GetRightVector(float angle);
193
194[[nodiscard]] inline constexpr float DotSquare(fge::Vector2f const& vec);
195
196[[nodiscard]] inline constexpr float
197GetHandedness(fge::Vector2f const& vec1, fge::Vector2f const& vec2, fge::Vector2f const& vec3);
198
199//Convert a 'x' value ranging from xMin-Xmax to a 'y' value ranging from yMin-Ymax
200[[nodiscard]] inline constexpr float ConvertRange(float x, float xMin, float xMax, float yMin, float yMax);
201[[nodiscard]] inline constexpr fge::Vector2f ConvertRange(fge::Vector2f const& x,
202 fge::Vector2f const& xMin,
203 fge::Vector2f const& xMax,
204 fge::Vector2f const& yMin,
205 fge::Vector2f const& yMax);
206
207[[nodiscard]] inline constexpr fge::Vector2f MapCircleToSquareCoords(fge::Vector2f const& circleCoords);
208[[nodiscard]] inline constexpr fge::Vector2f MapSquareToCircleCoords(fge::Vector2f const& squareCoords);
209
210/*
211Implementation of Andrew's monotone chain 2D convex hull algorithm.
212Asymptotic complexity: O(n log n).
213Practical performance: 0.5-1.0 seconds for n=1000000 on a 1GHz machine.
214*/
215FGE_API void GetConvexHull(std::vector<fge::Vector2f> const& input, std::vector<fge::Vector2f>& output);
216
218[[nodiscard]] FGE_API fge::Vector2f GetViewSizePercentage(fge::View const& view, fge::View const& defaultView);
219[[nodiscard]] FGE_API fge::Vector2f SetViewSizePercentage(float percentage, fge::View const& defaultView);
220[[nodiscard]] FGE_API fge::Vector2f SetViewSizePercentage(fge::Vector2f const& percentage,
221 fge::View const& defaultView);
222
223[[nodiscard]] FGE_API fge::Vector2f
224TransposePointFromAnotherView(fge::View const& pointView, fge::Vector2f const& point, fge::View const& newView);
225
226enum class ClipClampModes
227{
228 CLIP_CLAMP_NOTHING,
229 CLIP_CLAMP_STRETCH,
230 CLIP_CLAMP_PUSH,
231 CLIP_CLAMP_HIDE
232};
233[[nodiscard]] FGE_API fge::View ClipView(fge::View const& view,
234 fge::RenderTarget const& target,
235 fge::RectFloat const& worldCoordClipRect,
236 fge::ClipClampModes clampMode);
237
239[[nodiscard]] FGE_API fge::RectFloat GetScreenRect(fge::RenderTarget const& target);
240[[nodiscard]] FGE_API fge::RectFloat GetScreenRect(fge::RenderTarget const& target, fge::View const& view);
241
243template<class T>
244[[nodiscard]] inline float DurationToSecondFloat(T duration);
245
247FGE_API bool LoadJsonFromFile(std::filesystem::path const& path, nlohmann::json& j);
248FGE_API bool LoadOrderedJsonFromFile(std::filesystem::path const& path, nlohmann::ordered_json& j);
249FGE_API bool SaveJsonToFile(std::filesystem::path const& path, nlohmann::json const& j, int fieldWidth = 2);
250
252[[nodiscard]] FGE_API std::filesystem::path MakeRelativePathToBasePathIfExist(std::filesystem::path const& basePath,
253 std::filesystem::path const& path);
254
255} // namespace fge
256
257#include "extra_function.inl"
258
259#endif // _FGE_EXTRA_FUNCTION_HPP_INCLUDED
Definition C_color.hpp:35
This class is a wrapper for SDL events.
Definition C_event.hpp:59
Definition C_renderTarget.hpp:56
Define a camera in a 2D scene.
Definition C_view.hpp:43
Definition extra_function.hpp:100
Definition extra_function.hpp:63
Definition extra_function.hpp:44