Do you have facebook? If so, then you are probably familiar with the timeline feature.
I have decided to write up a timeline about myself. It gives a view of who I am, what I did, when and how I got influenced. It also showcases my projects in a chronological order.
Since I could not find an easy solution for this problem I have decided to blog about this.
Lets say your machine (running Windows 7) is running in a Dutch locale. If you install the JDK and request the default locale in your application you will get: nl_NL
I’m testing this with the following code:
import java.util.Locale;
public class DefaultLocaleTester {
public static void main(String[] args) {
Locale defaultLocale = Locale.getDefault();
System.out.println("Default locale is : " + defaultLocale);
}
}
– go to Control Panel
– go to Region and Language
– go to tab “Administrative”
– click “Change system locale”
– change to locale you want
– restart and you’re done!
So we do this, and we run the test application again.
You will found out, this will not work. When I rerun the test application I still got nl_NL.
On stackoverflow someone had the answer to this problem. You should be changing your Format instead. So:
– go to Control Panel
– go to Region and Language
– go to tab “Formats” (is default selected)
– change language in Formats dropdown to the locale you want
Now re-run the test code, and you’ll see it will be using the locale you want.
Thanks to Martin Bartlett on Stackoverflow for his answer!
In the previous post I have set up a raw architecture for the game. There is a Game class that has a function called execute, which allows basic stuff to happen:
– responding to events
– updating state
– and rendering state
In code it is:
while (running) {
handleEvents();
update();
render();
}
The end result of the first phase was, well, a black window. Nothing to be excited about just yet…
One of the very basic needs is drawing on the screen. One way to get something on the screen is by using something called blitting. Blitting is basically taking a picture in memory (called in SDL a surface, or in Allegro a BITMAP) and then copy it onto another surface which functions as the screen.
We already have this screen SDL Surface defined in our Game class. It is not the actual (hardware) screen buffer. But, by doing SDL_Flip we can make this screen visible.
In essense this means the render function will be like this:
void Game::render() {
// draw some stuff on the screen
// flip screen at the end
SDL_Flip(screen);
}
I want to be able to draw surfaces on the screen. SDL requires the usage of SDL_Rect‘s which allows you to draw pieces of surfaces. At this point I don’t want deal with these SDL_Rect’s directly. What i want is:
draw (source surface, dest surface, int x, int y);
This is where I introduce a new class (again Single Responsibility Principle) that allows drawing on the screen and providing easy methods to do so. I call this the surfaceDrawer. At this point in time, its class definition looks like this:
#ifndef SURFACEDRAWER_H
#define SURFACEDRAWER_H
#include <SDL/SDL.h>
class SurfaceDrawer {
public:
void draw(SDL_Surface * from, SDL_Surface * dest, int x, int y);
};
#endif
This implementation is easy; the SDL_BlitSurface accepts:
– a from surface
– a from rectangle (ie, what to copy from the from surface?, NULL = everything)
– a destination surface
– a position given by a rect. (where to draw this? Starting with upperleft corner of from surface)
In this case, we provide NULL as 2nd argument, saying we want to copy the entire from surface. Then, the last parameter is the position where to draw it in the form of an SDL_Rect.
Next step is to actually use this function. One thing that we always need in an RTS is to draw the mouse. What we need is an SDL_Surface with a mouse bitmap loaded. I will be using:
Loading this image is done by using SDL_LoadBMP . We need to make sure that the surface we have loaded is in the same bit/color format as our screen. We can do that by using SDL_DisplayFormat. We don’t want to be bothered with this everytime, so we have to make some class responsible for loading resources into SDL_Surface’s which are suitable for drawing by the surfaceDrawer. For that I have introduced a surfaceDao class, which has the following class definition:
#ifndef SURFACEREPO
#define SURFACEREPO
// Data Access Object for fetching SDL Surfaces
#include <SDL/SDL.h>
class SurfaceDao {
public:
SDL_Surface * load(char * file);
};
#endif
SDL supports BMP formats out of the box. We need to use the SDL_Image library to get support for other formats. Using a DAO we can move all this format specific stuff out of our game into this single class later. For now I will be using BMP, as the main focus is drawing surfaces.
The implementation of the surfaceDao looks like this:
We load the BMP, when that is succesful, we convert it to the current display format. We have to free the temp surface using SDL_FreeSurface, to prevent memory leaks.
Now, in the Game class, this all comes together, first the class definition is expanded and gets (at the private section) the following code:
In the render function I use the surfaceDrawer to draw the mouse at the current X and Y position of the mouse:
void Game::render() {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
surfaceDrawer.draw(mouse, screen, mouseX, mouseY);
// flip screen at the end
SDL_Flip(screen);
}
This all finally results into:
Finally, we have something to draw! yay! But we need to make this a little bit better first:
– remove the system cursor (you don’t see this on the picture, but when running this your system cursor is on top)
– make sure we don’t have a trail of the mouse, ie, clean the screen before drawing
– we also see the purple background of the mouse, we don’t want to draw that…
Don’t draw the purple background
What we want is to skip a certain color when blitting a surface to another surface. We can do this by specifying a color key. This color will be skipped with drawing. The nice thing about the surfaceDrawer is that all have to do is add a function there to do this and change the call in Game to use the new function:
Add function to the surfaceDrawer
class SurfaceDrawer {
public:
... snip ...
void drawTransparant(SDL_Surface * from, SDL_Surface * to, int x, int y);
One thing to note, in D2TM the assumption is that all colors with RGB: 255,0,255 (purple) will be skipped. That is the reason the surfaceDrawer does not have a color parameter. We could add this if it is needed later on.
The first line actually creates the purple color. The code is not that selfdescribing. Later on I will introduce a Colors class that basically says “give me purple”, and does the SDL_MapRGB in its implementation.
Of course, now in the Game we have to make a few adjustments.
void Game::render() {
surfaceDrawer.clearToColor(screen, Colors::black(screen));
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
surfaceDrawer.drawTransparant(mouse, screen, mouseX, mouseY);
// flip screen at the end
SDL_Flip(screen);
}
Two lines are added, first clear the screen to a specific color (black), and then for drawing the mouse we use the new drawTransparant function.
And after compiling, it looks like this:
Next blog post
Although we are able to draw stuff on the screen, it is far from efficient. If we want to draw a terrain we have up to 16 different surfaces for one terrain type (lets say, rocks). It is not doable to hold 16 different SDL_Surface’s and then draw the correct one. Besides, we also have spice, mountains, sand, hills, spicehills. So we need to do something clever. Thats where a tileset comes in. The next blog will be about that.
A side note about including SDL in every header file
One thing you’ll notice about my class definitions, is that I seem to include SDL all the time. Tutorials will say you’ll provide this at the top of your project in your main class (in this case it has to be main.cpp). Although I understand why (since the preprocessor will put the SDL code there, and will be accessible to all other files), it violates the SRP. In fact, you cannot compile a single CPP class anymore using SDL, since you do not refer to this.
As you can see in all my header files, there is a piece:
#ifndef SOMETHING
#define SOMETTHING
// here is code
#endif
This basically says “whenever I have not yet defined SOMETHING include the piece of code, and define SOMETHING”. This allows us to include the same files over and over again, but we are sure the preprocessor only includes it once. This is needed because else your program will not compile due multiple definitions of the same class.
So now:
– I can compile any CPP file seperately (and, the big plus: I can use that for testing later on! :))
– I know what all my dependencies are, they are not somewhere else hidden
In my previous post I have described a way to compile your project with a Maven style like project structure. Using that as basis I am busy rewriting my project Dune II – The Maker. This time I am using SDL.
Because I have started over I thought of writing blog posts about my progress. Blog posts will be about progress made, decisions taken, etc. This is by no means a real tutorial sequence, but you could follow the blog posts probably and make something out of it. Do note: In reality I am much further in development then my blog posts. The blog posts are based upon older revisions than HEAD. If you are interested in the most recent version you can getthe source yourself.
In this blog post I will describe how I have started to setup the project. I already have a directory structure as described in my previous blog post.
My goal is to have a primitive architecture set up, I have used the first SDL tutorial as starting point.
The reason I went with a tutorial is that I wanted to be inspired by a different approach than I have done myself in the past. In fact, I have always just tried to do what I thought what was best. In this case I took the tutorial and from there I will change it how I think it should be. Its never bad to take a fresh look at things 🙂
The very first start was to create a Game class, it has the following class declaration:
#ifndef GAME_H
#define GAME_H
#include <SDL/SDL.h> /* All SDL App's need this */
class Game {
public:
int execute();
private:
bool running;
int init();
void shutdown();
void handleEvents();
void update();
void render();
void onEvent(SDL_Event * event);
SDL_Surface * screen;
};
#endif
The only function that needs to be exposed is the execute method. All other functions are used internally.
The SDL surface screen represents the main display (screen).
The execute function does basically everything. A game loop is in essence very simple. In fact the game is one big loop which does two things:
– update state
– show state
To update state, in this case we have separated two things:
– handle events (in SDL terminology, this means we handle input events. Which can have effect on the game state)
– update (here you actually update the game state)
That still remains updating game state, though a bit more split up. We could later split it into keyboard/mouse states. But, these things all are depended on the certain kind of game state you’re in. A main menu screen should react differently on mouse input than lets say the actual game playing. The current design has not given any direction yet what to do. We could have one big switch statement, but we could also do something better… I’ll come to that in a future blog post.
The init function basically sets up the application. I am not too happy about this in this stage, but for the initial setup it is good enough. I believe the factory pattern should be used for constructing a game object though, so I have already created a class for it. The reason is that i want to respect the Single Responsibility Principle. Constructing a Game object, which requires loading resources, setup SDL, etc, has nothing to do really with the basic Game object itself. In fact, as you will see in later blog posts, the Game object has the only responsibility and that’s calling all the right collaborator classes that make the game what it is.
Since I already know I will be using a factory class I already introduce one which for now does nothing spectacular:
Header:
#ifndef GAMEFACTORY_H
#define GAMEFACTORY_H
#include "game.h"
class GameFactory {
public:
Game create();
};
#endif
Implementation:
#include "gamefactory.h"
Game GameFactory::create() {
Game game;
// TODO: load resources for game, do SDL initialization, etc.
return game;
}
Finally the main.cpp which has the entry point of the application:
#include "gamefactory.h"
int main(int argc, char **argv) {
GameFactory gameFactory;
Game game = gameFactory.create();
return game.execute();
}
And thats it! The result is a game that is able to start, it has functions that declare several phases in the game and allows us to expand it further. Because of the factory we can also easily move initialization code out of the Game class.
If you would compile this, and run it, you would see:
As for developing applications, I believe a certain principle lies behind it. Most of it, you won’t really see. Often, with a few lines of code you can make a lot of stuff happen on the screen. But the actual work is ‘under water’:
When you’re used to Java, you build your software using the comforting build-tools. Popular examples are Ant and Maven. Personally I love Maven.
When you’re writing an application in C++ it seems like a big step backwards build-tools wise. If you’re an Ant God you might feel a bit more comfortable with using Make. However, writing makefiles (the ‘equivalent’ of the Ant’s build.xml) are a more tough.
Since I am used to Maven, there are some tasks I want to perform, like:
– clean
– compile
– compile tests
– package
To start with. I have set up a little makefile that does this for me and for those who just want to get started, share the makefile with you. But what will you get? It won’t be exactlylike Maven…
It took a little while, and I bet it is far from perfect for real C++ developers who eat makefiles for breakfast. But I am happy with it for now.
The directory structure that needs to be in your project is like this (cpp vs mavenized):
When you run ‘make’ you will be doing all tasks. Meaning you will clean, compile everything (your ‘normal’ code and test code) and then copy any libraries, resources and binaries.
Caveats:
– I am compiling without using the -c flag, this simplifies a lot of stuff for me (it works). But, the larger your project the longer it will take to compile, since it will compile everything all the time. So even though you change one file, everything will be recompiled.
– You need to specify sub-dirs when you use them, when there are no files in those dirs the compilation fails (atleast using G++)