Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions libzhl/IsaacRepentance_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,40 +621,47 @@ void Entity_Bomb::UpdateDirtColor() {
}
}

void DestinationQuad::RotateRadians(const Vector& pivot, float radians)
inline void SourceQuad::ConvertToPixelSpace(KAGE_Graphics_ImageBase& image)
{
if (radians == 0.0)
if (this->_coordinateSpace == eCoordinateSpace::PIXEL)
{
return;
}

float sin = std::sin(radians);
float cos = std::cos(radians);

// translate
_topLeft -= pivot;
_topRight -= pivot;
_bottomLeft -= pivot;
_bottomRight -= pivot;

// apply rotation
auto rotate = [](auto& p, float sin, float cos) {
float x = p.x;
float y = p.y;
p.x = cos * x - sin * y;
p.y = sin * x + cos * y;
};

rotate(_topLeft, sin, cos);
rotate(_topRight, sin, cos);
rotate(_bottomLeft, sin, cos);
rotate(_bottomRight, sin, cos);

// undo translation
_topLeft += pivot;
_topRight += pivot;
_bottomLeft += pivot;
_bottomRight += pivot;
float width = (float)image.GetWidth();
float height = (float)image.GetHeight();

this->_topLeft.x *= width;
this->_topLeft.y *= height;
this->_topRight.x *= width;
this->_topRight.y *= height;
this->_bottomLeft.x *= width;
this->_bottomLeft.y *= height;
this->_bottomRight.x *= width;
this->_bottomRight.y *= height;
}

inline void SourceQuad::ConvertToUVSpace(KAGE_Graphics_ImageBase& image)
{
if (this->_coordinateSpace == eCoordinateSpace::NORMALIZED_UV)
{
return;
}

int width = image.GetWidth();
int height = image.GetHeight();

float inverseWidth = 1.0f / (float)width;
float inverseHeight = 1.0f / (float)height;

this->_topLeft.x *= inverseWidth;
this->_topLeft.y *= inverseHeight;
this->_topRight.x *= inverseWidth;
this->_topRight.y *= inverseHeight;
this->_bottomLeft.x *= inverseWidth;
this->_bottomLeft.y *= inverseHeight;
this->_bottomRight.x *= inverseWidth;
this->_bottomRight.y *= inverseHeight;
}

