SpriteCoder/code.php

587 lines
16 KiB
PHP

<?php
include('codeBasic.php');
function AjoutRetourLigne($str) //ajoute à la fin de la chaine $str un retour chariot
{
return $str.'
';
}
function estTropErreur($err)
{
$nb=count($err);
for ($i=0 ; $i<$nb ; $i++)
if ($err[$i]["err"]==2 )
return true;
return false;
}
function uploadFile()
{
$nbFichier=count($_FILES['fichier']['name']);
$nomImage = array();
if ($_FILES['fichier']['error'][0]!=4)
{
/**on upload les fichiers**/
for ($j= 0;$j<$nbFichier;$j++)
{
move_uploaded_file($_FILES['fichier']['tmp_name'][$j], "upload/". $_FILES['fichier']['name'][$j]);
$extension = $_FILES['fichier']['type'][$j]; //extension est sous la forme "image/png" par exemple ...
$extension=str_replace('image/','',$extension); //... du coup on supprime /image
if ($extension != "png" && $extension != "gif" && $extension != "jpeg")
/* on test si le fichier est bien une image*/
{
array_push($nomImage, array("ext"=>"" , "name"=>$_FILES['fichier']['name'][$j] , "err" => 2 , "msg" => "L'extension du fichier ".$_FILES['fichier']['name'][$j]." est invalide." ));
}
else
array_push($nomImage, array("ext"=>$extension , "name"=>$_FILES['fichier']['name'][$j] , "err" => 0 , "msg" => "" ));
}
}
else
{
array_push($nomImage, array("ext"=>"" , "name"=>"" , "err" => 2 , "msg" => "Vous n'avez pas choisi de fichier." ));
}
return $nomImage;
}
function errorMsg($str)
{
$string = "<div class='errorBox'>
<table>
<tr>
<td width=30px ><img src='image/error.png'/></td>
<td>".$str."</td>
</tr>
</table>
</div>";
return $string;
}
function warningMsg($str)
{
$string = "<div class='errorBox'>
<table>
<tr>
<td width=30px ><img src='image/warning.png'/></td>
<td>".$str."</td>
</tr>
</table>
</div>";
return $string;
}
function displayError($error)
{
$size = count($error);
for ($j= 0;$j<$size;$j++)
{
if ( $error[$j]["err"] == 2)
echo errorMsg($error[$j]["msg"])."<hr/>";
else if ( $error[$j]["err"] == 1)
echo warningMsg($error[$j]["msg"])."<hr/>";
}
}
function deboguage($uploadedFile) //cette fonction teste s'i n'y a aucun bug
{
if ($_POST["langage"] === " ")
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 2 , "msg" => "Language de programmation sélectionné invalide."));
else if ($_POST["langage"] === "Basic")
{
if ($_POST["calculatriceB"] === " ")
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 2 , "msg" => "Calculatrice sélectionné invalide."));
}
else if ($_POST["langage"] === "C/C++")
{
if ($_POST["calculatriceC"] === " ")
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 2 , "msg" => "Calculatrice sélectionné invalide."));
if ($_POST["nameC"] === "persona" && trim($_POST["namePersC"]) === "")
{
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 1 , "msg" => "Nom des sprites invalide, nom par défaut sélectionné"));
$_POST["nameC"] = "default";
}
if ($_POST["tabpointeurC"] === "oui" && trim($_POST["tabpointeur_name"]) === "")
{
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 1 , "msg" => "Nom du tableau de pointeur invalide, nom par défaut sélectionné"));
$_POST["tabpointeur_name"] = "pointerTab";
}
if ($_POST["coupeSprC"] === "oui")
{
if ($_POST["outil_largSprC"] < 1)
{
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 1 , "msg" => "Découpage de sprite : hauteur de chaque sprite invalide (doit être supérieur à 1)"));
$_POST["outil_largSprC"] = 16;
}
if ($_POST["outil_longSprC"] < 1)
{
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 1 , "msg" => "Découpage de sprite : longueur de chaque sprite invalide (doit être supérieur à 1)"));
$_POST["outil_longSprC"] = 16;
}
}
}
else if ($_POST["langage"] !== "Lua")
{
array_push($uploadedFile, array( "ext"=>"" , "name"=>"" , "err" => 2 , "msg" => "Language de programmation sélectionné invalide."));
}
return $uploadedFile;
}
function supprimerExtensionDansNom($nom)
{
$pattern = "/\.(.)*/";
return preg_replace($pattern, "", $nom);
}
function convertToReadablePicture($images)
{
$size = count($images);
$convertedPicture = array();
for ($j= 0;$j<$size;$j++)
{
if ($images[$j]["err"] == 0)
{
$extension = $images[$j]["ext"];
$name = "upload/".$images[$j]["name"];
$im = "";
if ($extension == "png")
$im = imagecreatefrompng($name);
else if ($extension == "gif")
$im = imagecreatefromgif($name);
else if ($extension == "jpeg")
$im = imagecreatefromjpeg($name);
array_push($convertedPicture,array("pict" => $im , "name" => supprimerExtensionDansNom($images[$j]["name"])));
}
if ($images[$j]["name"] != "")
unlink("upload/".$images[$j]["name"]);
}
return $convertedPicture;
}
function codeSpriteC_nom($name, $largeur, $hauteur)
{
$string = "";
if ($_POST["motcleC"]=="extern")
$string = "extern ";
else if ($_POST["motcleC"]=="static")
$string = "static ";
if (($_POST["bitC"]=="8bit") || $_POST["calculatriceC"]=="G75/G85/G95/G35")
$string = $string . "const unsigned char $name"."[]={";
else if ($_POST["bitC"]=="16bit")
$string = $string . "const color_t $name"."[".$largeur*2*$hauteur."]={";
if ( $_POST["retourLigneSpriteC"]=="default" )
$string = AjoutRetourLigne($string);
return $string;
}
function creerImage_gris($im, $estClair) // 0<noir<=64 , 64<grisClair<=128
//$estClair = true si sprite clair
//sinon foncé
{
$height = imagesy($im);
$width = imagesx($im);
$imReturn = imagecreatetruecolor ( $width , $height );
for ( $j=0 ; $j<$height ; $j++ )
{
for ( $i=0 ; $i<$width ; $i++ )
{
$rgb = imagecolorat($im,$i,$j);
$cols = imagecolorsforindex($im, $rgb);
$r = $cols['red'];
$g = $cols['green'];
$b = $cols['blue'];
if ($estClair)
{
if (($r+$g+$b)/3 <= 64)
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,0,0,0));
else if (($r+$g+$b)/3 > 64 && ($r+$g+$b)/3 <= 128 )
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,255,255,255));
else if (($r+$g+$b)/3 > 128 && ($r+$g+$b)/3 <= 192 )
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,0,0,0));
else
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,255,255,255));
}
else
{
if (($r+$g+$b)/3 <= 64)
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,0,0,0));
else if (($r+$g+$b)/3 > 64 && ($r+$g+$b)/3 <= 128 )
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,0,0,0));
else if (($r+$g+$b)/3 > 128 && ($r+$g+$b)/3 <= 192 )
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,255,255,255));
else
imagesetpixel($imReturn, $i, $j, imagecolorallocate ($imReturn,255,255,255));
}
}
}
return $imReturn;
}
function codeSpriteC_G75($x, $y, $width, $height, $im, $name,$maxwidth, $maxheight) //retourne sprite codé sous forme de string
{
$string = codeSpriteC_nom($name, $width, $height);
$valeurTableauPalette = array();
for ( $j=$y ; $j<$y+$height ; $j++ )
{
if ( $_POST["retourLigneSpriteC"]=="default" )
$string = $string . "\t";
for ( $i=$x ; $i<$x+$width ; $i+=8 )
{
$binaire = "";
for ($k=$i ; $k<$i+8 ; $k++)
{
if (($k<$maxwidth && $j<$maxheight) )
{
$rgb = imagecolorat($im,$k,$j);
$cols = imagecolorsforindex($im, $rgb);
$r = $cols['red'];
$g = $cols['green'];
$b = $cols['blue'];
if ( ($r+$g+$b)/3 > 127 )
$binaire = $binaire."0";
else
$binaire = $binaire."1";
}
else
$binaire = $binaire."1";
}
$hex = dechex(bindec($binaire));
$string = $string . "0x$hex, ";
}
if ( $_POST["retourLigneSpriteC"]=="default" && $j<$maxheight)
$string = AjoutRetourLigne($string);
}
$string = AjoutRetourLigne( AjoutRetourLigne( $string . "};" ) );
return $string;
}
function nomTabPointeur($name)
{
$string = "";
if (($_POST["bitC"]=="8bit") || $_POST["calculatriceC"]=="G75/G85/G95/G35")
$string = $string . "const unsigned char* $name"."[]={";
else if ($_POST["bitC"]=="16bit")
$string = $string . "const color_t* $name"."[]={";
if ( $_POST["retourLigne"]=="oui" )
$string = AjoutRetourLigne($string);
return $string;
}
function genererTabPointeur($tableauPonteur,$tableauPonteurPalette)
{
$string ="";
$taille = count($tableauPonteur);
$string = $string . nomTabPointeur($_POST["tabpointeur_name"]);
for ( $i=0 ; $i<$taille ; $i++ )
{
if ( $_POST["retourLigne"]=="oui" )
$string = $string . "\t";
$string = $string . $tableauPonteur[$i];
if ($i != $taille-1)
$string = $string . ", ";
if ( $_POST["retourLigne"]=="oui" )
$string = AjoutRetourLigne($string);
}
$string = $string . "};";
if ($_POST["bitC"]!=="16bit" && $_POST["calculatriceC"] === "Prizm/Cg-10/Cg-20")
{
$string = AjoutRetourLigne(AjoutRetourLigne($string)) . "const color_t* ".$_POST["tabpointeur_name"]."_palette[]={";
if ( $_POST["retourLigne"]=="oui" )
$string = AjoutRetourLigne($string);
$taille = count($tableauPonteurPalette);
for ( $i=0 ; $i<$taille ; $i++ )
{
if ( $_POST["retourLigne"]=="oui" )
$string = $string . "\t";
$string = $string . $tableauPonteurPalette[$i];
if ($i != $taille-1)
$string = $string . ", ";
if ( $_POST["retourLigne"]=="oui" )
$string = AjoutRetourLigne($string);
}
$string = $string . "};";
}
return $string;
}
function codeSpriteC_CG20($x, $y, $width, $height, $im, $name,$maxwidth, $maxheight) //retourne sprite codé sous forme de string
{
$string = codeSpriteC_nom($name, $width, $height);
$valeurTableauPalette = array();
for ( $j=$y ; $j<$y+$height ; $j++ )
{
if ( $_POST["retourLigneSpriteC"]=="default" )
$string = $string . "\t";
for ( $i=$x ; $i<$x+$width ; $i++ )
{
if ( $i<$maxwidth && $j<$maxheight )
{
$rgb = imagecolorat($im,$i,$j);
$cols = imagecolorsforindex($im, $rgb);
$r = $cols['red'];
$g = $cols['green'];
$b = $cols['blue'];
$hex = dechex( (( (31*$r/255) & 0x1F) << 11 ) | (( (63*$g/255) & 0x3F) << 5 ) | (( (31*$b/255) & 0x1F)) );
if ($_POST["bitC"]=="8bit")
{
if (array_search ($hex,$valeurTableauPalette) === FALSE)
array_push($valeurTableauPalette,$hex);
$string = $string . array_search ($hex,$valeurTableauPalette);
}
else if ($_POST["bitC"]=="16bit")
{
$string = $string . "0x$hex";
}
$string = $string . ", ";
}
}
if ( $_POST["retourLigneSpriteC"]=="default" && $j<$maxheight-1)
$string = AjoutRetourLigne($string);
}
$string = AjoutRetourLigne( AjoutRetourLigne( $string . "};" ) );
if ($_POST["bitC"]!="16bit")
{
$taillePalette = count($valeurTableauPalette);
$string = $string . "const color_t ".$name."_palette[".$taillePalette."]={";
$string = AjoutRetourLigne($string) . "\t";
for ( $i=0 ; $i<$taillePalette ; $i++ )
{
$string = $string . "0x" . $valeurTableauPalette[$i];
if ($i != $taillePalette-1)
$string = $string . ", ";
}
$string = AjoutRetourLigne( AjoutRetourLigne( AjoutRetourLigne($string) . "};" ) );
}
return $string;
}
function composer_nom_sprite($i, $nbImage, $name)
{
if ($_POST["nameC"] === "persona")
{
$name = $_POST["namePersC"];
if ($nbImage != 1)
$name = $name."$i";
return $name;
}
else
return $name;
}
function codeSprite($listeImage)
{
$nbImage = count ($listeImage);
$string = "";
$tableauPonteur = array();
$tableauPonteurPalette = array();
for ($i = 0 ; $i < $nbImage ; $i++)
{
$width = imagesx($listeImage[$i]["pict"]);
$height = imagesy($listeImage[$i]["pict"]);
if ($_POST["langage"] === "C/C++")
{
$name = composer_nom_sprite($i, $nbImage, $listeImage[$i]["name"]);
$nameSav = $name;
if ($_POST["calculatriceC"] === "Prizm/Cg-10/Cg-20")
{
if ($_POST["coupeSprC"] === "non")
{
$string = $string . codeSpriteC_CG20(0, 0, $width, $height, $listeImage[$i]["pict"], $name, $width+1, $height+1);
array_push($tableauPonteur, $name);
array_push($tableauPonteurPalette, $name."_palette");
}
else
{
$num = 0;
for ($y=0 ; $y < $height ; $y += $_POST["outil_largSprC"])
{
for ($x=0 ; $x < $width ; $x += $_POST["outil_longSprC"])
{
$name = $nameSav."$num";
$string = $string . codeSpriteC_CG20($x, $y, $_POST["outil_longSprC"], $_POST["outil_largSprC"], $listeImage[$i]["pict"], $name, $width, $height);
array_push($tableauPonteur, $name);
array_push($tableauPonteurPalette, $name."_palette");
$num++;
}
}
}
}
else
{
if ($_POST["coupeSprC"] === "non")
{
if ($_POST["couleur75"] === "n")
{
$string = $string . codeSpriteC_G75(0, 0, $width, $height, $listeImage[$i]["pict"], $name, $width, $height);
array_push($tableauPonteur, $name);
}
else
{
$string = $string . codeSpriteC_G75(0, 0, $width, $height, creerImage_gris($listeImage[$i]["pict"], true) , $name."_light", $width+1, $height+1);
array_push($tableauPonteur, $name."_light");
$string = $string . codeSpriteC_G75(0, 0, $width, $height, creerImage_gris($listeImage[$i]["pict"], false) , $name."_dark", $width+1, $height+1);
array_push($tableauPonteur, $name."_dark");
}
}
else
{
$num = 0;
for ($y=0 ; $y < $height ; $y += $_POST["outil_largSprC"])
{
for ($x=0 ; $x < $width ; $x += $_POST["outil_longSprC"])
{
if ($_POST["couleur75"] === "g")
{
$name = $nameSav."$num";
$string = $string . codeSpriteC_G75($x, $y, $_POST["outil_longSprC"], $_POST["outil_largSprC"], creerImage_gris($listeImage[$i]["pict"], true) , $name."_light", $width, $height);
array_push($tableauPonteur, $name."_light");
$string = $string . codeSpriteC_G75($x, $y, $_POST["outil_longSprC"], $_POST["outil_largSprC"], creerImage_gris($listeImage[$i]["pict"], false) , $name."_dark", $width, $height);
array_push($tableauPonteur, $name."_dark");
}
else
{
$name = $nameSav."$num";
$string = $string . codeSpriteC_G75($x, $y, $_POST["outil_longSprC"], $_POST["outil_largSprC"], $listeImage[$i]["pict"] , $name, $width, $height);
array_push($tableauPonteur, $name);
}
$num++;
}
}
}
}
}
else if ($_POST["langage"] === "Basic")
{
if ($_POST["coupeSprC"] === "non")
{
$string = $string . codeSpriteBasic(0, 0, $width, $height, $listeImage[$i]["pict"], $width, $height);
}
else
{
$num = 0;
for ($x=0 ; $x < $width ; $x += $_POST["outil_longSprC"])
{
for ($y=0 ; $y < $height ; $y += $_POST["outil_largSprC"])
{
$string = $string . codeSpriteBasic($x, $y, $_POST["outil_longSprC"], $_POST["outil_largSprC"], $listeImage[$i]["pict"] , $width, $height);
$num++;
}
}
}
}
else
{
$name = composer_nom_sprite($i, $nbImage, $listeImage[$i]["name"]);
if ($_POST["coupeSprC"] === "non")
{
$string = $string . codeSpriteLua(0, 0, $width, $height, $listeImage[$i]["pict"],$name, $width, $height);
}
else
{
$num = 0;
for ($x=0 ; $x < $width ; $x += $_POST["outil_longSprC"])
{
for ($y=0 ; $y < $height ; $y += $_POST["outil_largSprC"])
{
$string = $string . codeSpriteLua($x, $y, $_POST["outil_longSprC"], $_POST["outil_largSprC"], $listeImage[$i]["pict"] , $name, $width, $height);
$num++;
}
}
}
}
}
if ($_POST["tabpointeurC"] === "oui" && $_POST["langage"] === "C/C++")
$string = $string . genererTabPointeur($tableauPonteur, $tableauPonteurPalette);
return $string;
}