From fb23156296b781634bc7f4904e04a324266f9353 Mon Sep 17 00:00:00 2001
From: Odilitime <janesmith@airmail.cc>
Date: Tue, 22 Aug 2017 04:24:26 -0700
Subject: [PATCH] changeColor()

---
 src/graphics/components/BoxComponent.cpp | 20 ++++++++++++++++++++
 src/graphics/components/BoxComponent.h   |  1 +
 2 files changed, 21 insertions(+)

diff --git a/src/graphics/components/BoxComponent.cpp b/src/graphics/components/BoxComponent.cpp
index 22e5daf4..72d764e1 100644
--- a/src/graphics/components/BoxComponent.cpp
+++ b/src/graphics/components/BoxComponent.cpp
@@ -186,6 +186,26 @@ void BoxComponent::resize(const int passedWindowWidth, const int passedWindowHei
     verticesDirty = true;
 }
 
+void BoxComponent::changeColor(const unsigned int hexColor) {
+    // really a texture swap
+    
+    // set texture color
+    data[0][0][0] = (hexColor >> 24) & 0xFF;
+    data[0][0][1] = (hexColor >> 16) & 0xFF;
+    data[0][0][2] = (hexColor >>  8) & 0xFF;
+    data[0][0][3] = (hexColor >>  0) & 0xFF;
+    
+    glBindVertexArray(vertexArrayObject);
+    
+    // now replace existing texture
+    glBindTexture(GL_TEXTURE_2D, texture);
+    
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+    glGenerateMipmap(GL_TEXTURE_2D);
+    
+    glBindVertexArray(0); // unbind to prevent anything from accidentally changing it
+}
+
 /*
 void BoxComponent::resize(const int passedWindowWidth, const int passedWindowHeight) {
 
diff --git a/src/graphics/components/BoxComponent.h b/src/graphics/components/BoxComponent.h
index 1c04596b..a4b980a1 100644
--- a/src/graphics/components/BoxComponent.h
+++ b/src/graphics/components/BoxComponent.h
@@ -27,6 +27,7 @@ public:
     BoxComponent();
     BoxComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const unsigned int hexColor, const int passedWindowWidth, const int passedWindowHeight);
     ~BoxComponent();
+    void changeColor(const unsigned int hexColor);
     virtual void render();
     virtual void resize(const int passedWindowWidth, const int passedWindowHeight);
 };
-- 
GitLab