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

Re: [pygame] SDL 2.0



Which wiki? SDL2, PySDL2 or Pygame?

Just playing, but here are (probably suboptimal, even ignoring the
naive algorithm) Pygame and PySDL2 examples of Conway's life. They
require a recent numpy. I'd be interested to hear about better
approaches. I'd like to get my head around using OpenGL "shader"
threads to do it even more quickly in parallel, but that looks
annoyingly fiddly. Maybe writing versions using the multiprocessing
module would be a first step, I don't think I'd have any trouble with
that.

#life.py

import pygame
import numpy as np
from pygame.color import Color
from scipy.ndimage.filters import generic_filter

pygame.init()
window_size = width, height = (400, 400)
background = Color(0, 0, 0, 128) #colour. alpha, red, green and blue.
foreground = Color(0, 210, 0, 0) #colour. alpha, red, green and blue.
bg=int(background)
fg=int(foreground)

screen = pygame.display.set_mode(window_size)
surfarray=np.random.choice([bg, fg], size=(width, height))

#valid=[fg*x+bg*(8-x) for x in range(9)]

def life(a):
     live=(a[4]==fg)
     a[4]=0
     s=int(a.sum())
#     if s not in valid:
#             raise(Exception("invalid sum "+s))
     if live:
         if s < (2*fg+6*bg):
             return bg
         elif s <= (3*fg+5*bg):
             return fg
         else: # s > (3*fg + 5*bg)
             return bg
#dead
     if s == (3*fg+5*bg):
         return fg
     else:
         return bg

keep_going = True

try: #get ready to deal with any problems
    while keep_going:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keep_going = False #exit the loop
        new=generic_filter(surfarray, life, size=3, mode='wrap')
        del surfarray # explicitly delete old array
        surfarray=new
        pygame.surfarray.blit_array(screen, surfarray)
        pygame.display.flip()
except:
    raise #show what went wrong
finally:
    pygame.display.quit() #close the window

#life-sdl2.py
import sdl2
import numpy as np
from sdl2.ext import Color
from scipy.ndimage.filters import generic_filter

window_size = width, height = (400, 400)
background = Color(0, 0, 0, 128) #colour. alpha, red, green and blue.
foreground = Color(0, 210, 0, 0) #colour. alpha, red, green and blue.
bg=int(background)
fg=int(foreground)

sdl2.ext.init()
#http://pysdl2.readthedocs.org/en/latest/modules/sdl2ext_window.html#sdl2.ext.Window
window = sdl2.ext.Window("PySDL2 life", size=window_size)
window.show()
# http://wiki.libsdl.org/SDL_GetWindowSurface
# http://wiki.libsdl.org/SDL_Surface
windowsurf = sdl2.SDL_GetWindowSurface(window.window)
#http://pysdl2.readthedocs.org/en/latest/modules/sdl2ext_pixelaccess.html#sdl2.ext.PixelView
windowarray = sdl2.ext.pixels2d(windowsurf.contents)
surfarray=np.random.choice(np.array([bg, fg], dtype=np.uint32),
size=(width, height))

#valid=[fg*x+bg*(8-x) for x in range(9)]

def life(a):
     live=(a[4]==fg)
     a[4]=0
     s=int(a.sum())
#     if s not in valid:
#             raise(Exception("invalid sum "+s))
     if live:
         if s < (2*fg+6*bg):
             return bg
         elif s <= (3*fg+5*bg):
             return fg
         else: # s > (3*fg + 5*bg)
             return bg
#dead
     if s == (3*fg+5*bg):
         return fg
     else:
         return bg

keep_going = True

try: #get ready to deal with any problems
    while keep_going:
        for event in sdl2.ext.get_events():
            if event.type == sdl2.SDL_QUIT:
                keep_going = False #exit the loop
        new=generic_filter(surfarray, life, size=3, mode='wrap')
        del surfarray # explicitly delete old array
        surfarray=new
        del new
        np.copyto(windowarray, surfarray)
        window.refresh()
except:
    raise #show what went wrong
finally:
    sdl2.SDL_Quit() #close the window

On 18 August 2013 01:56,  <ninmonkeys@xxxxxxxxx> wrote:
> I did see in the wiki for porting, a method to use the read-access of
> surface, yet copies into an existing texture. Meaning you don't do the slow
> createTextureFromSurface() every frame.
>
> I'd paste it but I can't on my phone.
> --
> Monkey
>
> On Aug 13, 2013, at 1:47 AM, Andrew Barlow <andrew.barlow@xxxxxxxxx> wrote:
>
> Awesome! I hope we end up with a nice wrapper around previous pygame code so
> I don't have to rewrite everything!
>
> I'd even pay good money for that!
>
> On 13 August 2013 09:37, Renà Dudfield <renesd@xxxxxxxxx> wrote:
>>
>> Happy days...
>>
>>
>> SDL 2.0 is released!
>>
>> http://lists.libsdl.org/pipermail/sdl-libsdl.org/2013-August/089854.html
>>
>> """
>>
>> These are the most important new features in SDL 2.0:
>>
>>    - Full 3D hardware acceleration
>>    - Support for OpenGL 3.0+ in various profiles (core, compatibility,
>>    debug, robust, etc)
>>    - Support for OpenGL ES
>>    - Support for multiple windows
>>    - Support for multiple displays
>>    - Support for multiple audio devices
>>    - Android and iOS support
>>    - Simple 2D rendering API that can use Direct3D, OpenGL, OpenGL ES, or
>>    software rendering behind the scenes
>>    - Force Feedback available on Windows, Mac OS X and Linux
>>    - XInput and XAudio2 support for Windows
>>    - Atomic operations
>>    - Power management (exposes battery life remaining, etc)
>>    - Shaped windows
>>    - 32-bit audio (int and float)
>>    - Simplified Game Controller API (the Joystick API is still here, too!)
>>    - Touch support (multitouch, gestures, etc)
>>    - Better fullscreen support
>>    - Better keyboard support (scancodes vs keycodes, etc).
>>    - Message boxes
>>    - Clipboard support
>>    - Basic Drag'n'Drop support
>>    - Proper unicode input and IME support
>>    - A really powerful assert macro
>>    - Lots of old annoyances from 1.2 are gone
>>    - Many other things!
>>
>> """
>
>