将irrlicht游戏封装到DLL中

来源:互联网 发布:ios7cydia网络错误 编辑:程序博客网 时间:2024/05/16 00:27
1:新建一个dll项目,将游戏代码添加进cpp中,从def处导出主方法,如:
; GameDLL.def : 声明 DLL 的模块参数。
LIBRARY "GameDLL"
EXPORTS
; 此处可以是显式导出
go @1

2:打开主项目程序,在程序中引入lib和dll
#pragma comment(lib,"GameDLL.lib")
_declspec(dllimport) void go();
然后直接运行go即可。

3:游戏代码(简单的RGB人物行走)
// GameDLL.cpp : 定义 DLL 的初始化例程。
//
#include "stdafx.h"
#include "GameDLL.h"
#include <stdlib.h>
#include <irrlicht.h>
#include<iostream>
#include <math.h>
using namespace std;

#ifdef _IRR_WINDOWS_
#pragma comment(lib,"Irrlicht.lib")
#endif
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
//TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
// 则从此 DLL 导出的任何调入
// MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
// 该函数的最前面。
//
// 例如:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // 此处为普通函数体
// }
//
// 此宏先于任何 MFC 调用
// 出现在每个函数中十分重要。这意味着
// 它必须作为函数中的第一个语句
// 出现,甚至先于所有对象变量声明,
// 这是因为它们的构造函数可能生成 MFC
// DLL 调用。
//
// 有关其他详细信息,
// 请参阅 MFC 技术说明 33 和 58。
//
// CGameDLLApp

//********变量************************************************************
scene::IAnimatedMeshSceneNode* RoleNode; //人物模型节点
scene::ICameraSceneNode* camera; //摄像机节点
int IsRun = 0; //人物是否跑动
double speed = 3; //定义人物移动速度
double Rotation = 0.6; //定义人物旋转固定速度
vector3df RoleRotation; //人物当前角度
vector3df RolePosition; //人物位置
double rotationY;
vector3df CamRotation; //摄像机角度
vector3df CamPosition; //摄像机位置
bool IsUp = true; //判断按键是否抬起
//************************************************************************

BEGIN_MESSAGE_MAP(CGameDLLApp, CWinApp)
END_MESSAGE_MAP()

// CGameDLLApp 构造
CGameDLLApp::CGameDLLApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}

// 唯一的一个 CGameDLLApp 对象
CGameDLLApp theApp;

// CGameDLLApp 初始化
BOOL CGameDLLApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
void run();
void stand();
enum
{
// I use this ISceneNode ID to indicate a scene node that is
// not pickable by getSceneNodeAndCollisionPointFromRay()
ID_IsNotPickable = 0,
// I use this flag in ISceneNode IDs to indicate that the
// scene node can be picked by ray selection.
IDFlag_IsPickable = 1 << 0,
// I use this flag in ISceneNode IDs to indicate that the
// scene node can be highlighted. In this example, the
// homonids can be highlighted, but the level mesh can't.
IDFlag_IsHighlightable = 1 << 1
};

