I have an easy question that's hard to ask Google

Started by
1 comment, last by arthursouza 11 years, 6 months ago
I'll make this simple. Here's what I'm trying to do and how I'm currently doing it. My question is, are there any methods I'm not aware of for doing it better?


Basically I'm working 100% in 2d (directx 9) using textures and sprites. Very simple. I want to draw a square sprite that can be of variable height and width. It's a top-down view of a "room" with "walls" on all 4 sides. Of course you can't just draw that in a bitmap and scale it because the "wall" bordering would be stretched.

So I have 8 bitmaps. The floor, the 4 walls, and the 4 corners. I make 8 sprites, draw the floor scaled to whatever size, then draw the walls and corners in the proper bordering positions. The "walls" are 1 pixel wide so they can be scaled properly. (see attached images)

Is there a better way to do this? Perhaps a way to attach multiple textures to one sprite?

Thanks for any help!
Advertisement
you could make Your sprite class have child sprites. And the child sprites draw relative to their parent sprite.

so for your problem



Sprite
{
Sprite *parent;
Sprite childSprites[];

void Render();
};

void Render()
{
// if you have a parent
// Draw relative to the parent (parent pos + this pos)

// else draw like normal
}

On my engine, using XNA, I use an image of the entire square, with corners and walls.

I made a function to which I can just pass the size of the rectangle I want to draw to a function, and the function will draw the sections of the image, according to the rectangle size.
This is similar to the spritesheet technique or the tileset technique, in which a single image contains several textures, and you draw a section of the image at a time.

Don't know if youre using XNA, but I believe that in whatever you're using, you can select which section of the texture you're going to draw.

Some C#ish pseudocode


void DrawRectangle(Rectangle area, int borderSize)
{
// Rect constructor = Rectangle(x, y, width, height)
// borders
// top left

draw(
texture,
new Rectangle(area.X, area.Y, borderSize, borderSize, // where is this going to be drawn
new Rectangle(0, 0, borderSize, borderSize)); // section of the image to be drawn
// bottom left
batch.Draw(
texture,
new Rectangle(area.X, (area.Y + area.Height - borderSize), borderSize, borderSize),
new Rectangle(0, texture.Height - BorderPadding.Y), (int)BorderPadding.X, (int)BorderPadding.Y));
// top right
batch.Draw(
texture,
new Rectangle(area.X + area.Width - borderSize, area.Y, borderSize, borderSize),
new Rectangle((texture.Width - borderSize), 0, borderSize, borderSize));
// bottom right
batch.Draw(
texture,
new Rectangle(area.X + area.Width - borderSize, area.Y + area.Height - borderSize, borderSize, borderSize),
new Rectangle(texture.Width - borderSize, texture.Height - borderSize, borderSize, borderSize));
}

This topic is closed to new replies.

Advertisement