I am currently working on a game where you need to track a lot of objects.
I have a game object class that contains all the necessary information about the object and processes AI () on update ();
I decided that only objects with the brain would need to update each frame. Therefore, I have selected vectors to save time. However, I cannot find a similar solution when drawing objects. Therefore i got
std::vector<cOBJECT*> LivingObjects;
and
std::vector<cOBJECT*> WorldObjects;
When drawing all the objects, I first put all the objects in a RenderList:
//Add World and Living Objects into one list of RenderObjects RenderObjects = WorldObjects; RenderObjects.insert(RenderObjects.end(), LivingObjects.begin(), LivingObjects.end());
Then I sort them by Y:
//Sort all Objects by Y (bottom) Coordiante sort(RenderObjects.begin(), RenderObjects.end(), cmd);
Here is the code for this (maybe this is slow too ?:
//Sorts by Y-CoordinatΓ© bool cmd(cOBJECT* obj1, cOBJECT* obj2) { return obj1->getrect().y + obj1->getrect().h < obj2->getrect().y + obj2->getrect().h; }
Then I draw them. The drawing function checks if they are turned on on the screen! Therefore, I draw only visible objects:
//DRAW OBJECTS and for (std::vector<cOBJECT*>::size_type i = 0; i != RenderObjects.size(); i++) { RenderObjects[i]->render(Renderer, CameraX, CameraY, SCREEN_WIDTH, SCREEN_HEIGHT); }
Here is the real draw function:
int cTEXTURES::renderAnimation(int targetX, int targetY, double angle, SDL_Point* center, SDL_RendererFlip flip, SDL_Renderer* Renderer, int row, int speed, int offX, int offY, int SCREEN_HEIGHT, int SCREEN_WIDTH) { //Draw if part of it shown on screen if (targetX - offX >= -FrameWidth && targetX - offX <= SCREEN_WIDTH + FrameWidth && targetY - offY >= -FrameHeight && targetY - offY <= SCREEN_HEIGHT + FrameHeight) { SDL_Rect SourceRect; SDL_Rect TargetRect; TargetRect = { targetX - offX, targetY - offY, FrameWidth, FrameHeight }; SourceRect.x = (CurrentFrame - 1) * FrameWidth; SourceRect.y = (row - 1) * FrameHeight; SourceRect.w = FrameWidth; SourceRect.h = FrameHeight; if (OldTime + speed > SDL_GetTicks()) { SDL_RenderCopyEx(Renderer, TheTextureManager::Instance()->TextureList[TexturePath], &SourceRect, &TargetRect, angle, center, flip); return 0; } OldTime = SDL_GetTicks(); //SDL_RenderCopyEx(Renderer, TheTextureManager::Instance()->TextureList[TexturePath], &SourceRect, &TargetRect, angle, center, flip); TheTextureManager::Instance()->draw(TexturePath, Renderer, &SourceRect, &TargetRect, angle, center, flip); CurrentFrame++; if (CurrentFrame == (Frames + 1)) { CurrentFrame = 1; return 1; // returns 1 if one row is finished } return 0; // returns 0 if animation is not finished } return 0; }
Now I have 1000+ objects, maybe even 2000+ later. Just running a for loop to check if you need to draw objects is very time consuming, right?
What am I missing? How can I track 1000 objects and know if they should be drawn without such a large amount of work for the CPU? Especially since I need a lot of space for a lot of AI
Thanks, it drives me crazy.