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

📄 tddrawapp.cpp

📁 一个小游戏程序!! 大家可以来
💻 CPP
字号:
//---------------------------------------------------------------------------
#include "TDDrawApp.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TWinApp::TWinApp(HINSTANCE hInstance, int nCmdShow)
{
  _hWnd = 0;
  _hInst = hInstance;
  _nCmdShow = nCmdShow;
  _bInitHide = false;
  _hAccel = 0;
  _flpNewWndProc = (LRESULT(CALLBACK*)(HWND,UINT,WPARAM,LPARAM))MakeObjectInstance(_fWndProc);
  strcpy(_fCaption, AnsiString(ClassName()).c_str());
}
//---------------------------------------------------------------------------

void __fastcall TWinApp::CreateParams(Controls::TCreateParams &Params)
{
  AnsiString asClassName = ClassName();

  Params.Caption   = _fCaption;
  Params.Style     = WS_OVERLAPPEDWINDOW;
  Params.X         = CW_USEDEFAULT;
  Params.Y         = CW_USEDEFAULT;
  Params.Width     = CW_USEDEFAULT;
  Params.Height    = CW_USEDEFAULT;
  Params.ExStyle   = 0;
  Params.WndParent = 0;
  Params.Param     = 0;
  strcpy(Params.WinClassName, asClassName.c_str());

  Params.WindowClass.lpszClassName = Params.WinClassName;
  Params.WindowClass.lpfnWndProc   = _flpOldWndProc;
  Params.WindowClass.style         = CS_VREDRAW | CS_HREDRAW;
  Params.WindowClass.hInstance     = _hInst;
  Params.WindowClass.hIcon         = 0;
  Params.WindowClass.hCursor       = LoadCursor(NULL,IDC_ARROW);
  Params.WindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
  Params.WindowClass.lpszMenuName  = NULL;
  Params.WindowClass.cbClsExtra    = 0;
  Params.WindowClass.cbWndExtra    = 0;
}
//---------------------------------------------------------------------------

void __fastcall TWinApp::InitWindow(void)
{
  Controls::TCreateParams Params;
  memset(&Params, 0, sizeof(Controls::TCreateParams));
  CreateParams(Params);

  //Register the Window Class
  if(::RegisterClass(&Params.WindowClass) == 0)
    throw Exception("RegisterClass Error");

  //Create and show the main window
  _hWnd = ::CreateWindowEx(Params.ExStyle, Params.WinClassName, Params.Caption, Params.Style,
          Params.X, Params.Y, Params.Width, Params.Height, Params.WndParent, NULL, _hInst, Params.Param);
  if(_hWnd == NULL)
    throw Exception("CreateWindowEx Error");

  SetWindowLong(_hWnd, GWL_WNDPROC, (long)_flpNewWndProc);
  if(!_bInitHide)
   {
     ShowWindow(_hWnd, _nCmdShow);
     UpdateWindow(_hWnd);
   }
}
//---------------------------------------------------------------------------

LRESULT CALLBACK TWinApp::_flpOldWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if(msg == WM_DESTROY)
   {
     PostQuitMessage(0);
     return 0;
   }
  return DefWindowProc(hWnd, msg, wParam, lParam);
}
//---------------------------------------------------------------------------

void __fastcall TWinApp::WndProc(Messages::TMessage &Message)
{
  if(Message.Msg ==  WM_DESTROY)
   {
     PostQuitMessage(0);
     Message.Result = 0;
   }
  Message.Result = DefWindowProc(_hWnd, Message.Msg, Message.WParam, Message.LParam);
}
//---------------------------------------------------------------------------

void __fastcall TWinApp::IdleProcess(bool &Done)
{
  //do nothing
}
//---------------------------------------------------------------------------

int __fastcall TWinApp::Run(void)
{
   int RetVal = -1;

   try
    {
      InitWindow();

      MSG msg;
      while(1)
       {
         if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
          {
            if(GetMessage(&msg, NULL, 0, 0) == 0)
             {
               RetVal = (int)msg.wParam;
               break;
             }

            if(TranslateAccelerator(_hWnd, _hAccel, &msg) == 0)
             {
               TranslateMessage(&msg);
               DispatchMessage(&msg);
             }
           }
          else //Idle
           {
             bool bDone = true;
             IdleProcess(bDone);

             if(bDone)
              {
                WaitMessage();
              }
           }
       }
    }
   catch(Exception &e)
    {
      MessageBox(0, e.Message.c_str(), _fCaption, MB_OK|MB_ICONSTOP);
    }

   return RetVal;
}
//===========================================================================

__fastcall TDDrawApp::TDDrawApp(HINSTANCE hInstance, int nCmdShow)
                     :TWinApp(hInstance, nCmdShow)
{
  _Display   = NULL;
  _bActive   = false;

  _ScrWidth  = 640;
  _ScrHeight = 480;
  _ScrBpp    =  16;

  OnDispFrame = NULL;
  OnRestFrame = NULL;
  OnKeyboard  = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TDDrawApp::InitWindow(void)
{
  TWinApp::InitWindow();
  fInitDDraw();
}
//---------------------------------------------------------------------------

void __fastcall TDDrawApp::WndProc(Messages::TMessage &Message)
{
  switch(Message.Msg)
   {
     case WM_SIZE:
          if(Message.WParam==SIZE_MAXHIDE || Message.WParam==SIZE_MINIMIZED)
             _bActive = false;
          else
             _bActive = _Display != NULL;
          //Continue WM_MOVE

     case WM_MOVE:
          if(_Display)
             _Display->UpdateBounds();
          break;

     case WM_SYSCOMMAND:
          switch(Message.WParam)
           {
             case SC_MOVE:
             case SC_SIZE:
             case SC_MAXIMIZE:
             case SC_MONITORPOWER:
                  Message.Result = true;
                  return; //Prevent moving/sizing and power loss in fullscreen mode
           }
          break;

     case WM_KEYDOWN: EvKeyboard(Message.WParam, true ); break;
     case WM_KEYUP  : EvKeyboard(Message.WParam, false); break;
     case WM_DESTROY: fUninitDDraw();                    break;
   }

  TWinApp::WndProc(Message);
}
//---------------------------------------------------------------------------

void __fastcall TDDrawApp::fInitDDraw(void)
{
  if(!_Display)
   {
     _Display = new TDDrawDisplay(hWnd);

     if(FAILED(_Display->CreateFullScreenDisplay(ScreenWidth, ScreenHeight, ScreenBpp)))
      {
        throw Exception("Error: Display->CreateFullScreenDisplay");
      }

     EvInitDDraw();
   }
}
//---------------------------------------------------------------------------

void __fastcall TDDrawApp::fUninitDDraw(void)
{
  if(_Display)
   {
     EvUninitDDraw();

     delete _Display;
     _Display = NULL;
   }
}
//---------------------------------------------------------------------------

void __fastcall TDDrawApp::IdleProcess(bool &Done)
{
  TWinApp::IdleProcess(Done);

  if(_bActive && _Display)
   {
     EvDisplayFrame(); //user's display

     HRESULT hr = _Display->Present(); //flip
     if(FAILED(hr))
      {
        if(hr == DDERR_SURFACELOST)
         {
           if(SUCCEEDED(_Display->DirectDraw->RestoreAllSurfaces()))
             EvRestoreSurfaces();
         }
        else
         {
           throw Exception("DirectDraw error: display frames.");
         }
      }

     Done = false;
   }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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