Phil Hassey - game dev blog
Phil Hassey as Rambo
".. I've been there,
I know what it's like,
and I'll do it again if I have to."

Archive for the 'C++' Category

FEAR THE PANDA

Tuesday, March 2nd, 2010

Yeah, you better …

pandapromo

This image represents the last few days of work.  I have ported some 3000 lines of python code to tinypyC++ code.  Fear it.

-Phil

VisualC++ 2008 for gcc / SDL people

Thursday, January 14th, 2010

I’ve had to port a project to VC 2008 from my usual mingw setup.  Here’s what I did.  This covers quite a few things, in particular where to find common GCC settings in VC land.

Setting up the project
  1. New project
  2. Type: win32
  3. Template: Win32 console if you want a console.  Or just Win32 Project if you don’t want the console to pop up.
  4. Choose a name for your project
  5. The solution name is the folder everything gets put in.  In VC, you can have several projects within a single solution.
  6. On App settings, I choose Windows application, Empty project

Adding the source files

  1. I right click on my project name and Add>New filter, and use that to bundle a few different sets of source files together
  2. This is done so that VC will compile the files, but keep in mind that the separations don’t seem to mean anything to VC.  It’s just for your visual convenience.
  3. Note that if there are two files with the same base name “dostuff.c” and “dostuff.cc” this will cause VC to choke during compilation as it will create two .obj files with the same name and fail during linking.
  4. VC doesn’t support C99 so any C99 files will have to be Right Click > Properties > C/C++ > Advanced > Compile as > C++ Code

Note: a Project can have a number Configurations (Debug, Release ..) If you are targeting a certain distribution site with some special requirements you can add more Configurations via Solution > Properties > Configuration Manager >

Compilation Options

Adding some project compile time options (mainly -D and -I equivalents for specific defines and include folders.)  Keep in mind which Configuration you are modifying at all times.  You can also switch to “All Configurations” for global changes.

  1. (GCC -I)  To add in include folders, Project > Configuration Properties > C/C++ > General > Additional Include Directories > … I found with include folders you often need to stack on a few ../../’s to get it to point at what you want it to point at.  (In my project I’ve got my VC project separate from all my source code, so everything is up and over a few folders.)
  2. (GCC -D)  To add in defines, Project > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions > .. I often add a few of these to tell the code what build we’re doing.  (iPhone vs, Windows, vs Linux, etc)
  3. (GCC -W)  Although warnings might be useful .. They made it impossible to see the errors.  You can turn down the warnings from level 3 to 1 in Project > Configuration Properties > C/C++ > General > Warning Level >
  4. (GCC -l)  Linked libraries are probably going to be needed.  Again in my project I’ve often got a few ../../’s in the paths.  Here’s where you add this stuff: Project > Configuration Properties > Linker > Input > Additional Dependencies >
  5. .. an alternate way is to add #pragma comment(lib,”MyLib.lib”) into your main.cpp.  By doing this you can refer to a lib right within your source if you prefer.
  6. For whatever reason, you may need to exclude some libs from being compiled into your project.  (If you see errors like “_tolower already defined in MSVCRTD.lib, etc).  You can exclude a lib in Project > Configuration Properties > Linker > Input > Ignore Specific Library > .. (in my case, I had to add msvcrt.lib and libcmt.lib)
  7. For my project I needed to expand the size of my stack, due to having excessive amounts of static data compiled into the project.  You can do this via Project > Config Properties > Linker > System > Stack (Reserve&Commit) Size > .. I changed them both to 4000000 and things worked.

Final packaging

I found that running right from VC didn’t always work. (Maybe I should find out why, but I haven’t.)  But I can run from the command-line.  Before doing so, I needed to:

  1. Copy in required dlls from SDL, etc
  2. Copy in game data files
  3. Copy in the VC Runtime files from: C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT
  4. For a release build I’m sure to build against SDLmain_nostdio.lib so that the game won’t crash Vista/Win7 where writing out stdout.txt and stderr.txt may result in some kind of fail.

Saving to Your_Favorite_Version_Control_System

  1. You may leave out all your ‘Debug’ folders or other Configuration folders, those seem to only contain generated files.
  2. You may leave out the .ncb and the .COMPUTER.username.blah file, they seem to be used by VC for stuff that can be regenerated, etc.

Hopefully this will be helpful to anyone who does the same thing.  Between you and me, I found the install of VC 2008 to be pretty nice in comparison to trying to install mingw, which tends to require considerable muscling.  Also, since everyone provides their libs as MSVC libs, building against those vendor libs is not only easy, but possible.  (With mingw, many MSVC C++ libs are unusable due to binary incompatibility.)

-Phil

