Tag Info

Hot answers tagged

72

The main game loop handles three major tasks: Get user input Update the game state Draw the game A simple game loop just mushes these three tasks into one while loop. This has some undesired results: Game runs at different speeds on different computers. CPU (can be needlessly) pegged at 100% usage. "Game states"/menus are missing or mixed with game ...


41

I'd recommend Glenn Fiedler's article about robust framerate independence, "Fix Your Timestep!" (Less relevant to the topic at hand, but the other articles in the series are also quite good - as is everything on his site!)


40

For example having a GameObject base class with a deep inheritance hierarchy could be good for maintenance... Actually, deep hierarchies are generally worse for maintainability than shallow ones, and the modern architectural style for game objects is trending towards shallowing, aggregation-based approaches. However I think this approach can ...


39

When you need a single instance of a class throughout your program, we call that class a service. There are several standard methods of implementing services in programs: Global variables. These are the easiest to implement, but the worst design. If you use too many global variables, you will quickly find yourself writing modules that rely on each other ...


38

"Frame rate" and "FPS" (frames per second) are usually the same thing. A "frame" is usually a single image in the series of images presented to your screen rapidly so as to give the illusion of motion in your game, and so the terms generally refer to how many of those images your game can simulate and produce within one second. FPS is often used as a crude ...


32

"Delta", "d" or "Δ", means "difference" in a mathematical context. Whenever there's a difference difference between two numbers with similar meanings, that difference may be called a "delta", or a "d". Deltas are very common in game development. For example, the difference between a character's X-coordinate one second ago and its X-coordinate now can be ...


29

No, it's not. Sleep only guarantees a minimum time to sleep for, but it may actually sleep for any arbitrary amount of time over that. Your timer resolution (set via timeBeginPeriod) is also important for it, and even if you're using something else (like QueryPerformanceCounter) for your timer, you still need timeBeginPeriod to control Sleep. So in ...


24

Having actors draw themselves is not a good design, for two main reasons: 1) it violates the single responsibility principle, as those actors presumably had another job to do before you shoved render code into them. 2) it makes extension difficult; if every actor type implements its own drawing, and you need to change the way you draw in general, you may ...


24

This is the "time delta." It's how much time has elapsed since the previous update. It's necessary to ensure that animations, physics, and so on are running at the right speed. The code is running once per frame update. However, there's no guarantee that frames are drawn at a constant speed. One frame might take 1/60th of a second and the next might ...


19

