mirror of
https://gitea.windcorp.ru/Wind-Corporation/Progressia.git
synced 2025-04-21 19:20:45 +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->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;
|
||||||
}
|
}
|
||||||
|
250
main/game.cpp
250
main/game.cpp
@ -18,163 +18,179 @@ 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;
|
|
||||||
std::unique_ptr<View> perspective;
|
|
||||||
std::unique_ptr<Light> light;
|
|
||||||
|
|
||||||
GraphicsInterface *gint;
|
DISABLE_COPYING(GameImpl)
|
||||||
|
DISABLE_MOVING(GameImpl)
|
||||||
|
|
||||||
void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
|
public:
|
||||||
glm::vec4 color, std::vector<Vertex> &vertices,
|
std::unique_ptr<Primitive> cube1;
|
||||||
std::vector<Vertex::Index> &indices) {
|
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;
|
||||||
|
|
||||||
Vertex::Index offset = vertices.size();
|
GraphicsInterface *gint;
|
||||||
|
|
||||||
vertices.push_back({origin, color, {}, {0, 0}});
|
static void addRect(glm::vec3 origin, glm::vec3 width, glm::vec3 height,
|
||||||
vertices.push_back({origin + width, color, {}, {0, 1}});
|
glm::vec4 color, std::vector<Vertex> &vertices,
|
||||||
vertices.push_back({origin + width + height, color, {}, {1, 1}});
|
std::vector<Vertex::Index> &indices) {
|
||||||
vertices.push_back({origin + height, color, {}, {1, 0}});
|
|
||||||
|
|
||||||
indices.push_back(offset + 0);
|
Vertex::Index offset = vertices.size();
|
||||||
indices.push_back(offset + 1);
|
|
||||||
indices.push_back(offset + 2);
|
|
||||||
|
|
||||||
indices.push_back(offset + 0);
|
vertices.push_back({origin, color, {}, {0, 0}});
|
||||||
indices.push_back(offset + 2);
|
vertices.push_back({origin + width, color, {}, {0, 1}});
|
||||||
indices.push_back(offset + 3);
|
vertices.push_back({origin + width + height, color, {}, {1, 1}});
|
||||||
}
|
vertices.push_back({origin + height, color, {}, {1, 0}});
|
||||||
|
|
||||||
void addBox(glm::vec3 origin, glm::vec3 length, glm::vec3 height,
|
indices.push_back(offset + 0);
|
||||||
glm::vec3 depth, std::array<glm::vec4, 6> colors,
|
indices.push_back(offset + 1);
|
||||||
std::vector<Vertex> &vertices,
|
indices.push_back(offset + 2);
|
||||||
std::vector<Vertex::Index> &indices) {
|
|
||||||
addRect(origin, height, length, colors[0], vertices, indices);
|
|
||||||
addRect(origin, length, depth, colors[1], vertices, indices);
|
|
||||||
addRect(origin, depth, height, colors[2], vertices, indices);
|
|
||||||
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) {
|
indices.push_back(offset + 0);
|
||||||
|
indices.push_back(offset + 2);
|
||||||
|
indices.push_back(offset + 3);
|
||||||
|
}
|
||||||
|
|
||||||
debug("game init begin");
|
static void addBox(glm::vec3 origin, glm::vec3 length, glm::vec3 height,
|
||||||
gint = &gintp;
|
glm::vec3 depth, std::array<glm::vec4, 6> colors,
|
||||||
|
std::vector<Vertex> &vertices,
|
||||||
|
std::vector<Vertex::Index> &indices) {
|
||||||
|
addRect(origin, height, length, colors[0], vertices, indices);
|
||||||
|
addRect(origin, length, depth, colors[1], vertices, indices);
|
||||||
|
addRect(origin, depth, height, colors[2], vertices, indices);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
texture1 =
|
GameImpl(GraphicsInterface &gintp) {
|
||||||
gint->newTexture(progressia::main::loadImage("assets/texture.png"));
|
|
||||||
texture2 =
|
|
||||||
gint->newTexture(progressia::main::loadImage("assets/texture2.png"));
|
|
||||||
|
|
||||||
// Cube 1
|
debug("game init begin");
|
||||||
{
|
gint = &gintp;
|
||||||
std::vector<Vertex> vertices;
|
|
||||||
std::vector<Vertex::Index> indices;
|
|
||||||
auto white = glm::vec4(1, 1, 1, 1);
|
|
||||||
|
|
||||||
addBox({-0.5, -0.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1},
|
texture1 =
|
||||||
{white, white, white, white, white, white}, vertices, indices);
|
gint->newTexture(progressia::main::loadImage("assets/texture.png"));
|
||||||
|
texture2 = gint->newTexture(
|
||||||
|
progressia::main::loadImage("assets/texture2.png"));
|
||||||
|
|
||||||
for (std::size_t i = 0; i < indices.size(); i += 3) {
|
// Cube 1
|
||||||
Vertex &a = vertices[indices[i + 0]];
|
{
|
||||||
Vertex &b = vertices[indices[i + 1]];
|
std::vector<Vertex> vertices;
|
||||||
Vertex &c = vertices[indices[i + 2]];
|
std::vector<Vertex::Index> indices;
|
||||||
|
auto white = glm::vec4(1, 1, 1, 1);
|
||||||
|
|
||||||
glm::vec3 x = b.position - a.position;
|
addBox({-0.5, -0.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1},
|
||||||
glm::vec3 y = c.position - a.position;
|
{white, white, white, white, white, white}, vertices,
|
||||||
|
indices);
|
||||||
|
|
||||||
glm::vec3 normal = glm::normalize(glm::cross(x, y));
|
for (std::size_t i = 0; i < indices.size(); i += 3) {
|
||||||
|
Vertex &a = vertices[indices[i + 0]];
|
||||||
|
Vertex &b = vertices[indices[i + 1]];
|
||||||
|
Vertex &c = vertices[indices[i + 2]];
|
||||||
|
|
||||||
a.normal = normal;
|
glm::vec3 x = b.position - a.position;
|
||||||
b.normal = normal;
|
glm::vec3 y = c.position - a.position;
|
||||||
c.normal = normal;
|
|
||||||
|
glm::vec3 normal = glm::normalize(glm::cross(x, y));
|
||||||
|
|
||||||
|
a.normal = normal;
|
||||||
|
b.normal = normal;
|
||||||
|
c.normal = normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
cube1 = gint->newPrimitive(vertices, indices, &*texture1);
|
||||||
}
|
}
|
||||||
|
|
||||||
cube1 = gint->newPrimitive(vertices, indices, &*texture1);
|
// Cube 2
|
||||||
}
|
{
|
||||||
|
std::vector<Vertex> vertices;
|
||||||
|
std::vector<Vertex::Index> indices;
|
||||||
|
auto white = glm::vec4(1, 1, 1, 1);
|
||||||
|
|
||||||
// Cube 2
|
addBox({-0.5, -2.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1},
|
||||||
{
|
{white, white, white, white, white, white}, vertices,
|
||||||
std::vector<Vertex> vertices;
|
indices);
|
||||||
std::vector<Vertex::Index> indices;
|
|
||||||
auto white = glm::vec4(1, 1, 1, 1);
|
|
||||||
|
|
||||||
addBox({-0.5, -2.5, -0.5}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1},
|
for (std::size_t i = 0; i < indices.size(); i += 3) {
|
||||||
{white, white, white, white, white, white}, vertices, indices);
|
Vertex &a = vertices[indices[i + 0]];
|
||||||
|
Vertex &b = vertices[indices[i + 1]];
|
||||||
|
Vertex &c = vertices[indices[i + 2]];
|
||||||
|
|
||||||
for (std::size_t i = 0; i < indices.size(); i += 3) {
|
glm::vec3 x = b.position - a.position;
|
||||||
Vertex &a = vertices[indices[i + 0]];
|
glm::vec3 y = c.position - a.position;
|
||||||
Vertex &b = vertices[indices[i + 1]];
|
|
||||||
Vertex &c = vertices[indices[i + 2]];
|
|
||||||
|
|
||||||
glm::vec3 x = b.position - a.position;
|
glm::vec3 normal = glm::normalize(glm::cross(x, y));
|
||||||
glm::vec3 y = c.position - a.position;
|
|
||||||
|
|
||||||
glm::vec3 normal = glm::normalize(glm::cross(x, y));
|
a.normal = normal;
|
||||||
|
b.normal = normal;
|
||||||
|
c.normal = normal;
|
||||||
|
}
|
||||||
|
|
||||||
a.normal = normal;
|
cube2 = gint->newPrimitive(vertices, indices, &*texture2);
|
||||||
b.normal = normal;
|
|
||||||
c.normal = normal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cube2 = gint->newPrimitive(vertices, indices, &*texture2);
|
perspective = gint->newView();
|
||||||
|
light = gint->newLight();
|
||||||
|
|
||||||
|
debug("game init complete");
|
||||||
}
|
}
|
||||||
|
|
||||||
perspective = gint->newView();
|
void renderTick() override {
|
||||||
light = gint->newLight();
|
|
||||||
|
|
||||||
debug("game init complete");
|
{
|
||||||
}
|
float fov = 70.0F;
|
||||||
|
|
||||||
void renderTick() {
|
auto extent = gint->getViewport();
|
||||||
|
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),
|
||||||
float fov = 70.0f;
|
glm::vec3(0.0F, 0.0F, 0.0F),
|
||||||
|
glm::vec3(0.0F, 0.0F, 1.0F));
|
||||||
|
|
||||||
auto extent = gint->getViewport();
|
perspective->configure(proj, view);
|
||||||
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),
|
perspective->use();
|
||||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
|
||||||
glm::vec3(0.0f, 0.0f, 1.0f));
|
|
||||||
|
|
||||||
perspective->configure(proj, view);
|
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);
|
||||||
|
|
||||||
|
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->use();
|
||||||
|
|
||||||
|
auto model = glm::eulerAngleYXZ(0.0F, 0.0F, gint->tmp_getTime() * 0.1F);
|
||||||
|
|
||||||
|
gint->setModelTransform(model);
|
||||||
|
cube1->draw();
|
||||||
|
cube2->draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
perspective->use();
|
~GameImpl() override {
|
||||||
|
debug("game shutdown begin");
|
||||||
|
|
||||||
float contrast = glm::sin(gint->tmp_getTime() / 3) * 0.18f + 0.18f;
|
cube1.reset();
|
||||||
glm::vec3 color0(0.60f, 0.60f, 0.70f);
|
cube2.reset();
|
||||||
glm::vec3 color1(1.10f, 1.05f, 0.70f);
|
texture1.reset();
|
||||||
|
texture2.reset();
|
||||||
|
|
||||||
float m = glm::sin(gint->tmp_getTime() / 3) * 0.5 + 0.5;
|
light.reset();
|
||||||
glm::vec3 color = m * color1 + (1 - m) * color0;
|
perspective.reset();
|
||||||
|
|
||||||
light->configure(color, glm::vec3(1.0f, -2.0f, 1.0f), contrast, 0.1f);
|
debug("game shutdown complete");
|
||||||
light->use();
|
}
|
||||||
|
};
|
||||||
|
|
||||||
auto model = glm::eulerAngleYXZ(0.0f, 0.0f, gint->tmp_getTime() * 0.1f);
|
std::unique_ptr<Game> makeGame(GraphicsInterface &gint) {
|
||||||
|
return std::make_unique<GameImpl>(gint);
|
||||||
gint->setModelTransform(model);
|
|
||||||
cube1->draw();
|
|
||||||
cube2->draw();
|
|
||||||
}
|
|
||||||
|
|
||||||
void shutdown() {
|
|
||||||
debug("game shutdown begin");
|
|
||||||
|
|
||||||
cube1.reset();
|
|
||||||
cube2.reset();
|
|
||||||
texture1.reset();
|
|
||||||
texture2.reset();
|
|
||||||
|
|
||||||
light.reset();
|
|
||||||
perspective.reset();
|
|
||||||
|
|
||||||
debug("game shutdown complete");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace progressia::main
|
} // namespace progressia::main
|
||||||
|
11
main/game.h
11
main/game.h
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user