从文件构造对象不工作(使用struct中的数据构建工作正常)(C ++)

时间:2013-07-15 08:17:56

标签: c++ file-io

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

最初我用程序中其他地方定义的结构数据构建所有图像,但事实证明,对于我第一次练习图像之后的任何事情来说效率都很低,所以我尝试创建一个能够从文本中读取所需数据的构造函数而是文件。文本文件的格式为:

宽度

高度

[chars]

[颜色]

其中字符和颜色只是由宽度*高度大小的空格分隔的数组(如果您感兴趣,可以在下面粘贴完整图像文件)。然而,当我调用这个构造函数时,出现了问题 - 在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的定义。

0 个答案:

没有答案