| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gate/graphics/gl_apis.hpp" | ||
| 2 | #include "gate/exceptions.hpp" | ||
| 3 | |||
| 4 | namespace gate | ||
| 5 | { | ||
| 6 | namespace graph | ||
| 7 | { | ||
| 8 | |||
| 9 | ✗ | GlApi::GlApi() | |
| 10 | { | ||
| 11 | ✗ | result_t result = gate_gl_api_init(); | |
| 12 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 13 | ✗ | } | |
| 14 | |||
| 15 | ✗ | GlApi::~GlApi() noexcept | |
| 16 | { | ||
| 17 | ✗ | } | |
| 18 | |||
| 19 | ✗ | void GlApi::setColor(real32_t r, real32_t g, real32_t b) | |
| 20 | { | ||
| 21 | ✗ | gate_gl_api_color3(r, g, b); | |
| 22 | ✗ | } | |
| 23 | ✗ | void GlApi::setColor(real32_t r, real32_t g, real32_t b, real32_t a) | |
| 24 | { | ||
| 25 | ✗ | gate_gl_api_color4(r, g, b, a); | |
| 26 | ✗ | } | |
| 27 | ✗ | void GlApi::setColorByte(uint8_t r, uint8_t g, uint8_t b) | |
| 28 | { | ||
| 29 | ✗ | gate_gl_api_color3b(r, g, b); | |
| 30 | ✗ | } | |
| 31 | ✗ | void GlApi::setColorByte(uint8_t r, uint8_t g, uint8_t b, uint8_t a) | |
| 32 | { | ||
| 33 | ✗ | gate_gl_api_color4b(r, g, b, a); | |
| 34 | ✗ | } | |
| 35 | ✗ | void GlApi::setVertexPointer(size_t coord_count, real32_t const* ptr_coords) | |
| 36 | { | ||
| 37 | ✗ | gate_gl_api_vertexpointer(coord_count, ptr_coords); | |
| 38 | ✗ | } | |
| 39 | ✗ | void GlApi::setTexCoordPointer(size_t coord_count, real32_t const* ptr_coords) | |
| 40 | { | ||
| 41 | ✗ | gate_gl_api_texcoordpointer(coord_count, ptr_coords); | |
| 42 | ✗ | } | |
| 43 | ✗ | void GlApi::setShadeModel(ShadeModelEnum model) | |
| 44 | { | ||
| 45 | ✗ | gate_gl_api_shademodel((gate_gl_api_shademodel_t)(int)model); | |
| 46 | ✗ | } | |
| 47 | ✗ | void GlApi::setBlendFunc(BlendEnum srcFunc, BlendEnum dstFunc) | |
| 48 | { | ||
| 49 | ✗ | gate_gl_api_blendfunc((gate_gl_api_blend_t)(int)srcFunc, (gate_gl_api_blend_t)(int)dstFunc); | |
| 50 | ✗ | } | |
| 51 | ✗ | void GlApi::setClearColor(real32_t r, real32_t g, real32_t b, real32_t a) | |
| 52 | { | ||
| 53 | ✗ | gate_gl_api_clearcolor(r, g, b, a); | |
| 54 | ✗ | } | |
| 55 | ✗ | void GlApi::setClearDepth(real32_t depth) | |
| 56 | { | ||
| 57 | ✗ | gate_gl_api_cleardepth(depth); | |
| 58 | ✗ | } | |
| 59 | ✗ | void GlApi::enableCapability(CapabilityEnum capability) | |
| 60 | { | ||
| 61 | ✗ | gate_gl_api_enable((gate_gl_api_capability_t)(int)capability); | |
| 62 | ✗ | } | |
| 63 | ✗ | void GlApi::disableCapability(CapabilityEnum capability) | |
| 64 | { | ||
| 65 | ✗ | gate_gl_api_disable((gate_gl_api_capability_t)(int)capability); | |
| 66 | ✗ | } | |
| 67 | ✗ | void GlApi::setDepthFunc(DepthEnum depthFunction) | |
| 68 | { | ||
| 69 | ✗ | gate_gl_api_depthfunc((gate_gl_api_depth_t)(int)depthFunction); | |
| 70 | ✗ | } | |
| 71 | ✗ | void GlApi::setDepthMask(bool_t enabled) | |
| 72 | { | ||
| 73 | ✗ | gate_gl_api_depthmask(enabled); | |
| 74 | ✗ | } | |
| 75 | |||
| 76 | ✗ | void GlApi::setHint(HintEnum hintType, HintModeEnum hintMode) | |
| 77 | { | ||
| 78 | ✗ | gate_gl_api_hint((gate_gl_api_hint_t)(int)hintType, (gate_gl_api_hintmode_t)(int)hintMode); | |
| 79 | ✗ | } | |
| 80 | ✗ | void GlApi::setViewport(int32_t x, int32_t y, int32_t width, int32_t height) | |
| 81 | { | ||
| 82 | ✗ | gate_gl_api_viewport(x, y, width, height); | |
| 83 | ✗ | } | |
| 84 | ✗ | void GlApi::setMatrixMode(MatrixModeEnum matrixMode) | |
| 85 | { | ||
| 86 | ✗ | gate_gl_api_matrixmode((gate_gl_api_matrixmode_t)(int)matrixMode); | |
| 87 | ✗ | } | |
| 88 | ✗ | void GlApi::loadIdentity() | |
| 89 | { | ||
| 90 | ✗ | gate_gl_api_loadidentity(); | |
| 91 | ✗ | } | |
| 92 | ✗ | void GlApi::mulMatrix(real32_t const matrix[16]) | |
| 93 | { | ||
| 94 | ✗ | gate_gl_api_multmatrix(matrix); | |
| 95 | ✗ | } | |
| 96 | ✗ | void GlApi::translate(real32_t x, real32_t y, real32_t z) | |
| 97 | { | ||
| 98 | ✗ | gate_gl_api_translate(x, y, z); | |
| 99 | ✗ | } | |
| 100 | ✗ | void GlApi::rotate(real32_t angle, real32_t x, real32_t y, real32_t z) | |
| 101 | { | ||
| 102 | ✗ | gate_gl_api_rotate(angle, x, y, z); | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | ✗ | void GlApi::flush() | |
| 106 | { | ||
| 107 | ✗ | gate_gl_api_flush(); | |
| 108 | ✗ | } | |
| 109 | ✗ | void GlApi::finish() | |
| 110 | { | ||
| 111 | ✗ | gate_gl_api_finish(); | |
| 112 | ✗ | } | |
| 113 | ✗ | void GlApi::clear(unsigned clearBits) | |
| 114 | { | ||
| 115 | ✗ | gate_gl_api_clear(clearBits); | |
| 116 | ✗ | } | |
| 117 | ✗ | void GlApi::setPerspective(real32_t fovy, real32_t aspect, real32_t znear, real32_t zfar) | |
| 118 | { | ||
| 119 | ✗ | gate_gl_api_perspective(fovy, aspect, znear, zfar); | |
| 120 | ✗ | } | |
| 121 | ✗ | void GlApi::lookAt(real32_t eyeX, real32_t eyeY, real32_t eyeZ, real32_t lookX, real32_t lookY, real32_t lookZ, real32_t upX, real32_t upY, real32_t upZ) | |
| 122 | { | ||
| 123 | ✗ | gate_gl_api_lookat(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); | |
| 124 | ✗ | } | |
| 125 | ✗ | void GlApi::enableClientState(ClientStateEnum state) | |
| 126 | { | ||
| 127 | ✗ | gate_gl_api_enableclientstate((gate_gl_api_clientstate_t)(int)state); | |
| 128 | ✗ | } | |
| 129 | ✗ | void GlApi::disableClientState(ClientStateEnum state) | |
| 130 | { | ||
| 131 | ✗ | gate_gl_api_disableclientstate((gate_gl_api_clientstate_t)(int)state); | |
| 132 | ✗ | } | |
| 133 | ✗ | void GlApi::drawArrays(DrawModeEnum drawMode, int index, size_t vertex_count) | |
| 134 | { | ||
| 135 | ✗ | gate_gl_api_drawarrays((gate_gl_api_drawmode_t)(int)drawMode, index, vertex_count); | |
| 136 | ✗ | } | |
| 137 | ✗ | void* GlApi::createTexture(PixelFormatEnum pixelFormat, uint32_t width, uint32_t height, void const* data) | |
| 138 | { | ||
| 139 | ✗ | return gate_gl_api_createtexture((gate_gl_api_pixelformat_t)(int)pixelFormat, width, height, data); | |
| 140 | } | ||
| 141 | ✗ | void GlApi::deleteTexture(void* textureId) | |
| 142 | { | ||
| 143 | ✗ | gate_gl_api_deletetexture(textureId); | |
| 144 | ✗ | } | |
| 145 | ✗ | void GlApi::bindTexture(void* textureId) | |
| 146 | { | ||
| 147 | ✗ | gate_gl_api_bindtexture(textureId); | |
| 148 | ✗ | } | |
| 149 | ✗ | void GlApi::setCullFace(CullFaceEnum cullface) | |
| 150 | { | ||
| 151 | ✗ | gate_gl_api_cullface((gate_gl_api_cullface_t)cullface); | |
| 152 | ✗ | } | |
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | ✗ | GlSurfaceEventSink::GlSurfaceEventSink() | |
| 159 | ✗ | : surface_ptr(NULL) | |
| 160 | { | ||
| 161 | ✗ | } | |
| 162 | |||
| 163 | ✗ | GlSurfaceEventSink::~GlSurfaceEventSink() noexcept | |
| 164 | { | ||
| 165 | ✗ | } | |
| 166 | |||
| 167 | ✗ | GlSurface& GlSurfaceEventSink::getSurface() | |
| 168 | { | ||
| 169 | ✗ | if (!this->surface_ptr) | |
| 170 | { | ||
| 171 | ✗ | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 172 | } | ||
| 173 | ✗ | return *this->surface_ptr; | |
| 174 | } | ||
| 175 | ✗ | void GlSurfaceEventSink::connectSurface(GlSurface& surface) | |
| 176 | { | ||
| 177 | ✗ | this->surface_ptr = &surface; | |
| 178 | ✗ | } | |
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | enumint_t const GlSurface::Flag_SoftwareRendering = GATE_GL_SURFACE_FLAG_SOFTWARE_RENDERING; | ||
| 183 | |||
| 184 | |||
| 185 | ✗ | GlSurface::GlSurface() | |
| 186 | { | ||
| 187 | ✗ | result_t result = gate_gl_surface_init(&this->impl); | |
| 188 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 189 | ✗ | } | |
| 190 | ✗ | GlSurface::~GlSurface() noexcept | |
| 191 | { | ||
| 192 | ✗ | gate_gl_surface_uninit(&this->impl); | |
| 193 | ✗ | } | |
| 194 | |||
| 195 | ✗ | void GlSurface::openScreen(uint32_t width, uint32_t height, enumint_t flags) | |
| 196 | { | ||
| 197 | ✗ | result_t result = gate_gl_surface_open(&this->impl, gate_gl_surface_type_screen, width, height, flags); | |
| 198 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 199 | ✗ | } | |
| 200 | ✗ | void GlSurface::openImage(uint32_t width, uint32_t height, enumint_t flags) | |
| 201 | { | ||
| 202 | ✗ | result_t result = gate_gl_surface_open(&this->impl, gate_gl_surface_type_image, width, height, flags); | |
| 203 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 204 | ✗ | } | |
| 205 | |||
| 206 | ✗ | void GlSurface::close() | |
| 207 | { | ||
| 208 | ✗ | result_t result = gate_gl_surface_close(&this->impl); | |
| 209 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 210 | ✗ | } | |
| 211 | |||
| 212 | ✗ | void GlSurface::getSize(uint32_t& width, uint32_t& height) | |
| 213 | { | ||
| 214 | ✗ | result_t result = gate_gl_surface_get_size(&this->impl, &width, &height); | |
| 215 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 216 | ✗ | } | |
| 217 | ✗ | void GlSurface::resize(uint32_t width, uint32_t height) | |
| 218 | { | ||
| 219 | ✗ | result_t result = gate_gl_surface_resize(&this->impl, width, height); | |
| 220 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 221 | ✗ | } | |
| 222 | |||
| 223 | ✗ | RasterImage GlSurface::printImage() | |
| 224 | { | ||
| 225 | ✗ | gate_rasterimage_t rasterimage = GATE_INIT_EMPTY; | |
| 226 | ✗ | result_t result = gate_gl_surface_print_image(&this->impl, &rasterimage); | |
| 227 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 228 | ✗ | RasterImage image(&rasterimage); | |
| 229 | ✗ | gate_rasterimage_release(&rasterimage); | |
| 230 | ✗ | return image; | |
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | ✗ | static void surface_events_on_render(gate_gl_surface_t* surface, void* user_param) | |
| 235 | { | ||
| 236 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 237 | try | ||
| 238 | { | ||
| 239 | ✗ | sink->onRender(sink->getSurface()); | |
| 240 | } | ||
| 241 | ✗ | catch (...) {} | |
| 242 | ✗ | } | |
| 243 | ✗ | static void surface_events_on_exit(gate_gl_surface_t* surface, void* user_param) | |
| 244 | { | ||
| 245 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 246 | try | ||
| 247 | { | ||
| 248 | ✗ | sink->onExit(sink->getSurface()); | |
| 249 | } | ||
| 250 | ✗ | catch (...) {} | |
| 251 | ✗ | } | |
| 252 | ✗ | static void surface_events_on_resize(gate_gl_surface_t* surface, gate_uint32_t width, gate_uint32_t height, void* user_param) | |
| 253 | { | ||
| 254 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 255 | try | ||
| 256 | { | ||
| 257 | ✗ | sink->onResize(sink->getSurface(), width, height); | |
| 258 | } | ||
| 259 | ✗ | catch (...) {} | |
| 260 | ✗ | } | |
| 261 | ✗ | static void surface_events_on_key_down(gate_gl_surface_t* surface, gate_input_keycode_t keyboard_key, | |
| 262 | gate_input_keystates_t keyboard_states, void* user_param) | ||
| 263 | { | ||
| 264 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 265 | try | ||
| 266 | { | ||
| 267 | ✗ | sink->onKeyDown(sink->getSurface(), Keyboard::KeyEnum(keyboard_key), keyboard_states); | |
| 268 | } | ||
| 269 | ✗ | catch (...) {} | |
| 270 | ✗ | } | |
| 271 | ✗ | static void surface_events_on_key_up(gate_gl_surface_t* surface, gate_input_keycode_t keyboard_key, | |
| 272 | gate_input_keystates_t keyboard_states, void* user_param) | ||
| 273 | { | ||
| 274 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 275 | try | ||
| 276 | { | ||
| 277 | ✗ | sink->onKeyUp(sink->getSurface(), Keyboard::KeyEnum(keyboard_key), keyboard_states); | |
| 278 | } | ||
| 279 | ✗ | catch (...) {} | |
| 280 | ✗ | } | |
| 281 | ✗ | static void surface_events_on_pointer_down(gate_gl_surface_t* surface, gate_uint32_t pointer_type, gate_real32_t x, gate_real32_t y, void* user_param) | |
| 282 | { | ||
| 283 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 284 | try | ||
| 285 | { | ||
| 286 | ✗ | sink->onPointerDown(sink->getSurface(), pointer_type, x, y); | |
| 287 | } | ||
| 288 | ✗ | catch (...) {} | |
| 289 | ✗ | } | |
| 290 | ✗ | static void surface_events_on_pointer_move(gate_gl_surface_t* surface, gate_uint32_t pointer_type, gate_real32_t x, gate_real32_t y, void* user_param) | |
| 291 | { | ||
| 292 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 293 | try | ||
| 294 | { | ||
| 295 | ✗ | sink->onPointerMove(sink->getSurface(), pointer_type, x, y); | |
| 296 | } | ||
| 297 | ✗ | catch (...) {} | |
| 298 | ✗ | } | |
| 299 | ✗ | static void surface_events_on_pointer_up(gate_gl_surface_t* surface, gate_uint32_t pointer_type, gate_real32_t x, gate_real32_t y, void* user_param) | |
| 300 | { | ||
| 301 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 302 | try | ||
| 303 | { | ||
| 304 | ✗ | sink->onPointerUp(sink->getSurface(), pointer_type, x, y); | |
| 305 | } | ||
| 306 | ✗ | catch (...) {} | |
| 307 | ✗ | } | |
| 308 | ✗ | static void surface_events_on_scroll(gate_gl_surface_t* surface, gate_int32_t x_direction, gate_int32_t y_direction, void* user_param) | |
| 309 | { | ||
| 310 | ✗ | GlSurfaceEventSink* sink = static_cast<GlSurfaceEventSink*>(user_param); | |
| 311 | try | ||
| 312 | { | ||
| 313 | ✗ | sink->onScroll(sink->getSurface(), x_direction, y_direction); | |
| 314 | } | ||
| 315 | ✗ | catch (...) {} | |
| 316 | ✗ | } | |
| 317 | |||
| 318 | static gate_gl_surface_events_t cpp_dispatcher = | ||
| 319 | { | ||
| 320 | &surface_events_on_render, | ||
| 321 | &surface_events_on_exit, | ||
| 322 | &surface_events_on_resize, | ||
| 323 | |||
| 324 | &surface_events_on_key_down, | ||
| 325 | &surface_events_on_key_up, | ||
| 326 | &surface_events_on_pointer_down, | ||
| 327 | &surface_events_on_pointer_move, | ||
| 328 | &surface_events_on_pointer_up, | ||
| 329 | &surface_events_on_scroll | ||
| 330 | }; | ||
| 331 | |||
| 332 | ✗ | void GlSurface::runEventLoop(GlSurfaceEventSink& eventHandler) | |
| 333 | { | ||
| 334 | ✗ | eventHandler.connectSurface(*this); | |
| 335 | ✗ | result_t result = gate_gl_surface_run_event_loop(&this->impl, &cpp_dispatcher, static_cast<void*>(&eventHandler)); | |
| 336 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 337 | ✗ | } | |
| 338 | ✗ | void GlSurface::exitEventLoop() | |
| 339 | { | ||
| 340 | ✗ | result_t result = gate_gl_surface_exit_event_loop(&this->impl); | |
| 341 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 342 | ✗ | } | |
| 343 | ✗ | void GlSurface::swapBuffers() | |
| 344 | { | ||
| 345 | ✗ | result_t result = gate_gl_surface_swap_buffers(&this->impl); | |
| 346 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 347 | ✗ | } | |
| 348 | |||
| 349 | ✗ | GlApi& GlSurface::getGlApi() | |
| 350 | { | ||
| 351 | ✗ | return this->api; | |
| 352 | } | ||
| 353 | |||
| 354 | } // end of namespace graph | ||
| 355 | } // end of namespace gate | ||
| 356 |