view Mouse.cpp @ 1231:182b910c528d

Слияние
author Ritor1
date Sun, 09 Jun 2013 10:18:51 +0600
parents 29a8defbad9e
children fac7751b2dc9
line wrap: on
line source

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "OSAPI.h"

#include "VideoPlayer.h"
#include "Vis.h"
#include "Mouse.h"
#include "Items.h"
#include "Party.h"
#include "LOD.h"
#include "Game.h"

#include "stru11.h"





Mouse *pMouse;





//----- (00469860) --------------------------------------------------------
void Mouse::GetClickPos(unsigned int *pX, unsigned int *pY)
{
  *pX = uMouseClickX;
  *pY = uMouseClickY;
}

//----- (004698A6) --------------------------------------------------------
void Mouse::RemoveHoldingItem()
{
  pParty->pPickedItem.Reset();
  if (_stricmp(pCurrentCursorName, "MICON2"))
    SetCursorBitmap("MICON1");
}

//----- (004698D8) --------------------------------------------------------
void Mouse::SetCursorBitmapFromItemID(unsigned int uItemID)
{
  pMouse->SetCursorBitmap(pItemsTable->pItems[uItemID].pIconName);
}

//----- (004698F6) --------------------------------------------------------
void Mouse::SetCurrentCursorBitmap()
{
  SetCursorBitmap(pCurrentCursorName);
}

//----- (00469903) --------------------------------------------------------
void Mouse::SetCursorBitmap(const char *pName)
{
  Mouse *v2; // esi@1
  HCURSOR v3; // eax@10
  //int v4; // ecx@10
  double v5; // st7@11
  float v6; // ST04_4@12
  LONG v7; // eax@14
  LONG v8; // eax@17
  unsigned int v9; // eax@21
  float v10; // [sp+4h] [bp-18h]@11
  struct tagPOINT Point; // [sp+14h] [bp-8h]@20

  v2 = this;
  if ( !this->bInitialized || !pName )
    return;
  if ( _stricmp("MICON2", pName) )
    pGame->uFlags2 &= 0xFFFFFFEFu;
  else
    pGame->uFlags2 |= 0x10u;
  if ( _stricmp(v2->pCurrentCursorName, pName) )
    strcpy(v2->pCurrentCursorName, pName);
  ClearCursor();
  if ( _strnicmp(pName, "MICON1", 5u) )
  {
    v9 = pIcons_LOD->LoadTexture(pName, TEXTURE_16BIT_PALETTE);
    v2->uCursorTextureID = v9;
    v2->uCursorTextureID_2 = v9;
    v2->AllocCursorSystemMem();
    v2->field_C = 0;
    v2->bRedraw = 1;
    v2->bActive = 1;
    if ( !areWeLoadingTexture )
    {
      if (uCursorTextureID != -1)
        pIcons_LOD->pTextures[uCursorTextureID].Release();
      pIcons_LOD->SyncLoadedFilesCount();
    }
    return;
  }
  v2->bActive = 0;
  v2->field_C = 1;
  if ( !strcmp(pName, "MICON1") )
  {
    v3 = LoadCursorA(GetModuleHandleW(nullptr), "Arrow");
    SetClassLongA(hWnd, GCL_HCURSOR, (LONG)v3);
  }
  else if ( !strcmp(pName, "MICON2") )
  {
    v7 = (LONG)LoadCursorA(GetModuleHandleW(nullptr), "Target");
    SetClassLongA(hWnd, -12, v7);
  }
  else if ( !strcmp(pName, "MICON3") )
  {
    v8 = (LONG)LoadCursorA(0, (LPCSTR)IDC_WAIT);
    SetClassLongA(hWnd, -12, v8);
  }
//LABEL_18:

    GetCursorPos(&Point);
	ClientToScreen(hWnd,&Point);
    SetCursorPos(Point.x, Point.y);
}
// 506128: using guessed type int areWeLoadingTexture;

