Page 1 of 19

Blade Brothers Engine: Creating my first 2D Game Engine

Posted: Sat Jan 30, 2010 5:10 am
by LeonBlade
Hello everyone,

I've been working on making a simple engine in C++ using SDL (possibly integrating OpenGL).

I'll be going through my steps in creating the engine, and you guys can correct me on things if I make a mistake or if you feel that something should be done a different way.
This is my first Game Engine, and I'm new to the whole thing.

Thanks everyone.

Re: Questions on Creating a Game Engine

Posted: Sat Jan 30, 2010 6:22 am
by GroundUpEngine
LeonBlade wrote:Hello everyone,

I've been working on making a simple engine in C++ using SDL (possibly integrating OpenGL).
Now, the question I have, is how to structure a game engine...

I'm stumped on this really, I know that you need to make classes for textures and sprites or objects etc etc.
But I'm not entirely sure where to go or what to do...

If someone could point me in a direction where I can find how I should structure a game engine.
Right now I made a "GameObject" class and a "Game" class but I feel as if I'm not going about it right...

Thanks, hope you guys can be of assistance.
Sup, I'm working on an engine myself too so I'll tell you what I can 8-)

I'm not pro but I know that a good game engine needs a good structure, that is what you have to think about ;)
You need to think what your engine is going to be able to do, and plan the main basic structure but still think big e.g. ->

______________Engine Core______________
General | Graphics | Game Elements | Logic


Here your engine core handles the main classes that you have coded.

_________________General_________________
Windowing | Input | Timer | CPU/GPU Anaylsis

_______________Graphics(SDL/OGL)_______
2D (e.g. Menus, Sprites) | 3D | GUI | Models

_________________Game Elements_________________
Texture | Audio | Camera | Lighting | Client-Server :P

____________Logic____________
Vector | Algebra | Physics | etc..


To make things clear, an example of your engine functionality would be, drawing/handling a game object ->

Code: Select all

// Here you can see the flow of the engine //
Engine->Graphics->2D->DrawObject(box);
Engine->Logic->HandleObject(Vec2, box);
Edit: or even better, make a function in the engine that makes these calls ->

Code: Select all

Engine::DrawBox2D(Vec2 pos, int tex)
{
	Graphics2D->DrawRect(pos, tex);
	Logic->HandleObject(pos);
}

// So now you can do this //
Engine->loadtexture("box.png", 1);
vec2 box_pos(10, 10);
drawing_loop
{
    Engine->DrawBox2D(box_pos, 1);
}

Re: Questions on Creating a Game Engine

Posted: Sat Jan 30, 2010 7:11 am
by LeonBlade
Thanks for the reply, I saw your topic before.

Right now I'm trying to work with textures.
I was looking at using map to store my textures and use a texture manager basically.
I'm just gonna post basically a log of what I'm doing here, if anyone wants to tell me what I should do instead of what I'm doing please post here!

Right now I'm working on the graphics portion of the Engine, it's not going to be a large engine, just my first trying to get in the basics.

This is how I want my code to look like for loading a new texture:

Code: Select all

BETextureManager tm;
tm->AddTexture("filename.png");
I would create an instance of my BETextureManager class somewhere globally (I would assume) and then to add a texture I would pass in the file name that I want to load.

There would be a BETexture struct something like this I guess:

Code: Select all

struct BETexture {
     SDL_Surface *t;      // the SDL_Surface for the texture you load in
     int width,height;   // the width and height of the texture?
     SDL_Color tpc;      // the transparent color for the texture?
     // anything else need to be added here?
};
There would be a map for textures like this:

Code: Select all

std::map<uint, BETexture> be_textures;
The AddTexture function would look something like this:

Code: Select all

BETextureManager::AddTexture(std::string filename) {
     be_textures[INDEX] = IMG_Load(filename.c_str());   // the INDEX will be figured someway... not sure yet probably just "pushing" the new texture
     //do the rest of the stuff with the texture like transparency etc...
}
So now that the texture is stored with AddTexture you can apply the texture to a sprite or whatever and render it etc.

Does this sound like a good idea?
I'll be posting more on Sprites once I do this...

Re: Questions on Creating a Game Engine

Posted: Sat Jan 30, 2010 7:33 am
by GroundUpEngine
LeonBlade wrote: There would be a map for textures like this:

Code: Select all

std::map<uint, BETexture> be_textures;
The AddTexture function would look something like this:

Code: Select all

