Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / I have some questions about DarkGDK!

Author
Message
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 7th Apr 2013 19:27
I may have posted a question similar to this in the past but don't remember! I have been studying C++ lately and DBP also. In c++ I know about classes and am really looking forward to learning that but am pretty much stuck on pointers. Don't really understand them.

I have a small project started in DBP and want to port it over to DarkGDK. In DarkGDK is it mix c++ with DBP commands or does all my DBP commands have to go in that while loop? Not quite sure what that while loop does, guessing it is like the do loop/main game loop in DBP? When I do get to classes (Soon I hope!) do I have to make the classes in this while loop? or can I throw DBP commands in where ever I need them? I have looked at the tutorials that came with GDK but looking for more.

Any down to earth easy to understand explanation on pointers would be great! I do understand that they point to a memory address of something but don't understand why use them or do you even have to? I have been thinking about this and am wondering if pointers are a way to access a variable or function that may be local to a class or a variable local only to a function? I have read up a lot on pointers but just don't get it. I think if I can get past them I will really advance in c++

I really like the idea of learning C++ and using Dark GDK because I don't have to learn direct X or another API to do 3D!
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 8th Apr 2013 03:21 Edited at: 8th Apr 2013 03:23
Once you do make the jump you'll surprise yourself at how different and complex C++ is, but saying that, with your knowledge of DBPro you will find it easy to use Dark GDK.

Arrays are pretty much the same, just accessed using different syntax in C.

Types in DBRo are 'struct's or 'class's and the good thing about C++ is that you can have member functions.

There are tons of tutorials spread about the web on C++ and its uses so you won't have any problems finding what you're looking for when the need rises. You will soon grasp the curly braces '{ and }' for scopes.

Dark GDK in my opinion is the best way to make the jump to C++ as it is still a 'functional' API which from the start keeps you in the same routine as you were in DBPro. It's when you make the jump from Dark GDK that things get complex because OOP coding comes into play more. Although there may be a few glitches along the way, because you are in C/C++ then there's always a way round the issue.

I hope you stick with it...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 8th Apr 2013 22:37
I do understand the basics of C++ and I think someone told me you can use classes in darkGDK. Like I said I know what classes are and really love the idea of OOP but just have not gone into them in depth yet. I am still grappling with pointers which seem to be quite confusing.

You are right there are tons of c++ tutorials out there. I bought the non graded version of what the game institute has to offer. It starts at the very beginning of c++ and goes up through direct X. 3D studio max lessons, Unity lessons and a lot more I have not really looked into yet. With DGDK I am not sure how to use or mix the DBP commands in with the C++. I tried to port a DBP project into C++ and dark GDK yesterday and got about 109 errors. I guess I will just practice a few commands and routines here and there until I learn. Can't really find any tutorials on DGDK.
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 11th Apr 2013 00:32
You're probably right about the tuts for Dark GDK but there's the examples that come with Dark GDK that will at least get you started with understanding the setting up. Once you've got the basic setup sorted, using the DBP commands with GDK is a piece of cake.

As far as I remember GDK works the best with VS Express 2008. Although somewhere around the forums there are tuts on how to set it up with 2010. I tried with 2010 and went straight back to 2008.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 11th Apr 2013 14:21
Pointers are really useful things.
Like you said they are memory addresses. In C++ they are used for lots of things, for example polymorphism (OOP concept if you haven't come to that yet) only works with pointers in C++ (or references, but in the end they're really pointers too).

One thing they are commonly used for and that might be easy to grasp in the beginning is for transfering large amounts of data between functions. If you have a large object (a class / struct instance) and send that to a function what you're really doing is copying the entire object. If it is large enough it may be hundreds if not thousands of bytes. A pointer however is always 4 (or 8 if on a 64-bit architecture) bytes in size. As such they are much more efficient to transfer.
Another thing is that if you're sending an object to a function you are in fact making a temporary copy of it in the receiving function. As such any changes made to it from there will only be made to the copy. If you instead use a pointer you can change the pointed-to data (the actual object you have outside the function) from the function.

