Jump to content


Photo

Gmapi V0.6.2 [updated: 26 Feb 2010]


  • Please log in to reply
337 replies to this topic

#1 Snake_PL

Snake_PL

    GMC Member

  • New Member
  • 57 posts

Posted 22 April 2009 - 02:22 PM

Posted Image

About...


GMAPI is a library for Game Maker DLL developers, that gives the possibility of accessing the game's resources right from the DLL! What it currently features:

  • Ability to use the GML functions within your DLL.
  • Possibility to modify or even register a new GML functions for the game (increases external calling efficiency)
  • Direct access to the game resources, such as:
  • Sprite, background and font bitmaps
  • Sound files
  • Local and global variables/arrays
  • Built-in variables/properties (e.g room_speed, fps)
  • Internal game structures (e.g. the one that holds GM instance's data)
  • Game's Direct3D interfaces (IDirect3D8, IDirect3DDevice8, IDirect3DTexture8)
In the current version you have a possibility to access sprites, backgrounds, surfaces, sounds, scripts, particles, fonts and some other data, such as e.g. game's Direct3D interfaces, window handles, GML functions, GM version and more. Soon I'll try to give you possibility to access other runner's data as well... I'm also thinking about adding new features like adding a sprite to game from bitmap loaded in memory. Well, you can give suggestions what could be added to the library (BTW. something important/useful related to DirectX will be welcomed - I don't know much about that library ;p). Unfortunatelly, because of lack of time i didn't wrote a documentation... there are only some examples & comments in a header file... here i'll explain most important things.
Library is released on LGPL license, so everyone can take a look at the source and check out how it is made, or modify... :(
When project will be in more advanced stage (and there will be time for this ofc) i'll make the library compatible with MinGW compiler ;o

Using the library


Installation

All you have to do is extract the archive to chosen directory and specify the paths to "include" & "lib" directories in IDE. Run Visual C++, select from "Tools" menu "Options..." item, next go to "Projects and Solutions" >> "VC++ Directories" node, now select "Include files" from the "Show directories for" combobox and add the path to "include" directory from GMAPI, then select "Library files" from the combobox and add the path to "lib" directory. Done.

Initialization

Before you'll begin to work with the library you must include gmapi.h header to one of the source files in your project. If you don't have DirectX SDK installed define GMAPI_NO_D3D before including gmapi.h (D3D interfaces will be defined as void), thus this will look like:

#define GMAPI_NO_D3D
#include <gmapi.h>
Now you must add to the library dependencies proper static library - all depends on which runtime library you choose in your project:

gmapi-mt.lib - Multithreaded
gmapi-mt-dll.lib - Multithreaded DLL
gmapi-mt-d.lib - Multithreaded debug
gmapi-mt-d-dll.lib - Multithreaded debug DLL

Next, you must create main library object, that is CGMAPI class. That class is a singleton, so only one instance can be created at runtime in your library. To create an instance of this class use CGMAPI::Create static method, which takes as parameter a pointer to "unsigned long" variable type - it'll be used to return the result of initialization (possible values are: GMAPI_INITIALIZATION_SUCCESS - if initialization succeeded; GMAPI_INITIALIZATION_FAILED - when initialization fails; GMAPI_ALREADY_INITIALIZED - when an instance of CGMAPI class already exists). The method returns pointer to newly created class object, which you should store in a variable. I recommend to do this in DLL's entrypoint, that is DllMain function, for example:
gm::CGMAPI* gmapi;

BOOL WINAPI DllMain( HINSTANCE aModuleHandle, int aReason, int aReserved ) {
  switch ( aReason ) {
    case DLL_PROCESS_ATTACH: {
      // Initialization
      unsigned long result = 0;
      gmapi = gm::CGMAPI::Create( &result );

      // Check the result
      if ( result != gm::GMAPI_INITIALIZATION_SUCCESS ) {
        MessageBox( 0, "Unable to initialize GMAPI.", 0, MB_SYSTEMMODAL | MB_ICONERROR );
        return FALSE;
      }

      break;
    }

    case DLL_PROCESS_DETACH:
      // Release from memory & deinitialize GMAPI
      gmapi->Destroy();
      break;
  }

  return TRUE;
}
CGMAPI::Destroy() method frees the class instance from memory. BTW, all classes, functions, constants etc. in GMAPI are declared in "gm" namespace. You should not call any GML functions immediatelly after initialization - GMAPI hooks external_call function in runner in order to retrieve pointer to current instance (that is, from which the function is called) which is needed to call all GM functions properly in DLL. So, it is necessary to return to GM after initialization. When class object has been created without any problems you can call the GML functions from within your DLL and, of course, you can use CGMAPI class components.

Calling GM functions

When GMAPI is initialized you can now call GM functions from the DLL. All functions are nicely wrapped in the library so you can call them in a simple way, take a look:

int list;
gm::CGMVariable value;

list = gm::ds_list_create();

gm::ds_list_add( list, "test" );
gm::ds_list_add( list, 12345.12345 );

value = gm::ds_list_find_value( list, 0 ); // store "test" in the variable
value = gm::ds_list_find_value( list, 1 ); // return 12345.12345 to the variable

gm::ds_list_destroy( list );
CGMVariable class object that is defined in this code snippet mimics the GM variable, so it can be set to either string or real value. It must be used to store return values from functions that can return values that type is not known (e.g. ds_list_find_value can return either string or real). Also, you must pass it in parameters in some GM functions for the same reason... besides, that class emulates string type from Delphi - it deals with string allocation, "conversion" from char*, deallocation etc... using runner - char* is not fully compatible with Delphi string ("length" variable, reference counter for the garbage collector) so i had to do something ^^ Oh, one more thing... you should always return the result of GM function that can return string to the CMGVariable class object - it'll take care of deallocation.
All GM functions are available in GMAPI, excluding math functions and those that are operating on strings (and functions such as is_real or is_string). In addition, class has some constructors overloaded and it can be instatiated implicitly by passing a value in parameters that type is a reference to a CGMVariable class (e.g. ds_list_add takes CGMVariable reference type in second parameter, in above code snippet there will be created an temporary instance of class by passing "char*" or "double" to the function). There are also some operator overloading implemented, like "++", "-=" or casting to double/char*, plus "<<" overload for std::ostream :)

Accessing game resources

You can access them by a three ways: with GM functions of course (least efficent, less possibilities), by GM's structures that I've analyzed in runner and with "accessing classes" defined in GMAPI. Using structures is most efficent and equally unsafe, I'll explain how to use them in a documentation that I'll write later. Meanwhile, you can use the classes I mentioned - they're safer and easier to use than using the GM's structures. Why I called them "accessing" (wtf) ? Well, they're operating entirely on runner's memory, what is also reason why using them is somewhat entangled. In CGMAPI class there are some objects of these classes defined: Sprites (ISprites class), Backgrounds (IBackgrounds), Sounds (ISounds), Surfaces (ISurfaces) and Scripts (IScripts). All of these classes shares some methods that can retrieve various data that is relevant to respective GM resource type (for example, GetID() returns an ID of specified resource by given name of this type, GetCount() returns number of resources of this type) and all of them have an array operator ("[]") overloaded that returns reference type to next "accessing classes" - these classes gives you access to resource with ID specified in brackets. Damn, hard to explain... so, for example:

gm::CGMAPI* gmapi;
(...)
// Let's say that we've added an animated sprite named spr_anim
// to the game, now we want to get width of the sprite
int sprAnim, spriteWidth;

// First, we must get the ID
sprAnim = gmapi->Sprites.GetID( "spr_anim" );

// Now we will get the width
spriteWidth = gmapi->Sprites[sprAnim].GetWidth();

// What if our sprite has not been found because we didn't added it
// to the game ? An exception with EGMAPISpriteNotExists class object will be thrown
// (exception class), we will catch it with "try - catch" of course
try {
  spriteWidth = gmapi->Sprites[sprAnim].GetWidth();
}
catch ( gm::EGMAPISpriteNotExist& exc ) {
  exc.ShowError(); // Show message with information about error
}

// Operator [] in ISprites class returns ISprite reference type - that class is
// the only one of these "higher level" that have defined object of next such
// class - ISpriteSubimages (Subimages object), which gives you access to specified
// sprite subimage. Hm, let's say we want to store all of the texture ID (a'la
// sprite_get_texture from GM) in a vector array:
std::vector<int> textures;

for ( int i = 0; i < gmapi->Sprites[sprAnim].Subimages.GetCount(); i++ )
  textures.push_back( gmapi->Sprites[sprAnim].Subimages[i].GetTextureID() );
Well, that's all about accessing game resources.

Notes:

As I mentioned before, I'll write a documentation later. Meanwhile, you have to rely on IntelliSense, comments in GmapiInternal.h header file and on 3 crappy examples I wrote ;p In GMAPI there are also constants from GM. Of course please report all bugs you've found - project is still in testing stage.

Download:


You can find all the versions of GMAPI here: (the newest is 0.6.2)
http://code.google.c.../downloads/list

Examples:

My BBCode engine ( http://forum.gmclan....showtopic=15031 ) rewritten to C++:
src: http://gmclan.org/up...eEngineSrc.html
demo: http://gmclan.org/up...EngineDemo.html

Bitmap viewer:
src: http://gmclan.org/up...pViewerSrc.html
demo: http://gmclan.org/up...ViewerDemo.html

Direct3D interfaces test (hardcoding crap ;)):
src: http://gmclan.org/up...exampleSrc.html
demo: http://gmclan.org/up...xampleDemo.html

Example of accessing GM variables:
src: http://gmclan.org/up...riablesSrc.html
demo: http://gmclan.org/up...iablesDemo.html

GM function adding/replacing example:
src: http://gmclan.org/up...MFunctions.html

Particle & built-in variables accessing test:
src: http://gmclan.org/up...rticleTest.html

Resources:

GMAPI Wiki: http://gmapi.roket-enterprises.com/
GMAPI logos: http://gmclan.org/up...MAPI_logos.html
DirectX 8 SDK: http://www.darwinbot...x81sdk_full.exe

DLLs that uses GMAPI:

Precise 3D Collisions by brett14
RoketGames Game Integration by Hach-Que
G-pathfinding by paul23
GFFI by Clam
GMOgre3D by Houdini
gmPython by Hach-Que
Xenon PlayGate by PsichiX
Advanced console by TheMagicNumber
gmLua by Kofel


Edited by Snake_PL, 29 September 2010 - 04:42 PM.

  • 3

#2 gnysek

gnysek

    GMC Member

  • GMC Member
  • 323 posts
  • Version:GM:Studio

Posted 22 April 2009 - 02:44 PM

Thanks dude, it's awesome!
  • 0

#3 paul23

paul23

    GMC Member

  • Global Moderators
  • 3363 posts
  • Version:GM8

Posted 22 April 2009 - 03:14 PM

Well as I said in pm, maybe inverse the usage of d3d8.h: it's nowadays difficult to get the directx8 develop thing, (you can't get from MSDN anymore) - so you'll always have to add this line:
#define GMAPI_NO_D3D

maybe make it so that d3d8.h is ONLY used if something is defined....
  • 0

#4 Snake_PL

Snake_PL

    GMC Member

  • New Member
  • 57 posts

Posted 22 April 2009 - 03:22 PM

Ok, I'll correct that in next version.
  • 0

#5 ditdingiscool

ditdingiscool

    GMC Member

  • New Member
  • 335 posts

Posted 25 April 2009 - 08:33 AM

hi there, i'm kindoff new to c/c++ so i neaad some help setting this up, especialy what do i have to do EXACTLY when you say this:

Now You must add to the library dependencies proper static library - all depends on which runtime library You choose in Your project:
gmapi-mt.lib - Multithreaded
gmapi-mt-dll.lib - Multithreaded DLL
gmapi-mt-d.lib - Multithreaded debug
gmapi-mt-d-dll.lib - Multithreaded debug DLL

(using dev c++)

thanks already ;)
  • 0

