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

📄 ch19p1_advancedpartsys.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
#############################################################################

  Ch19p1_AdvancedPartSys.cpp: a program that demonstrates the fire algorithm,
  without any annoying bells and/or whistles.
  
#############################################################################
*/

// include files ////////////////////////////////////////////////////////////
#define STRICT
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "D3DHelperFuncs.h"
#include "Ch19p1_resource.h"
#include "GroundPlane.h"
#include "Camera.h"
#include "InputManager.h"
#include "Ch19p1_ParticleSystem.h"

//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the 
//       generic functionality needed in all Direct3D samples. CMyD3DApplication 
//       adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
  // Font for drawing text
  CD3DFont* m_pFont;
  CD3DFont* m_pFontSmall;

  // Scene
  CGroundPlane m_Ground;
  CUserControlledCamera m_Camera;

  // Mouse Input
  CInputManager m_InputManager;

  // The Particle System
  CParticleEmitter m_PartSys;

  // dialog box window handle
  HWND m_hWndPropDlg;

protected:
  HRESULT OneTimeSceneInit();
  HRESULT InitDeviceObjects();
  HRESULT RestoreDeviceObjects();
  HRESULT InvalidateDeviceObjects();
  HRESULT DeleteDeviceObjects();
  HRESULT FinalCleanup();
  HRESULT Render();
  HRESULT FrameMove();
  HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
  LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

  void ProcessInput();

public:
  CMyD3DApplication();

  // is the ground shown?
  bool m_bShowGround;
};

CMyD3DApplication* g_pD3DApp = NULL;

CParticleEmitter *g_theSystem = NULL;


void BrowseForColor(HWND hWnd, UINT idR, UINT idG, UINT idB, UINT idA)
{
  static COLORREF custcolors[16];

  CHOOSECOLOR cc;
  memset(&cc, 0, sizeof(cc));
  cc.lStructSize = sizeof(cc);
  cc.hwndOwner = hWnd;
  cc.Flags = CC_RGBINIT | CC_FULLOPEN;
  cc.lpCustColors = custcolors;

  // get current values, convert to 0-255 range
  /*
  D3DXCOLOR color = Stuff4EditsIntoColor(hWnd, idR, idG, idB, idA);
  cc.rgbResult = D3DXColorTo255RGB(color);

  if (ChooseColor(&cc)) {
    color.r = (float)GetRValue(cc.rgbResult) / 255.0f;
    color.g = (float)GetGValue(cc.rgbResult) / 255.0f;
    color.b = (float)GetBValue(cc.rgbResult) / 255.0f;
    StuffColorInto4Edits(color, hWnd, idR, idG, idB, idA);
  }
  */
}

std::string GetEditbox(HWND hWnd, UINT id)
{
  std::string str;
  int len = GetWindowTextLength(GetDlgItem(hWnd, id));
  try {
    char *text = new char[len+1]; // +1 so 0 length files will work
    GetWindowText(GetDlgItem(hWnd, id), text, len+1);
    str = text;
    delete[] text;
  } catch(...) { }
  return(str);
}
bool SaveEditbox(HWND hWnd, UINT id, const char *strFilename)
{
  int len = GetWindowTextLength(GetDlgItem(hWnd, id));
  try {
    char *text = new char[len+1]; // +1 so 0 length files will save
    GetWindowText(GetDlgItem(hWnd, id), text, len+1);
    int handle = open(strFilename, O_RDWR | O_BINARY | O_TRUNC | O_CREAT, S_IWRITE | S_IREAD);
    if (handle == -1) { throw("Cannot open file for output!"); }
    if (write(handle, text, len) != len) { throw("error writing file, probably disk full."); }
    close(handle);
    delete[] text;
  } 
  catch(char *e) { ::MessageBox(hWnd, e, "SaveEditbox function error catcher", MB_ICONSTOP); return(false); }
  catch(...) { return(false); }
  return(true);
}

bool LoadEditbox(HWND hWnd, UINT id, const char *strFilename)
{
  try {
    int handle = open(strFilename, O_RDONLY | O_BINARY);
    if (handle == -1) { throw("Cannot open file for input!"); }

    int len = filelength(handle);
  
    char *text = new char[len+1]; // +1 so 0 length files will save
    memset(text, 0, len+1);
    read(handle, text, len);
    close(handle);

    SetWindowText(GetDlgItem(hWnd, id), text);
    
    delete[] text;
  } 
  catch(char *e) { ::MessageBox(hWnd, e, "LoadEditbox function error catcher", MB_ICONSTOP); return(false); }
  catch(...) { return(false); }
  
  return(true);
}

