optimized sub-optimal code

This commit is contained in:
sillysagiri 2023-10-14 12:17:58 +07:00
parent ffd4b59eca
commit 9afe1d0993
18 changed files with 704 additions and 444 deletions

View File

@ -10,10 +10,10 @@ set(PROJECT_VERSION 1.0)
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION}) project(${PROJECT_NAME} VERSION ${PROJECT_VERSION})
############################################################## ##############################################################
set(RAYLIB_DIR "/home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/raylib-4.5.0") set(RAYLIB_DIR "/run/media/sillysagiri/linux-home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/raylib-4.5.0")
set(IMGUI_DIR "/home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/imgui-docking") set(IMGUI_DIR "/run/media/sillysagiri/linux-home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/imgui-docking")
set(RLIMGUI_DIR "/home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/rlImGui") set(RLIMGUI_DIR "/run/media/sillysagiri/linux-home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/rlImGui")
set(INIFILE_DIR "/home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/inifile-cpp") set(INIFILE_DIR "/run/media/sillysagiri/linux-home/tuanpanda/Panda/04-WORKSPACE/00-PROGRAMMING/_Git Clone/inifile-cpp")
### Fallback link ### Fallback link
set(RAYLIB_LINK "https://github.com/raysan5/raylib/archive/refs/tags/4.5.0.zip") set(RAYLIB_LINK "https://github.com/raysan5/raylib/archive/refs/tags/4.5.0.zip")

124
src/Panda.hpp Executable file
View File

@ -0,0 +1,124 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include "utils/Random.hpp"
struct Texture;
struct RenderTexture;
struct Color;
struct Vector2;
struct Rectangle;
namespace panda
{
struct TextureAtlasItem
{
int x;
int y;
int w;
int h;
float px;
float py;
};
class Scene {
public:
Scene() {}
virtual ~Scene() {}
virtual void update() {}
};
struct Sprite {
Texture *texture;
float x = 0.0f;
float y = 0.0f;
float scaleX = 1.0f;
float scaleY = 1.0f;
float rotation = 0.0f;
void Load(const char* path);
void Unload();
void Draw(Color color);
void SetPivot(Vector2 ratio);
void SetPivotRatio(Vector2 ratio);
void SetFlip(bool x, bool y);
void SetFlipX(bool x);
void SetFlipY(bool y);
Vector2 GetPivot();
Vector2 GetPivotFlip();
bool GetFlipX();
bool GetFlipY();
private:
bool flipX = false;
bool flipY = false;
float pivotX = 0.0f;
float pivotY = 0.0f;
float pivotX_flip = 0.0f;
float pivotY_flip = 0.0f;
void CalculatePivotFlip();
};
struct Matrix {
float a = 1.0f;
float b = 0.0f;
float c = 0.0f;
float d = 1.0f;
float tx = 0.0f;
float ty = 0.0f;
void identity();
void translate(float dx, float dy);
void rotate(float radian);
void scale(float sx, float sy);
void concat(Matrix m);
};
// ############################################################################################
// Core
void CreateWindow(int w, int h, const char *str);
void AppUpdate();
void SetScene(Scene* scene);
Scene* GetScene();
// # Graphic
// ### GUI
void GuiInit();
void GuiDestroy();
void GuiBegin();
void GuiEnd();
// ### atlas
bool LoadTextureAtlas(const char *image_path, const char *ini_path, std::string AtlasKey);
bool UnloadTextureAtlas(std::string AtlasKey);
void DrawTextureAtlas(std::string AtlasKey, std::string key, int x, int y);
void DrawTextureAtlasPro(std::string AtlasKey, std::string key, int x, int y, Color color, bool flipX = false, bool flipY = false);
// ### draw
void DrawTextureTiled(Texture texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint);
void DrawRenderTexture(const RenderTexture &buffer, const float &x, const float &y, const Color &color);
// ### texture utils
Rectangle GetTextureRect(const Texture &tex, bool flipX, bool flipY);
// Utils
Random &GetDefaultRandom();
// Euclidean Distance
double distance(const Vector2 &p1, const Vector2 &p2);
double distanceSqrt(const Vector2 &p1, const Vector2 &p2);
// Manhattan Distance
double distanceManhattan(const Vector2 &p1, const Vector2 &p2);
}

