⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winwindow.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
📖 第 1 页 / 共 4 页
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

#include "platformWin32/platformWin32.h"
#include "platform/platform.h"
#include "platform/platformIME.h"//IME
#include "platformWin32/platformGL.h"
#include "platform/platformVideo.h"
#include "platformWin32/winOGLVideo.h"
#include "platformWin32/winD3DVideo.h"
#include "platformWin32/winV2Video.h"
#include "platform/event.h"
#include "console/console.h"
#include "platformWin32/winConsole.h"
#include "platformWin32/winDirectInput.h"
#include "platform/gameInterface.h"
#include "math/mRandom.h"
#include "core/fileStream.h"
#include "game/resource.h"
#include "core/unicode.h"
#ifdef TGE_RPG
#include "gui/core/GuiControl.h"
#endif
//-------------------------------------- Resource Includes
#include "dgl/gBitmap.h"
#include "dgl/gFont.h"

extern void createFontInit();
extern void createFontShutdown();
extern void installRedBookDevices();
extern void handleRedBookCallback(U32, U32);

#ifdef TGE_RPG
	#ifdef UNICODE
	static const UTF16 *windowClassName = L"GWorld Window Class";
	static UTF16 windowName[256] = L"GWorld Window";
	#else
	static const char *windowClassName = "GWorld Window Class";
	static char windowName[256] = "GWorld Window";
	#endif
#else
	#ifdef UNICODE
	static const UTF16 *windowClassName = L"Darkstar Window Class";
	static UTF16 windowName[256] = L"Darkstar Window";
	#else
	static const char *windowClassName = "Darkstar Window Class";
	static char windowName[256] = "Darkstar Window";
	#endif
#endif


static bool gWindowCreated = false;

static bool windowNotActive = false;

static MRandomLCG sgPlatRandom;
static bool sgQueueEvents;

// is keyboard input a standard (non-changing) VK keycode
#define dIsStandardVK(c) (((0x08 <= (c)) && ((c) <= 0x12)) || \
                          ((c) == 0x1b) ||                    \
                          ((0x20 <= (c)) && ((c) <= 0x2e)) || \
                          ((0x30 <= (c)) && ((c) <= 0x39)) || \
                          ((0x41 <= (c)) && ((c) <= 0x5a)) || \
                          ((0x70 <= (c)) && ((c) <= 0x7B)))

extern U16  DIK_to_Key( U8 dikCode );

Win32PlatState winState;

//--------------------------------------
Win32PlatState::Win32PlatState()
{
   log_fp      = NULL;
   hinstOpenGL = NULL;
   hinstGLU    = NULL;
   hinstOpenAL = NULL;
   appWindow   = NULL;
   appDC       = NULL;
   appInstance = NULL;
   currentTime = 0;
   processId   = 0;
}

static bool windowLocked = false;

static BYTE keyboardState[256];
static bool mouseButtonState[3];
static bool capsLockDown = false;
static S32 modifierKeys = 0;
static bool windowActive = true;
static Point2I lastCursorPos(0,0);
#ifdef TGE_RPG
static Point2I windowSize(0,0);
#else
static Point2I windowSize;
#endif
static HANDLE gMutexHandle = NULL;
static bool sgDoubleByteEnabled = false;

//--------------------------------------
static const char *getMessageName(S32 msg)
{
   switch(msg)
   {
      case WM_KEYDOWN:
         return "WM_KEYDOWN";
      case WM_KEYUP:
         return "WM_KEYUP";
      case WM_SYSKEYUP:
         return "WM_SYSKEYUP";
      case WM_SYSKEYDOWN:
         return "WM_SYSKEYDOWN";
      default:
         return "Unknown!!";
   }
}

//--------------------------------------
bool Platform::excludeOtherInstances(const char *mutexName)
{
#ifdef UNICODE
   UTF16 b[512];
   convertUTF8toUTF16((UTF8 *)mutexName, b, sizeof(b));
   gMutexHandle = CreateMutex(NULL, true, b);
#else
   gMutexHandle = CreateMutex(NULL, true, mutexName);
#endif
   if(!gMutexHandle)
      return false;

   if(GetLastError() == ERROR_ALREADY_EXISTS)
   {
      CloseHandle(gMutexHandle);
      gMutexHandle = NULL;
      return false;
   }

   return true;
}

