Added platform specific architecture and cleaned up some of the platform

specific hacks in video.py
Added a config file (settings.cfg)
Added functionality for saving last used window position!
Added displaylists to models.py
Broke fonts (temporarily)
This commit is contained in:
cecilkorik 2011-10-23 01:42:40 -06:00
parent 14f391013f
commit ab901dc58f
12 changed files with 175 additions and 27 deletions

View file

@ -1,2 +1,3 @@
syntax: glob
*.pyc
settings.cfg

45
config.py Normal file
View file

@ -0,0 +1,45 @@
import os
import files
class configmanager(object):
def __init__(self):
self.filepath = None
self.data = {}
def load(self):
self.filepath = os.path.join(files.get_basedir(), 'settings.cfg')
if not os.path.exists(self.filepath):
return
fd = open(self.filepath, 'r')
for x in fd:
x = x.strip()
if not '=' in x:
continue
x = x.split('=')
k = x[0].strip()
v = '='.join(x[1:]).strip()
self.data[k] = v
fd.close()
def save(self):
fd = open(self.filepath, 'w')
for k, v in self.data.items():
fd.write("%s = %s\n" % (k, v))
fd.close()
def read_key(self, key):
if key in self.data:
return self.data[key]
else:
return None
def write_key(self, key, value):
self.data[key] = value
def init():
global mgr
mgr = configmanager()
mgr.load()
mgr = None

View file

