I'm using SDL2 to make a 2D game. I think I don't have the right words to search for what I want yet so I'll try and explain it here.
Currently the way I load the player image, background etc is all in the main function. The objects are defined there and pointers are used to store the images.
I guess that's acceptable for things that will be on screen all the time, but what about for objects like props or enemies that will only be in certain levels? Also what about building actual levels? How would you make that without hard coding everything?
I already have a way of reading level data (json parser) and setting the player's starting position and background image based on the player' progress (stored in another external file).
What I'd like to do is be able to define some objects and their xy positions as well as other metadata in the level file, and then be able to load the game level based on that instead of hard coded variables. Basically, what normal games do.
The following is what I think it would look like. But like I said I'm new to this so I'd appreciate some input.
On initialise: Parse the json file, get the type of each element and match that against the corresponding object class. Create an instance of that class along with all the initialisation data needed (texture, starting position etc) and store it in a level array (a vector ? or a list or something?).
In the game loop: For each iteration, user another loop to iterate through the level array and call the input handler (if it exists for that object), update and render functions for each element.
Does this seem like a good idea?
If so, any pointers (pun intended) on achieving this? If not, what would you suggest?
Are there any code examples of something like this out there?