BETextureManager::AddTexture(std::string filename) {
     be_textures[INDEX] = IMG_Load(filename.c_str());   // the INDEX will be figured someway... not sure yet probably just "pushing" the new texture
     //do the rest of the stuff with the texture like transparency etc...
}
So now that the texture is stored with AddTexture you can apply the texture to a sprite or whatever and render it etc.
cool, ye that would do the job, but make sure the other classes can use the texturemanager properly rather than doing it on the fly

I use a texturemanager aswell, it handle textures for everything in the engine, I don't use map though but I think it's a great idea!
also your BETexture struct doesn't really need anything else, once the texture, width/height and color/alpha is handled it's pretty much good to go ;)

Re: Questions on Creating a Game Engine

Posted: Sat Jan 30, 2010 10:09 am
by LeonBlade
Alright I have a basic TextureManager system going.
Right now it has a render function only so I could test to see if the textures were loading etc.

Code: Select all

btm->Render(screen, -1);
The first parameter is the destination surface and the second is the ID of the texture you wish to render.
If you pass in -1 for the ID it renders all that you passed in, but like I said, this is just temporary ;)

Here is a screenshot of that last code call:
Image

Next I'll be working on a BESprite class that will handle the source rectangle from the texture, and the destination rectangle for the destination surface (the screen).

Here's what I wanted to do for that:

Code: Select all

BESprite mySprite;
mySprite.loadTexture(btm.GetTexture(0, NULL));  // GetTexture(id, source_rect) 
mySprite.x = 100;
mySprite.y = 50;
This would do as expected... it would load in the texture from the BETextureManager with an ID of 0.
Then it would position it 100 pixels on the X axis and 50 pixels on the Y axis.
Note the comment next to the loadTexture function.
An optional parameter for the source rectangle can be passed in (this is useful when loading from a tilesheet however this may be managed in it's own separate class).

And rendering would be as follows:

Code: Select all

mySprite->Render(screen);
Passing in screen as the destination surface.

I'll post that once I get that done, any suggestions?

Re: Questions on Creating a Game Engine

Posted: Sat Jan 30, 2010 10:28 am
by GroundUpEngine
LeonBlade wrote:Here's what I wanted to do for that:

Code: Select all

BESprite mySprite;
mySprite.loadTexture(btm.GetTexture(0, NULL));  // GetTexture(id, source_rect) 
mySprite.x = 100;
mySprite.y = 50;
And rendering would be as follows:

Code: Select all

mySprite->Render(screen);
Passing in screen as the destination surface.

I'll post that once I get that done, any suggestions?
Nop, looks pretty much legit to me, cool engine name btw ;)
I miss SDL :cry:

Re: Questions on Creating a Game Engine

Posted: Sat Jan 30, 2010 10:39 am
by LeonBlade
GroundUpEngine wrote:
LeonBlade wrote:Here's what I wanted to do for that:

Code: Select all

BESprite mySprite;
mySprite.loadTexture(btm.GetTexture(0, NULL));  // GetTexture(id, source_rect) 
mySprite.x = 100;
mySprite.y = 50;
And rendering would be as follows:

Code: Select all

mySprite->Render(screen);
Passing in screen as the destination surface.

I'll post that once I get that done, any suggestions?
Nop, looks pretty much legit to me, cool engine name btw ;)
I miss SDL :cry:
I'm pretty new to it... however I have done stuff with it before.
And I just thought of the name Blade Engine from Leon Blade and Blade Brothers is the name of my "team" if you want to call it that ;)
Basically me and my cousin have been coming up with a game story line for most of our life blah blah blah etc etc.

I'm going to change the topic name to "Blade Engine: Creating my first 2D Game Engine with SDL" because that seems to be a better name for it.

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sat Jan 30, 2010 10:53 am
by programmerinprogress
Keep up the good work, It's always a plus to know people are working on such interesting and intricate projects :)

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sat Jan 30, 2010 11:56 am
by LeonBlade
Alright, update!

I got BESprite done (for the most part).
This is what it looks like:

Code: Select all

BESprite mySprite;
mySprite.LoadTexture(btm.GetTexture(0));   // you can also pass in a source rectangle
mySprite.Move(position, true);
...
mySprite.Render(screen);
That is loading in a texture, moving it relative to it's current position, and then rendering it.
The render is in a different function (not the init function) but I put it in the same code block.

The relative bool is so I can move the sprite relative to it's current position, rather than moving it to a direct position.
I created a new struct called BEPoint that is just a regular point with a x and y value.

Here is an example of loading in a sprite and I'm using the source rectangle to crop out a part of the texture:
Image

