View previous topic :: View next topic |
Author |
Message |
moechofe
Joined: 07 Oct 2006 Posts: 25 Location: Paris, France
|
Posted: Mon Apr 05, 2010 11:55 am Post subject: [FIXED] How to simulate private class members |
|
|
I'm using sfml and I make a class to manager sprite and animation.
I want to control animation, time, effect... with miniD because I'm too lazy to create a complex binary format to do the same thing.
So, I want to give access to the sprite class to miniD, but I don't want to give access to the internal part of my class: like pointer or other private members.
Last edited by moechofe on Wed Apr 21, 2010 12:52 am; edited 1 time in total |
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
|
Back to top |
|
|
moechofe
Joined: 07 Oct 2006 Posts: 25 Location: Paris, France
|
Posted: Wed Apr 07, 2010 11:41 am Post subject: |
|
|
So. I can create a native class.
And I need to use newInstance with extraByte to hide my pointer from miniD side.
That means: I cannot instanciate the class from miniD with "new", isn't it? |
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Wed Apr 07, 2010 8:20 pm Post subject: |
|
|
No, you can; that's why class allocators exist.
You can take a look at the source for the Vector class (/trunk/minid/vector.d) for an example of how to write a class that uses a custom allocator. The page I linked also talks about them. |
|
Back to top |
|
|
moechofe
Joined: 07 Oct 2006 Posts: 25 Location: Paris, France
|
Posted: Fri Apr 09, 2010 2:45 pm Post subject: |
|
|
Thanks,
I missed the "A First Attempt" section when I was reading the "Part5" article. What I want is writing here : allocator, checkIntParam... |
|
Back to top |
|
|
moechofe
Joined: 07 Oct 2006 Posts: 25 Location: Paris, France
|
Posted: Sat Apr 10, 2010 12:55 pm Post subject: |
|
|
I have a miniD class (with allocator and constructor) in a miniD module like this:
Code: | makeModule(t, "Engine", function uint( MDThread* t, size_t num_params)
{
CreateClass(t, "Sprite", (CreateClass* c)
{
c.method("constructor", &constructor);
});
newFunction(t, &allocator, "Sprite.allocator");
newGlobal(t, "Sprite");
return 0;
});
importModuleNoNS(t, "Engine"); |
When I allocate it with Code: | runString("local s = Engine.Sprite(...)"); |
I get the exception: Code: | Engine.Sprite.allocator(native): newInstance - expected 'class' for base, not 'namespace' |
I try several value for the 2nd parameter of newInstance, but I not sure what to do: Code: | static size_t allocator(MDThread* t, size_t num_params)
{
newInstance(t, 0, 0, Sprite.sizeof);
... |
|
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Sun Apr 11, 2010 1:24 pm Post subject: |
|
|
You're never setting the allocator function to the class, so the "Sprite" global variable is pointing at the allocator instead of at the class. You need to change it like so:
Code: | CreateClass(t, "Sprite", (CreateClass* c)
{
c.method("constructor", &constructor);
});
newFunction(t, &allocator, "Sprite.allocator");
setAllocator(t, -2); // <<<< this!
newGlobal(t, "Sprite");
|
That should work. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|