Blade Brothers Engine: Creating my first 2D Game Engine

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

User avatar
isamgray
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Mon Jul 19, 2010 11:24 am
Current Project: Boxy Engine
Favorite Gaming Platforms: Xbox360
Programming Language of Choice: C/++ and Objective-C
Location: Dallas, TX
Contact:

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

Post by isamgray »

LeonBlade wrote:I'll look more into how to create textures without SDL when I wake up, it's 10 AM time for sleep haha. :lol:
Thanks for the help Milch!
If you'd like, I can give you my TextureLoader C++ class that I made for my engine. It's not great, but it'll show you the basics of loading a .png image with libpng, and setting it up for use with OpenGL, with full transparency working. I'm not completely done with the class, but it does work. If you want it, just send me a PM and I'll upload it somewhere for you.

Also...about your sluggishness when you start rending the map that way as well. Are you re-rendering each tile for the map each frame? Cause if so, that's the problem for you sluggishness...I fixed it by rendering the entire map to a texture, so I just have to use one draw call to draw the entire map to the screen.

I hope this helps. ^^
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post by XianForce »

Well, first of all... I'm not sure I understand why your using SDL_BlitSurface... You should switch completely to OpenGL. As for loading images, you can still use SDL, and then just convert to an OpenGL texture. Personally I use DevIL for image loading.

Also for the sluggishness, you may want to check and see if double buffering is enabled by default, if not a simple call to:

Code: Select all

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
will do the trick :)
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

The reason I was using blitting was because I have no idea how to take only a source of my texture and display it on screen.
I have a full sprite sheet and I only need a portion of it and that changes all the time for characters.

How do I render a GL_QUAD with only a portion of my selected texture?

Okay, so I figured out how, haha.
But the transparency isn't working :(
Getting there though eating Pizza Hut now :)
There's no place like ~/
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post by XianForce »

LeonBlade wrote:The reason I was using blitting was because I have no idea how to take only a source of my texture and display it on screen.
I have a full sprite sheet and I only need a portion of it and that changes all the time for characters.

How do I render a GL_QUAD with only a portion of my selected texture?

Okay, so I figured out how, haha.
But the transparency isn't working :(
Getting there though eating Pizza Hut now :)
Yeah, Tex Coords =p.

And I believe you have to enable transparency in OpenGL, because alpha blending is off by default (I may be wrong though).

Also when you send the SDL_Surface to an OpenGL texture remember to set the right data format, and color format. Sooo make sure your SDL_Surface has alpha support, and let OpenGL know that your new texture should have it as well.

(Also, don't forget to free the SDL_Surface after converting to an OpenGL texture, I forgot to do this, and had a small memory leak that took me a while to catch haha).
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

XianForce wrote:
LeonBlade wrote:The reason I was using blitting was because I have no idea how to take only a source of my texture and display it on screen.
I have a full sprite sheet and I only need a portion of it and that changes all the time for characters.

How do I render a GL_QUAD with only a portion of my selected texture?

Okay, so I figured out how, haha.
But the transparency isn't working :(
Getting there though eating Pizza Hut now :)
Yeah, Tex Coords =p.

And I believe you have to enable transparency in OpenGL, because alpha blending is off by default (I may be wrong though).

Also when you send the SDL_Surface to an OpenGL texture remember to set the right data format, and color format. Sooo make sure your SDL_Surface has alpha support, and let OpenGL know that your new texture should have it as well.

(Also, don't forget to free the SDL_Surface after converting to an OpenGL texture, I forgot to do this, and had a small memory leak that took me a while to catch haha).
I'm not using SDL_Surfaces anymore yay! Haha, thank you so much though I truly appreciate it.
I'll have to see about the transparency enabling thing... it shows yellow instead of transparent :shock:

I figured out how to use libpng like others suggested so now I'm only using SDL for events (which is good because my main structure of the engine is based off of it!)

Next is figuring out transparency and getting my frames to work right again!
I got transparency working now :)

Thanks for everyones support thus far.

ALSO DOES ANYONE KNOW HOW YOU CAN GET THE DIMENSIONS OF AN OGL TEXTURE?
Like, one that's already made I use it for when I draw the tiles.
Thanks.
There's no place like ~/
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post by XianForce »

Well as for transparency, I always hear people have problems with converting SDL transparency to transparency in OpenGL... I figure it's because SDL handles it in software...

So you can either load a PNG that has transparency OR make your own color keying stuff in OpenGL... Lazy Foo outlines it in this article:
Starting Out With OpenGL Just scroll down to the stuff about Color keying... it's like number 5 or so.

Most people take the former solution though... seems like less of a hassle.

As for getting the image dimensions... I'm not sure how you get it through OpenGL, but I'd just handle it in it's own class. So like in my texture class I hold the width and height, and when the image is loaded (I use DevIL to load images...), a simple call to ilGetInteger with the parameter as either IL_IMAGE_WIDTH OR IL_IMAGE_HEIGHT.

Sooo I guess you'll have to check out libpng stuff...

EDIT:

Want to let me know how you got it to work? I'm just curious haha =p
But I'd definitely recommend going through that article Lazy Foo has. In it, he doesn't really tell you in code how to do it, rather just explains the steps to do it, while giving a few hints. I think it's a great approach, and really helped me learn alot =D.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

Code: Select all

// enable alpha
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Hehe :)

The way I load my images is different than how I normally do, so I'll just pass more arguments to the draw method and store the width and height in the object that holds the texture itself.

I have a sprite class which loads and returns textures and then also draws them.
But it's just a class you call like this...

