L
Lonewolff
Guest
GM Version: GM2
Target Platform: Windows
There has always been a bit of mystery surrounding DLL's, extension creation, and even more so - how to inject DirectX code to the renderer (or even replace GM's renderer if you feel that way inclined).
Why would we want to create an extension in the first place? Could be any of the following;
Once I add subsequent sections (pending any interest out there), I'll break up each part into 'spoiler' sections so as not to overwhelm and to give quicker navigation.
We'll start out by setting up the build environment and then having a foundation to work from, progress from there.
To quickly extinguish the 'cross platform entusiast' argument. The fundamentals are the same for all platforms. If you can write platform independent code, you can create external libraries for any operating system.
Granted the subject of DirectX will most certainly platform lock, if you choose to endeavour down the path.
Here goes
Phase 1 - Building the foundations
If you don't have it installed allready, grab a copy of Visual Studio (https://www.visualstudio.com/). For those who do have it installed but are wondering if it is a recent enough version, VS2012 or newer should be fine.
Create a new project of either 'Win32 Console Application' or 'Win32 Project' (the choice doesn't matter) and name it 'dx_injection'.
Click on Application settings and set up your project as follows.
Right click on 'source files' and add a 'C++ File (.cpp)'. Default file name will be 'Source.cpp', change the name if you really want to.
In the code editor add the following source code.
This is the start of our C++ extension. The 'fn_export' declaration just tells the DLL what functions will have access from the outside world (i.e. access from GMS).
Press F5 to build the DLL, making note of the path of the file in the build window. If there was a build failure, you'll need to go back and confirm the project settings are correct (as above).
The function 'test_linkage()' is just a quick test function that we will call from GMS to make sure that everything is interfacing correctly. We will soon call this from an object to make sure that we are actually communicating with the DLL.
Now fire up GMS and create a new GML based project.
Start out by creating a new extension and call it 'dx_injection'
Then add the dll we previously built into the extension.
Lastly add the function names as below.
I have chosen to prefix any functions with 'dx_' to save any possible naming clashes inside of GMS.
Now we can create a normal object (let's say 'obj_tutorial') and add it to the room.
Add a Draw_GUI event and insert the following code.
You'll notice that 'dx_test_linkage' will be orange. As far as GMS is concerned this is now a native function. No scripts or other shennanigans required.
Build and run the project.
All going well, you will the number '1' displayed on the screen. If it displays '0' then something was missed along the way. This is GM telling you that it couldn't call the function from the DLL.
Congrat's - you made your first DLL extension.
(That's all for now )
Target Platform: Windows
There has always been a bit of mystery surrounding DLL's, extension creation, and even more so - how to inject DirectX code to the renderer (or even replace GM's renderer if you feel that way inclined).
Why would we want to create an extension in the first place? Could be any of the following;
- Expand upon a particular GMS limitation.
- Get a better understanding on how things work.
- Increase performance in a particular task.
- Add some sort of multi-threading.
- Open up endless possibilities.
Once I add subsequent sections (pending any interest out there), I'll break up each part into 'spoiler' sections so as not to overwhelm and to give quicker navigation.
We'll start out by setting up the build environment and then having a foundation to work from, progress from there.
To quickly extinguish the 'cross platform entusiast' argument. The fundamentals are the same for all platforms. If you can write platform independent code, you can create external libraries for any operating system.
Granted the subject of DirectX will most certainly platform lock, if you choose to endeavour down the path.
Here goes
Phase 1 - Building the foundations
If you don't have it installed allready, grab a copy of Visual Studio (https://www.visualstudio.com/). For those who do have it installed but are wondering if it is a recent enough version, VS2012 or newer should be fine.
Create a new project of either 'Win32 Console Application' or 'Win32 Project' (the choice doesn't matter) and name it 'dx_injection'.
Click on Application settings and set up your project as follows.
Right click on 'source files' and add a 'C++ File (.cpp)'. Default file name will be 'Source.cpp', change the name if you really want to.
In the code editor add the following source code.
Code:
#define fn_export extern "C" __declspec (dllexport)
fn_export double test_linkage()
{
return 1;
}
Press F5 to build the DLL, making note of the path of the file in the build window. If there was a build failure, you'll need to go back and confirm the project settings are correct (as above).
The function 'test_linkage()' is just a quick test function that we will call from GMS to make sure that everything is interfacing correctly. We will soon call this from an object to make sure that we are actually communicating with the DLL.
Now fire up GMS and create a new GML based project.
Start out by creating a new extension and call it 'dx_injection'
Then add the dll we previously built into the extension.
Lastly add the function names as below.
I have chosen to prefix any functions with 'dx_' to save any possible naming clashes inside of GMS.
Now we can create a normal object (let's say 'obj_tutorial') and add it to the room.
Add a Draw_GUI event and insert the following code.
Code:
draw_text(20, 20, dx_test_linkage());
Build and run the project.
All going well, you will the number '1' displayed on the screen. If it displays '0' then something was missed along the way. This is GM telling you that it couldn't call the function from the DLL.
Congrat's - you made your first DLL extension.
(That's all for now )
Last edited by a moderator: