Skip to content

Commit

Permalink
Hopefully improve OpenGL context creation (#1053)
Browse files Browse the repository at this point in the history
* Hopefully improve OpenGL context creation

First try creating a 3.3 core profile.
If that fails try a 2.1 compatibility profile.
If the system does not support OpenGL 2.1 it should fail here rather than generating errors for each frame.

* Reformatted
  • Loading branch information
gentlegiantJGC authored May 29, 2024
1 parent a704332 commit dd7146b
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions amulet_map_editor/api/opengl/canvas/canvas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Optional
import logging
import sys

import wx
from wx import glcanvas
from wx.glcanvas import GLCanvas, GLAttributes, GLContext, GLContextAttrs
from OpenGL.GL import (
GL_DEPTH_TEST,
glEnable,
Expand All @@ -29,16 +28,16 @@
"""


class BaseCanvas(glcanvas.GLCanvas):
_context: Optional[glcanvas.GLContext]
class BaseCanvas(GLCanvas):
_context: Optional[GLContext]

def __init__(self, parent: wx.Window):
"""
Construct the canvas.
No OpenGL interaction should be done here.
OpenGL initialisation should be done in _init_opengl which is run after the window is first shown.
"""
display_attributes = glcanvas.GLAttributes()
display_attributes = GLAttributes()
display_attributes.PlatformDefaults().MinRGBA(8, 8, 8, 8).DoubleBuffer().Depth(
24
).EndList()
Expand All @@ -48,28 +47,43 @@ def __init__(self, parent: wx.Window):
size=parent.GetClientSize(),
style=wx.WANTS_CHARS,
)
if sys.platform == "darwin":
# This is required for MacOS. Amulet-Team/Amulet-Map-Editor#597
context_attributes = wx.glcanvas.GLContextAttrs()
context_attributes.CoreProfile().Robust().ResetIsolation().EndList()
self._context = glcanvas.GLContext(
self, ctxAttrs=context_attributes
) # setup the OpenGL context
else:
# This is required for linux and windows.
# Amulet-Team/Amulet-Map-Editor#84
# Amulet-Team/Amulet-Map-Editor#856
self._context = glcanvas.GLContext(self)

if not self._context.IsOK():
# Amulet-Team/Amulet-Map-Editor#84
# Amulet-Team/Amulet-Map-Editor#597
# Amulet-Team/Amulet-Map-Editor#856
context_constructors = [
lambda: GLContext(
self,
ctxAttrs=GLContextAttrs()
.PlatformDefaults()
.OGLVersion(3, 3)
.CoreProfile()
.EndList(),
),
lambda: GLContext(
self,
ctxAttrs=GLContextAttrs()
.PlatformDefaults()
.OGLVersion(2, 1)
.CompatibilityProfile()
.EndList(),
),
]

for constructor in context_constructors:
context = constructor()
if context.IsOK():
break
else:
raise Exception(f"Failed setting up context")

self._context = context
self._init = False

self.Bind(wx.EVT_SHOW, self._on_show)

@property
def context(self) -> glcanvas.GLContext:
def context(self) -> GLContext:
return self._context

@property
Expand Down

0 comments on commit dd7146b

Please sign in to comment.