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