Modern Game Engine Architectures

Game Engine Architectures

This week, in one of the game programming classes, students enquired about the best way of designing their Graphics Engines and Game Engines. We run courses in which students have to implement simple graphical applications with the use of OpenGL; and courses in which they have to implement games in C++.  I have recommended them to use component-based entity systems and delegation, as it is by far one of the most widely used solution. I have pointed out that typical object-oriented hierarchy modelling, even though it is simple to start with, may quickly lead to problems and the need for multiple inheritance. I’ve also suggested model-view-controller pattern, or MVC, to cleanly isolate their model, logic, and presentation layers. You can check the introduction article here. It is important to note that there have been multiple discussions on game engines, and this one is a good example this one: “Why are MVC and TDD not employed more in game architecture?” and this one “Component based game engine design“.

Obviously, the typical solutions that we use need regular update and therefore this weekend I’ve looked into the various options for design game engines. And it turns out to be quite a complicated topic on its own. I have learned 2 things: a new, pretty complex way of implementing component system, and functional reactive programming patterns. Let us start with CBES.

Component-based entity systems

CBES, or Component-based Entity Systems are one of the most popular and pervasive models used in modern designs of many games and game engines. One of the best introductions is provided in the Game Programming Patterns. However, they have their drawbacks. They tend to get quite complex, due to inter-component communication, state-tracking, and dependencies. They do not take any advantage of cache lines, and they can be limited in the way of abstracting behaviours.

One of the interesting models for implementing the components is Data-oriented Design. In CppCon 2015, Vittorio Romeo provided a skeleton for the Implementation of the component-based entity system in modern C++. His talk is definitely worth a watch.

 

There are other alternatives. In this simple article “Why I switched from CBES to FRP” the author is pointing out some of the limitation of component-based system, and suggesting the switch to Functional Reactive Programming.

 

Functional Reactive Programming

It turns out, that FRP, or Functional Reactive Programming is not only limited to Haskel or Elm. It can be used in your C++ projects to. There is Reactive programming in C++ (RFP in C++: video lecture). The code of David Sankel’s sbase system is available on Bitbucket.

ps. David has also C++ oriented podcast, that is quite interesting.


Poznan marathon: October 11

Poznan marathon story

Before the start

I was supported by my dad. We got near the stadium around 40min before the start. It was hard to find a parking spot. Cold morning. It was around +4C when we started driving. My dad had to scrape frost from the windshields. While walking through the Poznan trade centre towards the hall I’ve done a quick warmup. The hall was packed with runners, and the inside of the hall was nice and warm. We walked through the Hall #3 and exited on the other side. There were long queue to the toilets. Lots of people everywhere. I’ve run back and forth a bit, trying to keep warm. The time passed quickly. The last 10min of waiting I spent already in the crowd, near the start line. Jumping. Screaming. Getting the adrenaline pumping. And then – the run just started.

Before the 18th km

I started relatively fast, trying to get in front of the slower runners to have space to keep my own pace. I have not started far from the start line. 10 seconds to be precise. I kept the fast pace. Unusually fast. My heart-rate was in the excess of 180, hitting 190bpm. All due to adrenaline rush. I wasn’t actually running on my limit – quite to the contrary, I felt relaxed. 5min into the race and the heart settled on around 165bpm. I have never run that fast before. Usually, I start quite conservative for the first 10km or so. I try to pick up the pace later. Early fast start means early build-up of lactic acid in the muscles, which means trouble later in the race. Because the last two marathons I’ve finished with the power reserves, I wanted to know how it feels to run faster. The pace was set, and even though the pace was fast for me (4:15min/km), I was able to keep it up reasonably well. I’ve joined the group of runners with the 3:00 pacemaker, and running with that group felt great. I could see some of the runners in the group doing well. Others showing early signs of struggle. I was in the latter category. I knew I will not be able to keep up that pace till the end of the race. The first crisis came at about 18th kilometre.

After the 18th km

The running with a group means you have to keep the pace of the group. It is both: good and bad. The good thing is that you have less air resistance and that the group keeps you motivated. This particular group was quite densely packed, to a point that even small variation in speed was hard to execute. We all were so close to each other, that slowing down means everyone behind you have to slow down with you, and speeding up is not easy because there is no space. I decided I need to vary my speed a bit more than the running in tight group allows me to do. I slowly dropped to the tail of the group. I wasn’t much slower than the group, but I’ve noticed that moving forward towards the centre of the group became impossible. I just couldn’t move forward anymore. I continue running with slightly slower pace then. In the group I managed 4:13min/km, and then by dropping to the tail, I’ve dropped also to 4:15min/km. But, that was the speed I should have to achieve 3hrs mark anyway, so I was actually quite happy. Running at 4:15min/km felt somewhat easier, and I could vary my tempo more freely. The relatively happy running was then interrupted at around the 26th km, when I started feeling quite bad. The run suddenly became super HARD.

