0

I want to make a paint game in andengine. There is my codes. How can i use them in andengine? Or is there anything like drawPath in andengine? I tried to add Rects or Lines for drawing but my FPS was 10-15.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SingleTouchEventView extends View {
  private Paint paint = new Paint();
  private Path path = new Path();

  public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);

    setBackgroundColor(Color.WHITE);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      path.moveTo(eventX, eventY);
      return true;
    case MotionEvent.ACTION_MOVE:
      path.lineTo(eventX, eventY);
      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }
A.O.92
  • 13
  • 1
  • 5

1 Answers1

0

There are several ways to accomplish what you want.

  1. create Rectangles (not lines since they differ from device to device) and add them to the scene to get a path. Use object pools to reuse your objects (Rectangles).

  2. if the first approach doesn't perform well then you could also draw directly on an empty texture using canvas.

  3. there is a class called RenderTexture on which you can draw entities. Use such a RenderTexture and draw your lines to it. create a sprite using this Texture and add it to the scene.

sjkm
  • 3,887
  • 2
  • 25
  • 43
  • I couldnt do it can you help me a little bit more :( @sjkm – A.O.92 Jan 01 '16 at 19:22
  • @AnılOrga what do you have so far? – sjkm Jan 01 '16 at 19:23
  • i tried to made rectangular every single pixel and tried load them before paint. And after that i would set their color. But i think i tried to make too much rect. My codes like: for(x=0;x<1920;x++){for(y=0;y<1080;y++){ Make rect and position of rect (x,y)}} @sjkm – A.O.92 Jan 01 '16 at 20:05
  • you have several points (input) that make up a path, right? then create a rectangle from each point to the next. if you want to increase performance then you can use a limit - a min distance from each point to the next to use as few rectangles as possible. If you want a smooth line (smooth curves etc) you can use a simple smooth algorithm. – sjkm Jan 01 '16 at 20:08