Archive

Posts Tagged ‘Programming’

More Restructuring

09.04.2012 12 comments

Okay, a quick update on the site’s pending rearrangement.  Firstly, tutorials are in fact going to stay as posts, not pages. I decided I didn’t want a static front page, but I don’t want general “blog” content on the front page either, so thing’s will have to stay as they are. Secondly, I’m finding more and more as I write tutorials that I have to bother going into detail about which software to install and so on and so forth. This is not only time consuming for me, but also probably annoying for the majority of you who have the software already. But I have a solution: I’m going to write a few new posts about setting up “development environments” on Windows and Unix. Then, when I do a tutorial, I’ll just assume you’ve done all the setup in the other post. In fact, I’m thinking I might more generally put a little “prerequisites” and “recommended reading” section at the top of each tutorial. That way, we can focus on the new stuff, but all the background material is easily available to those who need it.

Summary of Content

Naturally there’ll be one for Windows and one for Linux/Unix, each with various subsections. Now, I don’t want to spend too long writing these setup tutorials, but I do want to keep everyone happy ;) So here’s a quick list of what I’ll cover in each one – lot me know if there’s anything you really want added!

Windows

  • Visual C++
  • MinGW
  • Code::Blocks
  • MSYS

Linux/Unix

  • GCC
  • Code::Blocks
  • Text editor (e.g. vim)

Generic

  • CMake
  • Doxygen
  • Git
  • Subversion

These last few will be covered in both tutorials, although the specifics will be a little different of course.

ArrayWars

I’ve decided it’d be good to write a few very simple – but complete – games to show you. The first and simplest is essentially noughts and crosses, though I’ve gone ahead and called it ArrayWars. (If you’re wondering, my reasoning is that noughts and crosses is basically just a little struggle over an array of 9 squares…)

Anyway, the actual game is clearly quite simple here. However, I’m going to try and illustrate a couple of useful techniques that would be useful in a more general and more advanced setting. For example, it’s going to have customizable settings, as well as player stats. Both of these need to be saved and loaded from a file (I’ve found a library which makes this really simple, but more about that tomorrow…) Naturally, there’ll be a main menu, plus sub menus for the settings, the stats, creating a game and so on. This means we’ll need simple GUI elements: primarily buttons and text boxes.

Under the hood, there’ll also be application logging (for bugtesting and diagnostic purposes) and if I find the time I might make a simple in game console. If you’re interested, the code is already in a repository on GitHub. And, needless to say, the sound, windowing, graphics and input will all be handled by SFML!

GoingNative 2012: Minus 1 Day « Sutter’s Mill

You’ve probably already heard, but there’s a big upcoming C++ conference from everyone’s favourite friendly software giant, Microsoft. Actual seats are, of course, all booked up. But you can watch online. If you can spare the time, watch as many as you can. It’s a great opportunity to learn the language from the big names of the industry, including Bjarne Stroustrup himself! And they’ll be available on demand, as well as live, so there’s no excuse to miss them. Have fun!

GoingNative 2012: Minus 1 Day « Sutter’s Mill.

Singletons

Here’s another little something to keep you entertained while you’re waiting for things to get underway here again (it’ll happen eventually, I promise!).

The Happy Singleton

The Happy Singleton

SFML Coder’s C++ Primer

24.10.2011 1 comment

Hi again! Firstly I want to confirm that I probably won’t be writing many more SFML tutorials until a stable SFML 2.0 is released. Unless – of course – a stable release is still a very long way off!

Anyway, I feel I should provide something in the mean time so I’ve decided to pick up my C++ tutorial series again. As some of you may know, I have already put one or two videos on YouTube on this topic and the blog has a few pages related to it. And don’t worry if you already know C++, as I’m sure many of you do. Hopefully some of the later tutorials will be interesting for you as well – I’ll try to make explanations fairly complete so you’ll still be able to take something from videos.

Now, I have some ideas as to the ‘syllabus’ I’m going to follow. Let me know what you think – anything I’ve missed out, things I should remove until later, or simply a few re-orderings.

  1. Introduction to C++, GCC and Code::Blocks
  2. Hello World! Program
  3. Statements, expressions and operators
  4. Variables and more operators
  5. Conditionals – if, else and switch; boolean variables and operators
  6. Loops – for, while and do while
  7. Functions – arguments and return types etc; inline functions
  8. Larger projects – multiple source files, header files, IDE projects

Building SFML 2.0 with Make (GCC/Ubuntu)

16.08.2011 15 comments
GNU Compiler Collection logo. Source/Author: from http://gcc.gnu.org/img/gccegg.svg, with tweaks.

Image via Wikipedia: The GNU Compiler Collection

I’ve now demonstrated how to build the SFML 2.0 binaries in an IDE and also with a build utility (better). However, all my tutorials have undoubtedly been Windows centric – I discussed Microsoft Visual C++ and MinGW, after all. Well now it’s time to make that right – in this article I’m going to explain how to build the latest versions of the SFML 2.0 binaries for Ubuntu, using GCC.

Other Unix Operating Systems

With the addition of the post, I’ve covered Windows – both MinGW and MSVC – and also GCC on Ubuntu, Linux Mint, Debian or an Ubuntu variant such as Xubuntu. But what about the rest of you? What if you’re on another Unix operating system? Well, the fact is that you should still have access to GCC. This means you should be able to invoke the actual build operation with the same commands. The other software we need – CMake – should also be available for most platforms. Thus the core elements of what I demonstrate here for GCC with Ubuntu should be applicable to GCC with other Unix platforms.