#6 tuntis

tuntis

    GMC Member

  • New Member
  • 1839 posts

Posted 25 April 2009 - 10:44 AM

(using dev c++)

read
  • 0

#7 ditdingiscool

ditdingiscool

    GMC Member

  • New Member
  • 335 posts

Posted 25 April 2009 - 01:28 PM

(using dev c++)

read

that page doesn't give any real argument, it only has one weak argument "it's outdated", but so is windows xp and that sucks less than vista, but ok, whatever, downloading code::blocks

EDIT:ok, that really solved nothing,

Process terminated with status 1 (0 minutes, 4 seconds)
50 errors, 22 warnings


Edited by ditdingiscool, 25 April 2009 - 01:42 PM.

  • 0

#8 freaked

freaked

    freak up!

  • New Member
  • 890 posts

Posted 25 April 2009 - 01:51 PM

Post some of those 50 errors.
Most probably they'll be linker related errors.
Also,(AFAIK) you'll have to convert those .lib files to .a archives for GCC's linker to use.
Use a tool in your bin folder called REIMP to do this.

Also Snake_PL, what do you use to hook d3d functions ?

Edited by freaked, 25 April 2009 - 01:53 PM.

  • 0

#9 tuntis

tuntis

    GMC Member

  • New Member
  • 1839 posts

