view UI/UISaveLoad.cpp @ 1735:873ac151c38d

Draw SaveLoadWindow
author Ritor1
date Thu, 26 Sep 2013 17:39:39 +0600
parents e013b92030e3
children 1776d4f033a7
line wrap: on
line source

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <io.h>
#include "..\MM7.h"

#include "..\Keyboard.h"

#include "..\MapInfo.h"
#include "..\GUIWindow.h"
#include "..\GUIFont.h"
#include "..\Render.h"
#include "..\LOD.h"
#include "..\SaveLoad.h"
#include "..\texts.h"

#include "..\mm7_data.h"


//----- (004601B7) --------------------------------------------------------
static void UI_DrawSaveLoad(bool save)
{
  const char *pSlotName; // edi@36
  GUIWindow save_load_window; // [sp+Ch] [bp-78h]@8
  unsigned int pSaveFiles; // [sp+70h] [bp-14h]@10
  unsigned __int64 full_hours;
  unsigned __int64 full_days;
  int full_weeks;
  int full_month;
  int current_year;
  int current_month;
  int current_day;
  int current_hour;
  int current_minutes;

  pRenderer->BeginScene();
  if ( GetCurrentMenuID() != MENU_SAVELOAD && GetCurrentMenuID() != MENU_LoadingProcInMainMenu )
  {
    pRenderer->DrawTextureIndexed(8, 8, pIcons_LOD->GetTexture(uTextureID_loadsave));
    if (save)
    {
      pRenderer->DrawTextureIndexed(241, 302, pIcons_LOD->GetTexture(uTextureID_LS_saveU));
      pRenderer->DrawTextureIndexed( 18, 139, pIcons_LOD->GetTexture(uTextureID_save_up));
    }
    else
    {
      pRenderer->DrawTextureIndexed(241, 302, pIcons_LOD->GetTexture(uTextureID_LS_loadU));
      pRenderer->DrawTextureIndexed( 18, 139, pIcons_LOD->GetTexture(uTextureID_load_up));
    }
    pRenderer->DrawTextureIndexed(351, 302, pIcons_LOD->GetTexture(uTextureID_x_u));
  }
  if ( pSavegameUsedSlots[uLoadGameUI_SelectedSlot] )
  {
    memset(&save_load_window, 0, 0x54);
    save_load_window.uFrameX = pGUIWindow_CurrentMenu->uFrameX + 240;
    save_load_window.uFrameWidth = 220;
    save_load_window.uFrameY = (pGUIWindow_CurrentMenu->uFrameY - pFontSmallnum->uFontHeight) + 157;
    save_load_window.uFrameZ = save_load_window.uFrameX + 219;
    save_load_window.uFrameHeight = pFontSmallnum->uFontHeight;
    save_load_window.uFrameW = pFontSmallnum->uFontHeight + save_load_window.uFrameY - 1;
    if ( pSavegameThumbnails[uLoadGameUI_SelectedSlot].pPixels )
      pRenderer->DrawTextureRGB(pGUIWindow_CurrentMenu->uFrameX + 276, pGUIWindow_CurrentMenu->uFrameY + 171, &pSavegameThumbnails[uLoadGameUI_SelectedSlot]);
//Draw map name
    save_load_window.DrawTitleText(pFontSmallnum, 0, 0, 0, pMapStats->pInfos[pMapStats->GetMapInfo(pSavegameHeader[uLoadGameUI_SelectedSlot].pLocationName)].pName, 3);
//Draw date
    full_hours = ((signed __int64)(pSavegameHeader[uLoadGameUI_SelectedSlot].uWordTime * 0.234375) / 60) / 60i64;
    full_days = (unsigned int)full_hours / 24;
    full_weeks = (unsigned int)(full_days / 7);
    full_month = (unsigned int)full_weeks / 4;
    current_year = (full_month / 12) + game_starting_year;
    current_month = full_month % 12;
    current_day = full_days % 28;
    current_hour = full_hours % 24;
    current_minutes = (((signed __int64)(pSavegameHeader[uLoadGameUI_SelectedSlot].uWordTime * 0.234375) / 60) % 60i64);

    save_load_window.uFrameY = pGUIWindow_CurrentMenu->uFrameY + 261;
    int am;
    if ( (signed int)current_hour >= 12 )
    {
      current_hour -= 12;
      if ( !current_hour )
        current_hour = 12;
      am = 1;
    }
    else
      am = 0;
    auto day = aDayNames[full_days % 7];
    auto ampm = aAMPMNames[am];
    auto month = aMonthNames[current_month];

    sprintfex(pTmpBuf.data(), "%s %d:%02d %s\n%d %s %d", day, current_hour, current_minutes, aAMPMNames[am], current_day + 1, month, current_year);
    save_load_window.DrawTitleText(pFontSmallnum, 0, 0, 0, pTmpBuf.data(), 3);
  }
  if ( pGUIWindow_CurrentMenu->receives_keyboard_input_2 == WINDOW_INPUT_CONFIRMED)
  {
    pGUIWindow_CurrentMenu->receives_keyboard_input_2 = WINDOW_INPUT_NONE;
    strcpy((char *)&pSavegameHeader + 100 * uLoadGameUI_SelectedSlot, (const char *)pKeyActionMap->pPressedKeysBuffer);
    pMessageQueue_50CBD0->AddMessage(UIMSG_SaveGame, 0, 0);
  }
  else
  {
    if ( pGUIWindow_CurrentMenu->receives_keyboard_input_2 == WINDOW_INPUT_CANCELLED)
      pGUIWindow_CurrentMenu->receives_keyboard_input_2 = WINDOW_INPUT_NONE;
  }
  if (GetCurrentMenuID() == MENU_LoadingProcInMainMenu)
  {
    pGUIWindow_CurrentMenu->DrawText(pFontSmallnum, pFontSmallnum->AlignText_Center(186, pGlobalTXT_LocalizationStrings[135]) + 25,
        220, 0, pGlobalTXT_LocalizationStrings[135], 0, 0, 0);//Загрузка
    pGUIWindow_CurrentMenu->DrawTextInRect(pFontSmallnum, pFontSmallnum->AlignText_Center(186,
        (const char *)pSavegameHeader.data() + 100 * uLoadGameUI_SelectedSlot) + 25, 0x106, 0, pSavegameHeader[uLoadGameUI_SelectedSlot].pName, 185, 0);
    pGUIWindow_CurrentMenu->DrawText(pFontSmallnum, pFontSmallnum->AlignText_Center(186, pGlobalTXT_LocalizationStrings[165]) + 25,
        304, 0, pGlobalTXT_LocalizationStrings[165], 0, 0, 0);//"Пожалуйста, пожождите"
  }
  else
  {
    if ( save )
      pSaveFiles = 40;
    else
      pSaveFiles = uNumSavegameFiles;

    int slot_Y = 199;
    for ( uint i = pSaveListPosition; i < pSaveFiles; ++i )
    {
      if ( slot_Y >= 346 )
        break;
      if ( pGUIWindow_CurrentMenu->receives_keyboard_input_2 != WINDOW_INPUT_IN_PROGRESS || i != uLoadGameUI_SelectedSlot )
        pGUIWindow_CurrentMenu->DrawTextInRect(pFontSmallnum, 27, slot_Y, i == uLoadGameUI_SelectedSlot ? TargetColor(0xFF, 0xFF, 0x64) : 0, pSavegameHeader[i].pName, 185, 0);
      else
        pGUIWindow_CurrentMenu->DrawFlashingInputCursor(pGUIWindow_CurrentMenu->DrawTextInRect(pFontSmallnum, 27, slot_Y, i == uLoadGameUI_SelectedSlot ? TargetColor(0xFF, 0xFF, 0x64) : 0, (const char *)pKeyActionMap->pPressedKeysBuffer, 175, 1) + 27,
           slot_Y, pFontSmallnum);
      slot_Y += 21;
    }
  }
  pRenderer->EndScene();
}
// 6A0C9C: using guessed type int dword_6A0C9C;

