Sorry for the bump, but I have another (different) memory problem, but I did not want to make yet another thread for such a similar problem.
Basically every entity I make uses about 12MB RAM, which is WAY too high, especially if I need to spawn about 100 enemies (which hits 1.2GB RAM usage in total..)
I need help fixing this because it also takes about 5 seconds for my game to launch because of the high RAM usage.
Here is the code which loads the image file and puts it inside the entitylist from inside CApp_OnInit.cpp:
- Code: Select all
char character[20] = "characters.png";
if(Orc.OnLoad(character, 32, 36, 3) == false) {
return false;
}
CEntity::EntityList.push_back(&Orc);
Here is the code where it renders the images from the entitylist from inside CApp_OnRender.cpp:
- Code: Select all
for(int i = 0;i < (int)CEntity::EntityList.size();i++) { //To spawn the entities.
if(!CEntity::EntityList[i]) continue;
CEntity::EntityList[i]->OnRender(Surf_Display);
}
And here is the FULL code of my COrc.cpp file:
- Code: Select all
#include "COrc.h"
#include "CApp.h"
COrc::COrc() {
X = 0;
Y = 0;
Width = 0;
Height = 0;
MoveLeft = false;
MoveRight = false;
MoveUp = false;
MoveDown = false;
CurrentFrameCol = 3;
CurrentFrameRow = 0;
MaxSpeedX = 10;
MaxSpeedY = 10;
Health = 2;
Interval = 0;
AttackDelay = 2;
Type = ENTITY_TYPE_ENEMY;
}
bool COrc::OnLoad(char* File, int Width, int Height, int MaxFrames) {
if(CEntity::OnLoad(File, Width, Height, MaxFrames) == false) {
return false;
}
return true;
}
void COrc::OnLoop() {
CEntity::OnLoop();
if((((X + 200) > app->AccuierePlayer().X) && ((X - 200) < app->AccuierePlayer().X)) && (((Y + 200) > app->AccuierePlayer().Y) && ((Y - 200) < app->AccuierePlayer().Y))) {
if(((X) > app->AccuierePlayer().X)) {
MoveLeft = true;
}else {
MoveLeft = false;
}
if((X) < app->AccuierePlayer().X) {
MoveRight = true;
}else {
MoveRight = false;
}
if((Y) < app->AccuierePlayer().Y) {
MoveDown = true;
}else {
MoveDown = false;
}
if((Y) > app->AccuierePlayer().Y) {
MoveUp = true;
}else {
MoveUp = false;
}
}else {
MoveDown = false;
MoveLeft = false;
MoveRight = false;
MoveUp = false;
}
Interval += CFPS::FPSControl.GetSpeedFactor();
if(Health <= 0) {
Dead = true;
}
}
void COrc::OnRender(SDL_Surface* Surf_Display) {
CEntity::OnRender(Surf_Display);
if(Surf_Entity == NULL || Surf_Display == NULL) return; //If we don't have anything to draw, then we skip this function
CSurface::OnDraw(Surf_Display, Surf_Entity, X - CCamera::CameraControl.GetX(), Y - CCamera::CameraControl.GetY(), (CurrentFrameCol + Anim_Control.GetCurrentFrame()) * Width, (CurrentFrameRow) * Height, Width, Height);
}
void COrc::OnCleanup() {
CEntity::OnCleanup();
}
void COrc::OnAnimate() {
if(SpeedX != 0 || SpeedY != 0) {
Anim_Control.MaxFrames = 3; //If our speed is higher than 0, then the maxframes will be 3 (so you'll get the animation).
}else{
Anim_Control.MaxFrames = 0; //If your speed is 0, then there will be no animation.
}
if(MoveLeft) {
CurrentFrameRow = 3; //Makes the animation look left
}
else if(MoveRight) {
CurrentFrameRow = 2; //Makes the animation look right
}
else if(MoveUp) {
CurrentFrameRow = 1; //Makes the animation look up
}
else if(MoveDown) {
CurrentFrameRow = 0; //Makes the animation look down
}
Anim_Control.OnAnimate();
}
bool COrc::OnCollision(CEntity* Entity) {
if(Entity->Type == ENTITY_TYPE_PLAYER) {
Dead = true;
}
if(Entity->Type == ENTITY_TYPE_PROJECTILE) {
Dead = true;
if(Interval > AttackDelay) {
app->AccuierePlayer().Kills += 1;
Interval = 0;
}
}
return true;
}
Can anyone help me with this?
If you think the issue lies within another code snipped, just ask and I'll place more code.
Thanks in advance