🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SDL 2: Implementing Sprite Clips

Started by
13 comments, last by Aldacron 6 years, 11 months ago

When you get a screen asking you if you want to use the debugger, use it! If you built in debug mode, it'll show you precisely where the program broke and will show you the values it has for your variables at that point. It's good that you had an intuition about what sort of error you had here, but as your code grows, finding a bug in your whole program without having some detailed diagnostics is going to be almost impossible.

What you're looking for here is the 'address of' operator. Given a full object - e.g. an SDL_Rect - and a function that wants a pointer to such an object - e.g. SDL_RenderCopyEX - you need to take the address of that object, which is essentially what a pointer is.

https://www.tutorialspoint.com/cplusplus/cpp_pointer_operators.htm

With that in mind, use normal objects wherever possible and just pass pointers to them when SDL needs you to.

In some cases, SDL will give you pointers to objects, such as with IMG_LoadTexture; in those cases, just keep using it as a pointer, and remember to free it at the end using whatever the prescribed method is (SDL_DestroyTexture in this case, I think).

 

11 minutes ago, windghost91 said:

In my experience, sometimes, the compiler is able to convert SDL_Rects to pointers

The compiler will never magically change a full object into a pointer. It can sometimes change an array of full objects into a pointer which happens to point to the first of those objects, but you shouldn't rely on this behaviour until you understand exactly what it's doing.

Advertisement
23 hours ago, Kylotan said:

When you get a screen asking you if you want to use the debugger, use it! If you built in debug mode, it'll show you precisely where the program broke and will show you the values it has for your variables at that point. It's good that you had an intuition about what sort of error you had here, but as your code grows, finding a bug in your whole program without having some detailed diagnostics is going to be almost impossible.

What you're looking for here is the 'address of' operator. Given a full object - e.g. an SDL_Rect - and a function that wants a pointer to such an object - e.g. SDL_RenderCopyEX - you need to take the address of that object, which is essentially what a pointer is.

https://www.tutorialspoint.com/cplusplus/cpp_pointer_operators.htm

With that in mind, use normal objects wherever possible and just pass pointers to them when SDL needs you to.

In some cases, SDL will give you pointers to objects, such as with IMG_LoadTexture; in those cases, just keep using it as a pointer, and remember to free it at the end using whatever the prescribed method is (SDL_DestroyTexture in this case, I think).

 

The compiler will never magically change a full object into a pointer. It can sometimes change an array of full objects into a pointer which happens to point to the first of those objects, but you shouldn't rely on this behaviour until you understand exactly what it's doing.

The debugger I used was Visual Studio 2015, since that is the only option that popped up. The message was not really that clear to me. It said something like "invalid access at memory location" and then it listed the address. Or perhaps that is just me not used to using debuggers too much. I definitely am seeing the need to use one as my program is around 400 lines and only puts sprites on the screen and lets someone move them. If you know of any good debuggers for Codeblocks using MinGW that are better than Visual Studio 2015, please let me know.

I see what you mean about using the address of operator- this solved the problem and the sprite clips render perfectly now with type SDL_Rect. I have seen the ampersand used before in SDL_RenderCopyEX, but I was not sure if it was a reference or the address. I did not know that it was legal to pass a memory address to SDL_RenderCopyEX without declaring a pointer, since the documentation says that SDL_RenderCopyEX requires a pointer. I knew in this context, i.e., a function call, that an ampersand means memory address, but not knowing that you did not have to declare a pointer confused me somehow. I am glad that is all cleared up now. I honestly had no idea what I was thinking and how not knowing this crucial piece of information mixed my brain up exactly! I am glad I am learning more about SDL 2 works with C++ after getting away from the tutorials and making my own (simple) games.

Visual Studio has the best debugger on Windows. It takes time to get used to using it, but it is time worth spending. The error message is not much help, but the debugger will show the exact line of code where the program crashed, and the value of the variables when it crashed, providing it has debug data for the program. (If you built the code using MinGW/Codeblocks, then Visual Studio might not have that debug data... but if you have Visual Studio, you should probably be using that to build your code instead.)

The ampersand symbol in C++ means several things, depending on context:

  • between 2 numbers (or similar), it means the boolean AND operation.
  • when placed before an existing variable name in an expression - e.g. "int* x = &y", it is the 'address of' operator, mentioned above. The address of a variable in C++ is a pointer. The expression above assigns the address of y, which must be an int, to x, which is a pointer to int.
  • when in the declaration of a variable or argument - e.g. "int& xx = y" - this is declaring a reference. This basically means that anywhere you see 'xx', you're actually working directly with 'y'. It uses the same symbol because conceptually you might consider that xx 'points' at y, or that xx has the same memory address as y, but it's not a pointer.

So, when you pass a "memory address" to SDL_RenderCopyEX, you are passing a pointer, because that's exactly what a memory address is, in C++. Pointers are the type that represents addresses of things.

On 7/13/2017 at 7:27 PM, windghost91 said:

I see what you mean about using the address of operator- this solved the problem and the sprite clips render perfectly now with type SDL_Rect. I have seen the ampersand used before in SDL_RenderCopyEX, but I was not sure if it was a reference or the address. I did not know that it was legal to pass a memory address to SDL_RenderCopyEX without declaring a pointer, since the documentation says that SDL_RenderCopyEX requires a pointer. I knew in this context, i.e., a function call, that an ampersand means memory address, but not knowing that you did not have to declare a pointer confused me somehow. I am glad that is all cleared up now. I honestly had no idea what I was thinking and how not knowing this crucial piece of information mixed my brain up exactly! I am glad I am learning more about SDL 2 works with C++ after getting away from the tutorials and making my own (simple) games.

I strongly recommend you pick up a copy of Understanding and Using C Pointers. It covers a lot of ground, even some that intermediate C and C++ programmers might be ignorant of. If you grok the material in that book, you'll be much more able to recognize any self-inflicted pointer-related bugs (and they almost always are self-inflicted) when they pop up.

This topic is closed to new replies.

Advertisement