2023-03-16

Android - How can I draw frames pixel by pixel (for NES emulation) in android studio faster?

This is a NES emulator project I am working on. Every second I need to draw the screen around 60 times to get accurate emulation, which means, every second I have to draw at least 256 * 240 * 60 pixels. For drawing, I am using canvas. On which every frame, I am going through the data of 256 * 240 pixels and drawing them on screen. Unsurprisingly, it was terrible. My emulator draws less than a frame per second

public class GameCanvas extends View{
public void drawSprite(int x, int y, Olc2C02A.Sprite sprite, Canvas canvas){
        for (int ir = 0; ir < sprite.height; ir++){
            for (int ic = 0; ic < sprite.width; ic++){
                Olc2C02A.Pixel p = sprite.getPixel(ic, ir);
                if (p.a != 0){
                    int pr = p.r, pg = p.g, pb = p.b, pa = p.a;
                    painter.setColor(Color.argb(pa, pr, pg, pb));
                    canvas.drawPoint((x + ic), (y + ir), painter);
                }
            }
        }
    }

@Override
protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#000000"));
        drawSprite(0, 0, this.gameScreen, canvas);
    }

}

The above code is a slightly simplified version of my code. In this code I am only drawing pixel by pixel disregarding the actual size of each pixels that would be needed if I considered resizing the frame according to the size of the screen I am running this app on. The sprite is a 256x240 sized array of Pixel objects. Which are custom classes I made to store the color value of each pixel. Therefore, unfortunately, just slight performance boost isn't enough for the emulator. I need to draw at least 30 frames per second if I want my emulator to be functional. What other alternatives can I use for this task that can fit in my android studio project.



No comments:

Post a Comment