image-manipulationwxwidgetsimage-editor

Drawing tools for a paint-like program in wxWidgets


I'm making a program in wxWidgets that has similar functionality to, say, MS Paint, but for more specific purposes.

I understand various ways to create the pen/pencil object which commits a single pixel as you drag the mouse around. One thing I'm having trouble visualizing are some of the other common tools: rectangle, oval, line, etc. For these, you press the left mouse button to set an origin point, and as you drag your mouse around it temporarily renders an overlay of the shape on the canvas from the origin to the current mouse position, finally committing the shape to the image when the mouse button is released.

What is the best way to create this process? The only solution I can think of is to store the origin point on mouse-down, and as the mouse is dragged to set a temporary variable to the current mouse position. Each time the mouse position changes, you would invalidate the rectangle from the origin point to the stored mouse position. Once the mouse button is released, the shape would be committed to the image model.

I haven't implemented this yet, as I would like feedback first. Is this the optimal solution, or is it prone to cause a visible flicker, with a much more preferable solution available?


Solution

  • Video display hardware is now so fast, you likely do not need to bother with any such cleverness.

    Here is a simpler approach

    - *Mouse moves with button down and tool X selected*
    - IF flagToolInUse THEN remove previous from image model
    - flagToolInUse = TRUE
    - Update image model with tool X
    - Draw image to invisible buffer
    - Copy buffer to display
    - Done
    
    - *Button released*
    - flagToolInUse = FALSE
    - Done
    

    The real advantage of this is you can write the code once, and use for every change to the image, no matter what tool is being used.

    If you need more details on implementing flicker-free double-buffered display with wxWidgets, here is an introduction ( windows mostly )