//----- (00469AE4) --------------------------------------------------------
LONG Mouse::_469AE4()
{
  Mouse *v1; // esi@1
  LONG v2; // ecx@2
  LONG result; // eax@2
  struct tagPOINT Point; // [sp+Ch] [bp-8h]@2

  v1 = this;
  this->field_8 = 1;
  /*if (pAsyncMouse)
  {
    v2 = *((int *)pAsyncMouse + 6);
    Point.x = *((int *)pAsyncMouse + 6);
    result = *((int *)pAsyncMouse + 7);
  }
  else
  {*/
    GetCursorPos(&Point);
    if ( pRenderer->bWindowMode )
      ScreenToClient(hWnd, &Point);
    result = Point.y;
    v2 = Point.x;
  //}
  v1->uMouseClickX = v2;
  v1->uMouseClickY = result;
  if ( pRenderer->bWindowMode )
    goto LABEL_16;
  //if (pAsyncMouse)
  //  goto LABEL_24;
  if ( v2 < 0 )
    v2 = 0;
  if ( result < 0 )
    result = 0;
  if ( v2 > 639 )
    v2 = 639;
  if ( result > 479 )
  {
    result = 479;
LABEL_16:
    //if (pAsyncMouse)
    //  goto LABEL_24;
    if ( pRenderer->bWindowMode && (v2 < 0 || result < 0 || v2 > 639 || result > 479) )
      goto LABEL_23;
  }
  if ( v1->field_C )
LABEL_23:
    v1->bActive = 0;
LABEL_24:
  v1->field_8 = 0;
  return result;
}

//----- (00469BA3) --------------------------------------------------------
void Mouse::ClearCursor()
{
  Mouse *v1; // esi@1
  void *v2; // eax@1
  void **v3; // esi@5

  v1 = this;
  v2 = this->pCursorBitmap_sysmem;
  this->bActive = 0;
  if ( v2 )
  {
    free(v2);
    v1->pCursorBitmap_sysmem = 0;
  }
  if ( v1->pCursorBitmap2_sysmem )
  {
    free(v1->pCursorBitmap2_sysmem);
    v1->pCursorBitmap2_sysmem = 0;
  }
  v3 = &v1->ptr_90;
  if ( *v3 )
    free(*v3);
  *v3 = 0;
}

//----- (00469BE6) --------------------------------------------------------
void Mouse::AllocCursorSystemMem()
{
  bActive = 0;
  if (!pCursorBitmap_sysmem)
    pCursorBitmap_sysmem = (unsigned __int16 *)DoAllocCursorMem();
  if (!pCursorBitmap2_sysmem)
    pCursorBitmap2_sysmem = (unsigned __int8 *)DoAllocCursorMem();
}

//----- (00469C0D) --------------------------------------------------------
void *Mouse::DoAllocCursorMem()
{
  auto tex = pIcons_LOD->GetTexture(uCursorTextureID);
  return malloc(4 * tex->uTextureWidth * tex->uTextureHeight);
}

//----- (00469C39) --------------------------------------------------------
POINT *Mouse::GetCursorPos(POINT *a2)
{
  a2->x = this->uMouseClickX;
  a2->y = this->uMouseClickY;
  return a2;
}

//----- (00469C65) --------------------------------------------------------
void Mouse::Initialize(HWND hWnd)
{
  this->hWnd = hWnd;
  this->bActive = 0;
  this->bInitialized = 1;
  this->pCursorBitmapPos.x = 0;
  this->pCursorBitmapPos.y = 0;
  this->uMouseClickX = 0;
  this->uMouseClickY = 0;
  this->pCursorBitmap_sysmem = 0;
  this->field_34 = 0;
  this->pCursorBitmap2_sysmem = 0;

  SetCursorBitmap("MICON3");
  SetCursorBitmap("MICON2");
  SetCursorBitmap("MICON1");
}

// inlined
//----- (0045FE00) mm6 chinese --------------------------------------------
void Mouse::SetActive(bool active)
{
  bActive = active;
}

//----- (00469CC2) --------------------------------------------------------
void Mouse::Deactivate()
{
  if (bInitialized)
    SetActive(false);
}

