/**************************************************************************************

                File        : INIT.CPP
                Author      : Delattre Franck & Valérie Cluzel
                Last update : 21 / 02 / 97

Three types of functions:
        - Initialization functions
        - functions used to change a field
        - liberation functions

***************************************************************************************/

#define TDE_GLOBALS

#include <stdio.h>

#define __DeclareGlobalVariableErrTde_h__
#include "TDE\errtde.h"
#undef __DeclareGlobalVariableErrTde_h__

#define __DeclareGlobalVariableMmgTde_h__
#include "TDE\mmgtde.h"
#undef __DeclareGlobalVariableMmgTde_h__

#include "TDE.h"

/*********************************************************************

        TDE_INIT.C
        First part : INITIALIZATION FUNCTIONS
        
        - TDE_vInitPictureAttributes
        - TDE_vInitCamera
        - TDE_vInitSuperObject
        - TDE_vInitSuperObjectWithSprite
        - TDE_vInitLine
        - TDE_vInitSprite
        - TDE_vInitMemSprite
        - TDE_vInitMemSpriteWithoutSO
        - TDE_vInitTextBox
        - TDE_vCreateBitmapFont
        - TDE_vCreateTrueTypeFont
        - TDE_vInitTransparencyTable
                
**********************************************************************/

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitPictureAttributes
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a TDE_tdsPictureAttributes structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitPictureAttributes(TDE_tdsPictureAttributes **h_stPicture,
                                    TDE_tdxValue xWidth,
                                    TDE_tdxValue xHeight,
                                    TDE_tdxValue xSrcPitch,
                                    TDE_tdLP_SOURCE lp_Data,
                                    TDE_tdLP_SURF   lp_DDS)
{
    M_TdeAlloc( (*h_stPicture), TDE_tdsPictureAttributes *, (sizeof(TDE_tdsPictureAttributes)) );
    if ((*h_stPicture) == NULL)
        return;
    (*h_stPicture)->xWidth    = xWidth;
    (*h_stPicture)->xHeight   = xHeight;
    (*h_stPicture)->xSrcPitch = xSrcPitch;
    (*h_stPicture)->lp_Data   = lp_Data;
    (*h_stPicture)->lp_DDS    = lp_DDS;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitCamera
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Initialize the camera
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitCamera(TDE_tdsCamera *stCamera)
{
    stCamera->xAngle = 0;
    stCamera->stScale.xX = 1;
    stCamera->stScale.xY = 1;
    stCamera->stTranslateVertex.xX = 0;
    stCamera->stTranslateVertex.xY = 0;
    TDE_vCreateSuperObjectMatrix(stCamera);
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitSuperObject
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a TDE_tdsSuperObject structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitSuperObject(TDE_tdsSuperObject **h_stSuperObject,
                           short sPriority,
                           TDE_tdsSuperObject *p_stChild,
                           TDE_tdsSuperObject *p_stLeftBrother,
                           TDE_tdsSuperObject *p_stRightBrother,
                           TDE_tdsSuperObject *p_stFather,
                           TDE_tdeType eType,
                           TDE_tdxAngle xAngle,
                           TDE_tdxValue xZX,
                           TDE_tdxValue xZY,
                           TDE_tdxValue xCenterX,
                           TDE_tdxValue xCenterY,
                           long lArea,
                           long lObject)
{    
    M_TdeAlloc( (*h_stSuperObject), TDE_tdsSuperObject *, (sizeof(TDE_tdsSuperObject)));
    if ((*h_stSuperObject) == NULL)
        return;
    (*h_stSuperObject)->sPriority = sPriority;
    (*h_stSuperObject)->p_stChild = p_stChild;
    (*h_stSuperObject)->p_stLeftBrother = p_stLeftBrother;
    (*h_stSuperObject)->p_stRightBrother = p_stRightBrother;
    (*h_stSuperObject)->p_stFather = p_stFather;                
    (*h_stSuperObject)->eType = eType;                
    (*h_stSuperObject)->stMatrix.xAngle = xAngle;
    (*h_stSuperObject)->stMatrix.stScale.xX = xZX;
    (*h_stSuperObject)->stMatrix.stScale.xY = xZY;                
    (*h_stSuperObject)->p_stSOClipRect = NULL; 
    (*h_stSuperObject)->stMatrix.stTranslateVertex.xX = xCenterX;                
    (*h_stSuperObject)->stMatrix.stTranslateVertex.xY = xCenterY;
    (*h_stSuperObject)->lArea = lArea;
    (*h_stSuperObject)->lObject = lObject;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitSuperObject
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a TDE_tdsSuperObject structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitSuperObjectWithSprite(TDE_tdsSuperObject **h_stSuperObject,
                                    TDE_tdsSprite      *p_stSprite,
                                    short sPriority,
                                    TDE_tdsSuperObject *p_stChild,
                                    TDE_tdsSuperObject *p_stLeftBrother,
                                    TDE_tdsSuperObject *p_stRightBrother,
                                    TDE_tdsSuperObject *p_stFather,
                                    TDE_tdxAngle xAngle,
                                    TDE_tdxValue xZX,
                                    TDE_tdxValue xZY,
                                    TDE_tdxValue xCenterX,
                                    TDE_tdxValue xCenterY,
                                    long lArea,
                                    long lObject)
{    
    M_TdeAlloc( (*h_stSuperObject), TDE_tdsSuperObject *, (sizeof(TDE_tdsSuperObject)));
    if ((*h_stSuperObject) == NULL)
        return;
    (*h_stSuperObject)->p_stSprite = p_stSprite;
    (*h_stSuperObject)->sPriority = sPriority;
    (*h_stSuperObject)->p_stChild = p_stChild;
    (*h_stSuperObject)->p_stLeftBrother = p_stLeftBrother;
    (*h_stSuperObject)->p_stRightBrother = p_stRightBrother;
    (*h_stSuperObject)->p_stFather = p_stFather;                
    (*h_stSuperObject)->eType = TDE_eOT_SPRITE;                
    (*h_stSuperObject)->stMatrix.xAngle = xAngle;
    (*h_stSuperObject)->stMatrix.stScale.xX = xZX;
    (*h_stSuperObject)->stMatrix.stScale.xY = xZY;                
    (*h_stSuperObject)->p_stSOClipRect = NULL; 
    (*h_stSuperObject)->stMatrix.stTranslateVertex.xX = xCenterX;                
    (*h_stSuperObject)->stMatrix.stTranslateVertex.xY = xCenterY;
    (*h_stSuperObject)->lArea = lArea;
    (*h_stSuperObject)->lObject = lObject;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitLine
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a tdsLine structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitLine(TDE_tdsSuperObject *p_SuperObject,
                   TDE_tdxValue xLength,
                   long lColor)
{    
    M_TdeAlloc( p_SuperObject->p_stLine, TDE_tdsLine *, (sizeof(TDE_tdsLine)) );    
    if (p_SuperObject->p_stLine == NULL)
    {
        p_SuperObject->eType = TDE_eOT_EMPTY;
        return;
    }
    p_SuperObject->eType                = TDE_eOT_LINE;
    p_SuperObject->p_stLine->xLength    = xLength;
    p_SuperObject->p_stLine->lColor     = lColor;
}                     

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitSprite
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a tdsSprite structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitSprite(TDE_tdsSuperObject *p_SuperObject,
                     TDE_tdxValue xWidth,
                     TDE_tdxValue xHeight,
                     TDE_tdxValue xXs,
                     TDE_tdxValue xYs,
                     TDE_tdxPixel *v_pData,
                     CHAR  cFlip,
                     CHAR  c_NZ,
                     CHAR  cSemiTransparent,
                     TDE_tdsPictureAttributes *p_stPicture)
{    
    M_TdeAlloc( p_SuperObject->p_stSprite, TDE_tdsSprite *, (sizeof(TDE_tdsSprite)) );
    if (p_SuperObject->p_stSprite == NULL)
    {
        p_SuperObject->eType = TDE_eOT_EMPTY;
        return;
    }
    p_SuperObject->p_stSprite->stDim.xX = xWidth;
    p_SuperObject->p_stSprite->stDim.xY = xHeight;
    p_SuperObject->p_stSprite->stSourceOrigin.xX = xXs;
    p_SuperObject->p_stSprite->stSourceOrigin.xY = xYs;
    p_SuperObject->p_stSprite->v_pData = v_pData;    
    p_SuperObject->p_stSprite->cFlip = cFlip;
    p_SuperObject->p_stSprite->c_NZ = c_NZ;
    p_SuperObject->p_stSprite->cSemiTransparent = cSemiTransparent;
    p_SuperObject->p_stSprite->stPicture = *p_stPicture;
}                     

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitMemSprite
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a tdsSprite structure for memory sprite
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitMemSprite(TDE_tdsSuperObject *p_SuperObject,
                        TDE_tdxPixel *v_pData,
                        CHAR  c_NZ)
{    
    M_TdeAlloc( p_SuperObject->p_stSprite, TDE_tdsSprite *, (sizeof(TDE_tdsSprite)) );
    if (p_SuperObject->p_stSprite == NULL)
    {
        p_SuperObject->eType = TDE_eOT_EMPTY;
        return;
    }
    p_SuperObject->eType                        = TDE_eOT_SPRITE;
    p_SuperObject->p_stSprite->v_pData          = v_pData;    
    p_SuperObject->p_stSprite->c_NZ             = c_NZ;
}                     

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitMemSpriteWithoutSO
 VERSION : 2.0 / Yan
           1.0 / Franck

 AIM : Fills a tdsSprite structure for memory sprite non attached
       to a super object       
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitMemSpriteWithoutSO(TDE_tdsSprite **p_stSprite,
                                 TDE_tdxPixel *v_pData,
                                 CHAR  c_NZ)
{    
    M_TdeAlloc( *p_stSprite, TDE_tdsSprite *, (sizeof(TDE_tdsSprite)) );
    if ((*p_stSprite) == NULL)
        return;
    (*p_stSprite)->v_pData = v_pData;    
    (*p_stSprite)->c_NZ = c_NZ;
}                     


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitTextBox
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Fills a tdsTextBox structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitTextBox (TDE_tdsSuperObject   *p_SuperObject,
                       TDE_tdeFontType      eFontType,
                       void                 *p_stFont,
                       unsigned long        ulStyle)
{
    // Allocate memory for text box
    M_TdeAlloc( p_SuperObject->p_stTextBox, TDE_tdsTextBox *, (sizeof(TDE_tdsTextBox)) );
    if (p_SuperObject->p_stTextBox == NULL)
        return;
    // Allocate memory for format rectangle
    M_TdeAlloc(p_SuperObject->p_stTextBox->p_stFormatRect, TDE_tdsRect*, sizeof(TDE_tdsLRect));
    if (p_SuperObject->p_stTextBox->p_stFormatRect == NULL)
    {
        M_TdeFree (p_SuperObject->p_stTextBox);
        p_SuperObject->p_stTextBox = NULL;
        p_SuperObject->eType = TDE_eOT_EMPTY;
        return;
    }
    // Allocate memory for clipping
    M_TdeAlloc(p_SuperObject->p_stTextBox->p_stClipRect,   TDE_tdsRect*, sizeof(TDE_tdsLRect));
    if (p_SuperObject->p_stTextBox->p_stClipRect == NULL)
    {
        M_TdeFree (p_SuperObject->p_stTextBox->p_stFormatRect);
        M_TdeFree (p_SuperObject->p_stTextBox);
        p_SuperObject->p_stTextBox = NULL;
        p_SuperObject->eType = TDE_eOT_EMPTY;
        return;
    }
    // Set font type and pointer to font
    p_SuperObject->p_stTextBox->eFontType = eFontType;
    switch (eFontType)
    {
    case TDE_eFT_BITMAP:
        p_SuperObject->p_stTextBox->p_stBFont = (TDE_tdsBitmapFont*)p_stFont;
        break;
    case TDE_eFT_TRUETYPE:
        p_SuperObject->p_stTextBox->p_stTFont = (TDE_tdsTrueTypeFont*)p_stFont;
        break;
    }
    // Set style
    p_SuperObject->p_stTextBox->ulStyle = ulStyle;
    // Set default values
    p_SuperObject->p_stTextBox->eCursorType     = TDE_eCT_NOCURSOR;
    p_SuperObject->p_stTextBox->lCursorPosition = 0;
    p_SuperObject->p_stTextBox->p_cText         = NULL;
    p_SuperObject->p_stTextBox->cColorMode      = 0;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vCreateBitmapFont
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Allocation of a bitmap font structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vCreateBitmapFont(TDE_tdsBitmapFont **BFont)
{
    long i;
    
    M_TdeAlloc( *BFont, TDE_tdsBitmapFont *, (sizeof(TDE_tdsBitmapFont)) );
    if ((*BFont) == NULL)
        return;
    // initialize every characters to NULL
    for ( i=0; i<TDE_kNBLETTERS; i++ )
    {
        (*BFont)->p_stMBLetter[i] = NULL;
    }
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vCreateTrueTypeFont
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Allocation of a true type font structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vCreateTrueTypeFont(TDE_tdsTrueTypeFont **TFont)
{
    M_TdeAlloc( *TFont, TDE_tdsTrueTypeFont *, (sizeof(TDE_tdsTrueTypeFont)) );
    if ((*TFont) == NULL)
        return;
    (*TFont)->hFont = NULL;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vInitTransparencyTable()
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Init tables for semi-transparency
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vInitTransparencyTable()
{
    char i, j;

    for (i=0; i<64; i++)
    {
        for (j=0; j<64; j++)
        {
            a_cTransparencyTable6[i][j] = (unsigned char) ((i*j)/63);
        }
        for (j=0; j<32; j++)
        {
            a_cTransparencyTable5[i][j] = (unsigned char) ((i*j)/63);
        }        
    }
}

/*********************************************************************

        TDE_INIT.C
        Second part : FUNCTIONS USED TO CHANGE A STRUCTURE FIELD
        
        - TDE_vSetSourcePointer
        - TDE_vSetSuperObjectDescendantsSourcePointer
        - TDE_vSetSourcePitch
        - TDE_vSetSuperObjectDescendantsSourcePitch
        - TDE_vChangeTextInTheBox
        - TDE_vChangeCursorType
        - TDE_vChangeCursorPosition
        - TDE_vChangeFormatRectangle
        - TDE_vChangeClipRectangle
        - TDE_vMoveFormatRectangle
        - TDE_vMoveClipRectangle
        - TDE_vChangeSuperObjetCoordinates
        - TDE_vChangeSuperObjetScale
        - TDE_vChangeSuperObjetAngle
        - TDE_vChangeSuperObjectFlip
        - TDE_vChangeSuperObjectAlpha
        - TDE_vChangeSuperObjectDescendantsAlpha
        - TDE_vChangeSuperObjectSemiTransparency
        - TDE_vChangeSuperObjectDescendantsSemiTransparency        
        - TDE_vChangeSourceCoordinates
                
**********************************************************************/

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vSetSourcePointer
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the pointer of a picture definition
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vSetSourcePointer(TDE_tdsSuperObject *p_stSuperObject,
                            TDE_tdLP_SOURCE lp_Data)
{
     p_stSuperObject->p_stSprite->stPicture.lp_Data = lp_Data;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vSetSuperObjectDescendantsSourcePointer
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Set the source pointer (has to be called when the source is locked)
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vSetSuperObjectDescendantsSourcePointer(TDE_tdsSuperObject *p_stSuperObject, TDE_tdLP_SOURCE lp_Data)
{
    TDE_tdsSuperObject *p_stSuperObjectTemp;

    TDE_vSetSourcePointer(p_stSuperObject, lp_Data);
    for ( p_stSuperObjectTemp = p_stSuperObject->p_stChild; p_stSuperObjectTemp != NULL; p_stSuperObjectTemp = p_stSuperObjectTemp->p_stRightBrother)
    {
      TDE_vSetSuperObjectDescendantsSourcePointer(p_stSuperObjectTemp, lp_Data);
    }
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vSetSourcePitch
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the pitch of a picture definition
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vSetSourcePitch(TDE_tdsSuperObject *p_stSuperObject,
                          TDE_tdxValue xSrcPitch)
{
     p_stSuperObject->p_stSprite->stPicture.xSrcPitch = xSrcPitch;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vSetSuperObjectDescendantsSourcePitch
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Set the source pitch (has to be called when the source is locked)
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vSetSuperObjectDescendantsSourcePitch(TDE_tdsSuperObject *p_stSuperObject, TDE_tdxValue xSrcPitch)
{
    TDE_tdsSuperObject *p_stSuperObjectTemp;

    TDE_vSetSourcePitch(p_stSuperObject, xSrcPitch);
    for ( p_stSuperObjectTemp = p_stSuperObject->p_stChild; p_stSuperObjectTemp != NULL; p_stSuperObjectTemp = p_stSuperObjectTemp->p_stRightBrother)
    {
      TDE_vSetSuperObjectDescendantsSourcePitch(p_stSuperObjectTemp, xSrcPitch);
    }
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeFormatRectangle
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Change the format rectangle of a text box
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeFormatRectangle(TDE_tdsTextBox *Box, TDE_tdxValue xCornerX, TDE_tdxValue xCornerY, TDE_tdxValue xWidth, TDE_tdxValue xHeight)
{
    Box->p_stFormatRect->tdsSommet[0].xX = xCornerX;
    Box->p_stFormatRect->tdsSommet[0].xY = xCornerY;

    Box->p_stFormatRect->tdsSommet[1].xX = xCornerX + xWidth-1;
    Box->p_stFormatRect->tdsSommet[1].xY = xCornerY;
            
    Box->p_stFormatRect->tdsSommet[2].xX = xCornerX + xWidth-1;
    Box->p_stFormatRect->tdsSommet[2].xY = xCornerY + xHeight-1;

    Box->p_stFormatRect->tdsSommet[3].xX = xCornerX;
    Box->p_stFormatRect->tdsSommet[3].xY = xCornerY + xHeight-1;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeClipRectangle
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Change the clipping rectangle of a text box
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeClipRectangle(TDE_tdsTextBox *Box, TDE_tdxValue xCornerX, TDE_tdxValue xCornerY, TDE_tdxValue xWidth, TDE_tdxValue xHeight)
{
    Box->p_stClipRect->tdsSommet[0].xX = xCornerX;
    Box->p_stClipRect->tdsSommet[0].xY = xCornerY;

    Box->p_stClipRect->tdsSommet[1].xX = xCornerX + xWidth-1;
    Box->p_stClipRect->tdsSommet[1].xY = xCornerY;
            
    Box->p_stClipRect->tdsSommet[2].xX = xCornerX + xWidth-1;
    Box->p_stClipRect->tdsSommet[2].xY = xCornerY + xHeight-1;

    Box->p_stClipRect->tdsSommet[3].xX = xCornerX;
    Box->p_stClipRect->tdsSommet[3].xY = xCornerY + xHeight-1;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vMoveFormatRectangle
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Translate the format rectangle of a text box
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vMoveFormatRectangle(TDE_tdsTextBox *Box, TDE_tdxValue xDx, TDE_tdxValue xDy)
{
    Box->p_stFormatRect->tdsSommet[0].xX += xDx;
    Box->p_stFormatRect->tdsSommet[0].xY += xDy;

    Box->p_stFormatRect->tdsSommet[1].xX += xDx;
    Box->p_stFormatRect->tdsSommet[1].xY += xDy;
            
    Box->p_stFormatRect->tdsSommet[2].xX += xDx;
    Box->p_stFormatRect->tdsSommet[2].xY += xDy;

    Box->p_stFormatRect->tdsSommet[3].xX += xDx;
    Box->p_stFormatRect->tdsSommet[3].xY += xDy;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vMoveClipRectangle
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Translate the clipping rectangle of a text box
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vMoveClipRectangle(TDE_tdsTextBox *Box, TDE_tdxValue xDx, TDE_tdxValue xDy)
{
    Box->p_stClipRect->tdsSommet[0].xX += xDx;
    Box->p_stClipRect->tdsSommet[0].xY += xDy;

    Box->p_stClipRect->tdsSommet[1].xX += xDx;
    Box->p_stClipRect->tdsSommet[1].xY += xDy;
            
    Box->p_stClipRect->tdsSommet[2].xX += xDx;
    Box->p_stClipRect->tdsSommet[2].xY += xDy;

    Box->p_stClipRect->tdsSommet[3].xX += xDx;
    Box->p_stClipRect->tdsSommet[3].xY += xDy;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeTextInTheBox
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Change the pointer to the text in a TextBox
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeTextInTheBox    (TDE_tdsSuperObject *p_SuperObject,
                                 char               *p_cText)                    
{    
    p_SuperObject->p_stTextBox->p_cText = p_cText;
}                     

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeCursorType
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Change the cursor type in a TextBox
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeCursorType  (TDE_tdsSuperObject *p_SuperObject,
                             TDE_tdeCursorType  eType)                    
{    
    p_SuperObject->p_stTextBox->eCursorType = eType;
}                     

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeCursorPosition
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Change the cursor position in a TextBox
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeCursorPosition  (TDE_tdsSuperObject *p_SuperObject,
                                 long               lPosition)                    
{
    p_SuperObject->p_stTextBox->lCursorPosition = lPosition;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeColorModeInTextBox
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Change the color mode in a TextBox
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeColorModeInTextBox  (TDE_tdsSuperObject *p_SuperObject,
                                     char               cColorMode)                    
{
    p_SuperObject->p_stTextBox->cColorMode = cColorMode;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjetCoordinates
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the super object center coordinates
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vChangeSuperObjetCoordinates(TDE_tdsSuperObject *p_stSuperObject,
                                          TDE_tdxValue xX,
                                          TDE_tdxValue xY)
{
    p_stSuperObject->stMatrix.stTranslateVertex.xX = xX;
    p_stSuperObject->stMatrix.stTranslateVertex.xY = xY;
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjetScale
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the super object X and Y scale factors
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSuperObjetScale(TDE_tdsSuperObject *p_stSuperObject,
                                 TDE_tdxValue xZX,
                                 TDE_tdxValue xZY)
{
    p_stSuperObject->stMatrix.stScale.xX = xZX;
    p_stSuperObject->stMatrix.stScale.xY = xZY;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjetAngle
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the super object angle
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSuperObjetAngle(TDE_tdsSuperObject *p_stSuperObjet,
                                 TDE_tdxAngle xAngle)
{
    p_stSuperObjet->stMatrix.xAngle = xAngle;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjectFlip
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the super object flip parameter
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/                                       

void TDE_vChangeSuperObjectFlip(TDE_tdsSuperObject *p_stSuperObjet,
                                char cFlip)
{
    p_stSuperObjet->p_stSprite->cFlip = cFlip&3;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjectAlpha
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Modify alpha channel for a superobject
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSuperObjectAlpha(TDE_tdsSuperObject *p_stSuperObjet,
                                  TDE_tdxValue xAlpha)
{
    p_stSuperObjet->p_stSprite->alpha = xAlpha;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjectSemiTransparency
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Enables or disables semi-transparency property
       1 -> enable
       0 -> disable
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSuperObjectSemiTransparency(TDE_tdsSuperObject *p_stSuperObjet,
                                             char cSemiTransparent)
{
    p_stSuperObjet->p_stSprite->cSemiTransparent = cSemiTransparent;
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjectDescendantsSemiTransparency
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Enables or disables semi-transparency property for a SuperObject and
       his descendants
       1 -> enable
       0 -> disable
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSuperObjectDescendantsSemiTransparency(TDE_tdsSuperObject *p_stSuperObject, char cSemiTransparent)
{
    TDE_tdsSuperObject *p_stSuperObjectTemp;

    TDE_vChangeSuperObjectSemiTransparency(p_stSuperObject, cSemiTransparent);
    for ( p_stSuperObjectTemp = p_stSuperObject->p_stChild; p_stSuperObjectTemp != NULL; p_stSuperObjectTemp = p_stSuperObjectTemp->p_stRightBrother)
    {
      TDE_vChangeSuperObjectDescendantsSemiTransparency(p_stSuperObjectTemp, cSemiTransparent);
    }
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSuperObjectDescendantsAlpha
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Modify alpha channel for a superobject and its descendants
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSuperObjectDescendantsAlpha(TDE_tdsSuperObject *p_stSuperObject, TDE_tdxValue xAlpha)

{
    TDE_tdsSuperObject *p_stSuperObjectTemp;

    TDE_vChangeSuperObjectAlpha(p_stSuperObject, xAlpha);
    for ( p_stSuperObjectTemp = p_stSuperObject->p_stChild; p_stSuperObjectTemp != NULL; p_stSuperObjectTemp = p_stSuperObjectTemp->p_stRightBrother)
    {
      TDE_vChangeSuperObjectDescendantsAlpha(p_stSuperObjectTemp, xAlpha);
    }
}


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vChangeSourceCoordinates
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Changes the source picture coordinates of a sprite
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vChangeSourceCoordinates(TDE_tdsSprite *p_stSprite,
                                   TDE_tdxValue xXs,
                                   TDE_tdxValue xYs)
{
    p_stSprite->stSourceOrigin.xX    = xXs;
    p_stSprite->stSourceOrigin.xY    = xYs;    
}                                      



/************************************************************************

        TDE_INIT.C
        Third part : Get Functions

        - TDE_eGetCursorType
        - TDE_lGetCursorPosition
*************************************************************************/

TDE_tdeCursorType TDE_eGetCursorType ( TDE_tdsSuperObject *p_SuperObject )
{
    return p_SuperObject->p_stTextBox->eCursorType;
}

long TDE_lGetCursorPosition ( TDE_tdsSuperObject *p_SuperObject )
{
    return p_SuperObject->p_stTextBox->lCursorPosition;
}


/*********************************************************************

        TDE_INIT.C
        fourth part : LIBERATION FUNCTIONS
        
        - TDE_vReleaseMenuBitmapFont
        - TDE_vReleaseTrueTypeFont
        - TDE_vReleaseMemSprite
        - TDE_vDestroyPictureAttributes
        - TDE_vDestroyTextBox
        - TDE_vDestroySuperObject
        - TDE_vDestroySuperObjectDescendants
                
**********************************************************************/


/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vReleaseBitmapFont
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Destroy a bitmap font structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vReleaseBitmapFont(TDE_tdsBitmapFont *BFont)
{
    short i;

    if (BFont == NULL)
        return;
    for ( i=0; i<TDE_kNBLETTERS; i++ )
    {
        if (BFont->p_stMBLetter[i] != NULL)
        {
            if (BFont->p_stMBLetter[i]->stBLetter.p_stSprite != NULL)
            {
                if (BFont->p_stMBLetter[i]->stBLetter.p_stSprite->v_pData != NULL)
                {
                    // sprite data        
                    M_TdeFree( BFont->p_stMBLetter[i]->stBLetter.p_stSprite->v_pData );
                }
                // sprite         
                M_TdeFree( BFont->p_stMBLetter[i]->stBLetter.p_stSprite );
            }
            // Letter        
            M_TdeFree( BFont->p_stMBLetter[i]);
        }
    }
    M_TdeFree(BFont);
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vReleaseTrueTypeFont
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Destroy a true type font structure
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vReleaseTrueTypeFont(TDE_tdsTrueTypeFont *TFont)
{
    if (TFont == NULL)
        return;
    if (TFont->hFont != NULL)
        DeleteObject ((HGDIOBJ)TFont->hFont);
    M_TdeFree(TFont);
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vReleaseMemSprite
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Frees a memory sprite
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vReleaseMemSprite(TDE_tdsSprite *p_stSprite)
{
    if (p_stSprite)
    {
        if (p_stSprite->v_pData)
            M_TdeFree(p_stSprite->v_pData);
        M_TdeFree(p_stSprite);
    }
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vDestroyPictureAttributes
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Free a TDE_tdsPictureAttributes object
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vDestroyPictureAttributes(TDE_tdsPictureAttributes **h_stPicture)
{
    M_TdeFree(*h_stPicture);
    *h_stPicture = NULL;
}



/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vDestroyTextBox
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Free a tdsTextBox object
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vDestroyTextBox(TDE_tdsTextBox **h_stTextBox)
{
    if (*h_stTextBox)
    {
        if ((*h_stTextBox)->p_stFormatRect)
            M_TdeFree ((*h_stTextBox)->p_stFormatRect);
        if ((*h_stTextBox)->p_stClipRect)
            M_TdeFree ((*h_stTextBox)->p_stClipRect);
        M_TdeFree(*h_stTextBox);
        *h_stTextBox = NULL;
    }
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vDestroySuperObject
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Free a tdsSuperObject object
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
void TDE_vDestroySuperObject(TDE_tdsSuperObject **h_stSuperObject)
{
    if (*h_stSuperObject)
    {
        M_TdeFree(*h_stSuperObject);
        *h_stSuperObject = NULL;
    }
}

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
 NAME : TDE_vDestroySuperObjectDescendants
 VERSION : 2.0 / Valérie
           1.0 / Franck

 AIM : Free a tdsSuperObject object
**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**/

void TDE_vDestroySuperObjectDescendants(TDE_tdsSuperObject **h_stSuperObject)
{
        char NotFinished = 1;
        TDE_tdsSuperObject *p_stSuperObjectTemp, *p_stSuperObject = *h_stSuperObject;

        while (NotFinished)
        {
                if (p_stSuperObject->p_stChild) p_stSuperObject = p_stSuperObject->p_stChild;
                else
                {
                        if (p_stSuperObject->p_stRightBrother)
                        {
                                p_stSuperObjectTemp = p_stSuperObject;
                                p_stSuperObject = p_stSuperObject->p_stRightBrother;
                                if (p_stSuperObjectTemp->p_stFather)
                                {
                                        p_stSuperObject->p_stFather = p_stSuperObjectTemp->p_stFather;
                                        p_stSuperObject->p_stFather->p_stChild = p_stSuperObject;
                                }
                                //free(p_stSuperObjectTemp);
                                M_TdeFree(p_stSuperObjectTemp);
                        }
                        else
                        {
                                if (p_stSuperObject->p_stFather)
                                {
                                        p_stSuperObject = p_stSuperObject->p_stFather;
                                        //free(p_stSuperObject->p_stChild);
                                        M_TdeFree(p_stSuperObject->p_stChild);
                                        p_stSuperObject->p_stChild = NULL;
                                }
                                else
                                {
                                        //free(p_stSuperObject);
                                        M_TdeFree(p_stSuperObject);
                                        NotFinished = 0;
                                        (*h_stSuperObject) = NULL;
                                }
                        }
                }
        }
}