//----- (004606F7) --------------------------------------------------------
void LoadUI_Draw()
{
  UI_DrawSaveLoad(false);
}

//----- (004606FE) --------------------------------------------------------
void SaveUI_Draw()
{
  UI_DrawSaveLoad(true);
}


//----- (0045E361) --------------------------------------------------------
void LoadUI_Load(unsigned int uDialogueType)
{
    unsigned int v1; // ebp@5
    unsigned int v2; // eax@5
    //signed int v3; // ebp@11
    FILE *v4; // eax@14
    FILE *v5; // eax@18
    unsigned int v6; // eax@25
    GUIButton *v7; // eax@27
    const char *v8; // [sp-8h] [bp-26Ch]@25
    //char *v9; // [sp-4h] [bp-268h]@19
    enum TEXTURE_TYPE v10; // [sp-4h] [bp-268h]@25
    unsigned int uDialogueType_; // [sp+10h] [bp-254h]@1
    //RGBTexture *pTex; // [sp+10h] [bp-254h]@12
    //SavegameHeader *Dest; // [sp+14h] [bp-250h]@12
    //const char *Str1; // [sp+18h] [bp-24Ch]@12
    LODWriteableFile pLODFile; // [sp+1Ch] [bp-248h]@1
    int v16; // [sp+260h] [bp-4h]@1

    uDialogueType_ = uDialogueType;
    dword_6BE138 = -1;
    pIcons_LOD->_inlined_sub2();

    memset(pSavegameUsedSlots.data(), 0, sizeof(pSavegameUsedSlots));
    memset(pSavegameThumbnails.data(), 0, 45 * sizeof(RGBTexture));
    uTextureID_loadsave = pIcons_LOD->LoadTexture("loadsave", TEXTURE_16BIT_PALETTE);
    uTextureID_load_up = pIcons_LOD->LoadTexture("load_up", TEXTURE_16BIT_PALETTE);
    uTextureID_save_up = pIcons_LOD->LoadTexture("save_up", TEXTURE_16BIT_PALETTE);
    uTextureID_LS_loadU = pIcons_LOD->LoadTexture("LS_loadU", TEXTURE_16BIT_PALETTE);
    uTextureID_LS_saveU = pIcons_LOD->LoadTexture("LS_saveU", TEXTURE_16BIT_PALETTE);
    uTextureID_x_u = pIcons_LOD->LoadTexture("x_u", TEXTURE_16BIT_PALETTE);
    if ( uDialogueType_ )
        {
        pRenderer->DrawTextureIndexed(8, 8, pIcons_LOD->GetTexture(uTextureID_loadsave));
        if ( pCurrentScreen == SCREEN_SAVEGAME )
            {
            v1 = uTextureID_save_up;
            v2 = uTextureID_LS_saveU;
            }
        else
            {
            v1 = uTextureID_load_up;
            v2 = uTextureID_LS_loadU;
            }
        pRenderer->DrawTextureIndexed(241, 302, pIcons_LOD->GetTexture(v2));
        pRenderer->DrawTextureIndexed( 18, 141, pIcons_LOD->GetTexture(v1));
        pRenderer->DrawTextureIndexed(351, 302, pIcons_LOD->GetTexture(uTextureID_x_u));
        }
    else
        {
        pRenderer->DrawTextureRGB(0, 0, &pTexture_PCX);
        }
    pGUIWindow_CurrentMenu = GUIWindow::Create(saveload_dlg_xs[uDialogueType_], saveload_dlg_ys[uDialogueType_], saveload_dlg_zs[uDialogueType_],
        saveload_dlg_ws[uDialogueType_], WINDOW_MainMenu_Load, 0, 0);
    pGUIWindow_CurrentMenu->DrawText(pFontSmallnum, 25, 199, 0, pGlobalTXT_LocalizationStrings[505], 0, 0, 0);// "Reading..."
    pRenderer->Present();
    pSavegameList->Initialize(0);
    if ( pSaveListPosition > (signed int)uNumSavegameFiles )
        {
        pSaveListPosition = 0;
        uLoadGameUI_SelectedSlot = 0;
        }
    pLODFile.AllocSubIndicesAndIO(0x12C, 0);
    Assert(sizeof(SavegameHeader) == 0x64);
    //v3 = 0;
    for (uint i = 0; i < uNumSavegameFiles; ++i)
        {

        sprintf(pTmpBuf.data(), "saves\\%s", pSavegameList->pFileList[i].pSaveFileName);
        if (_access(pTmpBuf.data(), 6))
            {
            pSavegameUsedSlots[i] = 0;
            strcpy(pSavegameHeader[i].pName, pGlobalTXT_LocalizationStrings[72]); // "Empty"
            continue;
            }
        pLODFile.LoadFile(pTmpBuf.data(), 1);
        v4 = pLODFile.FindContainer("header.bin", true);
        if ( v4 )
            fread(&pSavegameHeader[i], 0x64, 1, v4);
        if ( !_stricmp(pSavegameList->pFileList[i].pSaveFileName, pGlobalTXT_LocalizationStrings[613]) )// "AutoSave.MM7"
            strcpy(pSavegameHeader[i].pName, pGlobalTXT_LocalizationStrings[16]);// "Autosave"
        v5 = pLODFile.FindContainer("image.pcx", true);
        if ( !v5 )
            {
            pSavegameUsedSlots[i] = 0;
            strcpy(pSavegameList->pFileList[i].pSaveFileName, "");
            }
        else
            {
            pSavegameThumbnails[i].LoadFromFILE(v5, 0, true);
            pLODFile.CloseWriteFile();
            pSavegameUsedSlots[i] = 1;
            }
        }

    //LABEL_24:
    pLODFile.FreeSubIndexAndIO();
    if ( pCurrentScreen == SCREEN_SAVEGAME )
        {
        uTextureID_x_d = pIcons_LOD->LoadTexture("x_d", TEXTURE_16BIT_PALETTE);
        uTextureID_LS_ = pIcons_LOD->LoadTexture("LS_saveD",TEXTURE_16BIT_PALETTE);
        }
    else
        {
        uTextureID_x_d = pIcons_LOD->LoadTexture("x_d", TEXTURE_16BIT_PALETTE);
        uTextureID_LS_ = pIcons_LOD->LoadTexture("LS_loadD",TEXTURE_16BIT_PALETTE);
        }
    uTextureID_AR_UP_DN = pIcons_LOD->LoadTexture("AR_UP_DN", TEXTURE_16BIT_PALETTE);
    uTextureID_AR_DN_DN = pIcons_LOD->LoadTexture("AR_DN_DN", TEXTURE_16BIT_PALETTE);
    pGUIWindow_CurrentMenu->CreateButton(21, 198, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 0, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 219, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 1, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 240, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 2, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 261, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 3, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 282, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 4, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 303, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 5, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 324, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 6, 0, "", 0);
    pBtnLoadSlot  = pGUIWindow_CurrentMenu->CreateButton(241, 302, 105, 40, 1, 0, UIMSG_SaveLoadBtn, 0, 0, "", pIcons_LOD->GetTexture(uTextureID_LS_), 0);
    pBtnCancel    = pGUIWindow_CurrentMenu->CreateButton(350, 302, 105, 40, 1, 0, UIMSG_Cancel, 0, 0, "", pIcons_LOD->GetTexture(uTextureID_x_d), 0);
    pBtnArrowUp   = pGUIWindow_CurrentMenu->CreateButton(215, 199,  17, 17, 1, 0, UIMSG_ArrowUp, 0, 0, "", pIcons_LOD->GetTexture(uTextureID_AR_UP_DN), 0);
    pBtnDownArrow = pGUIWindow_CurrentMenu->CreateButton(215, 323,  17, 17, 1, 0, UIMSG_DownArrow, uNumSavegameFiles, 0, "", pIcons_LOD->GetTexture(uTextureID_AR_DN_DN), 0);
}

