Orx - Portable Game Engine
Welcome, Guest
Please Login or Register.    Lost Password?
Creating objects without config
(1 viewing) 1 Guest
Go to bottom
TOPIC: Creating objects without config
#8621
rgdev
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Creating objects without config 8 Months ago Karma: 0
Hi all, I'm currently learning Orx, and after some struggling I figured out how to create objects from code, without using any config.

I didn't see this documented anywhere. All the examples seem to be using orxObject_CreateFromConfig.

Here is an example of how to create an object purely from code:

Code:


orxOBJECT * createImageObject (char * filename, float pos_x,
                float pos_y, float scale_x, float scale_y)
{
    orxOBJECT * obj = orxObject_Create ();

    orxFRAME * frame = orxFrame_Create (orxFRAME_KU32_FLAG_NONE);
    orxObject_LinkStructure (obj, (orxSTRUCTURE*) frame);

    orxTEXTURE * tex = orxTexture_CreateFromFile (filename, orxTRUE);
    orxFLOAT image_x;
    orxFLOAT image_y;
    orxTexture_GetSize (tex, &image_x, &image_y);

    orxGRAPHIC * gfx = orxGraphic_Create ();
    orxGraphic_SetData (gfx, (orxSTRUCTURE*) tex);
    orxObject_LinkStructure (obj, (orxSTRUCTURE*)gfx);

    orxVECTOR v;

    v.fX = image_x;
    v.fY = image_y;
    v.fZ = 1.0;
    orxObject_SetSize (obj, &v);

    v.fX = pos_x;
    v.fY = pos_y;
    v.fZ = 0.0;
    orxObject_SetPosition (obj, &v);

    v.fX = scale_x;
    v.fY = scale_y;
    v.fZ = 1.0;
    orxObject_SetScale (obj, &v);

    return obj;
}



The important part here is creating a frame, and linking it to the object. Without that the object won't appear. Then a texture and a graphic can be created and linked to the object in that way. Finally the object's position, size and scale can be set. (Size comes from the image size in this example.)

I have only tested this on Android, but I guess it would work on other platforms too.

(Also note, that I think Orx's config system is quite good, but I needed completely dynamic objects for my project.)
 
Logged Logged
  The administrator has disabled public write access.
#8622
iarwain
Administrator
Posts: 3120
graph
User Offline Click here to see the profile of this user
Gender: Male Orx - Portable Game Engine Location: Montreal
Re:Creating objects without config 8 Months ago Karma: 71
Hi rgdev and welcome here!

Don't hesitate to ask questions either here or on the chat (gitter.im/orx/orx) if you find yourself stuck or struggling too much.

In your example, you'll be in charge of doing the cleanup yourself: you're the owner of what you directly created, in this case the orxGRAPHIC, the orxTEXTURE and the orxFRAME.

If you still want a dynamic approach but without having to trouble yourself with the bookkeeping, you can modify the config's content on the fly. Here's a quick example (I've written it directly on the forum so it might not fully work/compile):

Code:


orxOBJECT * createImageObject (char * filename, float pos_x,
                float pos_y, float scale_x, float scale_y)
{
    orxConfig_PushSection("ImageObject");

    orxConfig_SetString("Graphic", "@");
    orxConfig_SetString("Texture", filename);

    orxConfig_PopSection();

    orxOBJECT * obj = orxObject_CreateFromConfig("ImageObject");

    if(obj)
    {
        orxVECTOR v;

        orxVector_Set(&v, pos_x, pos_y, 0.0);
        orxObject_SetPosition(obj, &v);

        orxVector_Set(&v, scale_x, scale_y, 1.0);
        orxObject_SetScale(obj, &v);
    }

    return obj;
}



Of course both position and scale could also be passed via config, but in that case there's no ulterior bookkeeping to be made so I left them as is. You can of course modify any other relevant property or inherit from other sections as you see fit.
 
Logged Logged
 
Last Edit: 2017/01/30 00:39 By iarwain.
 
Want to follow orx's development? Check our Trello board: http://trello.com/b/g4jMS6jc/orx-tasks
  The administrator has disabled public write access.
#8623
rgdev
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:Creating objects without config 7 Months, 4 Weeks ago Karma: 0
Hi iarwain, thanks for all the info.

I guess that's the more standard way of creating objects dynamically in Orx.
 
Logged Logged
  The administrator has disabled public write access.
#8624
iarwain
Administrator
Posts: 3120
graph
User Offline Click here to see the profile of this user
Gender: Male Orx - Portable Game Engine Location: Montreal
Re:Creating objects without config 7 Months, 4 Weeks ago Karma: 71
Hi rgdev,

My pleasure. Your approach definitely works so don't hesitate to use it if you prefer it. I'm just a bit lazy and I'd rather have the engine doing the cleanup instead of doing it myself, that's why I choose the second option.
 
Logged Logged
 
Want to follow orx's development? Check our Trello board: http://trello.com/b/g4jMS6jc/orx-tasks
  The administrator has disabled public write access.
#8625
sausage
Moderator
Posts: 710
graphgraph
User Offline Click here to see the profile of this user
Gender: Male sausagejohnson http://waynejohnson.net Location: Near Canberra, Australia
Re:Creating objects without config 7 Months, 4 Weeks ago Karma: 3
Hi rgdev. Welcome.

This might make a good small tutorial if you don't mind me pinching the code for the wiki.
 
Logged Logged
 
Indie gamedev with Orx at night. Always feel free to come and say hi over in gitter.im/orx/orx gitter chat or drop me a message.
  The administrator has disabled public write access.
#8629
rgdev
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:Creating objects without config 7 Months, 3 Weeks ago Karma: 0
Hi sausage,

I think a tutorial about manually creating objects and cleaning them up would be a good idea. Sure, feel free to reuse any of this code.
 
Logged Logged
  The administrator has disabled public write access.
#8734
trovetaira
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
Re:Creating objects without config 3 Months ago Karma: 0
Thanks for the advice.
 
Logged Logged
 
Last Edit: 2017/09/15 02:13 By trovetaira.
  The administrator has disabled public write access.
#8735
iarwain
Administrator
Posts: 3120
graph
User Offline Click here to see the profile of this user
Gender: Male Orx - Portable Game Engine Location: Montreal
Re:Creating objects without config 3 Months ago Karma: 71
Hi Trovetaira and welcome here!

If you need more advices, have any questions or just want to chat, don't hesitate to use the chat as well: gitter.im/orx/orx
 
Logged Logged
 
Want to follow orx's development? Check our Trello board: http://trello.com/b/g4jMS6jc/orx-tasks
  The administrator has disabled public write access.
Go to top