How would I implement undo in an OpenGL ES painting application on the iPhone?

后端 未结 4 1673
长情又很酷
长情又很酷 2020-12-15 11:37

I\'m using Apple\'s sample application GLPaint as a basis for an OpenGL ES painting application, but I can\'t figure out how to implement undo functionality within it.

相关标签:
4条回答
  • 2020-12-15 11:43

    I would recommend using NSUndoManager to store a list of the actual drawing actions undertaken by the user (draw line from here to here using this paintbrush, etc.). If stored as a list of x, y coordinates for vector drawing, along with all other metadata required to recreate that part of the drawing, you won't be using anywhere near as much memory as storing images, vertex buffer objects, or framebuffer objects.

    In fact, if you store these drawing steps in a Core Data database, you can almost get undo / redo for free. See my answer here for more.

    0 讨论(0)
  • 2020-12-15 11:48

    Use vertex buffer objects (VBO) to render your content. On every new stroke copy the last VBO to some least recently used (LRU) list. If your LRU is full, delete the least recently used VBO. To restore (undo) the last stroke just use the most recently used VBO of the LRU and render it.

    VBO: http://developer.apple.com/iphone/library/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html

    LRU: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

    0 讨论(0)
  • 2020-12-15 11:50

    To undo in a graphical application, you can use coreData.

    here is a detailed blogpost and read this one as well.

    0 讨论(0)
  • 2020-12-15 11:59

    Either you can use NSUndoManager, class provided by iOS

    Or you can save current state of screen area by:

    CGContextRef current = UIGraphicsGetCurrentContext();
    

    You can have one array as stack with screen image objects and on undo action you can pop value from stack and on each change push value into the stack.

    0 讨论(0)
提交回复
热议问题