Commits

Anonymous committed 6fdef59

Added fill example. Added more keyword args.

  • Participants
  • Parent commits d3ce561
  • Branches pgreloaded

Comments (0)

Files changed (10)

doc/MODULE_FAQ.txt

 
 Additionally base/pgbase.h contains several functions to get an integer
 or floating point value from a PyObject. Those however require other
-module to import the base module.
+modules to import the base module.
 
 * SDL Rectangles *
 

doc/create_htmlref.py

         doccls.methods.append (DocMethod (doccls, name, desc))
 
     def create_desc_html (self, desc, refcache):
-        data = '<p>'
         written = 0
         blocks = 0
+        data = '<p>'
         for line in desc.split ('\n'):
             line = line.strip ()
             if written > 0 and line == '':
                 data += '</p><p>'
                 blocks += 1
-            else:
+            elif line != '':
                 data += line
-            written += 1
+                written += 1
         if blocks > 0:
             data += '</p>'
+        if written == 0:
+            return ''
         return data
     
     def create_func_html (self, func, refcache):
-        data = '<dt class="functions"><a name="%s">%s</a></dt>\n' % \
+        data = '    <dt class="functions"><a name="%s">%s</a></dt>\n' % \
                (func.name, func.name)
-        data += '<dd class="functions">%s</dd>\n' % \
+        data += '    <dd class="functions">%s</dd>\n' % \
                 self.create_desc_html (func.description, refcache)
         return data
     
         fname = os.path.join ("ref", "%s.html")
         fp = open (fname % self.modulename.replace (".", "_"), "w")
         fp.write (HTML_HEADER % self.modulename)
+        
+        fp.write ('<h1 class="module">%s</h1>\n' % self.modulename)
+        fp.write ('<div class="module">\n%s</div>\n' % self.description)
+        fp.write ('<div class="moddefs">\n')
+        
         if len (self.functions) > 0:
-            fp.write ('<dl class="functions">\n')
+            fp.write ('  <dl class="functions">\n')
             for func in self.functions:
                 fp.write (self.create_func_html (func, refcache))
-            fp.write ("</dl>")
+            fp.write ("  </dl>\n")
 
         if len (self.classes) > 0:
             for cls in self.classes:
                 for method in cls.methods:
                     fp.write (method.name)
                     fp.write (method.description)
+        fp.write ('</div>\n')
         fp.write (HTML_FOOTER)
         fp.close ()
         

examples/surface_fill.py

