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

#include <vcl.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 __fastcall TForm2::Button4Click(TObject *Sender)
{
    int xOffset = 720;
    int yOffset = 0;
    int xStart, yStart;
    xStart = atoi(Form1->Edit1->Text.c_str());
    yStart = atoi(Form1->Edit2->Text.c_str());
    yStart++;
    Form1->Edit2->Text = yStart;
    Graphics::TBitmap *Bitmap;
    Bitmap = new Graphics::TBitmap;
    Bitmap->Width = 800;
    Bitmap->Height = 600;
    TRect   MyRect, MyOther;
    MyRect = Rect(48,0,848,600);

    MyOther = Rect(0,0,800,600);
    Bitmap->Canvas->BrushCopy(MyOther, Image1->Picture->Bitmap, MyRect, RGB(255,0,255));
    Image1->Canvas->BrushCopy(MyOther, Bitmap, MyOther, RGB(255,0,255));
    delete Bitmap;
//    Application->ProcessMessages();
    yStart+=15;
    //draw end row on ground
    for (int i=xStart;i<Form1->height;i++) {
        for (int j=yStart;j<Form1->width;j++) {
            if (xOffset < Image1->Width) {
                if (yOffset < Image1->Height) {
                    int fnum, inum;
                    fnum = (int)(Form1->mapData[i][j].tle2 * 2);
                    fnum += (int)(Form1->mapData[i][j].tle1 / 128);
                    inum = (int)(Form1->mapData[i][j].tle1 % 128)*8;
                    inum += (int)(Form1->mapData[i][j].obj2 / 32);

                    Graphics::TBitmap *Bitmap;

                    TRect   MyRect, MyOther;

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

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

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

                        Graphics::TBitmap *Bitmap;

                        TRect   rDest, rSource;
                        int width, height;
                        height = Form1->objData[fnum][inum].yend-Form1->objData[fnum][inum].ystart;
                        width = Form1->objData[fnum][inum].xend-Form1->objData[fnum][inum].xstart;

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

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