Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon Join the GFS Discord Server!
3. July 2012

A poster, JakobProgsch, over on the TigSource forums has made a series of OpenGL samplesopengllogo available on GitHub. Each example is contained in a single file, doesn’t depend on any non-canonical libraries or hide code behind a framework.  As a results the examples are a bit longer, but easier to follow.


The following topics are covered ( description taken from each file header ).


Sample 1

Skeleton code that all the other examples are based on


Sample 2

This example shows the setup and usage of shaders and a vbo+vao


Sample 3

This example shows the setup and usage of shaders and a vbo+vao
In this example the attrbute locations are set via BindAttribLocation
instead of layout qualifiers in the shaders


Sample 4

same as the "Shader and VBO" example, only with an indexed vbo.


Sample 5

apply a texture to the fullscreen quad of "Indexed VBO"


Sample 6

set up a perspective projection and render a rotating cube


Sample 7

render the cube from the perspective example to a texture and

apply fxaa antialiasing to it.


Sample 8

create 8 instances of the cube from the perspective example

with an additional offset buffer and AttribDivisor


Sample 9

create 8 instances of the cube from the perspective example

the difference to the instancing1 example is that we are

using a texture buffer for the per instance data instead of a

vertex buffer with divisor.


Sample 10

create 8 instances of the cube from the perspective example

the per instance data is passed with a uniform buffer object


Sample 11

Uses a geometry shader to expand points to billboard quads.

The billboards are then blended while drawing to create a galaxy

made of particles.


Sample 12

This example uses the geometry shader again for particle drawing.

The particles are animated on the cpu and uploaded every frame by

mapping vbos. Multiple vbos are used to triple buffer the particle



Sample 13

This example simulates the same particle system as the buffer mapping

example. Instead of updating particles on the cpu and uploading

the update is done on the gpu with transform feedback.


Sample 14

This example renders a "voxel landscape/cave" from the view of a

moveable camera. Occlusion queries and conditional rendering are used

to cull occluded parts of the world and timer queries are used

to measure the performance.


Hope he keeps them coming, good OpenGL resources are scarce.


Edit: 7/5/2012 Fixed links and added 14th sample, which by the way, is a bit of a monster.


18. May 2012



You often hear people say things like “It’s memory management that makes C++ difficult”, this is patently false.  What makes C++ so incredibly difficult for new users ( and experienced ones! ) is the complexity.  Note, I didn’t say difficulty, I said complexity.


Today was one of those perfect examples, one of those experiences that I want to point at and say “THIS IS WHY YOU SHOULD LEARN WITH A DIFFERENT LANGUAGE!”.  Yes, that was shouting. 



Here’s the story.  Today I had a reader request guidance on how to make a release build of my C++ tutorial.  This was a really good request, so I put it together in tutorial form.  Something quickly dawned on me… I’ve never built this tutorial in release mode.  It was pretty obvious that I hadn’t because, well, it didn’t work!   Oops, my bad.



Let’s take a look at what happened, I want to see if you can guess what the problem is.  A gold star to whoever gets it before I reveal the answer at the end of this post.  That gold star offer applies to C++ veterans and new developers alike, this is a somewhat tricky one, especially considering the starting point!



So, I was building Pang 9 ( my Pong tutorial ) in release mode.  I downloaded the project files from here if you want to follow along at home.  I simply extracted the project, then I downloaded the full SFML 1.6 SDK, imported the Visual Studio 2008 project into Visual Studio 2010 and compiled the DLLs for multithreaded release mode, and copied those DLLs into my release directory.  Code compiles just fine under release, so then I run it ( within Visual Studio ) and:





Uhoh… this can’t be good!  The offending line of code is right here:


