Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Map editor: Code design for brushes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Juliean   GDNet+   -  Reputation: 795

Like
0Likes
Like

Posted 17 August 2012 - 07:55 AM

Hello,

so I've written the basics of my map editor, got three different kinds of brushes (pencil, square and flood fill) all working great. However I'd like to know how I should handle the actions of those brushes.

My basic question is: Should the brush itself implement its action (draw single tile, draw square, flood fill) or should the using class do this? For now I've got a switch-statement in the update()-method of the tilemap-window, where the actions are implented. The brush class itself just holds certain parameters like starting point, selected tiles, etc.. . This is how, eg the pencil implentation looks like, in the Window_Tilemap->Update()-method:

switch((*m_ppEditorBrush)->GetType())
{
case 0:
  if(Input::MouseClicked(0) || Input::MouseHold(0))
  {
   vector<int>* pSelected = (*m_ppEditorBrush)->GetSelected();
   int i = 0;
   Vector2 vStartPoint = ProjectPosition(this,0,Input::GetMousePos());
   vStartPoint /= 32;
   for(vector<int>::iterator ii = (*pSelected).begin();ii!=(*pSelected).end();++ii,i++)
   {
    m_pGameMap->ChangeTile(vStartPoint.x+i%(*m_ppEditorBrush)->GetWidth(),vStartPoint.y+i/(*m_ppEditorBrush)->GetWidth(),*ii);
   }
   m_bActive = true;
   return true;
  }
  m_bActive = false;
  return false;
}

Not talking about how to make this code more readable, but is this basically the right way, or should I stuff that into the brush-class? Obviously my Window_Tilemap-code would become and easier to extend, but it would also mean my brush-class would need to know about the Game_Map-class, holding all the tiles, and isn't it better the less my classes know about other classes? Whats the right thing to do here?

Sponsor:

#2 DekuTree64   Members   -  Reputation: 482

Like
1Likes
Like

Posted 17 August 2012 - 06:10 PM

Hey, if it works, it works.

In my editor, I only have one type of brush, which is a rectangle, with separate functions for plotting to the map and flood filling to the map (which repeats the rectangular brush pattern throughout the filled area). You can right click on the map and drag to select a variable size rectangle of tiles, and then click around to plot copies of it on the map. You can also click and drag within the tileset window to select multi-tile objects right from the tileset. No need to have separate code for single-tile drawing, since the variable size rectangle can be 1x1 and work just the same Posted Image

As for who implements the drawing... in my case, the brush class plotToMap and floodFillToMap functions take a MapLayer pointer as an argument, and call layer->setTile which finally does the actual editing of map data.

#3 Juliean   GDNet+   -  Reputation: 795

Like
0Likes
Like

Posted 18 August 2012 - 04:58 PM

Thanks for the reply,

just getting it to work isn't what I aim for. My first map editor worked pretty well, but the code was so messy, that I couldn't work with it after a certain extent, so I didn't get it anywhere near the finish. But ok, so if there isn't anything against it, I think I'll go with the implementation in the brush class itself. I just rewrote it, and it works like yours.

Just one more thing about the features: I do have all the same features, selecting from the tileset as well as the map, but instead of just plotting to the map and flood filling, I also got the rectangular class, which is very useful: If you click on a spot with that brush selected, hold the mouse and drag it, after releasing the rectangular between those two points is filled with the selected tiles. It gets useful in situations where you can't use floodfill but have to cover large areas with all the same tiles. I don't have seperate code for drawing single tiles, this is what I meant. ;)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS