Monday, December 5, 2011

Make your Engine more dynamic!


When I started to program my Engine, I wanted to create a somewhat portable Engine.
I wanted to support different libraries like OpenGL, OpenAL or DirectX to make my Engine run under Windows and other platforms.

My first attempt was to differentiate with the Preprocessor variables given by the OS
(For example _WIN32 under Windows).

That worked, but was somewhat ugly...

It was indeed so ugly that I decided to give it another try.
So I split everything into separate dynamic libraries.

At the end I had one library for every replaceable feature of my engine.
Here are some of them:

K15_GraphicModule.dll / K15_GraphicModule.so
K15_SoundModule.dll / K15_SoundModule.so
K15_InputQueueModule.dll / K15_InputQueueModule.so
They were implement as subsystems.

I'll try to explain the idea behind the subsystems by the example of the GraphicModule.

The GraphicModule is, for example, a subsystem of the Engine's GraphicManager.
The GraphicManager will try to find a "K15_GraphicModule.dll" or "K15_GraphicModule.so" (depends on the OS) in it's initialize function. When it finds the dynamic library, it loads it and tries to perform the function "createGraphicModule()" within the library.

The function createGraphicModule will return an implementation of a GraphicModule interface that is declared inside the Engine's library file.

Here's some code to clarify my example.

 GraphicManager::GraphicManager  
 {  
      typedef createGraphicModule IGraphicManagerModule* (*createGraphicModule)(void);  
      DynLib graphicLib("K15_GraphicModule") // load library (without extension...Determined by the current OS)  
      if(graphicLib.loadedSuccessful()){ //Check if library has been loaded.  
           createGraphicModule func = (createGraphicModule)graphiclib.getFuction("createGraphicModule");  
           if(func != NULL){ // function loaded?  
                this->m_subsystem = func(); //get Subsystem from dynamic library.  
           }  
      }  
 }  

This system allows me to switch between OpenGL and DirectX just by replacing the dynamic library files...Or to switch from Windows File Manager to some Unix File Manager just by replacing the library file.

I just implemented this system about a few days ago, so I don't know how well that will work in an actual game...But it's only a matter of time 'till I know it ;-)

Friday, November 25, 2011

Doom 3 source code released!

Hey guys,

the source code of Doom 3 got released.
You can download it here.

I downloaded it yesterday but haven't had time to take a look into it,yet.

Hint:
For everyone who's using Visual Studio: There's a Visual Studio Solution File (*.sol) in the \neo\ folder.

Sunday, October 23, 2011

Friday, September 9, 2011

Using the benefits of lambda functions with a component based game object model

Hi everybody!

Today I'd like to introduce a feature of the new C++11x standard, which is implemented in the Visual C++ compiler of the 2010 edition of visual studio and newer versions of gcc.

The new feature I'd like to talk about is the Lambda Function.

A Lambda Function is an anonymous function...
What exactly that means is demonstrated in the code below.

I'm going to refer to my last blog post (Component based game objects) and take components as an example.

 class IComponent
{
public:
virtual void update(float gameTime) = 0;
virtual bool handleMessage(Event const & componentEvent) = 0;
};
This is the (simplified) interface I'm using for components.

Now think about what you would do if you have, for example, a game object which needs to respond to mouse movement events.

You really need this behaviour only for this particular object and nothing else.

So far one had to write a new IComponent implementation for this.
Think about the mass of this kind of components you'll have at the end of your project...

Fortunately the Lambda Functions will help us with the mass of implementations for this "one purpose components".

Consider the following IComponent implemention:

 typedef std::function<void(float)> UpdateFunction;
typedef std::function<bool(const Event)> HandleEventFunction;
class VersatileComponent : public IComponent
{
public:
VersatileComponent(UpdateFunction updateFunc,HandleEventFunction handleFunc){
this->m_updateFunction = updateFunc;
this->m_handleFunction = handleFunc;
}
void update(float GameTime){
m_updateFunction(GameTime);
}
bool handleEvent(const Event &gameevent){
return m_handleFunction(gameevent);
}
private:
UpdateFunction m_updateFunction;
HandleEventFunction m_handleFunction;
};

What this component does is taking 2 functions as parameters and performing them.
(for those of you who are not familiar with std::function (or std::tr1::function) check out Effective C++ (Scott Meyers has some great items in there which covers std::function) or this link.)