class MyEventReceiver:public IEventReceiver {
public:
virtual bool OnEvent(const SEvent & event) {
if(event.EventType == EET_KEY_INPUT_EVENT) {
RolePosition = RoleNode->getPosition();
RoleRotation = RoleNode->getRotation();
CamPosition = camera->getPosition();
CamRotation = camera->getRotation();
CamRotation = camera->getRotation();
if(event.KeyInput.PressedDown == true) {
if(event.KeyInput.Key==KEY_KEY_A) {
if(!IsUp)
{
RolePosition.X += -speed*cos(rotationY);
RolePosition.Z += -speed*sin(rotationY);
}
rotationY += 0.008;
}
if(event.KeyInput.Key==KEY_KEY_D) {
if(!IsUp)
{
RolePosition.X += -speed*cos(rotationY);
RolePosition.Z += -speed*sin(rotationY);
}
rotationY -= 0.008;
}
if(event.KeyInput.Key==KEY_KEY_W) {
IsUp = false;
RolePosition.X += -speed*cos(rotationY);
RolePosition.Z += -speed*sin(rotationY);
run();
}
if(event.KeyInput.Key==KEY_KEY_S) {
IsUp = false;
RolePosition.X -= -speed*cos(rotationY);
RolePosition.Z -= -speed*sin(rotationY);
run();
}
}//if(pressed_down==true)
else if(event.KeyInput.PressedDown == false) { //pressed down false
if(event.KeyInput.Key==KEY_KEY_W) {
IsUp = true;
stand();
}
if(event.KeyInput.Key==KEY_KEY_S){
IsUp = true;
stand();
}
if(event.KeyInput.Key==KEY_KEY_A) {
stand();
}
if(event.KeyInput.Key==KEY_KEY_D) {
stand();
}
//get the ball!!!
}//else if(event.KeyInput.PressedDown == false) {
camera->setTarget(core::vector3df(RolePosition.X,RolePosition.Y,RolePosition.Z));
camera->setPosition(core::vector3df(RolePosition.X+300*cos(rotationY),200,RolePosition.Z+300*sin(rotationY)));
CamRotation = camera->getRotation();
RoleRotation.Y = CamRotation.Y+180;
RoleNode->setPosition(RolePosition);
RoleNode->setRotation(RoleRotation);
}//if(event.EventType == EET_KEY_INPUT_EVENT) {
return false;
}
}my_event_receiver;

void run()
{
if(IsRun != 1)
{
IsRun = 1;
RoleNode->setFrameLoop(200,500);
RoleNode->setAnimationSpeed(100);
RoleNode->setLoopMode(true);
}
}
void stand()
{
if(IsRun != 2)
{
IsRun = 2;
RoleNode->setFrameLoop(1,1);
RoleNode->setAnimationSpeed(100);
RoleNode->setLoopMode(false);
}
}

void go()
{
MyEventReceiver recv;
video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
IrrlichtDevice* device = createDevice(driverType,dimension2d<u32>(800,600),16,
false,false,false,0);
device->setEventReceiver(&my_event_receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
device->getFileSystem()->addZipFileArchive("../MAP/123.zip");
scene::IAnimatedMesh* mesh = smgr->getMesh("123.obj");
scene::IMeshSceneNode* node = 0;
if(mesh)
node = smgr->addOctreeSceneNode(mesh->getMesh(0),0,IDFlag_IsPickable);
if(node)
node->setMaterialFlag(video::EMF_LIGHTING,false);
scene::ITriangleSelector* selector = 0;
if(node)
{
node->setPosition(core::vector3df(0,0,0));
selector = smgr->createOctreeTriangleSelector(
node->getMesh(),node,128);
node->setTriangleSelector(selector);
}
device->getCursorControl()->setVisible(false);
RoleNode = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../MAP/ren.md2"));
if(RoleNode)
{
RoleNode->setMaterialFlag(video::EMF_LIGHTING,false);
RoleNode->setRotation(core::vector3df(0,0,0));
RoleNode->setMaterialTexture(0,driver->getTexture("../MAP/casual01_m_35.jpg"));
RoleNode->setPosition(core::vector3df(0,10,0));
}
camera = smgr->addCameraSceneNode();
camera->bindTargetAndRotation(true);
camera->setPosition(core::vector3df(0,200,300));
camera->setTarget(core::vector3df(0,0,0));
//******创建地形碰撞*******************************
const core::aabbox3d<f32>& box = RoleNode->getBoundingBox();
core::vector3df radius = box.MaxEdge - box.getCenter();
if(selector)
{
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector,RoleNode,radius,
core::vector3df(0,-10.0f,0),core::vector3df(0,0,0));
selector->drop();
// RoleNode->addAnimator(anim);
anim->drop();
}
//*************************************************
if(device->isWindowActive())
int lastFPS = -1;


RoleRotation = RoleNode->getRotation();
rotationY = RoleRotation.Y;
while (device->run())
{
if(device->isWindowActive())
{
driver->beginScene(true,true,video::SColor(0,200,200,200));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
}
}
device->drop();
}
原创粉丝点击