2

I'm making a 2d game with square tiles and it's time to make a good map editor.

Earlier I tried using DataGridView, but it turned out to be superslow.

I figured out there should be another way to draw the tile (the actual image or colored background) and its parameters (in text). PictureBox!

Is there an easy way to access the 2d array of pixels, insert tile images at desired coordinates, get the pointer coordinates and all that stuff, or should I try using a totally different approach?

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • There is also a helpful post about converting images to maps [link](http://stackoverflow.com/q/10127871/1306322) – user1306322 Apr 12 '12 at 19:00

1 Answers1

5

Using any grid of controls is going to be superslow. The 'correct' way to draw a grid of tiles is.. to draw a grid of tiles.

You can create your own control that takes an array of images or indices/keys into an image container and paints them.

Derive your custom control from Control, not UserControl (UserControl is a control container and you don't need that, or alternately from ScrollableControl if you want scroll bars). Override the OnPaint method to perform the drawing. Use the Graphics object passed to OnPaint, it has methods for drawing text, primitives, and bitmaps.

You can handle user input by overriding MouseDown/Up, or let the consumer handle it but provide a HitTest method so that a given X, Y can be translated to a grid index.

I've got some code here C# Drawing circles in a panel for drawing ellipses in response to user input. From that code you can see an example of painting some state on a control (the control in that case is the form, but you can extrapolate to your custom control). Notice the SetStyle call in the constructor for removing flicker. Also notice that when you alter state, you do not attempt to redraw, you simply Invalidate which tells the OS that you need to be redrawn.

Updated

Here's an extremely crude example to get you started: http://pastebin.com/DseuN56y

Community
  • 1
  • 1
Tergiver
  • 14,171
  • 3
  • 41
  • 68