Code: Select all

GLuint texture = Sprite::OnLoad("my_texture.png");

...

Sprite::OnDraw(texture, position, frame);
So I'll just pass the information through there perhaps? So like this instead...

Code: Select all

Sprite::OnDraw(texture, position, size, frame);
Not sure quite yet, but that seems kind of unnecessary to me.
What do you think?
There's no place like ~/
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post by XianForce »

Wait, you mean like pass the width and height as a parameter? That seems a bit unnecessary...
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

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

Post by qpHalcy0n »

If you REALLY want to obtain the texture width/height from OpenGL, bind the texture, and call:

Code: Select all

int width, height;
width = height=  0;

glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
This obtains the TOP LEVEL width/height. So if you have a mipchain, then you need to substitute the 2nd parameter for the appropriate LOD you want to query.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

XianForce wrote:Wait, you mean like pass the width and height as a parameter? That seems a bit unnecessary...
Very :lol:
qpHalcy0n wrote:If you REALLY want to obtain the texture width/height from OpenGL, bind the texture, and call:

Code: Select all

int width, height;
width = height=  0;

glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
This obtains the TOP LEVEL width/height. So if you have a mipchain, then you need to substitute the 2nd parameter for the appropriate LOD you want to query.
Thanks :)
There's no place like ~/
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

Okay, so I went with creating a struct for SpriteTexture that holds the texture itself a GLuint and the width and height both int.
Now, I tested my game and I get no map... and I'm thinking it HAS to be something with the rendering, and I mess around with that for an hour.
So I'm like, okay, what the hell is going on here... my player loads but this wont?

So after an hour or so of fucking ab out I decided to set the map tileset to the player sheet and saw it rendered.
At this point I was just confused...

Then I made the full 256x256 px tileset (which originally was small and only had a few tiles filled on the top left) into a full checkerboard with numbers so I could see if stuff was being out of place.

And I saw EXACTLY what went wrong, lets see if you can spot it...
Image

See it? Well, my tile numbers should be 1 for the ground and numbers around the 1-10 range or so, not in the 50s and 60s!
So, basically what happened was my texture is upside down... sorta.
OpenGL is different in that the 0,0 coord is at the bottom left instead of the top left like some people (myself included are used to) but I always took account for that when doing this. I then remember I DID have to mess with my player facing constants to get the right tile loaded before...

So now it all makes sense!
My question is, how do I flip my tiles around so that I'm loading tiles how I normally should?
I mean, I could very well draw tiles backwards and draw them starting from the bottom and go up... but that doesn't look pretty.

Let me know if you can help me out!
And thanks again for everyone who has helped out so far, this is my first experience now with OpenGL, and it's nice to have a lot of caring members on these forums that are willing to assist others.

Thank you so much! <3
There's no place like ~/
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

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

Post by Milch »

You calculate wrong what part of the texture each quad should use.
Thats the only possible error I can think of.
Maybe post code.
Maybe not.
Follow me on twitter!
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

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

Post by XianForce »

Milch wrote:You calculate wrong what part of the texture each quad should use.
.
Yeah, he said that ;).



Well the first fix that comes to mind is instead of doing the usual:

Code: Select all

x = (tileIndex % tilesInRow) * (tile width);
y = (tileIndex / tilesInRow) * (tile height);
You'll have to flip the y around by doing something along the lines of:

Code: Select all

y = texture_height - (tileIndex / tilesInRow) * (tile height);

That isn't too ugly I don't think? I'm pretty sure it should work as well...

EDIT: Also another bit of curiousity... How do you set up your Ortho projection? Like, is (0, 0) the top left on your window?


EDIT2:

Actually even better than what I mentioned before...

The way I mentioned would mean that every time you went to render anything from a sheet, you'd have to revert the y axis basically... So in your function where you render textures with a clipping rect do something like this with the tex coords:

Code: Select all

glTexCoord2f(x / textureWidth, (textureHeight - y) / textureHeight);
And that will revert it for you each time (I think...). =D
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

My ortho:

Code: Select all

glOrtho(0.0f, SCREEN_W, SCREEN_H, 0.0f, -1.0f, 1.0f);
Here's my Sprite::Draw:

Code: Select all


bool Sprite::OnDraw(SpriteTexture texture, Vector2 position, SDL_Rect tile)
{	
	glBindTexture(GL_TEXTURE_2D, texture.texture);
	
	glBegin(GL_QUADS);
	{		
		float min_x = (float)(tile.x)/texture.width;
		float min_y = (float)(tile.y)/texture.height;
		float max_x = (float)(tile.x + tile.w)/texture.width;
		float max_y = (float)(tile.y + tile.h)/texture.height;
		
		glTexCoord2f(min_x, max_y); glVertex2i(position.x, position.y);
		glTexCoord2f(max_x, max_y); glVertex2i(position.x + tile.w, position.y);
		glTexCoord2f(max_x, min_y); glVertex2i(position.x + tile.w, position.y + tile.h);
		glTexCoord2f(min_x, min_y); glVertex2i(position.x, position.y + tile.h);
	}
	glEnd();
	 
	return true;	
}
This is what I'm currently using, how should I flip my Y tile order from this?
(Also, let me know if this is too much hehe)
There's no place like ~/
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

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

Post by LeonBlade »

Image
Thanks a lot XianForce for the help :)
Next is... uh... well re-doing my entire Editor in Qt :mrgreen:

As of now, there's no way to make maps without me hand editing them >_> so I should probably do that (considering it's all in binary haha).
There's no place like ~/
Post Reply