The pain

The right quadriceps started aching shortly after 10th or 11th kilometre, and I had to favour my left leg for a while. This soon elevated the pain of the left calf to a point that it was aching more than the quadriceps before. With both left calf and right quad aching quite badly, I tried to vary the gait so that other muscles put in more effort offloading the two that starting showing signs of damage early. I have also started pushing more with my arms, hoping that the tired leg muscles will have just a bit easier. Once the pain sets in, it never really goes away, but when a new pain in new muscle appears with ferocious intensity, you somewhat forget about the previous pain.  It is like climbing the staircase, where each new step is just a bit taller and harder than the previous one. While you overcoming step 3, it feels intense and hard, but once you at step 8, looking back to your efforts and troubles at step 3 you get a completely different perspective. The leg pain actually faded into minor once my back and shoulders started aching from intense arm workout that I was putting in. I tense too much, and the hard fast running was taking its toll.

After the wall

I hit the wall around 26th km. Suddenly, I just felt extremely weak. I pushed harder, trying to wait it off, but the pain, tiredness and lack of power never went away. In fact, my leg muscles felt weaker and weaker. I had to push harder still, yet, watching my tempo slowly dropping down. 4:16min/km. Push push push push like never before, just to realise, it is now 4:18min/km. I tried real hard to find the power and energy and pushing even more. 4:20min/km. Yes, the intensity of the wall was somewhat crushed, and running for a while at 4:20min/km felt good again. Stable. Perhaps I could keep that pace? Nope. The thing is that the time between the walls actually shrinks the longer you run, and the new down period set in. Pushing you more into the hell. I felt quite good overall. Sure, I was aching everywhere, but the leg muscles stopped complaining, and went numb instead. I couldn’t feel much pain, just a total inability to generate energy. The leg muscles felt really slow and weak. The arms could pump something but that was rather painful. My back was hurting from the workout my hands were doing. I monitored my heart-rate and it was steady at around 165bpm. I asked myself: can I do 170bpm? And the answer was: NO. No matter how hard and fast I tried to run I simply couldn’t push my body beyond the 168bpm. Strange feeling. I new my heart can do more than that, I new my mind can withstand more pain that I was in, yet, for some reason, I just couldn’t push harder. Was I on my physical limit? I’d like to think that, but I probably wasn’t 😉  I did not feel that I was. Have I reached my physical limit?

The finish

When there were 6 or so kilometres left, it became even harder to run and to keep the pace. And then, on the last 4 km stretch we all had a head-wind pushing us back. Making the forward progression became even harder. I remember slipping slowly back in the pack, being completely depleted, and having no power for a strong finish. I was empty. Crossing the finish line have not delivered a strong emotional kick. It just felt the job was hard, and now, the job was done. I was pleased to reach the same time as before, which is 3:10. This time, without any power reserves left. And with the whole body in pain.

My dad was waiting for me at the finish line. My wife and kids where cheering my back home, watching the entire race through internet broadcast. They knew I went well for the first 15km or so, but I was slipping in position from then on. I eventually finished on place 334th.

About 20min after the finish, the endorphins kicked in, and I became overwhelmed with a good feelings 😉 That good feelings stay with me for about 2 weeks solid after every long run. (Writing this entry I have a grin on my face.)

Because I have already achieved 3:10, it turns overall into a bit of anticlimax. With all the prep and no improvement in the actual “result”. But then again, i’m not sure if the result in terms of time is the key, or if the experience is the main reason why I do it. The experience is worth the effort. The 13th marathon story ended.

 

Some stats

There were 7052 runners taking part in the run. 6242 finished. That means more than 10% tried to do something and gave up at some point.

The oldest man finishing was 74 years old (Piotr Silkin). He finished with the time of 4:14. The oldest woman was 73 (Wiera Stasiewicz). She finished with 5:28.

Poznan marathon results


Marathon: what it is.

Marathon: what it is?

It is not really about fitness. Or your legs. It is all about your head. And your heart.

