SFML in Xcode 5

I followed the official SFML tutorial for getting SFML programs to compile in Xcode, but kept failing to build because none of the libraries were linking.


I fixed it by manually adding them in the “Linked Libraries and Frameworks” section of the project settings (or whatever that panel is called).

Screen Shot 2013-12-17 at 6.28.45 PM



Posted in Programming | Leave a comment

Chocolate Quest

I’m trying to get better at C++ and learn the SFML framework, and my wife likes chocolate. What follows makes perfect sense.

Chocolate Quest Part One is a small and very simple game I wrote for my wife to play. Instead of a “You Win!” screen, there’s a screen revealing the location of some chocolate. Win-win. The arrow keys move a poorly drawn avatar around a poorly drawn version of my back yard, and the goal is to pick up the poorly drawn eggplants. I’m not an artist.


Github repo:


I’m not going to do a line-by-line breakdown, but here’s some notes.

C++ and OOP

The big reason to learn C++ has been to get a grip on OOP and other common programming principals. I made an attempt at writing some useful classes for the game. There is a base class called Scene, and two child classes called Intro and Game. Intro was poorly named. It should have been called CutScene or something. An instance of the Intro class takes a pointer to the game window, and an image that should be big enough to fill the entire window. When invoking the Run method, it shows this image on the screen and waits for a press of the space bar (and then ends itself). I used this for the intro screen, the instructions screen, and the end screen. This was a OOP win – I’ll be able to re-use this class in other little games when I use SFML.

The other child class, called Game, takes a pointer to the game window, a location of a background texture, location of a player texture, and the location of the items to pick up texture. The constructor for the class creates all the sprites from the textures, and sets their starting location. The Run method shows them all on the screen and handles the key motion to move the avatar around. Every key press also fires off a collision detection function (Pickup) that polls the player position and all the pickup-able items and sees if they intersect. If so, the pickup-able item that’s colliding with the avatar gets moved off the screen. It’s a cheap trick, but it works.

Here’s the header for the Intro class.

class Intro : public Scene
	Intro(sf::RenderWindow *window, const char *backgroundTexture);
	void Run();
	void ProcessEvent(sf::Event event);

	sf::RenderWindow *_window;
	bool _active;
	sf::Texture _backgroundTexture;
	sf::Sprite _sprite;

My naive take on private and public parts of a class is that if I don’t think I’ll need to get info in a class from somewhere outside of the class, it’s private. Also, I put underscores before private variables because I’ve seen lots of C that does that to indicate private-ness (to other programmers, not the compiler).

The other two C++ issues I’ll have to work through are using references and taking advantage of the Vector class. The items to be picked up could have been stored in a Vector class instead of a fixed-size array. I started to do this, but kept getting errors and I was in a hurry. I use C-style pointers instead of C++-style references to deal with accessing the instance of the sf::RenderWindow class in Main(). I’ll try to use references next time. I’m not completely certain what’s going on there yet. It seems unnecessary to me, but I’m sure it’s there for a reason.


Every time I had a “oh no how do I do this?” moment, I’d find something built into SFML that handled it. That was great. Sprite positions and movement? Handled. Collision detection between two rectangular items? Handled. I wouldn’t be using SFML if I was trying to learn game loops and rendering closer to the metal, but for learning C++ it’s great.

Doing the collision detection in SFML did take me a while to figure out. Here’s the method in the Game class.

void Game::Pickup()
	unsigned int numItemsToPickUp = 3;
	//get rectangle type of player
	sf::FloatRect playerRect(_playerSprite.getGlobalBounds());

	for (unsigned int i = 0; i < numItemsToPickUp; i++)
		if (playerRect.intersects((sf::FloatRect)_itemsToPickUpSpriteArray[i].getGlobalBounds()))
			_itemsToPickUpSpriteArray[i].setPosition(900, 900);

