Shows the basic usage of OGLplus with SDL and GLEW.
Copyright 2008-2014 Matus Chochlik. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <cassert>
#include <iostream>
#include <SDL/SDL.h>
#include <GL/glew.h>
class Example
{
private:
public:
Example(void)
{
using namespace oglplus;
vs.Source(" \
#version 330\n \
in vec3 Position; \
void main(void) \
{ \
gl_Position = vec4(Position, 1.0); \
} \
");
vs.Compile();
fs.Source(" \
#version 330\n \
out vec4 fragColor; \
void main(void) \
{ \
fragColor = vec4(1.0, 0.0, 0.0, 1.0); \
} \
");
fs.Compile();
prog.AttachShader(vs);
prog.AttachShader(fs);
prog.Link();
prog.Use();
triangle.Bind();
GLfloat triangle_verts[9] = {
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
verts.Bind(Buffer::Target::Array);
Buffer::Data(
Buffer::Target::Array,
9,
triangle_verts
);
VertexArrayAttrib vert_attr(prog, "Position");
vert_attr.Setup<GLfloat>(3);
vert_attr.Enable();
gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void Display(void)
{
using namespace oglplus;
gl.Clear().ColorBuffer();
gl.DrawArrays(PrimitiveType::Triangles, 0, 3);
}
};
class WrapSDL
{
private:
bool done;
public:
WrapSDL(const char* title)
: done(false)
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
throw std::runtime_error(SDL_GetError());
if(!SDL_SetVideoMode(
800, 600, 32,
SDL_HWSURFACE |
SDL_GL_DOUBLEBUFFER |
SDL_OPENGL
))
{
std::runtime_error e(SDL_GetError());
SDL_Quit();
throw e;
}
SDL_WM_SetCaption(title, nullptr);
}
~WrapSDL(void)
{
SDL_Quit();
}
void ProcessEvents(void)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
done = true;
}
}
}
bool Done(void) const
{
return done;
}
void SwapBuffers(void)
{
SDL_GL_SwapBuffers();
}
};
class WrapGLEW
{
public:
WrapGLEW(void)
{
GLenum res = glewInit();
if(res != GLEW_OK)
{
throw std::runtime_error(
(const char*)glewGetErrorString(res)
);
}
glGetError();
}
};
int main(int argc, char* argv[])
{
try
{
WrapSDL wrap_sdl("OGLplus+SDL+GLEW");
WrapGLEW wrap_glew;
do
{
example.Display();
wrap_sdl.SwapBuffers();
wrap_sdl.ProcessEvents();
}
while(!wrap_sdl.Done());
return 0;
}
{
std::cerr
<< "Error (in "
<< "'): "
<< err.what()
<< " ["
<< ":"
<< "] "
<< std::endl;
}
catch(std::runtime_error& sre)
{
std::cerr << "Runtime error: " << sre.what() << std::endl;
}
return 1;
}