//----- (00469CCD) --------------------------------------------------------
void Mouse::DrawCursor()
{
  Mouse *v1; // esi@1
  char v11; // zf@6
  signed int v3; // eax@6
  signed int v4; // ecx@6
  Vec4_int_ *v5; // edx@21
  int v6; // edi@21
  int v7; // ebx@21
  int v8; // eax@29
  unsigned int v9; // eax@31

  v1 = this;
  if ( this->bInitialized )
  {
    if ( !this->field_8 && this->bActive && !this->field_C )
      pMouse->_469AE4();
    v11 = v1->field_C == 0;
    v3 = v1->uMouseClickX;
    v4 = v1->uMouseClickY;
    v1->field_F4 = 1;
    if ( !v11 )
      goto LABEL_12;
    if ( pRenderer->bWindowMode )
    {
      if ( v3 < 0 || v4 < 0 || v3 > 639 || v4 > 479 )
      {
LABEL_12:
        v1->field_F4 = 0;
        return;
      }
    }
    else
    {
      if ( v3 < 0 )
        v3 = 0;
      if ( v4 < 0 )
        v4 = 0;
      if ( v3 > 639 )
        v3 = 639;
      if ( v4 > 479 )
        v4 = 479;
    }
    v5 = &v1->pCursorBitmapRect;
    v6 = v3 + v1->uCursorBitmapPitch;
    v7 = v4 + v1->field_5C[0];
    v1->pCursorBitmapRect.y = v4;
    v1->pCursorBitmapRect.x = v3;
    v1->pCursorBitmapRect.z = v6;
    v1->pCursorBitmapRect.w = v7;
    if ( v3 < 0 )
      v5->x = 0;
    if ( v4 < 0 )
      v1->pCursorBitmapRect.y = 0;
    if ( v6 > 640 )
      v1->pCursorBitmapRect.z = 640;
    if ( v7 > 480 )
      v1->pCursorBitmapRect.w = 480;
    v8 = v1->pCursorBitmapRect.z;
    v1->bActive = 0;
    v1->uCursorBitmapWidth = v8 - v5->x;
    v11 = v1->bRedraw == 0;
    v1->uCursorBitmapHeight = v1->pCursorBitmapRect.w - v1->pCursorBitmapRect.y;
    if ( !v11 )
    {
      if ( pMouse->ptr_90 )
        v9 = 2 * pMouse->uCursorBitmapPitch;
      else
        v9 = 0;
      pRenderer->_4A6DF5(
        v1->pCursorBitmap_sysmem,
        v9,
        &v1->pCursorBitmapPos,
        pRenderer->pTargetSurface,
        pRenderer->uTargetSurfacePitch,
        &v1->pCursorBitmapRect);
      v1->bRedraw = false;
    }
  }
}

//----- (00469E1C) --------------------------------------------------------
void Mouse::_469E1C()
{
  bActive = true;
}

//----- (00469E24) --------------------------------------------------------
void Mouse::_469E24()
{
  if (pCursorBitmap3_sysmembits_16bit)
  {
    free(pCursorBitmap3_sysmembits_16bit);
    pCursorBitmap3_sysmembits_16bit = 0;
  }
}

//----- (00469E3B) --------------------------------------------------------
void Mouse::DrawCursorToTarget()
{
  if (!pCursorBitmap3_sysmembits_16bit)
    return;

  auto pSrc = pCursorBitmap3_sysmembits_16bit;
  for (uint y = field_44; y < field_4C; ++y)
    for (uint x = field_40; x < field_48; ++x)
      pRenderer->pTargetSurface[y * pRenderer->uTargetSurfacePitch + x] = *pSrc++;
}

