CryEngine当库调用 之准备

版权声明:未经同意,不能用于商业用途,版权归本博主所有 https://blog.csdn.net/qq_16123279/article/details/82016548

闲话

本人一直从事Windows平台上的图形应用开发,从用opengl到OSG零零散散已经大概有3年了,OSG也是从看书到看源码
日子就这样平平淡淡的过着。大概是今年4月份左右群里闲聊知道CryEngine开源的消息,因为高中时候玩过《孤岛危机》
系列,被其渲染效果深深震撼过,于是决定搞搞它。

CryEngine是游戏开发引擎,而我只是个图形应用开发的程序员,所以游戏引擎对我有价值的就是源码了。想要调用别的库必须具备以下几个条件:


1.引入.h
2.引入.lib
3.运行肯定不能少dll了
3.处理好预定义宏
4.最重要的当然还是要有自己编译好的dll跟Lib啦
(关于CryEngine的编译会在以后教程做说明,可以先自己弄弄,爱折腾才会实现愿望)


在阅读大量源码后,终于一个初级调用的Demo实现了,下面就一起来看看吧,有图有真相。(本博客会一直写直到大家都能完成图所示)

运行截图

这里写图片描述

Main函数代码截图

这里写图片描述

git项目地址

https://github.com/clojur/CEVDemo

1.配置VS项目

VS项目需要准备以下内容:
项目需要的CryEngine头文件
要调用CE的功能,肯定要包含CE的头,但是CE头那么多如何知道自己要包含哪些头呢?答案就是去看源码,看源码肯定是要先看启动部分的源码的,就是WindowsLauncher和SandBox了,初次建议看WindowsLauncher吧。代码很简单如下:

#include "StdAfx.h"
#include "resource.h"

// Insert your headers here
#include <CryCore/Platform/CryWindows.h>
#include <ShellAPI.h> // requires <windows.h>

// We need shell api for Current Root Extraction.
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")

#include <CryCore/Platform/CryLibrary.h>

#include <CrySystem/IConsole.h>
#include <CrySystem/File/ICryPak.h>

#include <CryCore/Platform/platform_impl.inl>
#include <CrySystem/Profilers/FrameProfiler/FrameProfiler_impl.h>
#include <CryString/StringUtils.h>

// Advise notebook graphics drivers to prefer discrete GPU when no explicit application profile exists
extern "C"
{
    // nVidia
    __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    // AMD
    __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    SSystemInitParams startupParams;
    startupParams.sLogFileName = "Game.log";

    // Note: lpCmdLine does not contain the filename.
    string cmdLine = CryStringUtils::ANSIToUTF8(GetCommandLineA());
    cry_strcpy(startupParams.szSystemCmdLine, cmdLine.c_str());

    return CryInitializeEngine(startupParams) ? EXIT_SUCCESS : EXIT_FAILURE;
}

以上代码,最重要头文件和调用接口如下

#include <CryCore/Platform/platform_impl.inl>

CryInitializeEngine(startupParams)

只要CryInitializeEngine调用后整个引擎就启动了(《CE引擎架构概览》里面介绍了CE的整个框架,只要获取到ISystem这个类指针,就能获取整个引擎的所有模块),就像开车一样这个就是车的钥匙孔,startupParams就是车钥匙了。
引擎启动后,车要怎么走就完全掌握在你的手里了。
(如果不知道怎么弄就先复制WindowsLauncher项目的头文件包含。以后出问题了再去修正)

项目需要的CryEngine预定义宏
代码要正确编译缺少不了预定义宏,比如编译后是debug、release还是profile(debug版,只是把C++代码的编译完全优化了),需要编译的代码是Windows平台、Android还是linux等等许多信息都是需要预定义宏来实现的。(如果不知道怎么弄就先复制WindowsLauncher项目的预定义宏。以后出问题了再去修正)

项目需要的CryEngine链接目录和链接名
之前就说过调用CE是不需要lib的,但是CE使用了其他的第三方库,那些第三库如果用到的话有的是需要链接的。所以就暂时参考WindowsLauncher来设置下。

2.测试项目是否配置成功

编译以下代码测试你的VS项目是否配置成功

#include <CryCore/Project/CryModuleDefs.h>
#define eCryModule eCryM_Editor

#include <CryCore/Platform/platform.h>
#include <algorithm>
#include <vector>

#define USE_NEWPOOL
#include <CryMemory/CryMemoryManager.h>

#include    <CryCore/Platform/platform_impl.inl>
#include    <CryCore/Platform/CryLibrary.h>
#include    <CryString/StringUtils.h>

int main()
{
    /*初始化CE系统*/
    SSystemInitParams startupParams;

    startupParams.bEditor = true;
    startupParams.bPreview = false;

    startupParams.hWnd = nullptr;
    startupParams.sLogFileName = "UI.log";
    startupParams.pUserCallback = nullptr;

    const char* cmdLine = GetCommandLineA();
    cry_strcpy(startupParams.szSystemCmdLine, cmdLine);

    startupParams.pCheckFunc = &GameSystemAuthCheckFunction;

    startupParams.bExecuteCommandLine = false;

    if (!CryInitializeEngine(startupParams, true))
        return false;
}

如果有什么问题请留言或者加入QQ群:457378561问我。

未完待续…

猜你喜欢

转载自blog.csdn.net/qq_16123279/article/details/82016548