Same Game
Home Up

 

The "Same Game" tutorial

"The Same Game": A Simple Game from Start to Finish

I like the look of this example as it shows a point and click game that does a bit more than "hello world", but hopefully not enough to be incomprehensible.

So I'm looking at this tutorial from cprogramming.com and it is clear that things have moved on a little, at the time of the tutorial's writing the lowest tier of Visual Studio was "express" which did not include the required libraries.

At this time (2022) the entry tier of Visual Studio is the "community" version, which does include the required "Microsoft Foundation Classes", though they do need to be selected in the installer.

The MFC project template is easy enough to find:

MFC App Build apps with complex user interfaces

Following the tutorial things start to go a little bit "off the rails"

Application Type menu

so I'll do my best to muddle through matching the VS2022 options to the VS2005 tutorial images

User Interface Features menu

I got that one wrong, and I might have to go back and do it all again as I don't know how to correct it later

Advanced Features menu

Generated Classes menu

but apart from the Generated Classes page looking totally different I think things are going OK so I'll finish the "Wizard".

At this point something actually builds and runs:

Empty application window

though to be fair at this point it doesn't actually do anything except show an about box and close.

page 2: Document/View Architecture

Document/View Architecture

There's a brief discussion of document/view architecture which I won't repeat except to note that:

  1. It appears to bear a passing resemblance to "Model View Controller", which is worth reading up on as any sufficiently advanced user interface project is likely to turn into MVC at some point.
  2. The separation is probably unnecessary for a puzzle game as it means that much of the time one object simply passes actions on to another object, but it should become relevant in more complex projects

 

The Document: Keeping Your Data

In which we finally come into contact with some code.

Note that #include "StdAfx.h" is now #include "pch.h", don't worry.  

The "#pragma once" directive indicates that the header file should only be included once even though it may be reached multiple times, in the same way that "include guards" do in "C". The advantage of #pragma once is it doesn't need macro names for each file and by skipping whole files it makes the build process more efficient. A disadvantage is not all build environments recognize it.

Also note that if the application is still running then you'll get an error when you recompile as it can't overwrite a running program.

There is a lot of template "boilerplate" code that you need to leave untouched. More than there is in the tutorial so it is tricky to work out exactly what to add.

Another "Gotcha" to watch for is that when you go to add a class called "CSameGameBoard" the names of the files also get the extra letter and end up as "CSameGameBoard.h" and "CSameGameBoard.cpp". These need to be corrected to "SameGameBoard.h" and "SameGameBoard.cpp" to match the tutorial. If they've already been created and then renamed then the #include line in "SameGameBoard.cpp" will also need correcting.

Page 3: The View: Drawing your Game

This stage is where I start to come seriously unstuck. I'm having the utmost difficulty reaching the correct "properties" window.

First I need to look in the "View" menu and select "Object browser".

Object Browser menu  Properties menu

With CSameGameView selected the Properties box (bottom right) finally shows the required view.

After the changes have been made we get:

Game window picture

The section across the bottom shouldn't be there, but I don't know how to remove it.

Removing the unwanted status bar

It turns out the bar is only referenced in two places and these are easily commented out:

The Declaration in MainFrm.h:
protected: // control bar embedded members
//CStatusBar m_wndStatusBar;

and the initialization in MainFrm.cpp:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
   
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
       
return -1;
   
/*
    if (!m_wndStatusBar.Create(this))
    {
        TRACE0("Failed to create status bar\n");
        return -1; // fail to create
    }
    m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));
    */
   
return 0;
}

At this point the application runs as expected.