What you can do with the above implementation is something like this:

 bool handleMouseMovement(const Event &gameEvent){
if(gameEvent.getType() == MouseMovement){
//DoStuff
}
}
void update(float deltaTime){
//DoStuff
}
int main()
{
VersatileComponent *vComponent = new VersatileComponent(update,handleMouseMovement);
//DoStuff
}

Here we just define 2 functions which takes the same parameters and return the same types as the update and handle function of the VersatileComponent class... We just pass them as parameters so they get called in the VersatileComponent update() and handleMouseMovement() function.

(Look here if you need a refresh in function pointers)

So...That's nothing new so far, but now I'm going to use Lamda Functions (or anonymous functions, if you prefer).

I'm just going to throw this code at you, before I'll do any explanation:
 int main()
{
VersatileComponent *vComponent = new VersatileComponent([](float gameTime){
//DoUpdateStuff
},[](const Event &gameEvent)->bool{
if(gameEvent.getType() == MouseMovement){
//DoStuff
}
});
//DoStuff
}
I know this does look weird at first, but I'm just doing the same as in the previous code example...I'm passing two functions as parameters...two Lambda Functions.

As you might see, Lambda Functions are getting defined at the point where they are needed.
They don't get named (well they get internally,but you don't know the name of them), so you can't call them in other places of your code.

They also have some kind of unique initialization syntax, which you can look up here (the link also features more informations about Lambda Functions).

The benefit of this approach is, that you don't have to declare functions in other places of your code only because you need them once. (see the example with the function pointers)

With the new Lambda Functions the VersatileComponent truly gets maximum versatileness.

BTW: Just for your information : I don't get nothing for linking to Amazon items. ;)

Wednesday, August 10, 2011

Switching from hierarchical to component based game object design.

Hi everyone,

I read some interesting articles about component based game objects the last few weeks and after testing and playing with this kind of game object architecture, I decided to throw away my existing hierachical game object architecture and stick with the component based instead.

I know it's unthankful work and I really try to not think about it too much, but while I was playing around with this kind of architecture, I defenitly saw the advantages compared to the hierarchical one.

As you can see on the left, I already created an UML diagramm for better understanding.

I don't know if I will stick with this implementation, as I threw it together in just no time, but I think it's defenitly going into the right direction.













Enough on that topic, now I want to say a little bit about my private stuff.

As some of you might now, I just finished my apprenticeship about 5 weeks ago. I currently still work in the company I made my apprenticeship in, but I really want to get into this gaming stuff so I applied for a place in a german college of computer science - specialisation game development (FH Heidelberg).

After some really, REALLY annoying bureaucrazy I finally got the answer from the college that I can matriculate. So from the 1st October 2011, I'm officaly a student :)

Monday, June 27, 2011

Particle implementation.

Hi internet!

I've just successfully finished my oral exam (damn that sounds dirty).
So my time as an apprentice is officially over, which means I have more time now to focus on my 2D engine.

In this post I want to show you how I implemented the particle system in my engine using the decorator pattern.

First some UML:

As you might see here I decided to derive the particle emitter into a finite and an infinite subclass. The finite emitter stopps after it spawned n particle (determined by the setParticleAmount() method) whereas the infinite emitter runs as long as isStarted() return true.

The most difficult thing was to cover all the things a particle system must do (e.g. transparency, spreading, collision detection, etc.).






To provide all those contingencies I decided to use the decorator pattern (I just read the book Head First Design Patterns (a really good book), so all that stuff was still in my mind ;) ).

The particle system runs pretty well, although some of the subsystems (especially the transparency) don't work as expected yet.

Tuesday, June 21, 2011

Interesting article

Yesterday one of the guys I'm following on twitter posted this link :
http://www.agner.org/optimize/optimizing_cpp.pdf

It's a link to a very good article about optimizing C++ applications.

Sunday, June 5, 2011

Personal point of view (game engine).

