Example #1
0
	def populate_from_dmd_file(self, f):
		f.seek(0, os.SEEK_END) # Go to the end of the file to get its length
		file_length = f.tell()
		## MJO: Don't just skip the header!  Check it.
		
		f.seek(0) # Skip over the 4 byte DMD header.
		dmd_version = struct.unpack("I", f.read(4))[0]
		dmd_style = 0 # old
		if(dmd_version == 0x00646D64):
			# print("old dmd style")
			pass
		elif(dmd_version == 0x00DEFACE):
			# print("full color dmd style")
			dmd_style = 1

		frame_count = struct.unpack("I", f.read(4))[0]
		self.width = struct.unpack("I", f.read(4))[0]
		self.height = struct.unpack("I", f.read(4))[0]
		if(dmd_style==0):
			if file_length != 16 + self.width * self.height * frame_count:
				logging.getLogger('game.dmdcache').warning(f)
				logging.getLogger('game.dmdcache').warning("expected size = {%d} got {%d}", (16 + self.width * self.height * frame_count), (file_length))
				raise ValueError, "File size inconsistent with original DMD format header information.  Old or incompatible file format?"
		elif(dmd_style==1):
			if file_length != 16 + self.width * self.height * frame_count * 3:
				logging.getLogger('game.dmdcache').warning(f)
				raise ValueError, "File size inconsistent with true-color DMD format header information. Old or incompatible file format?"

		for frame_index in range(frame_count):
			new_frame = Frame(self.width, self.height)
			if(dmd_style==0):
				str_frame = f.read(self.width * self.height)
				new_frame.build_surface_from_8bit_dmd_string(str_frame)
			elif(dmd_style==1):
				str_frame = f.read(self.width * self.height * 3)
				surface = pygame.image.fromstring(str_frame, (self.width, self.height), 'RGB')
				new_frame.set_surface(surface)
			self.frames.append(new_frame)