From 1922cd76e2063deff9d8b1ddc589dea8ae557393 Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Sun, 21 Apr 2024 14:55:43 +0200 Subject: [PATCH] setSpriteColor(Color) -> setSpriteColor(Rgb) --- src/jngl/FrameBuffer.cpp | 18 ++++++------------ src/jngl/sprite.cpp | 37 ++++++++++++++++--------------------- src/jngl/sprite.hpp | 7 ++++--- src/spriteimpl.cpp | 28 ++++++++++------------------ src/spriteimpl.hpp | 2 +- 5 files changed, 37 insertions(+), 55 deletions(-) diff --git a/src/jngl/FrameBuffer.cpp b/src/jngl/FrameBuffer.cpp index a2853143..a72c383e 100644 --- a/src/jngl/FrameBuffer.cpp +++ b/src/jngl/FrameBuffer.cpp @@ -110,10 +110,8 @@ void FrameBuffer::draw(const Vec2 position, const ShaderProgram* const shaderPro glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE, opengl::modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, static_cast(spriteColorRed) / 255.0f, - static_cast(spriteColorGreen) / 255.0f, - static_cast(spriteColorBlue) / 255.0f, - static_cast(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, opengl::modelview.data); } impl->texture.draw(); @@ -129,10 +127,8 @@ void FrameBuffer::draw(Mat3 modelview, const ShaderProgram* const shaderProgram) -impl->height / getScaleFactor() / 2 }) .data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, static_cast(spriteColorRed) / 255.0f, - static_cast(spriteColorGreen) / 255.0f, - static_cast(spriteColorBlue) / 255.0f, - static_cast(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, modelview.scale(1, -1) .translate({ -impl->width / getScaleFactor() / 2, @@ -151,10 +147,8 @@ void FrameBuffer::drawMesh(const std::vector& vertexes, glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE, opengl::modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, static_cast(spriteColorRed) / 255.0f, - static_cast(spriteColorGreen) / 255.0f, - static_cast(spriteColorBlue) / 255.0f, - static_cast(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, opengl::modelview.data); } impl->texture.drawMesh(vertexes); diff --git a/src/jngl/sprite.cpp b/src/jngl/sprite.cpp index 09cfcd74..8559db21 100644 --- a/src/jngl/sprite.cpp +++ b/src/jngl/sprite.cpp @@ -148,9 +148,8 @@ void Sprite::draw() const { pushMatrix(); opengl::translate(static_cast(position.x), static_cast(position.y)); auto context = Texture::textureShaderProgram->use(); - glUniform4f(Texture::shaderSpriteColorUniform, float(spriteColorRed) / 255.0f, - float(spriteColorGreen) / 255.0f, float(spriteColorBlue) / 255.0f, - float(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), gSpriteColor.getGreen(), + gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, opengl::modelview.data); texture->draw(); popMatrix(); @@ -164,9 +163,8 @@ void Sprite::draw(Mat3 modelview, const ShaderProgram* const shaderProgram) cons glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE, modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, float(spriteColorRed) / 255.0f, - float(spriteColorGreen) / 255.0f, float(spriteColorBlue) / 255.0f, - float(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, modelview.data); } texture->draw(); @@ -181,8 +179,8 @@ void Sprite::draw(const ShaderProgram* const shaderProgram) const { opengl::modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, float(spriteColorRed) / 255.0f, float(spriteColorGreen) / 255.0f, - float(spriteColorBlue) / 255.0f, float(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, opengl::modelview.data); } texture->draw(); @@ -200,8 +198,8 @@ void Sprite::drawScaled(float xfactor, float yfactor, opengl::modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, float(spriteColorRed) / 255.0f, float(spriteColorGreen) / 255.0f, - float(spriteColorBlue) / 255.0f, float(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, opengl::modelview.data); } texture->draw(); @@ -218,9 +216,10 @@ void Sprite::drawClipped(float xstart, float xend, float ystart, float yend) con void Sprite::drawClipped(const Vec2 start, const Vec2 end) const { pushMatrix(); opengl::translate(static_cast(position.x), static_cast(position.y)); - texture->drawClipped(float(start.x), float(end.x), float(start.y), float(end.y), - float(spriteColorRed) / 255.0f, float(spriteColorGreen) / 255.0f, - float(spriteColorBlue) / 255.0f, float(spriteColorAlpha) / 255.0f); + texture->drawClipped(static_cast(start.x), static_cast(end.x), + static_cast(start.y), static_cast(end.y), + gSpriteColor.getRed(), gSpriteColor.getGreen(), gSpriteColor.getBlue(), + gSpriteColor.getAlpha()); popMatrix(); } @@ -235,10 +234,8 @@ void Sprite::drawMesh(Mat3 modelview, const std::vector& vertexes, glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE, modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, static_cast(spriteColorRed) / 255.0f, - static_cast(spriteColorGreen) / 255.0f, - static_cast(spriteColorBlue) / 255.0f, - static_cast(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, modelview.data); } texture->drawMesh(vertexes); @@ -257,10 +254,8 @@ void Sprite::drawMesh(const std::vector& vertexes, glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE, opengl::modelview.data); } else { - glUniform4f(Texture::shaderSpriteColorUniform, static_cast(spriteColorRed) / 255.0f, - static_cast(spriteColorGreen) / 255.0f, - static_cast(spriteColorBlue) / 255.0f, - static_cast(spriteColorAlpha) / 255.0f); + glUniform4f(Texture::shaderSpriteColorUniform, gSpriteColor.getRed(), + gSpriteColor.getGreen(), gSpriteColor.getBlue(), gSpriteColor.getAlpha()); glUniformMatrix3fv(Texture::modelviewUniform, 1, GL_FALSE, opengl::modelview.data); } texture->drawMesh(vertexes); diff --git a/src/jngl/sprite.hpp b/src/jngl/sprite.hpp index fb414c7b..536863e1 100644 --- a/src/jngl/sprite.hpp +++ b/src/jngl/sprite.hpp @@ -1,11 +1,11 @@ -// Copyright 2012-2023 Jan Niklas Hasse +// Copyright 2012-2024 Jan Niklas Hasse // For conditions of distribution and use, see copyright notice in LICENSE.txt /// Contains jngl::Sprite class and related functions /// \file #pragma once -#include "Color.hpp" #include "Drawable.hpp" +#include "Rgb.hpp" #include "ShaderProgram.hpp" #include "Vec2.hpp" @@ -185,7 +185,8 @@ void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue, void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue); -void setSpriteColor(Color); +/// Sets the global color used for drawing Sprites, leaves the alpha value untouched +void setSpriteColor(Rgb); void setSpriteAlpha(unsigned char alpha); diff --git a/src/spriteimpl.cpp b/src/spriteimpl.cpp index 9beadcb8..c1ecfc4c 100644 --- a/src/spriteimpl.cpp +++ b/src/spriteimpl.cpp @@ -3,6 +3,7 @@ #include "spriteimpl.hpp" +#include "jngl/Alpha.hpp" #include "jngl/ImageData.hpp" #include "jngl/message.hpp" #include "texture.hpp" @@ -10,44 +11,35 @@ namespace jngl { -unsigned char spriteColorRed = 255, spriteColorGreen = 255, spriteColorBlue = 255, - spriteColorAlpha = 255; +Rgba gSpriteColor{ 1, 1, 1, 1 }; void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue) { - spriteColorRed = red; - spriteColorGreen = green; - spriteColorBlue = blue; + gSpriteColor.setRgb(Rgb::u8(red, green, blue)); } -void setSpriteColor(Color color) { - spriteColorRed = color.getRed(); - spriteColorGreen = color.getGreen(); - spriteColorBlue = color.getBlue(); +void setSpriteColor(Rgb color) { + gSpriteColor.setRgb(color); } std::stack spriteAlphas; void pushSpriteAlpha(unsigned char alpha) { - spriteAlphas.push(spriteColorAlpha); - setSpriteAlpha(spriteColorAlpha * alpha / 255); + spriteAlphas.push(gSpriteColor.getAlpha()); + gSpriteColor.setAlpha(gSpriteColor.getAlpha() * Alpha::u8(alpha).getAlpha()); } void popSpriteAlpha() { - setSpriteAlpha(spriteAlphas.top()); + gSpriteColor.setAlpha(spriteAlphas.top()); spriteAlphas.pop(); } void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha) { - spriteColorRed = red; - spriteColorGreen = green; - spriteColorBlue = blue; - spriteColorAlpha = alpha; + gSpriteColor = Rgba::u8(red, green, blue, alpha); } void setSpriteAlpha(unsigned char alpha) { - spriteColorAlpha = alpha; - setSpriteColor(spriteColorRed, spriteColorGreen, spriteColorBlue, alpha); + gSpriteColor.setAlpha(Alpha::u8(alpha)); } std::unordered_map> sprites_; diff --git a/src/spriteimpl.hpp b/src/spriteimpl.hpp index 2433cabd..c526c676 100644 --- a/src/spriteimpl.hpp +++ b/src/spriteimpl.hpp @@ -11,7 +11,7 @@ namespace jngl { Finally loadSprite(const std::string&); Sprite& GetSprite(const std::string& filename, Sprite::LoadType loadType = Sprite::LoadType::NORMAL); -extern unsigned char spriteColorRed, spriteColorGreen, spriteColorBlue, spriteColorAlpha; +extern Rgba gSpriteColor; extern Rgba gShapeColor; } // namespace jngl