There are lots more to say about pointers but that might give you a general idea


"Why do programmers get Halloween and Christmas mixed up?"
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 12th Apr 2013 02:15
I get the general Idea of the pointers but I am going to really study them in depth and understand. As far as 2008 or 2010 I have 2008 and have been using that. I tried 2010 once without GDK and it seemed really slow deleted it. I also have C# 2010 and have played around with that and it also seems very slow. I like 2008 the best.

There is also a newer version of DEV-C out and I think I found it in the forums here somewhere. I used it a little bit seems ok but I pretty much stick with MS C++ 2008
Dum_Bass
11
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 28th May 2013 18:16 Edited at: 30th May 2013 07:11
Post has been edited after doing some fact checking.

I have checked out some of your other posts and I can not stop myself from saying, it sounds like you are diving too deep too fast.

This is EXACTLY the mistake I am currently making. I will recommend this particular book: Programming Principles and Practice Using C++ by Bjarne Stroustrup. If you are not a mathematician (I am not) you will get lost at points. The book does do a lot to clear up ambiguity about how code does work, should work, and even when it is worthy for your needs. Go to the library and check out a copy before you make the puchase. For the novice it can be a little intense. I still, as a dim bulbed programmer, constantly dip into it and try to divine the simplest, cleanest code I can write.

Now about pointers. In the sense of simple, trivial game programming you will find that you will not need them often at all. One thing about pointers is that they offer easier access to member functions, member objects, and a way to avoid global variables (the last can be dangerous because it can lead to pointers all over your code where you may not need them instead of structuring clean simple codeblocks). A pointer ONLY provides low level access to a specific block of memory that you want to use. Pointers are an advanced tool that has many, many uses... But not for the newly iniatited programmer.

Many new programmers often confuse pointers (pointing) with references (referring). If you want to use a variable without copying it (which will take up valuable RAM in large, intense programs). What follows is almost verbatum from that book that I suggested above.

What if a value that you wish to pass from one function to another is large, such as an image (often, several million bits), a large table of values (say, thousands of integers), or a long string (say, hundreds of characters)? then, copying can be costly. We should not be obsessed by cost, but doing unnecessary work can be embarrassing because it is an indication that we didn't directly express our idea of what we wanted. For example, we could write a function to print out a vector of floating-point numbers like this:



We could use this print() for vectors of all sizes. For example:



This code works, but the first call of print( ) has to copy ten doubles (probably 80 bytes), the second call has to copy a million doubles (probably 8 megabytes). and we don't know how much the third call has to copy. The question we must ask ourselves here is: "why are we copying anything at all?" We just wanted to print the vectors, not to make copies of their elements. Obviously, there has to be a way for us to pass a variable to a function without copying it. As an analogy, if you were given the task to make a list of books in a library, the librarians wouldn't ship you a copy of the library building and all its contents; they would send you lhe address of the library, so that You could go and look all the books.

So, we need a way of giving our print() function "the address" of the vector to print() rather than the copy of the vector. Such an "address" is called a reference and is used like this:



The & means "reference" and the const is there to stop print() from modifying its argument by accident. Apart from the change to the argument declaration, all is the same as before; the only change is that instead of operating on a copy, print() now refers back to the argument through the reference. Note the phrase "refer back"; such arguments are called references because they "refer" to objects defined elsewhere. We can call this print() exactly as before:



But with much less strain on the computer, thus improving the computer's performance whenever the call to print() is made.

Pointers are convinient when you NEED to point to the area of the memory instead of just referring to it. Pointers are an area you should explore outside of any big projects and just refer otherwise... Atleast that is what I think... I am not so much smert...

I invite any veterens to audit my response and correct any misinformation.

