//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AdvGrid"
#pragma link "BaseGrid"
#pragma resource "*.dfm"
TForm1 *Form1;
byte *buffer;
int buffersize;
byte *buffer2;
int buffer2size;

int mapx, mapy;
const int cobjNum = 1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    mapx = mapy = 0;
    for (int i=0;i<241;i++) {
        for (int j=0;j<128;j++) {
            tleData[i][j].loaded = false;
            tleData[i][j].imageloaded = false;
        }
    }
    for (int i=0;i<1417;i++) {
        for (int j=0;j<128;j++) {
            objData[i][j].loaded = false;
            objData[i][j].imageloaded = false;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if (OpenDialog1->Execute()) {
        Screen->Cursor = crHourGlass;
        FILE *f;
        char c;
        int iTemp;
        AnsiString Value;

        f = fopen(OpenDialog1->FileName.c_str(),"rb");
        if (f == NULL) {
//            ShowMessage("Error Loading File");
            Screen->Cursor = crDefault;
            return;
        }
        iTemp = fgetc(f);
        Value = "";
        for (int i=0;i<iTemp;i++) {
            c = fgetc(f);
            Value = Value + AnsiString(c);
        }
        Memo1->Lines->Clear();
        Memo1->Lines->Add(Value);
        width = fgetc(f);
        width += fgetc(f) << 8;
        width += fgetc(f) << 16;
        width += fgetc(f) << 24;
        Memo1->Lines->Add("Tiles across: " + IntToStr(width));

        height = fgetc(f);
        height += fgetc(f) << 8;
        height += fgetc(f) << 16;
        height += fgetc(f) << 24;
        Memo1->Lines->Add("Tiles down: " + IntToStr(height));

        iTemp = fgetc(f);
        Memo1->Lines->Add("Unknown ??: " + IntToStr(iTemp));
        Value = "";
        if (buffer != NULL) {
            free(buffer);
            buffer = NULL;
        }
        buffer = new byte[iTemp];
        buffersize = iTemp;
        for (int i=0;i<iTemp;i++) {
            buffer[i] = fgetc(f);
            Value = Value + IntToStr(buffer[i]) + ", ";
        }
        Value = Value.SubString(0,Value.Length()-2);
        Memo1->Lines->Add(Value);

        map = fgetc(f);
        map += fgetc(f) << 8;
        map += fgetc(f) << 16;
        map += fgetc(f) << 24;
        Memo1->Lines->Add("Map Number: " + IntToStr(map));

        AdvStringGrid1->RowCount = (height*width)+1;
        AdvStringGrid1->Cells[0][0] = "??";
        AdvStringGrid1->Cells[1][0] = "??";
        AdvStringGrid1->Cells[2][0] = "??";
        AdvStringGrid1->Cells[3][0] = "??";
        AdvStringGrid1->Cells[4][0] = "??";
        AdvStringGrid1->Cells[5][0] = "??";
        AdvStringGrid1->Cells[6][0] = "??";
        AdvStringGrid1->Cells[7][0] = "??";
        AdvStringGrid1->Cells[8][0] = "??";
        int iRow = 1;
        iTemp = 0;
        iTemp = 0;
        int wcurrent, hcurrent;
        wcurrent = hcurrent = 0;
        while (hcurrent != height) {
            AdvStringGrid1->Cells[0][iRow] = IntToStr(int(hcurrent)) + " " + IntToStr(int(wcurrent));
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[1][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].obj1 = iTemp;
            if (iTemp%4 != 0) {
                Memo1->Lines->Add(IntToStr(int(hcurrent)) + " " + IntToStr(int(wcurrent)));
            }
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[2][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].obj2 = iTemp;
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[3][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].tle1 = iTemp;
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[4][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].tle2 = iTemp;
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[5][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].cursor1 = iTemp;
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[6][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].unk1 = iTemp;
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[7][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].unk2 = iTemp;
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[8][iRow] = IntToStr(iTemp);
            mapData[hcurrent][wcurrent].unk3 = iTemp;
            iRow++;
            wcurrent++;
            if (wcurrent == width) {
                wcurrent = 0;
                hcurrent++;
            }
        }
        fclose(f);
        Screen->Cursor = crDefault;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    SaveDialog1->FileName = OpenDialog1->FileName;
    if (SaveDialog1->Execute()) {
        Screen->Cursor = crHourGlass;
        FILE *f;
        char c;
        int iTemp;
        AnsiString Value;

        f = fopen(SaveDialog1->FileName.c_str(),"wb");
        if (f == NULL) {
//            ShowMessage("Error Loading File");
            Screen->Cursor = crDefault;
            return;
        }
        iTemp = 19;
        fwrite(&iTemp,1,1,f);
        Value = "RedMoon MapData 1.0";
        fwrite(Value.c_str(), Value.Length(), 1, f);
        fwrite(&width,4,1,f);
        fwrite(&height,4,1,f);
        fwrite(&buffersize,1,1,f);
        fwrite(buffer,buffersize,1,f);
        fwrite(&map,4,1,f);
        fwrite(&unk,4,1,f);
        fwrite(buffer2,buffer2size,1,f);

        for (int i=1;i<AdvStringGrid1->RowCount;i++) {
            iTemp = atoi(AdvStringGrid1->Cells[1][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[2][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[3][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[4][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[5][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[6][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[7][i].c_str());
            fwrite(&iTemp,1,1,f);
            iTemp = atoi(AdvStringGrid1->Cells[8][i].c_str());
            fwrite(&iTemp,1,1,f);
        }
        fclose(f);
        Screen->Cursor = crDefault;
    }
}
//---------------------------------------------------------------------------

void TForm1::loadtle(int filenum)
{
    if (tleData[filenum][0].loaded == true) {
        return;
    }
    Screen->Cursor = crHourGlass;
    FILE *f;
    char c;
    int iTemp;
    int rmdnum, count;
    AnsiString Value;

    Value.sprintf("%5d",filenum);
    Value = StringReplace(Value," ","0", TReplaceFlags() << rfReplaceAll);
    Value = "C:\\Redmoon\\Datas\\Tle\\tle" + Value + ".rmd";
    f = fopen(Value.c_str(),"rb");
    if (f == NULL) {
        Screen->Cursor = crDefault;
        return;
    }
    iTemp = fgetc(f);
    Value = "";
    for (int i=0;i<iTemp;i++) {
        c = fgetc(f);
        Value = Value + AnsiString(c);
    }
    rmdnum = fgetc(f);
    rmdnum += fgetc(f) << 8;
    rmdnum += fgetc(f) << 16;
    rmdnum += fgetc(f) << 24;

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);

    count = fgetc(f);
    count += fgetc(f) << 8;
    count += fgetc(f) << 16;
    count += fgetc(f) << 24;
    int iRow = 1;
    int items = 0;
    while (items <= count) {
        int iNum=0;
        iNum = fgetc(f);
        iNum += fgetc(f) << 8;
        iNum += fgetc(f) << 16;
        iNum += fgetc(f) << 24;
        for (int j=0;j<iNum;j++) {
            for (int i=1;i<13;i++) {
                iTemp = fgetc(f);
                iTemp += fgetc(f) << 8;
                iTemp += fgetc(f) << 16;
                iTemp += fgetc(f) << 24;
                switch (i) {
                    case 1:
                        tleData[filenum][items].xstart = iTemp;
                        break;
                    case 2:
                        tleData[filenum][items].ystart = iTemp;
                        break;
                    case 3:
                        tleData[filenum][items].xend = iTemp;
                        break;
                    case 4:
                        tleData[filenum][items].yend = iTemp;
                        break;
                    case 12:
                        tleData[filenum][items].tlenumber = iTemp;
                        break;
                }
            }
            iRow++;
        }
        tleData[filenum][items].loaded = true;
        items++;
    }
    fclose(f);
    Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------

void TForm1::loadobj(int filenum)
{
    if (objData[filenum][0].loaded == true) {
        return;
    }
    Screen->Cursor = crHourGlass;
    FILE *f;
    char c;
    int iTemp;
    int rmdnum, count;
    AnsiString Value;

    Value.sprintf("%5d",filenum);
    Value = StringReplace(Value," ","0", TReplaceFlags() << rfReplaceAll);
    Value = "C:\\Redmoon\\Datas\\obj\\obj" + Value + ".rmd";
    f = fopen(Value.c_str(),"rb");
    if (f == NULL) {
//        ShowMessage("Error Loading File");
        Screen->Cursor = crDefault;
        return;
    }
    iTemp = fgetc(f);
    Value = "";
    for (int i=0;i<iTemp;i++) {
        c = fgetc(f);
        Value = Value + AnsiString(c);
    }
    rmdnum = fgetc(f);
    rmdnum += fgetc(f) << 8;
    rmdnum += fgetc(f) << 16;
    rmdnum += fgetc(f) << 24;

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);
    iTemp += fgetc(f) << 8;
    iTemp += fgetc(f) << 16;
    iTemp += fgetc(f) << 24;

    iTemp = fgetc(f);

    count = fgetc(f);
    count += fgetc(f) << 8;
    count += fgetc(f) << 16;
    count += fgetc(f) << 24;

    int iRow = 1;
    int items = 0;
    while (items <= count) {
        int iNum=0;
        iNum = fgetc(f);
        iNum += fgetc(f) << 8;
        iNum += fgetc(f) << 16;
        iNum += fgetc(f) << 24;
        for (int j=0;j<iNum;j++) {
            for (int i=1;i<13;i++) {
                iTemp = fgetc(f);
                iTemp += fgetc(f) << 8;
                iTemp += fgetc(f) << 16;
                iTemp += fgetc(f) << 24;
                if (j == cobjNum) {
                    switch (i) {
                        case 1:
                            objData[filenum][items].xstart = iTemp;
                            break;
                        case 2:
                            objData[filenum][items].ystart = iTemp;
                            break;
                        case 3:
                            objData[filenum][items].xend = iTemp;
                            break;
                        case 4:
                            objData[filenum][items].yend = iTemp;
                            break;
                        case 5:
                            objData[filenum][items].xOffset = iTemp;
                            break;
                        case 6:
                            objData[filenum][items].yOffset = iTemp;
                            break;
                        case 12:
                            objData[filenum][items].tlenumber = iTemp;
                            break;
                    }
                }
            }
            iRow++;
        }
        objData[filenum][items].loaded = true;
        items++;
    }
    fclose(f);
    Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------

void TForm1::loadtlelst(void)
{
    FILE *f;
    char *buff, *bptr;
    long flength;
    AnsiString Value; //temp string value
    AnsiString Version; //temp string value
    int t1 = 0; //temp int value
    int t2 = 0; //temp int value

    Value = "C:\\Redmoon\\RLEs\\tle.lst";
    f = fopen(Value.c_str(),"rb");
    if (f == NULL) {
        return;
    }

    fseek(f,0,SEEK_END);  // seek to end
    flength = ftell(f);        // get length of file in bytes
    buff = new char[flength+1];   // allocate full size of file
    if (buff==NULL) {
        fclose(f);
        ShowMessage("Not enough Memory");
        return;
    }
    fseek(f,0,SEEK_SET);  // rewind to beginning
    fread(buff,1,flength,f);   // grab whole file into buff[]
    fclose(f);
    buff[flength]=0; // ensure null terminator at eof
    bptr = buff;
    int ilength = *bptr++;
    Value = "";
    for (int j=0;j<ilength;j++) {
        Value = Value + AnsiString(*bptr++);
    }
    ilength = *bptr++;
    Version = "";
    for (int j=0;j<ilength;j++) {
        Version = Version + AnsiString(*bptr++);
    }
    t1 = (*bptr++ & 0xFF);
    t1 |= (*bptr++ & 0xFF) << 0x08;
    t1 |= (*bptr++ & 0xFF) << 0x10;
    t1 |= (*bptr++ & 0xFF) << 0x18;
    t1 = (*bptr++ & 0xFF);
    t1 |= (*bptr++ & 0xFF) << 0x08;
    t1 |= (*bptr++ & 0xFF) << 0x10;
    t1 |= (*bptr++ & 0xFF) << 0x18;
    int total = t1;
    //166186
    //13984
    for (int i=0; i<total; i++) {
        int tilenum,filenum,index;
        ilength = *bptr++;
        Value = "";
        for (int j=0;j<ilength;j++) {
            Value = Value + AnsiString(*bptr++);
        }
        //Name
        t1 = (*bptr++ & 0xFF);          t1 |= (*bptr++ & 0xFF) << 0x08;
        t1 |= (*bptr++ & 0xFF) << 0x10; t1 |= (*bptr++ & 0xFF) << 0x18;
        tilenum = t1;
        //TileNum
        t2 = (*bptr++ & 0xFF);          t2 |= (*bptr++ & 0xFF) << 0x08;
        t2 |= (*bptr++ & 0xFF) << 0x10; t2 |= (*bptr++ & 0xFF) << 0x18;
        filenum = t2;
        //FileNum
        t1 = (*bptr++ & 0xFF);          t1 |= (*bptr++ & 0xFF) << 0x08;
        t1 |= (*bptr++ & 0xFF) << 0x10; t1 |= (*bptr++ & 0xFF) << 0x18;
        index = t1;
        //Index
        for (int tlei=0;tlei<241;tlei++) {
            for (int tlej=0;tlej<128;tlej++) {
                if (tleData[tlei][tlej].tlenumber == tilenum) {
                    if (tleData[tlei][tlej].loaded == true) {
                        tleData[tlei][tlej].filename = "C:\\Projects\\mmview\\tle\\" + Value + ".bmp";
                    }
                }
            }
        }
        if (Version == "1.2") {
            t1 = (*bptr++ & 0xFF);          t1 |= (*bptr++ & 0xFF) << 0x08;
            t1 |= (*bptr++ & 0xFF) << 0x10; t1 |= (*bptr++ & 0xFF) << 0x18;
        }
    }
    free(buff);
}
//---------------------------------------------------------------------------

void TForm1::loadobjlst(void)
{
    FILE *f;
    char *buff, *bptr;
    long flength;
    AnsiString Value; //temp string value
    AnsiString Version; //temp string value
    int t1 = 0; //temp int value
    int t2 = 0; //temp int value

    Value = "C:\\Redmoon\\RLEs\\obj.lst";
    f = fopen(Value.c_str(),"rb");
    if (f == NULL) {
        return;
    }

    fseek(f,0,SEEK_END);  // seek to end
    flength = ftell(f);        // get length of file in bytes
    buff = new char[flength+1];   // allocate full size of file
    if (buff==NULL) {
        fclose(f);
        ShowMessage("Not enough Memory");
        return;
    }
    fseek(f,0,SEEK_SET);  // rewind to beginning
    fread(buff,1,flength,f);   // grab whole file into buff[]
    fclose(f);
    buff[flength]=0; // ensure null terminator at eof
    bptr = buff;
    int ilength = *bptr++;
    Value = "";
    for (int j=0;j<ilength;j++) {
        Value = Value + AnsiString(*bptr++);
    }
    ilength = *bptr++;
    Version = "";
    for (int j=0;j<ilength;j++) {
        Version = Version + AnsiString(*bptr++);
    }
    t1 = (*bptr++ & 0xFF);
    t1 |= (*bptr++ & 0xFF) << 0x08;
    t1 |= (*bptr++ & 0xFF) << 0x10;
    t1 |= (*bptr++ & 0xFF) << 0x18;
    t1 = (*bptr++ & 0xFF);
    t1 |= (*bptr++ & 0xFF) << 0x08;
    t1 |= (*bptr++ & 0xFF) << 0x10;
    t1 |= (*bptr++ & 0xFF) << 0x18;
    int total = t1;
    //166186
    //13984
    for (int i=0; i<total; i++) {
        int tilenum,filenum,index;
        ilength = *bptr++;
        Value = "";
        for (int j=0;j<ilength;j++) {
            Value = Value + AnsiString(*bptr++);
        }
        //Name
        t1 = (*bptr++ & 0xFF);          t1 |= (*bptr++ & 0xFF) << 0x08;
        t1 |= (*bptr++ & 0xFF) << 0x10; t1 |= (*bptr++ & 0xFF) << 0x18;
        tilenum = t1;
        //TileNum
        t2 = (*bptr++ & 0xFF);          t2 |= (*bptr++ & 0xFF) << 0x08;
        t2 |= (*bptr++ & 0xFF) << 0x10; t2 |= (*bptr++ & 0xFF) << 0x18;
        filenum = t2;
        //FileNum
        t1 = (*bptr++ & 0xFF);          t1 |= (*bptr++ & 0xFF) << 0x08;
        t1 |= (*bptr++ & 0xFF) << 0x10; t1 |= (*bptr++ & 0xFF) << 0x18;
        index = t1;
        //Index
        for (int tlei=0;tlei<1417;tlei++) {
            for (int tlej=0;tlej<128;tlej++) {
                if (objData[tlei][tlej].tlenumber == tilenum) {
                    if (objData[tlei][tlej].loaded == true) {
                        objData[tlei][tlej].filename = "C:\\Projects\\mmview\\obj\\" + Value + ".bmp";
                    }
                }
            }
        }
        if (Version == "1.2") {
            t1 = (*bptr++ & 0xFF);          t1 |= (*bptr++ & 0xFF) << 0x08;
            t1 |= (*bptr++ & 0xFF) << 0x10; t1 |= (*bptr++ & 0xFF) << 0x18;
        }
    }
    free(buff);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    Memo1->Lines->Add("Loading RMD Data Files");
    Application->ProcessMessages();
    for (int i=0;i<height;i++) {
        for (int j=0;j<width;j++) {
            int filenum = 0;
            filenum = (int)(mapData[i][j].tle2 * 2);
            filenum += (int)(mapData[i][j].tle1 / 128);
            if (filenum < 241) {
                loadtle(filenum);
            }
            filenum = (int)(mapData[i][j].obj1/4);
            filenum += (int)(mapData[i][j].obj2 % 32)*64;
            if ((filenum > 0) && (filenum < 1417)) {
                loadobj(filenum);
            }
        }
    }
    Memo1->Lines->Add("Loading Til LST and RLE Files");
    Application->ProcessMessages();
    loadtlelst();
    Memo1->Lines->Add("Loading Obj LST and RLE Files");
    Application->ProcessMessages();
    loadobjlst();
    Memo1->Lines->Add("Drawing Map on Image");
    Application->ProcessMessages();
    Form2->Image1->Canvas->Brush->Color = clBlack;
    Form2->Image1->Canvas->Rectangle(0,0,Form2->Image1->Width,Form2->Image1->Height);
    int xOffset = 0;
    int yOffset = 0;
    int xStart, yStart;
    xStart = atoi(Edit1->Text.c_str());
    yStart = atoi(Edit2->Text.c_str());
    //load tiles
    for (int i=xStart;i<height;i++) {
        for (int j=yStart;j<width;j++) {
            if (xOffset < Form2->Image1->Width) {
                if (yOffset < Form2->Image1->Height) {
                    int fnum, inum;
                    fnum = (int)(mapData[i][j].tle2 * 2);
                    fnum += (int)(mapData[i][j].tle1 / 128);
                    inum = (int)(mapData[i][j].tle1 % 128)*8;
                    inum += (int)(mapData[i][j].obj2 / 32);

                    Graphics::TBitmap *Bitmap;

                    TRect   MyRect, MyOther;

                    MyRect = Rect(xOffset,yOffset,xOffset+(tleData[fnum][inum].xend-tleData[fnum][inum].xstart),yOffset+(tleData[fnum][inum].yend-tleData[fnum][inum].ystart));

                    MyOther = Rect(tleData[fnum][inum].xstart,tleData[fnum][inum].ystart,tleData[fnum][inum].xend, tleData[fnum][inum].yend);
                    if (tleData[fnum][inum].filename != "") {
                        Bitmap = new Graphics::TBitmap;
                        Bitmap->LoadFromFile(tleData[fnum][inum].filename);
                        Form2->Image1->Canvas->BrushCopy(MyRect, Bitmap, MyOther, RGB(255,0,255));
                        delete Bitmap;
                    }
                }
            }
            xOffset += 48;
        }
        xOffset = 0;
        yOffset += 24;
    }
    //load objects
    xOffset = 0;
    yOffset = 0;
    for (int i=xStart;i<height;i++) {
        for (int j=yStart;j<width;j++) {
            int fnum, inum;
            fnum = (int)(mapData[i][j].obj1/4);
            fnum += (int)(mapData[i][j].obj2 % 32)*64;
            inum = (int)(mapData[i][j].unk3)*2;
            inum += (int)(mapData[i][j].unk2 / 128);

            if (fnum != 0) {
                if (xOffset < Form2->Image1->Width) {
                    if (yOffset < Form2->Image1->Height) {

                        Graphics::TBitmap *Bitmap;

                        TRect   rDest, rSource;
                        int lwidth, lheight;
                        lheight = objData[fnum][inum].yend-objData[fnum][inum].ystart;
                        lwidth = objData[fnum][inum].xend-objData[fnum][inum].xstart;

                        rSource = Rect(objData[fnum][inum].xstart,objData[fnum][inum].ystart,objData[fnum][inum].xend, objData[fnum][inum].yend);
                        rDest   = Rect(xOffset,yOffset,xOffset+width,yOffset+height);

                        if (objData[fnum][inum].filename != "") {
                            Bitmap = new Graphics::TBitmap;
                            Bitmap->LoadFromFile(objData[fnum][inum].filename);
                            for (int ii=0;ii<lheight;ii++) {
                                for (int jj=0;jj<lwidth;jj++) {
                                    if (Bitmap->Canvas->Pixels[objData[fnum][inum].xstart+jj][objData[fnum][inum].ystart+ii] != RGB(255,0,255)) {
                                        if ((Bitmap->Canvas->Pixels[objData[fnum][inum].xstart+jj][objData[fnum][inum].ystart+ii] != -1) &&
                                            (Bitmap->Canvas->Pixels[objData[fnum][inum].xstart+jj][objData[fnum][inum].ystart+ii] != 0)) {
                                            Form2->Image1->Canvas->Pixels[xOffset+jj][yOffset+ii] = Bitmap->Canvas->Pixels[objData[fnum][inum].xstart+jj][objData[fnum][inum].ystart+ii];
//                                            Form2->Image1->Canvas->Pixels[xOffset+jj+objData[fnum][inum].xOffset][yOffset+ii+objData[fnum][inum].yOffset] = Bitmap->Canvas->Pixels[objData[fnum][inum].xstart+jj][objData[fnum][inum].ystart+ii];
                                        }
                                    }
                                }
                            }
//                            Form2->Image1->Canvas->BrushCopy(rDest, Bitmap, rSource, RGB(255,0,255));
//                            Form2->Image1->Canvas->CopyMode = cmSrcCopy;
//                            Form2->Image1->Canvas->CopyRect(rDest, Bitmap->Canvas, rSource);
                            delete Bitmap;
                        }
                    }
                }
            }
            xOffset += 48;
        }
        xOffset = 0;
        yOffset += 24;
    }
    Form2->Show();
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Button4Click(TObject *Sender)
{
    Screen->Cursor = crHourGlass;
    FILE *f;
    char c;
    int iTemp;
    AnsiString Value;

    char tmp[100];
    sprintf(tmp,"C:\\Projects\\Bin\\Data\\Map\\map%5d.smm",++map);
    for (int i = 0; i < strlen(tmp); i++) {
        if (tmp[i] == ' ') {
            tmp[i] = '0';
        }
    }
    f = fopen(tmp,"rb");
    if (f == NULL) {
//            ShowMessage("Error Loading File");
        Screen->Cursor = crDefault;
        return;
    }
    iTemp = fgetc(f);
    Value = "";
    for (int i=0;i<iTemp;i++) {
        c = fgetc(f);
        Value = Value + AnsiString(c);
    }
    Memo1->Lines->Clear();
    Memo1->Lines->Add(Value);
    width = fgetc(f);
    width += fgetc(f) << 8;
    width += fgetc(f) << 16;
    width += fgetc(f) << 24;
    Memo1->Lines->Add("Tiles across: " + IntToStr(width));

    height = fgetc(f);
    height += fgetc(f) << 8;
    height += fgetc(f) << 16;
    height += fgetc(f) << 24;
    Memo1->Lines->Add("Tiles down: " + IntToStr(height));

    iTemp = fgetc(f);
    Memo1->Lines->Add("Unknown ??: " + IntToStr(iTemp));
    Value = "";
    if (buffer != NULL) {
        free(buffer);
        buffer = NULL;
    }
    buffer = new byte[iTemp];
    buffersize = iTemp;
    for (int i=0;i<iTemp;i++) {
        buffer[i] = fgetc(f);
        Value = Value + IntToStr(buffer[i]) + ", ";
    }
    Value = Value.SubString(0,Value.Length()-2);
    Memo1->Lines->Add(Value);

    map = fgetc(f);
    map += fgetc(f) << 8;
    map += fgetc(f) << 16;
    map += fgetc(f) << 24;
    Memo1->Lines->Add("Map Number: " + IntToStr(map));

    AdvStringGrid1->RowCount = (height*width)+1;
    AdvStringGrid1->Cells[0][0] = "X/Y";
    AdvStringGrid1->Cells[1][0] = "obj";
    AdvStringGrid1->Cells[2][0] = "tlei*32+obj2";
    AdvStringGrid1->Cells[3][0] = "tlei/8+tlef%2";
    AdvStringGrid1->Cells[4][0] = "tlef/2";
    AdvStringGrid1->Cells[5][0] = "cursor";
    AdvStringGrid1->Cells[6][0] = "??";
    AdvStringGrid1->Cells[7][0] = "??";
    AdvStringGrid1->Cells[8][0] = "??";
    int iRow = 1;
    iTemp = 0;
    iTemp = 0;
    int wcurrent, hcurrent;
    wcurrent = hcurrent = 0;
    while (hcurrent != height) {
        AdvStringGrid1->Cells[0][iRow] = IntToStr(int(hcurrent)) + " " + IntToStr(int(wcurrent));
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[1][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].obj1 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[2][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].obj2 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[3][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].tle1 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[4][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].tle2 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[5][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].cursor1 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[6][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].unk1 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[7][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].unk2 = iTemp;
        iTemp = fgetc(f);
        AdvStringGrid1->Cells[8][iRow] = IntToStr(iTemp);
        mapData[hcurrent][wcurrent].unk3 = iTemp;
        iRow++;
        wcurrent++;
        if (wcurrent == width) {
            wcurrent = 0;
            hcurrent++;
        }
    }
    fclose(f);
    Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------

