Added cards

This commit is contained in:
Lauchmelder 2022-01-13 23:24:39 +01:00
parent 2a8b38ffc4
commit f3b4ffd75f
14 changed files with 273 additions and 8 deletions

2
.gitignore vendored
View file

@ -2,4 +2,4 @@
[Oo]ut/ [Oo]ut/
[Bb]uild/ [Bb]uild/
*.json /*.json

View file

@ -5,8 +5,8 @@ cmake_minimum_required (VERSION 3.8)
project ("KoiKoi") project ("KoiKoi")
# Include sub-projects. # Include sub-projects.
add_subdirectory ("vendor/spdlog") add_subdirectory ("vendor/spdlog")
add_subdirectory ("vendor/glfw") add_subdirectory ("vendor/glfw")
add_subdirectory ("vendor/lol")
add_subdirectory ("src") add_subdirectory ("src")

12
res/card.fs Normal file
View file

@ -0,0 +1,12 @@
#version 440 core
in vec2 uvCoord;
out vec4 FragColor;
uniform vec2 offset;
uniform sampler2D motive;
void main()
{
FragColor = texture(motive, uvCoord + offset);
}

15
res/card.vs Normal file
View file

@ -0,0 +1,15 @@
#version 440 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 UV;
out vec2 uvCoord;
uniform mat4 view;
uniform mat4 projection;
void main()
{
uvCoord = UV;
gl_Position = projection * view * vec4(position, 0.0f, 1.0f);
}

34
res/card_atlas.json Normal file
View file

@ -0,0 +1,34 @@
[
{
"posX": 0,
"posY": 0,
"month": 0,
"type": 0,
"name_en": "Crane and Sun",
"name_jp": "松に鶴"
},
{
"posX": 0,
"posY": 1,
"month": 0,
"type": 2,
"name_en": "Poetry ribbon",
"name_jp": "松に赤短"
},
{
"posX": 0,
"posY": 2,
"month": 0,
"type": 3,
"name_en": "Pine plain",
"name_jp": "松のカス"
},
{
"posX": 0,
"posY": 3,
"month": 0,
"type": 3,
"name_en": "Pine plain",
"name_jp": "松のカス"
}
]

BIN
res/card_atlas.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 MiB

View file

@ -1,9 +1,17 @@
#include "Application.hpp" #include "Application.hpp"
#include <chrono>
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <stbi_image.h>
#include "Window.hpp" #include "Window.hpp"
#include "Card.hpp"
#include "resources.h"
void Application::Run() void Application::Run()
{ {
@ -14,9 +22,32 @@ 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)
{
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->Draw(*card);
window->Display(); window->Display();
} }
} }
@ -36,12 +67,32 @@ Application::Application() :
} }
spdlog::debug("GLFW initialized"); spdlog::debug("GLFW initialized");
window = new Window(800, 800, "Koi Koi"); window = new Window(1280, 720, "Koi Koi");
valid = window->IsValid(); valid = window->IsValid();
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
spdlog::critical("Failed to load GL loader");
valid = false;
return;
}
spdlog::debug("Loaded GL loader");
spdlog::debug("Loading resources...");
lol::Image image((unsigned char*)card_atlas_png, card_atlas_png_size);
manager.Create<lol::Texture2D>(0, 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));
spdlog::debug("Done!");
card = new Card(manager, Month::August, 0);
glViewport(0, 0, 1280, 720);
} }
Application::~Application() Application::~Application()
{ {
delete card;
delete window; delete window;
glfwTerminate(); glfwTerminate();

View file

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <lol/lol.hpp>
class Window; class Window;
class Card;
class Application class Application
{ {
@ -18,6 +21,9 @@ private:
bool valid; bool valid;
Window* window; Window* window;
lol::ObjectManager manager;
Card* card;
private: private:
Application(); Application();
~Application(); ~Application();

View file

@ -2,14 +2,46 @@ add_executable(koikoi
"main.cpp" "main.cpp"
"Application.cpp" "Application.cpp"
"Window.cpp" "Window.cpp"
) "Card.cpp")
target_include_directories(koikoi PRIVATE set( RC_DEPENDS "" )
set(output ${CMAKE_BINARY_DIR}/CMakeFiles/koikoi.dir/autogen/resources.h)
set(dir ${CMAKE_SOURCE_DIR}/res)
# Create empty output file
file(WRITE ${output} "")
# Collect input files
file(GLOB bins ${dir}/*)
# Iterate through input files
foreach(bin ${bins})
# Get short filename
string(REGEX MATCH "([^/]+)$" filename ${bin})
# Replace filename spaces & extension separator for C compatibility
string(REGEX REPLACE "\\.| |-" "_" filename ${filename})
# Read hex data from file
file(READ ${bin} filedata HEX)
# Convert hex data for C compatibility
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
# Append data to output file
file(APPEND ${output} "const unsigned char ${filename}[] = {${filedata}};\nconst unsigned ${filename}_size = sizeof(${filename});\n")
endforeach()
set( RC_DEPENDS ${RC_DEPENDS} ${output} PARENT_SCOPE )
add_custom_target( rc ALL DEPENDS ${RC_DEPENDS} )
add_dependencies(koikoi rc)
target_include_directories(koikoi PUBLIC
${CMAKE_SOURCE_DIR}/vendor/headeronly
${CMAKE_BINARY_DIR}/CMakeFiles/koikoi.dir/autogen
spdlog spdlog
glfw glfw
lol
) )
target_link_libraries(koikoi PRIVATE target_link_libraries(koikoi
spdlog spdlog
glfw glfw
lol
) )

53
src/Card.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "Card.hpp"
Card::Card(lol::ObjectManager& manager, Month month, int type)
{
try
{
vao = manager.Get<lol::VertexArray>(2);
}
catch (const lol::ObjectNotFoundException& err)
{
std::shared_ptr<lol::VertexBuffer> vbo = std::make_shared<lol::VertexBuffer>(std::vector<float>{
0.0f, 0.0f, 0.0f, 1.0f / 4.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.61035f, 1.0f, 1.0f / 12.0f, 0.0f,
0.61035f, 0.0f, 1.0f / 12.0f, 1.0f / 4.0f
});
lol::BufferLayout layout({
lol::VertexAttribute(lol::Type::Float, 2, false),
lol::VertexAttribute(lol::Type::Float, 2, false)
});
vbo->SetLayout(layout);
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);
}
shader = manager.Get<lol::Shader>(1);
cards = manager.Get<lol::Texture2D>(0);
UpdateSuitAndType(month, type);
}
void Card::UpdateSuitAndType(Month month, int type)
{
this->suit = month;
this->type = type;
textureOffset = glm::vec2(
static_cast<int>(this->suit) / 12.0f,
this->type / 4.0f
);
}
void Card::PreRender(const lol::CameraBase& camera)
{
cards->Bind();
shader->SetUniform("offset", textureOffset);
shader->SetUniform("view", camera.GetView());
shader->SetUniform("projection", camera.GetProjection());
}

26
src/Card.hpp Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#include <lol/lol.hpp>
enum class Month
{
January, February, March, April, May, June, July, August, September, October, November, December
};
class Card : public lol::Drawable, public lol::Transformable
{
public:
Card(lol::ObjectManager& manager, Month month, int type);
void UpdateSuitAndType(Month month, int type);
private:
void PreRender(const lol::CameraBase& camera) override;
private:
std::shared_ptr<lol::Texture2D> cards;
Month suit;
int type;
glm::vec2 textureOffset;
};

View file

@ -1,6 +1,5 @@
#include "Window.hpp" #include "Window.hpp"
#include <GLFW/glfw3.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
Window::Window(int width, int height, const std::string& title) : Window::Window(int width, int height, const std::string& title) :
@ -19,6 +18,21 @@ Window::Window(int width, int height, const std::string& title) :
spdlog::debug("Created GLFWwindow \n\tDimensions = ({}px, {}px), \n\tTitle = {}, \n\tFullscreen = {}", width, height, title, "NO"); spdlog::debug("Created GLFWwindow \n\tDimensions = ({}px, {}px), \n\tTitle = {}, \n\tFullscreen = {}", width, height, title, "NO");
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window,
[](GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
UserData* data = (UserData*)glfwGetWindowUserPointer(window);
data->camera->Update(0.0f, ((float)width / (float)height) * 1.0f, 0.0f, 1.0f, -100.0f, 100.0f);
}
);
camera = lol::OrthogonalCamera(0.0f, ((float)width / (float)height) * 1.0f, 0.0f, 1.0f);
data.camera = &camera;
spdlog::debug("Created orthogonal camera");
glfwSetWindowUserPointer(window, (void*)&data);
} }
Window::~Window() Window::~Window()
@ -30,6 +44,16 @@ Window::~Window()
} }
} }
void Window::Clear()
{
glClear(GL_COLOR_BUFFER_BIT);
}
void Window::Draw(lol::Drawable& drawable)
{
camera.Draw(drawable);
}
void Window::Display() void Window::Display()
{ {
glfwSwapBuffers(window); glfwSwapBuffers(window);

View file

@ -1,7 +1,14 @@
#pragma once #pragma once
#include <string> #include <string>
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <lol/lol.hpp>
struct UserData
{
lol::OrthogonalCamera* camera;
};
class Window class Window
{ {
@ -12,9 +19,14 @@ public:
inline bool IsValid() { return valid; } inline bool IsValid() { return valid; }
inline bool ShouldClose() { return glfwWindowShouldClose(window); } inline bool ShouldClose() { return glfwWindowShouldClose(window); }
void Clear();
void Draw(lol::Drawable& drawable);
void Display(); void Display();
private: private:
bool valid; bool valid;
GLFWwindow* window; GLFWwindow* window;
UserData data;
lol::OrthogonalCamera camera;
}; };

2
vendor/lol vendored

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