I set a local variable numItemsToPickUp to 3 because I know that there are three items to pick up. Then, I create an instance of the sf::FloatRect class called playerRect that holds a rectangle equal to the player sprite global bounds. There’s a method in the sf::Sprite class called getGlobalBounds(), so this part is easy enough.

The sf::FloatRect class has a method called intersects(), which takes another sf::FloatRect as an argument. The loop goes through each item in the _itemsToPickUpSpriteArray and tests that item’s global bounds (cast as a sf::FloatRect so that it can be used as an argument for the intersect function) against the player’s and if so, moves the item’s position off screen at 900,900. Eh, that’s not a good explanation. There aren’t any good posts on this easy collision detection in SFML that I could find through Google, so I might bang a more clear example out later.

What’s next

I have tons of other little game ideas. The next few will be simple like this, but I’ll try to get better at organizing and styling my code.

Posted in Hobbies, Programming | Leave a comment

Handy Python trick with list data

I had to manipulate a large amount of text data at work, and came across two neat tricks I’d like to remember.

If I have a Product A that’s always bundled with Product B, but I also sell Product B as a separate item, how many users do I have that have only purchased Product B? It’s not as easy as querying all Product B purchasers, because I’ll pick up purchasers of Product A as well.

So, I made a list of all Product A purchasers, and a list of all Product B purchasers, and made a final list of Product B purchasers that didn’t show up on the Product A purchasers list.

#AccountsWithProductA is a list of all purchasers of Product A

#AccountsWithProductB is a list of all purchasers of Product B

from sets import Set

SetOfProductAUsers = set(AccountsWithProductA)

SetOfProductBUsers = set(AccountsWithProductB)

SetOfProductBUsers -= SetOfProductAUsers

After those operations, SetOfProductBUsers only contains exclusive Product B users. It’s a handy manipulation that’s tough to do with lists alone.

Another problem I faced is that the initial parse of the data to extract accounts would fill the list with duplicate accounts since many accounts would purchase the products again from time to time. I did some Googling to track down a way to prune duplicates in a list and found this handy StackOverflow post.

I took the naive approach at both actions (removing duplicates and pruning members of a list that existed in another list) and didn’t have much luck. What I was doing worked with my test data, but when I passed the gigantic real data at it I ran out of memory. There’s a lot about Python internals I don’t understand!

Posted in Hobbies, Programming | Leave a comment

A few notes on compiling SFML on Ubuntu

I finally got around to updating my Ubuntu Linux box to 13.04, and now I want to do my SFML studies on it.

I wasn’t able to just download the SFML development binaries and header files and compile my test program because of some version mis-match between the SFML libraries and my existing libGLEW libraries. The error message I got when compiling my test program resembled the one here.

Compiling SFML from source is pretty straightforward, but here are a few notes I want to leave for myself in case I ever have to do this again on a new machine.

Dependencies I had to install:

  • Install cmake and cmake-gui
  • Install libglew1.5 (or whatever the latest version is)
  • Install libjpeg-dev
  • Install libopenal-dev
  • Install libsndfile-dev

Generate the make file, then make, make install. For some reason, despite defining an install directory in cmake, the header files and libraries get copied into /usr/local/include and /usr/local/lib (which is convenient, just unexpected). I defined an install directory and the libraries got copied there, but not much else. Maybe I missed a step?

Posted in Programming | Leave a comment

Salsa, as easy as 1, 2, 3!

Here’s my wife’s take on salsa. Her hobbies are edible, unlike mine.


I grew up eating my mom’s fresh salsa, or “pico de gallo,”  which I eventually started making myself.  Later, though I wanted to venture out, and after discovering the wonderfulness that is roasted tomatoes, made some roasted salsa.  Amazingly, it came out great the first time, although I did learn some tricks to make it better with further trials.

I used basically the same ingredients as the pico, except most of them are roasted. 

Okay here are the steps

1.  Gather/cut the ingredients:  here I have cored tomatoes, jalapeno, and onion. Note, I left the pieces pretty big because I don’t want them to burn.  Best part about doing in the summer, it could be really fresh and local.  


