Jump to content
  • Advertisement
Sign in to follow this  
namingway

selecting a portion of an image help

This topic is 3847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a map editor I'm writing and its coming along well, but I'm not sure how to select certain portions of the loaded tileset. The tile sizes are 32x32 and the tileset is loaded into a picture box, do I need a different component to be able to do this? Tilesets are a fix size of 8 across and 20 down or 256x640.

Share this post


Link to post
Share on other sites
Advertisement
You could probably split the loaded image into multiple images, one for each tile, and display each with a picture box of it's own, or you could write some custom selection logic for the single picture box approach. What language and tools are you using to create this editor with?

Share this post


Link to post
Share on other sites
This isn't something a API does for you, this is something you'll need to implement yourself in your map editor.

Here's how it might be done: First, store a bool somewhere, that's true when the user is selecting multiple tiles, and false when he's only grabbing one at a time. Next, have a button the user can turn on and off that controls that bool.
Then, when selecting tiles from the tileset, if the bool is false, get the tile as normal, but if the bool is true store wherever the player clicked, in terms of tiles. (Example: the player clicked on tilesheet X4 Y8) Don't select the tile yet, but wait for the player to click a second time, and store the second tile selected also. (Example: tilesheet X4 Y6)

If the second tile is less than the first tile, swap them, so the lowest numbered tile is first.(The tile higher up on the tilesheet)

Then, when the player clicks on the map to place a tile, check the bool. If the bool is false, place the single tile like normal, but if the bool is true, starting where the player clicked, cycle through the tiles from the tilesheet starting at the first place(in the tilesheet) the player clicked, and place them in the map until the second place(in the tilesheet) the player clicked is reached.

Share this post


Link to post
Share on other sites
Cant beleive I forgot to say the language... It's C#, Splitting different tiles into separat pictureboxs would be a nice approach but I still get the probelm of cutting out the individual tiles to do so. My initial Idea was to check a click in the picture box and then detect the coordinates to see which tile was clicked but that will use alot of if elses...
Also an interesting idea Servant, I'll look into that as well.

Share this post


Link to post
Share on other sites
It shouldn't use a lot of if-elses since your tiles are of a consistent size.

TileX = MouseX \ TileWidth
TileY = MouseY \ TileHeight

This will give you a 0-based X & Y indicator for which tile was clicked-on.

-Matt

Share this post


Link to post
Share on other sites
I'm trying your way but how do I get the mouse position from within the picturebox itself, my current attempt gets the global mouse position.

private void pictureBox1_Click(object sender, EventArgs e)
{
_tileX = MousePosition.X / 32;
_tileY = MousePosition.Y / 32;
if (_tileX == 1 && _tileY == 1)
{
_tileID = 1;
this.Text = "Mappy " + _tileID;
}
}

Share this post


Link to post
Share on other sites
I'm not familiar with how C# uses the normal Windows Form stuff, like cursor position per control, etc. Perhaps someone else can offer help in that area.

However, if MousePosition.X and .Y are the global positions ... can you subtract your Picturebox's top and left from those values to get the in-PictureBox value?

Once you have that, then instead of an If/Then or Switch to determine the Tile ID, you could do something like this ...

TileID = TileY * NumberOfTilesPerRow + TileX

I hope this is helpful.

-Matt

Share this post


Link to post
Share on other sites
I needed to use a mouse_click event then use the mouse args to track the position. Then by using code similiar to your suggestion it turned out perfectly. The problem I have now though is making something to put the tiles onto for a map. The code for its ready I just need to find out a way of making an array of picture boxs or something.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!