Created rudimentary board

This commit is contained in:
Lauchmelder 2022-01-14 22:11:42 +01:00
parent 63c7a1bb2d
commit 79736fc3bc
14 changed files with 164 additions and 37 deletions

View file

@ -5,11 +5,12 @@ layout (location = 1) in vec2 UV;
out vec2 uvCoord; out vec2 uvCoord;
uniform mat4 model;
uniform mat4 view; uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
void main() void main()
{ {
uvCoord = UV; uvCoord = UV;
gl_Position = projection * view * vec4(position, 0.0f, 1.0f); gl_Position = projection * view * model * vec4(position, 0.0f, 1.0f);
} }

View file

@ -1,6 +1,7 @@
#include "Application.hpp" #include "Application.hpp"
#include <chrono> #include <chrono>
#include <random>
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -9,9 +10,11 @@
#include <stbi_image.h> #include <stbi_image.h>
#include "Window.hpp" #include "Window.hpp"
#include "Card.hpp" #include "CardStack.hpp"
#include "Board.hpp"
#include "resources.h" #include "resources.h"
#include "ObjectIDs.hpp"
void Application::Run() void Application::Run()
{ {
@ -22,32 +25,18 @@ void Application::Run()
} }
spdlog::debug("Launched Application"); spdlog::debug("Launched Application");
int currentMonth = 0;
int currentType = 0;
std::chrono::system_clock::time_point begin = std::chrono::system_clock::now();
while (!window->ShouldClose()) while (!window->ShouldClose())
{ {
glfwPollEvents(); glfwPollEvents();
if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - begin).count() > 250) for (lol::Layer* layer : layerStack)
{ layer->OnUpdate();
begin = std::chrono::system_clock::now();
currentType++;
if (currentType > 3)
{
currentMonth++;
if (currentMonth > 11)
currentMonth = 0;
currentType = 0;
}
card->UpdateSuitAndType(static_cast<Month>(currentMonth), currentType);
}
window->Clear(); window->Clear();
window->Draw(*card);
for (lol::Layer* layer : layerStack)
window->Draw(*layer);
window->Display(); window->Display();
} }
} }
@ -81,18 +70,28 @@ Application::Application() :
spdlog::debug("Loading resources..."); spdlog::debug("Loading resources...");
lol::Image image((unsigned char*)card_atlas_png, card_atlas_png_size); lol::Image image((unsigned char*)card_atlas_png, card_atlas_png_size);
manager.Create<lol::Texture2D>(0, image, lol::TextureFormat::RGBA); manager.Create<lol::Texture2D>(TEXTURE_CARD_ATLAS, image, lol::TextureFormat::RGBA);
manager.Create<lol::Shader>(1, std::string((char*)card_vs, card_vs_size), std::string((char*)card_fs, card_fs_size)); manager.Create<lol::Shader>(SHADER_CARD, std::string((char*)card_vs, card_vs_size), std::string((char*)card_fs, card_fs_size));
spdlog::debug("Done!"); spdlog::debug("Done!");
card = new Card(manager, Month::August, 0); spdlog::debug("Creating card stack");
manager.Create<CardStack>(CARD_STACK, manager);
spdlog::debug("Setting up board");
layerStack.push_back(new Board(manager));
glViewport(0, 0, 1280, 720); glViewport(0, 0, 1280, 720);
// Enable transparency
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} }
Application::~Application() Application::~Application()
{ {
delete card; for (lol::Layer* layer : layerStack)
delete layer;
delete window; delete window;
glfwTerminate(); glfwTerminate();

View file

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <vector>
#include <lol/lol.hpp> #include <lol/lol.hpp>
class Window; class Window;
class Card;
class Application class Application
{ {
@ -22,7 +23,7 @@ private:
Window* window; Window* window;
lol::ObjectManager manager; lol::ObjectManager manager;
Card* card; std::vector<lol::Layer*> layerStack;
private: private:
Application(); Application();

42
src/Board.cpp Normal file
View file

@ -0,0 +1,42 @@
#include "Board.hpp"
#include "CardStack.hpp"
#include "ObjectIDs.hpp"
Board::Board(lol::ObjectManager& manager) :
Layer("Board")
{
// Draw 8 cards from stack
std::shared_ptr<CardStack> stack = manager.Get<CardStack>(CARD_STACK);
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 4; x++)
{
std::shared_ptr<Card> card = stack->Draw();
card->SetScale(glm::vec3(0.2f));
glm::vec3 cardSize = card->GetSize();
card->SetPosition(glm::vec3(
0.25f + (cardSize.x + 0.05f) * x,
0.5f - cardSize.y - 0.025f + (cardSize.y + 0.05f) * y,
0.0f
));
openCards.push_back(card);
}
}
}
Board::~Board()
{
}
void Board::OnUpdate()
{
}
void Board::OnRender(lol::CameraBase& camera)
{
for (std::shared_ptr<Card> card : openCards)
card->Draw(camera);
}

17
src/Board.hpp Normal file
View file

@ -0,0 +1,17 @@
#pragma once
#include <lol/lol.hpp>
#include "Card.hpp"
class Board : public lol::Layer
{
public:
Board(lol::ObjectManager& manager);
~Board();
void OnUpdate() override;
void OnRender(lol::CameraBase& camera) override;
private:
std::vector<std::shared_ptr<Card>> openCards;
};

View file

@ -2,7 +2,10 @@ add_executable(koikoi
"main.cpp" "main.cpp"
"Application.cpp" "Application.cpp"
"Window.cpp" "Window.cpp"
"Card.cpp") "Card.cpp"
"CardStack.cpp"
"Board.cpp"
)
set( RC_DEPENDS "" ) set( RC_DEPENDS "" )

View file

@ -1,10 +1,13 @@
#include "Card.hpp" #include "Card.hpp"
#include <spdlog/spdlog.h>
#include "ObjectIDs.hpp"
Card::Card(lol::ObjectManager& manager, Month month, int type) Card::Card(lol::ObjectManager& manager, Month month, int type)
{ {
try try
{ {
vao = manager.Get<lol::VertexArray>(2); vao = manager.Get<lol::VertexArray>(VAO_CARD);
} }
catch (const lol::ObjectNotFoundException& err) catch (const lol::ObjectNotFoundException& err)
{ {
@ -23,11 +26,16 @@ Card::Card(lol::ObjectManager& manager, Month month, int type)
std::shared_ptr<lol::ElementBuffer> ebo = std::make_shared<lol::ElementBuffer>(std::vector<unsigned int>{ 0, 1, 2, 0, 2, 3 }); std::shared_ptr<lol::ElementBuffer> ebo = std::make_shared<lol::ElementBuffer>(std::vector<unsigned int>{ 0, 1, 2, 0, 2, 3 });
vao = manager.Create<lol::VertexArray>(2, vbo, ebo); vao = manager.Create<lol::VertexArray>(VAO_CARD, vbo, ebo);
}
catch (const std::exception& err)
{
spdlog::critical("Unknown error in constructor of Card");
throw err;
} }
shader = manager.Get<lol::Shader>(1); shader = manager.Get<lol::Shader>(SHADER_CARD);
cards = manager.Get<lol::Texture2D>(0); cards = manager.Get<lol::Texture2D>(TEXTURE_CARD_ATLAS);
UpdateSuitAndType(month, type); UpdateSuitAndType(month, type);
} }
@ -48,6 +56,7 @@ void Card::PreRender(const lol::CameraBase& camera)
cards->Bind(); cards->Bind();
shader->SetUniform("offset", textureOffset); shader->SetUniform("offset", textureOffset);
shader->SetUniform("model", transformation);
shader->SetUniform("view", camera.GetView()); shader->SetUniform("view", camera.GetView());
shader->SetUniform("projection", camera.GetProjection()); shader->SetUniform("projection", camera.GetProjection());
} }

