Skip to content

Commit

Permalink
3D: Fixes compilation under MQL4
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Oct 29, 2021
1 parent 6e05989 commit d9a0b3a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
6 changes: 5 additions & 1 deletion 3D/Chart3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
#include "Device.h"
#include "Interface.h"

// Resources.
#ifdef __MQL5__
// Resource variables.
#resource "Shaders/chart3d_vs.hlsl" as string Chart3DShaderSourceVS;
#resource "Shaders/chart3d_ps.hlsl" as string Chart3DShaderSourcePS;
#endif

typedef BarOHLC (*Chart3DPriceFetcher)(ENUM_TIMEFRAMES, int);

Expand Down Expand Up @@ -94,7 +96,9 @@ class Chart3D : public Dynamic {
offset.x = offset.y = 0.0f;
offset.z = 25.0f;
initialized = false;
#ifdef __MQL5__
Interface::AddListener(chart3d_interface_listener, &this);
#endif
}

void OnInterfaceEvent(InterfaceEvent& _event) {
Expand Down
6 changes: 5 additions & 1 deletion 3D/Cube.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
#include "Face.h"
#include "Mesh.h"

// Resources.
#ifdef __MQL5__
// Resource variables.
#resource "Shaders/cube_ps.hlsl" as string ShaderCubeSourcePS;
#resource "Shaders/cube_vs.hlsl" as string ShaderCubeSourceVS;
#endif

/**
* Cube mesh.
Expand Down Expand Up @@ -70,11 +72,13 @@ class Cube : public Mesh<T> {
AddFace(f6);
}

#ifdef __MQL5__
/**
* Initializes graphics device-related things.
*/
virtual void Initialize(Device* _device) {
SetShaderVS(_device.VertexShader(ShaderCubeSourceVS, T::Layout));
SetShaderPS(_device.PixelShader(ShaderCubeSourcePS));
}
#endif
};
2 changes: 2 additions & 0 deletions 3D/Devices/MTDX/MTDXShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class MTDXShader : public Shader {
ResetLastError();
}

#ifdef __MQL5__
/**
* Converts vertex layout's item into required DX's color format.
*/
Expand All @@ -139,6 +140,7 @@ class MTDXShader : public Shader {
Alert("Wrong vertex layout!");
return (ENUM_DX_FORMAT)0;
}
#endif

/**
* Sets custom input buffer for shader.
Expand Down
12 changes: 11 additions & 1 deletion 3D/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ struct InterfaceEvent {
} data;
};


#ifdef __MQL5__
/**
* "OnChart" event handler function (MQL5 only).
*
* Invoked when the ChartEvent event occurs.
*/
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) {
datetime _dt;
double _mp;
Expand All @@ -65,6 +70,7 @@ void OnChartEvent(const int id, const long& lparam, const double& dparam, const
Interface::FireEvent(_event);
}
}
#endif

typedef void (*InterfaceListener)(InterfaceEvent&, void*);

Expand All @@ -85,6 +91,7 @@ class Interface
static int mouse_pos_y;
static bool initialized;

#ifdef __MQL5__
static void AddListener(InterfaceListener _listener, void* _target) {
if (!initialized) {
ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true);
Expand Down Expand Up @@ -120,10 +127,13 @@ class Interface
static int GetMouseY() {
return mouse_pos_y;
}
#endif
};

#ifdef __MQL5__
Interface::Installation Interface::installations[];
bool Interface::mouse_was_down = false;
int Interface::mouse_pos_x = 0;
int Interface::mouse_pos_y = 0;
bool Interface::initialized = false;
#endif
8 changes: 8 additions & 0 deletions 3D/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,7 @@ void DXSHRotate(float &out[], int order, const DXMatrix &_matrix, const float &i
return;
}

#ifdef __MQL5__
if ((float)fabs(_matrix.m[2][2]) != 1.0f) {
sinb = (float)sqrt(1.0f - _matrix.m[2][2] * _matrix.m[2][2]);
alpha = (float)atan2(_matrix.m[2][1] / sinb, _matrix.m[2][0] / sinb);
Expand All @@ -3153,6 +3154,13 @@ void DXSHRotate(float &out[], int order, const DXMatrix &_matrix, const float &i
beta = 0.0f;
gamma = 0.0f;
}
#else
alpha = 0.0f;
beta = 0.0f;
gamma = 0.0f;
sinb = 0.0f;
#endif

//---
DXSHRotateZ(temp, order, gamma, in);
rotate_X(temp1, order, 1.0f, temp);
Expand Down
6 changes: 5 additions & 1 deletion tests/CompileTest.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@
* Test compilation of all class files.
*/

// Includes.
// 3D includes (MQL5 only).
#ifdef __MQL5__
#include "../3D/Chart3D.h"
#include "../3D/Cube.h"
#include "../3D/Devices/MTDX/MTDXDevice.h"
#include "../3D/Devices/MTDX/MTDXIndexBuffer.h"
#include "../3D/Devices/MTDX/MTDXShader.h"
#include "../3D/Devices/MTDX/MTDXVertexBuffer.h"
#include "../3D/Frontends/MT5Frontend.h"
#endif

// Includes.
#include "../Account.mqh"
#include "../Action.mqh"
#include "../Array.mqh"
Expand Down

0 comments on commit d9a0b3a

Please sign in to comment.