2016-08-27 18:54:34 +00:00
|
|
|
#include <iostream>
|
2016-08-27 19:59:52 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <main.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <GL/glew.h>
|
2016-08-28 09:31:10 +00:00
|
|
|
#include <renderer/shader.h>
|
2016-08-27 19:59:52 +00:00
|
|
|
using namespace std;
|
|
|
|
bool TestGame::init()
|
|
|
|
{
|
2016-08-28 09:31:10 +00:00
|
|
|
GLint link_ok = GL_FALSE;
|
2016-08-27 19:59:52 +00:00
|
|
|
//Compile Vertex shader
|
2016-08-28 09:31:10 +00:00
|
|
|
vs = new VertexShader("shaders/triangle.v.glsl");
|
|
|
|
if(!vs->compile()) {
|
|
|
|
throw nullptr;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
|
|
|
//Compile Fragment shader
|
2016-08-28 09:31:10 +00:00
|
|
|
fs = new FragmentShader("shaders/triangle.f.glsl");
|
|
|
|
if(!fs->compile()) {
|
|
|
|
throw nullptr;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
|
|
|
//Linking
|
2016-08-28 09:31:10 +00:00
|
|
|
vector<Shader *> shaders;
|
|
|
|
shaders.push_back(vs);
|
|
|
|
shaders.push_back(fs);
|
|
|
|
program=link(shaders);
|
|
|
|
//Set attributes
|
2016-08-27 19:59:52 +00:00
|
|
|
const char* attribute_name = "coord2d";
|
|
|
|
attribute_coord2d = glGetAttribLocation(program, attribute_name);
|
|
|
|
if (attribute_coord2d == -1) {
|
|
|
|
cerr << "Could not bind attribute " << attribute_name << endl;
|
2016-08-28 09:31:10 +00:00
|
|
|
throw nullptr;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
2016-08-28 10:20:52 +00:00
|
|
|
attribute_name = "v_color";
|
|
|
|
attribute_v_color = glGetAttribLocation(program, attribute_name);
|
|
|
|
if (attribute_v_color == -1) {
|
|
|
|
cerr << "Could not bind attribute " << attribute_name << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLfloat triangle_colors[] = {
|
|
|
|
1.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0,
|
|
|
|
1.0, 0.0, 0.0,
|
|
|
|
};
|
|
|
|
t1.init(triangle_colors,sizeof(triangle_colors));
|
|
|
|
t2.init(triangle_colors,sizeof(triangle_colors));
|
|
|
|
t3.init(triangle_colors,sizeof(triangle_colors));
|
2016-08-27 19:59:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void TestGame::stop()
|
|
|
|
{
|
|
|
|
//To be written
|
|
|
|
}
|
|
|
|
bool TestGame::tick()
|
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
while (SDL_PollEvent(&ev)) {
|
|
|
|
if (ev.type == SDL_QUIT)
|
|
|
|
return false;
|
2016-08-28 11:39:13 +00:00
|
|
|
}
|
|
|
|
//Calculate colors
|
|
|
|
if(state.bright)
|
|
|
|
state.brightness+=0.001;
|
|
|
|
else
|
|
|
|
state.brightness-=0.001;
|
|
|
|
if(state.bright && state.brightness >= 1.0)
|
|
|
|
state.bright=false;
|
|
|
|
if(!state.bright && state.brightness < 0.0)
|
|
|
|
state.bright=true;
|
|
|
|
//Apply colors
|
|
|
|
GLfloat triangle_colors[] = {
|
|
|
|
state.brightness, 0.0, 0.0,
|
|
|
|
0.0, state.brightness, 0.0,
|
|
|
|
0.0, 0.0, state.brightness,
|
|
|
|
};
|
|
|
|
t1.update(triangle_colors, sizeof(triangle_colors));
|
|
|
|
t2.update(triangle_colors, sizeof(triangle_colors));
|
|
|
|
t3.update(triangle_colors, sizeof(triangle_colors));
|
2016-08-27 19:59:52 +00:00
|
|
|
render();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void TestGame::render()
|
|
|
|
{
|
|
|
|
//Clear Background
|
|
|
|
glClearColor(1.0, 1.0, 1.0, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2016-08-28 09:59:58 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2016-08-27 19:59:52 +00:00
|
|
|
glUseProgram(program);
|
|
|
|
glEnableVertexAttribArray(attribute_coord2d);
|
2016-08-28 10:20:52 +00:00
|
|
|
glEnableVertexAttribArray(attribute_v_color);
|
2016-08-28 09:31:10 +00:00
|
|
|
|
2016-08-28 10:20:52 +00:00
|
|
|
t1.render(attribute_coord2d, attribute_v_color);
|
|
|
|
t2.render(attribute_coord2d, attribute_v_color);
|
|
|
|
t3.render(attribute_coord2d, attribute_v_color);
|
2016-08-27 19:59:52 +00:00
|
|
|
|
2016-08-28 10:20:52 +00:00
|
|
|
glDisableVertexAttribArray(attribute_v_color);
|
2016-08-27 19:59:52 +00:00
|
|
|
glDisableVertexAttribArray(attribute_coord2d);
|
|
|
|
|
|
|
|
// Display the result
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
|
|
}
|
|
|
|
TestGame::~TestGame()
|
|
|
|
{
|
|
|
|
glDeleteProgram(program);
|
2016-08-28 09:31:10 +00:00
|
|
|
delete fs;
|
|
|
|
delete vs;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
2016-08-27 18:54:34 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2016-08-28 09:31:10 +00:00
|
|
|
TestGame game;
|
|
|
|
game.start();
|
|
|
|
return 0;
|
2016-08-27 18:54:34 +00:00
|
|
|
}
|