Sdl2 load png4/30/2024 Simple: when you have a variable of type int and you try to put something in it with a type of double the fractional part gets truncated and only the whole number is placed into the value. Well, why isn't the stick figure moving about 0.01667 pixels per frame then? When we calculate the expression m_image_position.x + (1 * 0.01667) we get 0.01667. Remember, I capped the frame rate to 60 fps by passing in 1.0/60.0 or approximately 0.01667 to update(). So, (1 * delta_time) is converted to (1.0 * delta_time).Īfter the subexpression is computed, the expression m_image_position.x + result_of_subexpression is calculated.īecause m_image_position.x is an int and result_of_subexpression is a double for reasons we saw earlier, the value in m_image_position.x will be promoted to a double. For example, if one operand is a double then other will be converted to a double. C++ only uses the same types when using an operator so of one the operands will have to give. Why? It has to do something with how C++ work with different types with the same operator. ![]() If we manually calculate how much the stick figure will move per frame and place the result in m_image_position.x we get the value of zero. The method update() will handle any events and draw to the back buffer.ĭraw() will simply call SDL_UpdateWindowSurface() to show the back buffer.Įnter fullscreen mode Exit fullscreen mode I'll be creating a class called Application and, for now, will have two methods: You can use functions, however, since I'm using C++ I'll be using classes. We can't put everything in the main() function so we'll have to break up the code into different sections. Now with that over with, let's do some clean up first. And besides, we weren't technically leaking memory since it was claimed back by the OS after our window closed.Īpology accepted. We didn't free the resources because the programs were short-lived and it would have cluttered the code. Gasp! What?! And you've never told this to us? How could you! What I'm trying to get at is we've been leaking memory all this time. ![]() ![]() Everything is manually done including resource management. Typically in C, when a function returns a pointer, it usually means there was something dynamically allocated and the pointer to it was returned back to you. This means it heavily relies on pointers and passing pointers around. Before we start this next part, we need to have a little talk about resource management in SDL2.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |