Architectural Problems so Pong

I’ve found that I’ve programmed myself into a bit of a corner.

The issue is that my game engine, despite my best efforts, is architecturally bad. I think this is probably a regular occurrence for a first-time effort of a project this large. This poor design came about despite having read Design PatternsGame Engine Architecture and Game Programming Patterns.

I think this was inevitable. Reading books such as the above, and I highly recommend them all, isn’t the same as applying the patterns in practice. There are many patterns throughout the code, don’t get me wrong, but I think the logical separation of state isn’t as logical as it should be.

So; having stepped back from the code and given it a very thorough critical review… it needs to be re-coded in large portions.

It’s not only the architecture that’s a problem, I think my build chain is poorly designed. Having known no better, I started as one large project; whereas each component likely could’ve been it’s own project, each outputting .dll files.

Further to this; I’ve just been programming and testing as I went along. Unit Testing, something I’ve never done before, is probably something I should look into. It makes complete sense, by enforcing unit tests on myself, I’d naturally enforce the decoupling of systems. I’m concerned about the amount of time and effort that needs to go into test driven development, but having never undertaken it, I’ll look into it.

Another problem I’ve found is that I didn’t architect the engine for networking. Something that I’ve learned the hard way, needs to be designed into the code up-front.

So; in order to begin applying patterns, networking, and possibly unit testing into my code; I’ve decided to make a 2D network-multiplayer Pong clone. Should be simply enough to code the actual game; but where it should get interesting is in applying network into the core of the engine.

I’ve designed it already.

Pong Architecture

Pong is probably too simple to really apply Design Patterns; however there’s the few usual patterns that you’d expect in a game, really just Update and a Game Loop. However, what I’ve done here is focused on the separation of game data from the rendering and looked heavily into Networking.

For the rendering, I’ve decided to use SFML rather than my own engine code. SFML is great, it’s quick and easy and does 2D brilliantly. The reason I’m making Pong isn’t for the graphics, so I had no desire to go that deep.

SFML also has a great networking abstraction layer which I’m using for Pong. I’m not too worried about ensuring packets are delivered correctly, so abstracting that out works well for me.

I’ll probably go back and replace SFML with my own code later.

I’ve read over the Multiplayer-Neworking information on the Valve Developer Wiki a few times to learn about how to implement my Pong game. The reason I’m looking so much into Valves information is that their networking is for fast-paced action games. My Pong game is going to use the mouse to control the paddles, therefore it is fast-paced.

This has led me to determine that a Server-Client model won’t work for Pong. I really wanted it to, as that’s the sort of common way big games do it; but I can’t see how that would work for me. One player, the Listen-Server player, would always have an advantage. In a one-on-one game, I don’t think that’s fair, sure it works for 16v16 or something, but not 1v1.

So instead, my Pong game will be a bit… funny. Whoever is currently in “possession” of the ball will be the “server”, and whoever is waiting on the ball will be the “client”. So, as soon as I hit the ball and return it, my opponent will now be deciding the game state.

This allows each player to locally control the very time sensitive hit-detection etc. For a real game, it would be disastrous. Cheating would be rampant. But no one is going to cheat at my Pong game, I’m sure.


Signed and Unsigned Integer Comparisons

Occasionally I find that I need to compare signed and unsigned int values. This understandably throws warnings. So I wrote the following helpful functions to safely compare the values when needed. Obviously I try not to, but there’s times when I’m forced to.

It allows me to compare signed int and unsigned int values as either the left or right parameters.

C++ Scripting User Interface

Bringing bits together

I’ve now got what’s shaping up to be a pretty good UI system:

  • The UI styling is defined via data in space-format (JSONish)
  • The layout of the UI is then defined by a second space-format file:
  • Then the “actions”, in the case above the single button, are defined in a space-script file:

    Basically, the UI Layout says the button will execute the function named “SeedButton”. That function creates a new button that will execute the function named “OutputNum”.

This all works well, surprisingly. The only gap is that every “execution” of the script, or execution of a function in the script, is completely independent of any other execution. Meaning that I can’t store information between button presses, at least not in the script.

To get around this, I’ll likely implement a key-store as a regular Engine Function so that scripts can store information for later use, even when it’s only ever needed by the script.

I didn’t think of this before because I only ever imagined scripting things like enemy behaviour, which would just access and set what it needed to.

C++ Tutorial

C++ Variant Type Walkthrough

Considering the amount of pain I had trying to work out how to do this; I thought I’d write up something lengthy to explain how it works. It also helps me find if there’s anything in it that I don’t fully understand, else I wouldn’t be able to explain it.

The full Variant types header file has already been posted. This is the code I’m going off of.


A Recursive Variant Type in C++

Because one of my goals is to rely on as few external libraries as possible, I am not using Boost. As a result, there comes times when I need to roll my own, extremely useful, library-like code that isn’t a part of the C++ standard yet.

One of these, is the Variant. A tagged union / discriminated union / sum type, that allows you to store multiple data types in one section of memory. A sort of type-erasure gimmick.

I need a type like his for two reasons so far. The first is that my scripting language, space script, is quasi-type safe. You define variables using your usual data types, but any variable type can be cast to any other type.

I did it this way mostly because I was curious to see how a language like that would behave. Probably not the best idea.

I’ve also allowed my Variant type to be recursive. This means that it can contain members of itself. I’ve done this specifically because my data format, space format, is JSON-esque, and basically every node can contain any number of numbers, strings and other objects etc. That’s the second reason I need a variant type.

This took me quite a bit of screwing around to get right. A lot of the code for the base Variant was taken from someone else’s code that I found online. This code was fundamentally broken though; so I’ve fixed it up and allowed for recursion.

I’ll probably do a write up on this at some point. I like it over the boost::variant primarily because I don’t want a never-empty guarantee. I can’t imagine why I’d want a type that doesn’t allow for null-states.

EDIT: Fixed up some of that code.

EDIT 20/05/2016: Fixed up some more of that code.