Commits

marcus  committed b564775

Added sdlgfx.base unit tests.
Added sdlgfx.FPSmanager example.
Made sdlgfx.FPSmanager constructor argument optional.
Fixed NULL pointer exception in sdlgfx.FPSmanager constructor.
Moved example font to pygame2.examples.resources
sdl.time.delay () and sdl.time.get_ticks () do not need SDL_INIT_TIMER.

  • Participants
  • Parent commits 1ab634b
  • Branches pgreloaded

Comments (0)

Files changed (14)

 	$(top_srcdir)/config \
 	$(top_srcdir)/examples \
 	$(top_srcdir)/examples/freetype \
+	$(top_srcdir)/examples/resources \
 	$(top_srcdir)/examples/sdl \
 	$(top_srcdir)/examples/sdlext \
+	$(top_srcdir)/examples/sdlgfx \
 	$(top_srcdir)/doc \
 	$(top_srcdir)/doc/capi \
 	$(top_srcdir)/doc/src \
 * freetype: brush up docs and examples
 * freetype: description/markup language for rendering text (like e.g. pango
    has)?
+* freetype: fix returned height and surface height for fonts!
 
 Things to ADD:
 ==============

File doc/src/sdlgfxbase.xml

   </desc>
 
   <class name="FPSmanager">
-    <constructor>FPSManager (framerate) -> FPSManager</constructor>
+    <constructor>FPSManager ([framerate]) -> FPSManager</constructor>
     <desc>
       Creates a new FPSManager using the specified frame rate.
       
       The FPSManager takes care of measuring and keeping constant frame
       rates  for the application it is used with. On demand it automatically
       delays the execution to keep the required frame rate.
-      
+
       The desired frame rate must be within the range of
-      :const:`FPS_LOWER_LIMIT` and const:`FPS_UPPER_LIMIT`.
+      :const:`FPS_LOWER_LIMIT` and const:`FPS_UPPER_LIMIT`. If the
+      *framerate* argument is omitted, :const:`FPS_DEFAULT` is assumed
+      for it.
     </desc>
     <method name="delay">
       <call>delay () -> None</call>

File doc/src/sdltime.xml

       
       Delays the program execution for a specific *time*. The *time* is
       expressed in milliseconds.
+
+      .. note::
+
+        This does *not* require :meth:`init` to be called before.
     </desc>
   </func>
   <func name="get_ticks">
       Gets the number of milliseconds since the initialization of the underlying
       SDL library. The value will wrap if the program runs for more than ~49
       days.
+
+      .. note::
+
+        This does *not* require :meth:`init` to be called before.
     </desc>
   </func>
   <func name="init">

File examples/__init__.py

     python -m pygame2.examples.sdlext.draw
 
 """
+
+import os
+
+_filepath = os.path.abspath (__file__)
+RESOURCEDIR = os.path.join (os.path.dirname (_filepath), "resources")
+FONTDIR = os.path.join (os.path.dirname (_filepath), "resources")

File examples/freetype/sans.ttf

Binary file removed.

File examples/freetype/sdlfont.py

 import sys, os
 import pygame2
 import pygame2.font
+import pygame2.examples
+
 try:
     import pygame2.sdl.constants as constants
     import pygame2.sdl.event as event
     video.init ()
     freetype.init (8)
 
-    fontdir = os.path.dirname (os.path.abspath (__file__))
-    font = freetype.Font (os.path.join (fontdir, "sans.ttf"))
+    fontfile = os.path.join (pygame2.examples.RESOURCEDIR, "sans.ttf")
+    font = freetype.Font (fontfile)
 
     screen = video.set_mode (800, 600)
     screen.fill (colors["grey_light"])
 
-    w,h, sf = font.render(None, "Hello World", colors["red"],
+    w, h, sf = font.render(None, "Hello World", colors["red"],
                 colors['grey_dark'], ptsize=64,
                 style=ftconstants.STYLE_UNDERLINE|ftconstants.STYLE_ITALIC)
     screen.blit (sf, (32, 32))

File examples/resources/sans.ttf

Binary file added.

File examples/sdlgfx/__init__.py

+"""Examples for pygame2.sdlgfx."""

File examples/sdlgfx/fpsmanager.py

+import os, sys, time
+import pygame2
+import pygame2.examples
+try:
+    import pygame2.sdl.constants as sdlconst
+    import pygame2.sdl.event as event
+    import pygame2.sdl.video as video
+    import pygame2.sdl.wm as wm
+except ImportError:
+    print ("No pygame2.sdl support")
+    sys.exit ()
+
+try:
+    import pygame2.freetype as freetype
+except ImportError:
+    print ("No pygame2.freetype support")
+    sys.exit ()
+
+try:
+    import pygame2.sdlgfx as sdlgfx
+    import pygame2.sdlgfx.constants as gfxconst
+except ImportError:
+    print ("No pygame2.sdlgfx support")
+    sys.exit ()
+
+black = pygame2.Color (0, 0, 0)
+white = pygame2.Color (255, 255, 255)
+
+def run ():
+    video.init ()
+    freetype.init ()
+
+    fontfile = os.path.join (pygame2.examples.RESOURCEDIR, "sans.ttf")
+    font = freetype.Font (fontfile)
+
+    fpsmanager = sdlgfx.FPSmanager (2)
+
+    screen = video.set_mode (640, 480)
+    wm.set_caption ("FPSmanager example")
+    screenrect = pygame2.Rect (640, 480)
+    screen.fill (black)
+    screen.flip ()
+
+    okay = True
+    while okay:
+        for ev in event.get ():
+            if ev.type == sdlconst.QUIT:
+                okay = False
+            if ev.type == sdlconst.KEYDOWN:
+                framerate = fpsmanager.framerate
+                if ev.key == sdlconst.K_ESCAPE:
+                    okay = False
+                elif ev.key in (sdlconst.K_PLUS, sdlconst.K_KP_PLUS):
+                    framerate = min (framerate + 1, gfxconst.FPS_UPPER_LIMIT)
+                    fpsmanager.framerate = framerate
+                elif ev.key in (sdlconst.K_MINUS, sdlconst.K_KP_MINUS):
+                    framerate = max (framerate - 1, gfxconst.FPS_LOWER_LIMIT)
+                    fpsmanager.framerate = framerate
+
+        screen.fill (black)
+
+        prev = time.time ()
+        fpsmanager.delay ()
+        last = time.time ()
+
+        millis = ((last - prev) * 1000)
+        fpstext = "FPS: %d" % fpsmanager.framerate
+        timetext = "time (ms) passed since last update: %.3f" % millis
+                   
+        w, h, surfacef = font.render (None, fpstext, white, ptsize=28)
+        w2, h2, surfacet = font.render (None, timetext, white, ptsize=28)
+        blitrect = pygame2.Rect (w, h)
+        blitrect.center = screenrect.centerx, screenrect.centery - h
+        screen.blit (surfacef, blitrect.topleft)
+        blitrect = pygame2.Rect (w2, h2)
+        blitrect.center = screenrect.centerx, screenrect.centery + h
+        screen.blit (surfacet, blitrect.topleft)
+        screen.flip ()
+    
+    video.quit ()
+
+if __name__ == "__main__":
+    run ()
                  "pygame2.examples.freetype",
                  "pygame2.examples.sdl",
                  "pygame2.examples.sdlext",
+                 "pygame2.examples.sdlgfx",
                  "pygame2.sprite",
                  "pygame2.threads",
                  "pygame2.test",

File src/sdl/timemod.c

 static PyObject*
 _sdl_timegetticks (PyObject *self)
 {
-    ASSERT_TIME_INIT(NULL);
     return PyLong_FromUnsignedLong (SDL_GetTicks ());
 }
 
 {
     Uint32 ms;
 
-    ASSERT_TIME_INIT(NULL);
-    
     if (!PyArg_ParseTuple (args, "l:delay", &ms))
         return NULL;
     Py_BEGIN_ALLOW_THREADS;

File src/sdlgfx/fpsmanager.c

     if (!fps)
         return NULL;
     fps->fps = NULL;
-    return 0;
+    return (PyObject *) fps;
 }
 
 static int
 _fps_init (PyObject *self, PyObject *args, PyObject *kwds)
 {
-    int rate = -1;
+    int rate = FPS_DEFAULT;
     FPSmanager *manager;
 
-    if (!PyArg_ParseTuple (args, "i", &rate))
+    if (!PyArg_ParseTuple (args, "|i", &rate))
         return -1;
 
     if (rate > FPS_UPPER_LIMIT || rate < FPS_LOWER_LIMIT)
         PyErr_SetString (PyExc_PyGameError, SDL_GetError ());
         return -1;
     }
+
     ((PyFPSmanager*)self)->fps = manager;
     return 0;
 }

File test/sdlgfx_base_test.py

+try:
+    import pygame2.test.pgunittest as unittest
+except:
+    import pgunittest as unittest
+
+import time
+import pygame2
+import pygame2.sdlgfx.base as base
+import pygame2.sdlgfx.constants as constants
+
+class SDLGfxTest (unittest.TestCase):
+
+    def test_pygame2_sdlgfx_base_FPSmanager (self):
+        manager = base.FPSmanager ()
+        self.assertEqual (type (manager), base.FPSmanager)
+        self.assertEqual (manager.framerate, constants.FPS_DEFAULT)
+
+        self.assertRaises (ValueError, base.FPSmanager, 0)
+        self.assertRaises (ValueError, base.FPSmanager, -10)
+        self.assertRaises (ValueError, base.FPSmanager, 201)
+        self.assertRaises (ValueError, base.FPSmanager, 400)
+
+    def test_pygame2_sdlgfx_base_FPSmanager_delay(self):
+
+        # __doc__ (as of 2010-01-06) for pygame2.sdlgfx.base.FPSmanager.delay:
+
+        # delay () -> None
+        # 
+        # Delays the execution of the application to keep up the desired frame
+        # rate.
+        for i in range (10):
+            prev = time.time ()
+            manager = base.FPSmanager (10)
+            manager.delay ()
+            last = time.time ()
+            delaytime = (last - prev) * 1000
+            self.assert_ (85 < delaytime < 115)
+
+    def test_pygame2_sdlgfx_base_FPSmanager_framerate(self):
+
+        # __doc__ (as of 2010-01-06) for pygame2.sdlgfx.base.FPSmanager.framerate:
+
+        # Gets or sets the frame rate to keep.
+        self.assertEqual (base.FPSmanager (constants.FPS_LOWER_LIMIT).framerate,
+                          constants.FPS_LOWER_LIMIT)
+        self.assertEqual (base.FPSmanager (constants.FPS_UPPER_LIMIT).framerate,
+                          constants.FPS_UPPER_LIMIT)
+
+    def test_pygame2_sdlgfx_base_get_compiled_version(self):
+
+        # __doc__ (as of 2010-01-06) for pygame2.sdlgfx.base.get_compiled_version:
+
+        # get_compiled_version () -> (int, int, int)
+        # 
+        # Gets the SDL_gfx version pygame2 was compiled against as
+        # three-value tuple.
+        # 
+        # This version is built at compile time. It can be used to detect
+        # which features may not be available through Pygame, if it is used
+        # as precompiled package using a different version of the SDL_gfx
+        # library.
+        self.assertEqual (len (base.get_compiled_version ()), 3)
+        self.assertEqual (base.get_compiled_version ()[0], 2)
+        self.assertEqual (base.get_compiled_version ()[1], 0)
+        self.assert_ (base.get_compiled_version ()[2] >= 18)
+
+if __name__ == "__main__":
+    unittest.main ()