Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

EDIT: I just saw your tag, which is C and not C++ in which case you will not be able to use vector containers. That is not a show stopper. See the answers to this questionanswers to this question for libraries that simplify management of arrays (among other things).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

EDIT: I just saw your tag, which is C and not C++ in which case you will not be able to use vector containers. That is not a show stopper. See the answers to this question for libraries that simplify management of arrays (among other things).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

EDIT: I just saw your tag, which is C and not C++ in which case you will not be able to use vector containers. That is not a show stopper. See the answers to this question for libraries that simplify management of arrays (among other things).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.

added 291 characters in body
Source Link
Samaursa
  • 2.2k
  • 19
  • 25

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

EDIT: I just saw your tag, which is C and not C++ in which case you will not be able to use vector containers. That is not a show stopper. See the answers to this question for libraries that simplify management of arrays (among other things).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

EDIT: I just saw your tag, which is C and not C++ in which case you will not be able to use vector containers. That is not a show stopper. See the answers to this question for libraries that simplify management of arrays (among other things).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.

Source Link
Samaursa
  • 2.2k
  • 19
  • 25

In general, you want to avoid freeing objects in your game. Generally, games are designed in such a way that when you load a level, the maximum number of objects are created while loading and only destroyed when exiting the level (sometimes, even that is not done so that the next level, which uses the same player/enemy classes loads even faster without an increase in the amount of memory used). Most games have pre-determined limits which the programmers can then work with (e.g. decal limits, maximum enemies, maximum bullet shells, maximum particle count) and sometimes this can be adjusted by the player.

Of course, this done not work for bigger games, such as Mass Effect, in which case complex memory pools are setup specifically for the purpose of avoiding memory fragmentation, lowering load times or eliminating them altogether (e.g. God of War).

I would recommend to start off by marking your projectiles as active or inactive instead of destroying them. The simplest memory pool will be a vector array where you reserve, let's say, 3000 projectiles[*]. Each projectile has a variable which lets you know when to update (alternatively, you can have your vector store std::pair<bool, projectile> where the first variable of the pair will let you know if an update is required).

In your update loop, simply iterate over the projectiles and update only the ones that require updating. You may think this is slow, but it is very very fast. I myself initially thought this sort of thing would be very slow and though a list would be better. Problem with list is that you lose cache coherency and you end up taking a hit by all the cache misses (no idea what I am talking about? Don't worry! :).

[*] Use resize() to create the projectiles as well. If you are using pointers to projectiles, then reserve() and push_back 3000 new projectiles. If your projectiles are big objects, I would store pointers rather in the vector.