⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spritegrab.cpp

📁 symbian 的一个 二维飞行游戏 源码 及相关技术文章
💻 CPP
字号:
// Copyright 2002 Kenneth Guy,
// 

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>


char hex(int i) {
  switch(i) {
  case 0:
    return '0';
  case 1:
    return '1';
  case 2:
    return '2';
  case 3:
    return '3';
  case 4:
    return '4';
  case 5:
    return '5';
  case 6:
    return '6';
  case 7:
    return '7';
  case 8:
    return '8';
  case 9:
    return '9';
  case 10:
    return 'a';
  case 11:
    return 'b';
  case 12:
    return 'c';
  case 13:
    return 'd';
  case 14:
    return 'e';
  case 15:
    return 'f';
  default:
    cerr << "hex passed no > 15\n";
    exit(-1);
  }  
}


main(int argc, char** argv) {
  bool binary;
  bool mask_binary;
  int offset=0;
  int bulletno=1;

  if(argc<2) {
    cerr << "syntax spriteGrab arrayName file1.pnm file2.pnm ...\n";
    std::exit(-1);
  }

  int param=1;
  string name(argv[param]);
  if(name=="-noheader") {
    param++;
    name=string(argv[param]);
  } else {
    cout << "extern const TUint16 " << name << "[]={\n";
  }

  param++;

  cout << "// " << argc-param << " sprites\n";
  for(int file=param;file < argc ; file++) {
    std::ifstream from(argv[file]);

    if(!from) {
      cerr << "Couldn't open " << argv[file] << "\n";
      std::exit(-1);
    }

    string maskname= string(argv[file]).insert(0,"mask_");
    std::ifstream from_mask(maskname.c_str());

    cout << "\n\n// Sprite " << argv[file] << " no " << (file-2) << "\n";
    cout << "// offset " << offset << "\n";
    string s; 
    from >> s;

    if(s!="P6") {
      cerr << "Can only handle binary pnm files " << argv[file] << "\n";
      std::exit(-1);      
    }

    int xsize,ysize,maxval;
    from >> s;
    if(s[0]=='#') {
      char c=' ';
      while(c!='\n') {
        from.get(c);
      }
      from >> xsize;
    } else {
      xsize = atoi(s.c_str());
    }
    from >> ysize >> maxval;
    if(maxval!=255) {
      cerr << "Can only handle 24bit binary pnm " << argv[file] << "\n";
      std::exit(-1);      
    }
    cout << "// " << xsize << "x" << ysize <<"\n";
    char newline;
    from.get(newline);

    if(from_mask) {
      from_mask >> s;
      if(s!="P6") {
        cerr << s << "\n";
        cerr << "Can only handle binary pnm files " << maskname << "\n";
        std::exit(-1);      
      }

      int mask_xsize,mask_ysize,mask_maxval;
      from_mask >> s;
      if(s[0]=='#') {
        char c=' ';
        while(c!='\n') {
          from_mask.get(c);
        }
        from_mask >> mask_xsize;
      } else {
        mask_xsize = atoi(s.c_str());
      }
      from_mask >> mask_ysize >> mask_maxval;
      if(mask_xsize!=xsize || mask_ysize!=ysize) {
        cerr << "mask size doesn't match sprite size " << maskname << "\n";
        std::exit(-1);      
      }
      if(mask_maxval!=255) {
        cerr << "Can only handle 24bit binary pnm" << maskname << "\n";
        std::exit(-1);      
      }
      cout << "// mask " << maskname << "\n";
      from_mask.get(newline);
    }
    bool bullet=false;
    for(int j=1;j<=ysize ;j++) {
      for(int i=1;i<=xsize ; i++) {
        int r,g,b;        
        int m_r,m_g,m_b;        
        unsigned char c;
        from.get(c);
        r = (int) c; 
        from.get(c);
        g = (int) c; 
        from.get(c);
        b = (int) c;

        if(from_mask) {
          from_mask.get(c);
          m_r = (int) c; 
          from_mask.get(c);
          m_g = (int) c; 
          from_mask.get(c);
          m_b = (int) c;

          // generate mask, for each 32 bit value (ie two pixels)
          // we have 2 12 bit colour values, and two 4 bit mask
          // values, bottom 4 mask bits are used for bullet numbers
          // top four are used for landscape/ship etc.
          
          // red is landscape,
          // green is ship extension
          // blue is ship
          // white is bullets

          if(m_r>220 && m_g<20 && m_b<20) {
            if(i%2==0)
              cout << "0x8"; //LandScape
            else
              cout << "0x0"; // bottom bits so ignore this
          } else if(m_r<20 && m_g>220 && m_b<20) {
            if(i%2==0)
              cout << "0x4"; //ShipExtension
            else
              cout << "0x0"; // bottom bits so ignore this
          } else if(m_r<20 && m_g<20 && m_b>220) {
            if(i%2==0)
              cout << "0x2"; //Ship
            else
              cout << "0x0"; // bottom bits so ignore this
          } else if(m_r>220 && m_g>220 && m_b>220) {
            if(i%2==0)
              cout  << "0x0"; // top bits so ignore this
            else {
              bullet=true;
              // each of the fifteen bullets gets a different number
              // we also loop so if we can have different images for the same
              // bullet
              cout << "0x" << hex(bulletno);
            }
          } else {
            cout << "0x0";
          }
        } else {
          // no mask file provided
          cout << "0x0";
        }
        cout << hex(r>>4) << hex(g >> 4) << hex(b >> 4);
        offset+=2;
        if(j!=ysize || i!=xsize || file!=(argc-1))
          cout << ", ";
      }
      cout << "\n";
    }
    if(bullet==true)
      bulletno = bulletno==8 ? 1 : bulletno+1;
  }
  cout << "\n};\n";
  cout << "const TInt " << name << "Size=" << offset << ";\n\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -