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