I'm not suggesting we should eradicate all stupid people. I am just saying lets remove all of the warning lables and let the situation sort its self out.
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 29th May 2013 02:05
You are probably right too deep to fast. I am not really good with DBP but find myself going back to it to just let my brain rest and have some fun. May have to get into a little FPSC just to relax a little! LOL
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 7th Jun 2013 15:29
There definitively are places where you need pointers as opposed to just references. Of course it's probably possible to program in such a way that these situations would seldom occur.

Perhaps it's a bit more on the "advanced" side as well, but consider you're writing a GUI system.
You have a bunch of components (buttons, text labels, input boxes and what-have-you) and a manager that is to update each of the components at appropriate times (most likely once per main loop cycle).
Now the manager determines that the user is pressing the left mouse button while the cursor is above a button component. The manager is rather abstract for scalability concerns and cannot be expected to handle the response to this event itself.
One common solution is to provide the button itself with a OnClick() member function. This works; the manager can call that and let it handle the event. But you will have to make new Button classes for all your different Buttons just so they can implement the OnClick() method differently. Also you might want to forward OnClick notices to other GUI components as well. That would make the manager code rather illegible in the end if it has to discern between all the possible gui components.
So you could add the OnClick() method to your base Component class and have the subclasses inherit it. But then you would have to fill out the OnClick function for all your different GUI components, even those which don't even use it. You could of course provide a default implementation of OnClick() which does nothing in the base Component class and then only override it as necessary. This is a common approach and it works quite well.
But... what if you want more than one button to trigger the same action? Or perhaps trigger the same "overall" action but that still depends on some particular setting stored for the button itself?

Here you could make use of a function pointer; every Component subclass has an onClick function pointer. But by default it is set to null. So when the manager discovers that the user is pressing his left mouse key while the cursor is over a certain Component, it can retrieve it's corresponding onClick pointer, check if it is null, and invoke it if it is not. The callback function can then receive a pointer (or a reference, if you prefer) to the Component it was called for and as such you can use the same callback function while still discerning between what component was used to invoke it.


The important difference between pointers and references is that pointers may be uninitialized and set to null. A null pointer is never to be dereferenced because it isn't pointing at anything. The more dangerous thing about pointers that you don't have with references in the same way is that they can point at anything, including data that is illegal to manipulate. Doing so will cause your application to crash.
Obviously the possibility of having pointers that can be dynamically bound at a later time (such as the OnClick function pointer that can be individually set for all your GUI components) comes in handy. References on the other hand have to be initialized with a valid address as soon as they are created and they can not be unset (point to null). As such you could not use them for the gui system described above, unless you used a default, empty callback function to catch the calls for components that don't use the OnClick callback.
One possible problem with that would be the calling overhead that arises, perhaps not so much in the gui example, but assume you have 20 million objects that are checked in realtime. You issue a callback to an empty function for each one of these, except the 100 ones that actually have a useful callback. Doing this then takes significantly more time than just checking if a pointer is null and if so skipping the calling, which involves copying values and stack jumps.


Well... that probably turned into a long and quite unintelligible exposition.


"Why do programmers get Halloween and Christmas mixed up?"
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 8th Jun 2013 00:41
Actually I did understand that for the most part. I did not fully understand pointers, I did pretty much get the point on references. I moved on to classes and am picking up more on pointers and references as I go along. I am going through a chapter in a book from Game Institute on classes where a very few pointers and references are used in the making of a very small RPG example. I don't fully understand Polymorphism and Inheritance yet but peeked ahead at that chapter and gave it a very quick once over and really like what I saw about Inheritance.

The example I looked at was a base class of Ship and it went on to tell how you could inherit from the base ship class to create a human ship class, pirate ship class, alien ship class etc... I thought of the possibilities of this, thought it was very cool and am looking forward to learning more but I don't really want to skip around in the books to much!

Login to post a reply

Server time is: 2024-11-18 21:26:54
Your offset time is: 2024-11-18 21:26:54