+import sys
+import pygame2
+try:
+    import pygame2.sdl.constants as sdlconst
+    import pygame2.sdl.event as event
+    import pygame2.sdl.video as video
+    import pygame2.sdl.image as image
+    import pygame2.sdl.wm as wm
+except ImportError:
+    print ("No pygame2.sdl support")
+    sys.exit ()
+
+white = pygame2.Color (255, 255, 255)
+black = pygame2.Color (0, 0, 0)
+red = pygame2.Color (255, 0, 0)
+green = pygame2.Color (0, 255, 0)
+blue = pygame2.Color (0, 0, 255)
+
+redt = pygame2.Color (255, 0, 0, 75)
+greent = pygame2.Color (0, 255, 0, 75)
+bluet = pygame2.Color (0, 0, 255, 75)
+
+rect = pygame2.Rect (10, 10, 200, 200)
+rect2 = pygame2.Rect (280, 10, 200, 200)
+rect3 = pygame2.Rect (550, 10, 200, 200)
+
+def fill_solid (screen):
+    wm.set_caption ("Solid RGB fill")
+    screen.fill (red, rect)
+    screen.fill (green, rect2)
+    screen.fill (blue, rect3)
+
+def fill_rgba (screen):
+    wm.set_caption ("Solid RGBA fill")
+    screen.fill (redt, rect)
+    screen.fill (greent, rect2)
+    screen.fill (bluet, rect3)
+
+def fill_min (screen):
+    wm.set_caption ("BLEND_MIN fill")
+    screen.fill (red, rect, sdlconst.BLEND_MIN)
+    screen.fill (green, rect2, sdlconst.BLEND_MIN)
+    screen.fill (blue, rect3, sdlconst.BLEND_MIN)
+
+def fill_rgba_min (screen):
+    screen.fill (redt, rect, sdlconst.BLEND_RGBA_MIN)
+    screen.fill (greent, rect2, sdlconst.BLEND_RGBA_MIN)
+    screen.fill (bluet, rect3, sdlconst.BLEND_RGBA_MIN)
+    wm.set_caption ("BLEND_RGBA_MIN fill")
+
+def fill_max (screen):
+    wm.set_caption ("BLEND_MAX fill")
+    screen.fill (red, rect, sdlconst.BLEND_MAX)
+    screen.fill (green, rect2, sdlconst.BLEND_MAX)
+    screen.fill (blue, rect3, sdlconst.BLEND_MAX)
+
+def fill_rgba_max (screen):
+    wm.set_caption ("BLEND_RGBA_MAX fill")
+    screen.fill (redt, rect, sdlconst.BLEND_RGBA_MAX)
+    screen.fill (greent, rect2, sdlconst.BLEND_RGBA_MAX)
+    screen.fill (bluet, rect3, sdlconst.BLEND_RGBA_MAX)
+
+def fill_add (screen):
+    wm.set_caption ("BLEND_ADD fill")
+    screen.fill (red, rect, sdlconst.BLEND_ADD)
+    screen.fill (green, rect2, sdlconst.BLEND_ADD)
+    screen.fill (blue, rect3, sdlconst.BLEND_ADD)
+
+def fill_rgba_add (screen):
+    wm.set_caption ("BLEND_RGBA_ADD fill")
+    screen.fill (redt, rect, sdlconst.BLEND_RGBA_ADD)
+    screen.fill (greent, rect2, sdlconst.BLEND_RGBA_ADD)
+    screen.fill (bluet, rect3, sdlconst.BLEND_RGBA_ADD)
+
+def fill_sub (screen):
+    wm.set_caption ("BLEND_SUB fill")
+    screen.fill (red, rect, sdlconst.BLEND_SUB)
+    screen.fill (green, rect2, sdlconst.BLEND_SUB)
+    screen.fill (blue, rect3, sdlconst.BLEND_SUB)
+
+def fill_rgba_sub (screen):
+    wm.set_caption ("BLEND_RGBA_SUB fill")
+    screen.fill (redt, rect, sdlconst.BLEND_RGBA_SUB)
+    screen.fill (greent, rect2, sdlconst.BLEND_RGBA_SUB)
+    screen.fill (bluet, rect3, sdlconst.BLEND_RGBA_SUB)
+
+def fill_mult (screen):
+    wm.set_caption ("BLEND_MULT fill")
+    screen.fill (red, rect, sdlconst.BLEND_MULT)
+    screen.fill (green, rect2, sdlconst.BLEND_MULT)
+    screen.fill (blue, rect3, sdlconst.BLEND_MULT)
+
+def fill_rgba_mult (screen):
+    wm.set_caption ("BLEND_RGBA_MULT fill")
+    screen.fill (redt, rect, sdlconst.BLEND_RGBA_MULT)
+    screen.fill (greent, rect2, sdlconst.BLEND_RGBA_MULT)
+    screen.fill (bluet, rect3, sdlconst.BLEND_RGBA_MULT)
+
+def run ():
+    filltypes = [ fill_solid, fill_rgba, fill_min, fill_rgba_min, fill_max,
+                  fill_rgba_max, fill_add, fill_rgba_add, fill_sub,
+                  fill_rgba_sub, fill_mult, fill_rgba_mult]
+    curtype = 0
+    video.init ()
+    
+    screen = video.set_mode (760, 300, 32)
+    surface = image.load_bmp ("logo.bmp").convert (flags=sdlconst.SRCALPHA)
+    
+    color = white
+    screen.fill (color)
+    screen.blit (surface, pygame2.Rect (40, 50, 0, 0))
+    screen.flip ()
+    
+    while True:
+        for ev in event.get ():
+            if ev.type == sdlconst.QUIT:
+                sys.exit ()
+            if ev.type == sdlconst.KEYDOWN and ev.key == sdlconst.K_ESCAPE:
+                sys.exit ()
+            if ev.type == sdlconst.MOUSEBUTTONDOWN:
+                curtype += 1
+                if curtype >= len (filltypes):
+                    curtype = 0
+                    if color == black:
+                        color = white
+                    else:
+                        color = black
+
+                screen.fill (color)
+                screen.blit (surface, pygame2.Rect (40, 50, 0, 0))
+                filltypes[curtype] (screen)
+                screen.flip ()
+
+if __name__ == "__main__":
+    run ()
 static PyObject* _cd_open (PyObject *self);
 static PyObject* _cd_close (PyObject *self);
 static PyObject* _cd_play (PyObject *self, PyObject *args);
