dev-c ++的代码在visual studio 2013上不起作用

时间:2015-11-12 21:23:55

标签: c++ visual-studio visual-studio-2013 bitmap dev-c++

我正在用c ++和SFML编写一个小小的游戏,我被困在生成一个迷宫中,所以我在网上找到了很少的代码并根据我的需要调整并创建了一个.bmp文件的迷宫。现在我想在程序中读取它并将其更改为适合我的地图并保存到名为level.map的文件中。在位图阅读器中我基于来自互联网的一些代码并且它运行良好,但仅在dev-c ++ 5.11(64位和32位版本)上,但是当我在visual studio 2013上进行编译时,它在任何地方读取0值,而不是混合1和0。

我的代码:

#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <fstream>
#include <windows.h>


using namespace std;
struct FileHeader {
    short bfType;
    int bfSize;
    short bfReserved1;
    short bfReserved2;
    short bfOffBits;
};
FileHeader File;

struct PictureHeader {
    int biSize;
    int biWidth;
    int biHeight;
    short biPlanes;
    short biBitCount;
    int biCompression;
    int biSizeImage;
    int biXPelsPerMeter;
    int biYPelsPerMeter;
    int biClrUsed;
    int biClrImportant;
};
PictureHeader Picture;

struct RGBcolors {
    char R;
    char G;
    char B;
};
RGBcolors Rgb;
char dane;
string str;
string bottom = "111111111111111111111111111111111";
int main(int argc, char*argv[]) {


    FILE *f = fopen("Maze.bmp", "rb");

    system("cls");
    printf("\n Info about BITMAP\n\n");

    fread(&File.bfType, sizeof(File.bfType), 1, f);
    printf(" Typ: %x", File.bfType);

    fread(&File.bfSize, sizeof(File.bfSize), 1, f);
    printf("\n Size of file: %d bytes", File.bfSize);

    fread(&File.bfReserved1, sizeof(File.bfReserved1), 1, f);
    printf("\n Reserved2: %d", File.bfReserved1);

    fread(&File.bfReserved2, sizeof(File.bfReserved2), 1, f);
    printf("\n Reserved2: %d", File.bfReserved2);

    fread(&File.bfOffBits, sizeof(File.bfOffBits), 1, f);

    printf("\n");

    fseek(f, 14, SEEK_SET);
    fread(&Picture.biSize, sizeof(Picture.biSize), 1, f);
    printf("\n Size of header: %d", Picture.biSize);

    fread(&Picture.biWidth, sizeof(Picture.biWidth), 1, f);
    printf("\n Width: %d px", Picture.biWidth);

    fread(&Picture.biHeight, sizeof(Picture.biHeight), 1, f);
    printf("\n Heigh: %d px", Picture.biHeight);

    fread(&Picture.biPlanes, sizeof(Picture.biPlanes), 1, f);

    fread(&Picture.biBitCount, sizeof(Picture.biBitCount), 1, f);
    printf("\n Bits per pixel: %d (1, 4, 8, or 24)", Picture.biBitCount);

    fread(&Picture.biCompression, sizeof(Picture.biCompression), 1, f);
    printf("\n Compression: %d (0=none, 1=RLE-8, 2=RLE-4)", Picture.biCompression);

    fread(&Picture.biSizeImage, sizeof(Picture.biSizeImage), 1, f);
    printf("\n Size of only a image: %d", Picture.biSizeImage);

    fread(&Picture.biXPelsPerMeter, sizeof(Picture.biXPelsPerMeter), 1, f);
    fread(&Picture.biYPelsPerMeter, sizeof(Picture.biYPelsPerMeter), 1, f);
    fread(&Picture.biClrUsed, sizeof(Picture.biClrUsed), 1, f);
    fread(&Picture.biClrImportant, sizeof(Picture.biClrImportant), 1, f);


    fseek(f, File.bfOffBits, SEEK_SET);
    cout << "\n\n\n Structure:\n";


    int bmpImg;

    int c = 0;
    for (int i = File.bfOffBits; i < File.bfSize; i++)
    {

        fseek(f, i, SEEK_SET);
        fread(&bmpImg, 1, 1, f);
        if ((i - File.bfOffBits) % 96 == 0)
        {
            cout << endl;
            if (i != File.bfOffBits)
                str += "1\n";
            c++;
        }
        if ((i - File.bfOffBits) % 3 == 0) {
            printf("%d", (bmpImg == 255 ? 1 : 0));
            str += (bmpImg == 255 ? "1" : "0");
        }

    }
    printf("\n");

    fclose(f);
    str += "1\n";
    str += bottom;

    {

    }
    if (str.length() > 1)
    {
        FILE *plik = fopen("maze.map", "w");
        for (int i = 0; i < str.length(); i++)
        {
            fprintf(plik, "%c", str[i]);
        }


        fclose(plik);
    }


    return 0;
}

位图大小为32x32。 例如。 generated maze

来自dev-c ++的正确结果 enter image description here

视觉工作室的结果不正确: enter image description here

欣赏任何建议和指示

1 个答案:

答案 0 :(得分:0)

已解决

刚刚使用我从fread读取的任何值变量初始化。