The run starts with lots of euphoria and enthusiasm. It is extremely elevating to be able to participant in one of the old running events. Events that inspired so many people in the past, and continue to inspire more. The event that goes back to the ancient Greece and the story of the Marathon. And it feels awesome to be surrounded by thousands of like-minded people, that undertake the challenge with you. Awesome. Truly elevating. You start. The rhythmic noise of people’s feet hitting the road puts your mind quickly into ease and meditation-like state. The first 10k passes rather quickly. You keep the pace that you set yourself for. You feel great. You have been overtaken by faster runners. You have overtaken the slower. And you felt great doing it. You settle to the group of runners that are LIKE YOU. Some might be slightly slower. Some slightly faster. But the same t-shirts, the same shoes, the same faces start building a family around you. You might talk with some. You might be running in silence. But you are part of that family. And you feel it. The rhythmic sound of shoes hitting the road accompany you.
With relatively happy thoughts you hit the half mark. You start thinking, that you might be able to DO IT. The half wasn’t that bad, right? It continues to feel good. You keep the pace. Things seem to be going according to your plan. And then, suddenly, it happens. Perhaps 25th kilometre or so. You slow down! Just a bit. But enough to notice. And you have to make a huge mental effort, to push your body to run. To return to the pace that you should have. And it happens again. You slow down. Just a bit. And again you have to force yourself to run faster. You repeat that for a while. You get used to it. You know it is a cycle. You pick-up, you give-up, you pick-up again. Easier. Harder. Easier. Harder. You get into the cycle. Your legs start to ache. Your entire body starts to ache. And you start feeling tired of the effort. In your head. You JUST want to slow down. But people around you help. You tag along a group. Or perhaps just one runner. They keep you moving.

You hit 30km or so. You start seeing some people that slowed down. Some start to walk. Some are giving up. You may shout to them. Or pat them on the back. Or encourage them to FIGHT. This is not the end. There is WORK to be done. And you suddenly feel good again. You feel that you might have what it takes! You keep pushing. You feel the run is great. You got a second wind perhaps.

You try to fool yourself thinking that MOST of the work is behind you. That things will be fine! But you KNOW this is a lie. You know, the race has not started yet. The race will really start after you hit the wall. The wall may hit you seriously hard. Or gradually. But it always happens. It sneaks up on you. Suddenly. The realisation, first in your body, then in your mind, that the pace is TOO MUCH for you to keep. You cannot run THAT FAST anymore. No way! Your body screams for you to slow down. Your mind keeps inventing reasons of why you should slow down. Slowing down is the only thing on your mind from now on.

But you are not slowing down. Your body aches. Screams. You feel it is unbearable. You have to “leave your body behind” and think about something else. You better HAVE something else to think about. Because the effort is more than you can take. More than you can deal with. The usual unity of your mind and your body is shuttered. The effort is way TOO MUCH to take in.

Yet you continue to run. And you continue to push. This is real HELL. Every passing minute feels like eternity. You start resisting checking the watch, because you know that you will be very disappointed. You cannot comprehend that the hours of suffering that you just went through, are actually minutes. Or worse: seconds. You KNOW you cannot continue this. But you do. You keep running. The only thing in your head is to give up. Your thoughts race, and you start seriously contemplating giving up. But you continue, and instead of slowing down, you give up hope. You start thinking that this suffering, this unbearable effort will never end. That this HELL will just continue for eternity. You start thinking of death. You may actually start begging for death. Anything, that would put you out of this misery. You would give anything for this to end. Your body and your mind scream at you to STOP.
You continue running.

And then you see it. You do not believe it, but it is real. You see the finish line. And ONLY THEN you start believing that you can actually finish it. That it will be OK. That you have conquered the impossible. How? You have no idea. But you know: YOU, somehow, managed to go through this inhumane effort. This hell. And you came out on the other end. There is still a doubt, a hint that you just attempted the impossible, and that IT WILL NOT WORK. But it does – you cross the finishing line. It is real. You are totally exhausted. Completely depleted. Both, physically. And mentally.

A surge of emotions hit you. You may cry. The emotions are super strong. It is HUGE. You have done something that was impossible. You have gone beyond your own limitations. You WERE THERE. You have witnessed it. And you came out.

It is one of the most elevating feelings that you can experience. It shakes you to the very core. It goes real deep in you. And stays with you. Forever.

One day left. The day after tomorrow I will stand with 8 thousands other runners. And run. If you never done it yourself, you have no idea. You do not know, or understand of how it feels. No idea of what it is. You may think you do, cognitively. But it is not possible for you to fully appreciate the effort and challenge. And what you missing out. Wanna try it?