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 / Beginning with classes.

Author
Message
Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 09:15
Lately I have been reading a post about managing code and somebody said ( can't remember who it was ) that you should break up your code into re-usable classes. So I thought, why not begin with a sprite manager class. So I started with that but I still have alot of questions. So the basic outline is this:



I thought about tracking the sprite and I came up with the idea to use a multidimensional array to keep data: like first row=id, second path, and then keep track of position,ghosted etc in the other rows. But there must be a better way that this to do that.

Second about commands used in classes, what are virtual voids? Because they don't mention them in cplusplus tutorials.

This was it for now, and if you think something needs to be changed or added ( like functions, which I probably missed ) then please say so.

Isocadia

MayoZebraHat 1979
16
Years of Service
User Offline
Joined: 21st Dec 2007
Location:
Posted: 24th Feb 2010 09:37
I hope I understand this right and I'm helping... If you wish to keep track of a list of visited positions in a path, I would use a stack method. I just use a single dimentional array: (x = i % MAX_WIDTH, y = i / MAX_HEIGHT, where i is the index of the array) for my screen positions and store the index number of the position in my stack. This way you can pop your way backwards through the stack to trace the path. You could make your stack a vector type and use a 2d array but I like to do it in a 1d.

The same idea is used to generate a depth-first maze.

As for virtual functions, this would help better than I can explain.
http://en.wikipedia.org/wiki/Virtual_function
Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 10:01 Edited at: 24th Feb 2010 10:04
I don't fully udnerstand what you are saying there, you mean that yuor code keeps track of previous positions of a sprite? Or does it do something like this:



So that next time I create a sprite, it looks for the next available ID ( which is 3 because apparently it has been deleted ). Next it saves the path from where it loaded ( to be able to see when you create a sprite if it has already been loaded and just needs an ID change ) next the position and ghosted to be able to change that with update_sprite ( 2, 500, 250 ) or something like that.

I don't know if your code does this ( from what I understand it keeps track of previous position ). But if it does could you post a bit of code on how I would do the thing I like?

Isocadia

MayoZebraHat 1979
16
Years of Service
User Offline
Joined: 21st Dec 2007
Location:
Posted: 24th Feb 2010 10:15 Edited at: 24th Feb 2010 10:16
Heh, I read too much into it. I think what you want is a "static member" to count the number of used objects.



this makes a variable that all instances of a bob share. that counter could be your next usable free sprite. Using dbSpriteExist and ++ you can get a free arbitrary number. If you wanted to keep the numbers low, just have a loop that searches from 1 for a !dbSpriteExist() and then return the number that is free. I think that's closer to what you needed.

BTW, cplusplus.com should have a good exp. in the Classes part II documentation.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 10:35 Edited at: 24th Feb 2010 10:35
i've always been wondering, why every use / recommend using classes, not structures?

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 10:36 Edited at: 24th Feb 2010 10:38
Quote: "I thought about tracking the sprite and I came up with the idea to use a multidimensional array to keep data: like first row=id, second path, and then keep track of position,ghosted etc in the other rows. But there must be a better way that this to do that."


It is alot simpler than this, your class holds any data you like for you, you don't need arrays etc..



You then access your sprite data like this:



Its at this point you may want to use arrays to keep all
your sprites, or create another class to do bulk
operations on your sprites(manager class), but as you
are just learning I would not worry about that yet.

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 10:38
@Hassan - As far as I know, you cannot put your object functions in a structure.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 10:47
Well, that could be the next sprite ( but what if I have 6 sprite and 3 gets deleted, the counter goes to 5 but 5 is still used, or does this work different? ) And what would you suggest to keep track of the paths, because if I did it like this, that means that I need a static ID, then search with that ID in the other statics ( like ghosted ). Because the idea that I had was something like this ( better explained I hope: )

So the array I tough of works like this:

first you look in the 2e kolom ( the one that goes from up to down ) for this path: //data//lol.png

This returns kolum 2 row 3. Since everything of the same sprite is in the same row, you can use the row the path returned to look for all the other data because you know that kolum 3 contains if it is ghosted or not.

This was the way I thought would work best. But I just don't have a clue how to code it and right now I'm beginning to doubt that this was the best way. because like said before, the if the counter is at 6 and 3 is deleted, and you decrease the counter by 1, the counter says 5 but 3 is free, so this doesn't work right?

Srry if what I say makes no sence xD.

Isocadia

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 10:52
lol, hassan and matty posted while I was writing. But matty, how would I know which sprite ID is in use. Because with your code you do not keep track of which sprite ID is in use or not. Ah, maybe I thought to far ahead for my first time with classes. Do you think that I'm asking to much of myself with trying to write something like this?

Isocadia

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 10:53
Its important to realise that static data is shared between every object created in that class, each sprite has its own version of the rest of the data.

Plus your 'counter' variable would not be related to 'id' numbers, it would just give you instant value on the amount of sprites you have.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 10:56 Edited at: 24th Feb 2010 10:56


^
you can put functions & constructors/destructors in structures

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 10:59
Quote: "ol, hassan and matty posted while I was writing. But matty, how would I know which sprite ID is in use. Because with your code you do not keep track of which sprite ID is in use or not. Ah, maybe I thought to far ahead for my first time with classes. Do you think that I'm asking to much of myself with trying to write something like this?"


If you want to give the sprite an ID without worrying what it is, put a function in your class called setID(), this function would do what mayo said above, check through every id until it finds one that is available using dbObjectExist() then when it has one set the id class variable to this value.
You could actually do this in the constructor but I don't want to confuse you.

You are not taking on too much, I think you just need to set up a basic Sprite class and have a mess with it.

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 11:01
Quote: "you can put functions & constructors/destructors in structures"


Oh o.k, I have never used them, maybe you cannot inherit from structures?

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 11:03 Edited at: 24th Feb 2010 11:05
So the counter is used to track how many sprites I have, but I still don't understand how to handle the ID's. Each time the class initialize you give it its own ID, Path and other basic data. To know which ID is the closest available you need to see which ID does not exist, and then I break because I don't know how to store the data of which ID are in use. Since each class contains its own ID it could be possible to search through every instance and then look for an ID where there is no instance and then use that ID. Is this the way to do it?

Isocadia

Edit: Damn, its hard to respond correctly when everyone is responding so quick, but I understand what you are saying and I'm gonna give it a try.

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 11:09
With this system you don't need to know which id's are in use as you have programmed the class to take care of this for you.

When you need the id you just use:


Or a better example:


The sprite id could be 5 or 189, it does not matter as you don't need to know.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 11:12
Quote: "Oh o.k, I have never used them, maybe you cannot inherit from structures?"


i think you can, never tested tho, but i remember that i read somewhere that you can

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 11:26
Ok, finally I understand. Well, need to go now ( frikking ID needs to be pikked up at.. uh like the building that runs the city. ) So I will just start writing some code and come back here if I need some more help.

Isocadia

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 13:56
Ok, so I just started and already found a problem, you said that I should find an unused sprite ID woth dbObjectExits (), but this is what the documentation says about it:

This command will return a one if the specified 3D object exists, otherwise zero will be returned.

How is that gonna help me find my ID's? Or does it also look for int's?

Isocadia

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 24th Feb 2010 14:05
Check this:



This will return a free sprite ID. You could make this function part of your sprite class. The class would have a variable integer called ID, that stores a free ID. Make sure you immediatly create the sprite after you have the free ID, or it might be used by another sprite!
Hope this helps.

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 14:16
How would I see if a certain image is loaded ( like how do I see if "Data\\media\\title.png" is loaded )?

Isocadia

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 24th Feb 2010 15:12
Well, you could check for an image ID just like with sprites,
but for image names? not quite sure...
I could think of saving file names of loaded images to an array, and check if already used when loading an image.

Example:



This should load an image and return its ID. If the given file is already loaded, it will use the loaded image and return its ID.
I just made that up in my head so I hope it actually works

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 15:24 Edited at: 24th Feb 2010 15:46
if you want to check if an image exist by passing the name of the image, not the ID, you should make a vector of a structure ( or a class ) which in includes the image data ( ID and name, maybe other stuff if you need ), and you should load images with another method, not dbLoadImage, a method that can define the image in the vector ( to make it simple )

so like:



something like that
[edit]edited the code..

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 15:29
Well, the point is, I want to check if a sprite is loaded, because if it is I do not need to reload it. But what I just realized is that I that when I like load title.png, I instance a class with objectname title.png. Is it possible to check if like the instance title.png is running, if it is then it is loaded, and if not it is not.

Isocadia

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 16:06
There is a simple object orientated way to do this but I may be introducing a concept you are not familiar with. You will create an Image class and then your Sprite class will hold an instance of the Image class.

Image class:


Create image before any sprites are created:


You would need to ammend your Sprite class so it takes an Image on creation.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 16:17
I understand what you are doing, but how does this help me keeping track of which sprite is loaded? Because what I have right now in my sprite creation is first get new ID, load image with that ID, create sprite and delete image. Isn't that more easy then what you are doing.

Isocadia

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 16:33
Welcome to oop programming, there are always many ways of doing things and its up to you which way you want to do it but imo, my way is neater, more object orientated and I think it would benefit you down the line

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 16:49
Well, I see how you way is much neater, but how does it help me keep track of what is loaded. Or isn't it doing that at all and do I need to find another way?

Isocadia

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 17:10
Basically, you only create one instance of 'Image' for every image you want in your game, it loads once and thats it.
If you dont have an image class then it gets more complicated and messy.

DGDK have images and sprites seperate as this is the simpler way to work with images and sprites, I think rolling it all into one 'Sprite' class might be bad design. But don't let me put you off, if you want to do it that way then go ahead, its whatever way you are more comfortable with.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 17:13 Edited at: 24th Feb 2010 17:45
Ah, that's way better then what I had in mind. But doesn't loading all the images take up much memory? So to implement your way, I need a image class that loads them all, and a sprite class that converts them into sprites whenever needed. I think I get it. Thanks alot everybody!

Isocadia

Edit: Alright, I think I got the outline of the image class. I only get one error and that is when I use char *path in dbLoadImage, probably doing something completely wrong. But here's the code:



Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 18:10 Edited at: 24th Feb 2010 18:11
make sure you delete the image ( or the sprite ) in a destructor:


also,

first, you passed char *path, which should be path, and you defined path twice, in the argument list and in the private variables, so better change one of them ( the name i mean ), and also, you need to set the private member "path" to the path specified, so you can use it ( at the moment, its useless )

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 18:25
You are also using dbSpriteExist() instead of dbImageExist().

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 18:53 Edited at: 24th Feb 2010 19:02
So here's the image class, I fixed all the things said above, its just that right now, I don't know why I should save the filepath because I don't use it... ah well, here it is:



So, I'm finishing the Sprite manager and then I will put them to the test xD.

Isocadia

Edit: SpriteManager:



Just 1 question: Do I need to delete all my int's and chars in the destructor and how do I declare a class and all its fellow functions in a header file?

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 19:01

Error!
try strcpy ();

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 19:03
Well, I'm probably gonna remove that, but why is it error? It doesn't give an error here ( when compiling ).

Isocadia

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 19:38 Edited at: 24th Feb 2010 19:42
Your Sprite class might want to contain an image, like in the code below, but thats a design issue and its whats better for you.
Also there is no need for OpenID to be a class variable as its pretty redundant outside the get_Open_ID() function.

You would keep the string for the filename as it may be needed in the future.



Edit: where it says Image it should be ImageManager although this choice of name for your classes may not be the best as you very well one day make another class to handle all your sprites/images in bulk, this would be an image/sprite manager.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 24th Feb 2010 20:05 Edited at: 24th Feb 2010 20:07
Huh, I don't fully understand how you get this Image anImage. You say it needs to look in the Image class, but what do you mean with anImage, and image ID or the name of the instance of the image? And to get back to my previous question, do I need to delete my variables in the destructor and how do I define a class in a header?

Isocadia

Edit: Because I only need the ID, shouldn't it be something like Image::ID??

JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 24th Feb 2010 20:07
Functionally, classes and structs are identical. The only difference between them are their default scopes...



Without out a specific scope identifier, classes default to private and structs default to public. That's the only difference beteen the two...

JTK

A little late, I know but had to put my $.02 in... Lol
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Feb 2010 21:48
When you create a new class, 'Image' for example, 'Image' is now a type like 'int' or 'float' and you can include it in other classes as a class variable, the same as you do with 'int' and 'float'.

It makes perfect sense for each 'Sprite' instance to contain an instance of 'Image' as all sprites have an image.
This can be a confusing aspect of oop at first but it is a powerful tool and I would recommend you try it the way I suggest, if nothing else but to understand what is possible with oop.


You probably should delete everything in the destructor.
If you right click in your solution explorer, go to 'add' then 'class', this will setup a class for you with a .h and a .cpp file. You then just include the .h one in any project you want to use your class in.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 24th Feb 2010 21:55
also, you might want to rename the class, so it makes more sense, like, lets say you will make a sprite, which one makes more sense?


obviously the second one makes more sense, because you are making a new "sprite" not a new "sprite manager", this is not important, but it's better, at least it makes your code more readable/understandable

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 25th Feb 2010 00:23 Edited at: 25th Feb 2010 00:24
I have a question about classes, too:
(sorry for asking here, but I think it fits)

Is the destructor of a class called automatically when the loop, where an instance was created, ends?

E.g.:



Is the destructor of Instance called at the end of each iteration of the loop?

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Feb 2010 00:43 Edited at: 25th Feb 2010 00:43
@Pauli - I would think so, but I dont really know, you could do a simple test, put a call to print something to screen in the destructor.

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 25th Feb 2010 00:55
Yes, the destructor is called whenever an instance of a class goes out of scope.

Success = Determination * Mood;
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 25th Feb 2010 01:29
Thanks for the info!

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 09:24
@Matty: So to get the image ID ( which is needed to create the sprite ) I would need to do something like this: Image AnImage:ID to get the variable ID out of that instance? So my new_sprite would be this:



and the Image:




Oh boy, this gives me so many errors, this is definatly not right... Could you guys tell me how to refer to an int that is in a different class?

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Feb 2010 09:40
you are defining variables twice again, keep the name of the class variables different than the argument list variables
also


AnImage:ID = wrong, it should be AnImage.ID, :: is used when you define the function, . is used to acsess the function/variable/whatever ( or call them )

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 11:18
Okay, so I changed all the class variables to be different from the argument list ( might have mised one or two ). But I get some errors that appears to be saying that it doesnt see Image as a type ( this line gives these errors: )



I honestly don't know what any of these mean, or well some I do but I don't know why its wants like this ";" in Image ItsImage. I'm porbably just missing something huge xD. But still, thanks alot for your help.

Isocadia

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Feb 2010 11:33
Make sure you define your image class before you sprite class.

You might want to forget your Sprite class and get your Image class working first, comment out your Sprite class for now, define your Image class then try making an Image object again in your main program.



Then try:


See if you can get this to work, then move onto your Sprite class.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 12:32
Oops, forgot to define the classes, their not even include in the main program xD.

Isocadia

PS: There's one thing I still don't fully understand, AnImage is the name of the instance of Image, but for every sprite I need a different instance of Image. So how do I make Image "Name of Image" definable?

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Feb 2010 12:48
you can either make a new image for every sprite or add the following to your image class:


i hope i understood your question

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 13:23
@Hassan, thanks for your help but that wasn't exactly what I ment. Matty said some post earlier that I should load every image and then make sprite out of them when needed ( if I'm correct ). So I got an Image class that loads the image. And a class that makes the sprite. But for the sprite class to know which image ID it needs I have this ( with help of matty ):



That gets the ID out of AnImage, but what if I named the Image instance Title ( because it loads the image of the title ). Then Image AnImage.ID doesn't work because it should be Image Title.ID. So how do I make that AnImage definable through an argument in the constructor?

Isocadia

Login to post a reply

Server time is: 2024-11-19 17:44:46
Your offset time is: 2024-11-19 17:44:46