]> icculus.org git repositories - theoddone33/hheretic.git/blob - pyhexen/vid.py
pyhexen progress
[theoddone33/hheretic.git] / pyhexen / vid.py
1 import pygame
2 import struct
3 import string
4
5 class VidException(Exception):
6         pass
7         
8 class Video:
9         def __init__(self):
10                 self.palette = []
11                 
12         # Stuff from I_* in the C code.
13         def SetPalette(self, data):
14                 self.palette = []
15                 for i in range (0, 768, 3):
16                         rgb = [ord(x) for x in data[i:i+3]]
17                         self.palette.append(rgb)
18                 self.screen.set_palette(self.palette)
19                 
20         def WaitVBL (self):
21                 pygame.display.flip()
22                 
23         # Stuff from V_* in the C code.
24         def __blitRaw (self, data, w, h):
25                 screen = pygame.surfarray.pixels2d (self.screen)
26                 for y in range(0,h):
27                         for x in range (0, w):
28                                 screen[x,y] = ord (data[y * w + x])
29                 
30         def __blitPatch(self, data, x, y, trans = lambda x: x):
31                 (width, height) = struct.unpack_from ('<hh', data, 0)
32                 (left_offset, top_offset) = struct.unpack_from('<hh', data, 4)
33                 column_offsets =  struct.unpack_from('<8i', data, 8)
34                 
35                 # Adjust pos
36                 x = x - left_offset
37                 y = y - top_offset
38
39                 # Crazy column/patch thing to buffer
40                 screen = pygame.surfarray.pixels2d (self.screen)
41                 for x_ofs in range (0, width):
42                         col_ofs = struct.unpack_from ('<i', data, 8 + 4 * x_ofs)[0]
43                         topdelta = ord (data[col_ofs + 0])
44                         # Step through the posts in a column
45                         while topdelta != 255:
46                                 length = ord (data[col_ofs + 1])
47                                 source_ofs = col_ofs + 3
48                                 for y_ofs in range (0, length):
49                                         screen[x + x_ofs, topdelta + y_ofs] = trans(ord (data[source_ofs + y_ofs]))
50                                 col_ofs = col_ofs + length + 4
51                                 topdelta = ord (data[col_ofs + 0])
52                                 
53         def Init(self):
54                 pygame.init()
55                 self.window = pygame.display.set_mode ((320,200), 0, 8)
56                 pygame.display.set_caption('PyHexen')
57                 self.screen = pygame.display.get_surface()
58                 
59         def DrawRawScreen(self, lump):
60                 # Horribly inefficent for now.
61                 self.__blitRaw (lump, 320, 200)
62                                 
63         def DrawPatch(self, x, y, lump):
64                 # Horribly inefficent for now.
65                 self.__blitPatch (lump, x, y)
66                                 
67         def DrawFuzzPatch(self, x, y, lump):
68                 # Stub
69                 self.DrawPatch(x,y,lump)
70                 
71         def DrawShadowedPatch(self,x,y,lump):
72                 # Stub
73                 self.DrawPatch(x,y,lump)
74