在Paint应用程序中撤消绘图 [英] Undo drawing in Paint Application

查看:61
本文介绍了在Paint应用程序中撤消绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGl框架制作绘画应用程序,但我只能使用UNDO/REDO选项.我实现的代码是这样的:

I am making a paint application using OpenGl framework and I'm stuck at UNDO/REDO option.. The code I implemented is like this:

  -(void)undo_called
{

artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate];
mbrushscale=app.brushscale;
brushimage=app.brush_image;
Erase=YES;
[self playRayundo];
 }


-(void)playRayundo
{
artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate];
glColor4f(app.r1g,
          app.b1g,
          app.g1g,
          0);
NSLog(@"%f",app.r1g);


if(undo != NULL)
{
    for(int l = 0; l < [undo count]; l++)
    {
        //replays my writRay -1 because of location point
        for(int p = 0; p < [[undo objectAtIndex:l]count]-1; p ++)
        {
            [self drawErase:[[[undo objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[undo objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]];
        }
    }
}

Erase=NO;
glColor4f(app.rg,
          app.bg,
          app.gg,
          kBrushOpacity);

  }



 -(void) drawErase:(CGPoint)start toPoint:(CGPoint)end

 {
    static GLfloat*     eraseBuffer = NULL;
    static NSUInteger   eraseMax = 64;

NSUInteger          vertexCount = 0,
count,
i;

[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

// Convert locations from Points to Pixels
CGFloat scale = 1.0;//self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;

// Allocate vertex array buffer
if(eraseBuffer == NULL)
    eraseBuffer = malloc(eraseMax * 2 * sizeof(GLfloat));

// Add points to the buffer so there are drawing points every X pixels      
count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);

for(i = 0; i < count; ++i) 
{
    if(vertexCount == eraseMax) 
    {
        eraseMax = 2 * eraseMax;
        eraseBuffer = realloc(eraseBuffer, eraseMax * 2 * sizeof(GLfloat));
    }

    eraseBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
    eraseBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
    vertexCount += 1;
  }

  [self ChangebrushPic:brushimage];

 //the erase brush color  is transparent.

glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(64/mbrushscale);

// Render the vertex array

glVertexPointer(2, GL_FLOAT, 0, eraseBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);


// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

// at last restore the  mixed-mode
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

 }

此代码效率不高,并且有很多错误.请查看撤消前后的图像.

This code is not efficient and it has so many faults..Have a look at images before and After Undo.

之前: http://imageshack.us/photo/my-images/577/screenshot20110714at121.png/

之后: http://imageshack.us/photo/my-images/200/screenshot20110714at121.png/

因此,我想每次用户的Touch结束时将图像保存在缓冲区中并在UNDO上调用上一张图像.有人能帮助我如何在缓冲区中存储图像并在UNDO上找回吗?我试图找到其示例代码,但找不到..

So i want to Save image in buffer every time user's Touch ends and call previous image on UNDO..Can anyone help me how to Store image in buffer and retrieve back on UNDO?? I tried to find its sample code but could not find..

谢谢..

推荐答案

好.我正在发布我自己的问题的答案,因为很多人似乎都在提到这个问题.

Ok. I am posting answer to my own Question as so many people seem to refer this question.

有一个名为 TouchPainter 的应用程序,其源代码可用.

There is an application named TouchPainter and its source code is available. It contains drawing, color blending, Undo/Redo (Amazing..!!), Save/Open drawings..

注意::这可能需要对目标C 设计模式的深入了解(我仍然不知道什么是设计模式.但我找到了,所以我要分享..).本书适用于iOS的Apress.Pro Objective-C设计模式" 中说明了整个应用程序的源代码.

Note : It may require very deep level knowledge of Objective C and design pattern (I still do not know what design pattern is. But I found it so I am sharing..). The whole application's source code is explained in this book "Apress.Pro Objective-C Design Patterns for iOS".

希望对您有帮助.:)祝您好运.

I hope it will be helpful to you.. :) Good luck..

这篇关于在Paint应用程序中撤消绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