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

📄 mlsscript.cpp

📁 [游戏开发参考书-用DirectX编写RPG游戏]这是一个系列的丛书如果你都看并且懂的话你就可以你工作啦!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**************************************************
WinMain.cpp
Chapter 14 Mad Lib Script Editor

Programming Role-Playing Games with DirectX
by Jim Adams (01 Jan 2002)
**************************************************/

#include <windows.h>
#include <stdio.h>
#include "MLSScript.h"

cMLSScript::cMLSScript()
{
  m_NumScriptActions = 0;
  m_NumActions       = 0;
  m_ActionParent     = NULL;
}

cMLSScript::~cMLSScript()
{
  FreeScript();
  FreeActions();
}

BOOL cMLSScript::LoadScript(char *Filename)
{
  FILE *fp;
  sAction *ActionPtr;
  sEntry  *EntryPtr;
  long i, j, Size;

  // Free a prior script
  FreeScript();

  // Open the script file
  if((fp=fopen(Filename, "wb"))==NULL)
    return FALSE;

  // Get the number of actions in script
  fread(&m_NumScriptActions, 1, sizeof(long), fp);

  // Return an error if no scripts
  if(!m_NumScriptActions) {
    fclose(fp);
    return FALSE;
  }

  // Read script actions
  ActionPtr = &m_ScriptActionParent;
  for(i=0;i<m_NumScriptActions;i++) {
    // Create a new action structure to use
    ActionPtr->Next = new sAction();
    ActionPtr = ActionPtr->Next;

    // Get action ID
    fread(&ActionPtr->ID, 1, sizeof(long), fp);

    // Get the number of entries
    fread(&ActionPtr->NumEntries, 1, sizeof(long), fp);

    // Get entries (if any)
    if(ActionPtr->NumEntries) {
      ActionPtr->Entries = new sEntry[ActionPtr->NumEntries];

      for(j=0;j<ActionPtr->NumEntries;j++) {
        EntryPtr = &ActionPtr->Entries[i];

        // TEXT
        if(EntryPtr->Type == _TEXT) {
          // Get point to TEXT entry
          sTEXTEntry *Ptr = (sTEXTEntry*)EntryPtr->Data;

          // Get max. length of string and allocate buffer
          fread(&Ptr->Length, 1, sizeof(long), fp);
          Ptr->Text = new char[Ptr->Length];
          ZeroMemory(Ptr->Text, Ptr->Length);

          // Get size of the text string
          fread(&Size, 1, sizeof(long), fp);

          // Get text (if any)
          if(Size) 
            fread(Ptr->Text, 1, Size, fp);
        } else

        // BOOL
        if(EntryPtr->Type == _BOOL) {
          // Get point to BOOL entry
          sBOOLEntry *Ptr = (sBOOLEntry*)EntryPtr->Data;

          // Get BOOL value
          fread(&Ptr->Value, 1, sizeof(BOOL), fp);
        } else

        // INT
        if(EntryPtr->Type == _INT) {
          // Get point to INT entry
          sINTEntry *Ptr = (sINTEntry*)EntryPtr->Data;

          // Get value
          fread(&Ptr->Value, 1, sizeof(long), fp);
        } else

        // FLOAT
        if(EntryPtr->Type == _FLOAT) {
          // Get point to FLOAT entry
          sFLOATEntry *Ptr = (sFLOATEntry*)EntryPtr->Data;

          // Get value
          fread(&Ptr->Value, 1, sizeof(float), fp);
        } else

        // CHOICE
        if(EntryPtr->Type == _CHOICE) {
          // Get point to CHOICE entry
          sCHOICEEntry *Ptr = (sCHOICEEntry*)EntryPtr->Data;

          // Get value
          fread(&Ptr->Selection, 1, sizeof(float), fp);
        }
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return TRUE;
}

BOOL cMLSScript::SaveScript(char *Filename)
{
  FILE *fp;
  sAction *ActionPtr;
  sEntry  *EntryPtr;
  long i;

  // Make sure there's some script actions to write
  if(!m_NumScriptActions)
    return FALSE;

  // Create the new file
  if((fp=fopen(Filename, "wb"))==NULL)
    return FALSE;

  // Output number of actions in script
  fwrite(&m_NumScriptActions, 1, sizeof(long), fp);

  // Output script actions
  ActionPtr = m_ScriptActionParent.Next;
  while(ActionPtr != NULL) {

    // Output action ID
    fwrite(&ActionPtr->ID, 1, sizeof(long), fp);

    // Output the number of entries
    fwrite(&ActionPtr->NumEntries, 1, sizeof(long), fp);

    // Output entries (if any)
    if(ActionPtr->NumEntries) {
      for(i=0;i<ActionPtr->NumEntries;i++) {
        EntryPtr = &ActionPtr->Entries[i];

        // TEXT
        if(EntryPtr->Type == _TEXT) {
          // Get pointer to TEXT entry
          sTEXTEntry *Ptr = (sTEXTEntry*)EntryPtr->Data;

          // Output max. length of string
          fwrite(&Ptr->Length, 1, sizeof(long), fp);

          // Output size of the text string
          fwrite((void*)(strlen(Ptr->Text)+1), 1, sizeof(long), fp);

          // Output text (if any)
          if(strlen(Ptr->Text))
            fwrite(Ptr->Text, 1, strlen(Ptr->Text)+1, fp);
        } else

        // BOOL
        if(EntryPtr->Type == _BOOL) {
          // Get pointer to BOOL entry
          sBOOLEntry *Ptr = (sBOOLEntry*)EntryPtr->Data;

          // Output BOOL value
          fwrite(&Ptr->Value, 1, sizeof(BOOL), fp);
        } else

        // INT
        if(EntryPtr->Type == _INT) {
          // Get pointer to INT entry
          sINTEntry *Ptr = (sINTEntry*)EntryPtr->Data;

          // Output value
          fwrite(&Ptr->Value, 1, sizeof(long), fp);
        } else

        // FLOAT
        if(EntryPtr->Type == _FLOAT) {
          // Get pointer to FLOAT entry
          sFLOATEntry *Ptr = (sFLOATEntry*)EntryPtr->Data;

          // Output value
          fwrite(&Ptr->Value, 1, sizeof(float), fp);
        } else

        // CHOICE
        if(EntryPtr->Type == _CHOICE) {
          // Get pointer to CHOICE entry
          sCHOICEEntry *Ptr = (sCHOICEEntry*)EntryPtr->Data;

          // Output value
          fwrite(&Ptr->Selection, 1, sizeof(float), fp);
        }
      }
    }

    // Go to next action
    ActionPtr = ActionPtr->Next;
  }

  // Close file
  fclose(fp);

  // Return success
  return TRUE;
}

BOOL cMLSScript::FreeScript()
{
  delete m_ScriptActionParent.Next;
  m_ScriptActionParent.Next = NULL;
  m_NumScriptActions        = 0;
  
  return TRUE;
}

BOOL cMLSScript::AddScriptAction(sAction *Action)
{
  Action->Next = m_ScriptActionParent.Next;
  m_ScriptActionParent.Next = Action;
  return TRUE;
}

sAction *cMLSScript::GetScriptActionParent()
{
  return &m_ScriptActionParent;
}

BOOL cMLSScript::LoadActions(char *Filename)
{
  FILE *fp;
  char Text[2048];
  sAction *Action;
  long i, j;
  
  // Free previous action structures
  FreeActions();

  // Open the action file
  if((fp=fopen(Filename, "rb"))==NULL)
    return FALSE;

  // Keep looping until end of file found
  while(1) {
    // Get next quoted action
    if(GetNextQuotedLine(Text, fp, 2048) == FALSE)
      break;

    // Quit if no action text
    if(!Text[0])
      break;

    // Allocate an action structure and link it in
    Action         = new sAction();
    Action->Next   = m_ActionParent;
    m_ActionParent = Action;

    // Copy action text
    strcpy(Action->Text, Text);

    // Store action ID
    Action->ID = m_NumActions;

    // Increate the number of actions loaded
    m_NumActions++;

    // Count the number of entries in the action
    for(i=0;i<(long)strlen(Text);i++) {
      if(Text[i] == '~')
        Action->NumEntries++;
    }

    // Allocate and read in entries (if any)
    if(Action->NumEntries) {
      Action->Entries = new sEntry[Action->NumEntries]();
      for(i=0;i<Action->NumEntries;i++) {

        // Get type of Entry entry
        GetNextWord(Text, fp, 2048);
        
        // TEXT type, get max length
        if(!stricmp(Text, "TEXT")) {
          // Set to text type and allocate TEXT entry
          Action->Entries[i].Type = _TEXT;
          Action->Entries[i].Data = (void*)new sTEXTEntry();
          sTEXTEntry *Ptr = (sTEXTEntry*)Action->Entries[i].Data;

          // Get max length of text
          GetNextWord(Text, fp, 2048);
          Ptr->Length = atol(Text);

          // Create text buffer and clear it out
          Ptr->Text = new char[Ptr->Length];
          ZeroMemory(Ptr->Text, Ptr->Length);
        } else

        // INT type, get min and max values
        if(!stricmp(Text, "INT")) {
          // Set to INT type and allocate INT entry
          Action->Entries[i].Type = _INT;
          Action->Entries[i].Data = (void*)new sINTEntry();

⌨️ 快捷键说明

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