I don't know the best answer to this but in my app I currently have a state machine and states. The state machine has a few methods:
Update(time)
Render
QueueState
PushState
PopState
and input type methods
Thanks, from what I have been reading down the track I will need to add a 'Stack' of states (just using a vector?) so I can have multiple screens or states open at once like you do. For now I just want to start simple and get the basics working.
The main loop just calls Update and Render.
Do you mean that in your game::update() loop all you call is gameState.update(), and let the gamestate class itself deal with all of the states, 'outside' of the main game loop?
Eg for each update step, instead of in game::update()
gameState.ChangeState(Intro),
gameState.ChangeState(Playing),
all you ever have called is
gameState.update()?
I would just initialise things, create a SplashScreen state, push it onto the state machine and then start the main loop. From there on it's the SplashState that decides what's happening,
Okay, so the state machine is the most 'outer layer' (besides the game loop), of the engine? Literally each update loop, the first thing the game engine does is check the state, and then executes the code only within that state?
States should take care of their own rendering (either directly or by getting themselves into some draw/command lists to render themselves), they should update themselves and they should deal with input themselves.
This is where I am a little stuck. From my reading and how I would like to go about things, I will have one 'RenderSystem' that just calls 'm_graphics->drawSprite(spriteData)'; each update loop. Previously I was using entitiyx ECS and would call it in the 'for (entityx::Entity entity : entities.entities_with_components(position, display))' loop, but I am moving away from ECS.
General Game/Engine Structure
Passing Data Between Game States
Thanks, I had actually read these articles already, the General Game Structure topic was great and got me started, but to be honest Passing Data Between Game States was a bit beyond me. I struggle to take programming concepts from just reading too... so am just trying to get stuck into it and then revisit things once I have a bit more of an idea.
Loaded on the next frame after being set.
Rendered every frame.
I think what I meant to ask is 'Where' should they get loaded / rendered. As in where about in the game engine. Should the gameState class itself handle all of this internally? Or should the main engine 'check' the game state class each loop and perform the relevant actions?
What does that mean?
My last attempt was using entityx ECS and game::update() called
void Game::update()
{
GameState state(m_gameManager.getGameState());
if (state == GS_Playing)
{
m_systemManager.update<PlayerControlSystem>(frameTime);
m_systemManager.update<MovementSystem>(frameTime);
}
}
so I am wondering do I use the same approach and have a
m_systemManager.update<MenuSystem>(frameTime);
that is controlled the same way the rest of the 'Game playing' Logic is, or are menus a separate thing?
These are not separate loops. There is only 1 game loop, which may update the game logic 0 or more times, and always renders one time.
Fixed-Time-Step Implementation
Sorry, by this I mean my game::run() loop, which looks like this (it is based on the game engine of Charles Kellys programming2dgames book:
void Game::run()
{
// calculate elapsed time of last frame, save in frameTime
QueryPerformanceCounter(&timeEnd);
frameTime = (float)(timeEnd.QuadPart - timeStart.QuadPart) / (float)timerFreq.QuadPart;
if (frameTime < MIN_FRAME_TIME)
{
sleepTime = (DWORD)((MIN_FRAME_TIME - frameTime) * 1000);
timeBeginPeriod(1); // Request 1mS resolution for windows timer
Sleep(sleepTime); // release cpu for sleepTime
timeEndPeriod(1); // End 1mS timer resolution
return;
}
if (frameTime > 0.0)
fps = (fps*0.99f) + (0.01f / frameTime); // average fps
if (frameTime > MAX_FRAME_TIME) // if frame rate is very slow
frameTime = MAX_FRAME_TIME; // limit maximum frameTime
timeStart = timeEnd;
update();
render();
}
Why would you hope for an over-engineered misguided system that you can’t possibly use correctly/fully and doesn’t serve your needs best?
ECS is a buzzword and solves a specific domain of problems suitable only to marketed engines such as Unity. It’s not for you and doesn’t help you in any way. Stop doing things just because they are buzzwords.
My bad, I am planning to use components as per http://gameprogrammingpatterns.com/component.html . My last attempt was an ECS engine and I should have listened to you guys and not gone down this track. I did find it quite 'convenient' but also did not find I was actually learning c++ game programming by using it, since the huge library was doing all of the heavy lifting for me. I was also having issues when it came to things that did not so obviously fit into the ECS pattern e.g. when I implemented a quadtree it was actually slower as I had to extract and pass in the entity ID's and do heaps of derefrencing rather than just filling it with all the objects and working on them directly. (which I think was the problem... I am not good enough at programming to say for sure...). One thing I did learn from ECS was how and why we want to separate out data and logic into these systems... so essentially I am trying to recreate that decoupling and flexibility but without some massive ECS library doing god knows what...
Thanks for all your comments guys.