summaryrefslogtreecommitdiffstats
path: root/src/Render.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-29 16:55:16 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-29 16:55:16 +0200
commitf942405184c2d6067fb5303b58a225edf7e452b1 (patch)
tree83e70c7e3019e5b195c9caf41194b2113fa76d7f /src/Render.cpp
parent2017-07-26 (diff)
downloadAltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.gz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.bz2
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.lz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.xz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.zst
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.zip
Diffstat (limited to 'src/Render.cpp')
-rw-r--r--src/Render.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/Render.cpp b/src/Render.cpp
new file mode 100644
index 0000000..ebfbc20
--- /dev/null
+++ b/src/Render.cpp
@@ -0,0 +1,137 @@
+#include "Render.hpp"
+
+#include "Utility.hpp"
+#include "Shader.hpp"
+#include "AssetManager.hpp"
+#include "Event.hpp"
+
+Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) {
+ InitSfml(windowWidth, windowHeight, windowTitle);
+ glCheckError();
+ InitGlew();
+ glCheckError();
+ PrepareToRendering();
+ glCheckError();
+}
+
+Render::~Render() {
+ delete window;
+}
+
+void Render::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string WinTitle) {
+ LOG(INFO) << "Creating window: " << WinWidth << "x" << WinHeight << " \"" << WinTitle << "\"";
+ sf::ContextSettings contextSetting;
+ contextSetting.majorVersion = 3;
+ contextSetting.minorVersion = 3;
+ contextSetting.attributeFlags = contextSetting.Core;
+ contextSetting.depthBits = 24;
+ window = new sf::Window(sf::VideoMode(WinWidth, WinHeight), WinTitle, sf::Style::Default, contextSetting);
+ glCheckError();
+ window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2,
+ sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2));
+ SetMouseCapture(false);
+}
+
+void Render::InitGlew() {
+ LOG(INFO) << "Initializing GLEW";
+ glewExperimental = GL_TRUE;
+ GLenum glewStatus = glewInit();
+ glCheckError();
+ if (glewStatus != GLEW_OK) {
+ LOG(FATAL) << "Failed to initialize GLEW: " << glewGetErrorString(glewStatus);
+ }
+ glViewport(0, 0, window->getSize().x, window->getSize().y);
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_CULL_FACE);
+ glCullFace(GL_BACK);
+ glFrontFace(GL_CCW);
+ //glEnable(GL_BLEND);
+ //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glCheckError();
+}
+
+void Render::PrepareToRendering() {
+ shader = new Shader("./shaders/face.vs", "./shaders/face.fs");
+ shader->Use();
+
+ //TextureAtlas texture
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas());
+ glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0);
+}
+
+void Render::RenderFrame() {
+ glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+
+ window->display();
+}
+
+void Render::HandleEvents() {
+ sf::Event event;
+ while (window->pollEvent(event)) {
+ switch (event.type) {
+ case sf::Event::Closed:
+ LOG(INFO) << "Received close event by window closing";
+ isRunning = false;
+ break;
+ case sf::Event::Resized:
+ glViewport(0, 0, window->getSize().x, window->getSize().y);
+ break;
+ case sf::Event::KeyPressed:
+ if (!window->hasFocus()) break;
+ switch (event.key.code) {
+ case sf::Keyboard::Escape:
+ LOG(INFO) << "Received close event by esc";
+ isRunning = false;
+ break;
+ case sf::Keyboard::T:
+ SetMouseCapture(!isMouseCaptured);
+ break;
+ default:
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ if (window->hasFocus()) {
+ /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) gameState->HandleMovement(GameState::FORWARD, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) gameState->HandleMovement(GameState::BACKWARD, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) gameState->HandleMovement(GameState::LEFT, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) gameState->HandleMovement(GameState::RIGHT, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) gameState->HandleMovement(GameState::JUMP, deltaTime);*/
+ }
+}
+
+void Render::HandleMouseCapture() {
+ sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
+ sf::Vector2i center = sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2);
+ sf::Mouse::setPosition(center, *window);
+ mouseXDelta = (mousePos - center).x, mouseYDelta = (center - mousePos).y;
+ const float Sensetivity = 0.7f;
+ //gameState->HandleRotation(mouseXDelta * Sensetivity, mouseYDelta * Sensetivity);
+}
+
+void Render::SetMouseCapture(bool IsCaptured) {
+ window->setMouseCursorVisible(!isMouseCaptured);
+ sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), *window);
+ isMouseCaptured = IsCaptured;
+ window->setMouseCursorVisible(!IsCaptured);
+}
+
+void Render::ExecuteRenderLoop() {
+ using namespace std::chrono_literals;
+ LoopExecutionTimeController timer(16ms);
+ while (isRunning) {
+ HandleEvents();
+ if (isMouseCaptured) HandleMouseCapture();
+ glCheckError();
+
+ RenderFrame();
+ timer.Update();
+ }
+ EventData data = GlobalAppStateData{GlobalState::Exiting};
+ EventAgregator::PushEvent(EventType::GlobalAppState,data);
+}