View previous topic - View next topic |
Author |
Message |
RedSlash Mage
Joined: 12 May 2005 Posts: 331
|
Posted: Tue Oct 28, 2008 8:47 pm Post subject: |
[quote] |
|
Nice DevX! Based on your analysis, I conclude the bug is right here:
Code: |
Sprite* pCur = NULL;
if(m_pFirst != NULL) pCur = m_pFirst;
else
{
m_pFirst = pAdd;
}
while(pCur->GetNext() != NULL)
|
If m_pFirst = NULL, then pCur will still be NULL as you don't assign it anything in the 'else { m_pFirst = pAdd; }' statement, so it will crash when u call pCur->GetNext().
|
|
Back to top |
|
 |
DeveloperX 202192397

Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Tue Oct 28, 2008 10:03 pm Post subject: |
[quote] |
|
yeah I came to that conclusion as well.
If Captain wants me to restructure his code for him, that is one that that he will not have to worry about anymore. _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
 |
Captain Vimes Grumble Teddy

Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Wed Oct 29, 2008 11:45 am Post subject: |
[quote] |
|
Wow. You guys know your stuff. I totally missed that. Thanks a lot. I'm going to fix that right now and see what happens.
DevX, feel free to restructure/rewrite as you feel necessary. I was planning to make the source code for all my games entirely available so that people can edit as they see fit anyway. The only thing I ask is that you tell me exactly what you do and why - not because I'm protective of the code or anything (I could care less, really) but I'd like to learn what I'm doing wrong and how to do it better. Thanks. _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
 |
Captain Vimes Grumble Teddy

Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Wed Oct 29, 2008 12:59 pm Post subject: |
[quote] |
|
Okay, I fixed the AddSprite() function. Unfortunately, that doesn't seem to be the problem, since the program is still crashing whenever I run it. Any other ideas, besides valderman's? And, on the subject of valderman's suggestion, how do I go about making sure that pEngine is actually a valid pointer? _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
 |
valderman Mage

Joined: 29 Aug 2002 Posts: 334 Location: Gothenburg, Sweden
|
Posted: Wed Oct 29, 2008 3:10 pm Post subject: |
[quote] |
|
Captain Vimes wrote: | Okay, I fixed the AddSprite() function. Unfortunately, that doesn't seem to be the problem, since the program is still crashing whenever I run it. Any other ideas, besides valderman's? And, on the subject of valderman's suggestion, how do I go about making sure that pEngine is actually a valid pointer? | Make sure you always initialise your pointers, either to the address they're supposed to point to, or to NULL if you don't have a suitable value for them yet. Then you check them for NULL-ness whenever you modify them. Also, replace your pointers with references wherever possible.
Of course, it's fully possible for pointers to go bad despite all this. If, for example, you write outside of an array's bounds it's quite possible to corrupt a pointer. Same thing if you write to a pointer that's already corrupted. Basically, be very, very careful any time you use arrays or pointers. You'll still make mistakes, because you always will where pointers are involved, but hopefully they will be easier to spot and fix. _________________ http://www.weeaboo.se
|
|
Back to top |
|
 |
DeveloperX 202192397

Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Wed Oct 29, 2008 3:17 pm Post subject: |
[quote] |
|
Captain Vimes wrote: |
DevX, feel free to restructure/rewrite as you feel necessary. I was planning to make the source code for all my games entirely available so that people can edit as they see fit anyway. The only thing I ask is that you tell me exactly what you do and why - not because I'm protective of the code or anything (I could care less, really) but I'd like to learn what I'm doing wrong and how to do it better. Thanks. |
I will start restructuring the code for you now.
I will keep a change log alongside that will explain the changes, and why I made them.
I think that this will be a very good educational experience.
I will let you know when I am finished. _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
 |
Captain Vimes Grumble Teddy

Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Wed Oct 29, 2008 5:07 pm Post subject: |
[quote] |
|
Thanks, DevX and Val. But before I go into the code and replace the pointers with references, I'd like to know why you think they're better. I mean, I've heard several people say that, but they never really explained why. _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
 |
DeveloperX 202192397

Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Wed Oct 29, 2008 5:17 pm Post subject: |
[quote] |
|
I'm still refactoring the code; but so far so good.
Should not be much longer. Maybe an hour or two.
I am actually personally against using references.
People use references because you cannot unset a reference.
Once you set the reference, its that. nothing else.
You cannot set a reference of type a to type b, or you will get compiler errors.
But don't you worry none. The new code will be quite easy to understand, use, and maintain. :) And of course, you've got me to answer any questions regarding the code.
And, I am keeping a VERY detailed log of what I do, so you will have quite the read when I'm done.
..ok, back to refactoring. :D _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
 |
valderman Mage

Joined: 29 Aug 2002 Posts: 334 Location: Gothenburg, Sweden
|
Posted: Wed Oct 29, 2008 6:03 pm Post subject: |
[quote] |
|
Captain Vimes wrote: | Thanks, DevX and Val. But before I go into the code and replace the pointers with references, I'd like to know why you think they're better. I mean, I've heard several people say that, but they never really explained why. | Basically, references are pointers with a padlock. As DevX says, once set, they work exactly like "normal" variables, and there's no way to change what it's pointing at. Basically they remove a lot of the headaches normally associated with pointers. _________________ http://www.weeaboo.se
|
|
Back to top |
|
 |
