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: 25th Feb 2010 13:54 Edited at: 25th Feb 2010 14:01
So, I updated my Image class and right now its fully funcional. Here is the code ( I added a way to move and show it on screen ):



So right now I'm trying to get the Sprite class to work.

Isocadia

Edit: Okay, so the image class is working. But the sprite class still isn't and gives me the same errors as before. Here's my code:

Main.cpp:



And the sprite class:



Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Feb 2010 15:34
you should declare the sprite inside the loop, because it gets deleted after dbSync(); or make update function that re-draws the sprite

also, at the previous question, i dont really get what you mean..

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 15:38
Huh, I thought that only images got deleted after dbSync (I'm pretty sure that sprites are still rendered even after a dbSync. But the sprite class doesn't work, it gives me these errors:



So I would like some help with that, because I don't understand what's wrong.

Isocadia

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Feb 2010 15:44 Edited at: 25th Feb 2010 15:49
all of them are because Image class is not defined before Sprite class

your isoCommands.h should look like


main.cpp


Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Feb 2010 15:55 Edited at: 25th Feb 2010 15:59
Yeh, put your Image class above your Sprite class, if they are in seperate files dont forget to #include "Image.h" in your Sprite .h file.


@Hassan, you would not really want all your class functions in your main.cpp, he should maybe have a isoCommands.cpp file for all his function implementations.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 16:18
Lol, that fixed all the errors I had. Exept that now I have a new one:



Is this because I include a reference to another class in my constructor?

Isocadia

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Feb 2010 16:30 Edited at: 25th Feb 2010 16:31
make a default constructor ( constructor without arguments )

@matty : well yeah, would be much more organized if you make a new file ( isoCommands.cpp )

Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Feb 2010 16:40 Edited at: 25th Feb 2010 16:41
You are still missing one important concept, your Sprite class has an Image variable, this needs to be set to the Image you pass into Sprite when you create your Sprite object.




entomophobiac
21
Years of Service
User Offline
Joined: 1st Nov 2002
Location: United States
Posted: 25th Feb 2010 16:54
Hassan: structs default all members to public, whereas classes default members to private. That's really the difference between them in C++. It's a hint towards the type of practices that are recommended in OOP programming, really. You should do as little stuff as possible as public.
Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 17:15
Well, everything loads fine and I don't get any errors, exept that my background has become completely white instead of colored ( and I checked with dbSpriteExist (), the sprite is loaded ). So what could be causing this wierd behavior?

Isocadia

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 25th Feb 2010 17:23
Quote: "Hassan: structs default all members to public, whereas classes default members to private. That's really the difference between them in C++. It's a hint towards the type of practices that are recommended in OOP programming, really. You should do as little stuff as possible as public."


knew that, but seriously, that doesnt count as a "difference", it will even contain same code line number ( usually ):


Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Feb 2010 18:13
Quote: "Well, everything loads fine and I don't get any errors, exept that my background has become completely white instead of colored ( and I checked with dbSpriteExist (), the sprite is loaded ). So what could be causing this wierd behavior?"


Could we see your code?

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 25th Feb 2010 19:03
ofc:

main.cpp:

isocommads.h

sprite.cpp

image.cpp


Isocadia

Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Feb 2010 21:02 Edited at: 25th Feb 2010 21:04
Its because you are deleting the image in the Image destructor although this might not be a bad thing.

Its important to know that when you call any function, the arguments are created again and used inside the function, then they go out of scope.
So:


Here, a new Image is being created called ImageInstance, then when the function ends it goes out of scope, calling the destructor and as it shares the ID of the Image object passed in to the function it is deleting that image:



So, you might want to take this out of the destructor and create a deleteImage function, the alternative may be to use references, its quite straight forward and what it does is it stops another instance of Image being created, this will also speed up your program.

References:
Add a '&' sign to the Image argument in your new_sprite function like this:



Dont forget to change the function declaration as well:


This just tells the program to use the same Image object inside that function and not create another temporary one. I know its alot to take in but it will be worth it in the end.

entomophobiac
21
Years of Service
User Offline
Joined: 1st Nov 2002
Location: United States
Posted: 25th Feb 2010 21:08
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 26th Feb 2010 17:44
Lol, good video.

I never wanted to go into pointers as the syntax is a bit more difficult and he basically just wanted to know how classes work.

Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 27th Feb 2010 01:23 Edited at: 27th Feb 2010 04:00
Would there be any difference if I made it a pointer to image like this:



I know it will get the same result but is one way better than the other, or is it really just up to the coder?

I'm asking because in my project I have been doing it the other way this whole time and I just want to make sure that I am not getting into bad habits or something.

Login to post a reply

Server time is: 2024-10-05 18:40:26
Your offset time is: 2024-10-05 18:40:26