The thing that you will have to bear in mind, however, is that the other terminal commands I use (such as apt-get to install software) may not be available on your system. You may have a different terminal, and different package manager which is invoked via a different command and syntax. If you want to follow this tutorial, you’ll have to translate those commands into ones appropriate for your OS. Just remember that the invocation of cmake and make should be the same in most circumstances.

The Video

Read on the for textual tutorial. I suggest that even those of you who prefer videos read this one, as we’ll be working primarily with the terminal: something which is undoubtedly easier to talk about in writing. Read more…

Two Small C++0x Features

I’m going to mention two little C++0x features today. I don’t have much time, but I thought I may as well share them briefly. They’re both fairly small and quick to explain, so let’s get to it. I’ve put the one that’s faster to discuss first. As always, check out Dr. Stroustrup’s FAQ for more information.

I. long long Integers

The C++0x standard has a new type – the long long integer – which is guaranteed to be at least 64bit. No more annoyances because too many compilers make int and long int the same size – use long long int and you can be sure that you’ll get 8 whole bytes of integral goodness!

Apparently this feature was already supported by a number of compilers, but it’s standard now so you don’t have to worry about non portable code (well you do, but you can blame compiler vendors for being slow to comply with the new standard… ;) ). There’s really not much to say here, merely that you have two 64bit types: long long (i.e. long long int) and unsigned long long (i.e. unsigned long long int). Also note the new literal suffixes: LL for long long and ULL for unsigned long long.

II. nullptr

This one’s exactly as the name suggests: that is, the nullptr keyword represents a null pointer. Previously one might have done something like this:

char* name = 0;

In C++0x, however, one can do the following instead:

char* name2 = nullptr;

These two examples have the same result, that is name == name2 would evaluate to true. On the other hand, while we can also assign the value o to an integral type,  we can assign nullptr only to a pointer variable. So the following would yield a compiler error:

int age = nullptr; // compiler error: age is not a pointer

This is all well and good, but it’s quicker to type 0 than nullptr. Why, therefore, should we use the latter over the former? Well the fact is that the double meaning of 0 as the integer 0 and as the null pointer 0×00000000 has the potential to cause problems. Allow me to demonstrate using Dr. Stroustrup’s example. Consider a function f with two overloads:

  • void f(int)
  • void f(char*)

We call f(0). What happens? Well in fact, f(int) is called, but what if we wanted to pass a null pointer to f(char*). Previously we should have had to resort to something like f((char*)0), which is rather undesirable, in my opinion at least (and, presumably in the opinion of committee members!). So, do the right thing and use nullptr to represent your null pointers.

Overloading Template Functions

Another little tip inspired by a question on cplusplus.com. How do we overload template functions? Well the answer is that we overload the template function in the same way as we would overload any other function.

template <class A> void foo()
{

}

template <class A> void foo(float f)
{

}

We do, however, have to be slightly more careful with specialised functions. If we overload a specialised function, the overload must match some overloaded version of the template.



template <class A> void foo()
{

}

template <class A> void foo(float f)
{

}

template<> void foo<int>() // allowed: matches the first template
{

}

template<> void foo<int>(float f) // allowed: matches the seconds template
{

}

template<> void foo<int>(const char*) // not allowed: does not match any template
{

}


C++ Tip – this

You probably all know about the this pointer already, but I just answered a question about it on cplusplus.com, so I thought that I may as well post here while it’s at the forefront of my mind!

So this is a keyword which we can use inside a member function. It is a pointer to the instance of the class from which the member function was called. Still not sure? Check out the following example program.

#include <iostream>
class foo {
public:
   void bar() { std::cout << this << std::endl; }
};

int main()
{
   using namespace std;
   foo foo1, foo2;
   cout << "foo1\n" << &foo1 << endl;
   foo1.bar();
   cout << "foo2\n" << &foo2 << endl;
   foo2.bar();

   return 0;
}

This program yielded the following output for me.

foo1
0031F916
0031F916
foo2
0031F917
0031F917

For you, the actual numerical values of the addresses may be different, but the point is that the address of foo1 is the same as the value printed by foo1.bar() (which prints the this pointer) and the address of foo2 is the same as the value printed by foo2.bar().

So, if you couldn’t before, you can now hopefully understand what I mean when I say that the this pointer points to the instance of the class from which the member function was called.

Interesting Point: Notice that the address of foo1 is only 1 behind the address of foo2. We created the two variables on the same line as stack variables, so they are in adjacent memory locations. However, as the classes are effectively empty, the memory locations are in fact even consecutive (each instance takes only one space on the stack).

Notes
  1. Remember that this may only be used from inside member functions (think about it – it wouldn’t make much sense anywhere else)
  2. this cannot be used in a constructor’s initializer list, but it can be used in its body
  3. this can be used in a destructor’s body, but be careful as if the class is being destroyed you probably don’t want to go around handing its address out to other functions and variables

Space Fight – Physics

So how do objects interact with each other in the game, and indeed, how can we generalise their individual manipulation in the world?

Important: This is my first post where I use some physics and vector algebra. If you aren’t comfortable with these topics, and would like help understanding the posts, feel free to comment and I’ll give you some help and/or suggestions. Moreover, if you are happy with these topics, but think I’ve done a bad job of explaining, then tell me that as well!

Individual Objects

Position and Facing

A physics object in the game doesn’t keep track of it’s own position or facing for technical reasons (they is part of the graphics object, which gets them from sf::Sprite). Nonetheless, a physics object is capable of retrieving and modifying it’s position via virtual functions and polymorphism. The same is true for it’s facing/angle.

Read more…

Follow

Get every new post delivered to your Inbox.

Join 76 other followers