Skip to content

Instantly share code, notes, and snippets.

@noio
Created February 3, 2014 15:53
Show Gist options
  • Save noio/8786461 to your computer and use it in GitHub Desktop.
Save noio/8786461 to your computer and use it in GitHub Desktop.
#include "engine.h"
#include <iostream>
#include "libs/Matrices.h"
#include "utility.h"
using std::cout;
using std::cerr;
using std::endl;
// Constructor for Engine
// Takes width and height of screen
Engine::Engine(int res_width, int res_height)
: res_width_(res_width),
res_height_(res_height),
window_(sf::VideoMode(res_width, res_height), "x", sf::Style::Default, sf::ContextSettings(0,0,0,3,2)),
flow_(res_width_, res_height_)
{
target_fps_ = 30;
window_.setFramerateLimit(target_fps_);
cout << "OpenGL " << window_.getSettings().majorVersion << "." << window_.getSettings().minorVersion << endl;
Init();
cout << "GLSL " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
}
//
// Calls initializers for SDL, openCV and openGL
//
void Engine::Init(){
cout << "Init Engine" << endl;
flow_.Next();
//Vertices of a triangle
float data[] = {100.0, 0.0, 0.0, 1.0f,
0.0, 100.0, 0.0, 1.0f,
100.0, 100.0, 0.0, 1.0f};
glGenBuffers(1, &vertex_buffer_);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}
//
// Run the game loop
//
void Engine::Run(){
Loop();
}
//
// Cleanup
//
void Engine::Clean(){
}
//========= Privates ==========//
//
// The main game loop
//
void Engine::Loop(){
sf::Time frame_end;
running_ = true;
while (running_ && window_.isOpen()) {
clock_.restart();
Update();
Display();
frame_end = clock_.getElapsedTime();
cout << frame_end.asMilliseconds() << "ms" << endl;
window_.display();
}
}
//
// Update game
//
void Engine::Update(){
HandleEvents();
flow_.Next();
}
//
// Handle SDL Events
//
void Engine::HandleEvents(){
sf::Event event;
while (window_.pollEvent(event))
{
// Close window : exit
switch (event.type) {
case sf::Event::Closed:
window_.close();
running_ = false;
break;
default:
break;
}
}
}
//
// Render
//
void Engine::Display(){
window_.clear();
glColor3f(1.0f, 1.0f, 1.0f);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(4, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment