gl_view.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "gl_view.h"
  2. #include "../app/core/game_engine.h"
  3. #include <QOpenGLContext>
  4. #include <QOpenGLFramebufferObject>
  5. #include <QOpenGLFramebufferObjectFormat>
  6. #include <QQuickWindow>
  7. #include <exception>
  8. #include <qglobal.h>
  9. #include <qobject.h>
  10. #include <qopenglcontext.h>
  11. #include <qopenglframebufferobject.h>
  12. #include <qpointer.h>
  13. #include <qquickframebufferobject.h>
  14. #include <qtmetamacros.h>
  15. #include <utility>
  16. GLView::GLView() { setMirrorVertically(true); }
  17. auto GLView::createRenderer() const -> QQuickFramebufferObject::Renderer * {
  18. QOpenGLContext *ctx = QOpenGLContext::currentContext();
  19. if ((ctx == nullptr) || !ctx->isValid()) {
  20. qCritical() << "GLView::createRenderer() - No valid OpenGL context";
  21. qCritical() << "Running in software rendering mode - 3D view not available";
  22. return nullptr;
  23. }
  24. return new GLRenderer(m_engine);
  25. }
  26. auto GLView::engine() const -> QObject * { return m_engine; }
  27. void GLView::set_engine(QObject *eng) {
  28. if (m_engine == eng) {
  29. return;
  30. }
  31. m_engine = qobject_cast<GameEngine *>(eng);
  32. emit engine_changed();
  33. update();
  34. }
  35. GLView::GLRenderer::GLRenderer(QPointer<GameEngine> engine)
  36. : m_engine(std::move(std::move(engine))) {}
  37. void GLView::GLRenderer::render() {
  38. if (m_engine == nullptr) {
  39. qWarning() << "GLRenderer::render() - engine is null";
  40. return;
  41. }
  42. QOpenGLContext *ctx = QOpenGLContext::currentContext();
  43. if ((ctx == nullptr) || !ctx->isValid()) {
  44. qCritical() << "GLRenderer::render() - OpenGL context lost";
  45. return;
  46. }
  47. try {
  48. m_engine->ensure_initialized();
  49. m_engine->update(1.0F / 60.0F);
  50. m_engine->render(m_size.width(), m_size.height());
  51. } catch (const std::exception &e) {
  52. qCritical() << "GLRenderer::render() exception:" << e.what();
  53. return;
  54. } catch (...) {
  55. qCritical() << "GLRenderer::render() unknown exception";
  56. return;
  57. }
  58. update();
  59. }
  60. auto GLView::GLRenderer::createFramebufferObject(const QSize &size)
  61. -> QOpenGLFramebufferObject * {
  62. m_size = size;
  63. QOpenGLContext *ctx = QOpenGLContext::currentContext();
  64. if ((ctx == nullptr) || !ctx->isValid()) {
  65. qCritical()
  66. << "GLRenderer::createFramebufferObject() - No valid OpenGL context";
  67. return nullptr;
  68. }
  69. QOpenGLFramebufferObjectFormat fmt;
  70. fmt.setAttachment(QOpenGLFramebufferObject::Depth);
  71. fmt.setSamples(0);
  72. return new QOpenGLFramebufferObject(size, fmt);
  73. }
  74. void GLView::GLRenderer::synchronize(QQuickFramebufferObject *item) {
  75. auto *view = dynamic_cast<GLView *>(item);
  76. m_engine = qobject_cast<GameEngine *>(view->engine());
  77. }