//--------------------------------------
void Platform::AlertOK(const char *windowTitle, const char *message)
{
   ShowCursor(true);
#ifdef UNICODE
   UTF16 m[1024], t[512];
   convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t));
   convertUTF8toUTF16((UTF8 *)message, m, sizeof(m));
   MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OK);
#else
   MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OK);
#endif
}

//--------------------------------------
bool Platform::AlertOKCancel(const char *windowTitle, const char *message)
{
   ShowCursor(true);
#ifdef UNICODE
   UTF16 m[1024], t[512];
   convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t));
   convertUTF8toUTF16((UTF8 *)message, m, sizeof(m));
   return MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OKCANCEL) == IDOK;
#else
   return MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OKCANCEL) == IDOK;
#endif
}

//--------------------------------------
bool Platform::AlertRetry(const char *windowTitle, const char *message)
{
   ShowCursor(true);
#ifdef UNICODE
   UTF16 m[1024], t[512];
   convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t));
   convertUTF8toUTF16((UTF8 *)message, m, sizeof(m));
   return (MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_RETRYCANCEL) == IDRETRY);
#else
   return (MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_RETRYCANCEL) == IDRETRY);
#endif
}

//--------------------------------------
HIMC gIMEContext;

static void InitInput()
{
#ifndef TORQUE_LIB
//#ifdef UNICODE
#if defined(TGE_RPG) || defined(UNICODE)
   gIMEContext = ImmGetContext(winState.appWindow);
   ImmReleaseContext( winState.appWindow, gIMEContext );
#endif
#endif

   dMemset( keyboardState, 0, 256 );
   dMemset( mouseButtonState, 0, sizeof( mouseButtonState ) );

   capsLockDown = (GetKeyState(VK_CAPITAL) & 0x01);

   if (capsLockDown)
   {
      keyboardState[VK_CAPITAL] |= 0x01;
   }
}

//--------------------------------------
// TGE_Mouse 修正了RPG鼠标位置
//static void setMouseClipping()
static void setMouseClipping(bool bRestoreCursor)
{
   ClipCursor(NULL);
   if(windowActive)
   {
      ShowCursor(false);

      RECT r;
      GetWindowRect(winState.appWindow, &r);

      if(windowLocked)
      {
         POINT p;
         GetCursorPos(&p);
         lastCursorPos.set(p.x - r.left, p.y - r.top);

         ClipCursor(&r);

         S32 centerX = (r.right + r.left) >> 1;
         S32 centerY = (r.bottom + r.top) >> 1;

         if(!windowNotActive)
            SetCursorPos(centerX, centerY);
      }
      //else
      else if(bRestoreCursor) /// TGE_Mouse
      {
         //if(!windowNotActive && false)
            SetCursorPos(lastCursorPos.x + r.left, lastCursorPos.y + r.top);
      }
   }
   else
      ShowCursor(true);
}

//--------------------------------------
static bool sgTaskbarHidden = false;
static HWND sgTaskbar = NULL;

static void hideTheTaskbar()
{
//    if ( !sgTaskbarHidden )
//    {
//       sgTaskbar = FindWindow( "Shell_TrayWnd", NULL );
//       if ( sgTaskbar )
//       {
//          APPBARDATA abData;
//          dMemset( &abData, 0, sizeof( abData ) );
//          abData.cbSize = sizeof( abData );
//          abData.hWnd = sgTaskbar;
//          SHAppBarMessage( ABM_REMOVE, &abData );
//          //ShowWindow( sgTaskbar, SW_HIDE );
//          sgTaskbarHidden = true;
//       }
//    }
}

static void restoreTheTaskbar()
{
//    if ( sgTaskbarHidden )
//    {
//       APPBARDATA abData;
//       dMemset( &abData, 0, sizeof( abData ) );
//       abData.cbSize = sizeof( abData );
//       abData.hWnd = sgTaskbar;
//       SHAppBarMessage( ABM_ACTIVATE, &abData );
//       //ShowWindow( sgTaskbar, SW_SHOW );
//       sgTaskbarHidden = false;
//    }
}

//--------------------------------------
void Platform::enableKeyboardTranslation(void)
{
#ifdef TGE_RPG
	//取消DirectInput
	//Input::disable();
	if(!GuiControl::smDesignTime)
	{
		Input::deactivate();
	}
#endif

#ifndef  TGE_RPG
#ifdef UNICODE
	//   Con::printf("translating...");        
	ImmAssociateContext( winState.appWindow, winState.imeHandle );
#endif
#endif

}