-static PyObject* _cd_playtracks (PyObject *self, PyObject *args);
+static PyObject* _cd_playtracks (PyObject *self, PyObject *args,
+    PyObject *kwds);
 static PyObject* _cd_pause (PyObject *self);
 static PyObject* _cd_resume (PyObject *self);
 static PyObject* _cd_stop (PyObject *self);
     { "open", (PyCFunction) _cd_open, METH_NOARGS, "" },
     { "close", (PyCFunction) _cd_close, METH_NOARGS, "" },
     { "play", _cd_play, METH_VARARGS, "" },
-    { "play_tracks", _cd_playtracks, METH_VARARGS, "" },
+    { "play_tracks", (PyCFunction) _cd_playtracks, METH_VARARGS | METH_KEYWORDS,
+      "" },
     { "pause", (PyCFunction) _cd_pause, METH_NOARGS, "" },
     { "resume", (PyCFunction) _cd_resume, METH_NOARGS, "" },
     { "stop", (PyCFunction) _cd_stop, METH_NOARGS, "" },
 }
 
 static PyObject*
-_cd_playtracks (PyObject *self, PyObject *args)
+_cd_playtracks (PyObject *self, PyObject *args, PyObject *kwds)
 {
     int start_track = 0, start_frame = 0, ntracks = 0, nframes = 0;
     PyObject *asfps = Py_False;
     int istrue;
+    SDL_CD *cd = ((PyCD*)self)->cd;
     
-    SDL_CD *cd = ((PyCD*)self)->cd;
+    static char *kwlist[] = { "starttrack", "ntracks", "startframe", "nframes",
+                              "asfps", NULL };
+    
     ASSERT_CDROM_OPEN(self, NULL);
 
-    if (!PyArg_ParseTuple (args, "|iiiiO:play_tracks", &start_track, &ntracks,
-            &start_frame, &nframes, asfps))
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "|iiiiO:play_tracks", kwlist,
+            &start_track, &ntracks, &start_frame, &nframes, asfps))
         return NULL;
 
     if (start_track < 0 || start_track > cd->numtracks)
 static void _event_dealloc (PyEvent *self);
 
 static PyObject* _event_gettype (PyObject *self, void *closure);
+static PyObject* _event_getname (PyObject *self, void *closure);
 
 /**
  */
  */
 static PyGetSetDef _event_getsets[] = {
     { "type", _event_gettype, NULL, "", NULL },
+    { "name", _event_getname, NULL, "", NULL },
     { NULL, NULL, NULL, NULL, NULL }
 };
 
     return PyInt_FromLong (((PyEvent*)self)->type);
 }
 