Posted 25 April 2009 - 01:55 PM

it only has one weak argument "it's outdated", but so is windows xp and that sucks less than vista, but ok, whatever, downloading code::blocks

...Except that XP is not outdated because MS still provides support for it.
  • 0

#10 TheMagicNumber

TheMagicNumber

    GMC Member

  • GMC Member
  • 5247 posts
  • Version:Unknown

Posted 25 April 2009 - 03:08 PM

If nobody is working on it, can you work on a Code::Blocks version? (Tuntis, if you ever tell me to use VC++, I despise Microsoft products)

By the way, Windows XP was unsupported for a while, but they extended the support, probably because many people downgraded from Windows Vista.
  • 1

#11 tuntis

tuntis

    GMC Member

  • New Member
  • 1839 posts

Posted 25 April 2009 - 04:30 PM

(Tuntis, if you ever tell me to use VC++, I despise Microsoft products)

Well, if you intend on using this thing right now, I can tell you that VC++ Express will be the quickest and most pain-free solution...
  • 0

#12 GearGOD

GearGOD

    Deus Verus

  • GMC Member
  • 2153 posts

Posted 25 April 2009 - 04:54 PM

Tuntis, if you ever tell me to use VC++, I despise Microsoft products

You're not using a dev tool because you despise that company that its affiliated with? Even though its arguably the best one around and free? You're so awesome.
  • 1

