General modernizations and porting to Python3

Included some missing files
This commit is contained in:
cecilkorik 2017-05-08 13:10:48 -07:00
parent 5f3e46d7bb
commit a9ac67e6d6
42 changed files with 1637 additions and 897 deletions

View file

@ -1,3 +1,8 @@
syntax: glob syntax: glob
*.pyc *.pyc
settings.cfg settings.cfg
lib
bin
include
pip-selfcheck.json
__pycache__

View file

@ -1,4 +1,4 @@
import roc from roc_core import *
import universe import universe
import pipeline import pipeline
import inputs import inputs
@ -9,4 +9,18 @@ import enums
import files import files
import gametimer import gametimer
import shader import shader
import video import video
#from .universe import *
#from .pipeline import *
#from .inputs import *
#from .physics import *
#from .models import *
#from .gamedata import *
#from .enums import *
#from .files import *
#from .gametimer import *
#from .shader import *
#from .video import *
#

View file

@ -1,5 +1,5 @@
from gameobj import gameobj from .gameobj import gameobj
class asset_manager(object): class asset_manager(object):
def __init__(self): def __init__(self):

View file

@ -1,4 +1,4 @@
from gameobj import game_object from .gameobj import game_object
import math import math
from quat import * from quat import *
from py3dutil import vect from py3dutil import vect

View file

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

BIN
data/font/coure10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

102
data/font/coure10.tfd Normal file
View file

@ -0,0 +1,102 @@
fontheader
32,128
9,14
13,8
128,128
charsizes
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13
8,13

BIN
data/font/micross20.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

102
data/font/micross20.tfd Normal file
View file

@ -0,0 +1,102 @@
fontheader
32,128
21,25
11,9
256,256
charsizes
5,24
6,24
7,24
13,24
11,24
17,24
13,24
4,24
7,24
7,24
8,24
11,24
6,24
7,24
6,24
6,24
11,24
12,24
12,24
11,24
12,24
11,24
11,24
11,24
11,24
11,24
6,24
6,24
12,24
11,24
12,24
12,24
20,24
15,24
14,24
14,24
15,24
14,24
12,24
15,24
15,24
6,24
10,24
14,24
12,24
17,24
15,24
16,24
14,24
16,24
14,24
13,24
13,24
15,24
14,24
19,24
14,24
14,24
13,24
6,24
6,24
6,24
9,24
11,24
7,24
11,24
11,24
10,24
11,24
11,24
7,24
11,24
11,24
4,24
5,24
10,24
4,24
16,24
11,24
12,24
11,24
11,24
7,24
11,24
7,24
11,24
10,24
15,24
10,24
10,24
11,24
7,24
6,24
8,24
12,24
6,24

BIN
data/font/micross42.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

102
data/font/micross42.tfd Normal file
View file

@ -0,0 +1,102 @@
fontheader
32,128
43,49
11,10
512,512
charsizes
11,48
12,48
15,48
25,48
23,48
38,48
28,48
8,48
15,48
15,48
16,48
24,48
12,48
13,48
12,48
12,48
24,48
24,48
23,48
24,48
24,48
24,48
24,48
23,48
24,48
24,48
12,48
12,48
24,48
24,48
24,48
24,48
42,48
30,48
27,48
30,48
30,48
28,48
26,48
33,48
30,48
12,48
21,48
28,48
23,48
34,48
30,48
33,48
28,48
33,48
30,48
28,48
26,48
30,48
28,48
40,48
28,48
28,48
26,48
12,48
12,48
12,48
20,48
24,48
14,48
24,48
23,48
21,48
23,48
23,48
13,48
23,48
24,48
10,48
12,48
22,48
10,48
36,48
24,48
22,48
23,48
23,48
15,48
21,48
12,48
24,48
21,48
31,48
21,48
21,48
21,48
14,48
11,48
13,48
25,48
12,48

BIN
data/font/monopro6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

102
data/font/monopro6.tfd Normal file
View file

@ -0,0 +1,102 @@
fontheader
32,128
7,11
13,8
128,128
charsizes
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

@ -0,0 +1,102 @@
fontheader
32,128
8,12
12,8
128,128
charsizes
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11
7,11

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -0,0 +1,102 @@
fontheader
32,128
7,11
13,8
128,128
charsizes
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10
6,10

BIN
data/font/vgafix12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

102
data/font/vgafix12.tfd Normal file
View file

@ -0,0 +1,102 @@
fontheader
32,128
9,16
14,8
128,128
charsizes
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15
8,15

BIN
data/tex/black.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 281 B

BIN
data/tex/dralthi.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

BIN
data/tex/nm_flat.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 298 B

BIN
data/tex/plasma1.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

BIN
data/tex/plasma2.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 B

BIN
data/tex/test1.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 17 KiB

BIN
data/tex/white.png Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 283 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

422
fonts.py
View file

