// UTS 2000 - Fichier 'fond.cpp' Affichage d'un fond de plan - V3.3 - Le 26/05/2009
// Code pas encore utilisé
/*****************************************************************************/
/* debut TRAIN                                                               */
/*****************************************************************************/
#include  <windows.h>
#include  <string.h>
#include  <stdlib.h>
#include  <stdio.h>
#include "Train_UTS.h"

#define _CRT_SECURE_NO_WARNINGS  // Limite les messages de warning dans Visual C++

// Déclaration des fonctions externes
extern int get_line(FILE*, char [][CHAR_CONFIG_NB], int*);
extern int can_to_cons(int, int);
// extern cCanton  canton;
// extern cGarage  garage;
extern void EasyTextOut(int, int, char*);
extern void EasyLine(int, int, int, int);

// Déclaration des variables externes
extern HBRUSH  bmp_pinceau_blanc, bmp_pinceau_gris;
extern HDC hMemDC;
extern HFONT     bmp_fonte;
extern HPEN      bmp_stylo_noir, bmp_stylo_gris192, bmp_stylo_blanc;
extern int vio_garage_lock_cmd[NB_GARAGE]; // 0 ou 1, 1 => Demande d'isolation du garage (Sera réalisée si il a un train sur le canton).
extern int vio_garage_lock_status[NB_GARAGE]; // 0 ou 1, 1 => Le garage est isolé du canton (état réel).
extern int vio_garage_lock_res[NB_GARAGE]; // 0 ou 1, 1 => Garage réservé.
extern int vio_can[NB_CARTE][NB_CAN];
extern int vio_pot_virtual_step[NB_POT_VIR]; //UU_NEW
extern int x_mouse,y_mouse,touche,touche_char,x_mouse_move,y_mouse_move;
extern int vio_det[NB_CARTE][NB_CANTON];
extern int vio_pot_virtual_conf[NB_POT_VIR][3];
extern int moins_char_normal;   // 1: '-' est un caractère normal (comme 0)  0: '-' est un séparateur (comme +)



#ifdef UU
#define FOND_TAB_NB          8
#define FOND_TAB_IND_NB      4
#define FOND_TAB_IND_TYPE    0
#define FOND_TAB_IND_FILE    1
//
#define FOND_TAB_IND_R       1
#define FOND_TAB_IND_V       2
#define FOND_TAB_IND_B       3
#define FOND_TAB_IND_DX      2
#define FOND_TAB_IND_DY      3
#define FOND_DXDY_IND_X      0
#define FOND_DXDY_IND_Y      1
#define FOND_CST_RVB         0
#define FOND_CST_FILE        1
int fond_tab[FOND_TAB_NB][FOND_TAB_IND_NB];
char filename_fond[5][100];
int filename_fond_dx_dy[5][2];
HBRUSH fond_tab_brush[8];

/*****************************************************************************/
/* FOND      --><--                                                          */
/*****************************************************************************/

