24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ |
26 */ |
27 |
27 |
28 #include "ascension/context.h" |
28 #include "ascension/context.h" |
29 #include "ascension/error.h" |
|
30 #include "ascension/utils.h" |
|
31 |
29 |
32 #include <SDL2/SDL.h> |
30 #include <SDL2/SDL.h> |
33 #include <SDL2/SDL_ttf.h> |
31 #include <SDL2/SDL_ttf.h> |
34 #include <SDL2/SDL_image.h> |
32 #include <SDL2/SDL_image.h> |
35 |
33 |
|
34 #include <GL/gl.h> |
|
35 |
|
36 #include <cx/printf.h> |
|
37 |
36 #include <time.h> |
38 #include <time.h> |
37 |
39 |
38 AscContext asc_context; |
40 AscContext asc_context; |
|
41 |
|
42 void asc_error_impl(const char* file, unsigned line, char const* fmt, ...) { |
|
43 asc_set_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
44 |
|
45 // write to error buffer |
|
46 va_list args; |
|
47 va_start(args, fmt); |
|
48 CxBuffer* buf = &asc_context.error_buffer; |
|
49 size_t bufpos = buf->pos; |
|
50 int written = cx_vfprintf(buf, cxBufferWriteFunc, fmt, args); |
|
51 cxBufferPut(buf, '\n'); |
|
52 va_end(args); |
|
53 |
|
54 // also print to stderr |
|
55 // avoid double-formatting, get it directly from the buffer |
|
56 fprintf(stderr, "[ERROR %s %u] %.*s\n", |
|
57 file, line, written, buf->space+bufpos); |
|
58 } |
|
59 |
|
60 bool asc_has_error(void) { |
|
61 return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
62 } |
|
63 |
|
64 char const* asc_get_error(void) { |
|
65 // we zero-terminate the current buffer contents before providing them |
|
66 cxBufferPut(&asc_context.error_buffer, 0); |
|
67 --asc_context.error_buffer.pos; |
|
68 --asc_context.error_buffer.size; |
|
69 return asc_context.error_buffer.space; |
|
70 } |
|
71 |
|
72 void asc_clear_error(void) { |
|
73 cxBufferClear(&asc_context.error_buffer); |
|
74 asc_clear_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); |
|
75 } |
|
76 |
|
77 void asc_error_gl(unsigned code, const char *message) { |
|
78 const char *glerr; |
|
79 switch(code) { |
|
80 case GL_NO_ERROR: |
|
81 return; |
|
82 case GL_INVALID_ENUM: |
|
83 glerr = "invalid enum"; |
|
84 break; |
|
85 case GL_INVALID_VALUE: |
|
86 glerr = "invalid value"; |
|
87 break; |
|
88 case GL_INVALID_OPERATION: |
|
89 glerr = "invalid operation"; |
|
90 break; |
|
91 case GL_INVALID_FRAMEBUFFER_OPERATION: |
|
92 glerr = "invalid framebuffer operation"; |
|
93 break; |
|
94 case GL_OUT_OF_MEMORY: |
|
95 glerr = "out of memory"; |
|
96 break; |
|
97 case GL_STACK_UNDERFLOW: |
|
98 glerr = "stack underflow"; |
|
99 break; |
|
100 case GL_STACK_OVERFLOW: |
|
101 glerr = "stack overflow"; |
|
102 break; |
|
103 default: |
|
104 glerr = "unknown GL error"; |
|
105 } |
|
106 asc_error("%s\nGL Error: %s", message, glerr); |
|
107 } |
|
108 |
|
109 int asc_error_catch_all_gl(void) { |
|
110 GLenum error; |
|
111 int ret = 0; |
|
112 while ((error = glGetError()) != GL_NO_ERROR) { |
|
113 asc_error_gl(error, "Uncaught"); |
|
114 ret = 1; |
|
115 } |
|
116 return ret; |
|
117 } |
39 |
118 |
40 static uint64_t asc_nanos(void) { |
119 static uint64_t asc_nanos(void) { |
41 struct timespec ts; |
120 struct timespec ts; |
42 clock_gettime(CLOCK_MONOTONIC, &ts); |
121 clock_gettime(CLOCK_MONOTONIC, &ts); |
43 return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec; |
122 return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec; |