+static PyObject*
+_event_getname (PyObject *self, void *closure)
+{
+    switch (((PyEvent*)self)->type)
+    {
+    case SDL_ACTIVEEVENT:
+        return PyString_FromString ("ActiveEvent");
+    case SDL_KEYDOWN:
+        return PyString_FromString ("KeyDown");
+    case SDL_KEYUP:
+        return PyString_FromString ("KeyUp");
+    case SDL_MOUSEMOTION:
+        return PyString_FromString ("MouseMotion");
+    case SDL_MOUSEBUTTONDOWN:
+        return PyString_FromString ("MouseButtonDown");
+    case SDL_MOUSEBUTTONUP:
+        return PyString_FromString ("MouseButtonUp");
+    case SDL_JOYAXISMOTION:
+        return PyString_FromString ("JoyAxisMotion");
+    case SDL_JOYBALLMOTION:
+        return PyString_FromString ("JoyBallMotion");
+    case SDL_JOYHATMOTION:
+        return PyString_FromString ("JoyHatMotion");
+    case SDL_JOYBUTTONUP:
+        return PyString_FromString ("JoyButtonUp");
+    case SDL_JOYBUTTONDOWN:
+        return PyString_FromString ("JoyButtonDown");
+    case SDL_QUIT:
+        return PyString_FromString ("Quit");
+    case SDL_SYSWMEVENT:
+        return PyString_FromString ("SysWMEvent");
+    case SDL_VIDEORESIZE:
+        return PyString_FromString ("VideoResize");
+    case SDL_VIDEOEXPOSE:
+        return PyString_FromString ("VideoExpose");
+    case SDL_NOEVENT:
+        return PyString_FromString ("NoEvent");
+    }
+    if (((PyEvent*)self)->type >= SDL_USEREVENT &&
+        ((PyEvent*)self)->type < SDL_NUMEVENTS)
+        return PyString_FromString ("UserEvent");
+    return PyString_FromString ("Unknown");
+}
+
 /* C API */
 PyObject*
 PyEvent_New (SDL_Event *event)

src/sdl/surface.c

 static PyObject* _surface_setcolorkey (PyObject *self, PyObject *args);
 static PyObject* _surface_getalpha (PyObject *self);
 static PyObject* _surface_setalpha (PyObject *self, PyObject *args);
-static PyObject* _surface_convert (PyObject *self, PyObject *args);
+static PyObject* _surface_convert (PyObject *self, PyObject *args,
+    PyObject *kwds);
 static PyObject* _surface_clone (PyObject *self);
 static PyObject* _surface_blit (PyObject *self, PyObject *args, PyObject *kwds);
 static PyObject* _surface_fill (PyObject *self, PyObject *args, PyObject *kwds);
     { "set_alpha", _surface_setalpha, METH_VARARGS, "" },
     { "get_at", _surface_getat, METH_VARARGS, "" },
     { "set_at", _surface_setat, METH_VARARGS, "" },
-    { "convert", _surface_convert, METH_VARARGS, "" },
+    { "convert", (PyCFunction) _surface_convert, METH_VARARGS | METH_KEYWORDS,
+        "" },
     { "clone", (PyCFunction)_surface_clone, METH_NOARGS, "" },
-    { "blit", _surface_blit, METH_VARARGS | METH_KEYWORDS, "" },
-    { "fill", _surface_fill, METH_VARARGS | METH_KEYWORDS, "" },
+    { "blit", (PyCFunction)_surface_blit, METH_VARARGS | METH_KEYWORDS, "" },
+    { "fill", (PyCFunction)_surface_fill, METH_VARARGS | METH_KEYWORDS, "" },
     { "save", _surface_save, METH_VARARGS, "" },
     { NULL, NULL, 0, NULL }
 };
 }
 
 static PyObject*
-_surface_convert (PyObject *self, PyObject *args)
+_surface_convert (PyObject *self, PyObject *args, PyObject *kwds)
 {
     PyObject *pxfmt = NULL, *sf;
     Uint32 flags = 0;
     SDL_Surface *surface;
     SDL_PixelFormat *fmt;
     
-    if (!PyArg_ParseTuple (args, "|Ol;convert", &pxfmt, &flags))
+    static char *keys[] = { "format", "flags", NULL };
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "|Ol;convert", keys, &pxfmt,
+        &flags))
         return NULL;
 
     if (pxfmt && !PyPixelFormat_Check (pxfmt))

src/sdl/videomod.c

 static PyObject* _sdl_getgammaramp (PyObject *self);
 static PyObject* _sdl_setgammaramp (PyObject *self, PyObject *args);
 static PyObject* _sdl_videomodeok (PyObject *self, PyObject *args);
