Hi, I started implementing 2D board game. I have concept of how to write rules, controlls etc, but i dont want to write another all-in-app. So i decided to do it "right". I divided my code into reuseable modules - ResourceManager, Renderer, Core, Math (for now). All modules use SDL2.
ResourceManager(RM) - loads textures, audio etc without duplicating them in memory. Resources are gathered in TextureResource, AudioResource (...) objects, handled by std::shared_ptr. For textures I have prepared Texture class that wraps SDL_Texture and I RM serves this Texture objs for Core module.
Core - The main game module, contains game loop, gameobject / component implementation and event handling. Core requests for data from RM and sends them to right components.
Renderer - Creates window and knows about render range (in core represented by camera). Takes info about texture, position, rotation and scale to render images (just this for now).
Its time for my questions:
- is this architecture good? After I end this board game I want to extend renderer module for example for rendering 3D objects.
- Loading resources while ingame is good idea? I mean single textures, models, sounds etc.
- As I said, for handling resources I am using shared_ptr, is it good cleaning cache every (for example) 3 minutes? By cleaning i mean removing not used resources (counter =1).
-
And the hardest thing for me right now - take a look at this flow:
- Core create a T1 token
- Component Renderer2D is connected to T1.
- Core requests a texture /textures/T1.png from RM.
- RM checks if /textures/T1.png is in map, if not, loads it.
- RM returns a std::shared_ptr<Texture> to Core.
-
Core assign texture to T1 Renderer2D component.
Now i want to pass this object to renderer. But I wont pass all gameObjects and checks which have renderer2D component (i also cant, because only Core know what is gameObject and component). So i had an idea: I can create Renderable interface (in Renderer module) and inherit from it in the renderer2D component. Renderable will contain only pointers to position data. Now i am able to pass renderer2D component pointer to Renderer and register it.
Is this good way to handle this? Or im overcomplicating things?
-
If point above is right I had last question - registering object in Renderer module. I dont want to iterate over all objects and check if I can render them (if they are in render range). I wanted to place them in "buckets" of - for example - screen size. Now calculating collisions should be faster - i would do this only for objects in adjacent buckets. But for 2D game i have to render objects in correct order using Z index. Objects have to be placed in correct bucket first, then sorted by Z in range of bucket. But now i have problem with unregistering objects from Renderer module.
I think I got lost somewhere in this place... Maybe You can help me? Of course it this is correct way to handle this problem.
I would love to read your comments and tips about what can I do better or how can i solve my problems.
If i didnt mention something but You see something in my approach, write boldly, I will gladly read all Your tips :).