(This post is absolutely IMHO so if you disagree with some points, that's just fine ;) )

Since I'm registered in the GameDev.net forum I noticed many people who were quite clueless wether they should make their own game engine or use a premade one.

I kept asking these questions myself back in the days I started game development and so I though it would be a good idea to make a little post about my personal point of view when it comes to game engines.

Before I start, I'll give you some examples of what I'd like to call good premade game engines:
  • Ogre3D (which is open source)
  • Unreal Development Kit (which is free for non commercial games (I was surpised when I heard about this))
  • Unity3D
  • RPG Maker (Which I made my first game with back in 2001...Man, that game was shitty!)
Check out the websites of these engines to learn more about them.

The first thing you should ask yourself when it comes to choose between selfmade and premade is why you want to make a game (engine).

Are you doing it for the sake of learning or just to, well..., make a game?
And, secondly, are you just doing it for fun or to make some sort of portfolio to get your foot into the industry (and if so,what kind of job do you wish to get? Developer, designer, etc.)?

Thats the points you should be certain about.

If you just want to make a game and don't care about the stuff going on in the background you should be fine with a premade engine. The learning curve should be much higher compared to a selfmade engine. (If you wish to work as a designer even a level editor like the Hammer editor from Valve (which you can get on Steam), or the Unreal ED (which is included in the above mentioned UDK and a part of nearly every installation of a Unreal related game (Unreal Tournament,Unreal)) should be enough to improve your portfolio).

I know that many people disagree with me on this point, but if you are interested in how things work (and especially as a developer) you should definitely start with your own engine. I learned tons of stuff since I started to work on my own 2D engine. Of course, programming your own engine shouldn't be your first project. I would recommend you to start with 2-3 simple games like Pong,Tetris or Pacman(especially Pacman is harder to realize then you might think at first) to get a feeling what components it takes to make a game.

If you are interested in starting your own engine, I can recommend the following books:
If you're stuck at one point and definitely have no idea what's wrong you should register in the above mentioned GameDev forum and search for your issue...Maybe some guy had the same issue before.

Monday, May 23, 2011

Addiction 2.0

Can't post...Must...play...Terraria...such a....great...gaaaammmeeeee aHAHHHAAAA

Tuesday, May 3, 2011

Thoughts about 2D animations.

I know that I shouldn't think of such things like animation systems (considering that I'll write my final exam tomorrow) but it's just something that got to my mind yesterday while I was falling asleep.

Actually I'm using a fairly simple approach for animations.
To create a animated graphic in my engine you got to use the following syntax (The parameters should be self explanatory):
  GraphicsFactory::createAnimatedGraphic(Uint32  animationStepTimeInMilliseconds,Uint8 animationSteps,bool autostart)
After you created the object you can add a graphic to it via:
 animatedGraphicObject.loadGraphic("graphic.png");