void fond_set_tco(int tco)
{
    int dx,dy,no_file;
    SelectObject(hMemDC,bmp_pinceau_blanc);
    SelectObject(hMemDC,bmp_stylo_blanc);
    Rectangle(hMemDC,0,0,1280,1024);
    SelectObject(hMemDC,bmp_pinceau_gris192);
    Rectangle(hMemDC,0,0,100,1024);
    if(tco>=FOND_TAB_NB) return;
    if(fond_tab[tco][FOND_TAB_IND_TYPE]==FOND_CST_RVB)
    {
        //SelectObject(hMemDC,bmp_pinceau_blanc);
        SelectObject(hMemDC,fond_tab_brush[tco]);
        Rectangle(hMemDC,0,0,1280,1024);
    }
    if(fond_tab[tco][FOND_TAB_IND_TYPE]==FOND_CST_FILE)
    {
        dx=fond_tab[tco][FOND_TAB_IND_DX];
        dy=fond_tab[tco][FOND_TAB_IND_DY];
        no_file=fond_tab[tco][FOND_TAB_IND_FILE];
        switch(no_file)
// BitBlt(hdc //h_dc_cible, x1,y1//xy_cible, dx,dy//largeur_hauteur, _hMemDC//h_dc_source ,x1,y1//xy_source, SRCCOPY.
        {
            case 0: BitBlt(hMemDC,0,0,dx,dy,bmp_h_dc_fond_0,0,0,SRCCOPY); break;
            case 1: BitBlt(hMemDC,0,0,dx,dy,bmp_h_dc_fond_1,0,0,SRCCOPY); break;
            case 2: BitBlt(hMemDC,0,0,dx,dy,bmp_h_dc_fond_2,0,0,SRCCOPY); break;
            case 3: BitBlt(hMemDC,0,0,dx,dy,bmp_h_dc_fond_3,0,0,SRCCOPY); break;
            default: break;
        }
    }
}
void fond_init_file(char filename[])
{
    FILE* pf;
    char ch[CHAR_CONFIG_NB],ch_tab[BLOC_CONFIG_NB][CHAR_CONFIG_NB],ch_error[200],ch_syst[100];
    int no_tco,no_file,ch_nb;
    int i,j;
    HBITMAP   hbitmap;
    for(j=0;j<FOND_TAB_NB;j++) for(i=0;i<FOND_TAB_IND_NB;i++) fond_tab[j][i]=0;  //RVB noir
    for(j=0;j<5;j++)
    {
        filename_fond[j][0]='\0';
        filename_fond_dx_dy[j][0]=800;
        filename_fond_dx_dy[j][1]=600;
    }
    if((pf=fopen(filename,"rt"))==NULL) {
        MessageBox(NULL,"Impossible d'ouvrir le fichier de configuration des fonds","Erreur",MB_OK|MB_ICONERROR); exit(1);
    }
    /*ex:
    #fond
    [
        filedescr:voyant,800,600,voyant.bmp
        filedescr:0     ,700,300,fond_ribo.bmp
        filedescr:1     ,700,300,fond_ribo_old.bmp
        tco:0 file:0
        tco:1 file:0
        tco:2 file:1
        tco:3 file:0
        tco:4 rvb:255,255,255
        tco:5 rvb:  0,255,255
        tco:6 rvb:  0,255,  0
        tco:7 rvb:  0,  0,255
    ]
    */
    while(fscanf(pf," %s",ch)!=EOF)
    {
        if(!strcmp(ch,"#fond"))
        {
            fscanf(pf," %s",ch); //remove [
            while(get_line(pf,ch_tab,&ch_nb))
            {
                for(i=0;i<ch_nb;i++) if(ch_tab[i][0]=='@') ch_nb=i;  //remove comments
                i=0;
                while(i<ch_nb)
                {
                    if((ch_nb-i)>=3)
                    if( (!strcmp(ch_tab[i],"tco")) && ch_tab[i+1][0]==':' )
                    {
                        no_tco=atoi(ch_tab[i+2]);
                        i=i+3;
                        continue;
                    }
                    if((ch_nb-i)>=7)
                    if( (!strcmp(ch_tab[i],"rvb")) && ch_tab[i+1][0]==':'
                       && ch_tab[i+3][0]==',' && ch_tab[i+5][0]==',' )
                    {
                        fond_tab[no_tco][FOND_TAB_IND_TYPE]=FOND_CST_RVB;
                        fond_tab[no_tco][FOND_TAB_IND_R]=atoi(ch_tab[i+2]);
                        fond_tab[no_tco][FOND_TAB_IND_V]=atoi(ch_tab[i+4]);
                        fond_tab[no_tco][FOND_TAB_IND_B]=atoi(ch_tab[i+6]);
                        i=i+7;
                        continue;
                    }
                    if((ch_nb-i)>=11)
                    if( (!strcmp(ch_tab[i],"filedescr")) && ch_tab[i+1][0]==':'
                       && ch_tab[i+3][0]==',' && ch_tab[i+5][0]==','
                       && ch_tab[i+7][0]==',' && ch_tab[i+9][0]=='.'
                       && (!strcmp(ch_tab[i+10],"bmp"))  )
                    {
                         if(!strcmp(ch_tab[i+2],"voyant")) no_file=4;
                         else no_file=atoi(ch_tab[i+2]);
                        sprintf_s(filename_fond[no_file],"%s.%s",ch_tab[i+8],ch_tab[i+10]);
                        filename_fond_dx_dy[no_file][FOND_DXDY_IND_X]=atoi(ch_tab[i+4]);
                        filename_fond_dx_dy[no_file][FOND_DXDY_IND_Y]=atoi(ch_tab[i+6]);
                        i=i+11;
                        continue;
                    }
                    if((ch_nb-i)>=3)
                    if( (!strcmp(ch_tab[i],"file")) && ch_tab[i+1][0]==':')
                    {
                        fond_tab[no_tco][FOND_TAB_IND_TYPE]=FOND_CST_FILE;
                        no_file=atoi(ch_tab[i+2]);
                        fond_tab[no_tco][FOND_TAB_IND_FILE]=no_file;
                        fond_tab[no_tco][FOND_TAB_IND_DX]=filename_fond_dx_dy[no_file][FOND_DXDY_IND_X];
                        fond_tab[no_tco][FOND_TAB_IND_DY]=filename_fond_dx_dy[no_file][FOND_DXDY_IND_Y];
                        i=i+3;
                        continue;
                    }
                    strcpy(ch_error,"Fond Error");
                    for(i=0;i<ch_nb;i++) { strcat(ch_error,"  "); strcat(ch_error,ch_tab[i]); }
                    MessageBox(NULL,ch_error,"UTS - Erreur",MB_OK|MB_ICONERROR); exit(1);
                }  //fin groupe de mot
            }    //fin chaine
        }      //fin #fond
    }        //fin file
    fclose(pf);

//
    bmp_fonte=CreateFont(15,10,0,0,400,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_MODERN,"Courier");

    bmp_pinceau_noir      = CreateSolidBrush(RGB(  0,  0,  0));
    bmp_pinceau_bleu      = CreateSolidBrush(RGB(  0,128,255));
    bmp_pinceau_vert      = CreateSolidBrush(RGB(  0,255,  0));
    bmp_pinceau_cyan      = CreateSolidBrush(RGB(  0,255,255));
    bmp_pinceau_rouge     = CreateSolidBrush(RGB(255,  0,  0));
    bmp_pinceau_violet    = CreateSolidBrush(RGB(128,  0,255));
    bmp_pinceau_jaune     = CreateSolidBrush(RGB(255,255,  0));
    bmp_pinceau_blanc     = CreateSolidBrush(RGB(255,255,255));
    bmp_pinceau_gris      = CreateSolidBrush(RGB(192,192,192));
    bmp_pinceau_grisf      = CreateSolidBrush(RGB(128,128,255));
    bmp_pinceau_orange    = CreateSolidBrush(RGB(255,128,  0));
    bmp_pinceau_marron    = CreateSolidBrush(RGB(128, 64,  0));
    bmp_pinceau_kaki      = CreateSolidBrush(RGB(128,128,  0));
    bmp_pinceau_bleuf   = CreateSolidBrush(RGB(128,  0, 64));
    bmp_pinceau_grisvert  = CreateSolidBrush(RGB( 64,128,128));
    bmp_pinceau_vertfonce = CreateSolidBrush(RGB(  0,128,  0));

    bmp_stylo_noir        = CreatePen(PS_SOLID,1,RGB(0  ,0  ,0  ));
    bmp_stylo_bleu        = CreatePen(PS_SOLID,1,RGB(  0,128,255));
    bmp_stylo_vert        = CreatePen(PS_SOLID,1,RGB(  0,255,  0));
    bmp_stylo_cyan        = CreatePen(PS_SOLID,1,RGB(  0,255,255));
    bmp_stylo_rouge       = CreatePen(PS_SOLID,1,RGB(255,  0,  0));
    bmp_stylo_violet      = CreatePen(PS_SOLID,1,RGB(128,  0,255));
    bmp_stylo_jaune       = CreatePen(PS_SOLID,1,RGB(255,255,  0));
    bmp_stylo_blanc       = CreatePen(PS_SOLID,1,RGB(255,255,255));
    bmp_stylo_gris        = CreatePen(PS_SOLID,1,RGB(192,192,192));
    bmp_stylo_grisf        = CreatePen(PS_SOLID,1,RGB(255,128,255));
    bmp_stylo_orange      = CreatePen(PS_SOLID,1,RGB(255,128,  0));
    bmp_stylo_marron      = CreatePen(PS_SOLID,1,RGB(128, 64,  0));
    bmp_stylo_kaki        = CreatePen(PS_SOLID,1,RGB(128,128,  0));
    bmp_stylo_bleuf     = CreatePen(PS_SOLID,1,RGB(128,  0, 64));
    bmp_stylo_grisvert    = CreatePen(PS_SOLID,1,RGB( 64,128,128));
    bmp_stylo_vertfonce   = CreatePen(PS_SOLID,1,RGB(  0,128,  0));

    for(j=0;j<8;j++)
    {
        if(fond_tab[j][FOND_TAB_IND_TYPE]==FOND_CST_RVB)
          fond_tab_brush[j]=CreateSolidBrush(RGB(fond_tab[j][FOND_TAB_IND_R],fond_tab[j][FOND_TAB_IND_V],fond_tab[j][FOND_TAB_IND_B]));
    }
    //MessageBox(NULL,"Init brush tab ok","Erreur",MB_OK|MB_ICONERROR);
    for(j=0;j<5;j++)
    if(filename_fond[j][0]!='\0')
    {
        hbitmap=(HBITMAP)LoadImage(ghinstance,filename_fond[j],
          IMAGE_BITMAP,filename_fond_dx_dy[j][FOND_DXDY_IND_X],
          filename_fond_dx_dy[j][FOND_DXDY_IND_Y],LR_LOADFROMFILE);
        if(hbitmap==NULL) {
            MessageBox(NULL,"Impossible d'ouvrir un des fichiers bmp des fonds (ou des voyants)","Erreur",MB_OK|MB_ICONERROR); exit(1);
        }
        switch(j) {
            case 0:  bmp_h_dc_fond_0=CreateCompatibleDC(hdc);SelectObject(bmp_h_dc_fond_0,hbitmap);break;
            case 1:  bmp_h_dc_fond_1=CreateCompatibleDC(hdc);SelectObject(bmp_h_dc_fond_1,hbitmap);break;
            case 2:  bmp_h_dc_fond_2=CreateCompatibleDC(hdc);SelectObject(bmp_h_dc_fond_2,hbitmap);break;
            case 3:  bmp_h_dc_fond_3=CreateCompatibleDC(hdc);SelectObject(bmp_h_dc_fond_3,hbitmap);break;
            case 4:  bmp_h_dc_mem   =CreateCompatibleDC(hdc);SelectObject(bmp_h_dc_mem   ,hbitmap);break;
            default: break;
        }
    }
}
void fond_debug(void)
{
    FILE* pf;
    int i,j;
    if((pf=fopen("debug_fond.txt","wt"))==NULL) {
        MessageBox(NULL,"Impossible de créer debug_fond.txt","Erreur",MB_OK|MB_ICONERROR); exit(1);
    }
    fprintf(pf,"\n\nFOND_TAB_TYPE\n");
    for(j=0;j<FOND_TAB_NB;j++) {
        fprintf(pf,"\n%3d:",j);
        for(i=0;i<FOND_TAB_IND_NB;i++) fprintf(pf," %3d",fond_tab[j][i]);
    }
    fprintf(pf,"\n\nFILENAME_FOND_DX_DY & FILENAME_FOND (interne)\n");
    for(j=0;j<5;j++) {
        fprintf(pf,"\n%3d: dx=%3d dy=%3d ch=%s",j,filename_fond_dx_dy[j][0],filename_fond_dx_dy[j][1],filename_fond[j]);
    }
    fclose(pf);
}
/*****************************************************************************/
/* END FOND                                                                  */
/*****************************************************************************/
#endif //UU