@ -1,212 +1,212 @@
import os import os
import pygame import pygame
from OpenGL.GL import * from OpenGL.GL import *
class TextureFile(object): class TextureFile(object):
def __init__(self): def __init__(self):
self.id = None self.id = None
self.filename = None self.filename = None
self.h = None self.h = None
self.w = None self.w = None
def load(self, filename): def load(self, filename):
self.filename = filename self.filename = filename
img = pygame.image.load(filename) img = pygame.image.load(filename)
img.convert_alpha() img.convert_alpha()
glActiveTexture(GL_TEXTURE0) glActiveTexture(GL_TEXTURE0)
texid = glGenTextures(1) texid = glGenTextures(1)
#print "Generated font texture id %s" % (texid,) #print "Generated font texture id %s" % (texid,)
self.id = texid self.id = texid
imgdata = pygame.image.tostring(img, "RGBA") imgdata = pygame.image.tostring(img, "RGBA")
imgr = img.get_rect() imgr = img.get_rect()
self.h = imgr.h self.h = imgr.h
self.w = imgr.w self.w = imgr.w
dimension = GL_TEXTURE_2D dimension = GL_TEXTURE_2D
glBindTexture(dimension, texid) glBindTexture(dimension, texid)
glTexImage2D(dimension, 0, GL_RGBA8, imgr.w, imgr.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgdata) glTexImage2D(dimension, 0, GL_RGBA8, imgr.w, imgr.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgdata)
glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
class TexFont(object): class TexFont(object):
def __init__(self): def __init__(self):
self.image = None self.image = None
self.start = None self.start = None
self.end = None self.end = None
self.rows = None self.rows = None
self.cols = None self.cols = None
self.charheight = None self.charheight = None
self.charwidth = None self.charwidth = None
self.texwidth = None self.texwidth = None
self.texheight = None self.texheight = None
self.gltex = None self.gltex = None
self.monospace = False self.monospace = False
self.charsizes = [] self.charsizes = []
self.colors = (1.0, 1.0, 1.0, 1.0) self.colors = (1.0, 1.0, 1.0, 1.0)
@staticmethod @staticmethod
def new(fontdir, fontname): def new(fontdir, fontname):
tf = TexFont() tf = TexFont()
tf.load_definition(os.path.join(fontdir, "%s.tfd" % (fontname,))) tf.load_definition(os.path.join(fontdir, "%s.tfd" % (fontname,)))
tf.load_texture(os.path.join(fontdir, "%s.png" % (fontname,))) tf.load_texture(os.path.join(fontdir, "%s.png" % (fontname,)))
return tf return tf
def render(self, text, color=None): def render(self, text, color=None):
if color is None: if color is None:
color = self.colors color = self.colors
#print "Binding texture %s" % (self.gltex.id,) #print "Binding texture %s" % (self.gltex.id,)
glActiveTexture(GL_TEXTURE0) glActiveTexture(GL_TEXTURE0)
dimension = GL_TEXTURE_2D dimension = GL_TEXTURE_2D
glBindTexture(GL_TEXTURE_2D, self.gltex.id) glBindTexture(GL_TEXTURE_2D, self.gltex.id)
glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glBegin(GL_TRIANGLES) glBegin(GL_TRIANGLES)
glNormal3f(0.0, 0.0, 1.0) glNormal3f(0.0, 0.0, 1.0)
glColor4f(*color) glColor4f(*color)
#print "Rendering font" #print "Rendering font"
offset = 0.0 offset = 0.0
for i, letter in enumerate(text): for i, letter in enumerate(text):
offset = self.render_letter(letter, offset) offset = self.render_letter(letter, offset)
glEnd() glEnd()
def render_letter(self, letter, offset): def render_letter(self, letter, offset):
charidx = self.mapchar(letter) charidx = self.mapchar(letter)
x1, y1, x2, y2 = self.get_coordinates(charidx) x1, y1, x2, y2 = self.get_coordinates(charidx)
cx, cy = self.charsize(charidx) cx, cy = self.charsize(charidx)
#print "Printing letter %s(%s) from %s, %s to %s, %s using texcoords %s, %s, %s, %s" % (letter, charidx, offset, 0.0, offset + cx, cy, x1*128.0, y1*128.0, x2*128.0, y2*128.0) #print "Printing letter %s(%s) from %s, %s to %s, %s using texcoords %s, %s, %s, %s" % (letter, charidx, offset, 0.0, offset + cx, cy, x1*128.0, y1*128.0, x2*128.0, y2*128.0)
glTexCoord2f(x1, y1); glVertex3f( offset + 0.0, 0.0, 0.0) glTexCoord2f(x1, y1); glVertex3f( offset + 0.0, 0.0, 0.0)
glTexCoord2f(x1, y2); glVertex3f( offset + 0.0, cy, 0.0) glTexCoord2f(x1, y2); glVertex3f( offset + 0.0, cy, 0.0)
glTexCoord2f(x2, y1); glVertex3f( offset + cx, 0.0, 0.0) glTexCoord2f(x2, y1); glVertex3f( offset + cx, 0.0, 0.0)
glTexCoord2f(x2, y1); glVertex3f( offset + cx, 0.0, 0.0) glTexCoord2f(x2, y1); glVertex3f( offset + cx, 0.0, 0.0)
glTexCoord2f(x1, y2); glVertex3f( offset + 0.0, cy, 0.0) glTexCoord2f(x1, y2); glVertex3f( offset + 0.0, cy, 0.0)
glTexCoord2f(x2, y2); glVertex3f( offset + cx, cy, 0.0) glTexCoord2f(x2, y2); glVertex3f( offset + cx, cy, 0.0)
glTexCoord2f(0.0, 0.0) glTexCoord2f(0.0, 0.0)
return offset + cx return offset + cx
def charsize(self, idx): def charsize(self, idx):
if self.monospace: if self.monospace:
return (self.charwidth, self.charheight) return (self.charwidth, self.charheight)
else: else:
return self.charsizes[idx] return self.charsizes[idx]
def mapchar(self, char): def mapchar(self, char):
""" """
Maps a char to an index. Maps a char to an index.
Takes a character either as ascii ordinal number or as a 1-length string. Takes a character either as ascii ordinal number or as a 1-length string.
Returns the index of that character in this texture font. Returns the index of that character in this texture font.
If the character is not valid in this texture font, the index number will If the character is not valid in this texture font, the index number will
instead reference the last character in the font, which is usually a instead reference the last character in the font, which is usually a
symbol for unknown characters (either totally blank or a box.) symbol for unknown characters (either totally blank or a box.)
""" """
if isinstance(char, basestring): if isinstance(char, str):
char = ord(char) char = ord(char)
if char >= self.start and char < self.end: if char >= self.start and char < self.end:
return char - self.start return char - self.start
else: else:
return self.end - self.start - 1 return self.end - self.start - 1
def get_coordinates(self, idx): def get_coordinates(self, idx):
""" """
Maps an index to their x, y coordinates in the texture. Maps an index to their x, y coordinates in the texture.
Invalid indexes will be mapped to the last character in the font. Invalid indexes will be mapped to the last character in the font.
""" """
if idx < 0 or idx >= (self.end - self.start): if idx < 0 or idx >= (self.end - self.start):
idx = (self.end - self.start - 1) idx = (self.end - self.start - 1)
xp1 = (idx % self.cols) * (self.charwidth + 1) xp1 = (idx % self.cols) * (self.charwidth + 1)
yp1 = (idx // self.cols) * (self.charheight + 1) yp1 = (idx // self.cols) * (self.charheight + 1)
if self.monospace: if self.monospace:
xp2 = xp1 + self.charwidth + 1 xp2 = xp1 + self.charwidth + 1
yp2 = yp1 + self.charheight + 1 yp2 = yp1 + self.charheight + 1
else: else:
xp2 = xp1 + self.charsizes[idx][0] + 1 xp2 = xp1 + self.charsizes[idx][0] + 1
yp2 = yp1 + self.charsizes[idx][1] + 1 yp2 = yp1 + self.charsizes[idx][1] + 1
xtc1 = (float(xp1) + 0.0) / self.texwidth xtc1 = (float(xp1) + 0.0) / self.texwidth
ytc1 = (float(yp1) + 0.0) / self.texheight ytc1 = (float(yp1) + 0.0) / self.texheight
xtc2 = (float(xp2) - 1.0) / self.texwidth xtc2 = (float(xp2) - 1.0) / self.texwidth
ytc2 = (float(yp2) - 1.0) / self.texheight ytc2 = (float(yp2) - 1.0) / self.texheight
return (xtc1, ytc1, xtc2, ytc2) return (xtc1, ytc1, xtc2, ytc2)
def load_definition(self, deffile): def load_definition(self, deffile):
""" """
Loads a texture font definition (.tfd). Loads a texture font definition (.tfd).
These simple datafiles are generated by fontmaker.py to provide character These simple datafiles are generated by fontmaker.py to provide character
spacing information about the associated texture font. spacing information about the associated texture font.
This should be called before load_texture. This should be called before load_texture.
""" """
dd = open(deffile, 'r') dd = open(deffile, 'r')
if dd.readline() != "fontheader\n": if dd.readline() != "fontheader\n":
raise ValueError('"%s" is not a font definition file' % (deffile,)) raise ValueError('"%s" is not a font definition file' % (deffile,))
def nextline(dd): def nextline(dd):
return [int(x) for x in dd.readline().rstrip().split(',')] return [int(x) for x in dd.readline().rstrip().split(',')]
self.start, self.end = nextline(dd) self.start, self.end = nextline(dd)
self.charwidth, self.charheight = nextline(dd) self.charwidth, self.charheight = nextline(dd)
self.cols, self.rows = nextline(dd) self.cols, self.rows = nextline(dd)
self.texwidth, self.texheight = nextline(dd) self.texwidth, self.texheight = nextline(dd)
self.charwidth -= 1 self.charwidth -= 1
self.charheight -= 1 self.charheight -= 1
assert dd.readline() == "charsizes\n" assert dd.readline() == "charsizes\n"
self.charsizes = [None] * (self.end - self.start) self.charsizes = [None] * (self.end - self.start)
monospace = True monospace = True
monoheight = True monoheight = True
for i in xrange(0, (self.end - self.start)): for i in range(0, (self.end - self.start)):
x, y = nextline(dd) x, y = nextline(dd)
if x != self.charwidth: if x != self.charwidth:
monospace = False monospace = False
if y != self.charheight: if y != self.charheight:
monoheight = False monoheight = False
self.charsizes[i] = (x, y) self.charsizes[i] = (x, y)
"not yet capable of dealing properly with fonts of variable height" "not yet capable of dealing properly with fonts of variable height"
assert monoheight assert monoheight
self.monospace = monospace self.monospace = monospace
def load_texture(self, texfile): def load_texture(self, texfile):
""" """
Loads a texture image (.png) Loads a texture image (.png)
The PNG image should contain 32-bit color with alpha channel. The image The PNG image should contain 32-bit color with alpha channel. The image
will be loaded as an OpenGL texture. will be loaded as an OpenGL texture.
This function must be called after load_definition. This function must be called after load_definition.
""" """
tf = TextureFile() tf = TextureFile()
tf.load(texfile) tf.load(texfile)
self.gltex = tf self.gltex = tf
assert self.gltex.w == self.texwidth assert self.gltex.w == self.texwidth
assert self.gltex.h == self.texheight assert self.gltex.h == self.texheight

View file

@ -1,276 +1,276 @@
import glob import glob
import os import os
try: try:
from xml.etree.cElementTree import parse from xml.etree.cElementTree import parse
except ImportError: except ImportError:
from elementtree.ElementTree import parse from elementtree.ElementTree import parse
import files import files
import shader import shader
from deprecate import deprecated from deprecate import deprecated
class MissingNode(object): class MissingNode(object):
def __init__(self): def __init__(self):
self.text = None self.text = None
def get(self, name): def get(self, name):
return None return None
class GameDataTagDef(object): class GameDataTagDef(object):
def __init__(self): def __init__(self):
self.multi = False self.multi = False
self.opt = False self.opt = False
self.type = None self.type = None
self.default = None self.default = None
class GameDataNode(object): class GameDataNode(object):
def __init__(self): def __init__(self):
self.dict = {} self.dict = {}
self.missing = False self.missing = False
def __setitem__(self, name, val): def __setitem__(self, name, val):
self.dict[name] = val self.dict[name] = val
def __getitem__(self, name): def __getitem__(self, name):
return self.dict[name]._value return self.dict[name]._value
def __getattr__(self, name): def __getattr__(self, name):
if name == 'dict': if name == 'dict':
raise AttributeError, name raise AttributeError(name)
try: try:
return self.dict[name] return self.dict[name]
except KeyError: except KeyError:
if name == '_value': if name == '_value':
return None return None
raise AttributeError, name raise AttributeError(name)
def __setattr__(self, name, val): def __setattr__(self, name, val):
if name == 'dict': if name == 'dict':
return object.__setattr__(self, name, val) return object.__setattr__(self, name, val)
try: try:
self.dict[name] = val self.dict[name] = val
except KeyError: except KeyError:
object.__setattr__(self, name, val) object.__setattr__(self, name, val)
def add_multi(self, key, value): def add_multi(self, key, value):
if not self.dict.has_key(key): if key not in self.dict:
self.dict[key] = [] self.dict[key] = []
self.dict[key].append(value) self.dict[key].append(value)
def add_single(self, key, value): def add_single(self, key, value):
self.dict[key] = value self.dict[key] = value
def __repr__(self): def __repr__(self):
return """<GameDataNode "%s">""" % (self.dict['_name'],) return """<GameDataNode "%s">""" % (self.dict['_name'],)
def has_key(self, key): def has_key(self, key):
return self.dict.has_key(key) return key in self.dict
class XMLGameDataReader(object): class XMLGameDataReader(object):
def __init__(self, bin, xml): def __init__(self, bin, xml):
self._bin = bin self._bin = bin
self._xml = xml self._xml = xml
self._tree = self.construct() self._tree = self.construct()
self._name = xml.tag self._name = xml.tag
def value_as_type(self, value, type): def value_as_type(self, value, type):
if type == 'string': if type == 'string':
return value return value
elif type == 'bool': elif type == 'bool':
if value and value[0].lower() in ('y', 't', '1'): if value and value[0].lower() in ('y', 't', '1'):
return True return True
else: else:
return False return False
elif type == 'int': elif type == 'int':
return int(value) return int(value)
elif type == 'float': elif type == 'float':
return float(value) return float(value)
elif type == 'csvarray': elif type == 'csvarray':
# csvarrays are always ints... # csvarrays are always ints...
return [int(x.strip()) for x in value.split(',')] return [int(x.strip()) for x in value.split(',')]
else: else:
raise TypeError, type raise TypeError(type)
def create_datatag_def(self, tag): def create_datatag_def(self, tag):
d = GameDataTagDef() d = GameDataTagDef()
if tag.get('multiple') != None and tag.get('multiple')[0].lower() == 'y': if tag.get('multiple') != None and tag.get('multiple')[0].lower() == 'y':
d.multi = True d.multi = True
if tag.get('optional') != None and tag.get('optional')[0].lower() == 'y': if tag.get('optional') != None and tag.get('optional')[0].lower() == 'y':
d.opt = True d.opt = True
if tag.get('data') != None: if tag.get('data') != None:
d.type = tag.get('data') d.type = tag.get('data')
if tag.get('default') != None: if tag.get('default') != None:
d.default = self.value_as_type(tag.get('default'), d.type) d.default = self.value_as_type(tag.get('default'), d.type)
d.opt = True d.opt = True
return d return d
def create_attribute_def(self, tag): def create_attribute_def(self, tag):
return self.create_datatag_def(tag) return self.create_datatag_def(tag)
def construct_node(self, bin, xml, allow_missing=False): def construct_node(self, bin, xml, allow_missing=False):
node = GameDataNode() node = GameDataNode()
if isinstance(xml, MissingNode): if isinstance(xml, MissingNode):
node.missing = True node.missing = True
value = None value = None
if bin.tag == 'datatag': if bin.tag == 'datatag':
df = self.create_datatag_def(bin) df = self.create_datatag_def(bin)
if df.type: if df.type:
value = xml.text value = xml.text
if df.type == 'bool': if df.type == 'bool':
# if tag exists, since it is a bool, that means it's True # if tag exists, since it is a bool, that means it's True
value = not node.missing value = not node.missing
elif (node.missing or value == None) and df.opt: elif (node.missing or value == None) and df.opt:
value = df.default value = df.default
elif (node.missing or value == None) and not allow_missing: elif (node.missing or value == None) and not allow_missing:
raise ValueError, "Missing value for mandatory tag %s" % (bin.get('name'),) raise ValueError("Missing value for mandatory tag %s" % (bin.get('name'),))
elif (node.missing or value == None): elif (node.missing or value == None):
value = None value = None
else: else:
value = self.value_as_type(value, df.type) value = self.value_as_type(value, df.type)
node['_value'] = value node['_value'] = value
elif bin.tag == 'attribute': elif bin.tag == 'attribute':
df = self.create_attribute_def(bin) df = self.create_attribute_def(bin)
if df.type: if df.type:
value = xml.get(bin.get('name')) value = xml.get(bin.get('name'))
if value == None and df.opt: if value == None and df.opt:
value = df.default value = df.default
elif not value and not allow_missing: elif not value and not allow_missing:
raise ValueError, "Missing value for mandatory tag %s" % (bin.get('name'),) raise ValueError("Missing value for mandatory tag %s" % (bin.get('name'),))
elif not value: elif not value:
value = None value = None
else: else:
value = self.value_as_type(value, df.type) value = self.value_as_type(value, df.type)
node['_value'] = value node['_value'] = value
node['_def'] = df node['_def'] = df
node['_name'] = bin.get('name') node['_name'] = bin.get('name')
return node return node
def construct_recurse(self, bin, xml, allow_missing=False): def construct_recurse(self, bin, xml, allow_missing=False):
xmldict = {} xmldict = {}
tagdict = {} tagdict = {}
attrdict = {} attrdict = {}
if bin.tag == 'xml_binary_packing': if bin.tag == 'xml_binary_packing':
node = GameDataNode() node = GameDataNode()
else: else:
node = self.construct_node(bin, xml, allow_missing) node = self.construct_node(bin, xml, allow_missing)
for child in bin.getchildren(): for child in bin.getchildren():
if child.tag == 'datatag': if child.tag == 'datatag':
tagdict[child.get('name')] = child tagdict[child.get('name')] = child
elif child.tag == 'attribute': elif child.tag == 'attribute':
attrdict[child.get('name')] = child attrdict[child.get('name')] = child
else: else:
raise ValueError raise ValueError
xmlattrdict = {} xmlattrdict = {}
xmltagdict = {} xmltagdict = {}
if not isinstance(xml, MissingNode): if not isinstance(xml, MissingNode):
for k, v in xml.items(): for k, v in list(xml.items()):
if not k in attrdict: if not k in attrdict:
raise ValueError, "Key %s not a valid attribute: %s" % (k, attrdict.keys()) raise ValueError("Key %s not a valid attribute: %s" % (k, list(attrdict.keys())))
continue continue
binchild = attrdict[k] binchild = attrdict[k]
xmlchild = xml xmlchild = xml
xmlattrdict[k] = 0 xmlattrdict[k] = 0
subnode = self.construct_node(binchild, xml, allow_missing) subnode = self.construct_node(binchild, xml, allow_missing)
if subnode._def.multi: if subnode._def.multi:
node.add_multi(k, subnode) node.add_multi(k, subnode)
else: else:
node.add_single(k, subnode) node.add_single(k, subnode)
for child in xml.getchildren(): for child in xml.getchildren():
if not child.tag in tagdict: if not child.tag in tagdict:
raise ValueError raise ValueError
continue continue
binchild = tagdict[child.tag] binchild = tagdict[child.tag]
xmlchild = child xmlchild = child
xmltagdict[child.tag] = 0 xmltagdict[child.tag] = 0
subnode = self.construct_recurse(binchild, xmlchild, allow_missing) subnode = self.construct_recurse(binchild, xmlchild, allow_missing)
if subnode._def.multi: if subnode._def.multi:
node.add_multi(child.tag, subnode) node.add_multi(child.tag, subnode)
else: else:
node.add_single(child.tag, subnode) node.add_single(child.tag, subnode)
missing = MissingNode() missing = MissingNode()
for k in tagdict.keys(): for k in list(tagdict.keys()):
if not k in xmltagdict: if not k in xmltagdict:
# Missing datatag # Missing datatag
subnode = self.construct_recurse(tagdict[k], missing, isinstance(xml, MissingNode)) subnode = self.construct_recurse(tagdict[k], missing, isinstance(xml, MissingNode))
if not subnode._def.multi: if not subnode._def.multi:
node.add_single(k, subnode) node.add_single(k, subnode)
else: else:
node.add_single(k, []) node.add_single(k, [])
for k in attrdict.keys(): for k in list(attrdict.keys()):
if not k in xmlattrdict: if not k in xmlattrdict:
# Missing attribute # Missing attribute
subnode = self.construct_node(attrdict[k], missing, isinstance(xml, MissingNode)) subnode = self.construct_node(attrdict[k], missing, isinstance(xml, MissingNode))
if not subnode._def.multi: if not subnode._def.multi:
node.add_single(k, subnode) node.add_single(k, subnode)
else: else:
node.add_single(k, []) node.add_single(k, [])
return node return node
def construct(self): def construct(self):
rootname = self._bin.get('name') rootname = self._bin.get('name')
assert rootname == self._xml.tag assert rootname == self._xml.tag
return self.construct_recurse(self._bin, self._xml) return self.construct_recurse(self._bin, self._xml)
def __getattr__(self, name): def __getattr__(self, name):
if name == 'tree': if name == 'tree':
raise AttributeError, name raise AttributeError(name)
return self._tree.__getattr__(name) return self._tree.__getattr__(name)
def __getitem__(self, name): def __getitem__(self, name):
return self._tree.__getitem__(name) return self._tree.__getitem__(name)
def load_xml_files(binfile, xmlfile): def load_xml_files(binfile, xmlfile):
bintree = parse(binfile).getroot() bintree = parse(binfile).getroot()
xmltree = parse(xmlfile).getroot() xmltree = parse(xmlfile).getroot()
return XMLGameDataReader(bintree, xmltree) return XMLGameDataReader(bintree, xmltree)
def get(dataname): def get(dataname):
if dataname in get.cache: if dataname in get.cache:
return get.cache[dataname] return get.cache[dataname]
bin = files.mgr.path("xml/def", "%s.xml" % (dataname,)) bin = files.mgr.path("xml/def", "%s.xml" % (dataname,))
xml = files.mgr.path("xml", "%s.xml" % (dataname,)) xml = files.mgr.path("xml", "%s.xml" % (dataname,))
if not os.path.exists(bin) or not os.path.exists(xml): if not os.path.exists(bin) or not os.path.exists(xml):
raise OSError("XML data file does not exist") raise OSError("XML data file does not exist")
dataobj = load_xml_files(bin, xml) dataobj = load_xml_files(bin, xml)
get.cache[dataname] = dataobj get.cache[dataname] = dataobj
return dataobj return dataobj
get.cache = {} get.cache = {}
@deprecated @deprecated
def load_gamedata(): def load_gamedata():
bins = glob.glob(files.mgr.path("xml/def", "*.xml")) bins = glob.glob(files.mgr.path("xml/def", "*.xml"))
xmls = glob.glob(files.mgr.path("xml", "*.xml")) xmls = glob.glob(files.mgr.path("xml", "*.xml"))
binfns = [os.path.split(x)[1] for x in bins] binfns = [os.path.split(x)[1] for x in bins]
xmlfns = [os.path.split(x)[1] for x in xmls] xmlfns = [os.path.split(x)[1] for x in xmls]
ffns = [] ffns = []
for bfn in binfns: for bfn in binfns:
if bfn in xmlfns: if bfn in xmlfns:
ffns.append(bfn) ffns.append(bfn)
dataobjs = {} dataobjs = {}
for fn in ffns: for fn in ffns:
binfile = files.mgr.path("xml/def", fn) binfile = files.mgr.path("xml/def", fn)
xmlfile = files.mgr.path("xml", fn) xmlfile = files.mgr.path("xml", fn)
dataobj = load_xml_files(binfile, xmlfile) dataobj = load_xml_files(binfile, xmlfile)
dataobjs[dataobj._name] = dataobj dataobjs[dataobj._name] = dataobj
shader.mgr.load_shaders(dataobjs['shaders']) shader.mgr.load_shaders(dataobjs['shaders'])
return dataobjs return dataobjs

View file

@ -65,7 +65,7 @@ def num_frames(delay, offset=0):
return int((g_timer - offset) / delay) - int((g_timer - g_elapsed - offset) / delay) return int((g_timer - offset) / delay) - int((g_timer - g_elapsed - offset) / delay)
def loop_frames(delay, offset=0): def loop_frames(delay, offset=0):
return xrange(num_frames(delay, offset)) return range(num_frames(delay, offset))
def get_timer(): def get_timer():
return g_timer return g_timer
@ -81,7 +81,7 @@ def average(d):
# pass # pass
v2 = float(d[0]) v2 = float(d[0])
for i in xrange(1, len(d)): for i in range(1, len(d)):
v1 = float(d[i]) v1 = float(d[i])
v2 = (smooth * v2) + ((1.0 - smooth) * v1) v2 = (smooth * v2) + ((1.0 - smooth) * v1)

View file

@ -221,6 +221,6 @@ def main():
glutMainLoop() glutMainLoop()
# Print message to console, and kick off the main to get it rolling. # Print message to console, and kick off the main to get it rolling.
print "Hit ESC key to quit." print("Hit ESC key to quit.")
main() main()

View file

@ -51,7 +51,7 @@ class Model_Manager(object):
self.textypes[enums.tt.specular] = TextureType(GL_TEXTURE2, GL_TEXTURE_2D) self.textypes[enums.tt.specular] = TextureType(GL_TEXTURE2, GL_TEXTURE_2D)
self.textypes[enums.tt.normal] = TextureType(GL_TEXTURE3, GL_TEXTURE_2D) self.textypes[enums.tt.normal] = TextureType(GL_TEXTURE3, GL_TEXTURE_2D)
for tt in self.textypes.values(): for tt in list(self.textypes.values()):
tt.initialize() tt.initialize()
@ -119,7 +119,7 @@ class Model_Manager(object):
fontname = os.path.splitext(os.path.split(fontfile)[1])[0] fontname = os.path.splitext(os.path.split(fontfile)[1])[0]
fontobj = fonts.TexFont.new(fontdir, fontname) fontobj = fonts.TexFont.new(fontdir, fontname)
self.fontlib[fontname] = fontobj self.fontlib[fontname] = fontobj
print "Loaded font %s" % (fontname,) print("Loaded font %s" % (fontname,))
def get_font(self, name): def get_font(self, name):
return self.fontlib[name] return self.fontlib[name]
@ -214,7 +214,7 @@ class TextureFile(object):
self.filename = filename self.filename = filename
img = files.mgr.png(filename) img = files.mgr.png(filename)
texid = glGenTextures(1) texid = glGenTextures(1)
print "Generated texture id %s from %s" % (texid, self.filename) print("Generated texture id %s from %s" % (texid, self.filename))
self.id = texid self.id = texid

View file

@ -67,7 +67,7 @@ class physics_manager(object):
if not other.massless: if not other.massless:
moved2 = obj.collide(other) moved2 = obj.collide(other)
print "Collision!" print("Collision!")
assert False assert False
return moved1 or moved2 return moved1 or moved2
@ -78,7 +78,7 @@ class physics_manager(object):
positions_adjusted = False positions_adjusted = False
iter += 1 iter += 1
pairmap = {} pairmap = {}
for key, cell in self.cells.items(): for key, cell in list(self.cells.items()):
for obj in cell: for obj in cell:
self._postcollide_single(obj, key, pairmap) self._postcollide_single(obj, key, pairmap)

View file

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

View file

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

View file

@ -19,7 +19,7 @@ try:
from PIL import PngImagePlugin from PIL import PngImagePlugin
except: except:
import PngImagePlugin import PngImagePlugin
from gameobj import * from .gameobj import *
from player import * from player import *
from particles import * from particles import *
from ai import * from ai import *

View file

View file

@ -1,12 +1,16 @@
#import roc
import os
import sys
sys.path.append(os.path.abspath('..'))
import roc import roc
import pygame import pygame
from OpenGL.GL import * from OpenGL.GL import *
from py3dutil import vect, quat from py3dutil import vect, quat
import models from roc import models
import fonts from roc import fonts
import gametimer from roc import gametimer
from platform import * from roc.platform import *
import pipeline from roc import pipeline
import time import time
import collections import collections
import random import random
@ -24,8 +28,8 @@ class partycle(object):
@classmethod @classmethod
def random(cls): def random(cls):
x, y = [((random.random() * 2.0) - 1.0) * 10.0 for _ in xrange(2)] x, y = [((random.random() * 2.0) - 1.0) * 10.0 for _ in range(2)]
xd, yd = [((random.random() * 2.0) - 1.0) * 4.0 for _ in xrange(2)] xd, yd = [((random.random() * 2.0) - 1.0) * 4.0 for _ in range(2)]
hue = random.random() hue = random.random()
m1 = 1.0 m1 = 1.0
h1 = hue * 6.0 h1 = hue * 6.0
@ -56,7 +60,7 @@ class partycle(object):
self.yd *= 0.985 self.yd *= 0.985
self.rot += self.rotd self.rot += self.rotd
class test_universe(roc.base_universe): class test_universe(roc.universe.base_universe):
def __init__(self): def __init__(self):
self.font = models.mgr.fontlib["micross20"] self.font = models.mgr.fontlib["micross20"]
self.text1 = models.TextModel("micross20", "#$% 0123 ,,,, Hello world!\xa8\xa8\xa8F", (1.0, 1.0, 0.5, 1.0)) self.text1 = models.TextModel("micross20", "#$% 0123 ,,,, Hello world!\xa8\xa8\xa8F", (1.0, 1.0, 0.5, 1.0))
@ -112,7 +116,7 @@ class test_universe(roc.base_universe):
self.text1.render() self.text1.render()
glPopMatrix() glPopMatrix()
for x in xrange(gametimer.num_frames(10)): for x in range(gametimer.num_frames(10)):
self.move = self.move + self.moveinc self.move = self.move + self.moveinc
if self.move > 300.0: if self.move > 300.0:
self.moveinc = -1.0 self.moveinc = -1.0
@ -139,7 +143,7 @@ class test_universe(roc.base_universe):
for x in xrange(gametimer.num_frames(1000)): for x in xrange(gametimer.num_frames(1000)):
self.string = self.string + str(len(self.string) % 10) self.string = self.string + str(len(self.string) % 10)
""" """
for x in xrange(gametimer.num_frames(100)): for x in range(gametimer.num_frames(100)):
self.string = str(round(gametimer.get_fps(), 1)) self.string = str(round(gametimer.get_fps(), 1))
#print len(gametimer.g_framelist) #print len(gametimer.g_framelist)
@ -148,20 +152,21 @@ class test_universe(roc.base_universe):
glPopMatrix() glPopMatrix()
# texture test # texture test
mdl2 = models.mgr.create("m_dralthi") mdl2 = models.mgr.create("m_particle")
mdl2.layers[0].color = None mdl2.layers[0].color = None
#print mdl2.layers[0].mesh #print mdl2.layers[0].mesh
for party in self.particles: for party in self.particles:
glPushMatrix() glPushMatrix()
glTranslate(600.0 + party.x, 350.0 + party.y, 6.0) glTranslate(600.0 + party.x, 350.0 + party.y, 6.0)
glRotate(party.rot, 0.0, 0.0, 1.0) glRotate(party.rot, 0.0, 0.0, 1.0)
#glColor4f(*party.color) glColor4f(*party.color)
mdl2.render() mdl2.render()
glPopMatrix() glPopMatrix()
roc.video.width = 1024
roc.video.height = 768
roc.init2d() roc.init2d()
roc.set_universe(test_universe()) roc.set_universe(test_universe())
roc.main() roc.main()

439
shader.py
View file

@ -1,219 +1,220 @@
import ctypes import ctypes
from OpenGL.GL import * from OpenGL.GL import *
try: try:
import OpenGL.raw.GL import OpenGL.raw.GL
raw = OpenGL.raw raw = OpenGL.raw
except ImportError: except ImportError:
pass pass
from OpenGL.arrays import GLintArray, GLcharArray, GLsizeiArray, GLcharARBArray from OpenGL.arrays import GLintArray, GLcharArray, GLsizeiArray, GLcharARBArray
import sys import sys
import gamedata import gamedata
class shader_manager(object): class shader_manager(object):
def __init__(self): def __init__(self):
# Fixed Pipeline is always zero # Fixed Pipeline is always zero
self.shaderlib = {'ffp': 0} self.shaderlib = {'ffp': 0}
self.shaderprograms = {} self.shaderprograms = {}
self.initshadersources = [] self.initshadersources = []
self.initmode = 'auto' self.initmode = 'auto'
self.initialized = False self.initialized = False
def use_gl2_shaders(self): def use_gl2_shaders(self):
self.funcs = { self.funcs = {
'create': glCreateShader, 'create': glCreateShader,
'source': glShaderSource, 'source': glShaderSource,
'compile': glCompileShader, 'compile': glCompileShader,
'program': glCreateProgram, 'program': glCreateProgram,
'attach': glAttachShader, 'attach': glAttachShader,
'link': glLinkProgram, 'link': glLinkProgram,
'use': glUseProgram, 'use': glUseProgram,
'param': glGetShaderiv, 'param': glGetShaderiv,
'uniform': glUniform4f, 'uniform': glUniform4f,
'arraytype': GLcharArray 'arraytype': GLcharArray
} }
def use_raw_shaders(self): def use_raw_shaders(self):
self.funcs = { self.funcs = {
'create': raw.GL.glCreateShader, 'create': raw.GL.glCreateShader,
'source': raw.GL.glShaderSource, 'source': raw.GL.glShaderSource,
'compile': raw.GL.glCompileShader, 'compile': raw.GL.glCompileShader,
'program': raw.GL.glCreateProgram, 'program': raw.GL.glCreateProgram,
'attach': raw.GL.glAttachShader, 'attach': raw.GL.glAttachShader,
'link': raw.GL.glLinkProgram, 'link': raw.GL.glLinkProgram,
'use': raw.GL.glUseProgram, 'use': raw.GL.glUseProgram,
'param': raw.GL.glGetShaderiv 'param': raw.GL.glGetShaderiv
} }
def use_arb_shaders(self): def use_arb_shaders(self):
from OpenGL.GL.ARB import shader_objects from OpenGL.GL.ARB import shader_objects
if not shader_objects.glInitShaderObjectsARB(): if not shader_objects.glInitShaderObjectsARB():
raise RuntimeError("ARB Shaders failed to initialize") raise RuntimeError("ARB Shaders failed to initialize")
self.funcs = { self.funcs = {
'create': shader_objects.glCreateShaderObjectARB, 'create': shader_objects.glCreateShaderObjectARB,
'source': shader_objects.glShaderSourceARB, 'source': shader_objects.glShaderSourceARB,
'compile': shader_objects.glCompileShaderARB, 'compile': shader_objects.glCompileShaderARB,
'program': shader_objects.glCreateProgramObjectARB, 'program': shader_objects.glCreateProgramObjectARB,
'attach': shader_objects.glAttachObjectARB, 'attach': shader_objects.glAttachObjectARB,
'link': shader_objects.glLinkProgramARB, 'link': shader_objects.glLinkProgramARB,
'use': shader_objects.glUseProgramObjectARB, 'use': shader_objects.glUseProgramObjectARB,
'uniform': shader_objects.glUniform4fARB, 'uniform': shader_objects.glUniform4fARB,
'param': shader_objects.glGetObjectParameterivARB 'param': shader_objects.glGetObjectParameterivARB
} }
def use_arb_new_shaders(self): def use_arb_new_shaders(self):
from OpenGL.GL.ARB import shader_objects_new from OpenGL.GL.ARB import shader_objects_new
shader_objects = shader_objects_new shader_objects = shader_objects_new
if not shader_objects.glInitShaderObjectsARB(): if not shader_objects.glInitShaderObjectsARB():
raise RuntimeError("ARB New Shaders failed to initialize") raise RuntimeError("ARB New Shaders failed to initialize")
self.funcs = { self.funcs = {
'create': shader_objects.glCreateShaderObjectARB, 'create': shader_objects.glCreateShaderObjectARB,
'source': shader_objects.base_glShaderSourceARB, 'source': shader_objects.base_glShaderSourceARB,
'compile': shader_objects.glCompileShaderARB, 'compile': shader_objects.glCompileShaderARB,
'program': shader_objects.glCreateProgramObjectARB, 'program': shader_objects.glCreateProgramObjectARB,
'attach': shader_objects.glAttachObjectARB, 'attach': shader_objects.glAttachObjectARB,
'link': shader_objects.glLinkProgramARB, 'link': shader_objects.glLinkProgramARB,
'use': shader_objects.glUseProgramObjectARB, 'use': shader_objects.glUseProgramObjectARB,
'param': shader_objects.glGetObjectParameterivARB, 'param': shader_objects.glGetObjectParameterivARB,
'arraytype': GLcharARBArray 'arraytype': GLcharARBArray
} }
def init_gl(self): def init_gl(self):
if self.initialized: if self.initialized:
return return
self.initialized = True self.initialized = True
import files import files
#print glGetString(GL_EXTENSIONS) #print glGetString(GL_EXTENSIONS)
if True: if True:
self.use_gl2_shaders() self.use_gl2_shaders()
else: else:
self.use_arb_shaders() self.use_arb_shaders()
#self.use_gl2_shaders() #self.use_gl2_shaders()
for name, vertex, fragment in self.initshadersources: for name, vertex, fragment in self.initshadersources:
vf = files.mgr.open('shaders/%s.vert' % (vertex,)) vf = files.mgr.open('shaders/%s.vert' % (vertex,))
ff = files.mgr.open('shaders/%s.frag' % (fragment,)) ff = files.mgr.open('shaders/%s.frag' % (fragment,))
vs = self.funcs['create'](GL_VERTEX_SHADER) vs = self.funcs['create'](GL_VERTEX_SHADER)
fs = self.funcs['create'](GL_FRAGMENT_SHADER) fs = self.funcs['create'](GL_FRAGMENT_SHADER)
self.funcs['source'](vs, vf.read()) self.funcs['source'](vs, vf.read())
self.funcs['source'](fs, ff.read()) self.funcs['source'](fs, ff.read())
rv = self.funcs['compile'](vs) rv = self.funcs['compile'](vs)
rv = self.funcs['compile'](fs) rv = self.funcs['compile'](fs)
sp = self.funcs['program']() sp = self.funcs['program']()
self.funcs['attach'](sp, vs) self.funcs['attach'](sp, vs)
self.funcs['attach'](sp, fs) self.funcs['attach'](sp, fs)
rv = self.funcs['link'](sp) rv = self.funcs['link'](sp)
self.shaderlib[name] = sp self.shaderlib[name] = sp
self.shaderprograms[name] = (vs, fs) self.shaderprograms[name] = (vs, fs)
def detect(self): def detect(self):
shaderlist = [ shaderlist = [
('GL2', self.use_gl2_shaders), ('GL2', self.use_gl2_shaders),
('ARB', self.use_arb_shaders), ('ARB', self.use_arb_shaders),
('ARBNEW', self.use_arb_new_shaders), ('ARBNEW', self.use_arb_new_shaders),
('RAW', self.use_raw_shaders) ('RAW', self.use_raw_shaders)
] ]
workable_shaders = [] workable_shaders = []
for shadername, shaderinit in shaderlist: for shadername, shaderinit in shaderlist:
try: try:
shaderinit() shaderinit()
except: except:
print "Shader type %s failed to initialize" % (shadername,) print("Shader type %s failed to initialize" % (shadername,))
continue continue
try: try:
self.select("ffp") self.select("ffp")
except: except:
print "Shader type %s failed to access the fixed function pipeline" % (shadername,) print("Shader type %s failed to access the fixed function pipeline" % (shadername,))
continue continue
try: try:
self.select("standard") self.select("standard")
except: except:
print "Shader type %s failed to load the \"standard\" vertex and fragment shaders" % (shadername,) print("Shader type %s failed to load the \"standard\" vertex and fragment shaders" % (shadername,))
continue continue
workable_shaders.append(shadername) workable_shaders.append(shadername)
return workable_shaders return workable_shaders
def autoinit(self): def autoinit(self):
shadertypes = self.detect() shadertypes = self.detect()
if not shadertypes: if not shadertypes:
raise ValueError, "No working shaders detected" raise ValueError("No working shaders detected")
self.init_shadertype(shadertypes[0]) self.init_shadertype(shadertypes[0])
def init_shadertype(self, shadertype): def init_shadertype(self, shadertype):
if shadertype == "GL2": if shadertype == "GL2":
self.use_gl2_shaders() self.use_gl2_shaders()
elif shadertype == "ARB": elif shadertype == "ARB":
self.use_arb_shaders() self.use_arb_shaders()
elif shadertype == "ARBNEW": elif shadertype == "ARBNEW":
self.use_arb_new_shaders() self.use_arb_new_shaders()
elif shadertype == "RAW": elif shadertype == "RAW":
self.use_raw_shaders() self.use_raw_shaders()
def select(self, shader): def select(self, shader):
if self.initshadersources: if self.initshadersources:
self.init_gl() self.init_gl()
self.initshadersources = [] self.initshadersources = []
if not shader in self.shaderlib: if not shader in self.shaderlib:
self.funcs['use'](0) self.funcs['use'](0)
return True return True
try: try:
self.funcs['use'](self.shaderlib[shader]) self.funcs['use'](self.shaderlib[shader])
except GLError: except GLError:
p = self.shaderlib[shader] p = self.shaderlib[shader]
v, f = self.shaderprograms[shader] v, f = self.shaderprograms[shader]
print "Shader failed to execute: %s" % (shader,) print("Shader failed to execute: %s" % (shader,))
print "Vertex shader log:" print("Vertex shader log:")
lenarr = GLintArray.zeros(1) lenarr = GLintArray.zeros(1)
glGetShaderiv(v, GL_INFO_LOG_LENGTH, lenarr) glGetShaderiv(v, GL_INFO_LOG_LENGTH, lenarr)
infoarr = GLcharArray.zeros(lenarr[0]) infoarr = GLcharArray.zeros(lenarr[0])
outsize = GLsizeiArray.zeros(1) outsize = GLsizeiArray.zeros(1)
glGetShaderInfoLog(v, lenarr[0], outsize, infoarr) glGetShaderInfoLog(v, lenarr[0], outsize, infoarr)
print ''.join([chr(x) for x in infoarr[:outsize[0]]]) print(''.join([chr(x) for x in infoarr[:outsize[0]]]))
print "Fragment shader log:" print("Fragment shader log:")
lenarr = GLintArray.zeros(1) lenarr = GLintArray.zeros(1)
glGetShaderiv(f, GL_INFO_LOG_LENGTH, lenarr) glGetShaderiv(f, GL_INFO_LOG_LENGTH, lenarr)
infoarr = GLcharArray.zeros(lenarr[0]) infoarr = GLcharArray.zeros(lenarr[0])
outsize = GLsizeiArray.zeros(1) outsize = GLsizeiArray.zeros(1)
glGetShaderInfoLog(f, lenarr[0], outsize, infoarr) glGetShaderInfoLog(f, lenarr[0], outsize, infoarr)
print ''.join([chr(x) for x in infoarr[:outsize[0]]]) print(''.join([chr(x) for x in infoarr[:outsize[0]]]))
print "Program info log:" print("Program info log:")
lenarr = GLintArray.zeros(1) lenarr = GLintArray.zeros(1)
glGetProgramiv(p, GL_INFO_LOG_LENGTH, lenarr) glGetProgramiv(p, GL_INFO_LOG_LENGTH, lenarr)
infoarr = GLcharArray.zeros(lenarr[0]) infoarr = GLcharArray.zeros(lenarr[0])
outsize = GLsizeiArray.zeros(1) outsize = GLsizeiArray.zeros(1)
glGetProgramInfoLog(p, lenarr[0], outsize, infoarr) glGetProgramInfoLog(p, lenarr[0], outsize, infoarr)
print ''.join([chr(x) for x in infoarr[:outsize[0]]]) print(''.join([chr(x) for x in infoarr[:outsize[0]]]))
glDeleteShader(v) glDeleteShader(v)
glDeleteShader(f) glDeleteShader(f)
glDeleteProgram(p) glDeleteProgram(p)
del self.shaderprograms[shader] del self.shaderprograms[shader]
del self.shaderlib[shader] del self.shaderlib[shader]
glUseProgram(0) glUseProgram(0)
return False return False
return True return True
def load(self): def load(self):
shader_data = gamedata.get('shaders') shader_data = gamedata.get('shaders')
self.load_shaders(shader_data) self.load_shaders(shader_data)
def load_shaders(self, shaders): def load_shaders(self, shaders):
for shader in shaders.shader: for shader in shaders.shader:
self.initshadersources.append((shader['id'], shader['vertex'], shader['fragment'])) self.initshadersources.append((shader['id'], shader['vertex'], shader['fragment']))
def init(): def init():
global mgr global mgr
mgr = shader_manager() mgr = shader_manager()
print("initiated shader mgr: %s" % (mgr,))
mgr = None
mgr = None

View file

@ -1,93 +1,93 @@
import os import os
import pygame import pygame
import sys import sys
import math import math
pygame.init() pygame.init()
pygame.font.init() pygame.font.init()
assert pygame.font.get_init() assert pygame.font.get_init()
fmod = pygame.font fmod = pygame.font
Font = pygame.font.Font Font = pygame.font.Font
fontname, fontsize = sys.argv[1:] fontname, fontsize = sys.argv[1:]
if '/' in fontname: if '/' in fontname:
ffile = fontname ffile = fontname
fontname = os.path.splitext(os.path.split(fontname)[1])[0].lower() fontname = os.path.splitext(os.path.split(fontname)[1])[0].lower()
else: else:
ffile = fmod.match_font(fontname) ffile = fmod.match_font(fontname)
if not ffile: if not ffile:
print "Unable to find font... here is a list of fonts:\n" print("Unable to find font... here is a list of fonts:\n")
for x in sorted(fmod.get_fonts()): for x in sorted(fmod.get_fonts()):
print x print(x)
sys.exit(0) sys.exit(0)
fontsize = int(fontsize) fontsize = int(fontsize)
fontname = "%s%s" % (fontname, fontsize) fontname = "%s%s" % (fontname, fontsize)
f = Font(ffile, fontsize) f = Font(ffile, fontsize)
start = 32 start = 32
end = 128 end = 128
images = [] images = []
imsizes = [] imsizes = []
maxwidth = 0 maxwidth = 0
maxheight = 0 maxheight = 0
for char in xrange(start, end): for char in range(start, end):
letter = chr(char) letter = chr(char)
sz = f.size(letter) sz = f.size(letter)
maxwidth = max(maxwidth, sz[0]) maxwidth = max(maxwidth, sz[0])
maxheight = max(maxheight, sz[1]) maxheight = max(maxheight, sz[1])
im = f.render(letter, True, (255, 255, 255)) im = f.render(letter, True, (255, 255, 255))
images.append(im) images.append(im)
imsizes.append(sz) imsizes.append(sz)
print len(images) print(len(images))
maxwidth += 1 maxwidth += 1
maxheight += 1 maxheight += 1
ratio = float(maxwidth) / float(maxheight) ratio = float(maxwidth) / float(maxheight)
print "Ratio %s" % (ratio,) print("Ratio %s" % (ratio,))
reqwidth = maxwidth * len(images) reqwidth = maxwidth * len(images)
ratioheight = int(math.ceil(math.sqrt(len(images) * ratio))) ratioheight = int(math.ceil(math.sqrt(len(images) * ratio)))
ratiowidth = int(math.ceil(len(images) / math.sqrt(len(images) * ratio))) ratiowidth = int(math.ceil(len(images) / math.sqrt(len(images) * ratio)))
print [ratioheight, ratiowidth] print([ratioheight, ratiowidth])
reqwidth = maxwidth * ratiowidth reqwidth = maxwidth * ratiowidth
reqheight = maxheight * ratioheight reqheight = maxheight * ratioheight
print [reqheight, reqwidth] print([reqheight, reqwidth])
texsize = 16 texsize = 16
while texsize < reqheight or texsize < reqwidth: while texsize < reqheight or texsize < reqwidth:
texsize *= 2 texsize *= 2
print texsize print(texsize)
tex = pygame.Surface((texsize, texsize), pygame.SRCALPHA, 32) tex = pygame.Surface((texsize, texsize), pygame.SRCALPHA, 32)
tex.fill((0, 0, 0, 0)) tex.fill((0, 0, 0, 0))
for i, im in enumerate(images): for i, im in enumerate(images):
tex.blit(im, (((i % ratiowidth) * maxwidth), ((i // ratiowidth) * maxheight))) tex.blit(im, (((i % ratiowidth) * maxwidth), ((i // ratiowidth) * maxheight)))
pygame.image.save(tex, "%s.png" % (fontname,)) pygame.image.save(tex, "%s.png" % (fontname,))
datf = open("%s.tfd" % (fontname,), "w") datf = open("%s.tfd" % (fontname,), "w")
datf.write("fontheader\n") datf.write("fontheader\n")
datf.write("%s,%s\n" % (start, end)) datf.write("%s,%s\n" % (start, end))
datf.write("%s,%s\n" % (maxwidth, maxheight)) datf.write("%s,%s\n" % (maxwidth, maxheight))
datf.write("%s,%s\n" % (ratiowidth, ratioheight)) datf.write("%s,%s\n" % (ratiowidth, ratioheight))
datf.write("%s,%s\n" % (texsize, texsize)) datf.write("%s,%s\n" % (texsize, texsize))
datf.write("charsizes\n") datf.write("charsizes\n")
for i, sz in enumerate(imsizes): for i, sz in enumerate(imsizes):
datf.write("%s,%s\n" % (sz[0], sz[1])) datf.write("%s,%s\n" % (sz[0], sz[1]))
datf.close() datf.close()

View file

@ -18,7 +18,8 @@ def enable_vsync():
def skipping_next_frame(): def skipping_next_frame():
return False return False
def set_res((width_in, height_in)): def set_res(xxx_todo_changeme):
(width_in, height_in) = xxx_todo_changeme
global height, width, hwnd global height, width, hwnd
load_window_pos() load_window_pos()
@ -85,7 +86,7 @@ def init2d():
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.3, 0.3, 0.3, 1.0)) glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.3, 0.3, 0.3, 1.0))
shader.mgr.load() shader.mgr.load()
shader.mgr.init_gl() shader.mgr.init_gl()
shader.mgr.select("ffp") shader.mgr.select("ffp")