Deterministic bool array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #16
    No problem, good luck with everything else.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #17
      To minimize garbage collection, don't create/destroy anything if you can help it. I can't see your code, but the biggest thing that helps, I find, is not creating new memory wherever possible. For example, lets say your byte array. Don't create/destroy it every cycle, just modify the values in it. Maybe you've got a couple of vectors in use for determining where things draw... don't create new ones every cycle, just modify the coordinate values of them instead.

      Things of that nature.

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #18
        Well unfortunately I have to create new items, at least the way it's written right now. It takes two bitmaps, one for "on" and one for "off" and flips and rotates it, then draws it to the proper portion of a larger bitmap, then puts the large one in a picture box. I know the logical way to do it would be to draw onto the graphics object of the paint event, and I'm working on that as we speak. You wouldn't believe how difficult it is to draw that stupid little shape.

        Let me ask you something, if I write a method and call it in the onpaint event of my drawing area, and pass the PaintEventArgs to it, will that create a new instance of the PaintEventArgs? What if I made it reference the args using the ref tag in the method?

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #19
          No everything in C# (except the primitive types) is passed by reference. Putting ref in front makes the reference itself passed by reference, which would let you modify the pointer itself. It's not creating a new graphics object when you do that, so you're fine.

          As for the bitmaps, you still don't have to create two bitmaps. For one, a bitmap object does have a getpixel and setpixel method so you can always directly modify the data and, for another, you can create a graphics object on a bitmap object and draw directly to it.

          You shouldn't have to create new bitmaps over and over again.

          It sounds like you're onto something but for what it's worth, here would be the general approach I'd take...

          Code:
          private Bitmap m_result;
          private Graphics m_resultTarget;
          
          // constructor
          {
            m_result = new Bitmap(desired_width, desired_height);
            m_resultTarget = Graphics.FromImage(m_result);
          }
          
          // paint method (Graphics g)
          {
            // Clear the buffer
            m_resultTarget.FilledRectangle(..., /* background color, */ ...);
          
            // Draw everything using m_resultTarget to draw to the bitmap
          
            // Draw the bitmap to the form as appropriate
            g.DrawImage(m_result, ...);
          }

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #20
            I would have a grid of bitmaps and 4 pictures for each of the possible states. And then change the source of the picture accordingly rather than redrawing everything.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #21
              If there are only four possible resulting images, that's actually probably the best way to go.

              I didn't read too much into this as Rabbit already did the bulk of the work. I just tried to swoop in and snatch up all the glory, muahahaha! (:P)

              Back on topic, even if you had more states you could do similar to make your drawing easier. It looks like you can slice up your result image into tiles and then just paint the appropriate tile as either on or off. It'd be a bit easier than drawing all the shapes yourself.

              I'd still recommend following the flow I laid out though... do all your drawing on another source, then draw that to the screen. That way you don't get to watch your screen draw ;)

              Comment

              • HaLo2FrEeEk
                Contributor
                • Feb 2007
                • 404

                #22
                That's what I do, I have a loop that goes through the array and paints the correct image in the correct location. There are 8 possible states, 4 for both on and off. I paint each image in the top-left of the larger bitmap, then mirror it to the right and bottom using Bitmap's rotate method. Once the loop is finished it sets the picture box's image to the large bitmap. I think it'd be easier to simply draw the shape to the graphics object in the onpaint event, eliminating the bitmaps altogether. The way I've got it now there's no flicker or anything since the entire image is drawn, then set as the picture box's image.

                Comment

                Working...