P.S. Adding an Icon to your Project

This is pretty easy.  Add a Project.rc to your project.  The Project.rc should include this line:

IDI_PROJECT       ICON         “path/to/icon256.ico”

You can create an Icon with IconFX .

P.S.S. Creating a windows Installer

I’ve used Innosetup in the past.  It seems to work.

P.S.S.S. Dealing with LPCTSTR conversions

If your whole project is using C-strings, you can go into Project > Properties > Configuration Properties > General > Character Set > and change it to “Not Set” so that VC will no longer try and use Unicode strings to some of its functions (like ShellExecute.)

P.S.S.S.S. Dealing with conflicting .obj files

If you get a linking error about a bunch of undefined symbols and you have say “game.c” and “game.cpp” in your project, they are both being compiled down to “game.obj” .. To set one of them to compile to a different filename, say “mygame.obj”, you need to Right Click > C/C++ > Output Files > Object File Name > “$(IntDir)\mygame.obj”

Alternately, you can just rename some of your source files so there are no name conflicts by default.

Exceptions and various compilation option speeds

Wednesday, January 6th, 2010

So after some discussion yesterday with a few folks about Exceptions in C++, I decided to add a compilation flag to disable the use of exceptions in tinypyC++.  The claim given to me was around these few items: 1. Exceptions slow your code down.  2. Exceptions aren’t cross-platform (in some cases).  3. Exceptions can easily add in memory-leaks.

I can’t argue with #2, although it’s not completely relevant for the platforms I’m targeting right now.  #3, with the way tinypyC++ does reference counting, I wouldn’t be surprised if this could happen.  #1, I figured might be true, but I wanted to know how real #1 actually was.  So here are my results:

g++ flags ms
(none) 894
-g 891
-O3 414
-DTP_NO_THROW -fno-exceptions 894
-g -DTP_NO_THROW -fno-exceptions 878
-O3 -DTP_NO_THROW -fno-exceptions 378

In the first test I’m rendering a Julia fractal for 20 frames and averaging the time it takes to render. In the second test (below) I’m doing the same test, but I did one code optimization to remove a new/delete that was happening for each pixel – by reusing the coordinates object.

g++ flags ms
(none) 293
-g 290
-O3 153
-DTP_NO_THROW -fno-exceptions 304
-g -DTP_NO_THROW -fno-exceptions 303
-O3 -DTP_NO_THROW -fno-exceptions 112

I found it interesting that in the 2nd test case I did, removing exceptions actually increased the time it took to render the fractal!  Not quite sure why .. hmmm.  (I tried it a few times and kept getting similar results too.)

What is clear in the second test is that the compiler is able to optimize the code much more aggressively.  I guess Exceptions choke up the optimizer.  In all cases it seems that adding on the -g flag seems to make the code run slightly faster, surprisingly enough!  (-g adds in debugger info.)  In all cases, the optimizer was able to roughly double the speed of the program, and when I disable exceptions it is able to throw in even more speed.

-Phil

Seahorse Adventures – Loading TGAs (and more)

Tuesday, January 5th, 2010

Here’s a screenshot.  This is my Ubuntu desktop of my work on a new iPhone game.  Take a look at it nice and big.  Below I’ll explain all of what is going on …

bsa_dev1

In short, I’m working on porting my pyweek#3 team entry to the iPhone.  Here’s the details:

  • Top left, you see Kate, my text editor.  You’ll notice I’ve got -what appears- to be python.  But that’s actually tinypyC++ code!  My converter still has some rough edges, but it’s starting to get pretty good.  If you weren’t looking to closely you might mistake that for ordinary python code!  (In fact, it should be 100% python parser friendly.)  You can see how I have to use a touch of annotation to make it all go.  But for the most part, the types get inferred.  (The C++ outputted is about 2x as long, so I’m saving a ton of keystrokes!)
  • Top right, you see the level being painted.  It isn’t all working yet, but those are the basic tiles.  Interesting bit about loading the level, when I created this game I used my own level editor and library I made with pygame.  To save on disk and time I save all the levels to .tga files.  Since I’m targeting the iPhone I need an easy way to read the data from .tga files.  I don’t have SDL_image available, so a friend pointed me towards this great site.  It appears to have some awesome bits of code, including very simple and easy to use image loader that can load tga/png/jpg/etc.  I think it has the most painless interface I’ve ever seen for a C-based image-loader.
  • Bottom right you can see me working on tinypyC++.  As I’m working on BSA, I’m always finding new bugs in it.  Lately most of the bugs have been of the “add more graceful error handling” nature.  tinypy will point out what line (and character) an error happened on, but with a bit of extra work I’m able to add in some coherent error messages to tell the user what is going on.  In this case it was to inform the user of an undefined module name.
  • Bottom left you can see the startup of the game.  I’m using irrKlang for all my  game audio now.  It is not open source, but it’s “free” to use for free games, and the price is quite reasonable for commercial projects.  irrKlang is not portable to the iPhone yet, so I have to have a separate driver for my iPhone audio.

