Microsoft fsx 飞行模拟器 SDK的一些心得

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/a13132163676/article/details/79529514

关于FSX的SDK的一些心得

 

这几日接触了一款Microsoft的一款飞行器模拟工具,研究了一下关于它的SDK,微软官方在安装包中加入了关于FSX中的教程,其中详细介绍了关于微软官方给客户端编写插件留下的接口,其次也有一些微软官方的插件简单的Samples。

我整理了一个sample,现在记录一下内容,以备以后可以作为参考。

此为Microsoft的官方samples,我觉得ThrottleControl 比较简单易懂,该sample是一个修改飞机油门的一个插件。具体实现如下。

 

 

 

 

其次,在编写插件的时候遇到该错误

 

解决方法:

这两个文件与生成的.EXE文件放在同一个文件夹下即可运行。

 

 

关于启动插件设置方法:将FSX包安装到C盘中,然后在如下路径中

 

 

 会有这两个文件一个库文件DLL,一个EXE的配置文件。

EXE.XML信息

只需将<Path>标签的内容更改成生成的EXE插件所在位置即可。

 

初次接触FSX一些个人的小小心得以及给以后的相关工作留下一个参考。 

 

 

详细代码:

 

 

 

//------------------------------------------------------------------------------

//

// simconnect  throttle control sample

//

//  description:

//              pressa to increase the throttle

//              pressz to decrease the throttle

//------------------------------------------------------------------------------

 

#include<windows.h>

#include<tchar.h>

#include<stdio.h>

#include<strsafe.h>

 

#include"SimConnect.h"

 

int     quit = 0;                  //定义一个INT类型变量用来指导用户是否退出模拟器

HANDLE  hsimconnect = NULL;         //最重要的一个变量,建立客户端与模拟器之间的通信

 

staticenumgroup_id{                 //枚举 一个工作组,用来处理事件,目前没有用到

   group_keys,

};

staticenuminput_id {              //定义按钮ID

   input_keys,

};

 

staticenumevent_id {                 //客户端定义的事件

   event_sim_start,                   //指模拟器开始执行

   event_a,                            //按下A时发生的事件

    event_z                             //摁下B时发生的事件

};

 

staticenumdata_define_id {           

   definition_throttle,                //客户端定义的油门事件

};

 

staticenumdata_request_id {

   request_throttle,                   //获取模拟器的油门相应数据

};

 

structstructthrottlecontrol

{

    double throttlepercent;             //油门

};

 

structthrottlecontrol       tc;        

 

voidCALLBACK mydispatchproctc(SIMCONNECT_RECV* pdata, DWORDcbdata, void *pcontext///回调函数   pdata:缓存,客户端和模拟器之间的通信数据/cbdata:数据缓冲区的大小,以字节为单位/pcontext:客户端指定的一个特定指针

{

    HRESULT hr;   //声明一个hr用来接受返回值

   

   switch(pdata->dwID)   //dwID pdata缓存数据中的数据编号

   {

       caseSIMCONNECT_RECV_ID_SIMOBJECT_DATA:   //这个返回类型是飞机本身的一些数据

       {

            SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pobjdata = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pdata;   //返回值

           

            switch(pobjdata->dwRequestID)// dwRequestID 包含着客户端定义的请求ID

            {

                caserequest_throttle:

                {

                    // read and set the initial throttle control value

                    structthrottlecontrol *ps = (structthrottlecontrol*)&pobjdata->dwData;  //读取初始的油门值

 

                    tc.throttlepercent  = ps->throttlepercent;

                   

                    printf("\nrequest_userid received, throttle = %2.1f", ps->throttlepercent);

 

                    // now turn the input events on

                    hr= SimConnect_SetInputGroupState(hsimconnect, input_keys, SIMCONNECT_STATE_ON);   //开始事件

                }

 

                default:

                   break;

            }

            break;

       }

 

       caseSIMCONNECT_RECV_ID_EVENT:

       {

            SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pdata;

 

            switch(evt->uEventID)

            {

 

                caseevent_sim_start:

                    {

                        // send this request to get the user aircraft id

                        hr =SimConnect_RequestDataOnSimObject(hsimconnect, request_throttle, definition_throttle, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_ONCE); //绑定要请求的客户端的事件

                    }

                    break;

                       

                caseevent_a:

                    {

                        // increase thethrottle

                        if (tc.throttlepercent <= 95.0f)

                            tc.throttlepercent  += 5.0f;

 

                        hr =SimConnect_SetDataOnSimObject(hsimconnect, definition_throttle, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(tc), &tc);//更改数据

                    }

                    break;

 

                caseevent_z:

                    {

                        // decrease thethrottle

                        if (tc.throttlepercent >= 5.0f)

                            tc.throttlepercent  -= 5.0f;

 

                        hr =SimConnect_SetDataOnSimObject(hsimconnect, definition_throttle, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(tc), &tc);

                    }

                    break;

 

                default:

                    break;

            }

            break;

       }

 

       caseSIMCONNECT_RECV_ID_QUIT:   //退出模拟器

       {

            quit = 1;

            break;

       }

 

       default:

            printf("\nreceived:%d",pdata->dwID);

            break;

   }

}

 

void testthrottlecontrol()

{

    HRESULT hr;

 

   if (SUCCEEDED(SimConnect_Open(&hsimconnect, "throttle control", NULL, 0, 0, 0)))

   {

       printf("\nconnected to flightsimulator!");  

          

       // set up a data definition for thethrottle control 

        // 将飞行器模拟变量名称添加到客户定义的对象定义中

        //通信连接

        //客户端定义事件

        //飞行器模拟变量名称

        //单位

       hr = SimConnect_AddToDataDefinition(hsimconnect, definition_throttle,

            "general eng throttle lever position:1", "percent");

 

       // request a simulation started event

       hr = SimConnect_SubscribeToSystemEvent(hsimconnect, event_sim_start, "simstart");   //请求模拟启动事件

 

       // create two private key events tocontrol the throttle

        hr= SimConnect_MapClientEventToSimEvent(hsimconnect, event_a); //绑定事件

        hr= SimConnect_MapClientEventToSimEvent(hsimconnect, event_z);

 

       // link the events to some keyboard keys

       hr = SimConnect_MapInputEventToClientEvent(hsimconnect, input_keys, "a", event_a); //将键盘按键与事件绑定

       hr = SimConnect_MapInputEventToClientEvent(hsimconnect, input_keys, "z", event_z);

 

        // ensure the input events are off until the sim is upand running

       hr = SimConnect_SetInputGroupState(hsimconnect, input_keys, SIMCONNECT_STATE_OFF); //关闭事件

 

       // sign up for notifications

       hr = SimConnect_AddClientEventToNotificationGroup(hsimconnect, group_keys, event_a);  //将事件放入一个组中

       hr = SimConnect_AddClientEventToNotificationGroup(hsimconnect, group_keys, event_z);

   

       while( 0 == quit )

       {

            SimConnect_CallDispatch(hsimconnect,mydispatchproctc, NULL);  //调用回调函数

            Sleep(1);  //1毫米一次

       }

 

        hr= SimConnect_Close(hsimconnect);  //关闭连接

   }

}

 

int__cdecl_tmain(intargc, _TCHAR* argv[])

{

   testthrottlecontrol();

 

    return 0;

}

 

展开阅读全文

没有更多推荐了,返回首页