(I know its a strange way, but I'll stick with this for now)

When the graphic gets drawn, only the current animationframe will get drawn.

Example:




This System works great for a normal walkcycle like this, but if you have an animation like this:






The system isn't the best solution.

The reason for that is the bounding box (bounding boxes are used for collision detection and are almost always as big as the animation frame) of each animation frame. In the walkcycle example the size of each frame never changes (its always 24x32), but if you see at the second example, especially this frame:





you'll might notice that this frame (109x49) is way bigger than the other frames (62x44 and 80x44).

If the above mentioned system is used, the bounding box of each frame is equally to the bounding box of the biggest frame. (I hope you get what I'm trying to say)

This is a big disadvantage IMHO because if a bounding box collision is detected, the collision system goes into pixel perfect collision deteciton (at least in my engine), which is way more expensive than the bounding box collision.

To avoid this problem I thought of the following approach.

At the point where the user determines the amount of animation frames, the size of each animation frame is known. (Width of the graphic / amount of animation frames = size of the biggest animation frame).

The next step would be to check each pixel of each animation frame for its color value to determine the real bounding box of each animation frame by comparing the color value of the current pixel with the color value of the images color key (The color key is the color in the image that is invisible later in the actual game).

After that step, each animation frame can be packed into an array, so you get a nice animation with perfectly fitting bounding boxes for each frame :)

Sunday, April 17, 2011

Facepalm

 warning C4237: The export-keyword is not supported but is reserved for future use.
One question: Why did they implement it in the first place?

Sunday, March 27, 2011

Graphic creation technique finished!



The content of this video is just me testing the AbstractAnimatedGraphic class.
The graphicfile used for this animation is the following:



The animation is just yoshi, turning around his own axis.
In the video you can see that it is possible to regulate the animationspeed by
key pressing (well you can't see the actual key press).
This feature could be used for fancy slow-motion effects ;)

I also just finished the graphic creation factory.
My engine will support 2 different graphic modes.
  1. Pure SDL (DirectDraw under Windows)
  2. OpenGL
When the function getNewGraphic() from the class "GraphicFactory" get's called,
the engine will check specific configuration flags (which you can change on runtime)
to determine the derived type of the returned AbstractGraphic object (AbstractGraphic is the class that SDLObject and OpenGLObject inherit from).All created graphicObjects are saved to an list to support easy conversion on the fly if the user wants to switch the graphics mode during gameplay.

It's time to sleep now.
I had to work the whole weekend and didn't got much sleep.

Thursday, March 24, 2011

Character designer ready!

I've just finished the character designer I started 3 hours ago.

Now I can easily create new GameActors with specific actions/animations and states for my engine.

Sunday, March 20, 2011

Rendering of levels complete!

I just finished the part of my engine where the levels that got created with the leveleditor gets rendered.
















The left window with the black background is ingame and right window is the leveleditor.

Wednesday, March 16, 2011

Qt Base64 encoding in non Qt application

Hello internet!

Today I'd like to share my experience with you I made today while trying to
decode an Base64 encoded (via Qt) string in a application that does not use Qt (and so does not use Qt for Base64 decoding).

After trying 3 Base64 libraries I though that I have to write the Base64 decoding on my own. Fortunately I found this library a few minutes ago.

Turns out that this library is capable of decoding Base64 string which were created with the Qt Base64 encoding function.

Sunday, March 13, 2011

Got you!

Ha! Just got that error from my last post fixed.
As I postet before (Exactly here)
I'm linking the MFC staticly to my projects to prevent the users from installing the Visual Studio runtimes if they want to play my future games.

Turns out that if I set the MFC linkage to 'dynamicly' everything works fine.
They now share the same 'STL Pool'.

The only remaining question now is what's about the vs runtimes now? :-/
I guess I have to figure that out tomorrow...

Good night, internet.

Sometime I just hate C++

Sometimes I just hate C++.

Like now...
I made a class called "FileAgent", this class has the following method:
 std::string FileAgent::createTmpFile( std::string &content) 

This method creates a temporary file with a random name.
It returns the name of the temporary file and puts a newly created object "FileHandle" in a list where all temporary files gets managed and deleted if they have surved they purpose.

Sounds great, doesn't it?...

I just wanted to test the function before going to sleep, but it turns out that SOMEHOW it is not possible to return an std::string from an DLL to an Executable.

I then tried to return the filename as a c string but that also didn't worked so well (the filename looked like shit).

I guess I have to redesign that somehow to make it work...Damnit...

Thursday, March 10, 2011

New video of the current state (finally!)

I finally managed it to create and upload a new video of the current state of the leveleditor. As you can see, I added several new features like the toolbar (on the left - with the ugly icons ;) ), multilayer drawing, event creation and several other things.



I once again recommend to watch it in fullscreen.

Wednesday, March 9, 2011

DOM Xml parsing don't do's #1

 root.appendChild(root); 


The line above caused confusion, anger and frustation.
Time to sleep, I guess.

I also would like to apologize for not writing the last month.
I had and still have a lot of work to do (not related to the leveleditor or engine).

We'll get a new ERP System at work in the next weeks (preperation is going on currently) , I need to learn for my final exams and I need to start my finish paper.

Monday, January 31, 2011

Qt is just awesome

I just have to say it over and over again.
Qt is just awesome.

Everytime I think of, for example, toolbars...How I could realize them in Qt, I just
open google and search for the tool I'd like to use and add an "Q" at the begining.

"QToolbar"
"QThread"
"QWizard"

It's always a hit.

Thursday, January 27, 2011

Finally got QT static linkage working! (How-to inside)

Yesterday I gave the current build of the leveleditor to a friend to test if the application runs correctly. He got an "Side-by-side configuration is incorrect" error when he wanted to start the editor.

