diff --git a/.hgignore b/.hgignore index 9e32b48..8cf0eac 100644 --- a/.hgignore +++ b/.hgignore @@ -1,2 +1,3 @@ syntax: glob *.pyc +settings.cfg diff --git a/config.py b/config.py new file mode 100644 index 0000000..77f5420 --- /dev/null +++ b/config.py @@ -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 diff --git a/fonts.py b/fonts.py index f7ed13f..0f358e3 100644 --- a/fonts.py +++ b/fonts.py @@ -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. diff --git a/models.py b/models.py index 54cd33e..0106d7d 100755 --- a/models.py +++ b/models.py @@ -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() diff --git a/platform.py b/platform.py new file mode 100644 index 0000000..a19ab0d --- /dev/null +++ b/platform.py @@ -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") \ No newline at end of file diff --git a/platform_darwin.py b/platform_darwin.py new file mode 100644 index 0000000..82fe4f5 --- /dev/null +++ b/platform_darwin.py @@ -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 + \ No newline at end of file diff --git a/platform_posix.py b/platform_posix.py new file mode 100644 index 0000000..d76ddd9 --- /dev/null +++ b/platform_posix.py @@ -0,0 +1,10 @@ + +def enable_vsync(): + pass + +def get_window_handle(): + return None + +def get_window_rect(handle=None): + pass + \ No newline at end of file diff --git a/platform_win32.py b/platform_win32.py new file mode 100644 index 0000000..2f01b4b --- /dev/null +++ b/platform_win32.py @@ -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 ) diff --git a/roc.py b/roc.py index cdb5751..8165341 100755 --- a/roc.py +++ b/roc.py @@ -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) diff --git a/roc_main.py b/roc_main.py index f5f1466..5cf4323 100755 --- a/roc_main.py +++ b/roc_main.py @@ -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() \ No newline at end of file diff --git a/roc_test.py b/roc_test.py index 23a61a4..d288367 100755 --- a/roc_test.py +++ b/roc_test.py @@ -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() \ No newline at end of file +roc.main() + + + diff --git a/video.py b/video.py index 37d2f9e..4b5d925 100755 --- a/video.py +++ b/video.py @@ -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() -