//----- (00469EA4) --------------------------------------------------------
void Mouse::_469EA4()
{
  //Mouse *v1; // esi@1
  unsigned int pTextureID; // eax@2
  Texture *pTexture; // edi@2
  unsigned int v4; // ecx@3
  unsigned int v5; // eax@3
  unsigned int v6; // ebx@5
  int v7; // ecx@15
  int v8; // ecx@25
  int v9; // ebx@26
  unsigned int v10; // eax@26
  int v11; // edx@27
  unsigned __int16 *v12; // edx@29
  unsigned __int16 *v13; // ebx@29
  unsigned int a2; // [sp+Ch] [bp-1Ch]@5
  unsigned int v15; // [sp+10h] [bp-18h]@5
  unsigned int v16; // [sp+14h] [bp-14h]@25
  unsigned __int16 *v17; // [sp+18h] [bp-10h]@25
  int v18; // [sp+1Ch] [bp-Ch]@27
  int v19; // [sp+20h] [bp-8h]@15
  unsigned __int16 *v20; // [sp+20h] [bp-8h]@28
  int pTextureWidth; // [sp+24h] [bp-4h]@12
  unsigned __int16 *v22; // [sp+24h] [bp-4h]@25

  //v1 = this;
  if ( pParty->pPickedItem.uItemID )
  {
    pTextureID = pIcons_LOD->LoadTexture(pItemsTable->pItems[pParty->pPickedItem.uItemID].pIconName, TEXTURE_16BIT_PALETTE);
    pTexture = (Texture *)(pTextureID != -1 ? (int)&pIcons_LOD->pTextures[pTextureID] : 0);

      v4 = pMouse->uMouseClickX;
      v5 = pMouse->uMouseClickY;

    v6 = v5;
    a2 = v4;
    v15 = v5;
    if ( (signed int)v4 <= 639 && (signed int)v5 <= 479 )
    {
      if ( (v4 & 0x80000000u) != 0 )
        a2 = 0;
      if ( (v5 & 0x80000000u) != 0 )
      {
        v6 = 0;
        v15 = 0;
      }
      if ( (signed int)(pTexture->uTextureWidth + a2) <= 640 )
        pTextureWidth = pTexture->uTextureWidth;
      else
        pTextureWidth = 640 - a2;
      if ( (signed int)(pTexture->uTextureHeight + v6) <= 480 )
      {
        v19 = pTexture->uTextureHeight;
        v7 = pTexture->uTextureHeight;
      }
      else
      {
        v7 = 480 - v6;
        v19 = 480 - v6;
      }
      if ( !this->pCursorBitmap3_sysmembits_16bit
        || a2 != this->field_40
        || v6 != this->field_44
        || a2 + pTextureWidth != this->field_48
        || v6 + v7 != this->field_4C )
      {
        if ( this->pCursorBitmap3_sysmembits_16bit )
          free(this->pCursorBitmap3_sysmembits_16bit);
        this->pCursorBitmap3_sysmembits_16bit = (unsigned __int16 *)operator new(2 * pTexture->uTextureHeight * pTexture->uTextureWidth);
        this->field_40 = a2;
        this->field_48 = a2 + pTextureWidth;
        this->field_44 = v6;
        this->field_4C = v6 + v19;
      }
      v8 = this->field_44;
      v22 = this->pCursorBitmap3_sysmembits_16bit;
      v17 = pRenderer->pTargetSurface;
      v16 = pRenderer->uTargetSurfacePitch;
      if ( v8 < this->field_4C )
      {
        v9 = this->field_48;
        v10 = pRenderer->uTargetSurfacePitch * v8;
        do
        {
          v11 = this->field_40;
          v18 = this->field_40;
          if ( v11 < v9 )
          {
            v20 = &v17[v10 + v11];
            do
            {
              v12 = v20;
              v13 = v22;
              ++v18;
              ++v20;
              ++v22;
              *v13 = *v12;
              v9 = this->field_48;
            }
            while ( v18 < v9 );
          }
          v10 += v16;
          ++v8;
        }
        while ( v8 < this->field_4C );
        v6 = v15;
      }
      if (pParty->pPickedItem.Broken())
        pRenderer->DrawTransparentRedShade(a2, v6, pTexture);
      else if (!pParty->pPickedItem.Identified())
        pRenderer->DrawTransparentGreenShade(a2, v6, pTexture);
      else
        pRenderer->DrawTextureTransparent(a2, v6, pTexture);
    }
  }
  else
  {
    if ( this->pCursorBitmap3_sysmembits_16bit )
    {
      free(this->pCursorBitmap3_sysmembits_16bit);
      this->pCursorBitmap3_sysmembits_16bit = 0;
    }
  }
}

//----- (0046A080) --------------------------------------------------------
void Mouse::Activate(int bActive)
{
  bActive = bActive;
}

//----- (0046A08A) --------------------------------------------------------
void Mouse::SetMouseClick(int x, int y)
{
  uMouseClickX = x;
  uMouseClickY = y;
}