TMP / Fixed game.cpp

This commit is contained in:
OLEGSHA 2023-03-28 16:25:05 +02:00
parent 543de5a841
commit 08b5661934
4 changed files with 157 additions and 124 deletions

View File

@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
glfwManager->setOnScreenResize([&]() { vulkanManager.resizeSurface(); }); glfwManager->setOnScreenResize([&]() { vulkanManager.resizeSurface(); });
glfwManager->showWindow(); glfwManager->showWindow();
main::initialize(vulkanManager.getVulkan()->getGint()); auto game = main::makeGame(vulkanManager.getVulkan()->getGint());
info("Loading complete"); info("Loading complete");
while (glfwManager->shouldRun()) { while (glfwManager->shouldRun()) {
@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
continue; continue;
} }
main::renderTick(); game->renderTick();
vulkanManager.endRender(); vulkanManager.endRender();
glfwManager->doGlfwRoutine(); glfwManager->doGlfwRoutine();
@ -49,7 +49,6 @@ int main(int argc, char *argv[]) {
info("Shutting down"); info("Shutting down");
vulkanManager.getVulkan()->waitIdle(); vulkanManager.getVulkan()->waitIdle();
main::shutdown();
return 0; return 0;
} }

View File

@ -18,14 +18,22 @@ using namespace progressia::main::logging;
namespace progressia::main { namespace progressia::main {
std::unique_ptr<Primitive> cube1, cube2; class GameImpl : public Game {
std::unique_ptr<Texture> texture1, texture2;
DISABLE_COPYING(GameImpl)
DISABLE_MOVING(GameImpl)
public:
std::unique_ptr<Primitive> cube1;
std::unique_ptr<Primitive> cube2;
std::unique_ptr<Texture> texture1;
std::unique_ptr<Texture> texture2;
std::unique_ptr<View> perspective; std::unique_ptr<View> perspective;
std::unique_ptr<Light> light; std::unique_ptr<Light> light;
GraphicsInterface *gint; GraphicsInterface *gint;
void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height, static void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
glm::vec4 color, std::vector<Vertex> &vertices, glm::vec4 color, std::vector<Vertex> &vertices,
std::vector<Vertex::Index> &indices) { std::vector<Vertex::Index> &indices) {
@ -45,7 +53,7 @@ void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
indices.push_back(offset + 3); indices.push_back(offset + 3);
} }
void addBox(glm::vec3 origin, glm::vec3 length, glm::vec3 height, static void addBox(glm::vec3 origin, glm::vec3 length, glm::vec3 height,
glm::vec3 depth, std::array<glm::vec4, 6> colors, glm::vec3 depth, std::array<glm::vec4, 6> colors,
std::vector<Vertex> &vertices, std::vector<Vertex> &vertices,
std::vector<Vertex::Index> &indices) { std::vector<Vertex::Index> &indices) {
@ -57,15 +65,15 @@ void addBox(glm::vec3 origin, glm::vec3 length, glm::vec3 height,
addRect(origin + depth, length, height, colors[5], vertices, indices); addRect(origin + depth, length, height, colors[5], vertices, indices);
} }
void initialize(GraphicsInterface &gintp) { GameImpl(GraphicsInterface &gintp) {
debug("game init begin"); debug("game init begin");
gint = &gintp; gint = &gintp;
texture1 = texture1 =
gint->newTexture(progressia::main::loadImage("assets/texture.png")); gint->newTexture(progressia::main::loadImage("assets/texture.png"));
texture2 = texture2 = gint->newTexture(
gint->newTexture(progressia::main::loadImage("assets/texture2.png")); progressia::main::loadImage("assets/texture2.png"));
// Cube 1 // Cube 1
{ {
@ -74,7 +82,8 @@ void initialize(GraphicsInterface &gintp) {
auto white = glm::vec4(1, 1, 1, 1); auto white = glm::vec4(1, 1, 1, 1);
addBox({-0.5, -0.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, addBox({-0.5, -0.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1},
{white, white, white, white, white, white}, vertices, indices); {white, white, white, white, white, white}, vertices,
indices);
for (std::size_t i = 0; i < indices.size(); i += 3) { for (std::size_t i = 0; i < indices.size(); i += 3) {
Vertex &a = vertices[indices[i + 0]]; Vertex &a = vertices[indices[i + 0]];
@ -101,7 +110,8 @@ void initialize(GraphicsInterface &gintp) {
auto white = glm::vec4(1, 1, 1, 1); auto white = glm::vec4(1, 1, 1, 1);
addBox({-0.5, -2.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, addBox({-0.5, -2.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1},
{white, white, white, white, white, white}, vertices, indices); {white, white, white, white, white, white}, vertices,
indices);
for (std::size_t i = 0; i < indices.size(); i += 3) { for (std::size_t i = 0; i < indices.size(); i += 3) {
Vertex &a = vertices[indices[i + 0]]; Vertex &a = vertices[indices[i + 0]];
@ -127,43 +137,44 @@ void initialize(GraphicsInterface &gintp) {
debug("game init complete"); debug("game init complete");
} }
void renderTick() { void renderTick() override {
{ {
float fov = 70.0f; float fov = 70.0F;
auto extent = gint->getViewport(); auto extent = gint->getViewport();
auto proj = glm::perspective(glm::radians(fov), auto proj = glm::perspective(
extent.x / (float)extent.y, 0.1f, 10.0f); glm::radians(fov), extent.x / (float)extent.y, 0.1F, 10.0F);
proj[1][1] *= -1; proj[1][1] *= -1;
auto view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), auto view = glm::lookAt(glm::vec3(2.0F, 2.0F, 2.0F),
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0F, 0.0F, 0.0F),
glm::vec3(0.0f, 0.0f, 1.0f)); glm::vec3(0.0F, 0.0F, 1.0F));
perspective->configure(proj, view); perspective->configure(proj, view);
} }
perspective->use(); perspective->use();
float contrast = glm::sin(gint->tmp_getTime() / 3) * 0.18f + 0.18f; float contrast = glm::sin(gint->tmp_getTime() / 3) * 0.18F + 0.18F;
glm::vec3 color0(0.60f, 0.60f, 0.70f); glm::vec3 color0(0.60F, 0.60F, 0.70F);
glm::vec3 color1(1.10f, 1.05f, 0.70f); glm::vec3 color1(1.10F, 1.05F, 0.70F);
float m = glm::sin(gint->tmp_getTime() / 3) * 0.5 + 0.5; auto m =
static_cast<float>(glm::sin(gint->tmp_getTime() / 3) * 0.5 + 0.5);
glm::vec3 color = m * color1 + (1 - m) * color0; glm::vec3 color = m * color1 + (1 - m) * color0;
light->configure(color, glm::vec3(1.0f, -2.0f, 1.0f), contrast, 0.1f); light->configure(color, glm::vec3(1.0F, -2.0F, 1.0F), contrast, 0.1F);
light->use(); light->use();
auto model = glm::eulerAngleYXZ(0.0f, 0.0f, gint->tmp_getTime() * 0.1f); auto model = glm::eulerAngleYXZ(0.0F, 0.0F, gint->tmp_getTime() * 0.1F);
gint->setModelTransform(model); gint->setModelTransform(model);
cube1->draw(); cube1->draw();
cube2->draw(); cube2->draw();
} }
void shutdown() { ~GameImpl() override {
debug("game shutdown begin"); debug("game shutdown begin");
cube1.reset(); cube1.reset();
@ -176,5 +187,10 @@ void shutdown() {
debug("game shutdown complete"); debug("game shutdown complete");
} }
};
std::unique_ptr<Game> makeGame(GraphicsInterface &gint) {
return std::make_unique<GameImpl>(gint);
}
} // namespace progressia::main } // namespace progressia::main

View File

@ -1,11 +1,16 @@
#pragma once #pragma once
#include "rendering.h" #include "rendering.h"
#include "util.h"
namespace progressia::main { namespace progressia::main {
void initialize(GraphicsInterface &); class Game : private NonCopyable {
void renderTick(); public:
void shutdown(); virtual ~Game() = default;
virtual void renderTick() = 0;
};
std::unique_ptr<Game> makeGame(GraphicsInterface &);
} // namespace progressia::main } // namespace progressia::main

View File

@ -17,7 +17,10 @@ Checks: "-*,\
-cppcoreguidelines-pro-bounds-pointer-arithmetic,\ -cppcoreguidelines-pro-bounds-pointer-arithmetic,\
-performance-trivially-destructible,\ -performance-trivially-destructible,\
-modernize-make-unique,\ -modernize-make-unique,\
-cppcoreguidelines-prefer-member-initializer" -cppcoreguidelines-prefer-member-initializer,\
-*-magic-numbers,\
-readability-suspicious-call-argument,\
-cppcoreguidelines-pro-type-union-access"
# modernize-use-trailing-return-type # modernize-use-trailing-return-type
# ignore reason: reduces readability # ignore reason: reduces readability
@ -63,3 +66,13 @@ Checks: "-*,\
# cppcoreguidelines-prefer-member-initializer # cppcoreguidelines-prefer-member-initializer
# ignore reason: rule fails to notice execution order dependencies # ignore reason: rule fails to notice execution order dependencies
# *-magic-numbers
# ignore reason: triggers in many trivial cases (e.g. 6 sides of a cube);
# infeasible to avoid while writing placeholder code
# readability-suspicious-call-argument
# ignore reason: trips up on geometry code (y no NOLINTBEGIN, Debian?)
# cppcoreguidelines-pro-type-union-access
# ignore reason: triggers on GLM code