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 "physics/Solver.h"
|
2021-10-02 21:17:03 +03:00
|
|
|
#include "animation/Timeline.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
Engine::Engine() {
|
|
|
|
screen = std::make_shared<Screen>();
|
2021-09-19 15:44:31 +03:00
|
|
|
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) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_name = name;
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
screen->open(screenWidth, screenHeight, name, verticalSync, background, style);
|
2021-09-19 15:44:31 +03:00
|
|
|
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);
|
2021-09-19 15:44:31 +03:00
|
|
|
mouse->setMouseInCenter();
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
while (screen->isOpen()) {
|
|
|
|
screen->clear();
|
|
|
|
|
|
|
|
Time::update();
|
2021-09-19 11:25:10 +03:00
|
|
|
update();
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
/* Project all mesh
|
2021-09-19 11:25:10 +03:00
|
|
|
* Here we project all _tris for each body from world._objects.
|
2021-09-13 15:53:43 +03:00
|
|
|
* When we call camera.project(m.second),
|
|
|
|
*/
|
|
|
|
|
|
|
|
// sometimes we dont need to update physics world
|
|
|
|
// (for example in menu or while pause)
|
2021-09-19 11:25:10 +03:00
|
|
|
// hence we can set '_updateWorld' equal to false in setUpdateWorld(bool):
|
|
|
|
if(_updateWorld) {
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-02 21:17:03 +03:00
|
|
|
Timeline::update();
|
2021-09-19 11:25:10 +03:00
|
|
|
|
2021-10-02 21:17:03 +03:00
|
|
|
camera->clear();
|
2021-09-19 11:25:10 +03:00
|
|
|
world->update();
|
|
|
|
world->projectObjectsInCamera(camera);
|
|
|
|
|
|
|
|
// draw projected body
|
|
|
|
for (auto &t : camera->sorted())
|
2021-10-12 17:12:47 +03:00
|
|
|
screen->drawTriangle(*t);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
_triPerSec = camera->buffSize() * Time::fps();
|
|
|
|
|
|
|
|
printDebugText();
|
|
|
|
gui();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
screen->display();
|
|
|
|
}
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::exit() {
|
|
|
|
if(screen->isOpen()) {
|
|
|
|
screen->close();
|
|
|
|
}
|
|
|
|
ResourceManager::unloadAllResources();
|
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 '" +
|
2021-09-19 15:44:31 +03:00
|
|
|
screen->title() + "'.");
|
2021-09-19 11:25:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::printDebugText() const {
|
|
|
|
if (_debugText) {
|
|
|
|
screen->debugText(_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-09-19 11:25:10 +03:00
|
|
|
std::to_string(screen->height()) + "\n" +
|
|
|
|
std::to_string(Time::fps()) +
|
2021-10-09 13:41:12 +03:00
|
|
|
" fps \n" + std::to_string((int) _triPerSec) + " tris/s");
|
2021-09-19 11:25:10 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|