FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DFL DoubleBuffering

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
zaczap



Joined: 22 Jun 2006
Posts: 4

PostPosted: Thu Jun 22, 2006 2:48 pm    Post subject: DFL DoubleBuffering Reply with quote

This probably counts as being a complete beginner question, but I need to know how to use double buffering on a DFL form.

From reading around, I thought that doing this would work (in my constructor):

Code:
setStyle(ControlStyles.USER_PAINT | ControlStyles.ALL_PAINTING_IN_WM_PAINT | ControlStyles.DOUBLE_BUFFER, false);


and then later:

Code:
protected override void onMouseMove(MouseEventArgs e) {
      invalidate();
}


to force an repaint call.

i tried doing this...and the flickering doesn't go away.

any ideas or suggestions?

full source (As of now):

Code:

import dfl.all;
import std.stdio;
import std.gc;

void main()
{
   ScreenManager sm;
   sm = new ScreenManager("window!",500,500);
   Application.run(sm);
}

class ScreenManager : Form {
   Point mouseLocation;
   private Bitmap _backBuffer;
   this(char[] title, int w, int h) {
      text = title;
      width = w;
      height = h;
      charm = new IMG("images/pic.gif",20,20);
      setStyle(ControlStyles.USER_PAINT | ControlStyles.ALL_PAINTING_IN_WM_PAINT | ControlStyles.DOUBLE_BUFFER, false);
   
   }
   

   protected override void onLoad(EventArgs ea) {
      
      
   }

   IMG charm;
   protected override void onPaint(PaintEventArgs ea)
   {   
      


      Graphics g = ea.graphics;
      
      Picture p = Picture.fromFile("images\1.gif");
      Point z;
      int b = 100;
      z.y = b;
      z.x = 50;

      
      charm.draw(g);
      p.draw(g, z);
   }

   protected override void onMouseMove(MouseEventArgs e) {
      mouseLocation.x = e.x;
      mouseLocation.y = e.y;
      charm.loc.x++;
      charm.loc.y++;
      invalidate();
   }
}

class IMG {
   public Picture front;
   public Point loc;
   
   this(char[] image, int x, int y) {
      loc.x = x;
      loc.y = y;
      front = Picture.fromFile(image);
   }
   
   public void draw(Graphics g) {
      front.draw(g,loc);
   }
}


* this code is pretty much experimental right now, as im moving my projects from java to D[/code]
Back to top
View user's profile Send private message AIM Address
zaczap



Joined: 22 Jun 2006
Posts: 4

PostPosted: Thu Jun 22, 2006 7:22 pm    Post subject: Reply with quote

note that above the first code sample should be:

Code:
setStyle(ControlStyles.USER_PAINT | ControlStyles.ALL_PAINTING_IN_WM_PAINT | ControlStyles.DOUBLE_BUFFER, [b]true[/b]);


i had just copied experimental code to see if falsifying it would for some reason make it work (obviously, it didnt)[/quote]
Back to top
View user's profile Send private message AIM Address
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Fri Jun 23, 2006 10:49 am    Post subject: Re: DFL DoubleBuffering Reply with quote

Sorry, ControlStyles.DOUBLE_BUFFER doesn't do anything yet; I wasn't sure how to do it optimally. I'll look back into it soon.

Note that "images\1.gif" contains ASCII code 1 and probably won't load your file. Also, you probably shouldn't load a picture every time the paint event fires. Ignore this if it's just due to test code.

If you need double buffering now, you could use the Windows API. I'm sure there are several tutorials on the net using CreateCompatibleBitmap, CreateCompatibleDC, SelectObject and BitBlt; be sure to clean up these resources!
Back to top
View user's profile Send private message
zaczap



Joined: 22 Jun 2006
Posts: 4

PostPosted: Fri Jun 23, 2006 11:39 am    Post subject: Reply with quote

AH. Thank you. That's what I figured was going on, and thanks in advance for checking it out.

Yeah, the images were just for the test code, and I'll look online. And the "images\1.gif" loads the images, they just flicker, but ill fix that too

Thanks!
Back to top
View user's profile Send private message AIM Address
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Sat Jun 24, 2006 9:06 pm    Post subject: Re: DFL DoubleBuffering Reply with quote

I added MemoryGraphics to the latest snapshot, so you don't need to mess with the functions I mentioned previously.

Also, I just wrote and posted a mostly untested addon called GraphicsBuffer, which you are welcome to use or just learn from.

- Chris
Back to top
View user's profile Send private message
zaczap



Joined: 22 Jun 2006
Posts: 4

PostPosted: Sun Jul 02, 2006 10:14 am    Post subject: DFL DoubleBuffering Reply with quote

using the new MemoryGraphics class, I cobbled together this based off of your code and it eliminates flicker perfectly (resizing is currently an issue, but i think it's an easy solution and just haven't looked at it yet):

Code:


private import dfl.all;

class XDblBfrFrm:Form {
   MemoryGraphics _backBuffer;
   Point mouseLocation;
   
   this() {
      setStyle(ControlStyles.USER_PAINT | ControlStyles.ALL_PAINTING_IN_WM_PAINT, true);   
   }
   
   protected override void onPaint(PaintEventArgs ea) {
      if(!_backBuffer) {
         _backBuffer = new MemoryGraphics(clientSize.width, clientSize.height);   
      }   
      
      Graphics g = _backBuffer;
      
      
      g.fillRectangle(Color(0,0,0), Rect(0,0,300,300));
      
      paint(g);
      
      _backBuffer.copyTo(ea.graphics, 0,0,clientSize.width, clientSize.height);
   }
   protected final override void onPaintBackground(PaintEventArgs pevent) {
   
   }
   protected override void onLoad(EventArgs e) {
   
   }
   protected override  void onResize(EventArgs e) {
        if(_backBuffer) {
           _backBuffer.dispose();
           _backBuffer=null;
      }
      invalidate();
    }
   
    protected override void onMouseMove(MouseEventArgs e) {
      mouseLocation.x = e.x;
      mouseLocation.y = e.y;
      invalidate();
   }
   
   abstract void paint(Graphics g);
}

interface XListener {
   void loop();   
}

class XTimer:Timer {
   XListener target;
   void addListener(XListener x) {
      target = x;
   }
   protected override void onTick(EventArgs ea) {
      target.loop();
   }
}

class ScreenManager:XDblBfrFrm, XListener{
   XTimer t;
   
   this() {
      t = new XTimer();
      t.addListener(this);
      t.interval(30);
      t.start();
   }
   
   void loop() {
      gameLoop();
      
      invalidate();
   }
   abstract void gameLoop();
   abstract void paint(Graphics g);
}


to implement it and play with a floating box, just do this:

Code:

class BoxTest:ScreenManager {
   enum dir {north, south, east, west}
   dir XM, YM;
   int x = 10, y = 10;
   this() {
      XM = dir.east;
      YM = dir.south;
   }
   
   void gameLoop() {
      if(XM == dir.east) {
         if(x + 10 < clientSize.width)
            x+=2;
         else
            XM = dir.west;
      } else {
         if(x > 0)
            x-=2;
         else
            XM = dir.east;
      }
   }   
   
   void paint(Graphics g) {
      g.fillRectangle(Color(0,255,0), Rect(x,y,10,10));
   }
}


void main() {
   Application.run(new BoxTest());
}



note that the main point of my working this out was to draw images and work out an animation basis for this. (i'm trying to adapt david brackeen's "Developing Games in Java" to suit my needs in D, since it's heavenly faster)
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group