c2ephp

sprites/S16File.php

Go to the documentation of this file.
00001 <?php
00002 require_once(dirname(__FILE__)."/../support/FileReader.php");
00003 require_once(dirname(__FILE__).'/SpriteFile.php');
00004 require_once(dirname(__FILE__)."/S16Frame.php");
00005 
00006 
00008 
00011 class S16File extends SpriteFile {
00012 
00014     
00015     private $encoding;
00016     private $frameCount;
00017     private $frames;
00018     private $reader;
00019     
00021 
00023 
00027     public function S16File(IReader $reader)
00028     {
00029         parent::SpriteFile('S16');
00030         $this->reader = $reader;
00031         $buffer = $this->reader->ReadInt(4);
00032         if($buffer == 1) {
00033             $this->encoding = "565";
00034         } else if($buffer == 0) {
00035             $this->encoding = "555";
00036         } else {
00037             throw new Exception("File encoding not recognised. (".$buffer.'|'.$this->reader->GetPosition().')');
00038         }
00039         $this->frameCount = $this->reader->ReadInt(2);
00040         for($i=0; $i < $this->frameCount; $i++)
00041         {
00042             $this->AddFrame(new S16Frame($this->reader,$this->encoding));
00043         }
00044     }
00046 
00051     public function SetEncoding($encoding) {
00052         $this->EnsureDecompiled();
00053         $this->encoding = $encoding;
00054     }
00055 
00057 
00062     public function Compile() {
00063         $data = ''; 
00064         /* S16 and C16 are actually the same format....
00065          * C16 just has RLE. But they're different classes.
00066          * not very DRY, I know. This is better for magic though. */
00067         $flags = 0; 
00068         // 0b00 => 555 S16,
00069         // 0b01 => 565 S16,
00070         // 0b10 => 555 C16,
00071         // 0b11 => 565 C16
00072         if($this->encoding == '565') {
00073             $flags = $flags | 1;
00074         }
00075         $data .= pack('V',$flags);
00076         $data .= pack('v',$this->GetFrameCount());
00077         $idata = '';
00078         $offset = 6+(8*$this->GetFrameCount());
00079         foreach($this->GetFrames() as $frame) {
00080             $data .= pack('V',$offset);
00081             $data .= pack('vv',$frame->GetWidth(),$frame->GetHeight());
00082 
00083             $framebin = $frame->Encode();
00084             $offset += strlen($framebin); 
00085             $idata .= $framebin;
00086         }
00087         return $data . $idata;
00088     }
00089 }
00090 ?>
 All Classes Files Functions Variables Enumerations