電腦遊戲製作開發設計論壇 首頁 電腦遊戲製作開發設計論壇
任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入 

Google
場景概念

 
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式初級班:語法及基礎概念
上一篇主題 :: 下一篇主題  
發表人 內容
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2009-1-7, AM 2:08 星期三    文章主題: 場景概念 引言回覆

這只是一個基本的場景管理器
功能沒寫很完整,不過看的懂的還是懂
再依自己需求增加其它功能


宣告一個場景管理器
MyScene 繼承SceneManager

由基底類別Scene產生其它類型的場景
再由場景自行去做繪製的動作

至於其它要新增或移除,再自行修改功能

代碼:

//main.cpp

#include <iostream>
#include "Scene.h"

using namespace std;

//===============================
SceneManager MyScene;

void main()
{

   Scene bg1;
   NPCScene npc1;
   HomeScene home1;

   //==================================
   //建立場景,並加入場景資訊
   //==================================
   strcpy(bg1.name , "場地");
   bg1.ID = 127;

   strcpy(npc1.name , "NPC大叔");
   npc1.ID = 178;
   npc1.old = 41;

   strcpy(home1.name ,"高樓大廈");
   home1.ID = 199;
   home1.size_h = 2000;
   home1.size_w = 100;

   MyScene.AddScene(&bg1);   //加入場景
   MyScene.AddScene(&npc1);  //加入NPC
   MyScene.AddScene(&home1);  //加入房屋
   //==================================

   MyScene.Update(); //更新場景

   cout << "目前繪製的場景有:" << endl;
   MyScene.Draw(); //場景繪製

   //==================================


   system("PAUSE");
   
}


代碼:
//Scene.h

#include <vector>
using namespace std;
//=============================================
class Scene //基底類別(基底場景)
{
public:
   char name[20];
   int ID;

public:
   Scene()   {}

   ~Scene() {}

   virtual void Update(){}

   virtual void Draw()
   {
      cout << "ID:" << ID << "  ";
      cout << "Name:" << name << endl;
   }
};

//=============================================
class NPCScene:public Scene //NPC類別(繼承場景類別)
{
public:
   int old;

public:
   virtual void Draw()
   {
      cout << "ID:" << ID << "  ";
      cout << "Name:" << name << "  ";
      cout << "年齡:" << old << endl;
   }
};

//=============================================
class HomeScene:public Scene //房屋類別(繼承場景類別)
{
public:
   int size_w;
   int size_h;

public:
   virtual void Draw()
   {
      cout << "ID:" << ID << "  ";
      cout << "Name:" << name << "  ";
      cout << "寬度:" << size_w << "  ";
      cout << "高度:" << size_h << endl;
   }
};

//==============================================
//場景管理
class SceneManager
{
public:
  vector<Scene*> scene_list; //記錄多少場景用

public:
   SceneManager()
   {
      Clear();
   }

   void AddScene(Scene *s)
   {
      scene_list.push_back(s);
   }

   void Clear()
   {
      //清除場景資訊
      
      //事實上只會清除場景管理的內容而已
      //不會清除已建立好的物件
      scene_list.clear();
   }


   void Update()
   {
      //更新場景
        Scene *scene;

      for(vector<Scene*>::iterator i=scene_list.begin();i<scene_list.end();i++)
      {
         scene = *i;
         scene->Update();
      }
   }

   void Draw()
   {
      //繪製場景
        Scene *scene;

      for(vector<Scene*>::iterator i=scene_list.begin();i<scene_list.end();i++)
      {
         scene = *i;
         scene->Draw();
      }
   }
};




輸出結果
代碼:

目前繪製的場景有:
ID:127  Name:場地
ID:178  Name:NPC大叔  年齡:41
ID:199  Name:高樓大廈  寬度:100  高度:2000
請按任意鍵繼續 . . .
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2009-1-7, PM 10:59 星期三    文章主題: 引言回覆

稍微強化過後的場景管理
不過要強化的東西還有很多

代碼:

//main.cpp

#include <iostream>
#include "Scene.h"

using namespace std;

//===============================
SceneManager MyScene;

void main()
{
   //==================================
   //建立場景,並加入場景資訊
   //==================================
   MyScene.CreateObj();
   MyScene.CreateScreen();

   //==================================

   MyScene.Update(); //更新場景

   cout << "目前繪製的場景有:" << endl;
   MyScene.Draw();  //場景繪製

   MyScene.Clear(); //清除場景資訊
   MyScene.Release(); //釋放
   //==================================

   system("PAUSE");
}


代碼:

//Scene.h
#ifndef SCENE_H
#define SCENE_H

#include <vector>
#include <iostream>
using namespace std;
//=============================================

enum enumSceneObj{
   BG,
   NPC,
   House
};

//=============================================