View file

@ -13,6 +13,7 @@ public:
Card(lol::ObjectManager& manager, Month month, int type); Card(lol::ObjectManager& manager, Month month, int type);
void UpdateSuitAndType(Month month, int type); void UpdateSuitAndType(Month month, int type);
glm::vec3 GetSize() { return glm::vec3(0.61035f, 1.0f, 0.0f) * GetScale(); }
private: private:
void PreRender(const lol::CameraBase& camera) override; void PreRender(const lol::CameraBase& camera) override;

32
src/CardStack.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "CardStack.hpp"
#include <algorithm>
#include <chrono>
#include <random>
CardStack::CardStack(lol::ObjectManager& manager)
{
// Create a full stack of cards
for (int month = 0; month < 11; month++)
{
for (int type = 0; type < 4; type++)
{
stack.push_back(std::make_shared<Card>(manager, static_cast<Month>(month), type));
}
}
// Shuffle stack
std::default_random_engine engine(time(0));
std::shuffle(stack.begin(), stack.end(), engine);
}
CardStack::~CardStack()
{
}
std::shared_ptr<Card> CardStack::Draw()
{
std::shared_ptr<Card> drawnCard = stack.back();
stack.pop_back();
return drawnCard;
}

16
src/CardStack.hpp Normal file
View file

@ -0,0 +1,16 @@
#pragma once
#include "Card.hpp"
class CardStack
{
public:
CardStack(lol::ObjectManager& manager);
~CardStack();
std::shared_ptr<Card> Draw();
inline bool Empty() { return stack.empty(); }
private:
std::vector<std::shared_ptr<Card>> stack;
};

6
src/ObjectIDs.hpp Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#define TEXTURE_CARD_ATLAS 0
#define SHADER_CARD 1
#define VAO_CARD 2
#define CARD_STACK 3

View file

@ -49,9 +49,9 @@ void Window::Clear()
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void Window::Draw(lol::Drawable& drawable) void Window::Draw(lol::Layer& layer)
{ {
camera.Draw(drawable); layer.OnRender(camera);
} }
void Window::Display() void Window::Display()

View file

@ -20,7 +20,7 @@ public:
inline bool ShouldClose() { return glfwWindowShouldClose(window); } inline bool ShouldClose() { return glfwWindowShouldClose(window); }
void Clear(); void Clear();
void Draw(lol::Drawable& drawable); void Draw(lol::Layer& layer);
void Display(); void Display();
private: private:

2
vendor/lol vendored

@ -1 +1 @@
Subproject commit 7b268efa6a4a893e64c2cad18a05aa633b5aebe1 Subproject commit bb055a48d214b6697b57db03a9f7e4300178c585