#13 ditdingiscool

ditdingiscool

    GMC Member

  • New Member
  • 335 posts

Posted 26 April 2009 - 07:22 AM

Tuntis, if you ever tell me to use VC++, I despise Microsoft products

You're not using a dev tool because you despise that company that its affiliated with? Even though its arguably the best one around and free? You're so awesome.

i'm also not a big fan off vc++, and not only because it's from micro$oft, it has 'adobe reader syndrome'(load a document, have lunch, wait another 10 minutes for it to load), and it isn't able to create empty projects on my pc...

ONTOPIC:
||=== vonoroi, default ===|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|32|warning: `GMDeallocateString' initialized and declared `extern'|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|32|error: variable or field `GMDeallocateString' declared void|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|32|error: `gm::core::GMDeallocateString' declared as an `inline' variable|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|32|warning: `__stdcall__' attribute only applies to function types|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|32|error: `__in' was not declared in this scope|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|33|warning: `GMDeallocateResult' initialized and declared `extern'|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|33|error: variable or field `GMDeallocateResult' declared void|
C:\Documents and Settings\lieuwe\Bureaublad\bende\GMAPI\dll_vonoroi\GmapiCore.h|33|error: `gm::core::GMDeallocateResult' declared as an `inline' variable|
<snip>
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 22 warnings ===|

  • 0

#14 X-tra Fear

X-tra Fear

    Behemoth Creator

  • GMC Member
  • 430 posts
  • Version:GM8

Posted 26 April 2009 - 09:08 AM

Honestly, I think you should have made this compatible with the GCC compiler also.

You're not using a dev tool because you despise that company that its affiliated with? Even though its arguably the best one around and free? You're so awesome.


Errr, he's not trying to be awesome, he has just been mis-guided about Microsoft products.

A lot of them are actually pretty good, and VC++ is one of them.
Honestly, if you were to ever download a Microsoft product, I reccomend you try that one.

Download Visual C++ Express

You could have at least been helpful GearGod. :/

EDIT: Honestly, I think GMREC uses linux anyway, and I have no clue why he is really worrying about Game Maker in the first place... I don't think WINE can even run it.

Edited by X-tra Fear, 26 April 2009 - 09:13 AM.

  • 0

#15 tuntis

tuntis

    GMC Member

  • New Member
  • 1839 posts

Posted 26 April 2009 - 10:24 AM

Errr, he's not trying to be awesome

Oh trust me, this guy refuses to use pretty much any Microsoft technology because it's a "giant fag company". Funny how he still uses Windows...
  • 0

#16 X-tra Fear

X-tra Fear

    Behemoth Creator

  • GMC Member
  • 430 posts
  • Version:GM8

Posted 26 April 2009 - 11:15 AM

Errr, he's not trying to be awesome

Oh trust me, this guy refuses to use pretty much any Microsoft technology because it's a "giant fag company". Funny how he still uses Windows...


Ohhhh, I don't really know him all that well... lol.
  • 0

#17 freaked

freaked

    freak up!

  • New Member
  • 890 posts

Posted 26 April 2009 - 11:57 AM

Lol. GMREC and me even had PM discussion involving C::B's greatness and MSVC's suckiness.
  • 0

#18 TheMagicNumber

TheMagicNumber

    GMC Member

  • GMC Member
  • 5247 posts
  • Version:Unknown

Posted 26 April 2009 - 01:57 PM

Funny how he still uses Windows...

Rarely.

And I did want a GCC version.
  • 1

#19 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14392 posts
  • Version:GM:Studio

Posted 26 April 2009 - 11:55 PM

What is weird is your DLLMain, which is proper to use in that manner but... I found out a while back in my dlls that this is never called for dll's used by GM (7)
  • 0

#20 paul23

paul23

    GMC Member

  • Global Moderators
  • 3363 posts
  • Version:GM8

Posted 27 April 2009 - 10:31 PM

What is weird is your DLLMain, which is proper to use in that manner but... I found out a while back in my dlls that this is never called for dll's used by GM (7)

? - Uhm well I found out that they are actually called, DLL_PROCESS_ATTACH, when you 'initialize' your first function.. And DLL_PROCESS_DETTACH when you unload the dll. (Also using GM 7)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users