//----- (0045E93E) --------------------------------------------------------
void SaveUI_Load()
{
    unsigned int v0; // ebp@4
    unsigned int v1; // eax@4
    char *v3; // eax@7
    FILE *v4; // eax@11
    FILE *v5; // eax@11
    LODWriteableFile v11; // [sp+1Ch] [bp-248h]@1

    ++pIcons_LOD->uTexturePacksCount;
    if ( !pIcons_LOD->uNumPrevLoadedFiles )
        pIcons_LOD->uNumPrevLoadedFiles = pIcons_LOD->uNumLoadedFiles;
    memset(pSavegameUsedSlots.data(), 0, 0xB4u);
    memset(&pSavegameThumbnails, 0, 0x708u);
    uTextureID_loadsave = pIcons_LOD->LoadTexture("loadsave", TEXTURE_16BIT_PALETTE);
    uTextureID_load_up = pIcons_LOD->LoadTexture("load_up", TEXTURE_16BIT_PALETTE);
    uTextureID_save_up = pIcons_LOD->LoadTexture("save_up", TEXTURE_16BIT_PALETTE);
    uTextureID_LS_loadU = pIcons_LOD->LoadTexture("LS_loadU", TEXTURE_16BIT_PALETTE);
    uTextureID_LS_saveU = pIcons_LOD->LoadTexture("LS_saveU", TEXTURE_16BIT_PALETTE);
    uTextureID_x_u = pIcons_LOD->LoadTexture("x_u", TEXTURE_16BIT_PALETTE);
    pRenderer->DrawTextureIndexed(8, 8, pIcons_LOD->GetTexture(uTextureID_loadsave));
    if ( pCurrentScreen == SCREEN_SAVEGAME )
    {
        v0 = uTextureID_save_up;
        v1 = uTextureID_LS_saveU;
        }
    else
    {
        v0 = uTextureID_load_up;
        v1 = uTextureID_LS_loadU;
        }
    pRenderer->DrawTextureIndexed(0xF1u, 0x12Eu, pIcons_LOD->GetTexture(v1));
    pRenderer->DrawTextureIndexed(0x15Fu, 0x12Eu, pIcons_LOD->GetTexture(uTextureID_x_u));
    pRenderer->DrawTextureIndexed(0x12u, 0x8Du, pIcons_LOD->GetTexture(v0));
    pGUIWindow_CurrentMenu->DrawText(pFontSmallnum, 25, 199, 0, pGlobalTXT_LocalizationStrings[505], 0, 0, 0);
    pRenderer->Present();
    pSavegameList->Initialize(1u);
    v11.AllocSubIndicesAndIO(0x12Cu, 0);
    //v2 = pSavegameUsedSlots;
    // Dest = pSavegameHeader;
    // this_ = pSavegameThumbnails;
    // v8 = (char *)pSavegameList->pSavesNames;
    for (uint i = 0; i < 40; ++i)
        {
        v3 = pSavegameList->pFileList[i].pSaveFileName;
        if ( !*pSavegameList->pFileList[i].pSaveFileName )
            v3 = "1.mm7";
        sprintf(pTmpBuf.data(), "saves\\%s", v3);
        if ( _access(pTmpBuf.data(), 0) || _access(pTmpBuf.data(), 6) )
            {
            pSavegameUsedSlots[i] = 0;
            strcpy(pSavegameHeader[i].pName, pGlobalTXT_LocalizationStrings[LOCSTR_EMPTY]);
            }
        else
            {
            v11.LoadFile(pTmpBuf.data(), 1);
            v4 = v11.FindContainer("header.bin", 1);
            fread(&pSavegameHeader[i], 100, 1u, v4);
            v5 = v11.FindContainer("image.pcx", 1);
            if ( v5 )
                {
                pSavegameThumbnails[i].LoadFromFILE(v5, 0, 1u);
                v11.CloseWriteFile();
                pSavegameUsedSlots[i] = 1;
                }
            else
                {
                pSavegameUsedSlots[i] = 0;
                }
            }

        }

    v11.FreeSubIndexAndIO();
    uTextureID_x_d = pIcons_LOD->LoadTexture("x_d", TEXTURE_16BIT_PALETTE);
    uTextureID_LS_ = pIcons_LOD->LoadTexture("LS_saveD", TEXTURE_16BIT_PALETTE);
    uTextureID_AR_UP_DN = pIcons_LOD->LoadTexture("AR_UP_DN", TEXTURE_16BIT_PALETTE);
    uTextureID_AR_DN_DN = pIcons_LOD->LoadTexture("AR_DN_DN", TEXTURE_16BIT_PALETTE);
    pGUIWindow_CurrentMenu = GUIWindow::Create(0, 0, 640, 480, WINDOW_SaveLoadButtons, 0, 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 198, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 0, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 218, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 1, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 238, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 2, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 258, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 3, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 278, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 4, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 298, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 5, 0, "", 0);
    pGUIWindow_CurrentMenu->CreateButton(21, 318, 191, 18, 1, 0, UIMSG_SelectLoadSlot, 6, 0, "", 0);
    pBtnLoadSlot  = pGUIWindow_CurrentMenu->CreateButton(241, 302, 105, 40, 1, 0, UIMSG_SaveLoadBtn, 0, 0, "", pIcons_LOD->GetTexture(uTextureID_LS_), 0);
    pBtnCancel    = pGUIWindow_CurrentMenu->CreateButton(350, 302, 105, 40, 1, 0, UIMSG_Cancel, 0, 0, "", pIcons_LOD->GetTexture(uTextureID_x_d), 0);
    pBtnArrowUp   = pGUIWindow_CurrentMenu->CreateButton(215, 199,  17, 17, 1, 0, UIMSG_ArrowUp, 0, 0, "", pIcons_LOD->GetTexture(uTextureID_AR_UP_DN), 0);
    pBtnDownArrow = pGUIWindow_CurrentMenu->CreateButton(215, 323,  17, 17, 1, 0, UIMSG_DownArrow, 34, 0, "", pIcons_LOD->GetTexture(uTextureID_AR_DN_DN), 0);
}