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

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "Unit2.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void F2loadRLE(AnsiString FileName, int tNum)
{
    FILE *f;
    long flength;
    AnsiString Value; //temp string value
    int t1 = 0; //temp int value
    int t2 = 0; //temp int value
    int iSize;
    char tmp;

    f = fopen(FileName.c_str(),"rb");
    if (f == NULL) {
//        ShowMessage("Error Loading File");
        return;
    }

    fread(&tmp,1,1,f);
    Value = "";
    while (tmp != 0) {
        Value = Value + AnsiString(tmp);
        fread(&tmp,1,1,f);
    }
    fread(&t1,4,1,f);
    fread(&t1,4,1,f);
    int total = t1;
    int addr=0;
    for (int i=0; i<total; i++) {
        fread(&t1,1,4,f);
        if (i == tNum) {
            addr = t1;
        }
    }
    //LoadItem1
    if (addr == 0) {
        fclose(f);
        return;
    }
    fseek(f,addr,SEEK_SET);  // goto address
    fread(&iSize,4,1,f);
    int xPos;
    int yPos;
    fread(&t1,4,1,f);
    //t1 unknown = 11
    xPos = t1;
    fread(&t1,4,1,f);
    //t1 unknown = 14
    yPos = t1;
    fread(&t1,4,1,f);
    //t1 width = 13
    Form2->Image1->Picture->Bitmap->Width = t1;
    Form2->Image1->Width = t1;
    fread(&t1,4,1,f);
    //t1 height = 10
    Form2->Image1->Picture->Bitmap->Height = t1;
    Form2->Image1->Height = t1;
    Application->ProcessMessages();
    fread(&t1,4,1,f);
    //t1 unknown =0
    fread(&t1,4,1,f);
    //t1 unknown = 0
    fread(&t1,4,1,f);
    //t1 unknown = 2
    fread(&t1,1,1,f);
    fread(&t1,1,1,f);
    fread(&t1,1,1,f);
    fread(&t1,1,1,f);
    Form2->Image1->Canvas->Brush->Color = RGB(255,0,255);
    Form2->Image1->Canvas->Rectangle(0,0,Form2->Image1->Width,Form2->Image1->Height);
    Screen->Cursor = crHourGlass;
    try {
        xPos = yPos = 0;
        if (fread(&t1,1,1,f) == 0) {
            ShowMessage("Error reading from file");
            fclose(f);
            return;
        }
        while (t1 != EOF) {
            switch (t1) {
                case 0: //done???
                    Screen->Cursor = crDefault;
                    fclose(f);
                    return;
                case 1:
                    if (fread(&total,4,1,f) == 0) {
                        Screen->Cursor = crDefault;
                        fclose(f);
                        return;
                    }
                    if (total > 1024) {
                        Screen->Cursor = crDefault;
                        fclose(f);
                        return;
                    }
                    for (int j=0; j<total; j++) {
                        int tmp1=0,tmp2=0;
                        fread(&tmp1,1,1,f);
                        fread(&tmp2,1,1,f);

                        int tmpr, tmpg,tmpb;
                        tmpr = int(tmp2/2)*2;//254
                        tmpg = int(tmp2%8)*32;//224
                        tmpb = tmp1*8;//912
                        tmpb = tmpb+ (tmp1%8);//2 914
                        while (tmpb > 255) {//3
                            tmpg += 4;
                            tmpb -= 256;
                        }
                        tmpg = tmpg+(tmpg%2);
                        while (tmpg > 255) {
                            tmpr += 2;
                            tmpg -= 256;
                        }
                        tmpr = tmpr+(tmpr%2);
                        while (tmpr > 255) {
                            tmpr -= 256;
                        }
                        Form2->Image1->Canvas->Pixels[xPos][yPos] = RGB(tmpr,tmpg,tmpb);
                        xPos++;
                    }
                    break;
                case 2:
                    int amount;
                    fread(&amount,4,1,f);
                    xPos += amount/2;
                    //move forward amount of t1?
                    break;
                case 3:
                    //new line??
                    yPos++;
                    int tmp1;
                    tmp1=0;
                    fread(&t2,1,1,f);
                    fread(&tmp1,4,1,f);
                    xPos += tmp1/2;
                    break;
                default:
//                    ShowMessage("Error " + IntToStr(t1));
                    fclose(f);
                    Screen->Cursor = crDefault;
                    return;
            }
            if (fread(&t1,1,1,f) == 0) {
                //end of file?
                t1 = -1;
            }
        }
    } catch(...) { }
    fclose(f);
    Screen->Cursor = crDefault;
    //End Loaditem1
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button1Click(TObject *Sender)
{
    if (Edit1->Text != "") {
        FILE *f;
        AnsiString ImageDirectory;
        AnsiString ImageStart;
        long flength;
        AnsiString Value; //temp string value
        AnsiString Version; //temp string value
        int t1 = 0; //temp int value
        int t2 = 0; //temp int value

        f = fopen(Edit1->Text.c_str(),"rb");
        if (f == NULL) {
//            ShowMessage("Error Loading File");
            return;
        }

        fseek(f,0,SEEK_END);  // seek to end
        Value = Edit1->Text;
        ImageDirectory = Value.SubString(0,Value.Pos(".")-1) + "\\";
        while (Value.Pos("\\") != 0) {
            Value = Value.SubString(Value.Pos("\\")+1,Value.Length());
        }
        ImageStart = Value.SubString(0,Value.Pos(".")-1);
        fseek(f,0,SEEK_SET);  // rewind to beginning
        int ilength;
        fread(&ilength,1,1,f);
        Value = "";
        for (int j=0;j<ilength;j++) {
            char c;
            fread(&c,1,1,f);
            Value = Value + AnsiString(c);
        }
        fread(&ilength,1,1,f);
        Version = "";
        for (int j=0;j<ilength;j++) {
            char c;
            fread(&c,1,1,f);
            Version = Version + AnsiString(c);
        }
        fread(&t1,4,1,f);
        fread(&t1,4,1,f);
        int total = t1;
        ProgressBar1->Max = total;
        ProgressBar1->Step = 1;
        ProgressBar1->Position = 0;
        for (int i=0; i<total; i++) {
            fread(&ilength,1,1,f);
            Value = "";
            for (int j=0;j<ilength;j++) {
                char c;
                fread(&c,1,1,f);
                Value = Value + AnsiString(c);
            }
            fread(&t1,4,1,f);
            fread(&t2,4,1,f);
            AnsiString Temp;
            Temp.sprintf("%5d",t2);
            Temp = StringReplace(Temp," ","0", TReplaceFlags() << rfReplaceAll);
            fread(&t1,4,1,f);
            try {
                F2loadRLE(ImageDirectory + ImageStart + Temp + ".rle",t1);
                if (!DirectoryExists(ExtractFilePath(Application->ExeName) + "\\" + ImageStart)) {
                    if (!CreateDir(ExtractFilePath(Application->ExeName) + "\\" + ImageStart)) {
                        ShowMessage("Error Creating Dir");
                        fclose(f);
                        return;
                    }
                }
                Image1->Picture->SaveToFile(ExtractFilePath(Application->ExeName) + "\\" + ImageStart + "\\" + Value + ".bmp");
            } catch(...) { }
            ProgressBar1->StepIt();
            Application->ProcessMessages();
            if (Version == "1.2") {
                fread(&t1,4,1,f);
            }
        }
        fclose(f);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button2Click(TObject *Sender)
{
    if (Edit1->Text != "") {
        FILE *f;
        AnsiString ImageDirectory;
        AnsiString ImageStart;
        AnsiString Value; //temp string value
        AnsiString Version; //temp string value
        int t1 = 0; //temp int value
        int t2 = 0; //temp int value

        f = fopen(Edit1->Text.c_str(),"rb");
        if (f == NULL) {
//            ShowMessage("Error Loading File");
            return;
        }

        fseek(f,0,SEEK_END);  // seek to end
        Value = Edit1->Text;
        ImageDirectory = Value.SubString(0,Value.Pos(".")-1) + "\\";
        while (Value.Pos("\\") != 0) {
            Value = Value.SubString(Value.Pos("\\")+1,Value.Length());
        }
        ImageStart = Value.SubString(0,Value.Pos(".")-1);
        fseek(f,0,SEEK_SET);  // rewind to beginning
        int ilength = 0;
        fread(&ilength,1,1,f);
        Value = "";
        for (int j=0;j<ilength;j++) {
            char c;
            fread(&c,1,1,f);
            Value = Value + AnsiString(c);
        }
        fread(&ilength,1,1,f);
        Version = "";
        for (int j=0;j<ilength;j++) {
            char c;
            fread(&c,1,1,f);
            Version = Version + AnsiString(c);
        }
        fread(&t1,4,1,f);
        fread(&t1,4,1,f);
        int total = t1;
        ProgressBar1->Max = total;
        ProgressBar1->Step = 1;
        ProgressBar1->Position = 0;
        for (int i=0; i<total; i++) {
            fread(&ilength,1,1,f);
            Value = "";
            for (int j=0;j<ilength;j++) {
                char c;
                fread(&c,1,1,f);
                Value = Value + AnsiString(c);
            }
            fread(&t1,4,1,f);
            fread(&t2,4,1,f);
            AnsiString Temp;
            Temp.sprintf("%5d",t2);
            Temp = StringReplace(Temp," ","0", TReplaceFlags() << rfReplaceAll);
            fread(&t1,4,1,f);
            try {
                if (!DirectoryExists(ExtractFilePath(Application->ExeName) + "\\" + ImageStart)) {
                    if (!CreateDir(ExtractFilePath(Application->ExeName) + "\\" + ImageStart)) {
                        ShowMessage("Error Creating Dir");
                        fclose(f);
                        return;
                    }
                }
                if (t2 == atoi(Edit2->Text.c_str())) {
                    F2loadRLE(ImageDirectory + ImageStart + Temp + ".rle",t1);
                    Image1->Picture->SaveToFile(ExtractFilePath(Application->ExeName) + "\\" + ImageStart + "\\" + Value + ".bmp");
                }
            } catch(...) { }
            ProgressBar1->StepIt();
            Application->ProcessMessages();
            if (Version == "1.2") {
                fread(&t1,4,1,f);
            }
        }
        fclose(f);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Edit1DblClick(TObject *Sender)
{
    if (OpenDialog1->Execute()) {
        Edit1->Text = OpenDialog1->FileName;
    }
}
//---------------------------------------------------------------------------