@ -48,7 +48,9 @@ class TexFont(object):
tf.load_definition(os.path.join(fontdir, "%s.tfd" % (fontname,)))
tf.load_texture(os.path.join(fontdir, "%s.png" % (fontname,)))
return tf
def render
def mapchar(self, char):
"""
Maps a char to an index.

View file

@ -252,8 +252,7 @@ class Material(object):
class Mesh(object):
def __init__(self):
self.vertexes = []
def set_rotation(self):
glPushMatrix()
glMultMatrixf(self.rot.get_matrix())
@ -319,6 +318,7 @@ class RenderLayer(object):
self.material = None
self.color = [1.0, 1.0, 1.0, 1.0]
self.scale = [1.0, 1.0, 1.0]
self.displaylist = None
self.anim = "idle"
def load(self, layerdata):
@ -334,7 +334,14 @@ class RenderLayer(object):
self.material.bind()
glPushMatrix()
glScalef(*self.scale)
self.mesh.draw(self.material.texcoords)
if self.displaylist == None:
self.displaylist = glGenLists(1)
glNewList(self.displaylist, GL_COMPILE)
self.mesh.draw(self.material.texcoords)
glEndList()
glCallList(self.displaylist)
glPopMatrix()

9
platform.py Normal file
View file

@ -0,0 +1,9 @@
import sys
if sys.platform == 'win32':
from platform_win32 import *
elif sys.platform == 'darwin':
from platform_darwin import *
elif sys.platform == 'posix':
from platform_posix import *
else:
raise NotImplementedError("Not ported to this platform")

17
platform_darwin.py Normal file
View file

@ -0,0 +1,17 @@
def enable_vsync():
try:
import ctypes
import ctypes.util
ogl = ctypes.cdll.LoadLibrary(ctypes.util.find_library("OpenGL"))
v = ctypes.c_int(1)
ogl.CGLSetParameter(ogl.CGLGetCurrentContext(), ctypes.c_int(222), ctypes.pointer(v))
except:
print "Unable to set vsync mode, using driver defaults"
def get_window_handle():
return None
def get_window_rect(handle=None):
pass

10
platform_posix.py Normal file
View file

@ -0,0 +1,10 @@
def enable_vsync():
pass
def get_window_handle():
return None
def get_window_rect(handle=None):
pass

30
platform_win32.py Normal file
View file

@ -0,0 +1,30 @@
from ctypes import *
import pygame
def enable_vsync():
pass
def get_window_handle():
return pygame.display.get_wm_info()['window']
def get_window_rect(handle=None):
if handle is None:
handle = get_window_handle()
class RECT(Structure):
_fields_ = [('left', c_long),
('top', c_long),
('right', c_long),
('bottom', c_long)]
class POINT(Structure):
_fields_ = [('x', c_long),
('y', c_long)]
r = RECT()
p = POINT()
p.x = 0
p.y = 0
windll.user32.ClientToScreen(c_int(handle), byref(p))
return ( p.x, p.y, p.x, p.y )
return ( r.left, r.top, r.right, r.bottom )

4
roc.py
View file

@ -4,6 +4,9 @@ import video
import shader
import models
import roc_main
import universe
import config
base_universe = universe.base_universe
def init2d():
init(videoinit=video.init2d)
@ -14,6 +17,7 @@ def init3d():
def init(videoinit):
pygame.init()
files.init()
config.init()
shader.init()
size = width, height = (1600,1200)

View file

@ -6,6 +6,7 @@ import video
import shader
import inputs
import models
import config
from OpenGL.GL import *
@ -36,7 +37,7 @@ def mainloop():
# video.force_redraw()
if 'exit' in inputs.commandsh:
if 'exit' in inputs.commands:
break
video.predraw()
@ -44,3 +45,6 @@ def mainloop():
g_universe.frame(events)
video.next_frame()
video.save_window_pos()
config.mgr.save()

View file

@ -1,7 +1,12 @@
import roc
import pygame
from OpenGL.GL import *
from py3dutil import vect, quat
import models
from platform_win32 import *
class test_universe(base_universe):
def frame(self):
class test_universe(roc.base_universe):
def frame(self, events):
# pink triangle
glBegin(GL_TRIANGLE_STRIP)
glNormal3f(0.0, 0.0, 1.0)
@ -14,7 +19,7 @@ class test_universe(base_universe):
# yellow square
glBegin(GL_TRIANGLE_STRIP)
glNormal3f(0.0, 0.0, 1.0)
glColor4f(1.0, 1.0, 0.0, 1.0)
glColor4f(0.0, 0.2, 0.5, 1.0)
glVertex3f( 150.0, 20.0, -50.0)
glVertex3f( 150.0, 400.0, -50.0)
glVertex3f( 360.0, 20.0, -50.0)
@ -23,10 +28,16 @@ class test_universe(base_universe):
# texture test
mdl = models.mgr.create("m_test")
glPushMatrix()
glTranslate(100.0, 300.0, -5.0)
mdl.render()
glPopMatrix()
roc.init2d()
roc.set_universe(test_universe())
roc.main()
roc.main()

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python2.4
import os
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.arrays import GLcharArray
@ -7,25 +6,22 @@ import pygame
from pygame.locals import *
import sys
import shader
import config
import platform
height=None
width=None
hwnd=None
def enable_vsync():
if sys.platform != 'darwin':
return
try:
import ctypes
import ctypes.util
ogl = ctypes.cdll.LoadLibrary(ctypes.util.find_library("OpenGL"))
v = ctypes.c_int(1)
ogl.CGLSetParameter(ogl.CGLGetCurrentContext(), ctypes.c_int(222), ctypes.pointer(v))
except:
print "Unable to set vsync mode, using driver defaults"
platform.enable_vsync()
def skipping_next_frame():
return False
def set_res((width_in, height_in)):
global height, width
global height, width, hwnd
load_window_pos()
height = height_in
width = width_in
@ -38,9 +34,10 @@ def set_res((width_in, height_in)):
video_flags = OPENGL|DOUBLEBUF
#video_flags = OPENGL|DOUBLEBUF|FULLSCREEN
pygame.display.set_mode((width, height), video_flags)
hwnd = platform.get_window_handle()
def get_res():
return (height, width)
return (width, height)
def get_height():
return height
@ -48,6 +45,21 @@ def get_height():
def get_width():
return width
def save_window_pos():
global hwnd
wpos = platform.get_window_rect(hwnd)
if not wpos is None:
config.mgr.write_key('windowpos', '%s,%s' % wpos[0:2])
def load_window_pos():
windowpos = config.mgr.read_key('windowpos')
if windowpos is None:
return
os.environ['SDL_VIDEO_WINDOW_POS'] = windowpos
def init2d():
#print pygame.display.gl_get_attribute(GL_DEPTH_SIZE)
@ -128,7 +140,3 @@ def predraw():
def next_frame():
pygame.display.flip()
if __name__ == '__main__': main()