What exactly is does "external only" mean referring to classes?

Posted By: PrenceOfDarkness

What exactly is does "external only" mean referring to classes? - 03/31/11 01:47

The manual says that C++ classes can be used in lite-c but only external. What exactly does this mean? Does that mean I have to use a DLL to do so?

Man, the thought of using classes in lite-c just makes my mouth salivate O_O'.
Posted By: Quad

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 06:19

yes, that means you can only use them with c++ plugins.
Posted By: PrenceOfDarkness

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 10:40

still great news. just wanted to verify a bit more on this. i would just try it but i wont be home for a while. if i define an object via dll can i make instances of it in lite-c? if not can i make instances in the dll and use functions of the class in lite-c?
Posted By: Quad

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 11:03

it basically works this way:

you create an instance/object and return it's pointer to lite-c
then wrap all functions of tat class in c++ as DLLFUNC s, make it so that it takes an instance pointer and necessary parameters, and calls that function over that instance with the given paramaters.

all code below is psuedo-code:

say, this is your class
Code:
class AwesomeClass{
   private: int a, int b;
   public AwesomeClass(int x,int y){
      a = x; b = y;
   }
   public void setA(int newa){
        a = newa;
   }
   public void setb(int newb){
        b = newb;
   }
   public int sum(){
       return a+b;
   }
}



this is the wrapping in c++ part:
Code:
DLLFUNC AwesomeClass* ac_create(int a, int b){
    return new AwesomeClass(a,b);
}

DLLFUNC void ac_setA(AwesomeClass* ac, int newa){
    ac.setA(newa);
}

DLLFUNC void ac_setB(AwesomeClass* ac, int newB){
    ac.setB(newB);
}

DLLFUNC int ac_sum(AwesomeClass* ac){
    return ac.sum();
}



this is how you use it in lite-c
first your Lite-c header, you may want to call it awesomeclass.h:
Code:
#define AwesomeClass int
AwesomeClass* ac_create(int a, int b);
void ac_setA(AwesomeClass* ac, int newa);
void ac_setB(AwesomeClass* ac, int newB);
int ac_sum(AwesomeClass* ac);


and then use it in lite-c:
Code:
AwesomeClass* myinstance = ac_create(3,5);
AwesomeClass* anotherinstance = ac_create(10,11);//you can create as many instances as you want
printf("%d",ac_sum(myinstance));//will show you 8
ac_setA(myinstance,5);
printf("%d",ac_sum(myinstance));//will show you 10


Posted By: 3dgs_snake

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 11:57

Hi,

If you create a class in a dll, you can access all its properties
with a struct with the same signature. You just need to create plain functions in the dll to call class methods. To create an instance of a class, you create a function in the dll that returns the instance you want (you also need to call the destructor of the class in a similar function).

ex : C++
Code:
class CObject
{
public:
   // Constructor
   CObject(int, int, int);
   // Destructor
   ~CObject();
   int a;
   int b;
   int c;
   
  // Sample method
  void foo(); 
}

/**
 * Constructor
 */ 
CObject::CObject(int x, int y, int z)
{
   this->a = x;
   this->b = y;
   this->c = z;
}

/**
 * Destructor
 */
CObject::~CObject()
{
}

// Sample method
void CObject::foo()
{
   this->c = this->a + this->b;
}

// Create instance of the class
DLLFUNC CObject *CObject_Create()
{
   return new CObject();
}

DLLFUNC void CObject_Destroy(CObject *instance)
{
   delete instance;
}

// This function will be called in lite-c
DLLFUNC void CObject_foo(CObject *instance)
{
   // Call the function
   instance->foo();
}




Lite - C
Code:
typedef struct CObject
{
   int a;
   int b;
   int c;
}CObject;

// Create instance of the class
CObject *CObject_Create();
// Destroy instance of the class
void CObject_Destroy(CObject *instance);
// Call method foo
void CObject_foo(CObject *instance);



I think that will help you.

blush OOps! too late again

Best regards.
Posted By: WretchedSid

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 12:31

Hey snake, just a short thing, your struct might not represent the real objects in memory.
The memory layout is only guaranteed for POD types, the compiler might do some optimization on the memory layout of your class and then accessing 'a' might result in an access to 'b' and 'c' might point to some foobar variable introduced by the compiler or whatsoever.

Also remark that structs in Lite-C are always __attribute__((packed)), so you might have to add padding variables depending on your architecture into the struct

So yeah, it is definitely a source of evil and hard to debug bugs, so you might have to look for some other way for communication (beside that having public variables outside of POD types is also considered as a very bad style).
Posted By: Quad

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 12:41

infact almost most of the time, the a,b,c in your struct wont be the a,b,c from your instance, so just as i did, define your type as int, and only use pointers, and getters/setters.
Posted By: 3dgs_snake

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 12:53