View File

@ -1,8 +1,8 @@
#include <raylib.h> #include <raylib.h>
#include "core/Panda.hpp" #include "Panda.hpp"
static Scene* currentScene = nullptr; static panda::Scene* currentScene = nullptr;
// CORE // CORE
void panda::CreateWindow(int w, int h, const char *str) void panda::CreateWindow(int w, int h, const char *str)
@ -19,7 +19,6 @@ void panda::CreateWindow(int w, int h, const char *str)
SetWindowMinSize(w*0.8f, h*0.8f); SetWindowMinSize(w*0.8f, h*0.8f);
#endif #endif
// SetTargetFPS(60);
SetExitKey(-1); SetExitKey(-1);
} }
@ -40,7 +39,7 @@ void panda::SetScene(Scene* scene)
currentScene = scene; currentScene = scene;
} }
Scene* panda::GetScene() panda::Scene* panda::GetScene()
{ {
return currentScene; return currentScene;
} }

View File

@ -1,9 +1,10 @@
#include <imgui.h> #include <imgui.h>
#include <rlImGui.h> #include <rlImGui.h>
#include <rlgl.h> #include <rlgl.h>
#include <stdbool.h>
#include <raylib.h>
#include "core/Panda.hpp" #include "Panda.hpp"
#include "raylib.h"
#if PLATFORM_ANDROID #if PLATFORM_ANDROID
@ -88,6 +89,11 @@ void panda::DrawRenderTexture(const RenderTexture &buffer, const float &x, const
{x, y}, color); {x, y}, color);
} }
Rectangle panda::GetTextureRect(const Texture &tex, bool flipX, bool flipY)
{
return { 0.0f, 0.0f, (flipX ? -tex.width : tex.width)*1.0f, (flipY ? -tex.height : tex.height)*1.0f };
}
///////////////////////////////// /////////////////////////////////

View File

@ -1,60 +0,0 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include "core/Scene.hpp"
#include "utils/Random.hpp"
struct Texture;
struct RenderTexture;
struct Color;
struct Vector2;
namespace panda
{
struct TextureAtlasItem
{
int x;
int y;
int w;
int h;
float px;
float py;
};
// Core
void CreateWindow(int w, int h, const char *str);
void AppUpdate();
void SetScene(Scene* scene);
Scene* GetScene();
// Graphic
void GuiInit();
void GuiDestroy();
void GuiBegin();
void GuiEnd();
bool LoadTextureAtlas(const char *image_path, const char *ini_path, std::string AtlasKey);
bool UnloadTextureAtlas(std::string AtlasKey);
void DrawTextureAtlas(std::string AtlasKey, std::string key, int x, int y);
void DrawTextureAtlasPro(std::string AtlasKey, std::string key, int x, int y, Color color, bool flipX = false, bool flipY = false);
void DrawRenderTexture(const RenderTexture &buffer, const float &x, const float &y, const Color &color);
// Tilemap
// TilemapMetadata LoadTilemapMetadata(const char *file);
// TilemapItem LoadTilemap(const char *file, TilemapMetadata metadata, Texture texture);
// Utils
Random &GetDefaultRandom();
// Euclidean Distance
double distance(const Vector2 &p1, const Vector2 &p2);
double distanceSqrt(const Vector2 &p1, const Vector2 &p2);
// Manhattan Distance
double distanceManhattan(const Vector2 &p1, const Vector2 &p2);
}

View File

@ -1,8 +0,0 @@
#pragma once
class Scene {
public:
Scene() {}
virtual ~Scene() {}
virtual void update() {}
};

