Minor issue with understanding
Code:
if(mLastupdate+mSpriteBase->mAnim[mFrame].pause*mSpeed<SDL_GetTicks())
{
mFrame++;
if(mFrame>mSpriteBase->mNumframes-1) mFrame=0;
mLastupdate = SDL_GetTicks();
}long mLastupdate
CSpriteBase mSpriteBase
(struct) CSpriteFrame mAnim
int mFrame
int pause
int mSpeed
int mNumframes
The one of interest to me is mLastupdate, because other than this if statement, it is never assigned a value in it's declaration or anywhere else in the project, so how is the if statement true? I just don't get it
Where did you get this code from? Without knowing that, it's impossible to understand the context.
If you got this from, say some forum or a friend or whatever, it's possible that it's wrong, but happened to work. Maybe.
The only way I could see such a thing working is if it were ObjectiveC, which implicitly zeros memver variables for a class, but since you're dealing with SDL that's unlikely.
If you got this from, say some forum or a friend or whatever, it's possible that it's wrong, but happened to work. Maybe.
The only way I could see such a thing working is if it were ObjectiveC, which implicitly zeros memver variables for a class, but since you're dealing with SDL that's unlikely.
It's the tutorials that NickGraevlyn (did I spell that right?) converted from another's SDL tutorials so that it would work using Cocoa.
Well, I suppose it's obj-c then that's doing it. I only gave that code snippet because it's the only bit that you really need to see. mLastupdate is just a long declared in class CSprite. the code snippet is in CSprite.cpp
Edit: and it compiles fine, which is why I don't get it.
Well, I suppose it's obj-c then that's doing it. I only gave that code snippet because it's the only bit that you really need to see. mLastupdate is just a long declared in class CSprite. the code snippet is in CSprite.cpp
Edit: and it compiles fine, which is why I don't get it.
Is the var a member of a c++ or ObjC class? Because, CSprite.cpp looks pretty C++ to me.
Perhaps there's a member initializer declation, such as
Perhaps there's a member initializer declation, such as
Code:
CSprite::CSprite(...)
:MLastupdate(0), ... other vars
{
//code
}
C++ as far as I know, which is what makes this strange.
It's just
It's just
Code:
class CSprite {
//....
private:
//....
long mLastupdate;
//....
};
I didn't convert them for use with Cocoa. They are still just SDL and C++. (for those without them, they are available on my site). All I did was explain how to set up SDL and the minor changes between Windows and Mac.
The variable (I believe) defaults at 0 when initialized. The whole purpose of that just check elapsed time. If the time is less than the expected time change, it resets the expected time change and changes the image and continues on it's way. I hope that answered it. I could go further in depth, but I'm not sure it needs it.
The variable (I believe) defaults at 0 when initialized. The whole purpose of that just check elapsed time. If the time is less than the expected time change, it resets the expected time change and changes the image and continues on it's way. I hope that answered it. I could go further in depth, but I'm not sure it needs it.
ExitToShell Wrote:C++ as far as I know, which is what makes this strange.
It's just
Code:
class CSprite {
//....
private:
//....
long mLastupdate;
//....
};
That's just the class declaration. How about the implementation? I appreciate the brevity here, but you're being a bit overzealous!
ExitToShell Wrote:The one of interest to me is mLastupdate, because other than this if statement, it is never assigned a value in it's declaration or anywhere else in the project
In other words, thats 100% of all text related to mLastupdate.
Here's my problem. In C++, if a variable, be it a member variable or just a variable in a function isn't initialized, it is stupefyingly unlikely that it will simply be zero at runtime. I mean, really, really unlikely. As in, it will be whatever value is left in that space in memory from whatever used it before.
If it *is* zero at runtime, you're either really lucky, or you have a memory leak which is overwriting that space, or, you have an array of these fellas and they're being zero'd by the creater, via a memset, which is bad form in C++, but nonetheless happens when people bring over C programming habits.
I'm trying to help you here. You're not showing enough code for me to help. The only way somebody's going to be able to help is if they know that specific code, or if they have magic mind reading powers, which I don't. Good luck.
If it *is* zero at runtime, you're either really lucky, or you have a memory leak which is overwriting that space, or, you have an array of these fellas and they're being zero'd by the creater, via a memset, which is bad form in C++, but nonetheless happens when people bring over C programming habits.
I'm trying to help you here. You're not showing enough code for me to help. The only way somebody's going to be able to help is if they know that specific code, or if they have magic mind reading powers, which I don't. Good luck.
Looking at Nick's code, I see that ExitToShell is quite right. mLastupdate is only initialised in the one place he mentioned. That means that it contains garbage until that line is executed. Bad Nick! 
You do some slightly odd, un-C++ things in your code, Nick. For starters, your CSprite class has a constructor, but it contains no code. Instead, the class has an .init() method which performs the tasks that the constructor should be responsible for. However, even .init() doesn't bother to initialise mLastupdate.
My suggestion is that you (and ExitToShell) eliminate the .init() method, move its code into the constructor, and initialise every member variable. Also, where you can, use the initialiser list syntax as shown in TomorrowPlusX's second post. That way you'll have less errors due to members containing random data at unexpected times.

