138 lines
2.6 KiB
C++
138 lines
2.6 KiB
C++
#ifndef __OPENGLPLAYGROUND_COMMON_HH__
|
|
#define __OPENGLPLAYGROUND_COMMON_HH__
|
|
|
|
#include <string>
|
|
#include <cstring>
|
|
#include <cstdio>
|
|
#include <cerrno>
|
|
|
|
#include <glbinding/gl/gl.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
using namespace gl;
|
|
|
|
class Exception {
|
|
public:
|
|
Exception() {
|
|
}
|
|
|
|
virtual ~Exception() {}
|
|
|
|
virtual std::string toString() const = 0;
|
|
};
|
|
|
|
class POSIXException : public Exception {
|
|
public:
|
|
POSIXException(int err): Exception(), err_(err) {
|
|
}
|
|
|
|
int getErr() const {return err_;}
|
|
|
|
std::string toString() const override {
|
|
return std::string("POSIXException: ") + std::strerror(err_);
|
|
}
|
|
|
|
private:
|
|
int err_;
|
|
};
|
|
|
|
class GLException : public Exception {
|
|
public:
|
|
GLException(GLenum err) : Exception(), err_(err) {
|
|
}
|
|
|
|
GLenum getErr() const {return err_;}
|
|
|
|
std::string errToString() const {
|
|
std::string ret;
|
|
if (err_ == GL_INVALID_ENUM)
|
|
ret += "GL_INVALID_ENUM ";
|
|
if (err_ == GL_INVALID_VALUE)
|
|
ret += "GL_INVALID_VALUE ";
|
|
if (err_ == GL_INVALID_OPERATION)
|
|
ret += "GL_INVALID_OPERATION ";
|
|
if (err_ == GL_INVALID_FRAMEBUFFER_OPERATION)
|
|
ret += "GL_INVALID_FRAMEBUFFER_OPERATION ";
|
|
if (err_ == GL_OUT_OF_MEMORY)
|
|
ret += "GL_OUT_OF_MEMORY ";
|
|
if (err_ == GL_STACK_UNDERFLOW)
|
|
ret += "GL_STACK_UNDERFLOW ";
|
|
if (err_ == GL_STACK_OVERFLOW)
|
|
ret += "GL_STACK_OVERFLOW ";
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::string toString() const override {
|
|
return "GLException: " + errToString() + "(" + std::to_string(static_cast<int>(err_)) + ")";
|
|
}
|
|
|
|
private:
|
|
GLenum err_;
|
|
};
|
|
|
|
class SDLException : public Exception {
|
|
public:
|
|
SDLException() : Exception(), msg_(SDL_GetError()) {
|
|
}
|
|
|
|
std::string toString() const override {
|
|
return "SDLException: " + msg_;
|
|
}
|
|
|
|
private:
|
|
std::string msg_;
|
|
};
|
|
|
|
class TTFException : public Exception {
|
|
public:
|
|
TTFException() : Exception(), msg_(TTF_GetError()) {
|
|
}
|
|
|
|
std::string toString() const override {
|
|
return "TTFException: " + msg_;
|
|
}
|
|
|
|
private:
|
|
std::string msg_;
|
|
};
|
|
|
|
static void checkGlError() {
|
|
GLenum err;
|
|
if ((err = glGetError()) != GL_NO_ERROR)
|
|
throw GLException(err);
|
|
}
|
|
|
|
static std::string fileToString(std::string const& name) {
|
|
std::FILE *file = std::fopen(name.c_str(), "r");
|
|
if (!file) {
|
|
throw POSIXException(errno);
|
|
}
|
|
|
|
std::string ret;
|
|
char buf[512];
|
|
std::size_t p;
|
|
while((p = std::fread(buf, 1, 511, file)) > 0) {
|
|
buf[p] = '\0';
|
|
ret.append(buf);
|
|
}
|
|
if (!std::feof(file)) {
|
|
std::fclose(file);
|
|
throw POSIXException(errno);
|
|
}
|
|
|
|
std::fclose(file);
|
|
return ret;
|
|
}
|
|
|
|
enum class VAFormats {
|
|
Vertex,
|
|
VertexTexcoord,
|
|
VertexNormal,
|
|
VertexNormalTexcoord
|
|
};
|
|
|
|
#endif
|