DrawPartialSprite
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 7m11s

This commit is contained in:
2024-08-22 10:58:20 -04:00
parent 32de87229e
commit c0b65818c8
4 changed files with 85 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ public:
//CachedGlyph(GLuint texture_id, char c);
CachedGlyph(char c, std::array<GLfloat, 12> texcoords, float x2o, float y2o, float w, float h, float advX, float advY);
char getCharacter();
const std::array<GLfloat, 12> getTexCoords() const;
[[nodiscard]] const std::array<GLfloat, 12> getTexCoords() const;
};
/// Represents a Font object as it exists in the font-cache.

View File

@@ -121,14 +121,37 @@ namespace JGL {
const Color4& color = Colors::White,
Inversion inversion = Inversion::None);
///Draws a piece of a sprite to the screen, similar to DrawSprite.
/// @param texture
/// @param position
/// @param sub_texture_position The top left corner of the sub-texture to be drawn.
/// @param sub_texture_size The size of the sub-texture in px.
/// @param origin
/// @param scale
/// @param color
/// @param inversion
void DrawPartialSprite(const Texture& texture, const Vector2& position, const Vector2& sub_texture_position, const Vector2& sub_texture_size,
const Vector2& origin = Vector2(0,0), const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White, Inversion inversion = Inversion::None);
void DrawPartialSprite(const 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 = 0, float originY = 0, float scaleX = 1, float scaleY = 1, const Color4& color = Colors::White, Inversion inversion = Inversion::None);
/// Draws a non axis-aligned fill rect to the screen.
/// The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
/// @param color
/// @param v1 top-left vertex.
/// @param v2 bottom-left vertex.
/// @param v3 bottom-right vertex.
/// @param v4 top-right vertex.
void FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
void FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
/// Draws a non axis-aligned outline rect to the screen.
/// The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
/// @param color
/// @param v1 top-left vertex.
/// @param v2 bottom-left vertex.
/// @param v3 bottom-right vertex.
/// @param v4 top-right vertex.
/// @param thickness the thickness of the GL_LINES to be connected together.
void OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
void OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
@@ -184,7 +207,6 @@ namespace JGL {
// TODO: Implement the following:
void FillTexturedTriangle();
void FillTexturedPolygon();
void DrawPartialSprite();
void DrawCubicBezierCurve(const Color4& color,

View File

@@ -172,7 +172,8 @@ public:
J2D::Begin();
J2D::FillQuad(Color4(Colors::Red), {500, 52}, {500, 152}, {600, 152}, {600, 52});
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::DrawSprite(*image, {200, 252}, {0.5, 0.5}, {2, 1});
J2D::DrawSprite(*image, {252, 252}, {0.5, 0.5});
J2D::DrawPartialSprite(*image, {200, 252}, image->GetDimensions() * 0.25, image->GetDimensions() * 0.75, {0.5, 0.5});
J2D::FillRect(Colors::Pinks::HotPink, {68, 120}, {32, 32});
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
J2D::FillRoundedRect(Colors::Red, {200, 52}, {100, 100}, 8, 8);

View File

@@ -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.")