Next I want to do something with input... controlling the sprites like a BECharacter class perhaps?
How should I go about this?

Should it inherit the BESprite class?
Or should it be a class of it's own?
And I plan on having NPCs in the Engine and then Characters that would inherit from NPCs or BLAH something.

Anyone have some suggestions?

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sat Jan 30, 2010 12:39 pm
by GroundUpEngine
LeonBlade wrote:I got BESprite done (for the most part).
This is what it looks like:

Code: Select all

BESprite mySprite;
mySprite.LoadTexture(btm.GetTexture(0));   // you can also pass in a source rectangle
mySprite.Move(position, true);
...
mySprite.Render(screen);
Should it inherit the BESprite class?
Or should it be a class of it's own?
And I plan on having NPCs in the Engine and then Characters that would inherit from NPCs or BLAH something.

Anyone have some suggestions?
Nice Work!
The idea of BEPoint sounds similar to Vector2 which is good, BECharacter class should inherit BESprite so you can assign a texture to a NPC :) ->

Code: Select all

class BECharacter: public BESprite {
  private:
    BEPoint pos; // NPC position var //

  public:
    BECharacter(int x, int y) { // Init Constuctor //
        pos.x = x;
        pos.y = y;
    }
    BEPoint BECharacter::GetPosition() { // Find NPC position //
        return(pos);
    }
};

Code: Select all

// So you can //
BECharacter bob(100, 100);
bob.LoadTexture(btm.GetTexture(0));
etc..

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sat Jan 30, 2010 1:13 pm
by LeonBlade
Thanks GroundUpEngine I'll get to work on that next :)

But first, I figured it would be best for me now to make a BETilesheet class so that I can render tiles on the screen and then make a level editor.
So far this is how my BETilesheet class looks like, and how I plan on doing it:

Code: Select all

BETilesheet::BETilesheet(BETexture sheet, int rows_w, int rows_h) {
     for (uint x=0; x<rows_w; x++) {
          for (uint y=0; y<rows_h; y++) {
               SDL_Rect tileRect;
               tileRect.w = tileRect.h = 32;
               tileRect.x = x*32;
               tileRect.y = y*32;
               tiles[x, y] = tileRect;
          }
     }
}
If you saw the line where I called the tiles[x, y] = tileRect that is map that looks like this.

Code: Select all

std::map<int[,], SDL_Rect> tiles;
Now this is using it in practice:

Code: Select all

BETilesheet mySheet(btm.GetTexture(0), 10, 5);  // 10 by 5 is the tiles 10 tiles wide and 5 tiles tall
BESprite mySprite;
mySprite.LoadTexture(btm.GetTexture(0), mySheet.GetTile(0,0));
...
mySprite.Render(screen);
Something like that... the line where I call GetTile I pass in the tile's X and Y position on the tilesheet and that will retrieve the SDL_Rect for the source of the tile on the tilesheet.

More on that in a few...

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sat Jan 30, 2010 6:17 pm
by Live-Dimension
Sorry to be the bringer of bad news (don't shoot the messenger?) but

http://www.blade3d.com/

http://www.bladeengine.com/

I'd hate to see you get sued or whatever because of the name. It may or may not be legal, just thought I'd point it out ;)

I've been keeping an eye on this. Good work so far!

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sat Jan 30, 2010 10:29 pm
by LeonBlade
Ah, well thank you!
I probably should of Google'd it first huh? :mrgreen:

I'll just change it to BBE for Blade Brothers Engine.

Re: Blade Engine: Creating my first 2D Game Engine with SDL

Posted: Sun Jan 31, 2010 6:17 am
by GroundUpEngine
Live-Dimension wrote:Sorry to be the bringer of bad news (don't shoot the messenger?) but

http://www.blade3d.com/

http://www.bladeengine.com/
Damn that sucks :nono:
LeonBlade wrote: I'll just change it to BBE for Blade Brothers Engine.
I like that, sounds more original :)

Re: Blade Brothers Engine: Creating my first 2D Game Engine

Posted: Sun Jan 31, 2010 1:05 pm
by LeonBlade
Hey everyone, sorry I haven't been posting much in here.
I've been sick lately and I just didn't have the energy to program :(
I have the spritesheet pretty much fleshed out in the posts above, so I'll just implement that and soon get start on a level editor of some kind.

Right now I'm stuck converting all the project files to BBE from BE.

Okay, I created a new project instead and finished changing all the files over.
Now I'll get to work on the BBESpriteSheet class.