[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] IronPython and SDL example



Here's a really simple example of doing SDL in IronPython, using the Tao 
Framework. It Just Works. It'd actually be remarkably easy to reimplement the 
pygame API on top of Tao - could probably just use the pygame-ctypes work as 
a starting point of what to implement and how.

This is a rough port of the Tao SDL Rectangles example. I skipped the mixer 
bit for this example. I've only tested it on Mono, but I'd assume it just 
works on .Net as well. The code just pops up a window and scribbles random 
blue-green coloured rectangles all over it.

So this is another possibility for people looking to deploy cross platform GUI 
apps. I've not played with the OpenGL bindings in Tao (or any of the other 
ones) yet, but there's a bunch of them.

Anthony
---
from Tao.Sdl import Sdl
import time
from random import randint
def Rectangles():
    flags = Sdl.SDL_HWSURFACE|Sdl.SDL_DOUBLEBUF|Sdl.SDL_ANYFORMAT
    bpp = 16
    width = 640
    height = 480
    Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING)
    Sdl.SDL_WM_SetCaption("Tao.Sdl Example - Rectangles", "")
    surfacePtr = Sdl.SDL_SetVideoMode(640, 480, 16, flags)
    rmask = 0x00000000
    gmask = 0x00ff0000
    bmask = 0x0000ff00
    amask = 0x000000ff
    rgbSurfacePtr = Sdl.SDL_CreateRGBSurface(flags, width, height, bpp,
                                        rmask, gmask, bmask, amask)
    rect = Sdl.SDL_Rect(0, 0, width, height)
    result = Sdl.SDL_FillRect(rgbSurfacePtr, rect, 0)
    Sdl.SDL_Flip(surfacePtr)
    while True:
        result = Sdl.SDL_PollEvent()
        time.sleep(0.01)
        evtflag, evt = result
        if evtflag:
            if evt.type == Sdl.SDL_QUIT: break
            elif evt.type == Sdl.SDL_KEYDOWN:
                if evt.key.keysym.sym == Sdl.SDLK_ESCAPE: break
                if evt.key.keysym.sym == Sdl.SDLK_p: print "ooo. A 'p'"
        rect2 = Sdl.SDL_Rect(randint(-300, width), randint(-300,height),
                             randint(20,300), randint(20,300))
        result = Sdl.SDL_FillRect(surfacePtr, rect2, randint(0,1000000))
        result = Sdl.SDL_BlitSurface(rgbSurfacePtr, rect2, surfacePtr, rect)
        result = Sdl.SDL_Flip(surfacePtr)
if __name__ == "__main__":
    Rectangles()


-- 
Anthony Baxter     <anthony@xxxxxxxxxxxxxxxx>
It's never too late to have a happy childhood.