Jump to content

[3.1] C++, passing Context to other objects.


Pastaspace
 Share

Recommended Posts

I've been working on converting my game to 3.1's engine, and I've run into a bit of a snag.

Basically, the things that handle my inventory are separate objects, in separate files from the main Start and Loop portions. These objects also contain the functions for drawing the player's inventory (a mix of DrawImage and DrawText). However, this doesn't seem to work, and I always get an "Assert failed" error.

 

This is a basic version of what my code is doing. In my main program, I create a context window as well as an inventory object and similar.

 

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
inventoryObject myObject;
bool App::Start()
{
 window = Window::Create();
 //Create a rendering context
 context = Context::Create(window);

 return true;
}

 

 

Somewhere in my InventoryObject's list of functions is the one that draws its image, it it passed the context it renders on from the main program. InventoryObject.cpp is, obviously, a separate file.

 

void inventoryObject::DrawObject(Context* cc_Context)
{
cc_Context->DrawImage(myImage, 0, 0);
}

 

Then in the main loop, it calls this function.

 

bool App::Loop()
{
 if (window->Closed() || window->KeyDown(Key::Escape)) return false;

 Draw::SetColor(0,0,1);
 context->Clear();
 Draw::SetColor(1,0,0);
 myObject.DrawObject(context);

 context->Sync();	
 return true;
}

 

However, again, I get an assert fail error. Is there something here that I'm missing, or do I have to draw all my context images and text in the main program only?

Link to comment
Share on other sites

Are those Start and Loop codes you posted snippets or complete code?

 

Have you initialised the myObject in the Start method?

 

 

Dont forget that you need to update and render the world.

Time::Update();
world->Update();
world->Render();
//Do your drawing here
context->Sync(false);

Link to comment
Share on other sites

I've done all that. This is not the actual code, merely a similar example to help demonstrate what I'm trying to accomplish. What I'm trying to do is use DrawImage and similar Context functions in a separate object. I can call anything just fine in the main body where the original context object was initialized, but cannot pass it to another object for use, this is what I'm trying to figure out.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...