]> git.localhorst.tv Git - gong.git/blob - src/app/error.cpp
code, assets, and other stuff stolen from blank
[gong.git] / src / app / error.cpp
1 #include "error.hpp"
2
3 #include <alut.h>
4 #include <cerrno>
5 #include <cstring>
6 #include <SDL.h>
7 #include <SDL_net.h>
8 #include <SDL_ttf.h>
9 #include <GL/glew.h>
10
11 using namespace std;
12
13
14 namespace {
15
16 const char *al_error_string(ALenum num) {
17         switch (num) {
18                 case AL_NO_ERROR:
19                         return "no error";
20                 case AL_INVALID_NAME:
21                         return "invalid name";
22                 case AL_INVALID_ENUM:
23                         return "invalid enum";
24                 case AL_INVALID_VALUE:
25                         return "invalid value";
26                 case AL_INVALID_OPERATION:
27                         return "invalid operation";
28                 case AL_OUT_OF_MEMORY:
29                         return "out of memory";
30         }
31         return "unknown AL error";
32 }
33
34 std::string al_error_append(ALenum num, std::string msg) {
35         return msg + ": " + al_error_string(num);
36 }
37
38 string alut_error_append(ALenum num, string msg) {
39         const char *error = alutGetErrorString(num);
40         if (error && *error != '\0') {
41                 msg += ": ";
42                 msg += error;
43         }
44         return msg;
45 }
46
47 string gl_error_append(string msg) {
48         const GLubyte *error = gluErrorString(glGetError());
49         if (error && *error != '\0') {
50                 const GLubyte *errEnd = error;
51                 while (*errEnd != '\0') {
52                         ++errEnd;
53                 }
54                 msg += ": ";
55                 msg.append(error, errEnd);
56         }
57         return msg;
58 }
59
60 string gl_error_get() {
61         string msg;
62         const GLubyte *error = gluErrorString(glGetError());
63         if (error && *error != '\0') {
64                 const GLubyte *errEnd = error;
65                 while (*errEnd != '\0') {
66                         ++errEnd;
67                 }
68                 msg.assign(error, errEnd);
69         }
70         return msg;
71 }
72
73 string net_error_append(string msg) {
74         const char *error = SDLNet_GetError();
75         if (*error != '\0') {
76                 msg += ": ";
77                 msg += error;
78         }
79         return msg;
80 }
81
82 string sdl_error_append(string msg) {
83         const char *error = SDL_GetError();
84         if (error && *error != '\0') {
85                 msg += ": ";
86                 msg += error;
87                 SDL_ClearError();
88         }
89         return msg;
90 }
91
92 string ttf_error_append(string msg) {
93         const char *error = TTF_GetError();
94         if (error && *error != '\0') {
95                 msg += ": ";
96                 msg += error;
97         }
98         return msg;
99 }
100
101 }
102
103
104 namespace gong {
105 namespace app {
106
107 ALError::ALError(ALenum num)
108 : std::runtime_error(al_error_string(num)) {
109
110 }
111
112 ALError::ALError(ALenum num, const std::string &msg)
113 : std::runtime_error(al_error_append(num, msg)) {
114
115 }
116
117
118 AlutError::AlutError(ALenum num)
119 : runtime_error(alutGetErrorString(num)) {
120
121 }
122
123 AlutError::AlutError(ALenum num, const string &msg)
124 : runtime_error(alut_error_append(num, msg)) {
125
126 }
127
128
129 GLError::GLError()
130 : runtime_error(gl_error_get()) {
131
132 }
133
134 GLError::GLError(const string &msg)
135 : runtime_error(gl_error_append(msg)) {
136
137 }
138
139
140 NetError::NetError()
141 : runtime_error(SDLNet_GetError()) {
142
143 }
144
145 NetError::NetError(const string &msg)
146 : runtime_error(net_error_append(msg)) {
147
148 }
149
150
151 SDLError::SDLError()
152 : runtime_error(SDL_GetError()) {
153
154 }
155
156 SDLError::SDLError(const string &msg)
157 : runtime_error(sdl_error_append(msg)) {
158
159 }
160
161
162 SysError::SysError()
163 : SysError(errno) {
164
165 }
166
167 SysError::SysError(const string &msg)
168 : SysError(errno, msg) {
169
170 }
171
172 SysError::SysError(int err_num)
173 : runtime_error(strerror(err_num)) {
174
175 }
176
177 SysError::SysError(int err_num, const string &msg)
178 : runtime_error(msg + ": " + strerror(err_num)) {
179
180 }
181
182
183 TTFError::TTFError()
184 : runtime_error(TTF_GetError()) {
185
186 }
187
188 TTFError::TTFError(const string &msg)
189 : runtime_error(ttf_error_append(msg)) {
190
191 }
192
193 }
194 }