Add example usage of the RAII opengl wrappers
This commit is contained in:
parent
eb334a2a90
commit
0eb54012dc
70
src/main.cpp
70
src/main.cpp
@ -15,6 +15,11 @@
|
||||
#include "audio/mixdata.hpp"
|
||||
#include "audio/mixer.hpp"
|
||||
|
||||
#include "graphics/vao.hpp"
|
||||
#include "graphics/vbo.hpp"
|
||||
#include "graphics/shader.hpp"
|
||||
#include "graphics/shader_program.hpp"
|
||||
|
||||
// 0 | 1 | 2
|
||||
// ---------
|
||||
// 3 | 4 | 5
|
||||
@ -142,24 +147,77 @@ void gay_thread(audio::mixer& m, std::atomic_bool& should_gay_thread_stop){
|
||||
}
|
||||
}
|
||||
|
||||
graphics::shader_program create_example_shader(){
|
||||
static constexpr const char vertex_source[] = "#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;"
|
||||
"void main(){"
|
||||
"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);"
|
||||
"}";
|
||||
static constexpr const char fragment_source[] = "#version 330 core\n"
|
||||
"out vec4 FragColor;"
|
||||
"void main(){"
|
||||
"FragColor = vec4(0.0, 0.5, 0.6, 1.0);"
|
||||
"}";
|
||||
graphics::shader_program prog;
|
||||
graphics::shader vert(vertex_source, graphics::shader::type::VERTEX);
|
||||
graphics::shader frag(fragment_source, graphics::shader::type::FRAGMENT);
|
||||
bool error = false;
|
||||
if(vert.has_compile_error()){
|
||||
debug_print("%s\n", vert.get_error().c_str());
|
||||
error = true;
|
||||
}
|
||||
if(frag.has_compile_error()){
|
||||
debug_print("%s\n", frag.get_error().c_str());
|
||||
error = true;
|
||||
}
|
||||
if(error)
|
||||
return prog;
|
||||
prog.attach_shaders(vert, frag);
|
||||
prog.link();
|
||||
if(prog.has_link_error()){
|
||||
debug_print("%s\n", prog.get_error().c_str());
|
||||
}
|
||||
return prog;
|
||||
}
|
||||
|
||||
int main(){
|
||||
srand(time(NULL));
|
||||
audio::mixer mix(audio::mixer::mode::STEREO, 1, 44100);
|
||||
game_state gs = {};
|
||||
|
||||
manager.init(640, 480, "Tic-Tac-Gugh");
|
||||
manager.handle_window_close_event(handle_window_close);
|
||||
manager.handle_keypress_event(handle_input_events);
|
||||
|
||||
std::atomic_bool should_gay_thread_stop = false;
|
||||
std::thread t(gay_thread, std::ref(mix), std::ref(should_gay_thread_stop));
|
||||
static constexpr float vertices[] = {
|
||||
-0.5f, -0.5f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f,
|
||||
0.0f, 0.5f, 0.0f
|
||||
};
|
||||
|
||||
while(running)
|
||||
//create our gl objects
|
||||
graphics::vbo vbo(sizeof(vertices), graphics::vbo::usage::STATIC_DRAW);
|
||||
graphics::shader_program prog = create_example_shader();
|
||||
graphics::vao vao;
|
||||
|
||||
//put the vertex data in the gl state
|
||||
vbo.buffer(vertices, sizeof(vertices));
|
||||
|
||||
//tell gl how to interpret the data in the vbo
|
||||
vao.bind();
|
||||
vbo.bind(graphics::buffer::array);
|
||||
auto attrib = vao.get_attribute(0);
|
||||
attrib.set_float_pointer(3, 3, 0);
|
||||
attrib.enable_array_mode();
|
||||
|
||||
while(running){
|
||||
//render using program 'prog' with data from 'vao'
|
||||
prog.use();
|
||||
vao.bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
manager.update();
|
||||
}
|
||||
|
||||
should_gay_thread_stop = true;
|
||||
while(exists_empty_tile(gs) && gs.turn != -1){
|
||||
game_turn(gs, get_player_input());
|
||||
}
|
||||
t.join();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user