Grid rendering problem in windows forms
-
I have the following code to draw a grid based on the size, width and height of a map:
public void DrawCollisionMap(Map map) { \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize); \_panel.Paint -= \_panel\_Paint; \_panel.Paint += \_panel\_Paint; \_panel.Invalidate(true); } void \_panel\_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); for (int x = 0; x < \_map.Width; x++) { for (int y = 0; y < \_map.Height; y++) { e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height); } } }
The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:
-
I have the following code to draw a grid based on the size, width and height of a map:
public void DrawCollisionMap(Map map) { \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize); \_panel.Paint -= \_panel\_Paint; \_panel.Paint += \_panel\_Paint; \_panel.Invalidate(true); } void \_panel\_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); for (int x = 0; x < \_map.Width; x++) { for (int y = 0; y < \_map.Height; y++) { e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height); } } }
The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:
venomation wrote:
I do not understand :(
:laugh:Man, are you happy or sad now? :confused:
venomation wrote:
The problem is that when it is rendered the grid does not render correctly
What do you mean? what's your expected output?
thatraja
**My Tip/Tricks
My Dad had a Heart Attack on this day so don't...
** -
I have the following code to draw a grid based on the size, width and height of a map:
public void DrawCollisionMap(Map map) { \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize); \_panel.Paint -= \_panel\_Paint; \_panel.Paint += \_panel\_Paint; \_panel.Invalidate(true); } void \_panel\_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); for (int x = 0; x < \_map.Width; x++) { for (int y = 0; y < \_map.Height; y++) { e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height); } } }
The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:
venomation wrote:
I do not understand
I do. I read your code and saw the mistake right away. However I'm not going to tell you what it is, I just give you some advice: when something does not behave as expected, trim it down to a fraction of what you intend to have, and check that first. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
venomation wrote:
I do not understand
I do. I read your code and saw the mistake right away. However I'm not going to tell you what it is, I just give you some advice: when something does not behave as expected, trim it down to a fraction of what you intend to have, and check that first. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3Aha how silly of me ! Apart from being silly the other reason why I didn't notice the maps dimensions instead of the tile-size is that I got it working on DirectX so "thought" I knew what I was doing ^^. Thanks for making me see the light :laugh:
-
I have the following code to draw a grid based on the size, width and height of a map:
public void DrawCollisionMap(Map map) { \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize); \_panel.Paint -= \_panel\_Paint; \_panel.Paint += \_panel\_Paint; \_panel.Invalidate(true); } void \_panel\_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); for (int x = 0; x < \_map.Width; x++) { for (int y = 0; y < \_map.Height; y++) { e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height); } } }
The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:
We're not done yet. I have two comments on your code: 1. the remove-then-add-a-handler stuff looks unnecessary. Why are you doing this? 2. once you fixed the error, you are drawing a large number of tiny adjacent rectangles, which means a lot of computational effort, and also each tiny edge gets drawn twice. And all you really need for a grid of N by N cells, is N+1 horizontal lines and N+1 vertical lines. That does not take more code, it does not have nested for loops, and even may be less error prone! :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
We're not done yet. I have two comments on your code: 1. the remove-then-add-a-handler stuff looks unnecessary. Why are you doing this? 2. once you fixed the error, you are drawing a large number of tiny adjacent rectangles, which means a lot of computational effort, and also each tiny edge gets drawn twice. And all you really need for a grid of N by N cells, is N+1 horizontal lines and N+1 vertical lines. That does not take more code, it does not have nested for loops, and even may be less error prone! :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3The event handling is terrible I know, only there for a quick hack as I wanted to fix the old issue... The cells need to be "cell by cell" instead of lines as the user will change them (tile map style). I don't know if your suggestion would help for that, I made a slight improvement in the loop by calculating the difference before:
for (int x = dbPanel1.HorizontalScroll.Value%size; x < width\*size; x+= size) { for (int y = dbPanel1.VerticalScroll.Value % size; y < height \* size; y += size) { e.Graphics.DrawRectangle(\_p, x , y , size, size); } }
-
The event handling is terrible I know, only there for a quick hack as I wanted to fix the old issue... The cells need to be "cell by cell" instead of lines as the user will change them (tile map style). I don't know if your suggestion would help for that, I made a slight improvement in the loop by calculating the difference before:
for (int x = dbPanel1.HorizontalScroll.Value%size; x < width\*size; x+= size) { for (int y = dbPanel1.VerticalScroll.Value % size; y < height \* size; y += size) { e.Graphics.DrawRectangle(\_p, x , y , size, size); } }
Yes, the readability has increased a lot. :thumbsup:
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
I have the following code to draw a grid based on the size, width and height of a map:
public void DrawCollisionMap(Map map) { \_panel.AutoScrollMinSize = new Size(map.Width\*Map.TileSize, map.Height\*Map.TileSize); \_panel.Paint -= \_panel\_Paint; \_panel.Paint += \_panel\_Paint; \_panel.Invalidate(true); } void \_panel\_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); for (int x = 0; x < \_map.Width; x++) { for (int y = 0; y < \_map.Height; y++) { e.Graphics.DrawRectangle(\_pen, x \* Map.TileSize, y \* Map.TileSize, \_map.Width, \_map.Height); } } }
The problem is that when it is rendered the grid does not render correctly: http://imageshack.us/photo/my-images/864/gridr.jpg/[^] I do not understand :( :laugh:
Some more comments, out of the blue: 1. if you create instances of a class that has a Dispose() method(such as Graphics, Font, Pen, Brush), you should call it when done with the objects. 2. you should avoid creating objects inside an event handler that is going to be called often; say you need a few brushes, just create them once and keep them around. 3. There are a whole lot of stock objects available at no extra charge, see classes such as
Pens
andBrushes
. (and no, you don't dispose them, they aren't yours, you just borrow them). :)Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3