Compare commits
2 Commits
d429d21c4f
...
638b16a51c
Author | SHA1 | Date | |
---|---|---|---|
638b16a51c | |||
58eb5044b1 |
@ -51,7 +51,9 @@ set(PROJECT_INCLUDE
|
||||
|
||||
set(PROJECT_LIBRARY raylib)
|
||||
|
||||
set(PROJECT_DEFINITION "_PANDA_CORE_")
|
||||
set(PROJECT_DEFINITION
|
||||
"_PANDA_CORE_"
|
||||
NO_FONT_AWESOME)
|
||||
|
||||
##############################################################
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
struct Vector2;
|
||||
@ -56,16 +57,6 @@ struct Camera2D {
|
||||
|
||||
namespace panda
|
||||
{
|
||||
struct TextureAtlasItem
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
int h;
|
||||
float px;
|
||||
float py;
|
||||
};
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene() {}
|
||||
@ -77,28 +68,27 @@ namespace panda
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
int width;
|
||||
int height;
|
||||
int offsetX;
|
||||
int offsetY;
|
||||
int frameX;
|
||||
int frameY;
|
||||
int frameW;
|
||||
int frameH;
|
||||
float pivotX;
|
||||
float pivotY;
|
||||
bool isRotated;
|
||||
};
|
||||
|
||||
class TextureMap {
|
||||
public:
|
||||
Texture *texture;
|
||||
std::map<const char*, Sprite> map;
|
||||
|
||||
void SetPivot(Vector2 ratio);
|
||||
void SetPivotRatio(Vector2 ratio);
|
||||
void SetFlip(bool x, bool y);
|
||||
void SetFlipX(bool x);
|
||||
void SetFlipY(bool y);
|
||||
|
||||
Vector2 GetPivot() const;
|
||||
Vector2 GetPivotFlip() const;
|
||||
bool GetFlipX() const;
|
||||
bool GetFlipY() const;
|
||||
|
||||
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();
|
||||
TextureMap() {}
|
||||
virtual ~TextureMap() {}
|
||||
virtual void Init() {}
|
||||
};
|
||||
|
||||
struct Matrix {
|
||||
@ -153,12 +143,6 @@ namespace panda
|
||||
void GuiThemeSetup();
|
||||
#endif
|
||||
|
||||
// ### 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);
|
||||
|
||||
// ### gridmap
|
||||
// Draw GridMap using cam, use this inside BeginMode2D
|
||||
void DrawGridMap(Texture texture, Camera2D cam);
|
||||
@ -171,10 +155,15 @@ namespace panda
|
||||
void DrawRenderTexture(const RenderTexture &buffer, const float &x, const float &y, const Color &color);
|
||||
|
||||
// ## sprite
|
||||
void DrawSprite(const panda::Sprite &sprite, const float &x, const float &y, const Color &color);
|
||||
void DrawSpriteV(const panda::Sprite &sprite, const Vector2 &pos, const Color &color);
|
||||
void DrawSpriteEx(const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation, const Color &color);
|
||||
Vector2 GetPointInSprite(const Vector2 &point, const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation);
|
||||
void DrawSprite(Texture &texture, const panda::Sprite &sprite, const int &x, const int &y, const Color &color);
|
||||
void DrawSpriteV(Texture &texture, const panda::Sprite &sprite, const Vector2 &pos, const Color &color);
|
||||
void DrawSpriteEx(Texture &texture, const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation, bool flipX, bool flipY, const Color &color);
|
||||
|
||||
void DrawTextureMap(const panda::TextureMap &atlas, const char* texture, const int &x, const int &y, const Color &color);
|
||||
void DrawTextureMapV(const panda::TextureMap &atlas, const char* texture, const Vector2 &pos, const Color &color);
|
||||
void DrawTextureMapEx(const panda::TextureMap &atlas, const char* texture, const Vector2 &pos, const Vector2 &scale, const float &rotation, bool flipX, bool flipY, const Color &color);
|
||||
|
||||
// Vector2 GetPointInSprite(const Vector2 &point, const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation);
|
||||
|
||||
// ### texture utils
|
||||
Rectangle GetTextureRect(const Texture &tex, bool flipX, bool flipY);
|
||||
|
@ -1,107 +0,0 @@
|
||||
#include <raylib.h>
|
||||
|
||||
#include "Panda.hpp"
|
||||
|
||||
|
||||
namespace panda {
|
||||
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() const
|
||||
{
|
||||
return {pivotX, pivotY};
|
||||
}
|
||||
|
||||
Vector2 Sprite::GetPivotFlip() const
|
||||
{
|
||||
return {pivotX_flip, pivotY_flip};
|
||||
}
|
||||
|
||||
bool Sprite::GetFlipX() const
|
||||
{
|
||||
return flipX;
|
||||
}
|
||||
|
||||
bool Sprite::GetFlipY() const
|
||||
{
|
||||
return flipY;
|
||||
}
|
||||
|
||||
void Sprite::CalculatePivotFlip()
|
||||
{
|
||||
pivotX_flip = flipX ? texture->width-pivotX : pivotX;
|
||||
pivotY_flip = flipY ? texture->height-pivotY : pivotY;
|
||||
}
|
||||
|
||||
void DrawSprite(const panda::Sprite &sprite, const float &x, const float &y, const Color &color)
|
||||
{
|
||||
DrawSpriteEx(sprite, {x, y}, {1.0f, 1.0f}, 0.0f, color);
|
||||
}
|
||||
|
||||
void DrawSpriteV(const panda::Sprite &sprite, const Vector2 &pos, const Color &color)
|
||||
{
|
||||
DrawSpriteEx(sprite, pos, {1.0f, 1.0f}, 0.0f, color);
|
||||
}
|
||||
|
||||
void DrawSpriteEx(const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation, const Color &color)
|
||||
{
|
||||
Vector2 dest = GetPointInSprite({0.0f, 0.0f}, sprite, pos, scale, rotation);
|
||||
|
||||
DrawTexturePro(
|
||||
*sprite.texture,
|
||||
panda::GetTextureRect(*sprite.texture, sprite.GetFlipX(), sprite.GetFlipY()),
|
||||
{
|
||||
dest.x, dest.y,
|
||||
scale.x*sprite.texture->width,
|
||||
scale.y*sprite.texture->height
|
||||
},
|
||||
{0.0f, 0.0f},
|
||||
rotation, color
|
||||
);
|
||||
}
|
||||
|
||||
Vector2 GetPointInSprite(const Vector2 &point, const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation)
|
||||
{
|
||||
Vector2 pivot_temp = (sprite.GetFlipX() || sprite.GetFlipY()) ? sprite.GetPivotFlip() : sprite.GetPivot();
|
||||
|
||||
panda::Matrix matrix;
|
||||
matrix.translate(-pivot_temp.x, -pivot_temp.y);
|
||||
matrix.translate(point.x, point.y);
|
||||
matrix.scale(scale.x, scale.y);
|
||||
matrix.rotate(rotation * DEG2RAD);
|
||||
matrix.translate(pos.x, pos.y);
|
||||
|
||||
return { matrix.tx, matrix.ty };
|
||||
}
|
||||
|
||||
}
|
@ -1,81 +1,63 @@
|
||||
#include "Panda.hpp"
|
||||
#include <raylib.h>
|
||||
#include <sstream>
|
||||
#include <inicpp.h>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace
|
||||
void panda::DrawSprite(Texture &texture, const panda::Sprite &sprite, const int &x, const int &y, const Color &color)
|
||||
{
|
||||
struct TextureAtlas
|
||||
{
|
||||
Texture2D texture;
|
||||
std::unordered_map<std::string, panda::TextureAtlasItem> items;
|
||||
};
|
||||
DrawSpriteEx(texture, sprite, {(float)x, (float)y}, {1.0f, 1.0f}, 0.0f, false, false, color);
|
||||
}
|
||||
|
||||
static std::unordered_map<std::string, TextureAtlas> TextureAtlasMap;
|
||||
|
||||
bool panda::LoadTextureAtlas(const char *image_path, const char *ini_path, std::string AtlasKey)
|
||||
void panda::DrawSpriteV(Texture &texture, const panda::Sprite &sprite, const Vector2 &pos, const Color &color)
|
||||
{
|
||||
|
||||
ini::IniFile ini;
|
||||
TextureAtlas atlas;
|
||||
|
||||
char* text = LoadFileText(ini_path);
|
||||
ini.decode(text);
|
||||
|
||||
// load texture
|
||||
atlas.texture = LoadTexture(image_path);
|
||||
|
||||
// loop item
|
||||
std::stringstream ss(ini["meta"]["list"].as<const char*>());
|
||||
std::string item;
|
||||
while (std::getline(ss, item, ';')) {
|
||||
atlas.items.insert({item, TextureAtlasItem{
|
||||
ini[item]["x"].as<int>(),
|
||||
ini[item]["y"].as<int>(),
|
||||
ini[item]["w"].as<int>(),
|
||||
ini[item]["h"].as<int>(),
|
||||
ini[item]["px"].as<float>(),
|
||||
ini[item]["py"].as<float>(),
|
||||
}});
|
||||
}
|
||||
|
||||
UnloadFileText(text);
|
||||
|
||||
TextureAtlasMap.insert({AtlasKey, atlas});
|
||||
return true;
|
||||
DrawSpriteEx(texture, sprite, pos, {1.0f, 1.0f}, 0.0f, false, false, color);
|
||||
}
|
||||
|
||||
bool panda::UnloadTextureAtlas(std::string AtlasKey)
|
||||
void panda::DrawSpriteEx(Texture &texture, const panda::Sprite &sprite, const Vector2 &pos, const Vector2 &scale, const float &rotation, bool flipX, bool flipY, const Color &color)
|
||||
{
|
||||
// looks like its already clear automatically (when close window)
|
||||
// just incase if it not when changing scene
|
||||
if (TextureAtlasMap.count(AtlasKey))
|
||||
{
|
||||
TextureAtlas &atlas = TextureAtlasMap.at(AtlasKey);
|
||||
UnloadTexture(atlas.texture);
|
||||
TextureAtlasMap.erase(AtlasKey);
|
||||
}
|
||||
return true;
|
||||
panda::Matrix m;
|
||||
|
||||
if (sprite.isRotated)
|
||||
m.translate(0, sprite.height);
|
||||
|
||||
m.translate(
|
||||
((flipX) ? -1+sprite.pivotX : -sprite.pivotX) *sprite.width,
|
||||
((flipY) ? -1+sprite.pivotY : -sprite.pivotY) *sprite.height);
|
||||
|
||||
m.translate(
|
||||
((flipX) ? -1 : 1) * sprite.offsetX,
|
||||
((flipY) ? -1 : 1) * sprite.offsetY);
|
||||
|
||||
m.scale(scale.x, scale.y);
|
||||
m.rotate(rotation * DEG2RAD);
|
||||
m.translate(pos.x, pos.y);
|
||||
|
||||
DrawTexturePro(
|
||||
texture,
|
||||
{
|
||||
(float)sprite.frameX, (float)sprite.frameY,
|
||||
((flipY) ? -1.0f : 1.0f) * sprite.frameW,
|
||||
((flipY) ? -1.0f : 1.0f) * sprite.frameH
|
||||
},
|
||||
{
|
||||
m.tx, m.ty,
|
||||
(sprite.isRotated) ? sprite.height*scale.y : sprite.width*scale.x,
|
||||
(sprite.isRotated) ? sprite.width*scale.x : sprite.height*scale.y
|
||||
},
|
||||
{0.0f, 0.0f},
|
||||
(sprite.isRotated) ? rotation-90 : rotation,
|
||||
color);
|
||||
}
|
||||
|
||||
void panda::DrawTextureAtlas(std::string AtlasKey, std::string key, int x, int y)
|
||||
void panda::DrawTextureMap(const panda::TextureMap &atlas, const char* texture, const int &x, const int &y, const Color &color)
|
||||
{
|
||||
DrawTextureAtlasPro(AtlasKey, key, x, y, WHITE);
|
||||
DrawSpriteEx(*atlas.texture, atlas.map.at(texture), {(float)x, (float)y}, {1.0f, 1.0f}, 0.0f, false, false, color);
|
||||
}
|
||||
|
||||
void panda::DrawTextureAtlasPro(std::string AtlasKey, std::string key, int x, int y, Color color, bool flipX, bool flipY)
|
||||
void panda::DrawTextureMapV(const panda::TextureMap &atlas, const char* texture, const Vector2 &pos, const Color &color)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void panda::DrawTextureMapEx(const panda::TextureMap &atlas, const char* texture, const Vector2 &pos, const Vector2 &scale, const float &rotation, bool flipX, bool flipY, const Color &color)
|
||||
{
|
||||
TextureAtlas &atlas = TextureAtlasMap.at(AtlasKey);
|
||||
TextureAtlasItem &item = atlas.items.at(key);
|
||||
|
||||
float _flipX = (flipX) ? -1.0f : 1.0f;
|
||||
float _flipY = (flipY) ? -1.0f : 1.0f;
|
||||
|
||||
// todo flip position
|
||||
DrawTextureRec(
|
||||
atlas.texture,
|
||||
{(float)item.x, (float)item.y, item.w*_flipX, item.h*_flipY},
|
||||
{x - item.px*item.w, y - item.py*item.h}, color);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user