Skip to content

Commit

Permalink
Added simple demo ... I want to sleep :'(
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKalbertodt committed Aug 10, 2014
1 parent 93fbbd2 commit 9f94a2c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 11 deletions.
93 changes: 93 additions & 0 deletions game/CraftGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "CraftGame.hpp"

#include <functional>
#include <chrono>

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<Vec2f, Color32f>([](HotVertexSeq<Vec2f, Color32f>& 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();
}
}
19 changes: 19 additions & 0 deletions game/CraftGame.hpp
Original file line number Diff line number Diff line change
@@ -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);
};
16 changes: 5 additions & 11 deletions game/main.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
}
CraftGame game;
game.init();
game.start();
}
14 changes: 14 additions & 0 deletions shader/CraftGame.fsh
Original file line number Diff line number Diff line change
@@ -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);
}
19 changes: 19 additions & 0 deletions shader/CraftGame.vsh
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 9f94a2c

Please sign in to comment.