undo, redo

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

undo, redo

Post by Virion »

how undo and redo is achieved? i have zero idea how they did that. any tips?
CuteAlien
Admin
Posts: 9644
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Basically you have 2 ways. You either save complete states or you save actions. The former is easier the latter usually more effective.
Using an image-manipulation program as example, saving the complete state would mean saving the whole image regularly and push it into an array. Then on undo you can get an older image out of the array and on redo you can get back to a newer one. Also think about what to do when you start modifying again after an undo/redo ... which images would you remove from the array and where would you insert the next ones. Just compare with how other applications handle those cases.

But saving states is often (especially when working with images) very memory consuming, so you can't do that always. So you save state-differences. Each action knows which parts need to be saves when they are executed. And each action also knows how to undo itself with the saved information. So for example a line-drawing action would save just all the affected pixels between start and endpoints for the undo. And it would save the line-drawing parameters for the redo (color, start-point, end-point, line-width).

Undo-redo is unfortunately often not trivial...
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

I agree almost entirely with CuteAlien. However, I would just like to add that instead of using an array, it would be a lot easier to use a stack.

This way, whenever a change is made, you put the difference into the stack. When you want to undo, you just pop the item off of the stack. To enable redo, you could then take the difference you took of the stack and put it into another stack.

I think it's just an easier way of keeping track of it.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I'd be willing to bet that CuteAlien recommended to use an array because Irrlicht doesn't have a stack implementation, and an array is the closest thing that it does offer. I'm sure we can all agree that a stack is really just an array that you access from one end, right?
CuteAlien
Admin
Posts: 9644
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Don't bet against vitek, he's got scary mind-reading skills!
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

Hmmm, stacks are dynamic are they not? Meaning you don't have to worry about expanding your array. Of course, I suppose if you want to limit the number of undos available, an array probably would be better.

Also, I have no desire to bet against any of you, my c++ knowledge as not as great as I'd like it to be. Two years in university and we're still just using Java.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

DarkDepths wrote:Hmmm, stacks are dynamic are they not?
Yes, they are, and yes you are correct about arrays not being dynamic. This is just an issue of context. In the C/C++ language, the term array usually refers to a fixed size collection of elements, like int x[10]. In Irrlicht, the term array usually means irr::core::array<> which is essentially the same as the C++ Standard Library std::vector<> container. It is dynamic.

Travis
DarkDepths
Posts: 126
Joined: Sun Apr 02, 2006 1:21 am
Location: Canada

Post by DarkDepths »

Oh, thanks for clearing that up, I never knew that! (Goes to show how much I've actually done with Irrlicht over the years).
Post Reply