PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

C++ misery and GapiDraw

I've been a games programmer for several years, but using C rather than C++. I know this question is due to lack of C++ knowledge rather than anything wrong with GapiDraw, but it's driving me mad:

I have a drawing function/method (PlotCard) for a game. It needs to go inside the CMyApplication class so as to have access to protected variables and methods. However, I can't access that function from outside the class directly from my C code (because it's not a member of the global name space)and I can't call it by a call to myapp.PlotCard because myapp isn't a global variable (and I don't see how you *can* have a global instance of an object because it has to be constructed at runtime).

I suppose I *could* dump all my game code into the CMyApplication object (or make a dummy 'friend' object)... but that seems really over the top. All I want is some way for my C functions to access the GapiDraw drawing functions from outside the CMyApplication object, or of calling a method within that object.

I know my problem comes from lack of C++ knowledge. But I would appreciate any help.

Cheers,
Tom
eek... I've now found a disgusting workaround:

declare a global...
CMyApplication* g_myapp = NULL;

put this line in the constructor...
g_myapp = this;

and extern it elsewhere...
extern CMyApplication* g_myapp;

I'd still like it if someone could supply me with a *proper* solution though :)
InexorableTash · 30 May 2003, 17:47 UTC
That's actually a fairly standard approach.

One of the many reasons to use classes (in C++ and other languages) is to encapsulate information; data that should be stored together actually is, and so are the tools (methods) for operating on the data.

A consequence of this is that code that wants to operate on data wrapped up in classes has to get access to that data.

You'll find that a lot of C++ code looks like this:

this->GetDialog().GetParentWindow().GetActiveDocument().GetCurrentTextSelection().IsBold()

You're always having to write code that burrows from your current context into the place that has the data you need to operate on. If you've looked at OO lightweight/glue coding lately (Python, JavaScript, VB, VBScript) you'll see plenty of code of the form foo.bar.baz.bat. The major pain in learning, say, JavaScript for web pages, is not the syntax; it's where you need to go in the object heirarchy to get anything done! And that's pretty much all you ever do in these glue languages - chain object references together to get data from point A to point B.

In your example, you've needed to have an object provide access to itself to the global context, so your approach is just fine. Another approach might be to have a static method (that is, it's on the class instance, not the object) and a static member variable, and have the static method return the static member, e.g.:

Code:

class Foo
{
public:
Foo() { s_Foo = this; }
static Foo* GetFoo() { return s_Foo; }

private:
Foo* s_Foo;
}


This avoids poluting the global namespace, since now you call Foo::GetFoo() instead of using g_Foo directly. C++ programmers like this sort of encapsulation since now it's harder for someone else working on the same code base as you (or yourself in 6 months when you've forgotten) to break the code by slamming the global variable or changing how it's initialized.

But your solution - a global variable - works just fine and I use it frequently in small apps like Win32 utilities where g_hInst is just too handy and too truly global to fuss over.
Cheers, IT - thanks for the help... makes me feel better to know that what I'm doing is not *too* dirty, even if it's not quite in the spirit of OOP :)

Tom
Conan · 02 Jun 2003, 21:46 UTC — to c++ or not to C++
Hi Tom, you have my sympathies. If it helps I found that so long as I could understand what classes are about I could happily ignore them & get on regular non oop coding. I just rip out the 'private' if I want to access the variables in a class as this is much less work than writing a function just to obtain the value of a variable.

I did write one class for sprites as that's something complex that I use over & over but otherwise I'm sticking to good old non-oop techniques & curiously my code runs just as fast & is no bigger than the equivalent oop stuff.

Don;t get me wrong. I'm not knocking oop technoques, just mentioning that you don't need them to write a program with these tools. ps I don't use Gapi Draw, I'm using PF but of course GD is not the only author supported toolkit left ( for the time being )
golan trevize x · 03 Jun 2003, 02:52 UTC
One way that I find, which still conforms with the "spirit" of OO, is to pass in a pointer to the major class when you initialise the minor class.

eg. Say you have a single CInterface class which contains a lot of things for your app, and you need it inside say, a CTextBox class, you could:

1) First instantiate and initialise your CInterface object, in the main program perhaps.
2) After you instantiate a new CTextBox object, you can pass in the CInterface pointer at initialisation.

So, you're not making copies of the CInterface object, just passing in a pointer to it.

Now, this may seem a bit tiresome, to set up so many minor classes with the ability to receive a pointer to this CInterface class, but I think it's kind of elegant and goes with the OO paradigm fairly well.

Cheers,
Nathan