1

OpenGlフレームワークを使用してペイントアプリケーションを作成していますが、UNDO /REDOオプションでスタックしています。実装したコードは次のとおりです。

  -(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);

 }

このコードは効率的ではなく、非常に多くの欠点があります。元に戻す前と元にした後の画像を見てください。

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

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

だから私はユーザーのタッチが終了するたびに画像をバッファに保存し、UNDOで前の画像を呼び出したいです..誰かが画像をバッファに保存してUNDOに戻す方法を教えてもらえますか?サンプルコードを見つけようとしましたが、見つかりませんでした。

ありがとう..

4

3 に答える 3

1

Ok。非常に多くの人がこの質問を参照しているように見えるので、私は自分の質問への回答を投稿しています。

TouchPainterというアプリケーションがあり、そのソース コードが公開されています。描画、カラー ブレンディング、元に戻す/やり直し (素晴らしい..!!)、描画の保存/開くなどの機能が含まれています。

注 : Objective C設計パターンの非常に深いレベルの知識が必要になる場合があります(設計パターンが何であるかはまだわかりません。しかし、見つけたので共有します..)。アプリ全体のソースコードは本書「Apress.Pro Objective-C Design Patterns for iOS」で解説しています。

お役に立てば幸いです.. :) 頑張ってください..

于 2012-02-23T10:53:49.047 に答える
0

nsundomanagerを使用します。これは、元に戻すための最良の方法です。

于 2011-07-14T07:17:54.540 に答える