You do some slightly odd, un-C++ things in your code, Nick. For starters, your CSprite class has a constructor, but it contains no code. Instead, the class has an .init() method which performs the tasks that the constructor should be responsible for. However, even .init() doesn't bother to initialise mLastupdate.
My suggestion is that you (and ExitToShell) eliminate the .init() method, move its code into the constructor, and initialise every member variable. Also, where you can, use the initialiser list syntax as shown in TomorrowPlusX's second post. That way you'll have less errors due to members containing random data at unexpected times.
Thank you for your reply, NCarter. I did not have any errors, it just threw me off guard and I wanted to understand why or how a variable can start at 0 using C++, since I was taught that you always have to initialize it before any sort of statements are used with it. I believe that due to "SDL Application" templates in XCode are made in Cocoa, thereby applying the aforementioned default initialization that comes with obj-c. Other than that, I really have no idea how it works.
In my defense, I didn't write any code. I just ported a tutorial that I found online so that it was easier for beginners to change the little things (include lines and using Xcode instead of VC++) and get it to work. You can blame the guy who owns this site:http://cone3d.gamedev.net/. He's the one who actually wrote the code.
Nick Wrote:You can blame the guy who owns this site:http://cone3d.gamedev.net/. He's the one who actually wrote the code.I see what you mean.

It worries me a bit when people write tutorials but don't seem to know how to properly apply the language (or worse, the subject) they're using. It's confusing and misleading to give beginners material which is badly written or badly designed.
EDIT: missed this...
ExitToShell Wrote:I believe that due to "SDL Application" templates in XCode are made in Cocoa, thereby applying the aforementioned default initialization that comes with obj-c. Other than that, I really have no idea how it works.The presence of Cocoa code in a project does not change the way C++ works, even in Objective-C++. As usual, if you fail to initialise a variable in C++ it will contain garbage.
In my experience, "tutorials" or demos can be the worst programming imaginable, even when they come from respectable sources such as NVIDIA or ATI, Apple, etc. They flagrantly avoid good programming or design, and generally speaking only barely work, and when they do, it still often doesn't make sense *how* they work.
Sorry for my snarkiness earlier ExitToShell, I didn't realize where to find the offending code.
Sorry for my snarkiness earlier ExitToShell, I didn't realize where to find the offending code.
topic.GoingOff();
@TomorrowPlusX: especially when they come from Apple, or "and here's how to create an OpenGL context using AGL/Carbon using only >500 lines of code..."
topic.ReturningYouTo();
@TomorrowPlusX: especially when they come from Apple, or "and here's how to create an OpenGL context using AGL/Carbon using only >500 lines of code..."
topic.ReturningYouTo();
Mark Bishop
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Understanding multisampled FBOs | Coyote | 12 | 7,104 |
Nov 27, 2009 01:01 AM Last Post: arekkusu |
|
| Understanding glColorMaterial | PhysicsGuy | 3 | 4,351 |
Nov 25, 2008 06:25 PM Last Post: PhysicsGuy |
|
| glTexImage2D() & memory understanding... | sealfin | 4 | 3,219 |
Aug 7, 2004 03:15 PM Last Post: OneSadCookie |
|

