1

我目前正在为 Windows 控制台编写一个 ASCII 游戏引擎,我正在使用的类之一是 Image 类,它的数据成员包括宽度、高度和指向动态分配的无符号字符数组的指针,这些数组代表字符和图像的颜色数据。

最初,我使用程序中其他地方定义的结构的数据来构建所有图像,但事实证明,这对于我的前几张练习图像之后的任何事情都非常低效,因此我尝试创建一个可以从文本文件中读取所需数据的构造函数。文本文件的格式为:

宽度

高度

[字符]

[颜色]

其中字符和颜色只是由宽度 * 高度大小的空格分隔的数组(如果您感兴趣,请在下面粘贴完整的图像文件)。然而,当我调用这个构造函数时,出现了问题——在 Eclipse 的调试模式下,看起来一点也不奇怪,实际上它看起来就像在复制字符和颜色数组一样好,但是执行程序并没有绘制图像被要求时显示在屏幕上。

构造函数的原始版本如下所示:

Image::Image(const int w, const int h, const CHAR *chrs, const COL *cols) :width(w), height(h) {
    chars = new CHAR[w * h];
    colours = new COL[w * h];
    for (int i = 0; i < w * h; ++i) {
        chars[i] = chrs[i];
        colours[i] = cols[i];
    }
}

并被称为:

Image *sun_image = new Image(Sun.width, Sun.height, Sun.chars, Sun.colours);

Sun 结构定义如下:

Sun_s Sun =
{
  SUNW,
  SUNH,
  {
    255,255,255,255,255,255,255,177,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,177,255,255,255,177,255,255,255,177,255,255,255,
    255,255,255,255,177,255,255,255,255,255,177,255,255,255,255,
    255,255,255,255,255,255,177,177,177,255,255,255,255,255,255,
    255,255,255,255,255,177,178,219,178,177,255,255,255,255,255,
    255,255,255,255,177,178,219,219,219,178,177,255,255,255,255,
    177,255,177,255,177,219, 94,219, 94,219,177,255,177,255,177,
    255,255,255,255,177,178,219,126,219,178,177,255,255,255,255,
    255,255,255,255,255,177,178,219,178,177,255,255,255,255,255,
    255,255,255,255,255,255,177,177,177,255,255,255,255,255,255,
    255,255,255,255,177,255,255,255,255,255,177,255,255,255,255,
    255,255,255,177,255,255,255,177,255,255,255,177,255,255,255,
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,177,255,255,255,255,255,255,255,
  },
  {
      0,  0,  0,  0,  0,  0,  0, 62,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0, 62,  0,  0,  0, 62,  0,  0,  0, 62,  0,  0,  0,
      0,  0,  0,  0, 62,  0,  0,  0,  0,  0, 62,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0, 62, 62, 62,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0, 62, 62, 14, 62, 62,  0,  0,  0,  0,  0,
      0,  0,  0,  0, 62, 62, 14, 14, 14, 62, 62,  0,  0,  0,  0,
     62,  0, 62,  0, 62, 14,224, 14,224, 14, 62,  0, 62,  0, 62,
      0,  0,  0,  0, 62, 62, 14,224, 14, 62, 62,  0,  0,  0,  0,
      0,  0,  0,  0,  0, 62, 62, 14, 62, 62,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0, 62, 62, 62,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0, 62,  0,  0,  0,  0,  0, 62,  0,  0,  0,  0,
      0,  0,  0, 62,  0,  0,  0, 62,  0,  0,  0, 62,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
      0,  0,  0,  0,  0,  0,  0, 62,  0,  0,  0,  0,  0,  0,  0,
  }
};

尝试的新版本的构造函数如下所示:

Image::Image(const char *filename) {
    std::ifstream inputFile;
    inputFile.open(filename, std::ios::in);

    if (inputFile.is_open()) {
        inputFile >> width;
        inputFile >> height;
        chars = new CHAR[width * height];
        colours = new CHAR[width * height];

    int temp, i;
    for (i = 0; i < height * width; ++i) {
        inputFile >> temp;
        chars[i] = (CHAR)temp;
    }

    for (i = 0; i < width * height; ++i) {
        inputFile >> temp;
        colours[i] = (CHAR)temp;
    }
    } else {
        width = 0;
        height = 0;
        chars = new CHAR[width * height];
        colours = new CHAR[width * height];
    }

inputFile.close();
}

调用如下所示:

    Image *sun_image = new Image("./artassets/sun.txt");

和一个 sun.txt 看起来像:

15
15

255 255 255 255 255 255 255 177 255 255 255 255 255 255 255 
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
255 255 255 177 255 255 255 177 255 255 255 177 255 255 255 
255 255 255 255 177 255 255 255 255 255 177 255 255 255 255 
255 255 255 255 255 255 177 177 177 255 255 255 255 255 255 
255 255 255 255 255 177 178 219 178 177 255 255 255 255 255 
255 255 255 255 177 178 219 219 219 178 177 255 255 255 255 
177 255 177 255 177 219  94 219  94 219 177 255 177 255 177 
255 255 255 255 177 178 219 126 219 178 177 255 255 255 255 
255 255 255 255 255 177 178 219 178 177 255 255 255 255 255 
255 255 255 255 255 255 177 177 177 255 255 255 255 255 255 
255 255 255 255 177 255 255 255 255 255 177 255 255 255 255 
255 255 255 177 255 255 255 177 255 255 255 177 255 255 255 
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
255 255 255 255 255 255 255 177 255 255 255 255 255 255 255
  0   0   0   0   0   0   0  62   0   0   0   0   0   0   0 
  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 
  0   0   0  62   0   0   0  62   0   0   0  62   0   0   0 
  0   0   0   0  62   0   0   0   0   0  62   0   0   0   0 
  0   0   0   0   0   0  62  62  62   0   0   0   0   0   0 
  0   0   0   0   0  62  62  14  62  62   0   0   0   0   0 
  0   0   0   0  62  62  14  14  14  62  62   0   0   0   0 
 62   0  62   0  62  14 224  14 224  14  62   0  62   0  62 
  0   0   0   0  62  62  14 224  14  62  62   0   0   0   0 
  0   0   0   0   0  62  62  14  62  62   0   0   0   0   0 
  0   0   0   0   0   0  62  62  62   0   0   0   0   0   0 
  0   0   0   0  62   0   0   0   0   0  62   0   0   0   0 
  0   0   0  62   0   0   0  62   0   0   0  62   0   0   0 
  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 
  0   0   0   0   0   0   0  62   0   0   0   0   0   0   0 

谁能指出我做错了什么?我的两个构造函数在我看来并没有什么不同......

编辑:忘了提一下,CHAR 和 COL 都只是为 unsigned char 定义的。

4

0 回答 0