2.  Next, roast those ingredients.  I think I did it at 400F for about 20-30 minutes.  


3.  Let it cool, then food processor (or chop it).  I first put…

View original post 56 more words

Posted in Uncategorized | Leave a comment

Mojo Frankenstein

I haven’t touched this site in a while, but for a very good reason! I’ve spent all my hobby time getting into shape and practicing so that I could do this:

I’ve played with these guys before, and it’s always resulted in the biggest leaps in ability and well-roundedness of my drumming capabilities.

The name of the band is Mojo Frankenstein. Here are a few of the tracks we recorded last weekend – http://www.reverbnation.com/open_graph/artist/3540275?1336410755

I’ve recorded dozens of songs with some of the guys in the band over the years. Someday I’ll get them all categorized and online. For now I’m looking forward to hopefully getting some gigs with the demo!

Posted in Music | 1 Comment

Visualizing the Mandelbrot Set with SFML: C Style

I use fractals as my “Hello World” when I want to explore graphics libraries. Since I’m learning SFML at the same time I’m learning C++ I figured it would be a good idea to explore different ways of doing the same thing.

I’m going to go into it in detail, but here’s a C-style implementation. Over the next few days I’ll see if I can implement a proper C++ class-based approach that achieves the same result.

#include <SFML/Graphics.hpp>

void CalcMandelbrotSet(int screenHoriz, int screenVert);
float PixelCoordToGraphCoord(int pixelCoord, float graphWidth, int imageWidth, float graphMin);
int CalcMandelbrotEscapeValueForComplexNum(float real, float imaginary, int escapeMax);

int horizontalResolution = 200;
int verticalResolution = 200;
int escapeMax = 500;
float graphXMax = 2;
float graphXMin = -0.5;
float graphYMax = 1;
float graphYMin = -1;
sf::VertexArray varray;

int main()
    sf::RenderWindow window(sf::VideoMode(horizontalResolution, verticalResolution), "Mandelbrot");
    CalcMandelbrotSet(horizontalResolution, verticalResolution);

    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)

    return 0;

void CalcMandelbrotSet(int screenHoriz, int screenVert)
	float graphX;
	float graphY;
	int x, y, escape;
	sf::Vertex vertex;
	for (y = 0; y < screenVert; y++)
		graphY = PixelCoordToGraphCoord(y, graphYMax - graphYMin, screenVert, graphYMin);

		for (x = 0; x < screenHoriz; x++)
			vertex.position = sf::Vector2f(x, y);
			graphX = PixelCoordToGraphCoord(x, graphXMax - graphXMin, screenHoriz, graphXMin);
			escape = CalcMandelbrotEscapeValueForComplexNum(graphX, graphY, escapeMax);
			if (escape == escapeMax)
				vertex.color = sf::Color::Green;
				vertex.color = sf::Color::White;


float PixelCoordToGraphCoord(int pixelCoord, float graphWidth, int imageWidth, float graphMin)
	return ((float)pixelCoord * (graphWidth / (float)imageWidth)) + graphMin;

int CalcMandelbrotEscapeValueForComplexNum(float real, float imaginary, int escapeMax)
	float zReal, zImag;
	float z0Real, z0Imag;
	int n, escape;
	zReal = zImag = 0;
	z0Real = z0Imag = 0;

	for (n = 0; n <= escapeMax; n++)
		z0Real = ((zReal * zReal) - (zImag * zImag) - real);
		z0Imag = (2 * (zReal * zImag)) + imaginary;

		if (((z0Real * z0Real) + (z0Imag * z0Imag)) >= 4)
			escape = n;
			return escape;

		zReal = z0Real;
		zImag = z0Imag;
		escape = escapeMax;
	return escape;

Nothing too terribly elaborate. I split out some of the more interesting calculations into their own functions for readability.

Posted in Programming | Leave a comment