-static PyObject* _sdl_listmodes (PyObject *self, PyObject *args);
-static PyObject* _sdl_setvideomode (PyObject *self, PyObject *args);
+static PyObject* _sdl_listmodes (PyObject *self, PyObject *args,
+    PyObject *kwds);
+static PyObject* _sdl_setvideomode (PyObject *self, PyObject *args,
+    PyObject *kwds);
 
 static PyMethodDef _video_methods[] = {
     { "init", (PyCFunction) _sdl_videoinit, METH_NOARGS, "" },
     { "get_gammaramp", (PyCFunction)_sdl_getgammaramp, METH_NOARGS, "" },
     { "set_gammaramp", _sdl_setgammaramp, METH_VARARGS, "" },
     { "is_mode_ok", _sdl_videomodeok, METH_VARARGS, "" },
-    { "list_modes", _sdl_listmodes, METH_VARARGS, "" },
-    { "set_mode", _sdl_setvideomode, METH_VARARGS, "" },
+    { "list_modes", (PyCFunction) _sdl_listmodes, METH_VARARGS | METH_KEYWORDS,
+      "" },
+    { "set_mode", (PyCFunction)_sdl_setvideomode, METH_VARARGS | METH_KEYWORDS,
+      "" },
     { NULL, NULL, 0, NULL }
 };
 
 }
 
 static PyObject*
-_sdl_listmodes (PyObject *self, PyObject *args)
+_sdl_listmodes (PyObject *self, PyObject *args, PyObject *kwds)
 {
     PyObject *pxfmt = NULL;
     Uint32 flags = 0;
     int i;
     PyObject *list, *rect;
 
+    static char *kwlist[] = { "format", "flags", NULL };
     ASSERT_VIDEO_INIT(NULL);
     
-    if (!PyArg_ParseTuple (args, "|Ol:list_modes", &pxfmt, &flags))
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "|Ol:list_modes", kwlist,
+        &pxfmt, &flags))
         return NULL;
     
     if (pxfmt && !PyPixelFormat_Check (pxfmt))
 }
 
 static PyObject*
-_sdl_setvideomode (PyObject *self, PyObject *args)
+_sdl_setvideomode (PyObject *self, PyObject *args, PyObject *kwds)
 {
     int width, height;
     int bpp = 32;
     SDL_Surface *surface;
     PyObject *sf;
 
+    static char *kwlist[] = { "width", "height", "bpp", "flags", NULL };
+    
     /* Not necessary usually. SDL_SetVideoMode() seems to do that
      * implicitly for recent versions of SDL. Though we'll force users
      * to do it explicitly. */
     ASSERT_VIDEO_INIT(NULL);
 
-    if (!PyArg_ParseTuple (args, "ii|il:set_mode", &width, &height, &bpp,
-            &flags))
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "ii|il:set_mode", kwlist,
+        &width, &height, &bpp, &flags))
         return NULL;
 
     Py_BEGIN_ALLOW_THREADS;

src/sdlmixer/channel.c

 static int _channel_init (PyObject *chunk, PyObject *args, PyObject *kwds);
 static void _channel_dealloc (PyChannel *self);
 
-static PyObject* _channel_play (PyObject *self, PyObject *args);
+static PyObject* _channel_play (PyObject *self, PyObject *args, PyObject *kwds);
 static PyObject* _channel_pause (PyObject *self);
 static PyObject* _channel_halt (PyObject *self);
 static PyObject* _channel_resume (PyObject *self);
