PageRenderTime 64ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/demo/sprites/sprite2c.pl

https://github.com/andreas23/ZombieGotcha
Perl | 66 lines | 32 code | 16 blank | 18 comment | 5 complexity | 033da1ca32595995cb0744e559fac57f MD5 | raw file
  1. #!/usr/bin/perl
  2. # sprite2c.pl by Travis Goodspeed
  3. # This is an ugly hack for converting PNM sprites to C. I'd rather
  4. # use the GIMP's exporter, but it prefers 24-bit color, which can't be
  5. # afforded for embedded applications.
  6. # N.B., that this expects some GIMP-specific crap.
  7. print "// ZombieGotcha Sprite Translator \n\n";
  8. my $pnmtype, $line;
  9. #First, verify the filetype
  10. $pnmtype=<>;
  11. print STDERR "ERROR: Type is not P3. Use ASCII GIMP PNM in RGB!." if $pnmtype ne "P3\n";
  12. # P1 Portable bitmap ASCII
  13. # P2 Portable graymap ASCII
  14. # P3 Portable pixmap ASCII
  15. # P4 Portable bitmap Binary
  16. # P5 Portable graymap Binary
  17. # P6 Portable pixmap Binary
  18. #Next, find the file dimensions. Comments should be printed in C.
  19. do{
  20. $line=<>;
  21. chomp $line;
  22. print STDOUT " // $line\n" if $line=~m/^#/;
  23. }while($line=~m/^#/);
  24. #$line still has the dimensions, parse it.
  25. $line=~ s/ /,/;
  26. print " $line, //Width, Height\n";
  27. $max=<>;
  28. chomp $max;
  29. print " //Max $max\n\n";
  30. #Now handle all the pixels in B&W.
  31. my $atbit=0;
  32. my $byte=5;
  33. print " //Pixels\n";
  34. while (<STDIN>){
  35. chomp;
  36. $bit=int($_)?0:1;
  37. $byte=$byte>>1;;
  38. $byte=$byte|($bit?0x80:0);
  39. #Waste two bits.
  40. <STDIN>;
  41. <STDIN>;
  42. #Advance the bit counter and print byte if needed.
  43. $atbit++;
  44. if ($atbit%8 ==0) {
  45. $byte&=0xFF;
  46. printf(" 0x%02x,",$byte);
  47. print "\n" if $atbit%192==0;
  48. }
  49. }
  50. print "\n // done.\n";