//=============================================
class Scene //基底類別(基底場景)
{
public:
   char name[20];
   int ID;

public:
   Scene()   {}
   ~Scene() {}

   virtual void Update(){}
   virtual void Draw()
   {
      cout << "ID:" << ID << "  ";
      cout << "Name:" << name << endl;
   }
};

//=============================================
class NPCScene:public Scene //NPC類別(繼承場景類別)
{
public:
   int old;

public:
   virtual void Draw()
   {
      cout << "ID:" << ID << "  ";
      cout << "Name:" << name << "  ";
      cout << "年齡:" << old << endl;
   }
};

//=============================================
class HouseScene:public Scene //房屋類別(繼承場景類別)
{
public:
   int size_w;
   int size_h;

public:
   virtual void Draw()
   {
      cout << "ID:" << ID << "  ";
      cout << "Name:" << name << "  ";
      cout << "寬度:" << size_w << "  ";
      cout << "高度:" << size_h << endl;
   }
};

//==============================================
//場景管理
class SceneManager
{
public:
  vector<Scene*> scene_list; //記錄多少場景用

  //======================
  //模型物件

 

  //======================
  //場景物件

  //背景
  Scene *bg;
  int bg_num;

  //NPC
  NPCScene *NPC;
  int NPC_num;

  //房屋
  HouseScene *house;
  int house_num;
  //======================

public:
   SceneManager(){Clear();}      //建構子
   ~SceneManager(){};  //解構子

   void CreateObj();        //建立物件
   void CreateScreen();     //建立場景   
   void AddScene(Scene *s); //增加場景
   void Clear();            //清除場景資訊
   void Release();          //釋放
   void Update();           //場景更新
    void Draw();             //繪製場景
};

#endif


代碼:

//Scene.cpp

#include "Scene.h"

void SceneManager::CreateObj()
{
   //建立物件

   //釋放
   Release();
}

void SceneManager::CreateScreen()
{
   //========================================
   //測試
    bg_num = 1;
   NPC_num = 2;
   house_num = 1;

    char bgn[20];
   strcpy(&bgn[0],"背景");
   int bgID = 0;

   char NPCn[2][20] = {"男子","女子"};

   int NPCID[2] = {1,2};
   int NPCold[2] = {21,20};

   char house_n[20];
   strcpy(&house_n[0],"高樓大廈");
   int houseID = 3;
   int house_w = 100;
   int house_h = 1000;
   //=========================================

    //建立場景   
    bg = new Scene[bg_num];
   NPC = new NPCScene[NPC_num];
   house = new HouseScene[house_num];

   enumSceneObj enumObj;

   //背景
   for(int i=0;i<bg_num;i++)
   {
      bg[i].ID = bgID;
      strcpy(bg[i].name,bgn);

      AddScene(&bg[i]);
   }

   //NPC
   for(int i=0;i<NPC_num;i++)
   {
      strcpy(NPC[i].name,NPCn[i]);
      NPC[i].ID = NPCID[i];
      NPC[i].old = NPCold[i];

      AddScene(&NPC[i]);
   }
   
   //房屋
   for(int i=0;i<house_num;i++)
   {
      strcpy(house[i].name,house_n);
      house[i].ID = houseID;
      house[i].size_w = house_w;
      house[i].size_h = house_h;

      AddScene(&house[i]);
   }
}

void SceneManager::AddScene(Scene *s)
{
   //增加場景
   scene_list.push_back(s);
}

void SceneManager:: Clear()
{
   //清除場景資訊
   
   //事實上只會清除場景管理的內容而已
   //不會清除已建立好的物件
   scene_list.clear();

   bg_num = 0;
    NPC_num = 0;
   house_num = 0;
}

void SceneManager:: Release()
{
   //釋放
   delete[] bg;
   delete[] NPC;
   delete[] house;
}

void SceneManager:: Update()
{
   //更新場景
    Scene *scene;

   for(vector<Scene*>::iterator i=scene_list.begin();i<scene_list.end();i++)
   {
      scene = *i;
      scene->Update();
   }
}

void SceneManager:: Draw()
{
   //繪製場景
    Scene *scene;

   for(vector<Scene*>::iterator i=scene_list.begin();i<scene_list.end();i++)
   {
      scene = *i;
      scene->Draw();
   }
}


輸出結果
代碼:

目前繪製的場景有:
ID:0  Name:背景
ID:1  Name:男子  年齡:21
ID:2  Name:女子  年齡:20
ID:3  Name:高樓大廈  寬度:100  高度:1000
請按任意鍵繼續 . . .
回頂端
檢視會員個人資料 發送私人訊息
從之前的文章開始顯示:   
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式初級班:語法及基礎概念 所有的時間均為 台灣時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
可以 在這個版面附加檔案
可以 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作