Your main problem is likely this: final int skipTicks = 1000 / ticksPerSecond; Dividing an int by an int returns an int (the value is rounded down if needed), so skipTicks evaluates to 16. 1000 / 16 is 62.5 so you get around this many ticks per second. To solve this the easiest solution would be to use a millisecondsPerTick variable (what you are ...


18

The simple answer is no, you do not want to use the glutIdleFunc callback in a game that has some sort of simulation. The reason for this is that this function divorces animation and draw code from window event handling but not asynchronously. In other words, receiving and handing window events stalls draw code (or whatever you put in this callback), this is ...


15

Short answer: no, it is not. To have a fixed frame rate you have to call a certain callback function that forces a specific frame rate. Obviously, if you don't now how long a single iteration in a loop will take you cannot set a fix sleep time. GLUT provides glutTimerFunc(), which is, if you are programming in OpenGL, the right function you need. Take a ...


14

The Application.Run call drives your Windows message pump, which is ultimately what powers all the events you can hook on the Form class (and others). To create a game loop in this ecosystem, you want listen for when the application's message pump is empty, and while it remains empty, do the typical "process input state, update game logic, render the scene" ...


13

I think that you can solve this problem simply by exerting more careful control over your possible code paths. For example, in the case where you're checking if the number of the player's lives has dropped below one, why not check only when the player loses a life, instead of every frame? void subtractPlayerLife() { // Generic life losing stuff, such ...


13

Delete the Thread.sleep() call (assuming you're on a desktop machine and have power to spare!). In general you never sleep in your game loop (except on Android, where there appears to be no other choice). The bad thing about Thread.sleep is it is unpredictable and may cause your game to give up exec time for longer than 16ms, which is the maximum time a ...


11

Fully Fixed You loose most of the benefits of a fixed timestep when you throw in a variable step once each frame. Noel Lopis has a great write up on how he implemented a fixed time step in his game Casey's Contraptions. As bonus to you he is an iphone developer, though his technique is not iphone specific. Some highlights from the article Use a time ...


11

The usual approach for collision detection is to not have either A or B detect collisions on their own. Instead, you first move all objects, then have a separate collision system look for collisions between all pairs of objects, telling every object about the things that it has collided with, and then finally render all objects. So in essence, instead of ...


11

It's easiest to think of your order in a single frame, think of it as a series of dependencies. User input depends on nothing, so it goes first. Objects being updated depend on the user input, so they go second. Physics depend on the new updated objects, so it goes third. Rendering depends on the latest physics state and object updates, so it goes fourth. ...


10

The Flixel framework uses the dead flag (actually several flags that determine whether it should be drawn, updated, and so on). I'd say that if you're going to revive entities, and if performance is an issue, you use the dead flag. In my experience, instantiating new entities is the most expensive operation in the use case you describe, and splicing out or ...


10

I wrote a FSM based off of a chapter in "Massively Multiplayer Game Development" Edited by Thor Alexander. Inside is a chapter labelled "Parallel-State Machines for Believable Characters". This is written in python, but the concepts are easily translatable into C++. I highly recommend checking this out, even though this is about character states, not game ...


10

I imagine a battle sequence as a minigame within your game. Update ticks (or turn ticks) are directed to a component handling these events. This approach encapsulates the battle sequence logic in a separate class, leaving your main game loop free to transition between game states. void gameLoop() { while(gameRunning) { if (state == EXPLORATION) ...


10

A lot of android games are not even large enough in scope to justify save/load or options/preferences, never mind custom characters and the like simply because they are played offhand for 10 minutes on the train home. Don't make the mistake I did of embarking on something with a large scope for a first android game. Make a bunch of smaller games then go ...


10

I have a damage system is my game and I would really love to stay away from float damage values, both for memory and game design reasons. My first instinct is to question your assumptions. It's extremely unlikely that using floats rather than ints for something like this is going to affect your memory usage in any noticeable way. On most systems, they ...


9

The traditional solution to this is a finite state machine, which is being suggested in several comments. I hate finite state machines. Sure, they're simple, they're supported in every language, but they're such an amazing pain to work with. Every manipulation takes a ton of bugprone copy-and-paste code, and tweaking the effect in small ways can be a huge ...


9

There is actually two ways that XNA can execute the game loop, Fixed Step and Variable Step. When you setup your game to run Fixed Step, it will consistently call the Update method based on the targeted elapsed game time. So no matter how fast your computer may be, it will always call the Update method at the same elapsed game time interval, so Update and ...


9

A lot of what I see in your diagram is UI related, and that much seems fairly reasonable. There is only a small portion of your diagram reserved for actual game logic, and it is very vague. I think you are trying too hard. I would suggest you just start writing code. Pick a particular problem and start solving it. You will learn so much that by the time you ...


8

No, it isn't a bad idea. In fact, this kind of technique -- determining which (usually rectangular) regions of the screen were "dirty" and only redrawing those -- used to be standard practice for rendering to the screen before the modern era of 3D graphics, hardware acceleration via powerful dedicated GPUs, and so on. There isn't any need to waste ...


8

A game loop is generally recommended because it is simple. Almost any game can be properly developed and ran by utilizing a loop, and most games would require one to properly function. For example, most physics engines require reliable constant updates for a proper simulation. Animations and other dynamic content and graphics need to be updated every ...


7

Programming Game AI By Example (http://www.ai-junkie.com/books/toc_pgaibe.html) has an example implementation that's pretty straightforward and just handles the basics. Transitions are handled in a single method call (first Enter(), then Execute() every update, Exit() when transitioning)> I don't know what you'd need besides that. I would implement more ...


7

Yes, its possible to do in a single thread. Generally speaking though, you'll want to be updating the objects every frame and not just when there are spare cycles. Your animations and movement will be disconnected from the frame rate and look rather choppy if you don't. If you're talking more about AI updates or something else that does not need to be ...



Only top voted, non community-wiki answers of a minimum length are eligible