Added the demo branch.
[krufty_fps.git] / demo / Engine.py
blob11eaf3d748ff0085f474ed26206cc9cd96943f73
1 #!/usr/bin/python
3 CONFIG = 'game_config.xml'
4 INIT = 'run.xml'
5 PACK_DIR = 'pack'
6 WINDOW_SIZE = (640, 480)
7 BACKGROUND_COLOR = (1, 0, 0, 0)
8 GUI_THEME = 'theme_two.zip'
9 WIDGETS_FILE = 'widgets.xml'
11 import os, sys
12 from os import *
13 from os.path import *
15 import pygame
16 from pygame import *
18 import lamina
20 from DerGUI import GUISystem
22 import OpenGL
23 import OpenGL.GL
24 from OpenGL.GL import *
25 import OpenGL.GLU
26 from OpenGL.GLU import *
28 from zipfile import *
30 from xml.dom.minidom import parse, parseString
32 from experimental import *
34 def main():
36 print "Engine.py class... more documentation to come"
38 class Engine:
40 def __init__(self, config_file=CONFIG):
42 assert exists(config_file)
43 dom = parse(config_file)
44 self.config = reduceDOM(dom)
45 self.config = reduceConfig(self.config)
47 # pygame init stuff
49 window_size = WINDOW_SIZE
51 if self.config.has_key('window_size'):
52 try:
53 window_size = eval(self.config['window_size'])
54 except:
55 print "Engine.__init__(): Error in eval() using default window size of", WINDOW_SIZE
57 video_flags = OPENGL|DOUBLEBUF
58 self.display = pygame.display.set_mode(window_size, video_flags)
60 # OpenGL init stuff
62 background_color = BACKGROUND_COLOR
64 if self.config.has_key('background_color'):
65 try:
66 background_color = eval(self.config['background_color'])
67 except:
68 print "Engine.__init__(): Error in eval(), using default background color of", BACKGROUND_COLOR
70 glShadeModel(GL_SMOOTH)
71 glClearColor(background_color[0],
72 background_color[1],
73 background_color[2],
74 background_color[3])
75 glClearDepth(1.0)
76 glEnable(GL_DEPTH_TEST)
77 glDepthFunc(GL_LEQUAL)
78 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
80 # more stuff
82 if self.config.has_key('window_title'):
83 self.window_title = self.config['window_title']
84 else:
85 self.window_title = WINDOW_CAPTION
86 print "Engine.__init__(): No window title specified, using default of", WINDOW_CAPTION
88 pygame.display.set_caption(self.window_title)
90 # lamina and gui stuff
92 gui_theme = GUI_THEME
94 if self.config.has_key('gui_theme'):
95 gui_theme = self.config['gui_theme']
96 else:
97 print "Engine.__init__(): No gui theme specified, using default of", GUI_THEME
99 self.lamina_screen = lamina.LaminaScreenSurface()
100 self.gui_system = GUISystem(WIDGETS_FILE, gui_theme)
102 # zipfile stuff
104 self.filelist = {}
105 self.zipfiles = []
107 pack_dir = PACK_DIR
109 if self.config.has_key('pack_dir'):
110 pack_dir = self.config['pack_dir']
111 else:
112 print "Engine.__init__(): No pack directory specified, using default of", PACK_DIR
114 for file in listdir(pack_dir):
115 if is_zipfile(os.path.join(pack_dir, file)):
116 self.zipfiles.append(ZipFile(os.path.join(pack_dir, file)))
118 for item in self.zipfiles[-1].namelist():
119 self.filelist[item] = "self.zipfiles[" + str(len(self.zipfiles) - 1) + "].read(\"" + item + "\")"
121 self.files = {}
123 self.menudefs = {}
124 self.objectdefs = {}
125 self.objects = {}
126 self.menus = {}
128 def addFile(self, filename, parent=''):
130 if self.filelist.has_key(filename):
132 dom = parseString(eval(self.filelist[filename]))
133 assert dom != None
135 parsed = reduceDOM(dom)
136 instances = []
138 results = {
139 'menu_def': {},
140 'object_def': {},
141 'menu_instance': {},
142 'object_instance': {},
143 'parent': None,
144 'children': []}
146 for item in parsed[1][0]:
148 if item[0] == 'def':
150 for defx in item[1][0]:
152 if defx[0] == 'object':
153 obj = reduceObjectDef(defx)
154 results[obj['name']] = obj
156 if defx[0] == 'menu':
157 menu = reduceMenuDef(defx)
158 results[menu[0]] = menu
160 if item[0] == 'instance':
162 for defx in item[1][0]:
164 inst = reduceInstance(defx)
165 instances.append(inst)
167 # do instance stuff here
168 for item in instances:
169 print "debug", item
171 results['parent'] = parent
172 if not parent == '':
173 self.files[parent]['children'].append(filename)
175 return 1
177 else:
178 return 0
180 if __name__ == '__main__': main()