Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Calin Leafshade on Tue 13/07/2010 15:35:03

Title: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 15:35:03
I've been trying to compile the plugin template that CJ provides and i'm not having much luck.

I know very little about C++ and compilers so i might be doing something obviously wrong.

I'm using g++ to compile for windows under minggw (directly from Dev-C++).

Is this ok or does it *have* to be a MS-Visual C++ compiler?

here's my error log for fun


Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c ags_template.cpp -o ags_template.o -I
"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include
/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"  -DBUILDING_DLL=1

In file included from ags_template.cpp:10:
agsplugin.h:38: error: conflicting declaration 'typedef int HWND'
C:/Dev-Cpp/include/windef.h:292: error: 'HWND' has a previous declaration as `typedef struct HWND__*HWND'
agsplugin.h:38: error: declaration of `typedef int HWND'
C:/Dev-Cpp/include/windef.h:292: error: conflicts with previous declaration `typedef struct HWND__*HWND'
agsplugin.h:38: error: declaration of `typedef int HWND'
C:/Dev-Cpp/include/windef.h:292: error: conflicts with previous declaration `typedef struct HWND__*HWND'

ags_template.cpp: In function `void AGS_EngineStartup(IAGSEngine*)':
ags_template.cpp:97: error: invalid conversion from `int (*)(int, int)' to `void*'
ags_template.cpp:97: error:   initializing argument 2 of `virtual void IAGSEngine::RegisterScriptFunction(const char*, void*)'

ags_template.cpp:98: error: invalid conversion from `int (*)(int, int)' to `void*'
ags_template.cpp:98: error:   initializing argument 2 of `virtual void IAGSEngine::RegisterScriptFunction(const char*, void*)'

make.exe: *** [ags_template.o] Error 1

Execution terminated
Title: Re: Compiling a plugin
Post by: Wyz on Tue 13/07/2010 15:42:32
in agsplugin.h

replace

#ifndev _WINDOWS_
typedef int HWND;
#endif

with

#if !(defined(_WINDOWS_) || defined(_WIN32))
typedef int HWND;
#endif


and you need to type the second parameter in RegisterScriptFunction to void *
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 15:52:03
Quote from: Wyz on Tue 13/07/2010 15:42:32
and you need to type the second parameter in RegisterScriptFunction to void *

where exactly? like this?


  engine->RegisterScriptFunction ("AddNumbers", void *AddNumbers);
Title: Re: Compiling a plugin
Post by: Wyz on Tue 13/07/2010 15:53:05
like this:


  engine->RegisterScriptFunction ("AddNumbers", (void *) AddNumbers);
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 15:54:18
ahh I see.. sort of
Title: Re: Compiling a plugin
Post by: Monsieur OUXX on Tue 13/07/2010 16:03:38
Quote from: Calin Leafshade on Tue 13/07/2010 15:35:03
I'm using g++ to compile for windows under minggw (directly from Dev-C++).
Is this ok or does it *have* to be a MS-Visual C++ compiler?

To answer your question :

You might litterally not NEED to have Visual C++.

However, as soon as a project has system-related dependancies (in that case, it's relying on some Windows-defined stuff, such as the _WIN32_ and HWND macros), then every complier has its way to offer an interface between your program and those system-related constants. They're defined in .h files shipped with the compiler, and each compiler has its own way to provide them to your project at compilation time.

Long things short: All Windows-related .h files ("headers") are supposed to be very similar, but some compilers give "too much", while some give "too little".

For example, MinGW seems to give too much since it has already declared "HWND" somewhere. The problem is that it can be SUPER tricky to adjust the code to fix that kind of issues, because system-related headers are hyper complex and have thousands of imbricated macros declared.

When you know what you're looking for, usually commenting a single line or something similar is enough. Otherwise you can get lost easily.

In other words, if you manage to fix it easily with the help of forumers, then grand. Otherwise don't spend too much time on that and install Vsual C++ Express Edition.
Title: Re: Compiling a plugin
Post by: Wyz on Tue 13/07/2010 16:12:30
Quote from: Monsieur OUXX on Tue 13/07/2010 16:03:38
For example, MinGW seems to give too much since it has already declared "HWND" somewhere.
In fact the problem was that Microsoft compilers define _WINDOWS_ and mingw defines _WIN32_
the preprocessor directive checks if the target platform is windows, if not it defines HWND. HWND got falsely redeclared because _WINDOWS_ does not exist in mingw.

Coincidently I've build my first ags plugin today using mingw and it was not that hard. There are a few things you need to configure, mainly the two things already mentioned.
There is one more:
add -DTHIS_IS_THE_PLUGIN=1 to both c and c++ compiler switches.
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 16:24:21
ok next question ^_^

If i wanted to include another library with my plugin how should I do it?

Shoudl I download the binaries and then somehow link that dll so essentially i have two dlls?

orrr do i download the source code and compile it all together...

I'm kinda lost.. I'm ok with programming logic and stuff but dependencies and libraries and stuff freak me out.
Title: Re: Compiling a plugin
Post by: DoorKnobHandle on Tue 13/07/2010 16:26:15
Slightly offtopic intermission: To anybody looking to create AGS plugins I whole-heartedly suggest you download and use Microsoft Visual C++ 2008 Express Edition. It's free, it's a great compiler (even tho it's by the "evil Microsoft people") and the plugin template by CJ works right away (at least it did here).
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 17:05:18
for christ's sake this is annoying the piss out of me...

What i want to do is so damn simple.. its literally about 4 lines of actual code but I cant do the stupid interfacing with the library... The error log is like a train wreck.

this ( http://curlpp.org/index.php/examples/47-example-00 ) is *all* i want to do.. just run a php script with arguments.. all the hard work will be done by the PHP.
Title: Re: Compiling a plugin
Post by: Wyz on Tue 13/07/2010 17:09:49
Quote from: Calin Leafshade on Tue 13/07/2010 16:24:21
Shoudl I download the binaries and then somehow link that dll so essentially i have two dlls?
orrr do i download the source code and compile it all together...

Actually both is possible, but that's where licences come in. Some licences do not allow you to compile everything into one file (static linking), others do but imply you supply the complete source code (like GPL).

Ok, that did not help at all, I know. :D

Erm, well, if AGS does copy both DLLs when including the plug-in I guess that would be the best safest option. If it does not, well, then things get annoying if you want other people using your plugin. Otherwise you can ship them manually.

If you want to know how to link dlls, well there is a lot I can tell about that, but I don't know where to start. :P
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 17:40:05
Quote from: Wyz on Tue 13/07/2010 17:09:49
If you want to know how to link dlls,

Yes I do ^_^

... or at least make the damn thing work..

at this point you can use magic for all i care.
Title: Re: Compiling a plugin
Post by: Wyz on Tue 13/07/2010 19:21:49
I'll give it a shot :)

I'll try to compile the example you showed.
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Tue 13/07/2010 19:31:53
Thank you ^_^
Title: Re: Compiling a plugin
Post by: Wyz on Tue 13/07/2010 22:00:37
I've got it compiling, but it is a lot of work to set it up, let alone explaining how to do it. Instead I cleaned up the test project a bit and uploaded it here:
http://shutupload.com/dl/6e1028952978/

I hope it works for you. :)

Ps.
I used the version that supports SSL, there is also a version with a lot less dlls without SSL but I don't know if that one works.
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 07:53:17
you are a beautiful man with a huge penis.. So i've heard...  :=
Title: Re: Compiling a plugin
Post by: GarageGothic on Wed 14/07/2010 08:06:04
Quote from: Calin Leafshade on Wed 14/07/2010 07:53:17you are a beautiful man with a huge penis.. So i've heard...  :=

I can totally confirm that!  ;D

Thanks Wyz, I downloaded this too. Most likely it will be a great help with my own plugin. I was having such a hard time with the template that I'd actually postponed integrating my drawing functions with AGS and just focused on getting stuff to work as a stand-alone application for the moment.
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 08:23:29
Slight typo in the plugin: the import in the header is testcUrl but the registered function is TestcUrl.

I've fixed it and the plugin does indeed make web requests ^_^

now i just need to generalise it a little and it can be used... now that the framework is in place I can mess around with it as much as i like.

Thanks alot! ^_^

Step one in my plan is done.
Title: Re: Compiling a plugin
Post by: Monsieur OUXX on Wed 14/07/2010 09:30:20
Quote from: Wyz on Tue 13/07/2010 17:09:49
Quote from: Calin Leafshade on Tue 13/07/2010 16:24:21
Shoudl I download the binaries and then somehow link that dll so essentially i have two dlls?
orrr do i download the source code and compile it all together...

Actually both is possible, but that's where licences come in.

Since compilation is also a nightmare for me, I'll also add this little tip for Calin :

- It's usually easier to use the DLLs, because all you have to do is to put them in the execution folder of your project, and add the right .h files in your code (they're usually shipped with the DLLs). And good websites usually offer a zip file with the whole pack of DLLs needed (including the f*****g DEPENDENCIES!) along with a small tutorial on how to get started.

- If you decide to compile the sources of the libraries you want to use, then you have to download the libraries they rely on, and the libraries for the libraries, and so on with all the dependencies - not to mention you'll have to understand how to compile every single one of them.
Good websites also usually offer a tutorial for that, but you'll have to navigate between all the sites, and start over adapting the compilation method for each single project. If you already can't compile a straightforward Windows project like an AGS plugin, then think of the trouble you'll go through to compile a cross-platform, huge, professional library with many dependencies!

Damn I hate makefiles :)  And still I've MADE my own compiler using Flex and Bison :)
Title: Re: Compiling a plugin
Post by: Wyz on Wed 14/07/2010 09:47:09
Quote from: GarageGothic on Wed 14/07/2010 08:06:04
Quote from: Calin Leafshade on Wed 14/07/2010 07:53:17you are a beautiful man with a huge penis.. So i've heard...  :=

I can totally confirm that!  ;D

Loooooool! :D

I'm glad it helped you! The most work was getting cURLpp linked and all, ohwell. Good luck with your projects.   ;D

Quote from: Monsieur OUXX on Wed 14/07/2010 09:30:20
If you already can't compile a straightforward Windows project like an AGS plugin, then think of the trouble you'll go through to compile a cross-platform, huge, professional library with many dependencies!

It takes a zen-like patience. :D
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 09:52:20
heh well now i'm trying to do this:

http://curlpp.org/index.php/examples/71-example-21

or at least this is what i think i want..

obviously if you want to *return* data from a php script you have to wait for the transfer but i obviously cant make the player physically wait.

So i thought i could use a 'stream' and terminate it once its done.

then i can have a function in AGS which returns the string so far and get ags to check for the termination string.

... this is harder than i thought :p
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 11:37:03
Another c++ question..

I know this is getting to the point of not being AGS relevant but it seemed wise to keep it in a single thread.


std::istringstream myStream("a");
string GetString(){
       
      return  myStream.str()
       
       }


ok so this creates a global stream (myStream) and the function GetString() is supposed to return the current value of the stream.

The function .str() should return the string value of the stream but for some reason you cant declare a function like that.

the proper declaration seems to be char* GetString() but thats an array of chars right? and str() doesnt return an array of chars it returns a cstring..

so how do i get a function to return a string?
Title: Re: Compiling a plugin
Post by: Wyz on Wed 14/07/2010 12:12:18
AGS uses c-style strings, you can retrieve them with the c_str() method on a string object.

This should do the trick:

std::istringstream myStream("a");

std::string buffer;

const char *GetString()
{    
buffer = myStream.str();
return buffer.c_str();
}
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 12:18:10
ok that seems pretty straight forward but i dont understand the const keyword.. why is that necessary?
Title: Re: Compiling a plugin
Post by: Wyz on Wed 14/07/2010 12:26:01
Const indicates that the variable is constant and may not be changed by anything that uses the function. IN this case AGS I guess, AGS will make a copy of the string anyway which can be changed. The string buffer does not allow the string to be changed in this way, so that's why it's in there.
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 12:46:46
Ok now i have a new problem (beside my ineptitude) but i think this may be a limitation of the way AGS works.

When running the request function from libcurl it freezes ags for a good 15 seconds. Is this just a limitation of the way the threading works within ags? or is there a way to run the plugin on a separate thread parallel to AGS's?

EDIT: Nevermind, plugin abandoned
Title: Re: Compiling a plugin
Post by: Wyz on Wed 14/07/2010 13:12:47
It is possible yes. The Windows API has the CreatThread (http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx) function.
Title: Re: Compiling a plugin
Post by: Monsieur OUXX on Wed 14/07/2010 13:47:19
Quote from: Calin Leafshade on Wed 14/07/2010 12:46:46
EDIT: Nevermind, plugin abandoned

:(
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 13:53:31
abandoned only because a better plugin has already been made... sans source unfortunately.

This one is a direct tcp/ip plugin so it is far more lightweight and flexible, you just need to supply the http requests yourself.

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=35018.msg458420#msg458420
Title: Re: Compiling a plugin
Post by: Wyz on Wed 14/07/2010 14:25:06
Right, ok. How convenient. :D
Title: Re: Compiling a plugin
Post by: Calin Leafshade on Wed 14/07/2010 14:39:00
Sorry for getting you mess around with libcurl unnecessarily :p

you can have kisses.  :-X