2D platformer and hit detection

Started by
15 comments, last by Dark_Oppressor 14 years, 8 months ago
Quote:Original post by Mantrid
also, you're sending a float, which means at one point in our program there are two floats, whereas it's "cheaper" to send a pointer to the float, since a pointer is smaller in size than a float, so you can do that

That's hardly ever true for primitives. If anything, passing a pointer to a float makes the code harder to understand and manage. But yes, for larger objects, passing a pointer (or often better, a reference) is often more efficient.


The concept behind pointers is indeed rather simple - they point to other objects - but their use can be quite tricky. For example, pointers can point to actual objects, but they can also just contain random addresses. Trying to dereference such pointers is asking for trouble.

Also, with regards to dynamic memory allocation, a pointer doesn't indicate whether it 'owns' the object it points at or not. What I mean is this: you can create a float on the stack (float a;) and make a pointer point to it (float* p = &a). That's all fine. But if you dynamically allocate a float and make a float pointer point to it (that float is now on the heap, or freestore): (float* p = new float;), then you're responsible for cleaning up that float (delete p;). In both cases you're using pointers, but in one case you need to clean up what you're pointing at, yet in the other case you definitely shouldn't call delete on it: what's on the stack will be removed automatically when it goes out of scope.


It's usually better to use higher level constructions (smart pointers, standard library containers) to reduce the risks and automate the tedious parts of memory management.
Create-ivity - a game development blog Mouseover for more information.
Advertisement
Ok, I have been working on learning some more stuff before moving on. I've learned about dynamic memory, and how to use vectors. Now, I can have dynamic arrays of classes. I would like to say that this may have been the most exciting discovery I have ever made while programming.

Anyway, today I've been working on learning how to use OpenGL with SDL. I've gotten a little pong game working with OpenGL, save one big problem. Apparently, alpha blending is really hard and complicated with OpenGL. So my ball and paddles (which are image files with some transparency around the edges to make them look round) now have black pixels in the corners of their textures.

I'm starting to wonder if using OpenGL is even worth the large amount of extra trouble. On the other hand, once I get it all figured out, I won't have to look at it again, since I'm putting all the OpenGL-related stuff in just a couple of functions (one to load image files to a SDL_surface and then a texture, and another to actually display textures). Could anyone clue me in to the actual advantages of OpenGL? I know that it allows hardware acceleration, which is why I originally started learning it. But is there anything I will want to do in my 2D game that will require hardware acceleration?

One other question: Should I keep asking my questions in here, or should I be making new threads for each topic I am asking about?
OpenGL and Direct3D allow hardware acceleration, sure, but it's more than that: these APIs are your interface to the graphics card. You will have to use one of them, or some kind of library that makes use of them, in order to do any graphics output at all (unless you use the Windows GDI, which isn't that fast or capable). It's definitely worth sticking with it; it'll expand your horizons so much more in the future.

Rotating sprites is really much easier with hardware acceleration, and alpha blending may be hard with OpenGL but not as hard as trying to do it without! As you say, once you've got the hang of it, you can write a few functions you can use in the future and then you'll be flying along.

Hope this helps.

Edit: Oh, if you start new topics for specific problems, it may help others in future who are looking for answers to the same questions.
Quote:Original post by Dark_Oppressor
Ok, I have been working on learning some more stuff before moving on. I've learned about dynamic memory, and how to use vectors. Now, I can have dynamic arrays of classes. I would like to say that this may have been the most exciting discovery I have ever made while programming.

It sure makes things easier, doesn't it? ;)

Be aware of the subtle details though. For example, if you have a vector of objects (not classes - a class is a blueprint, an instance or object is an actual object created according to a given blueprint), and you start using pointers to those objects, things can go wrong once you modify that vector. It may resize itself after adding a new instance, invalidating any pointers to it's content. Another thing to keep in mind is that whenever you add an object to a vector, a copy is made (using the copy constructor) and stored in the vector, so you want to make sure your objects don't do weird things when they're being copied (which easily happens when they dynamically allocate resources).

