c2ephp

sprites/SpriteFrame.php

Go to the documentation of this file.
00001 <?php
00003 
00007 abstract class SpriteFrame {
00008 
00010     
00011     private $decoded;
00012     protected $gdImage;
00013     private $width;
00014     private $height;
00015 
00017 
00019     
00021 
00025     public function SpriteFrame($width,$height,$decoded=false) {
00026         if($width == 0) {
00027             throw new Exception('Zero width');
00028         } else if($height == 0) {
00029             throw new Exception('Zero height');
00030         }
00031         $this->width = $width;
00032         $this->height = $height;
00033         $this->decoded = $decoded;
00034     }
00035 
00036     protected function HasBeenDecoded() {
00037         return $this->decoded;  
00038     }
00039 
00041 
00044 
00047     public function GetGDImage() {
00048         $this->EnsureDecoded();
00049         return $this->gdImage;
00050     }
00051 
00053     public function GetWidth() {
00054         return $this->width;
00055     }
00057     public function GetHeight() {
00058         return $this->height;
00059     }
00060 
00062 
00076     public function GetPixel($x,$y) {
00077         $this->EnsureDecoded();
00078         $colori = imagecolorat($this->gdImage, $x, $y);
00079         $color = imagecolorsforindex($this->gdImage, $colori);
00080         return $color;
00081     }
00082 
00084 
00091     public function SetPixel($x,$y,$r,$g,$b) {
00092         $this->EnsureDecoded();
00093         imagesetpixel($this->gdImage, $x, $y, imagecolorallocate($this->gdImage, $r, $g, $b));
00094     }
00095 
00097     
00099 
00103     protected function EnsureDecoded() {
00104         if(!$this->decoded)
00105             $this->Decode();
00106 
00107         $this->decoded = true;
00108     }
00109 
00111 
00113 
00115     protected abstract function Decode();
00116 
00118     public abstract function Encode();
00120 
00122 
00134     public function ToSpriteFrame($type) {
00135         $this->EnsureDecoded();
00136         if(substr(get_class($this),0,3) == $type && substr(get_class($this),3) == 'Frame') {
00137             return $this;
00138         }
00139         switch($type) {
00140         case 'C16':
00141             return new C16Frame($this->GetGDImage());
00142         case 'S16':
00143             return new S16Frame($this->GetGDImage());
00144         case 'SPR':
00145             return new SPRFrame($this->GetGDImage());
00146         default:
00147             throw new Exception('Invalid sprite type '.$type.'.');
00148         }
00149     }
00150 
00152 
00155     public function ToPNG() {
00156         $this->EnsureDecoded();
00157         ob_start();
00158         imagepng($this->GetGDImage());
00159         $data = ob_get_contents();
00160         ob_end_clean();
00161         return $data;
00162     }
00163 }
00164 ?>
 All Classes Files Functions Variables Enumerations