Page 2 of 3
Re: Game engine, C++,SDL (I'm new.)
Posted: Thu Sep 22, 2011 6:32 pm
by Falco Girgis
...moving to programming discussion for the 999th time.
Re: Game engine, C++,SDL (I'm new.)
Posted: Thu Sep 22, 2011 8:45 pm
by Benjamin100
Sorry Falco,
I wasn't quite sure where to put it. I'm new to the forum.
Anyhow, I did figure out a way to make tiles.
Thank you,
-Benjamin
Re: Game engine, C++,SDL (I'm new.)
Posted: Thu Sep 22, 2011 8:46 pm
by Benjamin100
Is Game Development more for general creative issues?
Re: Game engine, C++,SDL (I'm new.)
Posted: Thu Sep 22, 2011 10:23 pm
by Falco Girgis
Benjamin100 wrote:Is Game Development more for general creative issues?
Exactly. It's about a game as a whole or the process as a whole. It's more about a product. If you make a topic talking specifically about art/music, it gets moved there. If you make a topic talking specifically about programming, it gets moved here.
It's cool. It's not like 50 other people don't do it too. I'm just feeling more anal about enforcing it lately. And honestly, you'll get way more feedback about things like this in programming discussion.
edit: Oh wow, and the OP's first sentence was "I don't know where to put this." I didn't mean to come off as a dick about it. My bad.
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 1:16 pm
by Benjamin100
Lol. It's fine Falco. I bet you have a lot of work on your hands.
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 6:28 pm
by Benjamin100
Okay. This time I don't think I'm at all able to figure out the answer by myself...
I have a sprite for a selected tile...
And sprites with transparent backgrounds...
all sprites on same sheet, just changing source.
But when I select a tile and apply an image tile from the side of the screen, in its transparent background I see the selected tile image.
Any idea why this is?
Should I show you some of the code?
It basically changes the type of tile of the selected tile, which when I run an updating function through a loop, changes the source x,y to the source of the tile type's image.
Does that make sense? Does it seem like that is a bad way of doing things? It all seemed to work fine until I put a transparent background for the tile image in, and now I ee the old selected tile image around it.(Selected being a type of tile.)
Is there anything in that that stands out as possibly causing a problem?
Thanks,
-Benjamin
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 7:28 pm
by Live-Dimension
I've read through this four times and still can't understand what you're trying to say. Help us help you, show some code, show a screenshot of whats going on, even show the sprite sheet.
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 8:16 pm
by Benjamin100
Well there is a LOT of code, and I think it would take you quite a while to read it all.
tile class;(of which there are a couple arrays, one being tiles types to choose from, the other being tiles on the screen to select and change with the previously mentioned array of tiles.)
the function in tile class that changes the "version" of the tile(or tile's type.);
Code: Select all
int update_version()
{
if(version==0) //blank tile.
{
source.x=0;
source.y=0;
source.w=50;
source.h=50;
}
if(version==1) //selected tile
{
source.x=50;
source.y=0;
source.w=50;
source.h=50;
}
if(version==2) //tree top.
{
set_source(0,50);
}
if(version==3) //tree bottom.
{
set_source(0,100);
}
if(version==4) //grass.
{
set_source(50,50);
}
}
"version" is for each tile "on the board", if you will, and when the update function is run, it is supposed to give them the image source of that version's image.
I seem unable to get the image up, but it is 50x50 for each tile,
So my problem is that when it changes version to the tree top, which has transparency, in the transparent region I see the "selected tile" image.
It is as if it keeps drawing the
selected tile image below the
tree top image.
(Please note this is not dealing with layers yet. I plan to move the trees to another layer, but I am currently testing things out with just one layer.)
This is in my loop;
Code: Select all
for(int l=0;l<140;l++) //loop for each tile.
{
total_tiles[l].update_version();
draw_it(screen,total_tiles[l].sprite,total_tiles[l].source,total_tiles[l].location); //Draw_it only draws one at a time.
SDL_Flip(screen); //Draws each tile.
}
If there is something here that you think is problematic, please show it to me. If you think you would need to see more, please let me know.
Thank you,
-Benjamin
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 8:19 pm
by Benjamin100
It appears some of my comments in my code are out of place, but I'm sure you could tell if/where they are.
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 8:24 pm
by Benjamin100
Oh, and "set_source" is just for x and y, since I realized the others were staying 50x50, as that is to be the size of every tile.
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 10:04 pm
by Live-Dimension
Just a few tips that will (probably) provide cleaner code and faster too. You can safely ignore what I'm doing here for now if you want, but it is something you'll want to learn one day.
Code: Select all
//Don't forget, you can always wrap this in a namespace as to not pollute this scope!
enum tileType {
Blank,
Selected,
TreeTop,
TreeBottom,
Grass
};
int update_version()
{
//this is just a guess, but i assume that all tiles are 50x50. If so, you can safely put this somewhere else
source.w=50;
source.h=50;
switch(version)
{
case Blank:
{
source.x=0;
source.y=0;
}
case Selected:
{
source.x=50;
source.y=0;
}
case TreeTop:
set_source(0,50);
case TreeBottom:
set_source(0,100);
case Grass:
set_source(50,50);
}
}
It's also really odd to have tiles that are 50x50. Almost always, sprites are a power of 2, and have the same dimensions, ie, 32x32, 64x64, 128x128. The reason for this has to do with binary and that it's simply alot faster to process these figures when it comes to hardware acceleration. I'm not so sure if this applies to software CPU blitting however. I won't go into why this is the case, just that when it comes to using DX/OGL, its faster by a long shot. Older graphics cards won't even render textures if they aren't a power of 2.
Code: Select all
for(int l=0;l<140;l++) //loop for each tile.
{
total_tiles[l].update_version();
draw_it(screen,total_tiles[l].sprite,total_tiles[l].source,total_tiles[l].location); //Draw_it only draws one at a time.
SDL_Flip(screen); //Draws each tile.
}
SDL_Flip(screen); //Draws each tile.
um..... what? You're flipping the buffers each time you draw a single tile? Maybe that should be outside the for loop. It should (usually) be the last thing the graphics engine does per frame, or atleast, the last drawing call. I don't know what the exact code you should be using as I don't use SDL, but that's just wrong. We all make mistakes like this from time to time.
Re: Game engine, C++,SDL (I'm new.)
Posted: Fri Sep 23, 2011 11:02 pm
by Benjamin100
Okay.
Well I put the SDL_Flip outside the loop now.
It does seem to be running a lot faster.
(I was wondering why it was so slow.) Thanks. I should have thought to try that myself.
Any idea why I still see the selected tile type image behind the transparent part of the tree image when it is applied?
Thank you,
-Benjamin
Re: Game engine, C++,SDL (I'm new.)
Posted: Sat Sep 24, 2011 12:21 am
by Benjamin100
LOL!
Fill Rect!
I JUST thought of it!
LOL. I feel stupid. I hope I'm not the only one who makes such obvious mistakes.
-Benjamin
Re: Game engine, C++,SDL (I'm new.)
Posted: Sat Sep 24, 2011 10:54 am
by dandymcgee
Benjamin100 wrote:LOL!
Fill Rect!
I JUST thought of it!
LOL. I feel stupid. I hope I'm not the only one who makes such obvious mistakes.
-Benjamin
Yup. You need to clear the screen before you start drawing transparent images, or it will simply draw over what is already there. This isn't necessary if you are always drawing enough to replace every pixel with a new one.
Re: Game engine, C++,SDL (I'm new.)
Posted: Sat Sep 24, 2011 12:01 pm
by Benjamin100
I think I didn't notice before because all the tiles were the same width and height, and hadn't been made transparent yet.