98
src/display/Sprite.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <raylib.h>
#include "Panda.hpp"
namespace panda {
void Sprite::Load(const char* path)
{
texture = new Texture(LoadTexture(path));
}
void Sprite::Unload()
{
UnloadTexture(*texture);
}
void Sprite::Draw(Color color)
{
Vector2 pivot_temp = (flipX || flipY) ? Vector2{pivotX_flip, pivotY_flip} : Vector2{pivotX, pivotY};
panda::Matrix matrix;
matrix.translate(-pivot_temp.x, -pivot_temp.y);
matrix.scale(scaleX, scaleY);
matrix.rotate(rotation * DEG2RAD);
matrix.translate(x, y);
DrawTexturePro(
*texture,
panda::GetTextureRect(*texture, flipX, flipY),
{
matrix.tx, matrix.ty,
scaleX*texture->width,
scaleY*texture->height
},
{0.0f, 0.0f},
rotation, color
);
}
void Sprite::SetPivot(Vector2 p)
{
pivotX = p.x;
pivotY = p.y;
CalculatePivotFlip();
}
void Sprite::SetPivotRatio(Vector2 ratio)
{
pivotX = texture->width*ratio.x;
pivotY = texture->height*ratio.y;
CalculatePivotFlip();
}
void Sprite::SetFlip(bool x, bool y)
{
flipX = x;
flipY = y;
CalculatePivotFlip();
}
void Sprite::SetFlipX(bool x)
{
flipX = x;
CalculatePivotFlip();
}
void Sprite::SetFlipY(bool y)
{
flipY = y;
CalculatePivotFlip();
}
Vector2 Sprite::GetPivot()
{
return {pivotX, pivotY};
}
Vector2 Sprite::GetPivotFlip()
{
return {pivotX_flip, pivotY_flip};
}
bool Sprite::GetFlipX()
{
return flipX;
}
bool Sprite::GetFlipY()
{
return flipY;
}
void Sprite::CalculatePivotFlip()
{
pivotX_flip = flipX ? texture->width-pivotX : pivotX;
pivotY_flip = flipY ? texture->height-pivotY : pivotY;
}
}

View File

