Installing Simple2D and Compiling your first application
Simple2D is my graphics library for students and beginners looking to experiment with drawing 2D graphics in C++ in the simplest possible way. It is based on the Direct2D API but removes almost all of the complexity. This article contains instructions on installing the software and all of the extras it requires, and shows how to compile your first program using Simple2D.
System Requirements
Simple2D is based on Microsoft Direct2D therefore you require one of the operating systems listed below. Windows XP is not supported! Make sure your operating system meets these requirements before continuing.
Simple2D 1.06 and later:
- Windows 7 Service Pack 1 with Platform Update for Windows 7
- Windows Server 2008 R2 Service Pack 1 with Platform Update for Windows 7
- Windows 8
- Windows Vista is NOT supported
Simple2D 1.05 and earlier (no longer supported):
- Windows Vista Service Pack 2 with Platform Update
- Windows 7
- Windows Server 2008 R2
- Windows 8
Installation
Visual Studio
Visual Studio is required to compile Simple2D applications. The library is tested against the following versions:
- Visual Studio 2010 (compile with warnings)
- Visual Studio 2012 (no warnings)
- Visual Studio 2013 (no warnings)
If you don’t already have Visual Studio installed, download your preferred version for free:
- Visual Studio 2013 Express for Windows Desktop
- Visual Studio 2012 Express for Windows Desktop
- Visual C++ 2010 Express
It is strongly recommended that you use Visual Studio 2012 or 2013 where possible as the configuration is much simpler.
Windows 8 SDK (does not require Windows 8 to install or use)
Visual Studio 2010 users: Download and install the Windows 8 SDK so that Direct2D libraries are available in Visual Studio. See the section “Setting up Visual Studio 2010” in my Direct2D 1.1 Migration Guide for configuring the SDK paths correctly in your Visual Studio projects.
Visual Studio 2012 / 2013 users: When you perform the installation, ensure that you have chosen to install the Windows 8 SDK so that Direct2D libraries are available in Visual Studio. No changes to include or library paths on a per-project basis are needed.
Boost
Simple2D makes use of a common set of libraries called Boost to simplify many tasks. Simple2D has been tested against:
- Boost 1.47
- Boost 1.53
- Boost 1.55
First download the Boost sources and headers and unpack them into a folder such as C:\Program Files (x86)\boost.
Then download the pre-compiled binaries for Windows. Use the 32-bit versions even if you are running a 64-bit system, as Simple2D is designed for a 32-bit build. The EXE will create a folder of libraries. Move them to C:\Program Files (x86)\boost\boost_x_yy_z\lib32 where boost_x_yy_z is the version-named Boost folder you copied in the previous step.
The DirectX SDK is not required
The DirectX SDK is not required to use Simple2D, so you don’t need to download this.
Simple2D
Finally, download Simple2D itself from this web site and run the installer. This will install the libraries and header files you need to get working with Simple2D. If your system requires Platform Update for Windows 7, the installer will download and install it for you automatically.
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!