blush Yes, that's true. It's just a test I made long days ago. Thanks for pointing this out.

Best regards.
Posted By: Tobias

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 15:10

It should be mentioned that lite-C only supports class methods, but no inherited methods. I found that out some time ago. So you need to define the inherited methods again in the lite-C class.

You can see this in how the Direct X classes are defined in lite-C, like this:

Code:
#define INTERFACE IDirect3DTexture9

DECLARE_INTERFACE_(IDirect3DTexture9, IDirect3DBaseTexture9)
{
    /*** IUnknown methods ***/
    STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
    STDMETHOD_(ULONG,Release)(THIS) PURE;

    /*** IDirect3DBaseTexture9 methods ***/
    STDMETHOD(GetDevice)(THIS_ IDirect3DDevice9** ppDevice) PURE;
    STDMETHOD(SetPrivateData)(THIS_ REFGUID refguid,CONST void* pData,DWORD SizeOfData,DWORD Flags) PURE;
    STDMETHOD(GetPrivateData)(THIS_ REFGUID refguid,void* pData,DWORD* pSizeOfData) PURE;
    STDMETHOD(FreePrivateData)(THIS_ REFGUID refguid) PURE;
    STDMETHOD_(DWORD, SetPriority)(THIS_ DWORD PriorityNew) PURE;
    STDMETHOD_(DWORD, GetPriority)(THIS) PURE;
    STDMETHOD_(void, PreLoad)(THIS) PURE;
    STDMETHOD_(D3DRESOURCETYPE, GetType)(THIS) PURE;
    STDMETHOD_(DWORD, SetLOD)(THIS_ DWORD LODNew) PURE;
    STDMETHOD_(DWORD, GetLOD)(THIS) PURE;
    STDMETHOD_(DWORD, GetLevelCount)(THIS) PURE;
    STDMETHOD(SetAutoGenFilterType)(THIS_ D3DTEXTUREFILTERTYPE FilterType) PURE;
    STDMETHOD_(D3DTEXTUREFILTERTYPE, GetAutoGenFilterType)(THIS) PURE;
    STDMETHOD_(void, GenerateMipSubLevels)(THIS) PURE;
    STDMETHOD(GetLevelDesc)(THIS_ UINT Level,D3DSURFACE_DESC *pDesc) PURE;
    STDMETHOD(GetSurfaceLevel)(THIS_ UINT Level,void** ppSurfaceLevel) PURE;//IDirect3DSurface9
    STDMETHOD(LockRect)(THIS_ UINT Level,D3DLOCKED_RECT* pLockedRect,CONST RECT* pRect,DWORD Flags) PURE;
    STDMETHOD(UnlockRect)(THIS_ UINT Level) PURE;
    STDMETHOD(AddDirtyRect)(THIS_ CONST RECT* pDirtyRect) PURE;
};
MY_DECLARE_INTERFACE(IDirect3DTexture9)



This defines the IDirect3DTexture9 class, but the IDirect3DBaseTexture9 is ignored in the DECLARE_INTERFACE definition. So all IDirect3DBaseTexture9 methods are declared again in the IDirect3DTexture9 class under lite-C.
Posted By: Joozey

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 15:30

All in all, imo there's little use to port classes through DLL to lite-c. Either simulate an object oriented way in lite-c native, or code directly into C++ or C#.
Posted By: Uhrwerk

Re: What exactly is does "external only" mean referring to classes? - 03/31/11 15:43

Exactly. Every of the advantages of object oriented programming gets lost as soon as you connect the classes to lite-c. So do it directly in lite-c. This will save you some headaches.

The only exception for that are already written c++ libraries you want to connect to lite-c. In this case you might save some work with this method.
Posted By: PrenceOfDarkness

Re: What exactly is does "external only" mean referring to classes? - 04/02/11 19:59

thanks guys for all the input. it just seems writing directly in c++ would be so much more work just to use classes though no?
Posted By: Uhrwerk

Re: What exactly is does "external only" mean referring to classes? - 04/02/11 21:00

Object oriented programming has got some advantages, i.e. encapsulation, inheritance and polymorphism. When writing code in C++ you can take full advantage of these. Now encapsulation, inheritance and polymorphism do not enrich the concept of programming in a sense that you're suddenly able to do things you haven't been able to do before. Instead they try to make programming more intuitive with repsect to the real world and try to give you means to avoid common errors.

The problem with Gamestudio is now, that you have to provide a way of offering the code you have written in c++ to lite-c. In order to do this you create a dll and this dll provides functions. At this point any advantage of object oriented programming languages gets immediately lost. The advantages inside the code of the library are still present of course, however you cannot transport them over to lite-c. The more of work you have to do - compared to directly programming in lite-c - is creating the dll and providing the corresponding header file for that library which can really give you some headaches sometimes.
© 2024 lite-C Forums