SDL Zoom

From IronPython Cookbook

SDL zoom demo

This example demonstrates how to use the SDL library via tha Tao framework in order to display and manipulate 2D surfaces.

Run this example using this image.

This has been tested with:

  • IronPython 2.0Alpha8 (the original distribution)
  • mono 1.2.6 under Ubuntu
  • tao 2.0.0 (binary distribution deployed in the gac)
  • SDL 1.2.11
  • SDL_Image 1.2.5
  • SDL_gfx 2.0
# -*- coding: utf-8 -*-
from System import IntPtr
from System.Runtime.InteropServices import Marshal

import clr
clr.AddReference('Tao.Sdl')
from Tao.Sdl import Sdl, SdlGfx, SdlImage

def run():
    print "Using SDL linked version: %s" % str(Sdl.SDL_Linked_Version())
    
    Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING)
    Sdl.SDL_WM_SetCaption("Zoom example", "")

    flags = Sdl.SDL_HWSURFACE|Sdl.SDL_DOUBLEBUF|Sdl.SDL_ANYFORMAT
    screen = Sdl.SDL_SetVideoMode(640, 480, 16, flags)

    if screen == IntPtr.Zero:
        Sdl.SDL_Quit()
        raise RuntimeError("Couldn't initialize the SDL window")

    # Transform this IntPtr into a SDL_Surface instance
    _screen = Marshal.PtrToStructure(screen, Sdl.SDL_Surface)
    format = Sdl.SDL_Surface.format.__get__(_screen)
    _format = Marshal.PtrToStructure(format, Sdl.SDL_PixelFormat)
    
    # Load a PNG
    image = Sdl.SDL_DisplayFormatAlpha(SdlImage.IMG_Load('./hey.png'))
    _image = Marshal.PtrToStructure(image, Sdl.SDL_Surface)
    image_width = Sdl.SDL_Surface.w.__get__(_image)
    image_height = Sdl.SDL_Surface.h.__get__(_image)
    
    bpp = Sdl.SDL_PixelFormat.BitsPerPixel.__get__(_format)
    rmask = Sdl.SDL_PixelFormat.Rmask.__get__(_format)
    gmask = Sdl.SDL_PixelFormat.Gmask.__get__(_format)
    bmask = Sdl.SDL_PixelFormat.Bmask.__get__(_format)
    amask = Sdl.SDL_PixelFormat.Amask.__get__(_format)
    surface = Sdl.SDL_CreateRGBSurface(flags, 640, 480, bpp, rmask, gmask, bmask, amask)
    Sdl.SDL_BlitSurface(image, Sdl.SDL_Rect(0, 0, image_width, image_height),
                        surface, Sdl.SDL_Rect(0, 0, 640, 480))
                    
    running = True

    while running:
        # Clear the screen surface
        Sdl.SDL_FillRect(screen, Sdl.SDL_Rect(0, 0, 640, 480), Sdl.SDL_MapRGB(format, 0, 0, 0))

        # Grab any events
        result, evt = list(Sdl.SDL_PollEvent())
        # This is to avoid the:
        #    SystemError: Missing or incorrect header for method GetValue
        # exception
        evt_type = Sdl.SDL_Event.type.__get__(evt)
        
        if evt_type == Sdl.SDL_QUIT:
            running = False
        elif evt_type == Sdl.SDL_KEYDOWN:
            key = Sdl.SDL_Event.key.__get__(evt)
            keysym = Sdl.SDL_KeyboardEvent.keysym.__get__(key)
            sym = Sdl.SDL_keysym.sym.__get__(keysym)
            
            if sym == Sdl.SDLK_ESCAPE:
                running = False
            elif sym == Sdl.SDLK_UP:
                print "Zooming in"
                surface = SdlGfx.zoomSurface(image, 2, 2, 1)
                _surface = Marshal.PtrToStructure(surface, Sdl.SDL_Surface)
                image_width = Sdl.SDL_Surface.w.__get__(_surface)
                image_height = Sdl.SDL_Surface.h.__get__(_surface)
            elif sym == Sdl.SDLK_DOWN:
                print "Zooming out"
                surface = SdlGfx.zoomSurface(image, 1, 1, 1)
                _surface = Marshal.PtrToStructure(surface, Sdl.SDL_Surface)
                image_width = Sdl.SDL_Surface.w.__get__(_surface)
                image_height = Sdl.SDL_Surface.h.__get__(_surface)

        # If the surface is bigger than the screen size this will be clipped automatically
        Sdl.SDL_BlitSurface(surface, Sdl.SDL_Rect(0, 0, image_width, image_height),
                            screen, Sdl.SDL_Rect(0, 0, 640, 480))
        Sdl.SDL_Flip(screen)

    Sdl.SDL_FreeSurface(image)
    Sdl.SDL_FreeSurface(surface)
    Sdl.SDL_Quit()
    
if __name__ == '__main__':
    run()

Back to Contents.

TOOLBOX
LANGUAGES