title: How to wrap C structs in Cython for use in Python?
body: <p>For a bit of learning experience, I'm trying to wrap a few parts of SDL (1.2.14) in Cython in an extension for Python 3.2.</p>
  
  <p>I am having a problem figuring out how to wrap C structs straight into Python, being able to access its attributes directly like:</p>
  
  <pre><code>struct_name.attribute
  </code></pre>
  
  <p>For example, I want to take the struct SDL_Surface:</p>
  
  <pre><code>typedef struct SDL_Rect {
      Uint32 flags
      SDL_PixelFormat * format
      int w, h
      Uint16 pitch
      void * pixels
      SDL_Rect clip_rect
      int refcount
  } SDL_Rect;
  </code></pre>
  
  <p>And be able to use it like so in python:</p>
  
  <pre><code>import SDL
  # initializing stuff
  
  Screen = SDL.SetVideoMode( 320, 480, 32, SDL.DOUBLEBUF )
  
  # accessing SDL_Surface.w and SDL_Surface.h
  print( Screen.w, ' ', Screen.h )
  </code></pre>
  
  <p>For right now, I have wrapped the SDL_SetVideoMode and SDL_Surface like this in
  a file called SDL.pyx</p>
  
  <pre><code>cdef extern from 'SDL.h':
      # Other stuff
  
      struct SDL_Surface:
          unsigned long flags
          SDL_PixelFormat * format
          int w, h
          # like past declaration...
  
      SDL_Surface * SDL_SetVideoMode(int, int, int, unsigned )
  
  
  cdef class Surface(object):
      # not sure how to implement       
  
  
  def SetVideoMode(width, height, bpp, flags):
      cdef SDL_Surface * screen = SDL_SetVideoMode  
  
      if not screen:
          err = SDL_GetError()
          raise Exception(err)
  
      # Possible way to return?...
      return Surface(screen)
  </code></pre>
  
  <p>How should I implement SDL.Surface?</p>