LRESULT CALLBACK PropDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
  CMyD3DApplication *app = (CMyD3DApplication *)g_pD3DApp;
  static bool bTurnOffUpdates = false;
  switch (uMsg) {
    case WM_INITDIALOG: 
      {
        bTurnOffUpdates = true; // so as we call ourselves we don't change things
        
        SendMessage(GetDlgItem(hWnd, IDC_SHOWGROUND), BM_SETCHECK, app->m_bShowGround, 0);

        bTurnOffUpdates = false; // now allow updates to go to part sys

        return(true); // focus not set
      }
      break;

    case WM_COMMAND:
      {
        if (bTurnOffUpdates) return(false);
        UINT wID = LOWORD(wParam);         // item, control, or accelerator identifier 
        HWND hwndCtl = (HWND) lParam;      // handle of control 
        
        switch(wID) {
          case IDC_SHOWGROUND:
            app->m_bShowGround = app->m_bShowGround ? false : true;
            break;

          case IDC_COMPILE:
            {
              std::string str = GetEditbox(hWnd, IDC_SCRIPT);
              g_theSystem->Compile(str.c_str());
							g_theSystem->Start();
              SetWindowText(GetDlgItem(hWnd, IDC_ERRORS), g_theSystem->m_strLastError.c_str());
            }
            break;

          case IDC_SAVE:
            {
              OPENFILENAME ofn;       // common dialog box structure
              char szFile[260];       // buffer for filename
              
              // Initialize OPENFILENAME
              ZeroMemory(&ofn, sizeof(OPENFILENAME));
              ofn.lStructSize = sizeof(OPENFILENAME);
              ofn.hwndOwner = hWnd;
              strcpy(szFile, "ParticleSystem.txt");
              ofn.lpstrFile = szFile;
              ofn.nMaxFile = sizeof(szFile);
              ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
              ofn.lpstrFileTitle = "Save Particle System Script";
              ofn.nMaxFileTitle = 0;
              ofn.lpstrInitialDir = NULL;
              ofn.Flags = OFN_OVERWRITEPROMPT;

              // Display the Save As dialog box. 

              if (GetSaveFileName(&ofn)==TRUE) {
                if (!SaveEditbox(hWnd, IDC_SCRIPT, ofn.lpstrFile)) {
                  ::MessageBox(hWnd, "Could not save file to disk (disk full?)", "Error Saving File", MB_ICONSTOP);
                } 
                else SetWindowText(hWnd, ofn.lpstrFile);
              }
            }
            break;

          case IDC_LOAD:
            {
              OPENFILENAME ofn;       // common dialog box structure
              char szFile[260];       // buffer for filename
              
              // Initialize OPENFILENAME
              ZeroMemory(&ofn, sizeof(OPENFILENAME));
              ofn.lStructSize = sizeof(OPENFILENAME);
              ofn.hwndOwner = hWnd;
              strcpy(szFile, "ParticleSystem.txt");
              ofn.lpstrFile = szFile;
              ofn.nMaxFile = sizeof(szFile);
              ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
              ofn.lpstrFileTitle = "Save Particle System Properties";
              ofn.nMaxFileTitle = 0;
              ofn.lpstrInitialDir = NULL;
              ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

              // Display the Open dialog box. 

              if (GetOpenFileName(&ofn)==TRUE) {
                if (!LoadEditbox(hWnd, IDC_SCRIPT, ofn.lpstrFile)) {
                  ::MessageBox(hWnd, "Could not load file from disk.", "Error Loading File", MB_ICONSTOP);
                } 
                else {
                  SetWindowText(hWnd, ofn.lpstrFile);
                  SendMessage(hWnd, WM_COMMAND, IDC_COMPILE, 0); // click Compile!
                }
              }
            }
            break;
        } // switch wID
      } // WM_COMMAND block
      break;
  } // switch uMsg
  return(0);
}

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
//       message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
  CMyD3DApplication d3dApp;
  g_pD3DApp = &d3dApp;

  if( FAILED( d3dApp.Create( hInst ) ) )
    return 0;

  return d3dApp.Run();
}

//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
  m_strWindowTitle    = _T("Ch19p1_AdvancedPartSys");
  m_bUseDepthBuffer   = TRUE;

  m_pFont            = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  m_pFontSmall       = new CD3DFont( _T("Arial"),  9, D3DFONT_BOLD );

⌨️ 快捷键说明

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