//--------------------------------------
void Platform::disableKeyboardTranslation(void)
{
#ifndef  TGE_RPG
#ifdef UNICODE
//   Con::printf("not translating...");
   ImmAssociateContext( winState.appWindow, NULL );
#endif
#endif


#ifdef TGE_RPG
	//取消DirectInput
	//static bool sBackupState = true;//Input::smEnableDirectInput;

	//Input::smEnableDirectInput = sBackupState;
	//Input::enable();
	if(!GuiControl::smDesignTime)
	{
		Input::activate();
	}
	//sBackupState = Input::smEnableDirectInput;
#endif
}

//--------------------------------------
void Platform::setWindowLocked(bool locked)
{
   windowLocked = locked;
   setMouseClipping(true/*TGE_Mouse*/);
}

//--------------------------------------
void Platform::minimizeWindow()
{
   ShowWindow(winState.appWindow, SW_MINIMIZE);
   restoreTheTaskbar();
}




//--------------------------------------
static void processKeyMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
   S32 repeatCount = lParam & 0xffff;
   S32 scanCode  = (lParam >> 16) & 0xff;
   S32 nVirtkey  =  dIsStandardVK(wParam) ? TranslateOSKeyCode(wParam) : DIK_to_Key(scanCode);
   S32 keyCode;
   if ( wParam == VK_PROCESSKEY && sgDoubleByteEnabled )
      keyCode = MapVirtualKey( scanCode, 1 );   // This is the REAL virtual key...
   else
      keyCode = wParam;

   bool extended = (lParam >> 24) & 1;
   bool repeat   = (lParam >> 30) & 1;
   bool make     = (message == WM_KEYDOWN || message == WM_SYSKEYDOWN);

   S32 newVirtKey = nVirtkey;
   switch(nVirtkey)
   {
      case KEY_ALT:
         newVirtKey = extended ? KEY_RALT : KEY_LALT;
         break;
      case KEY_CONTROL:
         newVirtKey = extended ? KEY_RCONTROL : KEY_LCONTROL;
         break;
      case KEY_SHIFT:
         newVirtKey = ( scanCode == 54 ) ? KEY_RSHIFT : KEY_LSHIFT;
         break;
      case KEY_RETURN:
         if ( extended )
            newVirtKey = KEY_NUMPADENTER;
         break;
   }

   S32 modKey = modifierKeys;

   if(make)
   {
      switch (newVirtKey)
      {
         case KEY_LSHIFT:     modifierKeys |= SI_LSHIFT; modKey = 0; break;
         case KEY_RSHIFT:     modifierKeys |= SI_RSHIFT; modKey = 0; break;
         case KEY_LCONTROL:   modifierKeys |= SI_LCTRL; modKey = 0; break;
         case KEY_RCONTROL:   modifierKeys |= SI_RCTRL; modKey = 0; break;
         case KEY_LALT:       modifierKeys |= SI_LALT; modKey = 0; break;
         case KEY_RALT:       modifierKeys |= SI_RALT; modKey = 0; break;
      }

      if(nVirtkey == KEY_CAPSLOCK)
      {
         capsLockDown = !capsLockDown;
         if(capsLockDown)
            keyboardState[keyCode] |= 0x01;
         else
            keyboardState[keyCode] &= 0xFE;
      }

      keyboardState[keyCode] |= 0x80;
   }
   else
   {
      switch (newVirtKey)
      {
         case KEY_LSHIFT:     modifierKeys &= ~SI_LSHIFT; modKey = 0; break;
         case KEY_RSHIFT:     modifierKeys &= ~SI_RSHIFT; modKey = 0; break;
         case KEY_LCONTROL:   modifierKeys &= ~SI_LCTRL; modKey = 0; break;
         case KEY_RCONTROL:   modifierKeys &= ~SI_RCTRL; modKey = 0; break;
         case KEY_LALT:       modifierKeys &= ~SI_LALT; modKey = 0; break;
         case KEY_RALT:       modifierKeys &= ~SI_RALT; modKey = 0; break;
      }

      keyboardState[keyCode] &= 0x7f;
   }

   U16  ascii[3];
   WORD asciiCode = 0;
   dMemset( &ascii, 0, sizeof( ascii ) );

   S32 res = ToUnicode( keyCode, scanCode, keyboardState, ascii, 3, 0 );
   if (res == 2)
   {
      asciiCode = ascii[1];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -