DrawPartialSprite
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 7m11s
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 7m11s
This commit is contained in:
58
src/JGL.cpp
58
src/JGL.cpp
@@ -142,7 +142,7 @@ namespace JGL {
|
||||
// i.e. to render at 2x size, from the center, at coords XY, use {2, 2} scale, and {0.5, 0.5} offset.
|
||||
const Vector2 offset = origin * size;
|
||||
|
||||
Vector2 pos2 = pos - offset*scale;
|
||||
Vector2 pos2 = pos - offset * scale;
|
||||
Vector2 scaled_size = scale * size;
|
||||
Vector2 size2 = scaled_size;
|
||||
|
||||
@@ -180,6 +180,62 @@ namespace JGL {
|
||||
inversion);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialSprite(const Texture& texture, const Vector2& position, const Vector2& sub_texture_position, const Vector2& sub_texture_size, const Vector2& origin, const Vector2& scale, const Color4& color, Inversion inversion) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
||||
|
||||
const Vector2 textureSize = texture.GetDimensions();
|
||||
|
||||
// Calculate texture coordinates (correctly relative to the whole texture)
|
||||
std::array<GLfloat, 8> textureCoordinates = {
|
||||
sub_texture_position.x / textureSize.x,
|
||||
sub_texture_position.y / textureSize.y,
|
||||
sub_texture_position.x / textureSize.x,
|
||||
(sub_texture_position.y + sub_texture_size.y) / textureSize.y,
|
||||
(sub_texture_position.x + sub_texture_size.x) / textureSize.x,
|
||||
(sub_texture_position.y + sub_texture_size.y) / textureSize.y,
|
||||
(sub_texture_position.x + sub_texture_size.x) / textureSize.x,
|
||||
sub_texture_position.y / textureSize.y
|
||||
};
|
||||
|
||||
if (inversion & Inversion::Vertical) {
|
||||
std::swap(textureCoordinates[1], textureCoordinates[3]);
|
||||
std::swap(textureCoordinates[5], textureCoordinates[7]);
|
||||
}
|
||||
if (inversion & Inversion::Horizontal) {
|
||||
std::swap(textureCoordinates[0], textureCoordinates[6]);
|
||||
std::swap(textureCoordinates[2], textureCoordinates[4]);
|
||||
}
|
||||
|
||||
const Vector2 offset = origin * sub_texture_size;
|
||||
|
||||
Vector2 pos2 = position - offset * scale;
|
||||
Vector2 scaled_size = scale * sub_texture_size;
|
||||
Vector2 size2 = scaled_size;
|
||||
|
||||
const Vector2 vertices[] = {
|
||||
pos2, // Top-left
|
||||
{pos2.x, pos2.y + size2.y}, // Bottom-left
|
||||
{pos2.x + size2.x, pos2.y + size2.y},// Bottom-right
|
||||
{pos2.x + size2.x, pos2.y} // Top-right
|
||||
};
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
glBindTexture(GL_TEXTURE_2D, texture.GetGLTextureHandle());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates.data());
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialSprite(const JGL::Texture& texture, float positionX, float positionY, float sub_texture_positionX, float sub_texture_positionY, unsigned int sub_texture_sizeX,
|
||||
unsigned int sub_texture_sizeY, float originX,float originY, float scaleX, float scaleY, const Color4& color,JGL::Inversion inversion) {
|
||||
|
||||
J2D::DrawPartialSprite(texture, {positionX, positionY}, {sub_texture_positionX, sub_texture_positionY},
|
||||
{(float) sub_texture_sizeX, (float) sub_texture_sizeY}, {originX, originY}, {scaleX, scaleY}, color, inversion);
|
||||
}
|
||||
|
||||
void J2D::FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
Reference in New Issue
Block a user