And that’s it for today’s report!

-Phil

Elephants! First python + pygame game submitted to App Store :)

Saturday, December 19th, 2009

So, I managed to submit a “python” app to the App Store — “Elephants!” Here’s a few crazy things I had to work out to get things going:

  • I found that ObjC doesn’t care much for C++ objects that do their own reference counting.  To have a “Game” object at all, I had to use a pointer to my object instead of using my reference counting object.  It seems that during some of the “magic” of ObjC it copies objects without calling any of the C++ copying methods, so reference counting gets killed.
  • I ported a basic subset of pygame to tinypyC++ and made it work under openGL.  I don’t have this available in my tinypyC++ repository because it takes a good deal of prep-work to use (isn’t out-of-the-box) so it wouldn’t be generally useful to anyone yet.
  • I was able to get pause on call / resume working by saving the game state to a text file.  Since tab-separated files are so easy to create and parse in python this seemed to be the easiest route to getting the project done.
  • I learned a ton about C++ templates.  I still haven’t even touched the tip of the iceberg, and I’m not sure I want to, but at least I’m getting the basics down and that seems to be enough for me to stumble through this project with.

You can check out my current progress at svn://www.imitationpickles.org/tinypy/branches/tinypy2 (in the tinypy/example folder you can check out a julia fractal demo.  It also includes a mini pygame module that depends on SDL not OpenGL, so it actually works out-of-the-box.)

