c2ephp

sprites/C16File.php

Go to the documentation of this file.
00001 <?php
00002 require_once(dirname(__FILE__).'/../support/IReader.php');
00003 require_once(dirname(__FILE__).'/SpriteFile.php');
00004 require_once(dirname(__FILE__).'/C16Frame.php');
00005 
00007 class C16File extends SpriteFile
00008 {
00010     
00011     private $encoding;
00013 
00015 
00019     public function C16File(IReader $reader=null)
00020     {
00021         if($reader != null) {
00022             parent::SpriteFile('C16');
00023             $buffer = $reader->ReadInt(4);
00024             if(($buffer & 1) == 1) {
00025                 $this->encoding = '565';
00026             } else {
00027                 $this->encoding = '555';
00028             }
00029 
00030             if(($buffer & 2) == 0) { //buffer & 2 == 2 => RLE. buffer & 2 == 0 => non-RLE (same as s16 but not supported here because it's complex dude.
00031                 throw new Exception('This file is probably a S16 masquerading as a C16!');
00032             } else if($buffer > 3) {
00033                 throw new Exception('File encoding not recognised. ('.$buffer.')');
00034             }
00035 
00036             $buffer = $reader->ReadInt(2);
00037             if($buffer < 1)
00038                 throw new Exception('Sprite file appears to contain less than 1 frame.');
00039             $frameCount = $buffer;
00040             for($x=0; $x < $frameCount; $x++)
00041             {
00042                 $this->AddFrame(new C16Frame($reader,$this->encoding));
00043             }
00044         }
00045     }
00047 
00050     public function SetEncoding($encoding) {
00051         $this->EnsureDecompiled();
00052         $this->encoding = $encoding;
00053     }
00055 
00058     public function Compile() {
00059         $data = ''; 
00060         $flags = 2; // 0b00 => 555 S16, 0b01 => 565 S16, 0b10 => 555 C16, 0b11 => 565 C16
00061         if($this->encoding == '565') {
00062             $flags = $flags | 1;
00063         }
00064         $data .= pack('V',$flags);
00065         $data .= pack('v',$this->GetFrameCount());
00066         $idata = '';
00067         $offset = 6+(8*$this->GetFrameCount());
00068         foreach($this->GetFrames() as $frame) {
00069             $offset += ($frame->GetHeight()-1)*4;
00070         }
00071 
00072         foreach($this->GetFrames() as $frame) {
00073             $data .= pack('V',$offset);
00074             $data .= pack('vv',$frame->GetWidth(),$frame->GetHeight());
00075 
00076             $framedata = $frame->Encode();
00077             $framebin = $framedata['data'];
00078             foreach($framedata['lineoffsets'] as $lineoffset) {
00079                 $data .= pack('V',$lineoffset+$offset);
00080             }
00081             $offset += strlen($framebin); 
00082             $idata .= $framebin;
00083         }
00084         return $data . $idata;
00085     }
00086 
00087 }
00088 ?>
 All Classes Files Functions Variables Enumerations