@ -1,7 +1,8 @@
#include "core/Panda.hpp"
#include <cmath> #include <cmath>
#include <raylib.h> #include <raylib.h>
#include "Panda.hpp"
double panda::distance(const Vector2 &p1, const Vector2 &p2) double panda::distance(const Vector2 &p1, const Vector2 &p2)
{ {
double dx = p1.x - p2.x; double dx = p1.x - p2.x;

View File

@ -1,7 +1,7 @@
#include "utils/Easing.hpp"
#include <cmath> #include <cmath>
#include "utils/Easing.hpp"
namespace cur = easing; namespace cur = easing;
#ifndef PI #ifndef PI

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include "utils/RaylibDeprecated.hpp"
#include <cmath> #include <cmath>
#include <raylib.h> #include <raylib.h>
#include "utils/RaylibDeprecated.hpp"
class GridMap class GridMap
{ {
public: public:

66
src/utils/Matrix.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <cmath>
#include "Panda.hpp"
namespace panda {
void Matrix::identity()
{
a = 1.0f;
b = 0.0f;
c = 0.0f;
d = 1.0f;
tx = 0.0f;
ty = 0.0f;
}
void Matrix::translate(float dx, float dy)
{
Matrix m;
m.tx = dx;
m.ty = dy;
concat(m);
}
void Matrix::rotate(float radian)
{
float cos = std::cos(radian);
float sin = std::sin(radian);
float a1 = a * cos - b * sin;
b = a * sin + b * cos;
a = a1;
float c1 = c * cos - d * sin;
d = c * sin + d * cos;
c = c1;
float tx1 = tx * cos - ty * sin;
ty = tx * sin + ty * cos;
tx = tx1;
}
void Matrix::scale(float sx, float sy)
{
a *= sx;
b *= sy;
c *= sx;
d *= sy;
tx *= sx;
ty *= sy;
}
void Matrix::concat(Matrix m)
{
float a1 = a * m.a + b * m.c;
b = a * m.b + b * m.d;
a = a1;
float c1 = c * m.a + d * m.c;
d = c * m.b + d * m.d;
c = c1;
float tx1 = tx * m.a + ty * m.c + m.tx;
ty = tx * m.b + ty * m.d + m.ty;
tx = tx1;
}
}

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstddef>
#include <list> #include <list>
#include <vector> #include <vector>
#include <functional> #include <functional>
@ -48,13 +49,16 @@ namespace panda
// std::function<int()> custom; // std::function<int()> custom;
// }; // };
template<typename T> template<typename T, size_t SIZE>
class PoolPointer { class PoolPointer {
private: private:
std::list<T*> free; std::list<T*> free;
public: public:
PoolPointer() : free() { } PoolPointer() : free()
{
for (size_t i=0; i<SIZE; i++) free.emplace_back(new T());
}
T* get() { T* get() {
if (free.empty()) if (free.empty())
@ -75,4 +79,25 @@ namespace panda
inline void put(T* obj) { free.emplace_back(obj); } inline void put(T* obj) { free.emplace_back(obj); }
}; };
template<typename T, size_t SIZE>
class PoolCircular {
public:
T pool[SIZE];
PoolCircular() :
head(0)
{ }
T *Get()
{
head = (head + 1) % SIZE;
return pool[head];
}
size_t size() { return SIZE; }
int getHead() { return head; } // for debug only!!!
private:
int head;
};
} }

View File

@ -1,5 +1,7 @@
#include "utils/Random.hpp"
#include <chrono> #include <chrono>
#include <cmath>
#include "utils/Random.hpp"
panda::Random::Random(uint32_t _w, uint32_t _z) { panda::Random::Random(uint32_t _w, uint32_t _z) {
m_w = _w; m_w = _w;
@ -34,6 +36,13 @@ float panda::Random::range(float min, float max) {
return min + (max-min) * UNI(); return min + (max-min) * UNI();
} }
float panda::Random::rangeEx(float min, float max, float lamda) {
const float u = UNI();
return min + (max-min) * std::pow(u, lamda);
}
float panda::Random::xorshift_uni(uint32_t x) { float panda::Random::xorshift_uni(uint32_t x) {
return xorshift(x) * inv_uni; return xorshift(x) * inv_uni;
} }

View File

@ -19,6 +19,7 @@ namespace panda
uint32_t MWC(); uint32_t MWC();
float UNI(); float UNI();
float range(float min, float max); float range(float min, float max);
float rangeEx(float min, float max, float lamda);
float xorshift_uni(uint32_t x); float xorshift_uni(uint32_t x);
float xorshift64_uni(uint64_t x); float xorshift64_uni(uint64_t x);

View File

@ -1,7 +1,9 @@
#include "utils/RaylibDeprecated.hpp" #include <raylib.h>
#include "Panda.hpp"
// Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest. // Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
void DrawTextureTiled(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint) void DrawTextureTiled(Texture texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint)
{ {
if ((texture.id <= 0) || (scale <= 0.0f)) return; // Wanna see a infinite loop?!...just delete this line! if ((texture.id <= 0) || (scale <= 0.0f)) return; // Wanna see a infinite loop?!...just delete this line!
if ((source.width == 0) || (source.height == 0)) return; if ((source.width == 0) || (source.height == 0)) return;

View File

@ -1,4 +0,0 @@
#pragma once
#include <raylib.h>
void DrawTextureTiled(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint);

View File

@ -2,7 +2,7 @@
#include <inicpp.h> #include <inicpp.h>
#include <raylib.h> #include <raylib.h>
#include "core/Panda.hpp" #include "Panda.hpp"
namespace namespace
{ {