Here are some things I’ve been adding this week in preparation for my next project:

  • I’ve added “weak pointers” .. Since reference counting has the problem of cyclic references never getting collected, I had to do this for some use-cases.  In tinypyC++ weak pointers are just normal pointers, and if the object is collected, the pointer will be invalid, so be careful.  They are created by doing stuff like “x = ptr(GameData())”
  • I’ve added more C style types.  I’ve got uint16, cstr, Array, etc.  This all makes it possible for me to have a different kind of object – a struct in tinypyC++.  The struct is meant to be a C oriented type that can be saved directly to disk.  In Elephants I serialized via saving CSV data.  In the future, if I keep all the game state in C struct type data, I can just use a single simple line like “open(‘data.txt’,’wb’).write(struct_dump(game_data)”
  • I found that STL extensions include a “hash_map” which turns out to be about 2x as fast as the normal “map”.  I’m using that now.

I’m not entirely sure how useful this project is going to be for anyone else, or even for me for that matter.  But I think after my next game project I’ll have a much better feel for what the situation is.  TinypyC++ has some real tradeoffs in terms of being a bit of a hybrid of C++ and of python.  It doesn’t offer the full power of either language.  But I’m doing my best to capture a middle ground between them that will make my life easier.

Restricted tinypy to C++ compiler

Tuesday, December 1st, 2009

elephant1I’ve spent the last week working on a tinypy to C++ converter.  It works!  See the screenshot to the right – I’ve managed to port a pygame game over to C++.

Here’s how (and some of the catches):

  • I require type annotation of all the functions and methods.  “def test(x:str)->str: return x”
  • I do two passes on each file, the first pass to catch all the function types and class members, and the second pass to generate the code.
  • I generate C++ code that has automagic reference counting.  So you have to code your script so it won’t have any cyclic references if you want garbage collected for you.

How is this different from shedskin (really cool project!)?

  • Built-in reference counting, instead of using libgc.
  • I require the user to type annotate everything.
  • It only supports a subset of the tinypy subset of python.  Shedskin supports a much larger subset of python.

So what’s the point?

  • Well, I learned a lot about STL and C++.
  • I know it will produce iPhone friendly code, I’m pretty sure libgc isn’t iPhone friendly?  (At least, I haven’t found anything via a few searches…)
  • Way less magic.  Since everything is annotated, there are no surprises.
  • Implementing C++ modules is pretty easy – the code can be inlined within the python code and it just works.
  • This will make it easier for me to develop C++ games.

Anyway, if you’re super brave, you can check out svn://www.imitationpickles.org/tinypy/branches/tinypy2 .. I don’t have the elephants example in there, but the pygame.py that I include gives you a pretty good example of a complex module.

I’m going to chat with the tinypy folks to see if we’ll merge this into the tinypy trunk or have it as a separate project.  I’m not quite sure what makes sense to everyone else 🙂  The nice bit about merging this in is that I could unify the test suites nicely.  And tinypy would still function as normal, just better tested, and with function annotation parsing supported.  All that said, I should make a tinypy module for tinypyC++ so that I can do some code evals!

Galcon Flash has arrived ..

Tuesday, June 23rd, 2009

So .. Yeah.  Check it out.  Realtime multi-player game .. in flash!

So on a more technical note .. The game involves quite a number of technologies!

  • AS3 – of course – for the Flash client itself
  • C++ – for the server
  • PHP – for the web API and rankings system
  • python – for the bots

If I did the project over, I’d probably do the server in python as well.  But all in all the project went pretty well.  After things were done, it only took me a morning to write up the python client.  There’s a very good chance I’ll release that code in a few weeks and let people try making bots for the game.

Anyway – have fun checking it out.  I’m a bit wiped out from wrapping all this up, so I’ll try and post some more interesting details later!

-Phil

Beast for the iPhone / iPod touch

Monday, February 23rd, 2009

Today marks the release of my first iPhone game release in five months.  It’s about time I got going on dev again.  I’ve been in the midst of many transitions in both of my businesses, so I decided to start out with something short-n-sweet.  The game is called Beast.

Beast is a re-make of an ancient MS-DOS game that I played ages ago (and I’ve re-made several times).  The original is here.  You may have followed my progress on twitter.  Here are some interesting “factoids” about the game dev.

The original game is pretty tough to play, if you have dosbox and have all the proper slow-down settings going, you’ll find it can be a challenge to surround the beasts and crush them with blocks.  The iPhone doesn’t have a D-Pad, so moving a character around the screen is a bit tricky.  I had to make several modifications to the original game so that it would work with the touch interface:

  • You can move the player around the screen by pushing your finger around.  However I had to make the player unable to move diagonally as I found the resolution of your finger movement made it near impossible to play and quite jumpy if I allowed diagonals.
  • You can touch any point on the screen and warp to that location.  The controls were still a bit tricky, so I had to add this feature so you could do quick movements to escape from a beast, or to attack it quickly.
  • I disabled the beasts so they can’t move diagonally, made the super beasts spawn into only 4 instead of 8 normal beasts, and slowed down the beast movement by about 50%.  These changes were all required to make the game playable.

All these changes turned an impossible to play on the iPhone into a reasonably fun diversion.  I also added a notable element of speed into your score.  This gives the player a good bit of replay value, since even after they beat a level, they can try again and try and “master” a map they are playing.

Overall the main point of this whole exercise was to get myself back into iPhone dev.  Really last year when I worked on Galcon I threw myself violently into that project for 3-4 months and came out a bit burnt out.  I knew this time I needed to do a small scale project to get going again.

I’m pretty pleased with the results of this project.  For some more interesting factoids about the game project, check out my game blog announcement.

Excuse to post this to the python planets: I actually made a python version of this game a few years ago.  It contains some really bizarre pure python sound-synth for both the music and sound effects.  Check it out.  Not only all that, but the whole game is a single 18k python file!  I ported from this code for the iPhone version.  Again, another great example of python prototype -> C++ final product!

(Silly side-note, actually the python code was a port of some C++ code I made ages ago.  So this is a great example of C++ code that was ported to python and then later ported back to C++.  I won’t bother you with details about my even earlier Java and C versions of the game.)

From python to C++

Saturday, February 14th, 2009

I think I wrote an article a few months ago comparing C and python.  I’m doing some new iPhone games now, and this time I’m using C++.  I found not having classes was getting to be too painful for me, so I’m trying out C++.  Here are a few things I’ve observed:

(As a side note – I’m writing my games /almost/ the same way I’d write them in python.  So if you’ve seen my game code, I’m doing almost the same stuff, except in C++.)

  • As usual, not having memory management is a bit of bother.  Fortunately, I’ve found there are only a few places where I actually need it.  The main thing I had to do was write some basic reference counting code for my state engine class.
  • I’m using structs instead of classes, since I have no use for private variables or methods.
  • Having separate .h and .cpp files is a drag.  But them’s the breaks .. It sure would be swell if C++ was a wee bit smarter about that.
  • For my in-game objects, I just define a single struct that has all the possible variables I need.  This is pretty much how I did things in python anyways.  I’ve never been a big fan of using much inheritance.
  • For writing my GUI classes, in python I was able to get lots of magic into them.  In C++ it isn’t so easy to get magic, so I’m doing without most of it.  It seems that most of the magic was gold-plating anyways.
  • Having written all this code in python before, getting clean C++ code seems pretty natural.  I know what I WANT my code to look like, so I try to get my C++ to be as close as possible to that. 
I think the bottom line of this post, is that my code is coming out pretty pythonic.  Maybe?