Click here to Skip to main content
15,886,519 members
Articles / Multimedia / DirectX

DirectX Board Game Engine

Rate me:
Please Sign up or sign in to vote.
4.64/5 (19 votes)
29 Nov 2007CPOL11 min read 91.2K   1.7K   50  
An article on how to create a generic engine for board games such as Checkers or Chess
using System;
using System.Drawing;

using System.Collections.Generic;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Bornander.Games.BoardGame;
using Bornander.Games.Direct3D;
using Bornander.Games.BoardGame.Direct3D;

namespace Bornander.Games.Checkers.Direct3D
{
    class CheckersModelRepository : IBoardGameModelRepository
    {
        #region Private members

        private Model blackSquare;
        private Model redSquare;

        private Model redPiece;
        private Model redKingPiece;

        private Model blackPiece;
        private Model blackKingPiece;

        #endregion

        public CheckersModelRepository()
        {
        }

        #region IBoardGameModelRepository Members


        public void Initialize(Microsoft.DirectX.Direct3D.Device device)
        {
            // Create a box to be used as a board square
            // The .Clone call is used to get a Mesh that has vertices that 
            // contain both position, normal and color which I need to render them using flat shading.
            Mesh blockMesh = Mesh.Box(device, 1.0f, 0.5f, 1.0f).Clone(MeshFlags.Managed, VertexFormats.PositionNormal | VertexFormats.Specular, device);

            // Create some red and black material and their highlighted counterparts.
            Material darkRedMaterial = new Material();
            darkRedMaterial.Ambient = Color.DarkRed;
            darkRedMaterial.Diffuse = Color.DarkRed;

            Material redMaterial = new Material();
            redMaterial.Ambient = Color.Red;
            redMaterial.Diffuse = Color.Red;


            Material highlightedRedMaterial = new Material();
            highlightedRedMaterial.Ambient = Color.LightSalmon;
            highlightedRedMaterial.Diffuse = Color.LightSalmon;

            Material squareBlackMaterial = new Material();
            Color squareBlack = Color.FromArgb(0xFF, 0x30, 0x30, 0x30);
            squareBlackMaterial.Ambient = squareBlack;
            squareBlackMaterial.Diffuse = squareBlack;
            
            Material blackMaterial = new Material();
            Color almostBlack = Color.FromArgb(0xFF, 0x10, 0x10, 0x10);
            blackMaterial.Ambient = almostBlack;
            blackMaterial.Diffuse = almostBlack;

            Material highlightedBlackMaterial = new Material();
            highlightedBlackMaterial.Ambient = Color.DarkGray;
            highlightedBlackMaterial.Diffuse = Color.DarkGray;

            blackSquare = new Model(blockMesh, new Material[] { squareBlackMaterial, highlightedBlackMaterial });
            redSquare = new Model(blockMesh, new Material[] { darkRedMaterial, highlightedRedMaterial });

            blackSquare.PositionOffset = new Vector3(0.0f, -0.25f, 0.0f);
            redSquare.PositionOffset = new Vector3(0.0f, -0.25f, 0.0f);

            // Create meshes for the pieces.
            Mesh pieceMesh = Mesh.Cylinder(device, 0.4f, 0.4f, 0.2f, 32, 1).Clone(MeshFlags.Managed, VertexFormats.PositionNormal | VertexFormats.Specular, device);
            Mesh kingPieceMesh = Mesh.Cylinder(device, 0.4f, 0.2f, 0.6f, 32, 1).Clone(MeshFlags.Managed, VertexFormats.PositionNormal | VertexFormats.Specular, device);

            redPiece = new Model(pieceMesh, new Material[] { redMaterial, redMaterial });
            blackPiece = new Model(pieceMesh, new Material[] { blackMaterial, blackMaterial });
            redKingPiece = new Model(kingPieceMesh, new Material[] { redMaterial, redMaterial });
            blackKingPiece = new Model(kingPieceMesh, new Material[] { blackMaterial, blackMaterial });

            redPiece.PositionOffset = new Vector3(0.0f, 0.1f, 0.0f);
            redKingPiece.PositionOffset = new Vector3(0.0f, 0.3f, 0.0f);
            blackPiece.PositionOffset = new Vector3(0.0f, 0.1f, 0.0f);
            blackKingPiece.PositionOffset = new Vector3(0.0f, 0.3f, 0.0f);


            // The Mesh.Cylinder creates a cylider that extends along the Z axis 
            // but I want it to extend along the Y axis, this is easily fixed by
            // rotating it 90 degrees aroung the X axis.
            // First create the rotation...
            Quaternion rotation = Quaternion.RotationAxis(new Vector3(1.0f, 0.0f, 0.0f), (float)Math.PI / 2.0f);
            /// ... then apply it to all piece models
            redPiece.Orientation = rotation;
            blackPiece.Orientation = rotation;
            redKingPiece.Orientation = rotation;
            blackKingPiece.Orientation = rotation;
        }

        public Model GetBoardSquareModel(Square square)
        {
            if ((square.Column + square.Row) % 2 == 0)
                return blackSquare;
            else
                return redSquare;
        }

        public Model GetBoardPieceModel(int pieceId)
        {
            if (pieceId == CheckersLogic.PieceStateBlack)
                return blackPiece;
            if (pieceId == CheckersLogic.PieceStateBlackKing)
                return blackKingPiece;
            if (pieceId == CheckersLogic.PieceStateRed)
                return redPiece;
            if (pieceId == CheckersLogic.PieceStateRedKing)
                return redKingPiece;

            return null;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Article videos
Oakmead Apps Android Games

21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010

Comments and Discussions