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

View File

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

View File

@ -2,6 +2,7 @@
#include "Application.h"
#include "Utility.h"
#include "Input.h"
namespace nf {
IntroGamestate::IntroGamestate(Application* app) :
@ -12,18 +13,36 @@ namespace nf {
void IntroGamestate::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) {
if (m_counter >= 120) {
app->changeState("Main State");
if (m_counter >= 240) {
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++;
if (app->isInput(NFI_ESCAPE))
app->quit();
}
void IntroGamestate::render(Renderer& renderer) {
renderer.render(logoTex);
renderer.render(text);
}
void IntroGamestate::onExit() {

View File

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

View File

@ -8,28 +8,34 @@
#include "Shader.h"
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_string = string;
m_position = position;
m_color = color;
FT_Library ft;
if (FT_Init_FreeType(&ft))
Error("Could not initialize FreeType!");
FT_Face face;
m_scale = (float)scale;
m_opacity = (float)opacity;
AFont& newFont = *(AFont*)font;
if (newFont.alreadyLoaded) {
m_font = newFont.loadedFont;
}
else {
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))
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++) {
FT_Load_Char(face, c, FT_LOAD_RENDER);
unsigned int tex;
@ -61,6 +67,18 @@ namespace nf {
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() {
return "text";
}
@ -75,9 +93,9 @@ namespace nf {
float textHeight = 0.0f;
for (si = m_string.begin(); si != m_string.end(); 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)
textHeight = (float)c.size.y * scale;
textHeight = (float)c.size.y * scale * m_scale;
}
if (m_centeredX)
currX = ((float)windowWidth - textWidth) / 2;
@ -86,12 +104,13 @@ namespace nf {
}
glm::vec3 color = { m_color.x, m_color.y, m_color.z };
shader->setUniform("textColor", color);
shader->setUniform("opacity", m_opacity);
for (si = m_string.begin(); si != m_string.end(); si++) {
Character& c = m_font->m_characters[*si];
float x = currX + (float)c.bearing.x * scale;
float y = currY - float(c.size.y - c.bearing.y) * scale;
float w = (float)c.size.x * scale;
float h = (float)c.size.y * scale;
float x = currX + (float)c.bearing.x * scale * m_scale;
float y = currY - float(c.size.y - c.bearing.y) * scale * m_scale;
float w = (float)c.size.x * scale * m_scale;
float h = (float)c.size.y * scale * m_scale;
float vb[3][4] = {
x, y + h,
x, y,
@ -112,7 +131,7 @@ namespace nf {
m_vao->setBufferData(0, vb, sizeof(vb));
m_vao->setBufferData(1, tc, sizeof(tc));
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 "Texture.h"
#include "Shader.h"
namespace nf {
UITexture::UITexture() :
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;
ATexture* tex = (ATexture*)textureAsset;
m_position = position;
m_scale = (float)scale;
m_opacity = (float)opacity;
if (tex->alreadyLoaded) {
m_texture = tex->loadedTexture;
}
@ -39,6 +42,14 @@ namespace nf {
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) {
float posX = (float)m_position.x * windowWidth, posY = (float)m_position.y * windowHeight;
float scale = windowWidth / 5.0f;
@ -69,6 +80,7 @@ namespace nf {
m_texture->bind();
m_vao->setBufferData(0, vb, sizeof(vb));
m_vao->setBufferData(1, tc, sizeof(tc));
shader->setUniform("opacity", m_opacity);
glDrawArrays(GL_TRIANGLES, 0, 6);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,10 +7,10 @@
namespace nf {
struct Character {
unsigned int texID;
unsigned int texID = 0;
Vec2 size;
Vec2 bearing;
unsigned int advance;
unsigned int advance = 0;
};
struct Font {
@ -21,9 +21,12 @@ namespace nf {
public:
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 setText(const std::string& string);
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);
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;
~Text();
@ -31,5 +34,7 @@ namespace nf {
std::string m_string;
Font* m_font;
Vec3 m_color;
float m_scale;
float m_opacity;
};
}

View File

@ -10,13 +10,16 @@ namespace nf {
public:
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;
void setScale(double scale);
void setOpacity(double opacity);
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;
~UITexture();
private:
Texture* m_texture;
float m_scale;
float m_opacity;
};
}