from pgu import tilevid
import pygame

class Isovid(tilevid.Tilevid):
	def paint(self,screen):
		sw,sh = screen.get_width(),screen.get_height()
		
		tlayer = self.tlayer
		blayer = self.blayer
		zlayer = self.zlayer
		
		tw = 32
		th = 32
		
		todo = [[] for xy in xrange(0,1600,8)]
		div = 8
		#for v in xrange(0,480,8): print v

		for s in self.sprites:
			#HACK: gee, why are these numbers right?
			x,y = sw/2,0
			x += s.rect.centerx/2
			y += s.rect.centerx/4
			x -= s.rect.bottom/2
			y += s.rect.bottom/4
			
			by = y
			
			#HACK: ugh
			x -= s.shape.x-tw/4 #???
			y -= s.shape.y+th/2

			#screen.blit(s.image,(x,y))
			c = None
			
			img = s.image
			x,y,w,h = x,y,img.get_width(),img.get_height()
			r = pygame.Rect(x+24,y+8,w-48,h-16)
			s.crect = r

			#if s == self.control: c = s.crect
				
			tx,ty = s.rect.centerx/32,s.rect.centery/32
			
			#todo[(by-9)/div].append((s.image,(x,y),c))
			s.irect.x,s.irect.y = x,y + zlayer[ty][tx]
			todo[(by-1)/div].append((s.image,s.irect))
			
			#s.irect = pygame.Rect(x,y,s.irect.w,s.irect.h)
			


		#for n in range(0,480):
		#	print n,len(todo[n])
			
		#screen.fill((0,0,0))
		#import pygame
		#pygame.display.flip()
		
		#make for scrolling around the player location nice...
		#if not hasattr(self,'adj'): self.adj = pygame.Rect(0,0,0,0)
		
		self.view.w,self.view.h = sw,sh
		view = self.view
		
		pr = self.player.irect
		
		moveby = 12
		if not hasattr(self,'peeking'): 
			self.peeking = 0
			moveby = 16384 #big number, first frame, probably...
		if self.peeking == 0:
			self.view.left = min(self.view.left,max(self.view.left-moveby,pr.left-200))
			self.view.top = min(self.view.top,max(self.view.top-moveby,pr.top-150))
			self.view.right = max(self.view.right,min(self.view.right+moveby,pr.right+200))
			self.view.bottom = max(self.view.bottom,min(self.view.bottom+moveby,pr.bottom+150))
		
		#nw = pygame.Rect(pr)
		#nw.clamp_ip(view)
		#adj = self.adj = pygame.Rect(nw.x-pr.x,nw.y-pr.y,0,0) 		
		
		adj = self.adj = pygame.Rect(-self.view.x,-self.view.y,0,0)
				
		w,h = len(tlayer[0]),len(tlayer)
		tiles = self.tiles
		
		
		for ty in xrange(0,h):
			x,y = sw/2 - ty*tw/2,0 + ty*th/4
			bl = blayer[ty]
			tl = tlayer[ty]
			zl = zlayer[ty]
			for tx in xrange(0,w):
				n = bl[tx]
				if n != 0:
					t = tiles[n]
					if t != None and t.image != None:
						ww,hh = 32,64
						#ww = t.image.get_width()
						#hh = t.image.get_height()
						irect = pygame.Rect(x,y+zl[tx],ww,hh)
						if irect.colliderect(view):
							todo[y/div].append((t.image,irect))
				
				n = tl[tx]
				if n != 0:
					t = tiles[n]
					if t != None and t.image != None:
						#ww = t.image.get_width()
						ww = 32
						#hh = t.image.get_height()
						hh = t.h
						irect = pygame.Rect(x,y-hh+16+zl[tx],ww,hh)
						if irect.colliderect(view):
							todo[y/div].append((t.image,irect))
				
				x += tw/2
				y += th/4
				
		
		"""
		for s in self.sprites:
			s.irect.x += adj.x
			s.irect.y += adj.y
		"""
		
		#blits = 0
		for ys in todo:
			for img,irect in ys:
				#if pos[0]+adj.x+img.get_width() < 0 or pos[1]+adj.y+img.get_height() < 0 or pos[0]+adj.x >= sw or pos[1]+adj.y >= sh: continue
			
				screen.blit(img,(irect.x+adj.x,irect.y+adj.y))
				#blits+=1
				#if c != None:
				#	pygame.draw.rect(screen,(0,255,0),c,1)
		#print blits
		
		
		
	def pos2xy(self,screen,pos):
		tx,ty=pos
		sw,sh = screen.get_width(),screen.get_height()
		
		tw = 32
		th = 32
	
		x,y = sw/2 - ty*tw/2,0 + ty*th/4
		x += tx*tw/2
		y += tx*th/4
		adj = self.adj
		return x+adj.x,y+adj.y
		
		

	def tga_load_level(self,fname,bg=0):
		"""Load a TGA level.  
		
		g		-- a Tilevid instance
		fname	-- tga image to load
		bg		-- set to 1 if you wish to load the background layer
		"""
		
		sp = 5
		db = 0x1e
		
		img = pygame.image.load(fname)
		w,h = img.get_width(),img.get_height()
		self.resize((w+sp*2,h+sp*2),bg)
		for y in range(0,h+sp*2):
			for x in range(0,w+sp*2):
				self.blayer[y][x] = db
				
		for y in range(0,h):
			for x in range(0,w):
				t,b,c,_a = img.get_at((x,y))
				self.tlayer[y+sp][x+sp] = t
				#if bg: self.blayer[y][x] = b
				if b != 0: self.blayer[y+sp][x+sp] = b
				self.clayer[y+sp][x+sp] = c