After some investigation I found out that since VS2005 and Windows Vista you have to ship the Visual C++ Runtime library (Downloadlink see below) with your application to make them run properly.
(You'll notice the installation of the runtime files after you start nearly every game the first time in Valves Steam).

So you now have two choices...Either you ship your application with the runtime install files or you link the runtime library staticly to your application.Easy choice yeah...?

Well no. I was FORCED to find out that's it not that easy.

The tricky part is that every library you use in your application also has to be staticly linked with the runtime files.

So if you use foo.dll (DLL = Dynamic link library) and want to use it with your bar.exe but don't want the user to install the visual c++ runtime you'll have to compile foo.dll as foo.lib. To do that you have to open your DLL-project in Visual Studio and go to Project->Settings->Configurationsettings->Common. Set "Configurationtype" from "*.dll" to "*.lib" and choose "use MFC in a static library" at "Usage of MFC" after that go to "C\C++ -> Code generation" and change "Multithreaded Debug DLL" to "Multithreaded Debug" (Or "Multirthreaded DLL" to "Multithreaded" if you want to build a release).

If you've done that you do the same in your application project and then you are ready to go.

For QT-Developer:
If you want to staticly link Qt library you have to do the following:
  1. Download the OpenSource from the QT libraries.
  2. After you unpacked everything start the command prompt from Visual Studio.
  3. Go to the root directory of the unpacked sourcecode (the same directory where bin\ is in)
  4. Enter "configure -static" (during this process an project.sln will get created)
  5. Open Visual Studio and open the just created "project.sln"
  6. Choose the parts of QT you want to build and xo everything mentioned above to staticly link the runtime files.
If you've done that you should be able to staticly link the QT libraries.

If you don't want to link staticly, here you'll get the runtime installfiles:

Friday, January 21, 2011

Gesundheit!

I won't release a new video until end of the next week.

I didn't really program much new in the past days
because I'm down with influenza.

I hope I'll get better soon.

Thursday, January 13, 2011

QRect.setX() and QRect.setY() are evil!

I tried to tile an image using this algorythm:

 bool copyTileContainerFromImageFile( QString &fileString, QVector<Tile*> *sourceVector )  
{
if(fileString.isEmpty())
return false;
QRect rect(0,0,32,32);
QPixmap originalImage(fileString);
//goal = amount of possible tiles in this image.
int goal = (originalImage.width() * originalImage.height()) / 1024; //1024 Pixels fits in one Tile
sourceVector->reserve(goal); //reserves the amount of images in the vector.
for(int i = 0;i != goal;i++) {
sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
if(rect.x() + 32 >= originalImage.width()) {
rect.setY(32 + rect.y());
rect.setX(0);
}else
rect.setX(32 + rect.x());
}
return true;
}


the result using this algorythm was this:



If you're now like "WTF?" then I can tell you that "WTF?" was my first reaction,too.
I really had no freakin' idea why the image got tiled like THAT.

After I posted in some forums and read through the documentantion I changed
the algorythm to this:
 bool copyTileContainerFromImageFile( QString &fileString, QVector<Tile*> *sourceVector )  
{
if(fileString.isEmpty())
return false;
QRect rect(0,0,32,32);
QPixmap originalImage(fileString);
//goal = amount of possible tiles in this image.
int goal = (originalImage.width() * originalImage.height()) / 1024; //1024 Pixels fits in one Tile
sourceVector->reserve(goal); //reserves the amount of images in the vector.
for(int i = 0;i != goal;i++) {
sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
if(rect.x() + 32 >= originalImage.width()) {
rect.moveTop(32 + rect.y()); <--was setY(32 + rect.y()) before
rect.moveLeft(0); <--was setX(0) before
}else
rect.moveLeft(32 + rect.x()); <--was setX(32 + rect.x()) before
}
return true;
}

Now everything works fine.

EDIT:
Nice tool for everyone who's posting sourcecode in blogspot posts:
Code formatter

Tuesday, January 11, 2011

The problem about extreme programming

When I started with the tilesetmanager of my tileeditor I was like "How big could it be?I just do a little bit coding here and some geek stuff there and then everything should work fine".

Today is payback time.
I faced an error in the tilesetmanager and had no idea where to look for the error on the first look. No commentation and barely readable code.

I figured out the error but now I'll go and redesign the datamodel of the tilesetmanager because I can't live with the shame that I programmed something that ugly.

Long story short: I shouldn't do that anymore. (Although that's really not my style usually)

Monday, January 10, 2011

Happy new year (yeah..A little bit late...I know)

Woosa I'm back.
My holidays ended yesterday and now the cold,cruel world got me back.

Bad things first:
I really REALLY wanted to code during the holidays but I made a big mistake...I bought Oblivion G.O.T.Y Edition (Shivering Isles and Knights of Nine included) on Steam during the holiday specials and spent nearly the whole holidays playing it... But what's even worse is that I didn't even finished it. :-/ Such a big game with millions of opportunities ...agh!
Time swallowing beast!

I also realized that I apparently have something like a "programming-Karma"... Everytime I plan to do some programming but then end up gaming, my karma does something bad with my car.The last time I had to pay EUR1.400 at the car workshop (thanks to Red Dead Redemption). This time the windshield from my car got smashed during New Year's Eve (Thanks to god it costs me "only" EUR150).

Well yeah... Never mind.

What I originaly wanted to say is that I'll release a new video of the current state of the leveleditor at the end of the week.