The most important issue is separating the actual game logic from the display. The display will be coded last depending on how much time I have, so I should be able to 'plug-in' any sort of display that I want. I searched around and saw that the Model View Controller pattern is a good way to achieve this and I have created a test program to test this design. It worked really well. I have been able to attach a graphical (SDL) view and a text view to my game simultaneously and see both updated simultaneously. I have also created two different input types (controllers) - a text input and a SDL input.
For reference, here is a basic class diagram of the classes that I am using:
And this is the original gamedev wiki article on MVC that got me started:
http://wiki.gamedev.net/index.php/Observer_pattern
Here is the test program that I am using, just to demonstrate the basic setup that I have going:
#include "Game.h"#include "TextDisplay.h"#include "GraphicDisplay.h"#include "GraphicController.h"#include "TextController.h"int main( int argc, char* args[] ){ // Create a new Game. Maybe this can even be done in the controller class // using a TextSetup or GraphicSetup Game* game = new Game; // Create different displays Display* textDisplay = new TextDisplay(game); Display* graphicDisplay = new GraphicDisplay(game); // Create controllers for the game Controller* textController = new TextController(game); Controller* graphicController = new GraphicController(game, dynamic_cast<GraphicDisplay*>(graphicDisplay)); // Attach the displays to the game game->AttachDisplay(graphicDisplay); game->AttachDisplay(textDisplay); // Control the game with the controller graphicController->Execute(); // textController->Execute(); delete textController; delete textDisplay; delete graphicDisplay; delete game; return 0;}
Now some subtle problems start creeping in. This is mostly due to the inherent differences between console input/output flow than frame-based, event driven graphic interfaces.
With the MVC model, all views are only updated when the model decides they need to. However, when using SDL, everything needs to be rendered every frame. To accomplish this, I have passed a reference to a GraphicDisplay to my GraphicController, so that it's Render() method can be called every frame. This is already starting to look dodgy, because the controller should ultimately not have to care about the view. Any suggestions on how I can incorporate a SDL input/rendering system into the MVC pattern?
My second problem is that of graphical input. The MVC pattern ultimately separates input (controller) from display (view). However, in the case of clicking on a piece on the board to select it / target another piece / etc, the graphics and the input are ultimately very tightly coupled. Who should be responsible for what? How do you separate the two? Is it even possible? I've thought the problem through but I haven't managed to arrive at an elegant solution.
Please feel free to ask if you need anything clarified. I'd love to see some input on how to tackle this design problem :)