Separation of data model and visual output
No replies to this topic
Members - Reputation: 192
Posted 01 March 2009 - 12:07 AM
I am creating my first game, which is a simple puzzle game (you may have seen it before, it's not original). You have a grid of squares, red or green, and you have to get them all to green. When you select a square, it flips the colours of all squares in a 3x3 block centred on itself. I have been going off several tutorials for different aspects, but am trying to code it so that it is modular and reusable. I currently have the following parts: Model: Stores the state of the game world. For this game this is a vector of vectors of Square objects, which know state information. Performs game logic when actions are given. I have 2 child classes - PlayingModel and SelectingModel. On a move input, SelectingModel changes size, while PlayingModel changes the selected square. On an action input, SelectingModel returns GAME_WON, while PlayingModel iterates through the squares checking for a win condition and returns either GAME_WON or VALID_MOVE. Input: Polls the controller states, currently only keyboard, and sets up an array of current actions. Output: Knows how to draw the model. Currently each frame it takes the model, creates a vertex buffer, calculates the triangles to draw, and draws the scene in an orthogonal view. Controller: *Creates the main window. Creates Model, Input and Output objects. Initialises the game. Each frame gets input, updates model, calls for output. On win condition, switches between PlayingModel and SelectingModel (not elegant but I'm not polishing here) (Please comment on this design if any parts feel wrong, eg * the main window function may need to move elsewhere) I now want to move to a 3D view rather than orthogonal and am not sure where to store the graphics data. I want to be able to change between DirectX and OpenGL in the future without having to change all of the game model objects. I was thinking of Creating a GraphicsObject class in the Output module that takes a Square object in the constructor (will be generalised) and builds extra information around it such as the vertex buffer and index buffer, texturing, etc. These new objects would be held by the drawing class. Is this the best way to do it? How do I maintain the mapping between the graphics objects and the data objects? Also, if I keep a vertex buffer in the graphics object instead of recreating it each frame, do I have to manage it? Such as recreating it if the device is lost.