-static PyObject* _channel_fadein (PyObject *self, PyObject *args);
+static PyObject* _channel_fadein (PyObject *self, PyObject *args,
+    PyObject *kwds);
 static PyObject* _channel_fadeout (PyObject *self, PyObject *args);
 static PyObject* _channel_expire (PyObject *self, PyObject *args);
 
 /**
  */
 static PyMethodDef _channel_methods[] = {
-    { "play", _channel_play, METH_VARARGS, "" },
+    { "play", (PyCFunction)_channel_play, METH_VARARGS | METH_KEYWORDS, "" },
     { "pause", (PyCFunction) _channel_pause, METH_NOARGS, "" },
     { "halt", (PyCFunction) _channel_halt, METH_NOARGS, "" },
     { "resume", (PyCFunction) _channel_resume, METH_NOARGS, "" },
-    { "fade_in", _channel_fadein, METH_VARARGS, "" },
+    { "fade_in", (PyCFunction)_channel_fadein, METH_VARARGS | METH_KEYWORDS,
+      "" },
     { "fade_out", _channel_fadeout, METH_VARARGS, "" },
     { "expire", _channel_expire, METH_VARARGS, "" },
     { NULL, NULL, 0, NULL }
 
 /* Methods */
 static PyObject*
-_channel_play (PyObject *self, PyObject *args)
+_channel_play (PyObject *self, PyObject *args, PyObject *kwds)
 {
     int loops = -1, ticks = -1, retval;
     PyObject *obj;
     PyChannel *chan = (PyChannel*) self;
     PyChunk *chunk;
 
-    if (!PyArg_ParseTuple (args, "O|ii:play", &obj, &loops, &ticks))
+    static char *kwlist[] = { "sound", "loops", "ticks", NULL };
+    
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "O|ii:play", kwlist, &obj,
+        &loops, &ticks))
         return NULL;
 
     if (!PyChunk_Check (obj))
 }
 
 static PyObject*
-_channel_fadein (PyObject *self, PyObject *args)
+_channel_fadein (PyObject *self, PyObject *args, PyObject *kwds)
 {
     int loops = -1, ticks = -1, ms, retval;
     PyObject *obj;
     PyChannel *chan = (PyChannel*) self;
     PyChunk *chunk;
 
-    if (!PyArg_ParseTuple (args, "Oi|ii:fade_in", &obj, &ms, &loops, &ticks))
+    static char *kwlist[] = { "sound", "ms", "loops", "ticks", NULL };
+    
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "Oi|ii:fade_in", kwlist,
+        &obj, &ms, &loops, &ticks))
         return NULL;
 
     if (ms < 0)

src/sdlmixer/music.c

 static void _music_dealloc (PyMusic *self);
 
 static PyObject* _music_play (PyObject *self, PyObject *args);
-static PyObject* _music_fadein (PyObject *self, PyObject *args);
+static PyObject* _music_fadein (PyObject *self, PyObject *args, PyObject *kwds);
 
 static PyObject* _music_gettype (PyObject *self, void *closure);
 
  */
 static PyMethodDef _music_methods[] = {
     { "play", _music_play, METH_VARARGS, "" },
-    { "fade_in", _music_fadein, METH_VARARGS, "" },
+    { "fade_in", (PyCFunction) _music_fadein, METH_VARARGS | METH_KEYWORDS,
+      "" },
     { NULL, NULL, 0, NULL }
 };
 
 }
 
 static PyObject*
-_music_fadein (PyObject *self, PyObject *args)
+_music_fadein (PyObject *self, PyObject *args, PyObject *kwds)
 {
     int loops = -1, ms, retval;
     double pos = 0;
 
+    static char *kwlist[] = { "ms", "loops", "pos", NULL };
+    
     ASSERT_MIXER_OPEN(NULL);
 
-    if (!PyArg_ParseTuple (args, "i|id:fade_in", &ms, &loops, &pos))
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "i|id:fade_in", kwlist, &ms,
+        &loops, &pos))
         return NULL;
 
     if (ms < 0)

src/sdlttf/font.c

 static PyMethodDef _font_methods[] = {
     { "get_glyph_metrics", _font_glyphmetrics, METH_VARARGS, "" },
     { "get_size", _font_getsize, METH_VARARGS, "" },
-    { "render", _font_render, METH_VARARGS | METH_KEYWORDS, "" },
+    { "render", (PyCFunction)_font_render, METH_VARARGS | METH_KEYWORDS, "" },
     { NULL, NULL, 0, NULL }
 };
 
 
     ASSERT_TTF_INIT (NULL);
 
-    if (!PyArg_ParseTupleAndKeywords (args, kwds, "OO|Oi:render", &kwlist,
+    if (!PyArg_ParseTupleAndKeywords (args, kwds, "OO|Oi:render", kwlist,
             &text, &colorfg, &colorbg, &render))
         return NULL;