vannov / pygame_cards

pygame_cards is a Python package for creating simple card games powered by pygame framework.
MIT License
24 stars 13 forks source link

pygame.error: Unable to make GL context current #5

Open mtav opened 2 years ago

mtav commented 2 years ago

When I try to run the klondike example, I just get a screen that contains whatever was behind it before it appeared and the following error message in the terminal:

$  python3 ./main.py
pygame 2.1.2 (SDL 2.0.20, Python 3.10.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "pygame_cards/pygame_cards/game_app.py", line 33, in run
    pygame.display.flip()
pygame.error: Unable to make GL context current

Here is what I did after a git clone:

cd ./pygame_cards
pip install -e .
cd ../examples/klondike/
python3 ./main.py

System information:

$  git rev-parse HEAD
55363aebb66f81b976ea750c27315409688516a5
$  lsb_release -a
LSB Version:    core-11.1.0ubuntu4-noarch:printing-11.1.0ubuntu4-noarch:security-11.1.0ubuntu4-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy
$  dpkg -l python3-pygame
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=============================================
ii  python3-pygame 2.1.2+dfsg-3 amd64        SDL bindings for games development (Python 3)
$  python3 -c "import pygame; print(pygame.__version__); print(pygame.__file__)"
pygame 2.1.2 (SDL 2.0.20, Python 3.10.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
2.1.2
/usr/lib/python3/dist-packages/pygame/__init__.py
mtav commented 2 years ago

After some reseacrh, I came across this, according to which:

you can't rely on graphics being accessible from multiple threads. You would have to transfer all your drawing to the main thread. You can still have threads running other logic.

So I fixed my problem with this patch (including a problem with the pygame window not closing properly by adding a pygame.quit() call):

diff --git a/pygame_cards/pygame_cards/game_app.py b/pygame_cards/pygame_cards/game_app.py
index d64ef91..aae0c43 100644
--- a/pygame_cards/pygame_cards/game_app.py
+++ b/pygame_cards/pygame_cards/game_app.py
@@ -238,8 +238,9 @@ class GameApp(object, metaclass=abc.ABCMeta):
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 self.stopped = True
-                self.render_thread.join()
+                # self.render_thread.join()
                 self.game_controller.cleanup()
+                pygame.quit()
                 sys.exit()
             elif event.type == pygame.MOUSEBUTTONUP:
                 self.process_mouse_event(False, self.is_double_click())
@@ -300,11 +301,15 @@ class GameApp(object, metaclass=abc.ABCMeta):
         """ Runs endless loop where game logic and events processing are executed. """
         while 1:
             self.clock.tick(60)
+
+            self.render()
+            pygame.display.flip()
+
             self.process_events()
             self.execute_game_logic()

     def execute(self):
         """ Initializes game, starts rendering thread and starts game endless loop """
         self.init_game()
-        self.start_render_thread()
+        # self.start_render_thread()
         self.run_game_loop()

It basically disables the separate render thread.

Maybe an option could be added for disabling multithreading? I suppose it could be GNU/Linux-specific issue or related to graphics drivers, but for now this works for me.