Saturday, September 15, 2012

How to get Game Coding Complete's directory structure working in Visual C++ 2012

I like Mr. Mike's directory structure he uses for his Visual C++ projects, but I use a slightly modified structure. Either way can be a pain to set up, so I thought I'd write up some instructions how to get my structure working. I'm using Visual Studio 2012, but it should be the same in 2010 as well. Also, I only use two configurations: Debug and Release. If you ever want to add a Profile configuration you should be able to follow these instructions to configure it similarly.

Be sure to buy Mr. Mike and Rez's book, Game Coding Complete 4th Edition. It's an epic tome of game programming knowledge written by proven veterans of the AAA game industry.

Let's get started..

Here's what the folder structure will look like:

MyGame/       -root folder
  Assets/     -raw image and sound files (files NOT used by the game)
  Docs/       -documentation files
    Internal/ -internal docs
    External/ -external docs
  Game/       -game executables and data (images/soundFX/music/etc)
  Lib/        -game engine library files
  Source/     -code root folder (no files go directly under Source)
    MyGame/   -code files for MyGame project
      MSVC/   -solution/project files for MyGame
  Temp/       -temporary linker/build files

* Open Visual Studio
* File -> New -> Project
* Visual C++ -> Empty Project
* Name your project, click OK
* Create the folder structure above within Windows Explorer

At this point, the solution file is saved in the root folder, but we want it under the Source/MyGame/MSVC/ folder. Select the Solution in Solution Explorer, then click File -> "Save MyGame.sln as...". Save it under the Source/MyGame/MSVC/ folder. This will save the .sln and .suo files without breaking the references.

* Close Visual Studio (save if prompted to save).
* Open the new .sln file under the MSVC folder.
* You may now delete the .sdf, .sln and .suo files in the root folder.
* Right click MyGame project -> Properties

* Select "All Configurations" from the Configuration drop-down at the top.
* Select Configuration Properties -> General on the left.
* Set Output Directory to: $(SolutionDir)..\..\..\Game\
* Set Intermediate Directory to: $(SolutionDir)..\..\..\Temp\$(ProjectName)$(PlatformName)$(Configuration)\

* Select "Debug" from the Configuration drop-down at the top. (Save changes when prompted)
* Set Target Name to: $(ProjectName)_d

* Select "All Configurations" from the Configuration drop-down at the top.
* Select Configuration Properties -> Debugging on the left.
* Make sure Command is set to: $(TargetPath)
* Set Working Directory to: $(OutDir)

* Select C/C++ -> Precompiled Headers on the left.
* Make sure Precompiled Header Output File is set to: $(IntDir)$(TargetName).pch

* Select C/C++ -> Output Files on the left.
* Make sure "ASM List Location", "Object File Name", and "Program Database File Name" are all set to: $(IntDir)

* Select Linker -> Debugging on the left.
* Make sure "Generate Program Database File" is set to: $(OutDir)$(TargetName).pdb
* Set Map File Name to: $(OutDir)$(TargetName).map

Now you can set up your filter folders under the project to mimic our folder structure above.
* Delete Header Files, Resource Files, and Source Files under the MyGame project.
* Right-click MyGame project -> Add -> New Filter. Name it "Assets".
* Keep doing this until you have the same structure, minus the Game and Temp folders, since those are the output/intermediate folders. Basically, you want anything that would belong in Source Control in your project.

Last step is to move the project files the default location of MyGame/MyGame/ to MyGame/Source/MyGame/MSVC/
* Close Visual Studio
* Move all the files under MyGame/MyGame/ to MyGame/Source/MyGame/MSVC/
* Open the .sln file in a text editor. (located at MyGame/Source/MyGame/MSVC/MyGame.sln)
* Change the path to the vcxproj file from "..\..\..\MyGame\MyGame.vcxproj" to "MyGame.vcxproj"
* Save .sln file.
* It's now safe to delete the folder MyGame/MyGame (which should be empty now).
* Done!

------------------------------

References
Game Coding Complete 4th Edition
How to relocate a Visual Studio project Solution (.sln) file

4 comments:

  1. Hello.

    I was wondering if when adding new .cpp or .h files they should end up in /Source/Mygame/

    At the moment they are created in /Source/MSVC/

    ReplyDelete
    Replies
    1. How are you adding new files? When you right click in Solution Explorer and select "Add -> New Item", it gives you a "Location" field to select the location of the new file.

      Delete
  2. Was wondering the same as well, when i use the method above to create a new file the default "location" is ../Source/Mygame/MSVC. Is that intended or should only the project files be in the MSCV? Which would make sense if more build platforms where to be added, they would each have their own directory with the source files kept in /Source.

    ReplyDelete
    Replies
    1. If you look at the example code used in Game Coding Complete (TeapotWars in the 4th edition of the book), you'll find that it has the same behavior. It looks like the default location for new files in Visual Studio is the same location as the project file. You have the chance to select a different location when you right click in Solution Explorer and select "Add -> New Item".

      Honestly, the code in Game Coding Complete isn't the greatest for cross platform development even though they talk about "only needing to change the Application Layer". Like you say, it would be ideal to not place non-MSVC related code in the MSVC folder, but again looking at the TeapotWars solution, you can see that they have a few code files there (not much though). stdafx.h is the precompiled header. They also have the main game h/cpp files, but that's about it. The reality is that writing truely 100% cross platform code would involve quite a few additions/changes to Game Coding Complete's code and in some cases may be very difficult. For example, when using Xcode to target iOS, you'll more than likely need to use at least some Objective-C. You'll also need to add plenty of #if defined(WIN32) directives. There are a lot of Windows functions that also aren't exactly standard C++, but rather vendor specific extensions (they have the same name as the standard C++ function, but begin with an underscore), so you'd most likely need macros to convert the names.

      If you're really concerned about cross platform development, you're probably better off selecting some existing tools that specialize in it, such as Unity or MonoGame. Otherwise, I'd say do a Google search on how to write cross platform C++ code in Win32.

      Delete