Created
July 15, 2016 09:29
-
-
Save IntendedConsequence/20308c97ba8b5ac17331b5574aea7f9d to your computer and use it in GitHub Desktop.
SFML and wxPython
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# some explanation | |
# I used pysfml-cython to do this, since I couldn't find needed function in PySFML | |
# that constructs RenderWindow from existing window handle. And after I found it | |
# (according to python-sfml wiki it was sf.Window.from_handle), I already finished | |
# this example and porting my own project from pygame to pysfml-cython. | |
# I know pysfml-cython is outdated and based on sfml2.0. All I had to do is | |
# clone SFML project, checkout tag 2.0, compile it. Then checkout pysfml-cython, | |
# compile it and that's it. I ended up with 7 sfml dlls and sfml.pyd which I put | |
# in the same dir as this file. | |
# Now, I did all this on Windows, just so you know. I have no idea if it even works | |
# on linux. Compiling pysfml-cython as far as I remember wasn't hard - I already had | |
# a couple of compilers installed, msvc and mingw, I don't remember which one I used. | |
# I had to add an include path and a library search dir manually in setup.py but | |
# it was easy. | |
# If I update this to work with python-sfml I will update this gist. But right now | |
# I don't think so. You see, pysfml-cython doesn't need docs, since you can just | |
# open sfml.pyx and read it to find everything you need. I can't say the same about | |
# python-sfml - docs are scattered in random places and there doesn't seem to exist | |
# a comprehensive api reference. | |
import wx | |
import sfml as sf | |
import time | |
class wxSFMLCanvas(wx.Control): | |
def __init__(self, *args, **kwargs): | |
wx.Control.__init__(self, *args, **kwargs) | |
self.renderwindow = sf.RenderWindow.from_window_handle(self.GetHandle()) | |
self.size = self.GetSizeTuple() | |
self.Bind(wx.EVT_IDLE, self.OnIdle) | |
self.Bind(wx.EVT_SIZE, self.OnSize) | |
self.Bind(wx.EVT_PAINT, self.OnPaint) | |
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) | |
def OnUpdate(self): | |
pass | |
def OnSize(self, event): | |
self.renderwindow = sf.RenderWindow.from_window_handle(self.GetHandle()) | |
self.size = event.GetSize().Get() | |
self.renderwindow.size = self.size | |
def OnIdle(self, event): | |
self.Refresh() | |
time.sleep(0.001) | |
def OnPaint(self, event): | |
dc = wx.PaintDC(self) | |
self.OnUpdate() | |
self.renderwindow.display() | |
def OnEraseBackground(self, event): | |
pass | |
# just inherit from wxSFMLCanvas and overload OnUpdate | |
class MyCanvas(wxSFMLCanvas): | |
def __init__(self, *args, **kwargs): | |
wxSFMLCanvas.__init__(self, *args, **kwargs) | |
self.tex = sf.Texture.load_from_file("lol.png") # any image should do | |
self.spr = sf.Sprite(self.tex) | |
def OnUpdate(self): | |
self.spr.y += 1 | |
if self.spr.y > self.size[1]: | |
self.spr.y = 0 | |
self.renderwindow.clear(sf.Color(0, 128, 128)) | |
self.renderwindow.draw(self.spr) | |
class MyFrame(wx.Frame): | |
def __init__(self): | |
wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title="pySFML wxPython", pos=wx.DefaultPosition, size=(800, 600)) | |
self.panel = wx.Panel(self, wx.ID_ANY, size=(100,100)) | |
self.canvas1 = MyCanvas(self.panel, size=(100,100)) | |
self.canvas2 = MyCanvas(self.panel, size=(100,100)) | |
sizer = wx.BoxSizer(wx.HORIZONTAL) | |
sizer.Add(self.canvas1, 1, wx.ALL|wx.EXPAND) | |
sizer.Add(self.canvas2, 1, wx.ALL|wx.EXPAND) | |
self.panel.SetSizer(sizer) | |
# sizer.Fit(self) | |
app = wx.App(redirect=False) | |
frame = MyFrame() | |
app.SetTopWindow(frame) | |
frame.Show() | |
app.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment