Commits

Marcus von Appen committed 1dcbdb4

- updated examples to match new package and module behaviour
- added sdl2hello.py example that demonstrates direct SDL2 usage

Comments (0)

Files changed (10)

examples/__init__.py

 
 This package contains the examples for sdl2.
 """
-import os
-from sdl2.ext import Resources
 
-__all__ = ["RESOURCES"]
-
-_fpath = os.path.dirname(os.path.abspath(__file__))
-RESOURCES = Resources(os.path.join(_fpath, "resources"))

examples/colorpalettes.py

     # Try to import SDL2. The import might fail, if the SDL2 DLL could not be
     # loaded. In that case, just print the error and exit with a proper
     # error code.
+    from sdl2 import SDL_QUIT, SDL_MOUSEBUTTONDOWN
     import sdl2.ext as sdl2ext
-    from sdl2 import events as sdlevents
     # Import the pre-defined color palettes
     import sdl2.ext.colorpalettes as colorpalettes
 except ImportError:
             # application, just as you are used to closing an editor.
             # If the user wants to quit the application, we should let him do
             # so. This is done by breaking out of the while loop.
-            if event.type == sdlevents.SDL_QUIT:
+            if event.type == SDL_QUIT:
                 running = False
                 break
 
             # type, the user pressed the mouse button, but did not necesarily
             # release  it. As such, it is not a typical click, but only 50% of
             # it, which is sufficient for our case here.
-            if event.type == sdlevents.SDL_MOUSEBUTTONDOWN:
+            if event.type == SDL_MOUSEBUTTONDOWN:
                 # If the user pressed the button, we want to draw the next
                 # palette and update the window title accordingly. We do this
                 # by increasing the storage variable and - in case it reached
 """User interface examples."""
 import sys
-
-# Import the Color class, so we can create RGBA color values.
-from sdl2.ext.color import Color
+from sdl2.events import SDL_TEXTINPUT
 
 # Try to import SDL2. The import might fail, if the SDL2 DLL could not be
 # loaded. In that case, just print the error and exit with a proper
 # error code.
 try:
+    from sdl2 import SDL_QUIT
     import sdl2.ext as sdl2ext
-    import sdl2.events as sdlevents
 except ImportError:
     import traceback
     traceback.print_exc()
     sys.exit(1)
 
+
 # Define some global color constants
-WHITE = Color(255, 255, 255)
-GREY = Color(200, 200, 200)
+WHITE = sdl2ext.Color(255, 255, 255)
+GREY = sdl2ext.Color(200, 200, 200)
 
-# Import the resources, so we have easy access to the example images.
-from sdl2.ext.resources import Resources
-RESOURCES = Resources(__file__, "resources")
+# Create a resource, so we have easy access to the example images.
+RESOURCES = sdl2ext.Resources(__file__, "resources")
 
 
 # A callback for the Button.motion event.
     while running:
         events = sdl2ext.get_events()
         for event in events:
-            if event.type == sdlevents.SDL_QUIT:
+            if event.type == SDL_QUIT:
                 running = False
                 break
             # Pass the SDL2 events to the UIProcessor, which takes care of

examples/helloworld.py

 # We'll use sys to properly exit with an error code.
 import sys
 
-# Try to import the video system. Since mule.video makes use of
-# mule.sdl, the import might fail, if the SDL DLL could not be
-# loaded. In that case, just print the error and exit with a proper
-# error code.
+# Try to import the video system. Since sdl2.ext makes use of sdl2, the
+# import might fail, if the SDL DLL could not be loaded. In that case, just
+# print the error and exit with a proper error code.
 try:
     import sdl2.ext as sdl2ext
 except ImportError:
 # resource, we bundle with our application. We are using the current
 # file's location and define the "resources" subdirectory as the
 # location, in which we keep all data.
-from sdl2.ext import Resources
-RESOURCES = Resources(__file__, "resources")
-
+RESOURCES = sdl2ext.Resources(__file__, "resources")
 
 
 def run():
     else:
         print("Using software rendering")
         factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
-    
+
     # Creates a simple rendering system for the Window. The
     # SpriteRenderer can draw Sprite objects on the window.
     spriterenderer = factory.create_sprite_renderer(window)

examples/particles.py

 """Particle simulation"""
 import sys
-import ctypes
 import random
 
 # Try to import SDL2. The import might fail, if the SDL2 DLL could
 # not be loaded. In that case, just print the error and exit with a
 # proper error code.
 try:
+    from sdl2 import SDL_QUIT, SDL_MOUSEMOTION, SDL_FlushEvent, \
+        SDL_WarpMouseInWindow, SDL_ShowCursor, SDL_Rect, SDL_RenderCopy
     import sdl2.ext as sdl2ext
-    import sdl2.events as sdlevents
-    import sdl2.mouse as sdlmouse
-    import sdl2.timer as sdltimer
-    import sdl2.render as sdlrender
-    import sdl2.rect as sdlrect
 except ImportError:
     import traceback
     traceback.print_exc()
 # Import the particles module, so we have access to all relevant parts
 # for dealing with particles.
 import sdl2.ext.particles as particles
-# We will create some systems and an entity for creating the particle
-# simulation. Hence we will need some things from the ebs module.
-from sdl2.ext.ebs import Entity, System, World
 
-
-# Import the resources, so we have easy access to the example images.
-from sdl2.ext.resources import Resources
-RESOURCES = Resources(__file__, "resources")
+# Create a resource, so we have easy access to the example images.
+RESOURCES = sdl2ext.Resources(__file__, "resources")
 
 
 # The Particle class offered by sdl2.ext.particles only contains the life
 
 # A simple Entity class, that contains the particle information. This
 # represents our living particle object.
-class EParticle(Entity):
+class EParticle(sdl2ext.Entity):
     def __init__(self, world, x, y, vx, vy, ptype, life):
         self.cparticle = CParticle(self, x, y, vx, vy, ptype, life)
 
 # similar to the TextureSprinteRenderer from mule.video. Since we
 # operate on particles rather than sprites, we need to provide our own
 # rendering logic.
-class ParticleRenderer(System):
+class ParticleRenderer(sdl2ext.System):
     def __init__(self, renderer, images):
         # Create a new particle renderer. The surface argument will be
         # the targets surface to do the rendering on. images is a set of
         #
         # The SDL_Rect is used for the blit operation below and is used
         # as destination position for rendering the particle.
-        r = sdlrect.SDL_Rect()
+        r = SDL_Rect()
 
         # The SDL2 blit function to use. This will take an image
         # (SDL_Texture) as source and copies it on the target.
-        dorender = sdlrender.SDL_RenderCopy
+        dorender = SDL_RenderCopy
 
         # And some more shortcuts.
         sdlrenderer = self.renderer.renderer
 
 def run():
     # Create the environment, in which our particles will exist.
-    world = World()
+    world = sdl2ext.World()
 
     # Set up the globally available information about the current mouse
     # position. We use that information to determine the emitter
 
     # Center the mouse on the window. We use the SDL2 functions directly
     # here. Since the SDL2 functions do not know anything about the
-    # video.Window class, we have to pass the window's SDL_Window to it.
-    sdlmouse.SDL_WarpMouseInWindow(window.window, world.mousex, world.mousey)
+    # sdl2.ext.Window class, we have to pass the window's SDL_Window to it.
+    SDL_WarpMouseInWindow(window.window, world.mousex, world.mousey)
 
-    # Hide the mouse cursor, os it does not show up - just show the
+    # Hide the mouse cursor, so it does not show up - just show the
     # particles.
-    sdlmouse.SDL_ShowCursor(0)
+    SDL_ShowCursor(0)
 
     # Create the rendering system for the particles. This is somewhat
     # similar to the SoftSpriteRenderer, but since we only operate with
     running = True
     while running:
         for event in sdl2ext.get_events():
-            if event.type == sdlevents.SDL_QUIT:
+            if event.type == SDL_QUIT:
                 running = False
                 break
 
-            if event.type == sdlevents.SDL_MOUSEMOTION:
+            if event.type == SDL_MOUSEMOTION:
                 # Take care of the mouse motions here. Every time the
                 # mouse is moved, we will make that information globally
                 # available to our application environment by updating
                 # queue (10ths to 100ths!), and we do not want to handle
                 # each of them. For this example, it is enough to handle
                 # one per update cycle.
-                sdlevents.SDL_FlushEvent(sdlevents.SDL_MOUSEMOTION)
+                SDL_FlushEvent(SDL_MOUSEMOTION)
                 break
         world.process()
 

examples/pixelaccess.py

 # loaded. In that case, just print the error and exit with a proper
 # error code.
 try:
+    from sdl2 import SDL_QUIT, SDL_MOUSEBUTTONDOWN
     import sdl2.ext as sdl2ext
-    import sdl2.events as sdlevents
 except ImportError:
     import traceback
     traceback.print_exc()
     # automatically at the time the PixelView is garbage-collected.
     del pixelview
 
+
 # as draw_horizontal_stripes(), but vertical
 def draw_vertical_stripes(surface, x1, x2, y1, y2):
     sdl2ext.fill(surface, BLACK)
     while running:
         events = sdl2ext.get_events()
         for event in events:
-            if event.type == sdlevents.SDL_QUIT:
+            if event.type == SDL_QUIT:
                 running = False
                 break
-            if event.type == sdlevents.SDL_MOUSEBUTTONDOWN:
+            if event.type == SDL_MOUSEBUTTONDOWN:
                 curindex += 1
                 if curindex >= len(functions):
                     curindex = 0
                     vel.vy = 0
             else:
                 bcentery = self.ball.sprite.y + self.ball.sprite.size[1] // 2
-                if bcentery < centery + sheight // 3:
+                if bcentery < centery:
                     vel.vy = -PADDLE_SPEED
-                elif bcentery > centery - sheight // 3:
+                elif bcentery > centery:
                     vel.vy = PADDLE_SPEED
                 else:
                     vel.vy = 0

examples/scene.py

 """Scene management examples."""
 import sys
 
-from sdl2.ext.scene import Scene, SceneManager
-
-# Try to import the video system and event system. Since mule.video makes
-# use of mule.sdl, the import might fail, if the SDL DLL could not be
+# Try to import SDL2. The import might fail, if the SDL2 DLL could not be
 # loaded. In that case, just print the error and exit with a proper
 # error code.
 try:
+    from sdl2 import SDL_QUIT
     import sdl2.ext as sdl2ext
-    import sdl2.events as sdlevents
 except ImportError:
     import traceback
     traceback.print_exc()
     sys.exit(1)
 
+# Import the scene system
+from sdl2.ext.scene import Scene, SceneManager
+
 
 # A simple, extended Scene that contains visual components
 class ExampleScene(Scene):
         # The main event loop; we already learned about that in other examples.
         # Check for the events and pass them around.
         for event in sdl2ext.get_events():
-            if event.type == sdlevents.SDL_QUIT:
+            if event.type == SDL_QUIT:
                 running = False
                 break
             # Pass the SDL2 events to the UIProcessor, which takes care of

examples/sdl2hello.py

+"""Simple example for using sdl2 directly."""
+import os
+import sys
+import ctypes
+
+from sdl2 import *
+
+def run():
+    SDL_Init(SDL_INIT_VIDEO)
+    window = SDL_CreateWindow(b"Hello World",
+                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+                              592, 460, SDL_WINDOW_SHOWN)
+    fname = os.path.join(os.path.dirname(os.path.abspath(__file__)),
+                         "resources", "hello.bmp")
+    image = SDL_LoadBMP(fname.encode("utf-8"))
+    windowsurface = SDL_GetWindowSurface(window)
+    SDL_BlitSurface(image, None, windowsurface, None)
+    SDL_UpdateWindowSurface(window)
+    SDL_FreeSurface(image)
+
+    running = True
+    event = SDL_Event()
+    while running:
+        while SDL_PollEvent(ctypes.byref(event)) != 0:
+            if event.type == SDL_QUIT:
+                running = False
+                break
+
+    SDL_DestroyWindow(window)
+    SDL_Quit()
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(run())
 import sys
 from distutils.core import setup
 
-VERSION = "0.1.0"
+VERSION = "0.2.0"
 
 if __name__ == "__main__":