Helo there, again! I’m a little rusty after 3 years of hiatus, and I’ve been developing a 2d strategy game on the last week.
Designed the levels and player movement, it worked all well and good, until I decided it’s time to add enemies. I’ve added a variable that gets incremented by one each time the main loop runs. This variable serves a role of deciding if any additional enemy needs to be spawned. If its modulo 200 equals 0, then it’s done so. The main issue lies here:
When I start the game, move the Player, the loop gets stuck. Every movement it does, the counter is only larger with 1. BUT: when I move the cursor outside the window, it starts growing like crazy, and the enemies are added in a satisfactory way. AND the game hangs. doesn’t react to movement keys.
I would need a counter constantly growing “without respect to” the cursor position, or if any keys (to move the player) been pressed.
Basically, if i’m able to add an “Time elapsed: “ counter, it’s good, but it’s growing in the same weird manner. I.e. the time elapsed gets added a second only when I’m moving the player.
When I click outside the window, the counter is working normally, by the second.
Show us your main loop or describe exactly how it works, because the result you’ve achieved tells us your main loop is completely wrong. I don’t understand how it’s possible for the main loop tickrate to be dependent on character movement or cursor position.
From your description, it appears that the main loop is broken (algorithmically incorrect), so it needs to be fixed, but to do that, we need to know/see what it looks like — without the code, we can only guess.
A correctly implemented main loop works independently of external factors such as window status or input events. This means that it should continue to run even when the window loses focus or is minimized/hidden.
The only thing that can stop the loop from running are system modal loops, which stops executing the main game thread instructions while you move/resize the window with the mouse.
The simplest solution is to use SDL main callbacks, because they were implemented so that you don’t have to manually implement the main loop.
By default, these callbacks ensure that 60fps is maintained, but you can use the SDL_HINT_MAIN_CALLBACK_RATE hint to change the rate at which the update and render callbacks are called. They are easy to use, so you shouldn’t have any problems with them.
Oh, indeed, you are right — sorry for my nonsense.
But this still doesn’t change the fact that using main callbacks would be a reasonable solution for the OP, since he has such big problems implementing the main loop on his own. This is especially true if the solution already existing in SDL is sufficient for his project.
This is not an useful response, also speak for yourself only.
You guys will laugh at me… The issue was that I didn’t close the while SDL_PollEvent loop with a bracket xdd . and it was solved with the bracked added. The bad issue is that i have used maladaptive Break-s and it breaks the game now. As a lot of time the loop was needed to be exited…now i know why
So it seems my original guess was right after all… You forgot the end bracket which meant everything after became part of the loop (the counter increment and everything else). This explains why the update pace was affected by how many events your program was receiving (moving the mouse will generate lots of mouse motion events, especially for high-DPI mice). I assume you had an extra end bracket somewhere at the end of the function because otherwise the code wouldn’t have compiled.
I’m not laughing by the way. I’m glad you found the problem.
This is a useful answer because I stated a fact — you have a problem implementing your own main loop, so using an existing mechanism in SDL is one solution.
I don’t think anyone intends that. We’re here to help solve problems, not to make fun of those who have them. But you didn’t make it easy for us because you didn’t show us your loop code, so we’re left with just guessing, and that’s not an efficient way to solve problems (it’s a waste of time in most cases).
For the future — less attitude, more useful information (source code first).