Better error handling.
This commit is contained in:
@@ -15,7 +15,6 @@ enum class GAMESTATE: uint8_t {
|
||||
IN_MAIN_MENU = 1,
|
||||
IN_PAUSE_MENU = 2,
|
||||
IN_LEVEL_ANIMATION = 3, //A cutscene which moves entities in the world and cannot be interrupted.
|
||||
IN_QUIT = 4, //The game should close.
|
||||
};
|
||||
|
||||
enum class ENGINE_ERROR_CODE: uint8_t {
|
||||
@@ -58,8 +57,8 @@ public:
|
||||
float farPlane = 100.0f;
|
||||
float fov = 75;
|
||||
[[nodiscard]] float framerate() const;
|
||||
void takeScreenshot() const;
|
||||
void quit();
|
||||
static void takeScreenshot() ;
|
||||
void quit() const;
|
||||
void quit (ENGINE_ERROR_CODE code);
|
||||
static float getGLVersion();
|
||||
void initGL();
|
||||
|
@@ -10,14 +10,46 @@
|
||||
|
||||
using namespace J3ML;
|
||||
|
||||
void Engine::quit() {
|
||||
void Engine::quit() const {
|
||||
window->destroyWindow();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void outputErrorCode() {
|
||||
switch (engine->getError().error) {
|
||||
case ENGINE_ERROR_CODE::NO_ERROR:
|
||||
std::cerr << "NO ERROR" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::ANIM_OUT_OF_BOUNDS:
|
||||
std::cerr << "ANIM OUT OF BOUNDS" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND:
|
||||
std::cerr << "TEXTURE NOT FOUND" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::SHADER_COMPILATION_ERROR:
|
||||
std::cerr << "SHADER COMPILATION ERROR" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::SHADER_LINKING_ERROR:
|
||||
std::cerr << "SHADER LINKING ERROR" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::INVALID_SHADER_TYPE:
|
||||
std::cerr << "INVALID SHADER TYPE" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::SHADER_NOT_FOUND:
|
||||
std::cerr << "SHADER NOT FOUND" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::MODEL_NOT_FOUND:
|
||||
std::cerr << "MODEL NOT FOUND" << std::endl;
|
||||
break;
|
||||
case ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS:
|
||||
std::cerr << "MULTI-TEXTURE SIZE EXCEEDS" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
float Engine::getGLVersion()
|
||||
{
|
||||
const std::string full_gl_ver = reinterpret_cast<const char *>(glGetString(GL_VERSION));
|
||||
const std::string full_gl_ver = (const char*) (glGetString(GL_VERSION));
|
||||
const std::string gl_major = full_gl_ver.substr(0, 3);
|
||||
return std::stof(gl_major);
|
||||
}
|
||||
@@ -41,10 +73,7 @@ void Engine::initGL()
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
if (glError != GL_NO_ERROR) {
|
||||
std::cerr << "OpenGL: " << gluErrorString(glError);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
glClearColor(0.f, 0.f, 0.f, 1.f);
|
||||
glViewport(0,0,window->getSize().x,window->getSize().y);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -184,7 +213,7 @@ float Engine::framerate() const
|
||||
return 1.f / this->frameDelta;
|
||||
}
|
||||
|
||||
void Engine::takeScreenshot() const {
|
||||
void Engine::takeScreenshot() {
|
||||
int width = (int) engine->window->getSize().x;
|
||||
int height = (int) engine->window->getSize().y;
|
||||
std::vector<unsigned char> frameBuffer(3 * width * height);
|
||||
@@ -272,6 +301,7 @@ void Engine::loadConfig() {
|
||||
void Engine::setError(ENGINE_ERROR_CODE code, bool critical) {
|
||||
error.error = code;
|
||||
error.critical = critical;
|
||||
outputErrorCode();
|
||||
if (error.critical)
|
||||
engine->quit(code);
|
||||
|
||||
|
@@ -7,6 +7,5 @@ SA::sSkeleton AnimatedMoby::getSkeleton() const {
|
||||
SA::sAnimation* AnimatedMoby::getCurrentAnimation() {
|
||||
if(currentAnim >= animations.size())
|
||||
return &animations[currentAnim];
|
||||
std::cerr << "Animation Out Of Bounds." << std::endl;
|
||||
engine->setError(ENGINE_ERROR_CODE::ANIM_OUT_OF_BOUNDS, true);
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ void compilationError(GLuint shader) {
|
||||
if (!success) {
|
||||
GLchar info[512];
|
||||
glGetShaderInfoLog(shader, sizeof(info), nullptr, info);
|
||||
std::cerr << "Shader compilation error: " << info << std::endl;
|
||||
std::cerr << info << std::endl;
|
||||
engine->setError(ENGINE_ERROR_CODE::SHADER_COMPILATION_ERROR, true);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ void linkingError(GLuint shader) {
|
||||
if (!success) {
|
||||
GLchar info[512];
|
||||
glGetProgramInfoLog(shader, sizeof(info), nullptr, info);
|
||||
std::cerr << "ShaderProgram linking " << info << std::endl;
|
||||
std::cerr << info << std::endl;
|
||||
engine->setError(ENGINE_ERROR_CODE::SHADER_LINKING_ERROR, true);
|
||||
}
|
||||
}
|
||||
@@ -32,16 +32,12 @@ void linkingError(GLuint shader) {
|
||||
void Shader::load(std::string filePath, GLenum type) {
|
||||
std::ifstream file(filePath);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Couldn't Load Shader: " << filePath << std::endl;
|
||||
if (!file.is_open())
|
||||
engine->setError(ENGINE_ERROR_CODE::SHADER_NOT_FOUND, true);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (type != GL_VERTEX_SHADER && type != GL_FRAGMENT_SHADER) {
|
||||
std::cerr << "Invalid Shader Type: " << type << std::endl;
|
||||
if (type != GL_VERTEX_SHADER && type != GL_FRAGMENT_SHADER)
|
||||
engine->setError(ENGINE_ERROR_CODE::INVALID_SHADER_TYPE, true);
|
||||
}
|
||||
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
file.close();
|
||||
@@ -65,9 +61,8 @@ void Shader::load(std::string filePath, GLenum type) {
|
||||
}
|
||||
|
||||
void Shader::link() {
|
||||
if (id != NULL || vertex == NULL && fragment == NULL) {
|
||||
std::cerr << "Error Linking Shader" << std::endl;
|
||||
}
|
||||
if (id != NULL || vertex == NULL && fragment == NULL)
|
||||
engine->setError(ENGINE_ERROR_CODE::SHADER_LINKING_ERROR, true);
|
||||
|
||||
id = glCreateProgram();
|
||||
if (vertex != NULL)
|
||||
|
@@ -67,10 +67,9 @@ void VertexArray::mapfaces(const FaceIndices& facedata, const std::vector<Vector
|
||||
|
||||
void VertexArray::load (const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Couldn't Load Model: " << filename << std::endl;
|
||||
if (!file.is_open())
|
||||
engine->setError(ENGINE_ERROR_CODE::MODEL_NOT_FOUND, true);
|
||||
}
|
||||
|
||||
std::vector<Vector3> positions;
|
||||
std::vector<Vector2> uvs;
|
||||
std::string line;
|
||||
|
Reference in New Issue
Block a user