Jump to content
  • Advertisement
Sign in to follow this  
phil05

C# OnPaint() Question

This topic is 4753 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

As you can tell by this code, this is rediculous. I have to read each Array, paint it. Read next array, paint it. This occures about 50 times. Obviously, this is what methods are, to reduce the same repeated code. However, how can I pass OnPaint() to a method? Can I pass 'g' and it would work? How can I keep each array as just one so I don't have to keep re-drawing everything?
        // OnPaint() Override. Where map and PC/NPC/Enemies will be displayed.
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            switch (Loc)
            {
                case eLocation.HillcrestForest1:
                    {
                        // Draw Map
                        for (int y = 0; y < 10; y++)
                            for (int x = 0; x < 13; x++)
                                switch (rWorld.HillcrestWoods1[y, x])
                                {
                                    case 0: g.DrawImage(rWorld.imgGrass, x * SIZE, y * SIZE);
                                        break;
                                    case 1: g.DrawImage(rWorld.imgTree, x * SIZE, y * SIZE);
                                        break;
                                    case 2: g.DrawImage(rWorld.imgTrail_LR1, x * SIZE, y * SIZE);
                                        break;
                                    case 3: g.DrawImage(rWorld.imgTrail_LR2, x * SIZE, y * SIZE);
                                        break;
                                    case 4: g.DrawImage(rWorld.imgWater, x * SIZE, y * SIZE);
                                        break;
                                    case 5: g.DrawImage(rWorld.imgTreasure, x * SIZE, y * SIZE);
                                        break;
                                    case 6: g.DrawImage(rWorld.imgHouse1_00, x * SIZE, y * SIZE);
                                        break;
                                    case 7: g.DrawImage(rWorld.imgHouse1_01, x * SIZE, y * SIZE);
                                        break;
                                    case 8: g.DrawImage(rWorld.imgHouse1_10, x * SIZE, y * SIZE);
                                        break;
                                    case 9: g.DrawImage(rWorld.imgHouse1_11, x * SIZE, y * SIZE);
                                        break;
                                    case 10: g.DrawImage(rWorld.imgTrailCL, x * SIZE, y * SIZE);
                                        break;
                                    case 11: g.DrawImage(rWorld.imgTrail, x * SIZE, y * SIZE);
                                        break;
                                    case 12: g.DrawImage(rWorld.imgShore, x * SIZE, y * SIZE);
                                        break;
                                    case 13: g.DrawImage(rWorld.imgShoreTop, x * SIZE, y * SIZE);
                                        break;
                                    case 14: g.DrawImage(rWorld.imgTreeStump1, x * SIZE, y * SIZE);
                                        break;
                                    case 15: g.DrawImage(rWorld.imgFlowers1, x * SIZE, y * SIZE);
                                        break;
                                }
                    }
                    break;

                case eLocation.HillcrestForest2:
                    {
                        // Draw Map
                        for (int y = 0; y < 10; y++)
                            for (int x = 0; x < 13; x++)
                                switch (rWorld.HillcrestWoods2[y, x])
                                {
                                    case 0: g.DrawImage(rWorld.imgGrass, x * SIZE, y * SIZE);
                                        break;
                                    case 1: g.DrawImage(rWorld.imgTree, x * SIZE, y * SIZE);
                                        break;
                                    case 2: g.DrawImage(rWorld.imgTrail_LR1, x * SIZE, y * SIZE);
                                        break;
                                    case 3: g.DrawImage(rWorld.imgTrail_LR2, x * SIZE, y * SIZE);
                                        break;
                                    case 4: g.DrawImage(rWorld.imgWater, x * SIZE, y * SIZE);
                                        break;
                                    case 5: g.DrawImage(rWorld.imgTreasure, x * SIZE, y * SIZE);
                                        break;
                                    case 6: g.DrawImage(rWorld.imgHouse1_00, x * SIZE, y * SIZE);
                                        break;
                                    case 7: g.DrawImage(rWorld.imgHouse1_01, x * SIZE, y * SIZE);
                                        break;
                                    case 8: g.DrawImage(rWorld.imgHouse1_10, x * SIZE, y * SIZE);
                                        break;
                                    case 9: g.DrawImage(rWorld.imgHouse1_11, x * SIZE, y * SIZE);
                                        break;
                                    case 10: g.DrawImage(rWorld.imgTrailCL, x * SIZE, y * SIZE);
                                        break;
                                    case 11: g.DrawImage(rWorld.imgTrail, x * SIZE, y * SIZE);
                                        break;
                                    case 12: g.DrawImage(rWorld.imgShore, x * SIZE, y * SIZE);
                                        break;
                                    case 13: g.DrawImage(rWorld.imgShoreTop, x * SIZE, y * SIZE);
                                        break;
                                    case 14: g.DrawImage(rWorld.imgTreeStump1, x * SIZE, y * SIZE);
                                        break;
                                    case 15: g.DrawImage(rWorld.imgFlowers1, x * SIZE, y * SIZE);
                                        break;
                                }
                    }
                    break;

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure about passing around graphics context. But I do know you need to organize that code! That's horrible! You need to store all those images in some data structure. If C# has an equivalent of C++'s std::map, that seems near perfect. The key type would be the index of the image, the value type a pointer (reference in this case) to the image itself. With this method, you could get rid of that horrid switch statement and replace it with something like this:

g.DrawImage( rWorld.ImageResources[ rWorld.HillcrestWoods1[ y, x ] ], x * SIZE, y * SIZE );

Edit: I suggest employing a similar method to get rid of the switch for Loc. That code will just prove nearly impossible to maintain later on.

Edit2: For even more elegancy, perhaps store all the image indices in a text file? That would make editing easy as pie [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by phil05
As you can tell by this code, this is rediculous. I have to read each Array, paint it. Read next array, paint it. This occures about 50 times. Obviously, this is what methods are, to reduce the same repeated code. However, how can I pass OnPaint() to a method? Can I pass 'g' and it would work? How can I keep each array as just one so I don't have to keep re-drawing everything?


// OnPaint() Override. Where map and PC/NPC/Enemies will be displayed.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;

switch (Loc)
{
case eLocation.HillcrestForest1:
{
// Draw Map
for (int y = 0; y < 10; y++)
for (int x = 0; x < 13; x++)
switch (rWorld.HillcrestWoods1[y, x])
{
case 0: g.DrawImage(rWorld.imgGrass, x * SIZE, y * SIZE);
break;
case 1: g.DrawImage(rWorld.imgTree, x * SIZE, y * SIZE);
break;
case 2: g.DrawImage(rWorld.imgTrail_LR1, x * SIZE, y * SIZE);
break;
case 3: g.DrawImage(rWorld.imgTrail_LR2, x * SIZE, y * SIZE);
break;
case 4: g.DrawImage(rWorld.imgWater, x * SIZE, y * SIZE);
break;
case 5: g.DrawImage(rWorld.imgTreasure, x * SIZE, y * SIZE);
break;
case 6: g.DrawImage(rWorld.imgHouse1_00, x * SIZE, y * SIZE);
break;
case 7: g.DrawImage(rWorld.imgHouse1_01, x * SIZE, y * SIZE);
break;
case 8: g.DrawImage(rWorld.imgHouse1_10, x * SIZE, y * SIZE);
break;
case 9: g.DrawImage(rWorld.imgHouse1_11, x * SIZE, y * SIZE);
break;
case 10: g.DrawImage(rWorld.imgTrailCL, x * SIZE, y * SIZE);
break;
case 11: g.DrawImage(rWorld.imgTrail, x * SIZE, y * SIZE);
break;
case 12: g.DrawImage(rWorld.imgShore, x * SIZE, y * SIZE);
break;
case 13: g.DrawImage(rWorld.imgShoreTop, x * SIZE, y * SIZE);
break;
case 14: g.DrawImage(rWorld.imgTreeStump1, x * SIZE, y * SIZE);
break;
case 15: g.DrawImage(rWorld.imgFlowers1, x * SIZE, y * SIZE);
break;
}
}
break;

case eLocation.HillcrestForest2:
{
// Draw Map
for (int y = 0; y < 10; y++)
for (int x = 0; x < 13; x++)
switch (rWorld.HillcrestWoods2[y, x])
{
case 0: g.DrawImage(rWorld.imgGrass, x * SIZE, y * SIZE);
break;
case 1: g.DrawImage(rWorld.imgTree, x * SIZE, y * SIZE);
break;
case 2: g.DrawImage(rWorld.imgTrail_LR1, x * SIZE, y * SIZE);
break;
case 3: g.DrawImage(rWorld.imgTrail_LR2, x * SIZE, y * SIZE);
break;
case 4: g.DrawImage(rWorld.imgWater, x * SIZE, y * SIZE);
break;
case 5: g.DrawImage(rWorld.imgTreasure, x * SIZE, y * SIZE);
break;
case 6: g.DrawImage(rWorld.imgHouse1_00, x * SIZE, y * SIZE);
break;
case 7: g.DrawImage(rWorld.imgHouse1_01, x * SIZE, y * SIZE);
break;
case 8: g.DrawImage(rWorld.imgHouse1_10, x * SIZE, y * SIZE);
break;
case 9: g.DrawImage(rWorld.imgHouse1_11, x * SIZE, y * SIZE);
break;
case 10: g.DrawImage(rWorld.imgTrailCL, x * SIZE, y * SIZE);
break;
case 11: g.DrawImage(rWorld.imgTrail, x * SIZE, y * SIZE);
break;
case 12: g.DrawImage(rWorld.imgShore, x * SIZE, y * SIZE);
break;
case 13: g.DrawImage(rWorld.imgShoreTop, x * SIZE, y * SIZE);
break;
case 14: g.DrawImage(rWorld.imgTreeStump1, x * SIZE, y * SIZE);
break;
case 15: g.DrawImage(rWorld.imgFlowers1, x * SIZE, y * SIZE);
break;
}
}
break;


wah! wats this?

Share this post


Link to post
Share on other sites
Quote:
Original post by phil05
As you can tell by this code, this is rediculous. I have to read each Array, paint it. Read next array, paint it. This occures about 50 times. Obviously, this is what methods are, to reduce the same repeated code. However, how can I pass OnPaint() to a method? Can I pass 'g' and it would work? How can I keep each array as just one so I don't have to keep re-drawing everything?


// OnPaint() Override. Where map and PC/NPC/Enemies will be displayed.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;

switch (Loc)
{
case eLocation.HillcrestForest1:
{
// Draw Map
for (int y = 0; y < 10; y++)
for (int x = 0; x < 13; x++)
switch (rWorld.HillcrestWoods1[y, x])
{
case 0: g.DrawImage(rWorld.imgGrass, x * SIZE, y * SIZE);
break;
case 1: g.DrawImage(rWorld.imgTree, x * SIZE, y * SIZE);
break;
case 2: g.DrawImage(rWorld.imgTrail_LR1, x * SIZE, y * SIZE);
break;
case 3: g.DrawImage(rWorld.imgTrail_LR2, x * SIZE, y * SIZE);
break;
case 4: g.DrawImage(rWorld.imgWater, x * SIZE, y * SIZE);
break;
case 5: g.DrawImage(rWorld.imgTreasure, x * SIZE, y * SIZE);
break;
case 6: g.DrawImage(rWorld.imgHouse1_00, x * SIZE, y * SIZE);
break;
case 7: g.DrawImage(rWorld.imgHouse1_01, x * SIZE, y * SIZE);
break;
case 8: g.DrawImage(rWorld.imgHouse1_10, x * SIZE, y * SIZE);
break;
case 9: g.DrawImage(rWorld.imgHouse1_11, x * SIZE, y * SIZE);
break;
case 10: g.DrawImage(rWorld.imgTrailCL, x * SIZE, y * SIZE);
break;
case 11: g.DrawImage(rWorld.imgTrail, x * SIZE, y * SIZE);
break;
case 12: g.DrawImage(rWorld.imgShore, x * SIZE, y * SIZE);
break;
case 13: g.DrawImage(rWorld.imgShoreTop, x * SIZE, y * SIZE);
break;
case 14: g.DrawImage(rWorld.imgTreeStump1, x * SIZE, y * SIZE);
break;
case 15: g.DrawImage(rWorld.imgFlowers1, x * SIZE, y * SIZE);
break;
}
}
break;

case eLocation.HillcrestForest2:
{
// Draw Map
for (int y = 0; y < 10; y++)
for (int x = 0; x < 13; x++)
switch (rWorld.HillcrestWoods2[y, x])
{
case 0: g.DrawImage(rWorld.imgGrass, x * SIZE, y * SIZE);
break;
case 1: g.DrawImage(rWorld.imgTree, x * SIZE, y * SIZE);
break;
case 2: g.DrawImage(rWorld.imgTrail_LR1, x * SIZE, y * SIZE);
break;
case 3: g.DrawImage(rWorld.imgTrail_LR2, x * SIZE, y * SIZE);
break;
case 4: g.DrawImage(rWorld.imgWater, x * SIZE, y * SIZE);
break;
case 5: g.DrawImage(rWorld.imgTreasure, x * SIZE, y * SIZE);
break;
case 6: g.DrawImage(rWorld.imgHouse1_00, x * SIZE, y * SIZE);
break;
case 7: g.DrawImage(rWorld.imgHouse1_01, x * SIZE, y * SIZE);
break;
case 8: g.DrawImage(rWorld.imgHouse1_10, x * SIZE, y * SIZE);
break;
case 9: g.DrawImage(rWorld.imgHouse1_11, x * SIZE, y * SIZE);
break;
case 10: g.DrawImage(rWorld.imgTrailCL, x * SIZE, y * SIZE);
break;
case 11: g.DrawImage(rWorld.imgTrail, x * SIZE, y * SIZE);
break;
case 12: g.DrawImage(rWorld.imgShore, x * SIZE, y * SIZE);
break;
case 13: g.DrawImage(rWorld.imgShoreTop, x * SIZE, y * SIZE);
break;
case 14: g.DrawImage(rWorld.imgTreeStump1, x * SIZE, y * SIZE);
break;
case 15: g.DrawImage(rWorld.imgFlowers1, x * SIZE, y * SIZE);
break;
}
}
break;


Share this post


Link to post
Share on other sites
Quote:
Original post by nilkn
I'm not sure about passing around graphics context. But I do know you need to organize that code! That's horrible! You need to store all those images in some data structure. If C# has an equivalent of C++'s std::map, that seems near perfect. The key type would be the index of the image, the value type a pointer (reference in this case) to the image itself. With this method, you could get rid of that horrid switch statement and replace it with something like this:

g.DrawImage( rWorld.ImageResources[ rWorld.HillcrestWoods1[ y, x ] ], x * SIZE, y * SIZE );

Edit: I suggest employing a similar method to get rid of the switch for Loc. That code will just prove nearly impossible to maintain later on.

Edit2: For even more elegancy, perhaps store all the image indices in a text file? That would make editing easy as pie [smile]


exactly, don't use ArrayList tough, because it's slow to convert a class back and to object (especially converting from an object is slow apparently)
so use Image[] or Image[][] if you want a 2d array, don't use Image[,] because it's slower

as for the text file suggestion, better use xml, c# has excellent xml support..

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!