DeveloperX 202192397

Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Wed Oct 29, 2008 6:52 pm Post subject: |
[quote] |
|
Okay man. I'm finished. And, your code runs perfectly now.
No crashes, nothing. :)
I only ask for one thing in return.
Drop my name "Richard Marks" in your game credits somewhere. Either in the README or in your credits screen in game. that's the only thing I ask in return.
Here is your new working code: Download Here
There is a file called SConstruct in the archive as well; which is what I use to build the project. I know you use Dev-C++, so you don't need to bother with that file at all.
Goodluck to you; and let me know if you need anything. _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
 |
RedSlash Mage
Joined: 12 May 2005 Posts: 331
|
Posted: Wed Oct 29, 2008 8:50 pm Post subject: |
[quote] |
|
In general, references are safer to use. But there is a difference between references and pointers.. so don't go ahead and replace all your pointers just yet.
References are aliases to other objects, and unlike pointers, they do not necessarily occupy memory space (as determined by the compiler). That is why you cannot simply change the reference to another object.
References are most useful when passed as a parameter or returned (in the case of overloaded operator). When choosing whether to pass by pointer or reference, it is usually better to pass by reference because it is cleaner and you don't have to check for NULL pointer and still get the efficiency as passing by pointer. Futhermore, you can pass temporaries to const references.
Another reason why you should use references is that C++ operator overloading depends heavily on use of references. You can save yourself alot of trouble in the future if you intend to use C++ designed features.
Pointers have a different role in that they point to objects. You will still need to use pointers if you allocate objects off the heap and if you need to point to different objects including NULL. Hope that helps.
|
|
Back to top |
|
 |
Captain Vimes Grumble Teddy

Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Fri Oct 31, 2008 12:23 pm Post subject: |
[quote] |
|
Well, I'm pretty sure that I will need to point to NULL at some point, and I still don't really get why anyone would use references - I'll go look it up in my free time - so I think I'll just stick with pointers for now. Thanks though.
And DevX, thanks a lot. I didn't even know that it was possible to forward-declare a class or structure. That makes things a lot easier.
I was planning to make a SpriteManager class myself, once I had gotten the basic engine working, but yours is a lot better and simpler than anything I could have done. Thanks. And the Hammer class is a lot more readable now. I didn't know that people didn't use the old notation any more.
I only have a few questions (this is extremely easy to read; thanks again):
- In the Sprite::Draw() function, why does it pass NULL as the source rectangle?
- Why have all the SDL_Rect variables become SDL_Rect*s?
- I thought SDL couldn't handle x and y values below 0, but the Sprite constructor sets them to -1 unless another value is given. Why doesn't this cause a crash?
I've learned a lot just by reading the logs and comments you left. I've probably learned as much in the last half-hour as I did when I started using SDL :D
THANK YOU! _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
 |
DeveloperX 202192397

Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Fri Oct 31, 2008 1:12 pm Post subject: |
[quote] |
|
Captain Vimes wrote: |
And DevX, thanks a lot. I didn't even know that it was possible to forward-declare a class or structure. That makes things a lot easier.
|
Indeed, I was at that point in my life too. I didn't learn that until I took courses at GameInstitute.com
Its an excellent way to clean up your code, and ensure you are not having recursive includes.
Quote: |
I was planning to make a SpriteManager class myself, once I had gotten the basic engine working, but yours is a lot better and simpler than anything I could have done. Thanks. And the Hammer class is a lot more readable now. I didn't know that people didn't use the old notation any more.
|
Happy to help. There are some things that can be done to the spritemanager class that can optimize your game engine, such as shared resources, but thats more complex than you need to worry about for now.
I was always wondering why I was being bashed for using hungarian notation, then I found out that it was not heavily used in the real world, and that only a few programmers still used it out of bad habits.
Quote: |
I only have a few questions (this is extremely easy to read; thanks again):
- In the Sprite::Draw() function, why does it pass NULL as the source rectangle?
|
If either the source rect or destination rect are NULL, the entire surface (source or destination) is copied.
Quote: |
- Why have all the SDL_Rect variables become SDL_Rect*s?
|
Because in order to use forward class declarations, you cannot use non-pointers of the class. If I did not make them pointers, then I would be forced to include SDL.h in the sprite header.
Quote: |
- I thought SDL couldn't handle x and y values below 0, but the Sprite constructor sets them to -1 unless another value is given. Why doesn't this cause a crash?
|
The values are never used if they are -1. So there is no crash.
Quote: |
I've learned a lot just by reading the logs and comments you left. I've probably learned as much in the last half-hour as I did when I started using SDL :D
THANK YOU! |
I am glad that I was able to help you out.
If you have any other questions, or want to implement something else and do not know how to go about it, don't be afraid to ask me.
:) _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
 |
Captain Vimes Grumble Teddy

Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Fri Oct 31, 2008 2:46 pm Post subject: |
[quote] |
|
DeveloperX wrote: | If either the source rect or destination rect are NULL, the entire surface (source or destination) is copied. |
Ah, I did not know that. That's pretty cool.
DeveloperX wrote: | If you have any other questions, or want to implement something else and do not know how to go about it, don't be afraid to ask me. |
Thanks, I will. And thanks to everybody here who's helped me with this. _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
 |
DeveloperX 202192397

Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Fri Oct 31, 2008 2:56 pm Post subject: |
[quote] |
|
Captain Vimes wrote: | Well, I'm pretty sure that I will need to point to NULL at some point, and I still don't really get why anyone would use references - I'll go look it up in my free time - so I think I'll just stick with pointers for now. Thanks though.
|
I just posted an article about references on my forums; feel free to register and check it out. _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|