Yay, C++. :)

Quote:I'm starting to wonder if using OpenGL is even worth the large amount of extra trouble.

Personally I wouldn't put in the effort, because there are several alternatives. Take a look at Haaf's Game Engine, for example. OpenGL and DirectX, and SDL to some extend, are still rather low-level.
Create-ivity - a game development blog Mouseover for more information.
Yes, I must say that I am regretting the entire day that I wasted trying to get OpenGL to work. After many hours of endless work, my 2 or 3 OpenGL related blocks of code continue to leak memory, even though I have carefully gone over the code a million times, making sure everything is cleaned up properly. I have absolutely no idea what could possibly be causing it to STILL leak. It certainly worked great before I attempted to use OpenGL.

EDIT: I have completely removed OpenGL from the code, and it is still leaking... I think. What exactly constitutes a memory leak? That simply means that the program continues to eat up more and more memory the longer it runs, right? Mine is definitely doing that. As I type here, I am removing almost all of the code, and it is still leaking. I am now down to a blank screen that doesn't calculate anything, and it is STILL leaking.

[Edited by - Dark_Oppressor on August 20, 2009 7:59:19 PM]
Quote:Original post by Dark_Oppressor
EDIT: I have completely removed OpenGL from the code, and it is still leaking... I think. What exactly constitutes a memory leak? That simply means that the program continues to eat up more and more memory the longer it runs, right? Mine is definitely doing that. As I type here, I am removing almost all of the code, and it is still leaking. I am now down to a blank screen that doesn't calculate anything, and it is STILL leaking.

A memory leak is lost memory you can't account for. It could be possible that your game is eating up more and more memory because you continue to load more and more game data, which isn't necessarily a leak but rather just high memory usage. But if you're missing a few dozen megabytes and none of the memory you think you currently have allocated accounts for it, then it's very likely there's a leak somewhere. There are plenty of tools out there that can help you find leaks, such as this one.
I actually looked at that tool, among others. I have Windows, though. Anyway, I figured it out on my own by the process of elimination and some luck. It was only happening in the drawing parts of my code, but I looked back and a previous game I made with just SDL had the same problem.

Turns out, it apparently has something to do with the stringstream I am using to display int's and such as text. So, I guess I'm looking for a better way (or the reason my stringstream usage is causing a problem). But, I blamed OpenGL unfairly, ha ha. My own lack of knowledge of how stringstreams work, I guess. I'm going to be reading up on how to use them properly now, but if anyone has a better solution for getting an int/float etc. into a string, please let me know.

Also, if anyone could recommend a good Windows-compatible memory leak detector (or whatever you want to call it), that would be swell.

EDIT: Alright, I figured out how to stop the leak :-)
I was displaying a string, "FPS: " and then an int, frame_rate both at once using a stringstream to make one string out of them. However, I was reusing the same stringstream over and over, and to do that you need to properly clear it out each time you reuse it. To do THAT, you must:
my_ss.clear();my_ss.str("");ss<<"FPS: ";ss<<frame_rate;msg=ss.str();

And after that, I simply passed msg along to my "write strings of text to the screen" function. The above code makes it work without any problems, and no memory is leaking.

I now have a new problem, but it is unrelated to stringstreams. My message writing function makes a new surface with the text I want, then makes a texture(I'm using OpenGL, remember) out of that. This is then rendered to the screen like any other texture using a "render textures to the screen" function. This uses way too much CPU power and drastically reduces the framerate(and the ms per frame, for you picky people). I am guessing the solution is to use a bitmap font instead of SDL_ttf, bypassing the whole surface/make a new texture each time something is written to the screen portion. This way, I could simply load the entire alphabet and such into memory when the game first starts, and never have to do that again.

So I guess I'm off to learn how to do bitmap fonts.

[Edited by - Dark_Oppressor on August 21, 2009 3:51:30 AM]

This topic is closed to new replies.

Advertisement