@@ -1,19 +1,19 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | function DeArchive($data) { |
| 3 | - if(is_string($data)) { |
|
| 4 | - if(substr($data,0,55) == "Creatures Evolution Engine - Archived information file.") { |
|
| 5 | - $data = substr($data,strpos($data,chr(0x1A).chr(0x04))+2); |
|
| 3 | + if (is_string($data)) { |
|
| 4 | + if (substr($data, 0, 55) == "Creatures Evolution Engine - Archived information file.") { |
|
| 5 | + $data = substr($data, strpos($data, chr(0x1A).chr(0x04))+2); |
|
| 6 | 6 | $data = gzuncompress($data); |
| 7 | 7 | return $data; |
| 8 | 8 | } |
| 9 | 9 | die('Couldn\'t dearchive -- Probably invalid file'); |
| 10 | 10 | return false; |
| 11 | - } else if(is_resource($data)) { |
|
| 11 | + } else if (is_resource($data)) { |
|
| 12 | 12 | return false; //coming soon |
| 13 | 13 | } |
| 14 | 14 | } |
| 15 | -function Archive($data,$filehandle=null) { |
|
| 16 | - if(is_resource($filehandle)) { |
|
| 15 | +function Archive($data, $filehandle = null) { |
|
| 16 | + if (is_resource($filehandle)) { |
|
| 17 | 17 | return false; |
| 18 | 18 | } |
| 19 | 19 | $data = gzcompress($data); |
@@ -21,11 +21,11 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function FileReader($filename) |
| 23 | 23 | { |
| 24 | - if(!file_exists($filename)) |
|
| 24 | + if (!file_exists($filename)) |
|
| 25 | 25 | throw new Exception("File does not exist: ".$filename); |
| 26 | - if(!is_file($filename)) |
|
| 26 | + if (!is_file($filename)) |
|
| 27 | 27 | throw new Exception("Target is not a file."); |
| 28 | - if(!is_readable($filename)) |
|
| 28 | + if (!is_readable($filename)) |
|
| 29 | 29 | throw new Exception("File exists, but is not readable."); |
| 30 | 30 | |
| 31 | 31 | $this->fp = fopen($filename, 'rb'); |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function Read($count) |
| 35 | 35 | { |
| 36 | - if($count > 0) { |
|
| 36 | + if ($count > 0) { |
|
| 37 | 37 | return fread($this->fp, $count); |
| 38 | 38 | } |
| 39 | 39 | return ''; |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | public function ReadInt($count) |
| 43 | 43 | { |
| 44 | 44 | $int = 0; |
| 45 | - for($x = 0; $x < $count; $x++) |
|
| 45 | + for ($x = 0; $x < $count; $x++) |
|
| 46 | 46 | { |
| 47 | - $buffer = (ord(fgetc($this->fp)) << ($x * 8)); |
|
| 48 | - if($buffer === false) |
|
| 47 | + $buffer = (ord(fgetc($this->fp)) << ($x*8)); |
|
| 48 | + if ($buffer === false) |
|
| 49 | 49 | throw new Exception("Read failure"); |
| 50 | 50 | $int += $buffer; |
| 51 | 51 | } |
@@ -57,31 +57,31 @@ discard block |
||
| 57 | 57 | return ftell($this->fp); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - public function GetSubString($start,$length = FALSE) |
|
| 60 | + public function GetSubString($start, $length = FALSE) |
|
| 61 | 61 | { |
| 62 | 62 | $oldpos = ftell($this->fp); |
| 63 | - fseek($this->fp,$start); |
|
| 63 | + fseek($this->fp, $start); |
|
| 64 | 64 | $data = ''; |
| 65 | - if($length === false) { |
|
| 66 | - while($newdata = $this->Read(4096)) { |
|
| 67 | - if(strlen($newdata) == 0) { |
|
| 65 | + if ($length === false) { |
|
| 66 | + while ($newdata = $this->Read(4096)) { |
|
| 67 | + if (strlen($newdata) == 0) { |
|
| 68 | 68 | break; |
| 69 | 69 | } |
| 70 | 70 | $data .= $newdata; |
| 71 | 71 | } |
| 72 | 72 | } else { |
| 73 | - $data = fread($this->fp,$length); |
|
| 73 | + $data = fread($this->fp, $length); |
|
| 74 | 74 | } |
| 75 | - fseek($this->fp,$oldpos); |
|
| 75 | + fseek($this->fp, $oldpos); |
|
| 76 | 76 | return $data; |
| 77 | 77 | } |
| 78 | 78 | public function ReadCString() { |
| 79 | 79 | $string = ''; |
| 80 | - while(($char = $this->Read(1)) !== false) { |
|
| 81 | - if(ord($char) == 0) { |
|
| 80 | + while (($char = $this->Read(1)) !== false) { |
|
| 81 | + if (ord($char) == 0) { |
|
| 82 | 82 | break; |
| 83 | 83 | } |
| 84 | - $string.=$char; |
|
| 84 | + $string .= $char; |
|
| 85 | 85 | } |
| 86 | 86 | return $string; |
| 87 | 87 | } |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | * tend to get big enough to make this inefficiency a concern on |
| 32 | 32 | * any reasonably modern hardware. |
| 33 | 33 | */ |
| 34 | - public function GetSubString($start,$length=FALSE); |
|
| 34 | + public function GetSubString($start, $length = FALSE); |
|
| 35 | 35 | /// @brief Gets the position of the cursor |
| 36 | 36 | /** |
| 37 | 37 | * This is analagous to ftell in C or PHP. |
@@ -23,11 +23,11 @@ discard block |
||
| 23 | 23 | $this->position = 0; |
| 24 | 24 | } |
| 25 | 25 | public function Read($characters) { |
| 26 | - if($characters > 0) { |
|
| 27 | - if($this->position+$characters > strlen($this->string)) { |
|
| 26 | + if ($characters > 0) { |
|
| 27 | + if ($this->position+$characters > strlen($this->string)) { |
|
| 28 | 28 | return false; |
| 29 | 29 | } |
| 30 | - $str = substr($this->string,$this->position,$characters); |
|
| 30 | + $str = substr($this->string, $this->position, $characters); |
|
| 31 | 31 | |
| 32 | 32 | $this->position += $characters; |
| 33 | 33 | return $str; |
@@ -36,13 +36,13 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | public function ReadCString() { |
| 38 | 38 | $string = ''; |
| 39 | - while(($char = $this->Read(1)) !== false) { |
|
| 40 | - $string.=$char; |
|
| 41 | - if($char == "\0") { |
|
| 39 | + while (($char = $this->Read(1)) !== false) { |
|
| 40 | + $string .= $char; |
|
| 41 | + if ($char == "\0") { |
|
| 42 | 42 | break; |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | - return substr($string,0,-1); |
|
| 45 | + return substr($string, 0, -1); |
|
| 46 | 46 | } |
| 47 | 47 | public function Seek($position) { |
| 48 | 48 | $this->position = $position; |
@@ -56,23 +56,23 @@ discard block |
||
| 56 | 56 | public function GetPosition() { |
| 57 | 57 | return $this->position; |
| 58 | 58 | } |
| 59 | - public function GetSubString($start,$length = FALSE) { |
|
| 60 | - if($length == FALSE) { |
|
| 59 | + public function GetSubString($start, $length = FALSE) { |
|
| 60 | + if ($length == FALSE) { |
|
| 61 | 61 | $length = strlen($this->string)-$start; |
| 62 | 62 | } |
| 63 | - $str = substr($this->string,$start,$length); |
|
| 63 | + $str = substr($this->string, $start, $length); |
|
| 64 | 64 | return $str; |
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | function BytesToIntLilEnd($string) { //little endian |
| 69 | - if($string == "") { |
|
| 69 | + if ($string == "") { |
|
| 70 | 70 | return false; |
| 71 | 71 | } |
| 72 | 72 | $length = strlen($string); |
| 73 | 73 | $int = 0; |
| 74 | - for($i=0;$i<$length;$i++) { |
|
| 75 | - $int += ord($string{$i})<<($i*8); |
|
| 74 | + for ($i = 0; $i < $length; $i++) { |
|
| 75 | + $int += ord($string{$i}) << ($i*8); |
|
| 76 | 76 | } |
| 77 | 77 | return $int; |
| 78 | 78 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once(dirname(__FILE__) . '/AbstractTestCase.php'); |
|
| 3 | -require_once(dirname(__FILE__) . '/../support/FileReader.php'); |
|
| 4 | -require_once(dirname(__FILE__) . '/../support/StringReader.php'); |
|
| 2 | +require_once(dirname(__FILE__).'/AbstractTestCase.php'); |
|
| 3 | +require_once(dirname(__FILE__).'/../support/FileReader.php'); |
|
| 4 | +require_once(dirname(__FILE__).'/../support/StringReader.php'); |
|
| 5 | 5 | |
| 6 | 6 | class ReaderTest extends c2ephpAbstractTestCase { |
| 7 | 7 | |
@@ -10,19 +10,19 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | public function testStringReader($string) { |
| 12 | 12 | $stringReader = new StringReader($string); |
| 13 | - $this->assertEquals($string[0],$stringReader->Read(1)); |
|
| 14 | - $this->assertEquals($string[1],$stringReader->Read(1)); |
|
| 13 | + $this->assertEquals($string[0], $stringReader->Read(1)); |
|
| 14 | + $this->assertEquals($string[1], $stringReader->Read(1)); |
|
| 15 | 15 | $stringReader->skip(2); |
| 16 | - $this->assertEquals($string[4],$stringReader->Read(1)); |
|
| 17 | - $this->assertEquals(substr($string,3,8),$stringReader->GetSubString(3,8)); |
|
| 18 | - $this->assertEquals($string[5],$stringReader->Read(1)); |
|
| 16 | + $this->assertEquals($string[4], $stringReader->Read(1)); |
|
| 17 | + $this->assertEquals(substr($string, 3, 8), $stringReader->GetSubString(3, 8)); |
|
| 18 | + $this->assertEquals($string[5], $stringReader->Read(1)); |
|
| 19 | 19 | |
| 20 | - $this->assertEquals($string,$stringReader->GetSubString(0)); |
|
| 20 | + $this->assertEquals($string, $stringReader->GetSubString(0)); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public function generateStrings() { |
| 24 | 24 | return array( |
| 25 | - array('striiing','blimp!!!!!!!'), |
|
| 25 | + array('striiing', 'blimp!!!!!!!'), |
|
| 26 | 26 | ); |
| 27 | 27 | } |
| 28 | 28 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once(dirname(__FILE__) . '/../agents/PRAYFile.php'); |
|
| 3 | -require_once(dirname(__FILE__) . '/../support/FileReader.php'); |
|
| 2 | +require_once(dirname(__FILE__).'/../agents/PRAYFile.php'); |
|
| 3 | +require_once(dirname(__FILE__).'/../support/FileReader.php'); |
|
| 4 | 4 | |
| 5 | 5 | abstract class c2ephpAbstractTestCase extends PHPUnit_Framework_TestCase { |
| 6 | 6 | /// @brief Agent Files fixture |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | ); |
| 28 | 28 | |
| 29 | 29 | $prayfiles = array(); |
| 30 | - foreach($files as $info) { |
|
| 30 | + foreach ($files as $info) { |
|
| 31 | 31 | |
| 32 | 32 | $prayfiles[] = array(new PRAYFile(new FileReader($info['path'])), $info); |
| 33 | 33 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | |
| 46 | 46 | $prayfiles = array(); |
| 47 | - foreach($files as $info) { |
|
| 47 | + foreach ($files as $info) { |
|
| 48 | 48 | |
| 49 | 49 | $prayfiles[] = array(new PRAYFile(new FileReader($info['path'])), $info); |
| 50 | 50 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -require_once(dirname(__FILE__) . '/AbstractTestCase.php'); |
|
| 2 | +require_once(dirname(__FILE__).'/AbstractTestCase.php'); |
|
| 3 | 3 | |
| 4 | 4 | class PRAYFileTest extends c2ephpAbstractTestCase { |
| 5 | 5 | |
@@ -7,56 +7,56 @@ discard block |
||
| 7 | 7 | * @dataProvider createAgentFiles |
| 8 | 8 | */ |
| 9 | 9 | public function testCreatePRAYFile(PRAYFile $prayfile) { |
| 10 | - $this->assertInstanceOf('PRAYFile',$prayfile); |
|
| 10 | + $this->assertInstanceOf('PRAYFile', $prayfile); |
|
| 11 | 11 | $this->assertNotNull($prayfile); |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * @dataProvider createAgentFiles |
| 16 | 16 | */ |
| 17 | - public function testNumberOfBlocks(PRAYFile $prayfile,$fixtureInfo) { |
|
| 17 | + public function testNumberOfBlocks(PRAYFile $prayfile, $fixtureInfo) { |
|
| 18 | 18 | $this->assertNotNull($prayfile); |
| 19 | - $this->assertEquals($fixtureInfo['block count'],sizeof($prayfile->GetBlocks())); |
|
| 19 | + $this->assertEquals($fixtureInfo['block count'], sizeof($prayfile->GetBlocks())); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @dataProvider createAgentFiles |
| 24 | 24 | */ |
| 25 | - public function testFirstAgentDescription(PRAYFile $prayfile,$info) { |
|
| 25 | + public function testFirstAgentDescription(PRAYFile $prayfile, $info) { |
|
| 26 | 26 | $this->assertNotNull($prayfile); |
| 27 | 27 | $blocks = $prayfile->GetBlocks(PRAY_BLOCK_AGNT); |
| 28 | - $this->assertEquals($info['first agent desc'],$blocks[0]->GetAgentDescription()); |
|
| 28 | + $this->assertEquals($info['first agent desc'], $blocks[0]->GetAgentDescription()); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * @dataProvider createAgentFiles |
| 33 | 33 | */ |
| 34 | - public function testChangeAGNTAndRecompile(PRAYFile $prayfile,$info) { |
|
| 34 | + public function testChangeAGNTAndRecompile(PRAYFile $prayfile, $info) { |
|
| 35 | 35 | $this->assertNotNull($prayfile); |
| 36 | 36 | $blocks = $prayfile->GetBlocks(PRAY_BLOCK_AGNT); |
| 37 | - $blocks[0]->SetTag('Agent Description','Testing'); |
|
| 38 | - $this->assertEquals('Testing',$blocks[0]->GetAgentDescription()); |
|
| 37 | + $blocks[0]->SetTag('Agent Description', 'Testing'); |
|
| 38 | + $this->assertEquals('Testing', $blocks[0]->GetAgentDescription()); |
|
| 39 | 39 | $data = $prayfile->Compile(); |
| 40 | 40 | $newfile = new PRAYFile(new StringReader($data)); |
| 41 | - $this->assertEquals($info['block count'],sizeof($newfile->GetBlocks())); |
|
| 41 | + $this->assertEquals($info['block count'], sizeof($newfile->GetBlocks())); |
|
| 42 | 42 | $newfile->GetBlocks('AGNT'); |
| 43 | - $this->assertEquals('Testing',$blocks[0]->GetAgentDescription()); |
|
| 43 | + $this->assertEquals('Testing', $blocks[0]->GetAgentDescription()); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * @dataProvider createCreatureFiles |
| 48 | 48 | */ |
| 49 | - public function testChangeGLSTAndRecompile(PRAYFILE $prayfile,$info) { |
|
| 49 | + public function testChangeGLSTAndRecompile(PRAYFILE $prayfile, $info) { |
|
| 50 | 50 | $this->assertNotNull($prayfile); |
| 51 | 51 | $blocks = $prayfile->GetBlocks(PRAY_BLOCK_GLST); |
| 52 | - $this->assertEquals($info['events count'],$blocks[0]->GetHistory()->CountEvents()); |
|
| 52 | + $this->assertEquals($info['events count'], $blocks[0]->GetHistory()->CountEvents()); |
|
| 53 | 53 | $blocks[0]->GetHistory()->RemoveEvent(0); |
| 54 | - $this->assertEquals($info['events count']-1,$blocks[0]->GetHistory()->CountEvents()); |
|
| 54 | + $this->assertEquals($info['events count']-1, $blocks[0]->GetHistory()->CountEvents()); |
|
| 55 | 55 | |
| 56 | 56 | $data = $prayfile->Compile(); |
| 57 | 57 | $newfile = new PRAYFile(new StringReader($data)); |
| 58 | 58 | $blocks = $newfile->GetBlocks(PRAY_BLOCK_GLST); |
| 59 | - $this->assertEquals($info['events count']-1,$blocks[0]->GetHistory()->CountEvents()); |
|
| 59 | + $this->assertEquals($info['events count']-1, $blocks[0]->GetHistory()->CountEvents()); |
|
| 60 | 60 | |
| 61 | 61 | } |
| 62 | 62 | } |
@@ -16,30 +16,30 @@ discard block |
||
| 16 | 16 | * If $reader is null, creates an empty C16File ready to add sprites to. |
| 17 | 17 | * @param $reader The reader to read the sprites from. Can be null. |
| 18 | 18 | */ |
| 19 | - public function C16File(IReader $reader=null) |
|
| 19 | + public function C16File(IReader $reader = null) |
|
| 20 | 20 | { |
| 21 | - if($reader != null) { |
|
| 21 | + if ($reader != null) { |
|
| 22 | 22 | parent::SpriteFile('C16'); |
| 23 | 23 | $buffer = $reader->ReadInt(4); |
| 24 | - if(($buffer & 1) == 1) { |
|
| 24 | + if (($buffer & 1) == 1) { |
|
| 25 | 25 | $this->encoding = '565'; |
| 26 | 26 | } else { |
| 27 | 27 | $this->encoding = '555'; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - 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. |
|
| 30 | + 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. |
|
| 31 | 31 | throw new Exception('This file is probably a S16 masquerading as a C16!'); |
| 32 | - } else if($buffer > 3) { |
|
| 32 | + } else if ($buffer > 3) { |
|
| 33 | 33 | throw new Exception('File encoding not recognised. ('.$buffer.')'); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | $buffer = $reader->ReadInt(2); |
| 37 | - if($buffer < 1) |
|
| 37 | + if ($buffer < 1) |
|
| 38 | 38 | throw new Exception('Sprite file appears to contain less than 1 frame.'); |
| 39 | 39 | $frameCount = $buffer; |
| 40 | - for($x=0; $x < $frameCount; $x++) |
|
| 40 | + for ($x = 0; $x < $frameCount; $x++) |
|
| 41 | 41 | { |
| 42 | - $this->AddFrame(new C16Frame($reader,$this->encoding)); |
|
| 42 | + $this->AddFrame(new C16Frame($reader, $this->encoding)); |
|
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | } |
@@ -58,30 +58,30 @@ discard block |
||
| 58 | 58 | public function Compile() { |
| 59 | 59 | $data = ''; |
| 60 | 60 | $flags = 2; // 0b00 => 555 S16, 0b01 => 565 S16, 0b10 => 555 C16, 0b11 => 565 C16 |
| 61 | - if($this->encoding == '565') { |
|
| 61 | + if ($this->encoding == '565') { |
|
| 62 | 62 | $flags = $flags | 1; |
| 63 | 63 | } |
| 64 | - $data .= pack('V',$flags); |
|
| 65 | - $data .= pack('v',$this->GetFrameCount()); |
|
| 64 | + $data .= pack('V', $flags); |
|
| 65 | + $data .= pack('v', $this->GetFrameCount()); |
|
| 66 | 66 | $idata = ''; |
| 67 | 67 | $offset = 6+(8*$this->GetFrameCount()); |
| 68 | - foreach($this->GetFrames() as $frame) { |
|
| 68 | + foreach ($this->GetFrames() as $frame) { |
|
| 69 | 69 | $offset += ($frame->GetHeight()-1)*4; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - foreach($this->GetFrames() as $frame) { |
|
| 73 | - $data .= pack('V',$offset); |
|
| 74 | - $data .= pack('vv',$frame->GetWidth(),$frame->GetHeight()); |
|
| 72 | + foreach ($this->GetFrames() as $frame) { |
|
| 73 | + $data .= pack('V', $offset); |
|
| 74 | + $data .= pack('vv', $frame->GetWidth(), $frame->GetHeight()); |
|
| 75 | 75 | |
| 76 | 76 | $framedata = $frame->Encode(); |
| 77 | 77 | $framebin = $framedata['data']; |
| 78 | - foreach($framedata['lineoffsets'] as $lineoffset) { |
|
| 79 | - $data .= pack('V',$lineoffset+$offset); |
|
| 78 | + foreach ($framedata['lineoffsets'] as $lineoffset) { |
|
| 79 | + $data .= pack('V', $lineoffset+$offset); |
|
| 80 | 80 | } |
| 81 | 81 | $offset += strlen($framebin); |
| 82 | 82 | $idata .= $framebin; |
| 83 | 83 | } |
| 84 | - return $data . $idata; |
|
| 84 | + return $data.$idata; |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | } |
@@ -25,26 +25,26 @@ discard block |
||
| 25 | 25 | * @param $height Ignored when creating an SPRFrame from a GD image. |
| 26 | 26 | * @param $offset How far through the IReader the SPRFrame is. May not ever be used. |
| 27 | 27 | */ |
| 28 | - public function SPRFrame($reader,$width=0,$height=0,$offset=false) { |
|
| 29 | - if($reader instanceof IReader) { |
|
| 28 | + public function SPRFrame($reader, $width = 0, $height = 0, $offset = false) { |
|
| 29 | + if ($reader instanceof IReader) { |
|
| 30 | 30 | $this->reader = $reader; |
| 31 | - parent::SpriteFrame($width,$height); |
|
| 32 | - if($offset !== false) { |
|
| 31 | + parent::SpriteFrame($width, $height); |
|
| 32 | + if ($offset !== false) { |
|
| 33 | 33 | $this->offset = $offset; |
| 34 | 34 | } else { |
| 35 | 35 | $this->offset = $this->reader->GetPosition(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | //initialise palette if necessary. |
| 39 | - if(empty(self::$sprToRGB)) { |
|
| 39 | + if (empty(self::$sprToRGB)) { |
|
| 40 | 40 | $paletteReader = new FileReader(dirname(__FILE__).'/palette.dta'); |
| 41 | - for($i = 0;$i<256;$i++) { |
|
| 42 | - self::$sprToRGB[$i] = array('r'=>$paletteReader->ReadInt(1)*4,'g'=>$paletteReader->ReadInt(1)*4,'b'=>$paletteReader->ReadInt(1)*4); |
|
| 41 | + for ($i = 0; $i < 256; $i++) { |
|
| 42 | + self::$sprToRGB[$i] = array('r'=>$paletteReader->ReadInt(1)*4, 'g'=>$paletteReader->ReadInt(1)*4, 'b'=>$paletteReader->ReadInt(1)*4); |
|
| 43 | 43 | } |
| 44 | 44 | unset($paletteReader); |
| 45 | 45 | } |
| 46 | - } else if(get_resource_type($reader) == 'gd') { |
|
| 47 | - parent::SpriteFrame(imagesx($reader),imagesy($reader),true); |
|
| 46 | + } else if (get_resource_type($reader) == 'gd') { |
|
| 47 | + parent::SpriteFrame(imagesx($reader), imagesy($reader), true); |
|
| 48 | 48 | $this->gdImage = $reader; |
| 49 | 49 | } else { |
| 50 | 50 | throw new Exception('$reader was not an IReader or a gd image.'); |
@@ -58,13 +58,13 @@ discard block |
||
| 58 | 58 | * feel free to use it yourself, it's not going anywhere. |
| 59 | 59 | */ |
| 60 | 60 | public function Flip() { |
| 61 | - if($this->HasBeenDecoded()) { |
|
| 61 | + if ($this->HasBeenDecoded()) { |
|
| 62 | 62 | throw new Exception('Too late!'); |
| 63 | 63 | return; |
| 64 | 64 | } |
| 65 | 65 | $tempData = ''; |
| 66 | - for($i=($this->GetHeight()-1);$i>-1;$i--) { |
|
| 67 | - $tempData .= $this->reader->GetSubString($this->offset+($this->GetWidth())*$i,($this->GetWidth())); |
|
| 66 | + for ($i = ($this->GetHeight()-1); $i > -1; $i--) { |
|
| 67 | + $tempData .= $this->reader->GetSubString($this->offset+($this->GetWidth())*$i, ($this->GetWidth())); |
|
| 68 | 68 | } |
| 69 | 69 | $this->reader = new StringReader($tempData); |
| 70 | 70 | $this->offset = 0; |
@@ -78,12 +78,12 @@ discard block |
||
| 78 | 78 | protected function Decode() { |
| 79 | 79 | $image = imagecreatetruecolor($this->GetWidth(), $this->GetHeight()); |
| 80 | 80 | $this->reader->Seek($this->offset); |
| 81 | - for($y = 0; $y < $this->GetHeight(); $y++) |
|
| 81 | + for ($y = 0; $y < $this->GetHeight(); $y++) |
|
| 82 | 82 | { |
| 83 | - for($x = 0; $x < $this->GetWidth(); $x++) |
|
| 83 | + for ($x = 0; $x < $this->GetWidth(); $x++) |
|
| 84 | 84 | { |
| 85 | 85 | $colour = self::$sprToRGB[$this->reader->ReadInt(1)]; |
| 86 | - imagesetpixel($image,$x,$y,imagecolorallocate($image,$colour['r'],$colour['g'],$colour['b'])); |
|
| 86 | + imagesetpixel($image, $x, $y, imagecolorallocate($image, $colour['r'], $colour['g'], $colour['b'])); |
|
| 87 | 87 | } |
| 88 | 88 | } |
| 89 | 89 | $this->gdImage = $image; |
@@ -101,10 +101,10 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function Encode() { |
| 103 | 103 | $data = ''; |
| 104 | - for($y = 0; $y < $this->GetHeight(); $y++ ) { |
|
| 105 | - for($x = 0; $x < $this->GetWidth(); $x++ ) { |
|
| 104 | + for ($y = 0; $y < $this->GetHeight(); $y++) { |
|
| 105 | + for ($x = 0; $x < $this->GetWidth(); $x++) { |
|
| 106 | 106 | $color = $this->GetPixel($x, $y); |
| 107 | - $data .= pack('C',$this->RGBToSPR($color['red'], $color['green'], $color['blue'])); |
|
| 107 | + $data .= pack('C', $this->RGBToSPR($color['red'], $color['green'], $color['blue'])); |
|
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | return $data; |
@@ -117,13 +117,13 @@ discard block |
||
| 117 | 117 | * Runs in O(n) time. |
| 118 | 118 | */ |
| 119 | 119 | |
| 120 | - private function RGBToSPR($r,$g,$b) { |
|
| 120 | + private function RGBToSPR($r, $g, $b) { |
|
| 121 | 121 | //start out with the maximum distance. |
| 122 | - $minDistance = ($r^2) + ($g^2) + ($b^2); |
|
| 122 | + $minDistance = ($r ^ 2)+($g ^ 2)+($b ^ 2); |
|
| 123 | 123 | $minKey = 0; |
| 124 | - foreach(self::$sprToRGB as $key => $colour) { |
|
| 125 | - $distance = pow(($r-$colour['r']),2) + pow(($g-$colour['g']),2) + pow(($b-$colour['b']),2); |
|
| 126 | - if($distance == 0) { |
|
| 124 | + foreach (self::$sprToRGB as $key => $colour) { |
|
| 125 | + $distance = pow(($r-$colour['r']), 2)+pow(($g-$colour['g']), 2)+pow(($b-$colour['b']), 2); |
|
| 126 | + if ($distance == 0) { |
|
| 127 | 127 | return $key; |
| 128 | 128 | } else if ($distance < $minDistance) { |
| 129 | 129 | $minKey = $key; |