diff --git a/game/CraftGame.cpp b/game/CraftGame.cpp new file mode 100644 index 0000000..7df70f3 --- /dev/null +++ b/game/CraftGame.cpp @@ -0,0 +1,93 @@ +#include "CraftGame.hpp" + +#include +#include + +using namespace lumina; +using namespace std; + +CraftGame::CraftGame() { + m_running = true; +} + + +void CraftGame::init() { + // configure window + m_window.setTitle("CG Praktikum 2014 :)"); + m_window.setVersionHint(3, 3); + + // add event callback (capture by reference) + m_window.addEventCallback([&](InputEvent e) { + // if the inputType was a KeyInput and the key was just pressed and the + // key was Escape -> set m_running to false to stop program + if(e.type == InputType::Key && e.keyInput.type == KeyInputType::Pressed + && e.keyInput.key == KeyCode::Escape) { + m_running = false; + return EventResult::Processed; + } + return EventResult::Skipped; + }); + + // resize window + m_window.resize(Vec2i(800, 600)); +} + +void CraftGame::start() { + // open the window (we need to call init before!) + m_window.open(); + + // obtain and create render context + auto& renderContext = m_window.getRenderContext(); + renderContext.create(); + + // we just need one context, so we can prime it here just once + renderContext.prime([this](HotRenderContext& hotContext) { + this->run(hotContext); + }); +} + +void CraftGame::run(lumina::HotRenderContext& hotContext) { + // create VertexSeq that represents a triangle + VertexSeq triangle; + triangle.create(5, 3); // 2+3 floats, 3 vertices + triangle.prime([](HotVertexSeq& hot){ + hot.vertex[0].set(Vec2f(-1.f, -1.f), Color32f(1, 0, 0)); + hot.vertex[1].set(Vec2f( 0.f, 1.f), Color32f(0, 1, 0)); + hot.vertex[2].set(Vec2f( 1.f, -1.f), Color32f(0, 0, 1)); + }); + + + // load and compile vertex and fragment shader + VShader vs; + vs.compile(loadShaderFromFile("shader/CraftGame.vsh")); + FShader fs; + fs.compile(loadShaderFromFile("shader/CraftGame.fsh")); + + // create program and link the two shaders + Program p; + p.create(vs, fs); + + + // run as long as the window is valid and the user hasn't pessed ESC + while(m_running && m_window.isValid()) { + // poll events + m_window.update(); + + + // we need the default FrameBuffer + hotContext.getDefaultFrameBuffer().prime([&](HotFrameBuffer& hotFB) { + // clear the background color of the screen + hotFB.clearColor(0, Color32fA(0, 0, 0, 1)); + + // prime program to use it + p.prime([&](HotProgram& hot) { + // draw the triangle + hot.draw(triangle, PrimitiveType::Triangle); + }); + }); + + + // swap buffer + hotContext.swapBuffer(); + } +} diff --git a/game/CraftGame.hpp b/game/CraftGame.hpp new file mode 100644 index 0000000..f19eff3 --- /dev/null +++ b/game/CraftGame.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "lumina/lumina.hpp" + +class CraftGame { +public: + CraftGame(); + + void init(); + void start(); + +private: + lumina::Window m_window; + bool m_running; + + void run(lumina::HotRenderContext& hotContext); + + lumina::EventResult onEvent(lumina::InputEvent e); +}; diff --git a/game/main.cpp b/game/main.cpp index 167d2fa..227b0ba 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,13 +1,7 @@ -#include "lumina/lumina.hpp" -using namespace lum; +#include "CraftGame.hpp" int main() { - Window win("CG Praktikum :-)"); - win.setVersionHint(3, 3); - win.open(); - slog(" Opened Window :)"); - - while(win.isValid()) { - win.update(); - } -} \ No newline at end of file + CraftGame game; + game.init(); + game.start(); +} diff --git a/shader/CraftGame.fsh b/shader/CraftGame.fsh new file mode 100644 index 0000000..1e21fbd --- /dev/null +++ b/shader/CraftGame.fsh @@ -0,0 +1,14 @@ +#version 330 core + +// we get the interpolated color from the vertex shader +in VertexData { + vec3 color; +} inData; + +// we need to output a color +out vec4 o_color; + + +void main() { + o_color = vec4(inData.color, 1); +} \ No newline at end of file diff --git a/shader/CraftGame.vsh b/shader/CraftGame.vsh new file mode 100644 index 0000000..df720aa --- /dev/null +++ b/shader/CraftGame.vsh @@ -0,0 +1,19 @@ +#version 330 core + +// Our vertex consists just of a Vec2f (aka vec2) which represents position +layout(location = 0) in vec2 i_pos; +layout(location = 1) in vec3 i_color; + +// forward color to fragment shader (will be linear interpolated) +out VertexData { + vec3 color; +} outData; + + +void main() { + // just forward position + outData.color = i_color; + + // write gl_Position -> make OpenGL happy + gl_Position = vec4(i_pos, 0, 1); +} \ No newline at end of file