sf::Music* SoundFileCache::GetSong(std::string soundName) const { std::map<std::string,sf::Music *>::iterator itr = _music.find(soundName); if(itr == _music.end()) { sf::Music * song = new sf::Music(); if(!song->OpenFromFile(soundName)) <----- EXCEPTION IS HERE { delete song; throw SoundNotFoundExeception( soundName + " was not found in call to SoundFileCache::GetSong"); } ..... SNIP ......

Well, there is a big bad pointer right above the line, that seems an obvious candidate doesn’t it?   Well don’t waste your time on that train of thought, the pointer isn’t the issue.  The .find call seems a likely candidate too, it isn’t though.


Hmm, lets start looking there, ill set a breakpoint and trace into the _music.find() call.  Oh yeah, this a release mode only bug, so our hands our tied on the debugging side of things… great.  Alright, lets take a closer look.





<Bad Ptr>.  Oooh, that’s not good.  Obviously I’ve done a bad allocation here somewhere, but that just doesn’t make sense, not with the way this data type works.  In fact, the only place these pointers are even allocated is in this actual method, and we haven’t even got to that code yet!


For some reason, our empty map ( it hasn’t been used yet, this is the first call ), is returning a Bad Ptr instead of working as expected.  This can’t be right?  But wait… that’s a red herring anyways, isn’t it, after all our exception has nothing to do with the Bad Ptr or the std::map at all, this is just the IDE sending us on a wild goose chase. We can literally boil it down to exactly two lines of code:


sf::Music * song = new sf::Music(); if(!song->OpenFromFile(soundName))


Now we are getting somewhere, we have isolated our reproduce case down to exactly a two line program!  Hmmm, both are just standard SFML calls.  Short of being out of memory, nothing here should be able to fail.  So then, what the hell is causing this problem???



// ANSWER TIME, got your guess yet?  Did you get it right and I owe you a gold star?  Let’s find out!



Well, long story short, it’s our SFML dlls built for Visual Studio 2010.  See, this is our first method call into any SFML DLL and it’s causing an explosion… but, why the heck is that???



Well, lets take a trip over to the SFML 1.6 project and check out how I built them.  All I did was download the SFML 1.6 project for Visual Studio 2008, delete all the examples and set my build mode to Release DLL and compiled everything.  Hidden between the various warnings was one very very very important but cryptic message:




A bunch of warnings as a side effect of the import process, and one extremely important warning… the ahah moment if you will:


Warning    8    warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library    C:\temp\t\SFML-1.6\build\vc2008\LINK    sfml-graphics


Say what?



Now here is the thing, with C++ libraries need to be IDENTICAL.  You link debug to debug, multithread to multithread, etc…  At the same time, you have to link Visual Studio 2010 binaries to Visual Studio 2010 binaries ( although not all versions are binary incompatible, VS 2005 and 2008 could share libraries I believe ), and this here is the source of all the trouble!


The sfml-graphics graphics library statically links to a lib called freetype, and this library isn’t compatible with Visual Studio 2010.  Download the newest version, extract it into the extlibs folder, recompile and PRESTO, problem solved.




So there you go, an unhandled exception in a call to open an audio file ended up actually being caused by a statically linked font library in a 3rd party graphics dll!



Still think it’s memory management that makes C++ tricky for beginners?  The biggest problem is, new developers run into this stuff almost immediately.  You need to conquer so much to get up and running with C++, the linker being one of the earliest and most daunting obstacles.  I honestly don’t expect anyone with a few months of programming experience to have been able to puzzle this one out.  It’s exactly these kinds of things that make people throw up their hands saying “Oh screw it, programming is too hard!”.  I don’t like a quitter, but frankly in this case… they are right!



Of course, this is by no means limited to C++.  I have had very similar experiences with Java where some XML file feeding another XML file feeding a code generator called by Maven puked out a message code like “Error, unknown problem”.  Thing is, with no other programming languages do you encounter this kind of problem until you are ready to deal with the complexity.  In C++, you start dealing with this crap on day 1.


So this, is one such reason why I say C++ isn’t a beginner friendly language!


18. May 2012



At the request of a reader I put together a guide on how to build your SFML 1.6 project in release mode, then how to package it for distribution.


It’s part of the main tutorial series, but can be accessed directly here.  If you are looking into how to distribute a release version of your SFML game, be sure to check it out



In doing so, I ran into a NASTY C++ related problem, which I will mention in a post shortly!


4. May 2012



I have to say, Nathan does quick work, much faster than I do! Smile


As I said in this earlier post Nathan volunteered to port my SFML C++ tutorial to SFML 2.0.  Well, he is done.  So in addition to the 1.6 projects, you can now download source for SFML 2.0.


Keep in mind, the instruction are still for 1.6, that hasn’t changed, but you can now see code for either version.  Shortly I will edit the tutorial posts themselves to include the new download option, but for now you can download the 2.0 version sources here.  Part 8 does not include the FMOD sources, which were for demonstration purposes only and made the project quite a bit larger.


GameFromScratch C++ Edition SFML 2.0 downloads:

Part 6

Part 7

Part 8

Part 9



While he was porting, he ran into and reminded me of a bug that I encountered and Ninja-edited for future versions.  You may find that on occasion the ball disappears completely, at least in versions before part 9.  That was because GameBall used a counter from when the game started, instead of when play started.  So if you took a while to start the game, the ball could actually move on its own ( and off screen ).  The fix was a fairly simple flag I added in part 9.  For the fix details, take a look at the first few lines of GameBall::Update().


Thanks again Nathan.


2. May 2012



Nathan, a reader of this site, has graciously agreed to provide his port(s) of the Game From Scratch C++ tutorial from 1.6 to 2.0.  He started with chapter 6, as prior chapters mostly involved simple changes in property/method names, as SFML has moved to CamelCase.  Chapter 6 is the first chapter where more intensive changes needed to be made.



So if you are following along my C++ tutorial, but would prefer to work with SFML 2.0, download these projects instead.  Nathan has promised to make each following chapters code available as well, and once I am up to current, I will edit in the original tutorial posts so you have an option of downloading either 1.6 or 2.0 project versions.


Sometime in the future, I will put together a post on what is required to port from 1.6 to 2.0, I promise… sometime. Smile


This project is exactly like the 1.6 ones.  It is preconfigured ( with SFML 2 release candidate ), so  all you have to do is download, unzip and open the solution in Visual Studio.


Download Pang6 here.   Will update as further chapters become available.


Thanks Nathan.


EDIT: Link fixed.


See More Tutorials on!

Month List