#19 SDL support

closed-works-for-me
GLU (1)
5
2004-03-01
2004-03-01
Anonymous
No

Hi,

Is there going to be support for PyOpenGL working
nicely with SDL/Pygame? GLU/GLUT don't satisfy most
needs for even a half-way decent game.

Discussion

  • Mike C. Fletcher

    Logged In: YES
    user_id=34901

    PyGame is certainly supported for most things. For
    instance, OpenGLContext has a fairly extensive PyGame
    context. There are translations of the first 10 NeHe
    tutorials to raw PyGame available from the PyGame site,
    etceteras.

    Certain things, such as using PyGame movies as images
    haven't AFAIK been tried, but likely wouldn't be too
    impossibly hard with a fairly minimal extension or two.

    BTW, GLU doesn't have anything to do with the GUI library as
    such, it's just a set of utilities for working with OpenGL.

     
  • Mike C. Fletcher

    • assigned_to: nobody --> mcfletch
    • status: open --> closed-works-for-me
     
  • Nobody/Anonymous

    Logged In: NO

    PyOpenGL works with Pygame just like OpenGL works with SDL.

    Here's a sample program found online:

    import sys # All we will use is the
    sys.exit() routine
    import pygame # Tell python we want to
    use the pygame module
    from random import choice # Imports the choice()
    function
    # from the random module
    from pygame.locals import * # Imports the constant
    symbols from
    # pygame.locals - like
    OPENGL, K_1, etc...
    from OpenGL.GL import * # This is so we can just
    do glXXXX()
    # instead of
    OpenGL.GL.glXXXXX()

    rot1=0
    rot2=0
    direction1=0
    direction2=0

    # Colors for the inner rectangle
    square1Colors=[[1,0,0,1],[0,1,0,1],[0,0,1,1],[1,1,1,1]]

    # Vertices of the inner rectangle
    square1Vertices=[[-64,-64],[64,-64],[64,64],[-64,64]]

    # Texture coordinates for the outer rectangle
    square2TexCoords=[[0,0],[1,0],[1,1],[0,1]]

    # Vertices of the outer rectangle
    square2Vertices=[[-128,-128],[128,-128],[128,128],[-128,128]]

    def draw ():
    # global means use the values declared in the global scope
    outside this function
    global rot1,rot2,direction1,direction2
    global
    square1Colors,square1Vertices,square2TexCoords,square2Vertices
    glClearColor(0.0,0.0,0.0,0.0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    # Select GL_MODELVIEW matrix
    glMatrixMode(GL_MODELVIEW)

    # Save current GL_MODELVIEW matrix state
    glPushMatrix()

    # Translate 200 units to the right and back
    glTranslatef(200,200,0)

    # Tell OpenGL that we will use a color-array
    glEnableClientState(GL_COLOR_ARRAY)

    # Tell OpenGL that we will use a vertex-array
    glEnableClientState(GL_VERTEX_ARRAY)

    # square1Colors contains colors for square1
    glColorPointerf(square1Colors)

    # square1Vertices contains vertices for square1
    glVertexPointerf(square1Vertices)

    glRotatef(rot2,0,0,1) # Rotate square1
    around (0,0,1)
    rot2=(rot2+direction2)%360 # Update rot2 value
    glBegin(GL_QUADS) # Tell OpenGL
    that we will draw quads
    glArrayElement(0) # First element
    in vertex-array
    glArrayElement(1) # Second element
    in vertex-array
    glArrayElement(2) # Third element
    in vertex-array
    glArrayElement(3) # Fourth element
    in vertex-array
    glEnd()
    glDisableClientState(GL_COLOR_ARRAY) # Disable
    color-array

    # Restore last GL_MODELVIEW matrix state
    glPopMatrix()

    # Save current GL_MODELVIEW matrix state
    glPushMatrix()

    # Translate 200 units to the right and back
    glTranslatef(200,200,0)

    # Enable 2D texturemapping
    glEnable(GL_TEXTURE_2D)

    # Enable blending
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) #
    Define blending function

    # Tell OpenGL that we will use a texture-coord-array
    glEnableClientState(GL_TEXTURE_COORD_ARRAY)

    # Tell OpenGL that we will use a vertex-array
    glEnableClientState(GL_VERTEX_ARRAY)

    # square2TexCoords contains texture coordinates for
    square2
    glTexCoordPointerf(square2TexCoords)

    # square2Vertices contains vertices for square2
    glVertexPointerf(square2Vertices)

    glRotatef(rot1,0,0,1) # Rotate squre2
    around (0,0,1)
    rot1=(rot1+direction1)%360 # Update rot1 value
    glBegin(GL_QUADS) # Tell OpenGL
    that we will draw quads
    glArrayElement(0) # First element
    of vertex-array
    glArrayElement(1) # Second element
    of vertex-array
    glArrayElement(2) # Third element
    of vertex-array
    glArrayElement(3) # Fourth element
    of vertex-array
    glTexCoord2f(0,0)
    glEnd()
    glDisable(GL_BLEND) # Disable
    blending state
    glDisable(GL_TEXTURE_2D) # Disable 2D
    texturemapping

    glFlush() # Flush
    everything to screen ASAP

    # Restore last GL_MODELVIEW matrix state
    glPopMatrix()
    pygame.display.flip() # Flip the
    double-buffer

    pygame.init() # kickstart pygame

    # we want a fullscreen, 1280x1024, SDL surface that supports
    doublebuffered OpenGL
    #pygame.display.set_mode((1280,1024),OPENGL|DOUBLEBUF|FULLSCREEN)
    pygame.display.gl_set_attribute(GL_ALPHA_SIZE, 8)
    pygame.display.set_mode((800,600),OPENGL|DOUBLEBUF)

    # create the partially transparent texture
    # pixel format is: \xRR\xGG\xBB\xAA Red,Green,Blue,Alpha
    tex=""
    for i in xrange(512):

    tex+="\xff\xff\xff\x7f\xff\xff\xff\x7f\xff\xff\xff\x7f\xff\xff\xff\x7f"

    tex+="\xff\xff\xff\x3f\xff\xff\xff\x3f\xff\xff\xff\x3f\xff\xff\xff\x3f"

    glTexImage2D(GL_TEXTURE_2D,0,4,64,64,0,GL_RGBA,GL_UNSIGNED_BYTE,tex)

    # Clamp our texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP)

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP)

    # Magnification filter is linear
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)

    # Minification filter is linear
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)

    # Texture will not be affected by current color state
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)

    glDisable(GL_DEPTH_TEST) # No depth testing

    glMatrixMode(GL_PROJECTION) # Select
    GL_PROJECTION matrix
    glLoadIdentity() # Reset
    GL_PROJECTION matrix
    #glOrtho (0,1280,0,1024,-1,1) # Set up
    orthographic projection
    glOrtho (0,800,0,600,-1,1) # Set up orthographic
    projection

    draw() # Call draw function

    # put random wave files in the soundList list
    # provide your own .wav files
    #soundList=[]
    ##soundList.append(pygame.mixer.Sound("dingdong.wav"))
    #soundList.append(pygame.mixer.Sound("trumpet1.wav"))
    #soundList.append(pygame.mixer.Sound("boo.wav"))

    # the nice and simple event loop
    while 1:
    event=pygame.event.poll ()

    if event.type is QUIT:
    sys.exit(0)

    draw()

    if event.type is KEYDOWN:
    if event.key is K_ESCAPE:
    sys.exit(0)
    if event.key is K_1:
    choice(soundList).play()
    direction2=2
    if event.key is K_2:
    choice(soundList).play()
    direction2=-2
    if event.key is K_3:
    choice(soundList).play()
    direction2=0
    if event.key is K_4:
    choice(soundList).play()
    direction1=2
    if event.key is K_5:
    choice(soundList).play()
    direction1=-2
    if event.key is K_6:
    choice(soundList).play()
    direction1=0

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks