Posted on July 25, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/roguelike.

In the last post of this series we’ve added a dummy map to the game on which the main character was able to move freely. Now it is time to add a real map to our roguelike game, in order to be able to add elements on the map we are going to use a Perlin noise function. If you are interested in how I’ve implemented the Perlin noise function in C++ you could read my Perlin noise in C++11 article.

I’ll include here, for completeness, the PerlinNoise class definition. The complete code is on the Github repository posted at the beginning of this article:

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15``` ```class PerlinNoise { // The permutation vector std::vector p; public: // Initialize with the reference values for the permutation vector PerlinNoise(); // Generate a new permutation vector based on the value of seed PerlinNoise(unsigned int seed); // Get a noise value, for 2D images z can have any value double noise(double x, double y, double z); private: double fade(double t); double lerp(double t, double a, double b); double grad(int hash, double x, double y, double z); }; ```

Posted on July 21, 2012 by Sol

3 August 2012 update

You can use this tutorial for compiling gcc-4.7.1 on Mountain Lion too. I’ve used the Command Line Tools from Xcode 4.4 to test the procedure described bellow.

In this tutorial, I will show you how to compile from source and install the current stable version of gcc with Graphite loop optimizations on your Mac OSX Lion computer. The instructions from this tutorial were tested with Xcode 4.3.3 and Mac OSX Lion 10.7.4.

Having the latest stable version of gcc on your Mac could be handy if you want to test some of the new C++11 capabilities that are present in gcc-4.7, like for e.g. lambdas, multithread programming or initializer lists (not supported by Clang for e.g.). It could also be useful if you want to be able to play with the latest Fortran 2008 standard.

Posted on July 18, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/Perlin_Noise.

Ken Perlin’s noise function is the building block of many texture generation algorithms, you can use it to create realistically looking materials, clouds, mountains etc … The first version of this function was developed in 1988 and it is still used in various graphical libraries. In 2002 the author has published an improved version of his noise function.

I searched the Internet for a C++ implementation of the improved Perlin noise function, while obviously available in various libraries I didn’t found this implemented as a ready to use class. Also there seems to be a general confusion between what is a Perlin noise function, some websites confuse the Perlin noise function with FBM (Fractal Brownian Motion).

Posted on July 16, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/roguelike.

The last post of this series has laid the ground for a small roguelike game - the main character @ was added on the screen and the user was able to change his position using the arrow keys. Now it is time to add more interactivity to our game by creating a test map on which the character is allowed to move freely.

We’ll start by refactoring a bit the code for the screen initialization part implemented last time, we could put all this code in a Screen class, we show here only the definition of the Screen class:

Posted on July 12, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/roguelike.

I suppose you’ve skimmed through the Introduction to this series of blog posts about implementing a toy roguelike game in C++. That being said, let’s get started!

If you want to follow along with the development, you will need a C++ compiler and the ncurses library. I’m going to use the GNU C++ compiler (specifically g++-4.7.1) for testing and compiling my code. In principle any C++ compiler available on your system should work, just be sure that you can work with the last version of ncurses. For Mac users, installing Xcode and the Command line tools will also install ncurses. On Linux you could use your package manager for installing g++ and ncurses, the procedure is different from distribution to distribution, if you use a Debian derived distro, like Ubuntu, all you have to do is to write in a Terminal:

 ```1 2``` ```sudo apt-get install g++ sudo apt-get install libncurses5-dev ```

Posted on July 12, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/roguelike.

A few days ago I read an article named How to Write a Roguelike in 15 Steps, this article wet my appetite to implement my own roguelike game in C++. Why am I losing my time with a text based game instead of say attempting to implement the next clone of Angry Birds for iOS? Well, first I think implementing a small game from scratch is fun and I could learn a lot during the development phase. I tried a few times before, to work on a fully blown graphical game (C++ and OpenGL) but it is easy to lose interest when you code a few hours for testing an idea and you notice that the initial concept was a bad one. It seems to me that I could be more productive if I use my time to develop the game concept instead of thinking at how to implement a complex graphical effect.

I plan to document the development of the game in a few blog posts.

Posted on June 28, 2012 by Sol

If you are a home working hacker like me, I bet that you have some trouble maintaining a healthy life style. It is so easy to forget about your body when your mind is busy with coding or reading other peoples code. Some of us have problems keeping a nice waistline, others have bigger problems as secondary effects of a sedentary life.

Not long ago, empty bottles of Cola and piles of pizza boxes where an integral part of my room landscape for one or two weeks during some intense coding periods. Fortunately for me, I always revert to more healthy food at the end of my coding sessions. I’m one of those guys that loves cooking a good meal when he is not busy coding, playing or watching some movies. It’s summer now and the market is full of fresh fruits and vegetables, so I have no excuse to eat frozen pizza or preprocessed food.

Posted on May 31, 2012 by Sol

The code for this tutorial is on GitHub: https://github.com/sol-prog/cuda_cublas_curand_thrust.

Matrix multiplication is an essential building block for numerous numerical algorithms, for this reason most numerical libraries implements matrix multiplication. One of the oldest and most used matrix multiplication implementation GEMM is found in the BLAS library. While the reference BLAS implementation is not particularly fast there are a number of third party optimized BLAS implementations like MKL from Intel, ACML from AMD or CUBLAS from NVIDIA.

In this post I’m going to show you how you can multiply two arrays on a CUDA device with CUBLAS. A typical approach to this will be to create three arrays on CPU (the host in CUDA terminology), initialize them, copy the arrays on GPU (the device on CUDA terminology), do the actual matrix multiplication on GPU and finally copy the result on CPU. Our first example will follow the above suggested algorithm, in a second example we are going to significantly simplify the low level memory manipulation required by CUDA using Thrust which aims to be a replacement for the C++ STL on GPU.

Posted on May 11, 2012 by Sol

The code for this tutorial is on GitHub: https://github.com/sol-prog/mix_fortran_cpp.

Different programming languages have different strengths and while you can express any imaginable algorithm in C++11 sometimes you need to interface your code with legacy codes written in Fortran, or you want to use modern Fortran for his rich matrix operations. This post will present some simple examples of mixed Fortran 2008 and C++11 code, as a side note it is entirely possible to write these examples in pure Fortran or C++, coding every operations from scratch in C++ or using matrix libraries like Eigen.

Mixing Fortran with C (or C++) was a painful experience in the past and the exact mechanism was compiler dependent. Starting with Fortran 2003 you can use a standardized mechanism for interoperability with C, calling Fortran from C and vice versa can be done using the iso_c_binding Fortran module.

Posted on May 9, 2012 by Sol

The code for this tutorial is on GitHub: https://github.com/sol-prog/threads.

we’ve seen that C++11 allows us to use a clean syntax (compared with the one used by POSIX for e.g.) for managing multithread applications. The second tutorial presents an example of threads synchronization using mutex and atomic operations. In this tutorial I’m going to show you how to use a member function and lambda with threads.

Posted on May 1, 2012 by Sol

iOS 5 is a major release for iPhone and iPad programmers. One of the major modification is the addition of ARC (automated reference counting) that involve removing the need to manually manage memory from your code. Using ARC will increase your productivity but will also require a change in the way you think about your code. Another addition is the GLKit framework that is meant to simplify the use of OpenGL ES 2 in your programs.

These new features of iOS5 will broke in subtle ways almost all iOS books published prior to the end of 2011. iOS 5 By Tutorials by R. Wenderlich, S. Baranski, J. Gundersen, M. Hollemans, F. L. Marsetti, C. Rocchi, M. Todorov is a must read for everyone interested in coding for iOS5 and up.

This are some of the chapters of the book that I find particularly interesting: Beginning ARC, Beginning OpenGL ES 2.0 with GLKit, Intermediate OpenGL ES 2.0 with GLKit, Beginning iCloud etc … Other topics from the book: Storyboards, including Twitter in your programs, turn based gaming, Game Center API.

Posted on April 24, 2012 by Sol

With the arrival of the new C++ standard last year, C++ has changed. In a sense C++11 is a new language while keeping backward compatibility with the old standards. You can run any valid C++98 or C++2003 program through a C++11 compiler and the code will pass without a problem, however if you take a modern piece of C++ code this could look quite different from the equivalent C++ code written ten years ago.

The C++ Standard Library: A Tutorial and Reference (2nd Edition) by N. M. Josuttis was updated to reflect the new C++11 standard. The author presents in detail the changes and additions from the Standard C++ Library with a particular focus on: iterators, containers and STL algorithms. A large set of exercises that exemplify each aspect of the library presented in the book is also available on the book's website http://www.cppstdlib.com/.

At the time of this writing, there is no compiler that fully implements the new standard. If you are Windows user, Visual Studio 11 implements most of the new C++11 standard. On Linux, you can build from sources the last gcc-4.7. On a Mac computer you could use Clang or compile from source the last version of gcc.

If you are C++ beginner you can consult my list of C++ books for beginners.

Posted on April 13, 2012 by Sol

15 October 2012 update:

I’ve updated the article for gcc-4.7.2 and his dependencies: gmp-5.0.5, mpfr-3.1.1 and mpc-1.0.1.

This is a short how to compile from sources gcc-4.7 on Ubuntu 12.04. I was surprised to find that the process of building gcc-4.7 from sources on Ubuntu is so complicated, after all, this is a Linux system …

First check that you have the default Ubuntu gcc and binutils installed, in principle the above are already installed if you use the 64 bits Desktop edition of Ubuntu. In the rest of this tutorial I will suppose you will use a Terminal to input all the described instructions.

If your system is not up to date, you can install the latest updates with:

 ```1 2``` ```sudo apt-get update sudo apt-get upgrade ```

Now, let’s start by installing a few prerequisites:

Posted on April 11, 2012 by Sol

If you were involved in working with large arrays and linear algebra, you’ve probably heard mantras like A Fortran implementation can usually achieve more performance than C or C++ or C++ is slow for scientific computations, don’t use it!. Until recently I honestly believed that, at least for elementary linear algebra operations, like vector addition for e.g., C can easily beat C++ (specifically that working with C-arrays should be faster than the more elaborate vector data structure from C++) and Fortran will beat both. Please be aware that I’m not talking about which language is inherently superior to another, but rather about what is the actual state of their implementation on personal computers running Linux and Windows.

In order to test the above myths I’ve implemented a few simple codes for vector, one dimensional arrays, addition in C, C++ and Fortran.