shooter/engine/Engine.cpp

115 lines
3.5 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 14.01.2021.
//
#include "Engine.h"
#include "utils/Time.h"
#include <iostream>
#include "ResourceManager.h"
#include "animation/Timeline.h"
2021-10-17 19:38:16 +03:00
#include "SoundController.h"
2021-09-13 15:53:43 +03:00
Engine::Engine() {
2021-10-17 19:38:16 +03:00
Time::init();
Timeline::init();
ResourceManager::init();
SoundController::init();
2021-09-13 15:53:43 +03:00
screen = std::make_shared<Screen>();
keyboard = std::make_shared<Keyboard>();
mouse = std::make_shared<Mouse>();
2021-09-13 15:53:43 +03:00
world = std::make_shared<World>();
camera = std::make_shared<Camera>();
}
void Engine::create(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, sf::Uint32 style) {
_name = name;
2021-09-13 15:53:43 +03:00
screen->open(screenWidth, screenHeight, name, verticalSync, background, style);
screen->attachMouse(mouse);
2021-09-13 15:53:43 +03:00
2021-10-09 13:41:12 +03:00
Log::log("Engine::create(): started engine (" + std::to_string(screenWidth) + "x" + std::to_string(screenHeight) + ") with title '" + name + "'.");
2021-09-13 15:53:43 +03:00
Time::update();
start();
camera->init(screenWidth, screenHeight);
mouse->setMouseInCenter();
2021-09-13 15:53:43 +03:00
while (screen->isOpen()) {
screen->clear();
Time::update();
update();
2021-09-13 15:53:43 +03:00
// sometimes we dont need to update physics world
// (for example in menu or while pause)
// hence we can set '_updateWorld' equal to false in setUpdateWorld(bool):
if(_updateWorld) {
2021-09-13 15:53:43 +03:00
Timeline::update();
world->update();
2021-09-13 15:53:43 +03:00
2021-10-22 19:42:32 +03:00
if(_useOpenGL) {
GLfloat* view = camera->view();
for(auto & it : *world) {
if (it.second->isVisible()) {
GLfloat* geometry = Screen::glMeshToGLfloatArray(it.second, camera->position());
screen->glDrawMesh(geometry, view, 3 * it.second->triangles().size());
free(geometry);
}
}
free(view);
} else {
// clear triangles from previous frame
camera->clear();
// project triangles to the camera plane
for(auto & it : *world)
camera->project(it.second);
// draw triangles on the screen
for (auto &t : camera->sorted())
screen->drawTriangle(*t);
_triPerSec = camera->buffSize() * Time::fps();
}
printDebugText();
gui();
2021-09-13 15:53:43 +03:00
}
screen->display();
}
}
void Engine::exit() {
if(screen->isOpen()) {
screen->close();
}
2021-10-17 19:38:16 +03:00
SoundController::free();
ResourceManager::free();
Timeline::free();
Time::free();
2021-10-09 13:41:12 +03:00
Log::log("Engine::exit(): exit engine (" + std::to_string(screen->width()) + "x" + std::to_string(screen->height()) + ") with title '" +
screen->title() + "'.");
}
void Engine::printDebugText() const {
if (_debugText) {
2021-10-22 19:42:32 +03:00
std::string str = _name + "\n\n X: " +
2021-10-02 20:36:07 +03:00
std::to_string((camera->position().x())) + "\n Y: " +
std::to_string((camera->position().y())) + "\n Z: " +
std::to_string((camera->position().z())) + "\n\n" +
2021-10-09 13:41:12 +03:00
std::to_string(screen->width()) + "x" +
2021-10-22 19:42:32 +03:00
std::to_string(screen->height()) + "\t" +
std::to_string(Time::fps()) + " fps";
if(_useOpenGL) {
str += "\n Using OpenGL acceleration";
} else {
str += "\n" + std::to_string((int) _triPerSec) + " tris/s";
}
screen->debugText(str);
}
2021-09-13 15:53:43 +03:00
}