ModReference* LuaEngine::GetModRefByTable(int tblIdx)
Expand Down
99 changes: 95 additions & 4 deletions libzhl/functions/DestinationQuad.zhl
Original file line number Diff line number Diff line change
@@ -1,24 +1,115 @@
struct DestinationQuad depends (Vector) { {{
void LIBZHL_API DestinationQuad::RotateRadians(const Vector& pivot, float radians);
{{
struct RenderMatrix
{
float a = 1.0f, b = 0.0f, tx = 0.0f;
float c = 0.0f, d = 1.0f, ty = 0.0f;
};
}}

inline void DestinationQuad::FlipX()
struct DestinationQuad depends (Vector) { {{
inline void FlipX()
{
std::swap(_topLeft, _topRight);
std::swap(_bottomLeft, _bottomRight);
}

inline void DestinationQuad::FlipY()
inline void FlipY()
{
std::swap(_topLeft, _bottomLeft);
std::swap(_topRight, _bottomRight);
}

inline void Translate(const Vector& offset)
{
this->_topLeft += offset;
this->_topRight += offset;
this->_bottomLeft += offset;
this->_bottomRight += offset;
}

inline void Scale(const Vector& scale, const Vector& anchor)
{
this->Translate(-anchor);

auto scale_point = [&](Vector& vec) {
vec.x *= scale.x;
vec.y *= scale.y;
};

scale_point(this->_topLeft);
scale_point(this->_topRight);
scale_point(this->_bottomLeft);
scale_point(this->_bottomRight);

this->Translate(anchor);
}

inline void RotateRadians(const Vector& pivot, float radians)
{
float sin = std::sin(radians);
float cos = std::cos(radians);

this->Translate(-pivot);

auto rotate = [](auto& p, float sin, float cos) {
float x = p.x;
float y = p.y;
p.x = cos * x - sin * y;
p.y = sin * x + cos * y;
};

rotate(_topLeft, sin, cos);
rotate(_topRight, sin, cos);
rotate(_bottomLeft, sin, cos);
rotate(_bottomRight, sin, cos);

this->Translate(pivot);
}

inline void RotateDegrees(const Vector& pivot, float degrees)
{
constexpr float DEGREES_TO_RADIANS = 3.14159265358979323846f / 180.0f;
float radians = degrees * DEGREES_TO_RADIANS;
RotateRadians(pivot, radians);
}

inline void Shear(const Vector& shear, const Vector& anchor)
{
this->Translate(-anchor);

auto shear_point = [&](Vector& vec) {
float x = vec.x;
float y = vec.y;
vec.x += shear.x * y;
vec.y += shear.y * x;
};

shear_point(this->_topLeft);
shear_point(this->_topRight);
shear_point(this->_bottomLeft);
shear_point(this->_bottomRight);

this->Translate(anchor);
}

inline void ApplyMatrix(const RenderMatrix& matrix, const Vector& anchor)
{
this->Translate(-anchor);

auto apply_matrix = [](Vector& v, const RenderMatrix& m) {
float x = v.x;
float y = v.y;
v.x = (m.a * x + m.b * y + m.tx);
v.y = (m.c * x + m.d * y + m.ty);
};

apply_matrix(this->_topLeft, matrix);
apply_matrix(this->_topRight, matrix);
apply_matrix(this->_bottomLeft, matrix);
apply_matrix(this->_bottomRight, matrix);

this->Translate(anchor);
}
}}
Vector _topLeft : 0x0, _topRight : 0x8, _bottomLeft : 0x10, _bottomRight : 0x18;
} : 0x20;
1 change: 1 addition & 0 deletions libzhl/functions/Global.zhl
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,7 @@ enum class eImageFlag : uint64_t {
};

enum class eVertexAttributeFormat : int {
TERMINATOR = 0, // Used to define the end of the VertexAttributeDescriptor array
FLOAT = 1,
VEC_2 = 2,
VEC_3 = 3,
Expand Down
15 changes: 14 additions & 1 deletion libzhl/functions/KAGE_Graphics_ImageManager.zhl
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
struct KAGE_Graphics_ImageManager {
struct KAGE_Graphics_ImageManager depends (KAGE_SmartPointer_ImageBase, KColor) {
vector_ImagePtr _frameImages : 0x10;
vector_pair_RenderBatchPtr_ImagePtr _transparentBatches : 0x28;

{{
static KAGE_SmartPointer_ImageBase CreateProceduralImage(uint32_t width, uint32_t height, const char* name, KColor& color)
{
KAGE_SmartPointer_ImageBase result;
CreateProceduralImage_Internal(&result, width, height, nullptr, name, 0, 0, color);
return result;
}
}}
} : 0x3c;

"538bdc83ec0883e4f883c404558b6b??896c24??8bec83ec30568b35":
__thiscall void KAGE_Graphics_ImageManager::apply_frame_images();

// Libzhl doesn't push the implicit_output on it's own. The function assumes the smart pointer is not initialized so the helper is created to avoid potential problems.
"558bec6aff68????????64a1????????5083ec14535657a1????????33c5508d45??64a3????????c745??00000000c745??00000000":
static KAGE_SmartPointer_ImageBase* KAGE_Graphics_ImageManager::CreateProceduralImage_Internal(KAGE_SmartPointer_ImageBase* implicit_output, uint32_t width, uint32_t height, void* unused_1, const char* name, uint32_t unused_2, uint32_t unused_3, KColor color);

"a1(????????)8d0c??8b34":
reference KAGE_Graphics_ImageManager g_KAGE_Graphics_ImageManager;
2 changes: 1 addition & 1 deletion libzhl/functions/KAGE_Graphics_Manager_GL.zhl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct KAGE_Graphics_Manager_GL depends (KAGE_Graphics_VertexAttributeDescriptor, KAGE_Graphics_Shader) {};

"538bdc83ec0883e4f883c404558b6b??896c24??8bec6aff68????????64a1????????505381ec80000000a1????????33c58945??5657508d45??64a3????????8995":
static cleanup void KAGE_Graphics_Manager_GL::LoadShader(KAGE_Graphics_Shader *shader<ecx>, KAGE_Graphics_VertexAttributeDescriptor *descriptor<edx>, const char* shaderPath);
static cleanup void KAGE_Graphics_Manager_GL::LoadShader(KAGE_Graphics_Shader *shader<ecx>, const KAGE_Graphics_VertexAttributeDescriptor *descriptor<edx>, const char* shaderPath);
51 changes: 45 additions & 6 deletions libzhl/functions/KAGE_Graphics_Shader.zhl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,60 @@ reference KAGE_Graphics_Shader* g_CurrentShader;
reference KAGE_Graphics_Shader* g_AllShaders;

"c705????????(????????)c705????????ffffffffc605????????00c705????????000000000f2905":
reference void* KAGE_Graphics_ShaderBase_vftable;
reference void* KAGE_Graphics_Shader_vftable;

struct KAGE_Graphics_ShaderBase {
{{
KAGE_Graphics_ShaderBase() : _vtable(__ptr_KAGE_Graphics_ShaderBase_vftable) {};
// ShaderBase has pure methods and so it should not be instantiable
protected:
KAGE_Graphics_ShaderBase() : _vtable(__ptr_KAGE_Graphics_Shader_vftable) {
std::memset((void*)((uintptr_t)this + 0x4), 0, sizeof(*this) - 0x4); // the constructor would normally set everything to 0 so we emulate that here
};

public:
~KAGE_Graphics_ShaderBase() {
this->Free(false);
}
}}

__vtable {
pure void Free(bool deletePtr);
skip; // Initialize
pure void Shutdown();
};

void* _vtable : 0x0;
KAGE_Graphics_ImageBase_VertexAttributeDescriptor* _vertexAttributes : 0x8;
bool _initialized : 0x4;
KAGE_Graphics_VertexAttributeDescriptor* _vertexAttributes : 0x8;
uint32_t _numVertexAttributes : 0xc;
} : 0x28;

struct KAGE_Graphics_Shader depends (KAGE_Graphics_ShaderBase) {
struct KAGE_Graphics_Shader : public KAGE_Graphics_ShaderBase {
{{
inline int GetShaderId() { return *(int*)((char*)this + 0x28); }
KAGE_Graphics_Shader() {
_vtable = __ptr_KAGE_Graphics_Shader_vftable;
this->_glProgram = -1;
}

~KAGE_Graphics_Shader() {
if (this->_initialized)
{
// We must call Shutdown manually, as the Free method only calls the base class Shutdown, causing _glProgram to leak.
// _glProgram is not reset to a default value on Shutdown, so we can't be sure that we are not deleting an already deleted glProgram,
// but based on usage the only time glDeleteProgram is called is on Shutdown (where _initialized is set to false),
// and when Initialize fails (where _initialized is left as false).
this->Shutdown();
}
}

// Use this if you want to reload the shader, as none of the methods gracefully handle a double initialization, causing some resources to leak.
void HardReset()
{
this->~KAGE_Graphics_Shader();
new (this) KAGE_Graphics_Shader();
}

inline int GetShaderId() { return this->_glProgram; }
}}
KAGE_Graphics_ShaderBase _shaderBase : 0x0;
unsigned int _glProgram : 0x28;
} : 0x2c;
17 changes: 16 additions & 1 deletion libzhl/functions/KAGE_Graphics_Vertex.zhl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ struct KAGE_Graphics_RenderBatch depends (KAGE_Graphics_GraphicsBufferObject) {
KAGE_Graphics_GraphicsBufferObject _indexBuffer : 0x30;
} : 0x3c; // (checked)

struct KAGE_Graphics_VertexAttributeDescriptor {};
struct KAGE_Graphics_VertexAttributeDescriptor {
const char* name : 0x0;
uint32_t format : 0x4; // eVertexFormat

{{
bool operator==(const KAGE_Graphics_VertexAttributeDescriptor& other) const
{
return std::strcmp(this->name, other.name) == 0 && this->format == other.format;
}

bool operator!=(const KAGE_Graphics_VertexAttributeDescriptor& other) const
{
return !(*this == other);
}
}}
} : 0x8;
14 changes: 13 additions & 1 deletion libzhl/functions/SourceQuad.zhl
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
struct SourceQuad depends(Vector, DestinationQuad) : public DestinationQuad {
int __coordinateSpaceEnum : 0x20;
int _coordinateSpace : 0x20;

{{
enum eCoordinateSpace
{
PIXEL = 0,
NORMALIZED_UV = 1,
};

LIBZHL_API void ConvertToPixelSpace(KAGE_Graphics_ImageBase& image);
LIBZHL_API void ConvertToUVSpace(KAGE_Graphics_ImageBase& image);
}}

} : 0x24;
4 changes: 4 additions & 0 deletions libzhl/functions/Vector2.zhl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ struct Vector { {{
Vector() : x(0.f), y(0.f) {}
Vector(float _x, float _y) : x(_x), y(_y) {}

Vector operator-() const {
return Vector(-x, -y);
}

Vector operator+(const Vector& other)
{
return Vector(x + other.x, y + other.y);
Expand Down
Loading