added rest of basics in TSE_Core

This commit is contained in:
2026-01-17 21:06:02 +01:00
parent d09953f476
commit 117c1e6adb
27 changed files with 2908 additions and 9 deletions

View File

@@ -0,0 +1,112 @@
#pragma once
constexpr int errorTexWidth = 20;
constexpr int errorTexHeight = 20;
constexpr int errorTexBpp = 32;
constexpr int errorTexChannels = 4;
const unsigned char errorTextureData[errorTexWidth * errorTexHeight * errorTexChannels] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255,
0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255,
0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255,
0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255,
0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
};

View File

@@ -0,0 +1,7 @@
#include "ImageAnimationSet.hpp"
TSE::ImageAnimationSet::ImageAnimationSet(std::vector<Sprite *> *s, std::string &name)
{
Sprites = *s;
Name = std::string(name);
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include <vector>
#include "Sprite.hpp"
#include "Types.hpp"
namespace TSE
{
struct ImageAnimationSet
{
public:
std::string Name = "";
std::vector<Sprite*> Sprites;
float frameTime = 0;
ImageAnimationSet() = default;
ImageAnimationSet(std::vector<Sprite*>* s, std::string& name);
};
} // namespace TSE

View File

@@ -0,0 +1,124 @@
#include "Scene.hpp"
void TSE::Scene::Render(IRenderer &rnd, const IWindow &wnd)
{
int counter = 1;
for(auto l : layers)
{
l.second->Render(rnd);
if(counter++ != layers.size())
{
rnd.End(); //OPTIMIZE:
//takes up 13,97% of function, but only needed, if there is more then one layer
//possible optimizations:
// -remove layers
// -make layer calculations, in shader
// -make an offset, that is calculated on cpu, and then commit everything at once
// now it is better because it is only done once per frame if only one shader is used, or textures are full, or more then one layers are used
rnd.Flush();
rnd.Begin();
wnd.ClearDepthBuffer();
}
}
}
void TSE::Scene::DoneRender()
{
IRenderer::camerasToRenderWith.clear();
}
void TSE::Scene::AddLayer(Layer *l)
{
layers.push_back(std::pair(l->GetName(), l));
}
void TSE::Scene::RemoveLayer(const string &name)
{
auto it = layers.begin();
for (int j = 0; j < layers.size(); j++)
{
if(it->first == name)
{
layers.erase(it);
break;
}
it++;
}
}
int TSE::Scene::GetLayerCount() const
{
return layers.size();
}
TSE::Layer *TSE::Scene::GetLayerAt(const int &i) const
{
auto it = layers.begin();
for (int j = 0; j < i; j++)
{
it++;
}
return (*it).second;
}
void TSE::Scene::SetName(const string &nname)
{
name = nname;
}
TSE::string TSE::Scene::GetName()
{
return name;
}
void TSE::Scene::Update()
{
for(auto l : layers)
{
l.second->Update();
}
}
void TSE::Scene::RenameLayer(const string &oldName, const string &newName)
{
for (int j = 0; j < layers.size(); j++)
{
if(layers[j].first == oldName)
{
layers[j].first = newName;
layers[j].second->SetName(newName);
break;
}
}
}
void TSE::Scene::MoveLayerUp(Layer *l)
{
auto it = layers.begin();
for (int j = 0; j < layers.size(); j++)
{
if(it->first == l->GetName())
{
std::swap(layers[j-1], layers[j]);
break;
}
it++;
}
}
void TSE::Scene::MoveLayerDown(Layer *l)
{
auto it = layers.begin();
for (int j = 0; j < layers.size(); j++)
{
if(it->first == l->GetName())
{
std::swap(layers[j+1], layers[j]);
break;
}
it++;
}
}

View File

@@ -0,0 +1,30 @@
#pragma once
#include <vector>
#include "Layer.hpp"
#include "interfaces/IWindow.hpp"
#include "Types.hpp"
namespace TSE
{
class Scene
{
private:
string name = "Unnamed";
std::vector<std::pair<string, Layer*>> layers;
public:
void Render(IRenderer& rnd, const IWindow& wnd);
void DoneRender();
void AddLayer(Layer* l);
void RemoveLayer(const string& name);
int GetLayerCount() const;
Layer* GetLayerAt(const int& i) const;
void SetName(const string& name);
string GetName();
void Update();
void RenameLayer(const string& oldName, const string& newName);
void MoveLayerUp(Layer* l);
void MoveLayerDown(Layer* l);
};
} // namespace TSE

View File

@@ -0,0 +1,26 @@
#include "Sprite.hpp"
TSE::Sprite::Sprite(Texture *tex, Rect bounds)
{
this->tex = tex;
uvBounds = bounds;
}
TSE::Texture *TSE::Sprite::GetTexture()
{
return tex;
}
TSE::Rect &TSE::Sprite::GetUVRect()
{
return uvBounds;
}
void TSE::Sprite::GetUVs(std::vector<Vector2> &uvs)
{
uvs.clear();
uvs.push_back(Vector2(uvBounds.p1));
uvs.push_back(Vector2(uvBounds.p2.x, uvBounds.p1.y));
uvs.push_back(Vector2(uvBounds.p2));
uvs.push_back(Vector2(uvBounds.p1.x, uvBounds.p2.y));
}

View File

@@ -0,0 +1,23 @@
#pragma once
#include "Rect.hpp"
namespace TSE
{
class Texture;
struct Sprite
{
private:
Texture* tex = nullptr;
Rect uvBounds = Rect(0,0,0,0);
public:
inline Sprite() {};
Sprite(Texture* tex, Rect bounds);
~Sprite() = default;
Texture* GetTexture();
Rect& GetUVRect();
void GetUVs(std::vector<Vector2>& uvs);
};
} // namespace TSE

View File

@@ -0,0 +1,359 @@
#include "Texture.hpp"
#include "Debug.hpp"
#include "Color.hpp"
#include "ErrorTextureData.hpp"
TSE::Texture::Texture(const string &path)
{
FREE_IMAGE_FORMAT fif = FREE_IMAGE_FORMAT::FIF_UNKNOWN;
bmp = nullptr;
imagePtr = nullptr;
Size = Vector2(0,0);
fif = FreeImage_GetFileType(path.c_str(), 0);
if(fif == FREE_IMAGE_FORMAT::FIF_UNKNOWN)
fif = FreeImage_GetFIFFromFilename(path.c_str());
if(fif == FREE_IMAGE_FORMAT::FIF_UNKNOWN)
{
TSE_ERROR("Failed to load image. Unsupported Format.");
Bpp = 24;
makeError(*this);
return;
}
if(FreeImage_FIFSupportsReading(fif))
{
bmp = FreeImage_Load(fif, path.c_str());
if(FreeImage_GetBPP(bmp) != 32)
{
auto tmpBmp = FreeImage_ConvertTo32Bits(bmp);
FreeImage_Unload(bmp);
bmp = tmpBmp;
}
}
if(bmp == nullptr)
{
TSE_ERROR("Failed to load image. Bitmap was nullptr.");
Bpp = 24;
makeError(*this);
return;
}
Bpp = FreeImage_GetBPP(bmp);
imagePtr = FreeImage_GetBits(bmp);
switch (Bpp)
{
case 32:
chanels = 4;
break;
case 24:
chanels = 3;
case 8:
chanels = 4;
}
Size = Vector2(FreeImage_GetWidth(bmp), FreeImage_GetHeight(bmp));
regist();
}
TSE::Texture::Texture(const int &width, const int &height, int bpp)
{
switch (bpp)
{
case 32:
chanels = 4;
break;
case 24:
chanels = 3;
case 8:
chanels = 4;
}
Bpp = bpp;
Size = Vector2(width, height);
bmp = FreeImage_Allocate(width, height, bpp);
imagePtr = FreeImage_GetBits(bmp);
regist();
}
TSE::Texture::Texture(const Vector2 &size, int bpp)
{
switch (bpp)
{
case 32:
chanels = 4;
break;
case 24:
chanels = 3;
case 8:
chanels = 4;
}
Bpp = bpp;
Size = size;
bmp = FreeImage_Allocate(size.x, size.y, bpp);
imagePtr = FreeImage_GetBits(bmp);
regist();
}
TSE::Texture::~Texture()
{
if(bmp != nullptr)
{
FreeImage_Unload(bmp);
bmp = nullptr;
}
if(TextureID != 0)
{
//glDeleteTextures(1, &TextureID); --> need to do in platform openGL
PlatformDestroy();
TextureID = 0;
}
}
TSE::uint TSE::Texture::bpp() const
{
return Bpp;
}
TSE::Vector2 TSE::Texture::size() const
{
return Size;
}
float TSE::Texture::width() const
{
return Size.x;
}
float TSE::Texture::height() const
{
return Size.y;
}
TSE::byte TSE::Texture::Chanels() const
{
return chanels;
}
TSE::byte *TSE::Texture::GetImagePtr() const
{
return imagePtr;
}
void TSE::Texture::SetPixel(const Vector2 &pos, const Color &c)
{
SetPixel(pos.x, pos.y, c);
}
void TSE::Texture::GetPixel(const Vector2 &pos, Color &c) const
{
GetPixel(pos.x, pos.y, c);
}
void TSE::Texture::SetPixelNoApply(const Vector2 &pos, const Color &c)
{
SetPixelNoApply(pos.x, pos.y, c);
}
void TSE::Texture::ToSprite(Sprite &s)
{
s = Sprite(this, Rect(0,0,1,1));
}
void TSE::Texture::SetChanels(const byte &ch)
{
chanels = ch;
}
uint TSE::Texture::GetTextureId() const
{
return TextureID;
}
void TSE::Texture::SetTextureId(uint id)
{
TextureID = id;
}
void TSE::Texture::SetPixel(const int &x, const int &y, const Color &c)
{
SetPixelNoApply(x,y,c);
Apply();
}
void TSE::Texture::GetPixel(const int &x, const int &y, Color &c) const
{
if(x >= width() || x < 0 ||y >= height() || y < 0)
{
TSE_WARNING("trying to access pixel outside of texture.\n pixel: (" + std::to_string(x) + ";" + std::to_string(y) + ")\nTexture size: (" + std::to_string(width()) + ";" + std::to_string(height()) );
return;
}
byte* pixel = getPixelPointer(x,y);
byte b = *pixel++;
byte g = *pixel++;
byte r = *pixel++;
byte a = *pixel++;
if(bpp() == 8)
c = Color(r,r,r);
else if(bpp() == 24)
c = Color(r,g,b,a);
else if(bpp() == 32)
c = Color(r,g,b);
}
void TSE::Texture::Fill(const Color &c)
{
for (int x = 0; x < width(); x++)
{
for (int y = 0; y < height(); y++)
{
SetPixelNoApply(x,y,c);
}
}
Apply();
}
void TSE::Texture::SetPixelNoApply(const int &x, const int &y, const Color &c)
{
if(x >= width() || x < 0 ||y >= height() || y < 0)
{
TSE_WARNING("trying to access pixel outside of texture.\n pixel: (" + std::to_string(x) + ";" + std::to_string(y) + ")\nTexture size: (" + std::to_string(width()) + ";" + std::to_string(height()) );
return;
}
byte* pixel = getPixelPointer(x,y);
if(chanels == 4 && bpp() == 8)
{
byte r2bit = static_cast<byte>(c.r * 3.0f);
byte g2bit = static_cast<byte>(c.g * 3.0f);
byte b2bit = static_cast<byte>(c.b * 3.0f);
byte a2bit = static_cast<byte>(c.a * 3.0f);
byte result = (r2bit << 6) | (g2bit << 4) | (b2bit << 2) | a2bit;
*pixel++ = result;
return;
}
*pixel++ = c.B();
if(bpp() > 8)
{
*pixel++ = c.G();
*pixel++ = c.R();
if(bpp() > 24)
*pixel++ = c.A();
}
}
void TSE::Texture::AddPixelNoApply(const int &x, const int &y, const Color &c)
{
if(x >= width() || x < 0 ||y >= height() || y < 0)
{
TSE_WARNING("trying to access pixel outside of texture.\n pixel: (" + std::to_string(x) + ";" + std::to_string(y) + ")\nTexture size: (" + std::to_string(width()) + ";" + std::to_string(height()) );
return;
}
byte* pixel = getPixelPointer(x,y);
if(chanels == 4 && bpp() == 8) //TODO add propper adding, becouse currently is only setting
{
byte r2bit = static_cast<byte>(c.r * 3.0f);
byte g2bit = static_cast<byte>(c.g * 3.0f);
byte b2bit = static_cast<byte>(c.b * 3.0f);
byte a2bit = static_cast<byte>(c.a * 3.0f);
byte result = (r2bit << 6) | (g2bit << 4) | (b2bit << 2) | a2bit;
*pixel++ = result;
return;
}
Color bc(pixel[2], pixel[1], pixel[0], pixel[3]);
bc = c + bc;
*pixel++ = bc.B();
if(bpp() > 8)
{
*pixel++ = bc.G();
*pixel++ = bc.R();
if(bpp() > 24)
*pixel++ = bc.A();
}
}
void TSE::Texture::Recreate(const int &x, const int &y, const int &bpp, const int &chanels)
{
if(bmp != nullptr)
{
FreeImage_Unload(bmp);
bmp = nullptr;
}
if(TextureID != 0)
{
PlatformDestroy();
TextureID = 0;
}
this->chanels = chanels;
Bpp = bpp;
Size = Vector2(x, y);
bmp = FreeImage_Allocate(x, y, bpp);
imagePtr = FreeImage_GetBits(bmp);
regist();
}
void TSE::Texture::SavePNG(const std::string &path) const
{
if(!FreeImage_Save(FREE_IMAGE_FORMAT::FIF_PNG, bmp, path.c_str(), PNG_Z_NO_COMPRESSION))
TSE_ERROR("Failed to save Image to \"" + path + "\"");
}
TSE::byte *TSE::Texture::getPixelPointer(const int &x, const int &y) const
{
int alphaoffset = y * 2;
if(bpp() > 24)
alphaoffset = 0;
int offset = ((y * width() + x) * (bpp() / 8) + alphaoffset);
return imagePtr + offset;
}
void TSE::Texture::makeError(Texture &tex)
{
tex.Bpp = errorTexBpp;
tex.chanels = errorTexChannels;
tex.Size = Vector2(errorTexWidth, errorTexHeight);
tex.bmp = FreeImage_ConvertFromRawBits(
const_cast<BYTE*>(errorTextureData), // wichtig: cast!
errorTexWidth,
errorTexHeight,
errorTexWidth * errorTexChannels,
errorTexBpp,
FI_RGBA_RED_MASK,
FI_RGBA_GREEN_MASK,
FI_RGBA_BLUE_MASK,
true // top-down
);
tex.imagePtr = FreeImage_GetBits(tex.bmp);
tex.regist();
}
void TSE::Texture::bind() const
{
helper->Bind(this);
}
void TSE::Texture::unbind() const
{
helper->UnBind(this);
}
void TSE::Texture::Apply()
{
helper->Apply(this);
}
void TSE::Texture::regist()
{
helper->Regist(this);
}
void TSE::Texture::PlatformDestroy()
{
helper->PlatromDestroy(this);
}

View File

@@ -0,0 +1,62 @@
#pragma once
#include "Sprite.hpp"
#include "interfaces/ITexture.hpp"
#include "Types.hpp"
#include "Vector2.hpp"
#include "interfaces/ITextureHelper.hpp"
#define FREEIMAGE_LIB
#include "FI/FreeImage.h"
namespace TSE
{
class Texture : public ITexture
{
protected:
uint TextureID = 0;
Vector2 Size;
uint Bpp;
byte chanels = 0;
FIBITMAP* bmp = nullptr;
byte* imagePtr = nullptr;
public:
string name = "Unnamed";
static ITextureHelper* helper;
Texture(const string& path);
Texture(const int& width, const int& height, int bpp = 32);
Texture(const Vector2& size, int bpp = 32);
~Texture();
uint bpp() const;
Vector2 size() const override;
float width() const override;
float height() const override;
byte Chanels() const;
byte* GetImagePtr() const;
void SetPixel(const Vector2& pos, const Color& c);
void GetPixel(const Vector2& pos, Color& c) const;
void SetPixelNoApply(const Vector2& pos, const Color& c);
void ToSprite(Sprite& s);
void SetChanels(const byte& ch);
uint GetTextureId() const override;
void SetTextureId(uint id);
void SetPixel(const int& x, const int& y, const Color& c);
void GetPixel(const int& x, const int& y, Color& c) const;
void Fill(const Color& c);
void SetPixelNoApply(const int& x, const int& y, const Color& c);
void AddPixelNoApply(const int& x, const int& y, const Color& c);
void Recreate(const int& x, const int& y, const int& bpp, const int& chanels);
void SavePNG(const std::string& path) const;
byte* getPixelPointer(const int& x, const int& y) const;
static void makeError(Texture& tex);
void bind() const;
void unbind() const;
void Apply();
void regist();
void PlatformDestroy();
};
} // namespace TSE

View File

@@ -0,0 +1,35 @@
#include "TileSet.hpp"
#include "Debug.hpp"
#define PADDING 0.0f
TSE::TileSet::TileSet(Texture *tex, int x, int y)
{
SetTexture(tex);
SetCount(x,y);
}
void TSE::TileSet::SetCount(int x, int y)
{
resx = x;
resy = y;
}
void TSE::TileSet::SetTexture(Texture *tex)
{
this->tex = tex;
}
void TSE::TileSet::GetSpriteAt(int x, int y, Sprite &s)
{
if(x < 0 || x >= resx || y < 0 || y >= resy)
{
TSE_ERROR("The sprite you are trying to access is out of range");
return;
}
Vector2 startpos = Vector2(((tex->size().x / resx) * (x + PADDING)) / tex->size().x, ((tex->size().y / resy) * (y + PADDING)) / tex->size().y);
Vector2 endpos = Vector2(((tex->size().x / resx) * ((x + 1) - PADDING)) / tex->size().x, ((tex->size().y / resy) * ((y + 1) - PADDING)) / tex->size().y);
s = Sprite(tex, Rect(startpos, endpos));
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include "Texture.hpp"
#include "Sprite.hpp"
namespace TSE
{
class TileSet
{
private:
Texture* tex = nullptr;
int resx = 0, resy = 0;
public:
TileSet(Texture* tex, int x, int y);
void SetCount(int x, int y);
void SetTexture(Texture* tex);
void GetSpriteAt(int x, int y, Sprite& s);
inline void SetCount(Vector2& v)
{
SetCount(v.x, v.y);
};
inline void GetSpriteAt(Vector2& v, Sprite& s)
{
GetSpriteAt(v.x, v.y, s);
};
};
} // namespace TSE