mirror of
https://gitea.windcorp.ru/Wind-Corporation/Progressia.git
synced 2025-04-21 16:00:46 +03:00
TMP / Fixed game.cpp
This commit is contained in:
parent
543de5a841
commit
08b5661934
@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
|
||||
glfwManager->setOnScreenResize([&]() { vulkanManager.resizeSurface(); });
|
||||
glfwManager->showWindow();
|
||||
|
||||
main::initialize(vulkanManager.getVulkan()->getGint());
|
||||
auto game = main::makeGame(vulkanManager.getVulkan()->getGint());
|
||||
|
||||
info("Loading complete");
|
||||
while (glfwManager->shouldRun()) {
|
||||
@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
main::renderTick();
|
||||
game->renderTick();
|
||||
|
||||
vulkanManager.endRender();
|
||||
glfwManager->doGlfwRoutine();
|
||||
@ -49,7 +49,6 @@ int main(int argc, char *argv[]) {
|
||||
info("Shutting down");
|
||||
|
||||
vulkanManager.getVulkan()->waitIdle();
|
||||
main::shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -18,14 +18,22 @@ using namespace progressia::main::logging;
|
||||
|
||||
namespace progressia::main {
|
||||
|
||||
std::unique_ptr<Primitive> cube1, cube2;
|
||||
std::unique_ptr<Texture> texture1, texture2;
|
||||
std::unique_ptr<View> perspective;
|
||||
std::unique_ptr<Light> light;
|
||||
class GameImpl : public Game {
|
||||
|
||||
GraphicsInterface *gint;
|
||||
DISABLE_COPYING(GameImpl)
|
||||
DISABLE_MOVING(GameImpl)
|
||||
|
||||
void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
|
||||
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<Light> light;
|
||||
|
||||
GraphicsInterface *gint;
|
||||
|
||||
static void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
|
||||
glm::vec4 color, std::vector<Vertex> &vertices,
|
||||
std::vector<Vertex::Index> &indices) {
|
||||
|
||||
@ -43,9 +51,9 @@ void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
|
||||
indices.push_back(offset + 0);
|
||||
indices.push_back(offset + 2);
|
||||
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,
|
||||
std::vector<Vertex> &vertices,
|
||||
std::vector<Vertex::Index> &indices) {
|
||||
@ -55,17 +63,17 @@ void addBox(glm::vec3 origin, glm::vec3 length, glm::vec3 height,
|
||||
addRect(origin + height, depth, length, colors[3], vertices, indices);
|
||||
addRect(origin + length, height, depth, colors[4], vertices, indices);
|
||||
addRect(origin + depth, length, height, colors[5], vertices, indices);
|
||||
}
|
||||
}
|
||||
|
||||
void initialize(GraphicsInterface &gintp) {
|
||||
GameImpl(GraphicsInterface &gintp) {
|
||||
|
||||
debug("game init begin");
|
||||
gint = &gintp;
|
||||
|
||||
texture1 =
|
||||
gint->newTexture(progressia::main::loadImage("assets/texture.png"));
|
||||
texture2 =
|
||||
gint->newTexture(progressia::main::loadImage("assets/texture2.png"));
|
||||
texture2 = gint->newTexture(
|
||||
progressia::main::loadImage("assets/texture2.png"));
|
||||
|
||||
// Cube 1
|
||||
{
|
||||
@ -74,7 +82,8 @@ void initialize(GraphicsInterface &gintp) {
|
||||
auto white = glm::vec4(1, 1, 1, 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) {
|
||||
Vertex &a = vertices[indices[i + 0]];
|
||||
@ -101,7 +110,8 @@ void initialize(GraphicsInterface &gintp) {
|
||||
auto white = glm::vec4(1, 1, 1, 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) {
|
||||
Vertex &a = vertices[indices[i + 0]];
|
||||
@ -125,45 +135,46 @@ void initialize(GraphicsInterface &gintp) {
|
||||
light = gint->newLight();
|
||||
|
||||
debug("game init complete");
|
||||
}
|
||||
}
|
||||
|
||||
void renderTick() {
|
||||
void renderTick() override {
|
||||
|
||||
{
|
||||
float fov = 70.0f;
|
||||
float fov = 70.0F;
|
||||
|
||||
auto extent = gint->getViewport();
|
||||
auto proj = glm::perspective(glm::radians(fov),
|
||||
extent.x / (float)extent.y, 0.1f, 10.0f);
|
||||
auto proj = glm::perspective(
|
||||
glm::radians(fov), extent.x / (float)extent.y, 0.1F, 10.0F);
|
||||
proj[1][1] *= -1;
|
||||
|
||||
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, 1.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, 1.0F));
|
||||
|
||||
perspective->configure(proj, view);
|
||||
}
|
||||
|
||||
perspective->use();
|
||||
|
||||
float contrast = glm::sin(gint->tmp_getTime() / 3) * 0.18f + 0.18f;
|
||||
glm::vec3 color0(0.60f, 0.60f, 0.70f);
|
||||
glm::vec3 color1(1.10f, 1.05f, 0.70f);
|
||||
float contrast = glm::sin(gint->tmp_getTime() / 3) * 0.18F + 0.18F;
|
||||
glm::vec3 color0(0.60F, 0.60F, 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;
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
cube1->draw();
|
||||
cube2->draw();
|
||||
}
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
~GameImpl() override {
|
||||
debug("game shutdown begin");
|
||||
|
||||
cube1.reset();
|
||||
@ -175,6 +186,11 @@ void shutdown() {
|
||||
perspective.reset();
|
||||
|
||||
debug("game shutdown complete");
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Game> makeGame(GraphicsInterface &gint) {
|
||||
return std::make_unique<GameImpl>(gint);
|
||||
}
|
||||
|
||||
} // namespace progressia::main
|
||||
|
11
main/game.h
11
main/game.h
@ -1,11 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "rendering.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace progressia::main {
|
||||
|
||||
void initialize(GraphicsInterface &);
|
||||
void renderTick();
|
||||
void shutdown();
|
||||
class Game : private NonCopyable {
|
||||
public:
|
||||
virtual ~Game() = default;
|
||||
virtual void renderTick() = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<Game> makeGame(GraphicsInterface &);
|
||||
|
||||
} // namespace progressia::main
|
||||
|
@ -17,7 +17,10 @@ Checks: "-*,\
|
||||
-cppcoreguidelines-pro-bounds-pointer-arithmetic,\
|
||||
-performance-trivially-destructible,\
|
||||
-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
|
||||
# ignore reason: reduces readability
|
||||
@ -63,3 +66,13 @@ Checks: "-*,\
|
||||
|
||||
# cppcoreguidelines-prefer-member-initializer
|
||||
# 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user