Set up the intro gamestate; Added opacity and scale controls to UI elements

This commit is contained in:
Grayson Riffe (Desktop) 2021-09-06 03:01:11 -05:00
parent 9158de2f39
commit cdcd8a1994
19 changed files with 136 additions and 56 deletions

View File

@ -4,10 +4,11 @@ in vec2 texCoord;
uniform sampler2D text; uniform sampler2D text;
uniform vec3 textColor; uniform vec3 textColor;
uniform float opacity;
out vec4 color; out vec4 color;
void main() { void main() {
vec4 temp = vec4(1.0, 1.0, 1.0, texture(text, texCoord).r); vec4 temp = vec4(1.0, 1.0, 1.0, texture(text, texCoord).r);
color = vec4(textColor.xyz, 1.0) * temp; color = vec4(textColor.xyz, opacity) * temp;
} }

View File

@ -3,9 +3,12 @@
in vec2 texCoord; in vec2 texCoord;
uniform sampler2D tex; uniform sampler2D tex;
uniform float opacity;
out vec4 color; out vec4 color;
void main() { void main() {
color = texture(tex, texCoord); vec3 texColor = texture(tex, texCoord).rgb;
color = vec4(texColor.rgb, opacity);
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

View File

@ -54,7 +54,7 @@ namespace nf {
void Application::addDefaultState(const std::string& stateName) { void Application::addDefaultState(const std::string& stateName) {
if (!m_defaultStateAdded) { if (!m_defaultStateAdded) {
if (m_states.find(stateName) != m_states.end()) { if (m_states.find(stateName) != m_states.end()) {
m_DefaultState = m_states[stateName]; m_defaultState = stateName;
m_defaultStateAdded = true; m_defaultStateAdded = true;
} }
else else
@ -64,6 +64,10 @@ namespace nf {
Error("More than one default state defined!"); Error("More than one default state defined!");
} }
const std::string& Application::getDefaultState() {
return m_defaultState;
}
void Application::run() { void Application::run() {
showWindow(true); showWindow(true);
m_running = true; m_running = true;
@ -146,7 +150,6 @@ namespace nf {
y = m_mouseDiffY; y = m_mouseDiffY;
m_mouseDiffX = 0; m_mouseDiffX = 0;
m_mouseDiffY = 0; m_mouseDiffY = 0;
//TODO: Replace with atomic?
} }
void Application::registerWindowClass() { void Application::registerWindowClass() {
@ -283,6 +286,7 @@ namespace nf {
void Application::doStateChange() { void Application::doStateChange() {
m_stateChange = false; m_stateChange = false;
//TODO: Do fade in and out here
m_currentState->onExit(); m_currentState->onExit();
m_currentState = m_states[m_nextState]; m_currentState = m_states[m_nextState];
m_currentState->onEnter(); m_currentState->onEnter();

View File

@ -116,7 +116,7 @@ namespace nf {
curr->numImages++; curr->numImages++;
if (curr->numImages == 6) { if (curr->numImages == 6) {
m_assets[cmName] = curr; m_assets[cmName + (std::string)".cm"] = curr;
} }
cubemapCount++; cubemapCount++;
@ -175,5 +175,7 @@ namespace nf {
AModel* BaseAssets::cylinder; AModel* BaseAssets::cylinder;
AModel* BaseAssets::torus; AModel* BaseAssets::torus;
ATexture* BaseAssets::logo;
AFont* BaseAssets::defaultFont; AFont* BaseAssets::defaultFont;
} }

View File

@ -2,6 +2,7 @@
#include "Application.h" #include "Application.h"
#include "Utility.h" #include "Utility.h"
#include "Input.h"
namespace nf { namespace nf {
IntroGamestate::IntroGamestate(Application* app) : IntroGamestate::IntroGamestate(Application* app) :
@ -12,18 +13,36 @@ namespace nf {
void IntroGamestate::onEnter() { void IntroGamestate::onEnter() {
Log("Intro onEnter!"); Log("Intro onEnter!");
m_counter = 0; logoTex.create(BaseAssets::logo, Vec2(0.0, 0.0));
logoTex.centered(true, true);
text.create("(C) Grayson Riffe 2021", Vec2(0.01, 0.025), Vec3(0.8));
text.setScale(0.6);
} }
void IntroGamestate::update(double deltaTime) { void IntroGamestate::update(double deltaTime) {
if (m_counter >= 120) { if (m_counter >= 240) {
app->changeState("Main State"); app->changeState(app->getDefaultState());
} }
static double opacity = 0.0;
static double scale = 1.0;
logoTex.setOpacity(opacity);
logoTex.setScale(scale);
text.setOpacity(opacity);
if (m_counter >= 20) {
opacity += 0.02;
scale += 0.002;
}
m_counter++; m_counter++;
if (app->isInput(NFI_ESCAPE))
app->quit();
} }
void IntroGamestate::render(Renderer& renderer) { void IntroGamestate::render(Renderer& renderer) {
renderer.render(logoTex);
renderer.render(text);
} }
void IntroGamestate::onExit() { void IntroGamestate::onExit() {

View File

@ -3,7 +3,10 @@
#include "Utility.h" #include "Utility.h"
namespace nf { namespace nf {
Drawable::Drawable() { Drawable::Drawable() :
m_vao(nullptr),
m_ib(nullptr)
{
} }

View File

@ -8,28 +8,34 @@
#include "Shader.h" #include "Shader.h"
namespace nf { namespace nf {
Text::Text() { Text::Text() :
m_font(nullptr),
m_scale(1.0f),
m_opacity(1.0f)
{
} }
void Text::create(const std::string& string, const Vec2& position, const Vec3& color, unsigned int size, Asset* font) { void Text::create(const std::string& string, const Vec2& position, const Vec3& color, double opacity, double scale, Asset* font) {
m_constructed = true; m_constructed = true;
m_string = string; m_string = string;
m_position = position; m_position = position;
m_color = color; m_color = color;
FT_Library ft; m_scale = (float)scale;
if (FT_Init_FreeType(&ft)) m_opacity = (float)opacity;
Error("Could not initialize FreeType!");
FT_Face face;
AFont& newFont = *(AFont*)font; AFont& newFont = *(AFont*)font;
if (newFont.alreadyLoaded) { if (newFont.alreadyLoaded) {
m_font = newFont.loadedFont; m_font = newFont.loadedFont;
} }
else { else {
m_font = new Font; m_font = new Font;
FT_Library ft;
if (FT_Init_FreeType(&ft))
Error("Could not initialize FreeType!");
FT_Face face;
if (FT_New_Memory_Face(ft, (const unsigned char*)newFont.data, newFont.size, 0, &face)) if (FT_New_Memory_Face(ft, (const unsigned char*)newFont.data, newFont.size, 0, &face))
Error("Could not load font!"); Error("Could not load font!");
FT_Set_Pixel_Sizes(face, 0, size); FT_Set_Pixel_Sizes(face, 0, 160);
for (unsigned char c = 0; c < 128; c++) { for (unsigned char c = 0; c < 128; c++) {
FT_Load_Char(face, c, FT_LOAD_RENDER); FT_Load_Char(face, c, FT_LOAD_RENDER);
unsigned int tex; unsigned int tex;
@ -61,6 +67,18 @@ namespace nf {
m_string = string; m_string = string;
} }
void Text::setColor(const Vec3& color) {
m_color = color;
}
void Text::setScale(double scale) {
m_scale = (float)scale;
}
void Text::setOpacity(double opacity) {
m_opacity = (float)opacity;
}
const char* Text::identity() { const char* Text::identity() {
return "text"; return "text";
} }
@ -75,9 +93,9 @@ namespace nf {
float textHeight = 0.0f; float textHeight = 0.0f;
for (si = m_string.begin(); si != m_string.end(); si++) { for (si = m_string.begin(); si != m_string.end(); si++) {
Character& c = m_font->m_characters[*si]; Character& c = m_font->m_characters[*si];
textWidth += (c.advance >> 6) * scale; textWidth += (c.advance >> 6) * scale * m_scale;
if (c.size.y >= textHeight) if (c.size.y >= textHeight)
textHeight = (float)c.size.y * scale; textHeight = (float)c.size.y * scale * m_scale;
} }
if (m_centeredX) if (m_centeredX)
currX = ((float)windowWidth - textWidth) / 2; currX = ((float)windowWidth - textWidth) / 2;
@ -86,12 +104,13 @@ namespace nf {
} }
glm::vec3 color = { m_color.x, m_color.y, m_color.z }; glm::vec3 color = { m_color.x, m_color.y, m_color.z };
shader->setUniform("textColor", color); shader->setUniform("textColor", color);
shader->setUniform("opacity", m_opacity);
for (si = m_string.begin(); si != m_string.end(); si++) { for (si = m_string.begin(); si != m_string.end(); si++) {
Character& c = m_font->m_characters[*si]; Character& c = m_font->m_characters[*si];
float x = currX + (float)c.bearing.x * scale; float x = currX + (float)c.bearing.x * scale * m_scale;
float y = currY - float(c.size.y - c.bearing.y) * scale; float y = currY - float(c.size.y - c.bearing.y) * scale * m_scale;
float w = (float)c.size.x * scale; float w = (float)c.size.x * scale * m_scale;
float h = (float)c.size.y * scale; float h = (float)c.size.y * scale * m_scale;
float vb[3][4] = { float vb[3][4] = {
x, y + h, x, y + h,
x, y, x, y,
@ -112,7 +131,7 @@ namespace nf {
m_vao->setBufferData(0, vb, sizeof(vb)); m_vao->setBufferData(0, vb, sizeof(vb));
m_vao->setBufferData(1, tc, sizeof(tc)); m_vao->setBufferData(1, tc, sizeof(tc));
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
currX += (c.advance >> 6) * scale; currX += (c.advance >> 6) * scale * m_scale;
} }
} }

View File

@ -4,20 +4,23 @@
#include "Assets.h" #include "Assets.h"
#include "Texture.h" #include "Texture.h"
#include "Shader.h"
namespace nf { namespace nf {
UITexture::UITexture() : UITexture::UITexture() :
m_texture(nullptr), m_texture(nullptr),
m_scale(1.0f) m_scale(1.0f),
m_opacity(1.0f)
{ {
} }
void UITexture::create(Asset* textureAsset, const Vec2& position, double scale) { void UITexture::create(Asset* textureAsset, const Vec2& position, double scale, double opacity) {
m_constructed = true; m_constructed = true;
ATexture* tex = (ATexture*)textureAsset; ATexture* tex = (ATexture*)textureAsset;
m_position = position; m_position = position;
m_scale = (float)scale; m_scale = (float)scale;
m_opacity = (float)opacity;
if (tex->alreadyLoaded) { if (tex->alreadyLoaded) {
m_texture = tex->loadedTexture; m_texture = tex->loadedTexture;
} }
@ -39,6 +42,14 @@ namespace nf {
return "texture"; return "texture";
} }
void UITexture::setScale(double scale) {
m_scale = (float)scale;
}
void UITexture::setOpacity(double opacity) {
m_opacity = (float)opacity;
}
void UITexture::render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) { void UITexture::render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) {
float posX = (float)m_position.x * windowWidth, posY = (float)m_position.y * windowHeight; float posX = (float)m_position.x * windowWidth, posY = (float)m_position.y * windowHeight;
float scale = windowWidth / 5.0f; float scale = windowWidth / 5.0f;
@ -69,6 +80,7 @@ namespace nf {
m_texture->bind(); m_texture->bind();
m_vao->setBufferData(0, vb, sizeof(vb)); m_vao->setBufferData(0, vb, sizeof(vb));
m_vao->setBufferData(1, tc, sizeof(tc)); m_vao->setBufferData(1, tc, sizeof(tc));
shader->setUniform("opacity", m_opacity);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
} }

View File

@ -82,6 +82,7 @@ namespace nf {
BaseAssets::cone = (AModel*)m_baseAP["cone.obj"]; BaseAssets::cone = (AModel*)m_baseAP["cone.obj"];
BaseAssets::cylinder = (AModel*)m_baseAP["cylinder.obj"]; BaseAssets::cylinder = (AModel*)m_baseAP["cylinder.obj"];
BaseAssets::torus = (AModel*)m_baseAP["torus.obj"]; BaseAssets::torus = (AModel*)m_baseAP["torus.obj"];
BaseAssets::logo = (ATexture*)m_baseAP["logo.png"];
BaseAssets::defaultFont = (AFont*)m_baseAP["default.ttf"]; BaseAssets::defaultFont = (AFont*)m_baseAP["default.ttf"];
} }

View File

@ -7,7 +7,10 @@
#include "Utility.h" #include "Utility.h"
namespace nf { namespace nf {
Texture::Texture() { Texture::Texture() :
m_x(0),
m_y(0)
{
glGenTextures(1, &m_id); glGenTextures(1, &m_id);
} }

View File

@ -98,7 +98,6 @@ namespace nf {
std::string readFile(const std::string& filename) { std::string readFile(const std::string& filename) {
std::ifstream in; std::ifstream in;
in.open(filename, std::ios::binary); in.open(filename, std::ios::binary);
//TODO: Test this change
if (!in) if (!in)
Error("File \"" + (std::string)filename + (std::string)"\" could not be read!"); Error("File \"" + (std::string)filename + (std::string)"\" could not be read!");
std::stringstream ss; std::stringstream ss;

View File

@ -20,6 +20,7 @@ namespace nf {
Renderer* getRenderer() const; Renderer* getRenderer() const;
void addState(Gamestate* state, const std::string& stateName); void addState(Gamestate* state, const std::string& stateName);
void addDefaultState(const std::string& stateName); void addDefaultState(const std::string& stateName);
const std::string& getDefaultState();
void run(); void run();
void changeState(const std::string& stateName); void changeState(const std::string& stateName);
void showWindow(bool show); void showWindow(bool show);
@ -67,7 +68,7 @@ namespace nf {
//Mapped to const char* to be referenced later in the frontend //Mapped to const char* to be referenced later in the frontend
std::unordered_map<std::string, Gamestate*> m_states; std::unordered_map<std::string, Gamestate*> m_states;
Gamestate* m_sIntro; Gamestate* m_sIntro;
Gamestate* m_DefaultState; std::string m_defaultState;
bool m_defaultStateAdded; bool m_defaultStateAdded;
Gamestate* m_currentState; Gamestate* m_currentState;
bool m_stateChange; bool m_stateChange;

View File

@ -7,39 +7,39 @@ namespace nf {
struct Font; struct Font;
struct Asset { struct Asset {
char* data; char* data = nullptr;
bool alreadyLoaded = false; bool alreadyLoaded = false;
virtual ~Asset(); virtual ~Asset();
}; };
struct AModel : Asset { struct AModel : Asset {
Model* loadedModel; Model* loadedModel = nullptr;
~AModel() override; ~AModel() override;
}; };
struct ATexture : Asset { struct ATexture : Asset {
size_t size; size_t size = 0;
Texture* loadedTexture; Texture* loadedTexture = nullptr;
~ATexture() override; ~ATexture() override;
}; };
struct ACubemap : Asset { struct ACubemap : Asset {
char* frontData; char* frontData = nullptr;
size_t frontSize; size_t frontSize = 0;
char* backData; char* backData = nullptr;
size_t backSize; size_t backSize = 0;
char* topData; char* topData = nullptr;
size_t topSize; size_t topSize = 0;
char* bottomData; char* bottomData = nullptr;
size_t bottomSize; size_t bottomSize = 0;
char* leftData; char* leftData = nullptr;
size_t leftSize; size_t leftSize = 0;
char* rightData; char* rightData = nullptr;
size_t rightSize; size_t rightSize = 0;
unsigned int numImages; unsigned int numImages = 0;
~ACubemap(); ~ACubemap();
}; };
@ -49,8 +49,8 @@ namespace nf {
}; };
struct AFont : Asset { struct AFont : Asset {
size_t size; size_t size = 0;
Font* loadedFont; Font* loadedFont = nullptr;
~AFont() override; ~AFont() override;
}; };
@ -76,8 +76,9 @@ namespace nf {
static AModel* cylinder; static AModel* cylinder;
static AModel* torus; static AModel* torus;
static ATexture* logo;
static AFont* defaultFont; static AFont* defaultFont;
//static ATexture* logo;
}; };
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "Gamestate.h" #include "Gamestate.h"
#include "UITexture.h"
#include "Text.h"
namespace nf { namespace nf {
class IntroGamestate : public Gamestate { class IntroGamestate : public Gamestate {
@ -14,6 +16,7 @@ namespace nf {
void onExit() override; void onExit() override;
private: private:
int m_counter; int m_counter;
//TODO: Flesh out intro gamestate UITexture logoTex;
Text text;
}; };
} }

View File

@ -89,6 +89,7 @@ namespace nf {
Renderer* getRenderer() const; Renderer* getRenderer() const;
void addState(Gamestate* state, const std::string& stateName); void addState(Gamestate* state, const std::string& stateName);
void addDefaultState(const std::string& stateName); void addDefaultState(const std::string& stateName);
const std::string& getDefaultState();
void run(); void run();
void changeState(const std::string& stateName); void changeState(const std::string& stateName);
void showWindow(bool show); void showWindow(bool show);
@ -136,7 +137,7 @@ namespace nf {
//Mapped to const char* to be referenced later in the frontend //Mapped to const char* to be referenced later in the frontend
std::unordered_map<std::string, Gamestate*> m_states; std::unordered_map<std::string, Gamestate*> m_states;
Gamestate* m_sIntro; Gamestate* m_sIntro;
Gamestate* m_DefaultState; std::string m_defaultState;
bool m_defaultStateAdded; bool m_defaultStateAdded;
Gamestate* m_currentState; Gamestate* m_currentState;
bool m_stateChange; bool m_stateChange;

View File

@ -7,10 +7,10 @@
namespace nf { namespace nf {
struct Character { struct Character {
unsigned int texID; unsigned int texID = 0;
Vec2 size; Vec2 size;
Vec2 bearing; Vec2 bearing;
unsigned int advance; unsigned int advance = 0;
}; };
struct Font { struct Font {
@ -21,9 +21,12 @@ namespace nf {
public: public:
Text(); Text();
void create(const std::string& string, const Vec2& position, const Vec3& color = {1.0, 1.0, 1.0}, unsigned int size = 160, Asset* font = BaseAssets::defaultFont); void create(const std::string& string, const Vec2& position, const Vec3& color = {1.0, 1.0, 1.0}, double opacity = 1.0, double scale = 1.0, Asset* font = BaseAssets::defaultFont);
void setText(const std::string& string);
const char* identity() override; const char* identity() override;
void setText(const std::string& string);
void setColor(const Vec3& color);
void setScale(double scale);
void setOpacity(double opacity);
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override; void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;
~Text(); ~Text();
@ -31,5 +34,7 @@ namespace nf {
std::string m_string; std::string m_string;
Font* m_font; Font* m_font;
Vec3 m_color; Vec3 m_color;
float m_scale;
float m_opacity;
}; };
} }

View File

@ -10,13 +10,16 @@ namespace nf {
public: public:
UITexture(); UITexture();
void create(Asset* textureAsset, const Vec2& position, double scale = 1.0); void create(Asset* textureAsset, const Vec2& position, double scale = 1.0, double opacity = 1.0 );
const char* identity() override; const char* identity() override;
void setScale(double scale);
void setOpacity(double opacity);
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override; void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;
~UITexture(); ~UITexture();
private: private:
Texture* m_texture; Texture* m_texture;
float m_scale; float m_scale;
float m_opacity;
}; };
} }