24.04.2014, 08:50
(
Последний раз редактировалось iFarbod; 26.04.2014 в 11:25.
Причина: Adding final project files
)
Hello all.
IntroTo coding heavy projects (i mean samp servers) you have to write your own functions or use includes or plugins.
for that people want to wriite their own functions: some of functions can't be write in PAWN. Need be native function.
This is a tut (my first tut) for creating simple function with plugin using Visual Studio 2012.
Anywhere of the whole tut, if any bad words you see, tell me via PM. also Sorry for the bad english.
Quote:
VS 2012 = Visual Studio 2012 |
Let's start it!
1. You need have a C++ compiler. I use VS 2012* in this tutorial.
2. Open up the VS 2012.
3. Click on the New Project button/link. (on the File > new > project ...) or CTRL+SHIFT+N.
You must see this : (if you installed the VS 2012 correctly)
http://i.gyazo.com/7ff496f075da74b13808105dc63dd184.png
4. Click on the Visual c++.
5. Select 'Win32 Console App'.
http://i.gyazo.com/3c3bb31293cfd7eccebe03b96f60a939.png
5. go to the Name section and type a suitable name.
http://i.gyazo.com/6d40a091aa7414f8ed8c4542fe8acb8d.png
I leave it the ConsoleApplication5. Tap 'OK'.
You see this :
http://i.gyazo.com/d7683f6d9472c3c1ac55cd49cd486aba.png
6. Click 'next >'.
You see Application Settings.
Set the options like me. (in the below image
i.gyazo.com/e04f73fd5f2129f9662a9d0e44542bd0.png
You are done. Click 'finish'.
Wait project loads.
Before start development. You need the SA:MP Plugin SDK, download from here :
Download
Go to the project location selected in New pROJECT. In my case :
Код:
C:\Users\i Farbod\Documents\Visual Studio 2012\Projects\ConsoleApplication5
http://i.gyazo.com/b2da9267ae41aacca3ff9914b816dc48.png
7. Go to you Solution Explorer.
i.gyazo.com/ecddbf8f6e36024dc9145591742b9e00.png
8. Right click on the project name. in my case ConsoleApplication5.
9. Add > New Filter. Name it SDK.
i.gyazo.com/af1a1a2204193048126b62d54cd65d4b.png
9. Go on SDK. Right Click > 'Add' > 'Existing Item ...'.
You see this :
i.gyazo.com/79412fcee76846d48cfc0f7326bd0110.png
Open 'SDK' then : ADD amxplugin.cpp.
Repeat this and ADD plugincommon.h.
OK, That all. you get started. let's DEV!
Development
10. Right click on Source Files. then 'ADD', new item.
i.gyazo.com/956a8829da7d727991534c8beb6c1219.png
Add a New Module Definition File (.def). we need this for build our plugin.
Name it. I leave it 'Source.def'. Click aDD.
A new tab open with this .def file.
put this code in it :
Код:
LIBRARY "ConsoleApplication5" EXPORTS Supports Load Unload AmxLoad AmxUnload
i.gyazo.com/390533cbd5dd77992d20fd3c02f47217.png
We need to create a cpp file holds the natives and data of the Plugin.
11. Right click again on Source files. ADD NEW ITEM. .cpp file and name it. i leave it 'Source.cpp'.
12. Add it. then go :
SLN (Solution Explorer) > ConsoleApplication5 > [Right Click] > Properties.
Go Linker > input .
http://i.gyazo.com/6b42164f0b0e3671381b6d6c1e4bc87e.png
Locate your Module Definition File like me.
Click ok.
13. Go to the Source.cpp. Paste this into this :
Код:
#include "..\SDK\amx\amx.h" #include "..\SDK\plugincommon.h" typedef void (*logprintf_t)(char* format, ...); logprintf_t logprintf; extern void *pAMXFunctions; cell AMX_NATIVE_CALL OurNative(AMX* amx, cell* params) { logprintf("TesT!!"); return 1; } PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() { return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES; } PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData) { pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS]; logprintf = (logprintf_t) ppData[PLUGIN_DATA_LOGPRINTF]; logprintf("// Our Plugin loaded. Whoa!"); return true; } PLUGIN_EXPORT void PLUGIN_CALL Unload() { logprintf("$$$ plugin UnLoAdEd!!! $$$"); } AMX_NATIVE_INFO PluginNatives[] = { {"OurNative", OurNative}, {0, 0} }; PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx ) { return amx_Register(amx, PluginNatives, -1); } PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx ) { return AMX_ERR_NONE; }
We WAnt have two natives. SumOf(x, y); and DivideOf(x, y);
First is for adding param 1 to param 2. (Sum)
Second for divide.
Go to here :
Код:
cell AMX_NATIVE_CALL OurNative(AMX* amx, cell* params) { logprintf("TesT!!"); return 1; }
Код:
cell AMX_NATIVE_CALL SumOf(AMX* amx, cell* params) { return params[1] + params[2]; } cell AMX_NATIVE_CALL DivideOf(AMX* amx, cell* params) { return params[1] / params[2]; }
Код:
AMX_NATIVE_INFO PluginNatives[] = { {"OurNative", OurNative}, {0, 0} };
Код:
AMX_NATIVE_INFO PluginNatives[] = { {"SumOf", SumOf}, {"DivideOf", DivideOf}, {0, 0} };
Go to :
C:\Users\i Farbod\Documents\Visual Studio 2012\Projects\ConsoleApplication5\Debug
Grab the DLL. Copy it to the 'plugins' in your server.
14. Go to server.cfg and add this :
Код:
plugins ConsoleApplication5
You Should see this :
Код:
// Our Plugin loaded. Whoa!
16. Our plugin loaded. Let's go start using natives.
Using Natives
We want to create a Include for our plugin.
17. Go to the /pawno/include. new .inc file.
18. Rename it to whatever. 'CA5' in my case.
http://i.gyazo.com/4527a9684f56fc655f98c4e42f45756c.png
19. Open it. paste the code init:
Код:
native SumOf(x, y); native DivideOf(x, y);
Paste this in a script (GM).
Код:
#include <a_samp> #include <CA5> main() { printf("Sum of 1 and 2 : %d", SumOf(1, 2)); printf("Divide of 64 and 2 : %d", DivideOf(64, 2)); }
You Should see this :
i.gyazo.com/8fee788e5507cd18f394698f000d72c4.png
Код:
[01:00:20] Sum of 1 and 2 : 3 [01:00:20] Divide of 64 and 2 : 32
You build more powerful plugins. not like this, this was easy!
like SSCANF, MySQL for SA:MP, Streamer , and more.
Conclusion
Thanks for read this.
You can find another useful resources in C/C++ dev sites.
This tutorial is good for learning to create plugins.
Thanks!!
DoWnLoAd
Project, Plugin DLL, Include :
SolidFiles
MediaFire
DropBox