From 79736fc3bc13028a937b328aebedb8bf796e134a Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Fri, 14 Jan 2022 22:11:42 +0100 Subject: [PATCH] Created rudimentary board --- res/card.vs | 3 ++- src/Application.cpp | 49 ++++++++++++++++++++++----------------------- src/Application.hpp | 5 +++-- src/Board.cpp | 42 ++++++++++++++++++++++++++++++++++++++ src/Board.hpp | 17 ++++++++++++++++ src/CMakeLists.txt | 5 ++++- src/Card.cpp | 17 ++++++++++++---- src/Card.hpp | 1 + src/CardStack.cpp | 32 +++++++++++++++++++++++++++++ src/CardStack.hpp | 16 +++++++++++++++ src/ObjectIDs.hpp | 6 ++++++ src/Window.cpp | 4 ++-- src/Window.hpp | 2 +- vendor/lol | 2 +- 14 files changed, 164 insertions(+), 37 deletions(-) create mode 100644 src/Board.cpp create mode 100644 src/Board.hpp create mode 100644 src/CardStack.cpp create mode 100644 src/CardStack.hpp create mode 100644 src/ObjectIDs.hpp diff --git a/res/card.vs b/res/card.vs index e7367fa..6b0f7b7 100644 --- a/res/card.vs +++ b/res/card.vs @@ -5,11 +5,12 @@ layout (location = 1) in vec2 UV; out vec2 uvCoord; +uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { uvCoord = UV; - gl_Position = projection * view * vec4(position, 0.0f, 1.0f); + gl_Position = projection * view * model * vec4(position, 0.0f, 1.0f); } \ No newline at end of file diff --git a/src/Application.cpp b/src/Application.cpp index d52aad2..1cd258e 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -1,6 +1,7 @@ #include "Application.hpp" #include +#include #include #include @@ -9,9 +10,11 @@ #include #include "Window.hpp" -#include "Card.hpp" +#include "CardStack.hpp" +#include "Board.hpp" #include "resources.h" +#include "ObjectIDs.hpp" void Application::Run() { @@ -22,32 +25,18 @@ void Application::Run() } 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()) { glfwPollEvents(); - if (std::chrono::duration_cast(std::chrono::system_clock::now() - begin).count() > 250) - { - begin = std::chrono::system_clock::now(); - currentType++; - if (currentType > 3) - { - currentMonth++; - if (currentMonth > 11) - currentMonth = 0; - - currentType = 0; - } - - card->UpdateSuitAndType(static_cast(currentMonth), currentType); - } + for (lol::Layer* layer : layerStack) + layer->OnUpdate(); window->Clear(); - window->Draw(*card); + + for (lol::Layer* layer : layerStack) + window->Draw(*layer); + window->Display(); } } @@ -81,18 +70,28 @@ Application::Application() : spdlog::debug("Loading resources..."); lol::Image image((unsigned char*)card_atlas_png, card_atlas_png_size); - manager.Create(0, image, lol::TextureFormat::RGBA); - manager.Create(1, std::string((char*)card_vs, card_vs_size), std::string((char*)card_fs, card_fs_size)); + manager.Create(TEXTURE_CARD_ATLAS, image, lol::TextureFormat::RGBA); + manager.Create(SHADER_CARD, std::string((char*)card_vs, card_vs_size), std::string((char*)card_fs, card_fs_size)); spdlog::debug("Done!"); - card = new Card(manager, Month::August, 0); + spdlog::debug("Creating card stack"); + manager.Create(CARD_STACK, manager); + + spdlog::debug("Setting up board"); + layerStack.push_back(new Board(manager)); glViewport(0, 0, 1280, 720); + + // Enable transparency + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } Application::~Application() { - delete card; + for (lol::Layer* layer : layerStack) + delete layer; + delete window; glfwTerminate(); diff --git a/src/Application.hpp b/src/Application.hpp index 523bf14..1d7c04a 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -1,9 +1,10 @@ #pragma once +#include #include + class Window; -class Card; class Application { @@ -22,7 +23,7 @@ private: Window* window; lol::ObjectManager manager; - Card* card; + std::vector layerStack; private: Application(); diff --git a/src/Board.cpp b/src/Board.cpp new file mode 100644 index 0000000..57644be --- /dev/null +++ b/src/Board.cpp @@ -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 stack = manager.Get(CARD_STACK); + for (int y = 0; y < 2; y++) + { + for (int x = 0; x < 4; x++) + { + std::shared_ptr 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 : openCards) + card->Draw(camera); +} diff --git a/src/Board.hpp b/src/Board.hpp new file mode 100644 index 0000000..9116600 --- /dev/null +++ b/src/Board.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#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> openCards; +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 891456d..f93a01c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,10 @@ add_executable(koikoi "main.cpp" "Application.cpp" "Window.cpp" - "Card.cpp") + "Card.cpp" + "CardStack.cpp" + "Board.cpp" +) set( RC_DEPENDS "" ) diff --git a/src/Card.cpp b/src/Card.cpp index 8ed2b97..7ac0b31 100644 --- a/src/Card.cpp +++ b/src/Card.cpp @@ -1,10 +1,13 @@ #include "Card.hpp" +#include +#include "ObjectIDs.hpp" + Card::Card(lol::ObjectManager& manager, Month month, int type) { try { - vao = manager.Get(2); + vao = manager.Get(VAO_CARD); } catch (const lol::ObjectNotFoundException& err) { @@ -23,11 +26,16 @@ Card::Card(lol::ObjectManager& manager, Month month, int type) std::shared_ptr ebo = std::make_shared(std::vector{ 0, 1, 2, 0, 2, 3 }); - vao = manager.Create(2, vbo, ebo); + vao = manager.Create(VAO_CARD, vbo, ebo); + } + catch (const std::exception& err) + { + spdlog::critical("Unknown error in constructor of Card"); + throw err; } - shader = manager.Get(1); - cards = manager.Get(0); + shader = manager.Get(SHADER_CARD); + cards = manager.Get(TEXTURE_CARD_ATLAS); UpdateSuitAndType(month, type); } @@ -48,6 +56,7 @@ void Card::PreRender(const lol::CameraBase& camera) cards->Bind(); shader->SetUniform("offset", textureOffset); + shader->SetUniform("model", transformation); shader->SetUniform("view", camera.GetView()); shader->SetUniform("projection", camera.GetProjection()); } diff --git a/src/Card.hpp b/src/Card.hpp index b72827d..d054382 100644 --- a/src/Card.hpp +++ b/src/Card.hpp @@ -13,6 +13,7 @@ public: Card(lol::ObjectManager& manager, Month month, int type); void UpdateSuitAndType(Month month, int type); + glm::vec3 GetSize() { return glm::vec3(0.61035f, 1.0f, 0.0f) * GetScale(); } private: void PreRender(const lol::CameraBase& camera) override; diff --git a/src/CardStack.cpp b/src/CardStack.cpp new file mode 100644 index 0000000..8dad0fc --- /dev/null +++ b/src/CardStack.cpp @@ -0,0 +1,32 @@ +#include "CardStack.hpp" + +#include +#include +#include + +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(manager, static_cast(month), type)); + } + } + + // Shuffle stack + std::default_random_engine engine(time(0)); + std::shuffle(stack.begin(), stack.end(), engine); +} + +CardStack::~CardStack() +{ +} + +std::shared_ptr CardStack::Draw() +{ + std::shared_ptr drawnCard = stack.back(); + stack.pop_back(); + return drawnCard; +} diff --git a/src/CardStack.hpp b/src/CardStack.hpp new file mode 100644 index 0000000..8eeba8f --- /dev/null +++ b/src/CardStack.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "Card.hpp" + +class CardStack +{ +public: + CardStack(lol::ObjectManager& manager); + ~CardStack(); + + std::shared_ptr Draw(); + inline bool Empty() { return stack.empty(); } + +private: + std::vector> stack; +}; \ No newline at end of file diff --git a/src/ObjectIDs.hpp b/src/ObjectIDs.hpp new file mode 100644 index 0000000..02ec4a5 --- /dev/null +++ b/src/ObjectIDs.hpp @@ -0,0 +1,6 @@ +#pragma once + +#define TEXTURE_CARD_ATLAS 0 +#define SHADER_CARD 1 +#define VAO_CARD 2 +#define CARD_STACK 3 \ No newline at end of file diff --git a/src/Window.cpp b/src/Window.cpp index 8df0e55..781dc26 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -49,9 +49,9 @@ void Window::Clear() 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() diff --git a/src/Window.hpp b/src/Window.hpp index 7378653..65be36a 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -20,7 +20,7 @@ public: inline bool ShouldClose() { return glfwWindowShouldClose(window); } void Clear(); - void Draw(lol::Drawable& drawable); + void Draw(lol::Layer& layer); void Display(); private: diff --git a/vendor/lol b/vendor/lol index 7b268ef..bb055a4 160000 --- a/vendor/lol +++ b/vendor/lol @@ -1 +1 @@ -Subproject commit 7b268efa6a4a893e64c2cad18a05aa633b5aebe1 +Subproject commit bb055a48d214b6697b57db03a9f7e4300178c585