Installing Simple2D and Compiling your first application
How to compile applications with Simple2D
The following instructions will change the compilation settings for all current and future projects you create in Visual Studio. This saves you having to repeat these steps for every new project. If you want to change the settings for just your current project, simply right-click the project name in Solution Explorer and choose Properties, instead of editing the Property Sheets directly when indicated below. The windows shown are otherwise identical.
Visual Studio 2010 users: replace all references to $(WindowsSdkDir)
below with the paths described in my Direct2D 1.1 Migration Guide so that the compiler uses the Windows 8 SDK instead of the Windows 7 SDK which comes bundled with Visual Studio 2010.
All Visual Studio versions:
- Create a Visual C++ -> General -> Empty Project as shown in Figure 1. Call it Simple2D-Test, for example.
- Choose View -> Property Manager from the main menu.
- In the pane that appears, expand your project (Simple2D-Test), and one of the folders under this (either is fine), right-click on Microsoft.Cpp.Win32.user and select Properties (Figure 2).
- You must now set the folders where Visual Studio will look for Simple2D, the Windows SDK (which contains the libraries for Direct2D) and Boost. Click on the VC++ Directories tab, click the arrow next to Include Directories and choose Edit.
- Click the Folder-with-star icon near the top right of this page, and add the folders for the Windows SDK, Boost and Simple2D one by one. It does not matter what order they appear in. The list should look something like this (Figure 3):
$(WindowsSdkDir)include C:\Program Files (x86)\DJKaty.com\Simple2D\src C:\Program Files (x86)\boost\boost_1_55
Figure 3 (use the paths indicated in the installation instructions, not those indicated in this screenshot)
Note that there is no backslash between
$(WindowsSdkDir)
andinclude
. If you have installed Boost to a different folder, you must change the folder name here accordingly. If you are running on a 32-bit machine, the files will be in C:\Program Files instead of C:\Program Files (x86). Visual Studio may change the brackets into numbers like %28 and %29. Do not worry about this.If you installed a version of Boost other than the one pictured, change the version number in the folder name as appropriate.
- You now need to repeat the same process for the Library Directories item in the property manager. This time you need to add:Simple2D 1.13 and later, Visual Studio 2013:
$(WindowsSdkDir)lib C:\Program Files (x86)\DJKaty.com\Simple2D\lib\vc12\$(Configuration) C:\Program Files (x86)\boost\boost_1_55\lib
Simple2D 1.13 and later, Visual Studio 2012:
$(WindowsSdkDir)lib C:\Program Files (x86)\DJKaty.com\Simple2D\lib\vc11\$(Configuration) C:\Program Files (x86)\boost\boost_1_55\lib
Simple2D 1.13 and later, Visual Studio 2010:
A pre-compiled Simple2D library for Visual Studio 2010 is not provided in the installation package. You must compile the source code yourself.
Simple2D 1.12 and earlier, all Visual Studio versions:
$(WindowsSdkDir)lib C:\Program Files (x86)\DJKaty.com\Simple2D\lib C:\Program Files (x86)\boost\boost_1_55\lib
Once again, note there is no backslash between
$(WindowsSdkDir)
andlib
, and the same rules about changing the folder names apply as in step 5. - Now choose Linker -> Input in the left pane. Edit the Additional Dependencies item as you did in steps 5 and 6. This time you must add the following list of libraries on separate lines:
Simple2D.lib Effects11.lib d3dcompiler.lib d2d1.lib dwrite.lib d3d11.lib dxguid.lib xinput9_1_0.lib
These are the libraries for:
Simple2D (required)
DirectX 11 Effects Framework (optional; pre-compiled library included in the Simple2D installation package)
Direct3D Shader Compiler (optional; required for HLSL shader compilation functions eg.D3DCompileFromFile
)
Direct2D (required)
DirectWrite (required)
Direct3D (required)
Direct2D Effects Framework (required)
XInput (required; DirectX 11’s replacement for DirectInput).
Per-project linker input settings
Simple2D 1.13 and later:
No per-project Linker Input settings required.
Simple2D 1.12 and earlier:
You’re almost good to go for all projects, but there is one more thing you must do on a per-project basis and that is to include Simple2D itself. The reason this must be done per project is because I have supplied two versions of the library, one for Debug builds and one for Release builds.
- In Solution Explorer, right-click on the project and choose Properties. Choose Debug from the Configuration box in the top-left corner of the window, then open up the Linker Input window as in step 7. Add the following library (Figure 4):
Simple2Dd.lib
Note the ‘d’ at the end.
- Now change the Configuration box to Release, open up the Linker Input again and add:
Simple2D.lib
Per-project static link settings
The libraries are statically linked to the C++ run-time, so your projects must match:
- In Solution Explorer, right-click on your project and choose Properties. Choose Debug from the Configuration box in the top-left corner of the window, then in the left-hand tree pane, browse to Configuration Properties -> C/C++ -> Code Generation. Change the Runtime Library setting to Multi-threaded Debug (/MTd).
- Now change the Configuration box to Release, browse to the same item and change it to Multi-threaded (/MT).
….and you’re ready to roll! The only steps you have to repeat for each new project are the two bullet points above. The seven steps above only need to be performed once.
Hello, I’ve followed all of the directions specified and now when I run the program I get a syntax error stemming from simple2dlib.h referring to objToClone. The exact piece of code is
ObjectManager(ObjectManager const &o)
{
for (auto &objToClone : o.objects)
{
auto cloned = objToClone.clone();
cloned->SetDeleteBehaviour(SODB_Destroy);
objects.push_back(cloned);
}
}
It says that objToClone reference has not been initialized. How do I go about initializing this?
Hi Kaden and sorry for the slow reply. That sounds like a bug in the library. Which version are you using, and can you post the entire source code so I can take a look? The example code from the article is a simplified version of the ‘MSDN-Direct2D’ example in the examples folder of the installation. All of the examples are re-compiled and tested before each new version is released so it should work, so let’s have a look at your code. Thanks!
I am using the Simple2D 1.10 and I am running the code from your 2D Platform Games Collision detection. Even when I try the test code from the above article, I get the same syntax error. I haven’t made any alterations to the simple2dlib.h code. The error is on Line 2231 of simple2dlib.h . I am using Windows 7, and VS 2010. My source code is as follows for main
#include “C:\Program Files (x86)\DJKaty.com\Simple2D\src\Simple2D.h”
using namespace S2D;
class MyProgram : public Simple2D
{
// API entry points
bool SetupResources();
void ReleaseResources();
void DrawScene();
void UpdateObjects();
// The bitmap brush to use to draw the platforms
ImageBrush tile;
// The sprite to use for the player
Image player;
// The number of objects in the game world
int worldObjectCount;
// The geometry of all the landscape objects (platforms and walls)
Geometry *worldObjects;
public:
MyProgram()
{
// Load graphics
tile = MakeBrush(MakeImage(L”tile.png”), Custom, D2D1_EXTEND_MODE_WRAP);
player = MakeImage(L”stickman.png”);
}
};
// Geometry matrix transformations are render-target dependent as they rely on the window/screen size
// so the layout of the platforms in the game must be defined here
bool MyProgram::SetupResources()
{
typedef enum { Rectangle, Ellipse } ShapeType;
// This structure allows us to quickly define the layout of each landscape object
typedef struct {
int width;
int height;
ShapeType shape;
float scaleX;
float scaleY;
GeometryTransformPoint scalePoint;
float skewAngleX;
float skewAngleY;
GeometryTransformPoint skewPoint;
float rotAngle;
GeometryTransformPoint rotPoint;
int topLeftX;
int topLeftY;
} ObjectDefinition;
worldObjectCount = 12;
// Create the landscape definition
ObjectDefinition objdef[] =
{
// Gently sloping straight surface
{ 80*6, 80*1, Rectangle, .5f, .5f, PointBottomLeft, 0.f, -10.f, PointBottomLeft, 0.f, PointBottomLeft, 80, ResolutionY – 200 },
// Block to help climb up steep sloping wall
{ 80*1, 80*1, Rectangle, .5f, .5f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 0.f, PointBottomLeft, 250, ResolutionY – 60 },
// Steep sloping wall
{ 80*8, 80*1, Rectangle, .6f, .6f, PointBottomLeft, 0.f, 0.f, PointBottomLeft, 75.f, PointTopLeft, 240, ResolutionY – 250 },
// Edge walls
{ 80*32, 80*1, Rectangle, .25f, .25f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 0.f, PointBottomLeft, 0, ResolutionY – 20 },
{ 80*24, 80*1, Rectangle, .25f, .25f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 90.f, PointTopLeft, 20, 0 },
{ 80*24, 80*1, Rectangle, .25f, .25f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 90.f, PointTopLeft, ResolutionX, 0 },
// Gently sloping curved surface
{ 150, 40, Ellipse, 1.f, 1.f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 0.f, PointTopLeft, 460, 380 },
// Square blocks (‘stairs’)
{ 80*1, 80*1, Rectangle, .75f, .75f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 0.f, PointTopLeft, 20, ResolutionY – 80 },
{ 80*1, 80*1, Rectangle, .4f, .4f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 0.f, PointTopLeft, 20, ResolutionY – 110 },
// Two sloping platforms on left-hand side to give access to a long fall through a thin platform
{ 80*5, 80*1, Rectangle, .25f, .25f, PointTopLeft, 0.f, 10.f, PointBottomLeft, 0.f, PointTopLeft, 20, 200 },
{ 80*20, 80*1, Rectangle, .25f, .25f, PointTopLeft, 0.f, -15.f, PointBottomLeft, 0.f, PointTopLeft, 100, 150 },
// Very thin platform (to test fall-through)
{ 80*20, 80*1, Rectangle, .1f, .05f, PointTopLeft, 0.f, 0.f, PointBottomLeft, 0.f, PointTopLeft, 420, 250 }
};
// Generate the game world landscape geometry
worldObjects = new Geometry[worldObjectCount];
for (int o = 0; o < worldObjectCount; o++)
{
ObjectDefinition &obj = objdef[o];
if (obj.shape == Rectangle)
worldObjects[o] = MakeRectangleGeometry(obj.width, obj.height);
if (obj.shape == Ellipse)
worldObjects[o] = MakeEllipseGeometry(obj.width, obj.height);
worldObjects[o].SetTransform(
worldObjects[o].Scale(obj.scaleX, obj.scaleY, obj.scalePoint)
* worldObjects[o].Skew(obj.skewAngleX, obj.skewAngleY, obj.skewPoint)
* worldObjects[o].Rotate(obj.rotAngle, obj.rotPoint)
* worldObjects[o].Move(obj.topLeftX, obj.topLeftY));
worldObjects[o].SetAutoAdjustBrushTransform(true);
}
return true;
}
// Free up game world resources
void MyProgram::ReleaseResources()
{
delete [] worldObjects;
}
// Per-frame game world update / physics
void MyProgram::UpdateObjects()
{
}
// Render the game world
void MyProgram::DrawScene()
{
}
// Application entry point
void Simple2DStart()
{
MyProgram test;
test.SetWindowName(L"2D Geometry Collision Demo");
test.SetBackgroundColour(Colour::Black);
test.SetResizableWindow(true);
test.Run();
}
Ok, I copy pasted your code into a new project and linked it against Simple2D 1.10 and it worked correctly. I suspect the problem is you are using Visual Studio 2010 and the problem line in question uses a C++11 syntax which isn’t supported by Microsoft until Visual Studio 2012. This was my fault – that code should be in the Simple2D.cpp file instead of the header so that you don’t need Visual Studio 2012 to compile applications with Simple2D. I will fix it in the next version. In the meantime, you should be able to make it compile correctly by changing these two lines:
for (auto &objToClone : o.objects)
{
auto cloned = objToClone.clone();
to:
for (auto it = o.objects.begin(); it != o.objects.end(); it++)
{
auto cloned = it->clone();
in Simple2DLib.h. Let me know if that helps and thanks for the report!
That helped get rid of that syntax error. Thank you. Now I get an error that I’ve never come across before. fatal error C1900: Il mismatch between ‘P1’ version ‘20100826’ and ‘P2’ version ‘20081201’. I checked online and haven’t found many solutions except to reinstall my service pack. I did just that but it hasn’t fixed the problem. If you have any suggestions, I would really appreciate it. I feel like I’m so close to getting this to work.
I’ve never heard of that problem before, but there are some pages eg. http://connect.microsoft.com/VisualStudio/feedback/details/633817/fatal-error-c1900 where it is suggested that a static library compiled with /GL (whole program optimization enabled) requires the compiler tools which consume it to be of the same version as those which created it. I wasn’t aware of this… in Simple2D, the debug version (Simple2Dd.lib) is compiled without /GL but the release version (Simple2D.lib) is compiled with /GL. I will turn this off for future releases – in the meantime, can you try linking against Simple2Dd.lib and building with ‘Debug’ as the selected configuration and see if the problem goes away?
You get the same for example with VS2008 code vs VS2008SP1. The /GL will generate code at link time. Which means it has to compile, and if the code doesn’t match the exact compiler version, you’ll get these problems. Better avoid /GL for libraries in general.
Yep, I just posted a new version of the library yesterday and I have removed /GL altogether. I’ll accept my own inexperience with publishing C++ libraries to blame there…
Hey Katy, great work! I followed your steps but it seems it doesn’t work for me. I’m using VS C++ 2010 Express and tried to run one of the collision demos but I get like 30 linker errors (LNK2019 unresolved external symbol). Do you happen to know what would be the problem?
Can you post the linker output so I can take a look for you? 🙂
Ah right if you’re using 1.13 I need to update the installation instructions. You no longer need to add Simple2Dd.lib and Simple2D.lib as debug and release linker inputs – you can instead go to the global settings (properties manager) and add c:\program files (x86)\djkaty.com\simple2d\lib\vc11\$(Configuration) and it will automatically link to the correct library for both debug and release. Note though that the vc11 folder is compiled against Visual Studio 2012 (and vc12 for Visual Studio 2013) so you may get a version mismatch error (1600 vs 1700 or some such) – if so you need to recompile Simple2D itself and I’m not sure if you can under VS2010 anymore because it uses some C++11 stuff, but you can try. I recommend you upgrade to Visual C++ 2012 Express or Visual Studio for Windows Desktop Express 2013 if you are able to. If you need to recompile Simple2D for VS2010 and you get errors, post them and I’ll come up with some compatibility fixes.
I got VS Express 2012 but I still didn’t make it work. Downloaded boost 1.53 and simple2D 1.13. Unfortunately, I still get the 30 linker errors.
If it’s the same errors it is because it can’t find Simple2D.lib; did you change the linker input in property manager as I suggested above?
Sorry, I meant adding the above mentioned folder to Library Directories in VC++ Directories in Property Manager.
Yes, I added C:\Program Files (x86)\DJKaty.com\Simple2D\lib\vc11\$(Configuration) to Library Directories in VC++ Directories but I still get the errors. They are the same errors. Maybe I’m forgetting something, but I can’t find what.
I have just updated the article with new installation instructions for Simple2D 1.13 on various versions of Visual Studio. Can you please try again and let me know how it goes? Sorry for taking so long to reply, been sick.
Hi, I tried to install this today, but i don’t know how to include the library on the Visual Studio 2013, and was wondering if you could help me out? I downloaded your Tetris source code and tried to run it, and it appeared that I couldn’t open the #include . Thanks!