-
Notifications
You must be signed in to change notification settings - Fork 0
/
PygameGtkCanvas.py
76 lines (55 loc) · 2.06 KB
/
PygameGtkCanvas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'''
Created on 10/06/2016
@author: George
'''
import gi
import pygame
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, Gdk, GdkPixbuf, GLib
import time
class PygameGtkCanvas(Gtk.Image):
def __init__(self, width, height):
super().__init__()
pygame.init()
self.last_tick_time = time.perf_counter()
self.surface = pygame.Surface((width, height))
self.fps = 0
self.pixbuf = GdkPixbuf.Pixbuf()
GObject.timeout_add(16, self.on_draw)
def on_draw(self):
"""
Draws surface to the Gtk.Image
:return: None
"""
self.surf_draw(self.surface)
cur_time = time.perf_counter()
self.fps = 1/(cur_time-self.last_tick_time)
self.last_tick_time = cur_time
surface_data = GLib.Bytes(pygame.image.tostring(self.surface, "RGB"))
pix = self.pixbuf.new_from_bytes(surface_data, GdkPixbuf.Colorspace.RGB, False, 8,
self.surface.get_width(),
self.surface.get_height(),
self.surface.get_width()*3)
self.set_from_pixbuf(pix.copy()) # Massive memory leak without .copy()
self.queue_draw_area(0, 0, self.surface.get_width(), self.surface.get_height())
# return True to continue the ticking
return True
def surf_draw(self, surface):
"""
Replace this method to blit to the surface before each draw tick
:param surface: surface to draw to
:return: None
"""
pass
def load_image(self, filename):
"""
Can't load/convert images without a display initialised.
Workaround is to spawn a tiny window with no border and
destroy it as quickly as possible.
:param filename: Filename of image to load
:return: Converted image surface
"""
pygame.display.set_mode((1, 1,), pygame.NOFRAME)
image = pygame.image.load(filename).convert_alpha()
pygame.display.quit()
return image