Skip to content

Commit

Permalink
setSpriteColor(Color) -> setSpriteColor(Rgb)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Apr 21, 2024
1 parent d95c5f0 commit 1922cd7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 55 deletions.
18 changes: 6 additions & 12 deletions src/jngl/FrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(spriteColorRed) / 255.0f,
static_cast<float>(spriteColorGreen) / 255.0f,
static_cast<float>(spriteColorBlue) / 255.0f,
static_cast<float>(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();
Expand All @@ -129,10 +127,8 @@ void FrameBuffer::draw(Mat3 modelview, const ShaderProgram* const shaderProgram)
-impl->height / getScaleFactor() / 2 })
.data);
} else {
glUniform4f(Texture::shaderSpriteColorUniform, static_cast<float>(spriteColorRed) / 255.0f,
static_cast<float>(spriteColorGreen) / 255.0f,
static_cast<float>(spriteColorBlue) / 255.0f,
static_cast<float>(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,
Expand All @@ -151,10 +147,8 @@ void FrameBuffer::drawMesh(const std::vector<Vertex>& vertexes,
glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE,
opengl::modelview.data);
} else {
glUniform4f(Texture::shaderSpriteColorUniform, static_cast<float>(spriteColorRed) / 255.0f,
static_cast<float>(spriteColorGreen) / 255.0f,
static_cast<float>(spriteColorBlue) / 255.0f,
static_cast<float>(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);
Expand Down
37 changes: 16 additions & 21 deletions src/jngl/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ void Sprite::draw() const {
pushMatrix();
opengl::translate(static_cast<float>(position.x), static_cast<float>(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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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<float>(position.x), static_cast<float>(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<float>(start.x), static_cast<float>(end.x),
static_cast<float>(start.y), static_cast<float>(end.y),
gSpriteColor.getRed(), gSpriteColor.getGreen(), gSpriteColor.getBlue(),
gSpriteColor.getAlpha());
popMatrix();
}

Expand All @@ -235,10 +234,8 @@ void Sprite::drawMesh(Mat3 modelview, const std::vector<Vertex>& vertexes,
glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE,
modelview.data);
} else {
glUniform4f(Texture::shaderSpriteColorUniform, static_cast<float>(spriteColorRed) / 255.0f,
static_cast<float>(spriteColorGreen) / 255.0f,
static_cast<float>(spriteColorBlue) / 255.0f,
static_cast<float>(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);
Expand All @@ -257,10 +254,8 @@ void Sprite::drawMesh(const std::vector<Vertex>& vertexes,
glUniformMatrix3fv(shaderProgram->getUniformLocation("modelview"), 1, GL_FALSE,
opengl::modelview.data);
} else {
glUniform4f(Texture::shaderSpriteColorUniform, static_cast<float>(spriteColorRed) / 255.0f,
static_cast<float>(spriteColorGreen) / 255.0f,
static_cast<float>(spriteColorBlue) / 255.0f,
static_cast<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->drawMesh(vertexes);
Expand Down
7 changes: 4 additions & 3 deletions src/jngl/sprite.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2012-2023 Jan Niklas Hasse <[email protected]>
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]>
// 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"

Expand Down Expand Up @@ -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);

Expand Down
28 changes: 10 additions & 18 deletions src/spriteimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,43 @@

#include "spriteimpl.hpp"

#include "jngl/Alpha.hpp"
#include "jngl/ImageData.hpp"
#include "jngl/message.hpp"
#include "texture.hpp"
#include "windowptr.hpp"

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<float> 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<std::string, std::shared_ptr<Sprite>> sprites_;
Expand Down
2 changes: 1 addition & 1 deletion src/spriteimpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1922cd7

Please sign in to comment.