2

I am using the framework that is described in the (great) book Beginning Android Games by Mario Zechner and Robert Green. So far all has been going great, where I can utilize the framework to draw my bitmaps for the main menu. With the framework, I am also able to clear the background, which also works great. However, when it comes to drawing pixels, lines, or shapes in general, I am completely stuck as they are not drawn. The methods execute in a similar fashion to that of the two working methods and my debugging with logging has told me that they do execute, however nothing new appears on the screen.

One source( canvas.drawLine() not appearing on bitmap ) said to draw onto a bitmap and then draw the bitmap onto the canvas, but I already made sure to do this. My paint is not null either. When drawing a rectangle, I made sure that its height and width were greater than 0. When drawing a line, I tried setting a width to 10 and the fill the STROKE. However, all of this was in vain.

Here is the code for the graphics class

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;

import com.learnplex.framework.Graphics;
import com.learnplex.framework.Pixmap;

import java.io.IOException;
import java.io.InputStream;

public class AndroidGraphics implements Graphics {
    AssetManager assets;
    Bitmap frameBuffer;
    Canvas canvas;
    Paint paint;
    Rect srcRect = new Rect();
    Rect dstRect = new Rect();

    public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
        this.assets = assets;
        this.frameBuffer = frameBuffer;
        this.canvas = new Canvas(frameBuffer);
        this.paint = new Paint();
    }

    public Pixmap newPixmap(String fileName, PixmapFormat format) {
        Config config = null;
        if (format == PixmapFormat.RGB565)
            config = Config.RGB_565;
        else if (format == PixmapFormat.ARGB4444)
            config = Config.ARGB_4444;
        else
            config = Config.ARGB_8888;

        Options options = new Options();
        options.inPreferredConfig = config;

        InputStream in = null;
        Bitmap bitmap = null;
        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in);
            if (bitmap == null)
                throw new RuntimeException("Couldn't load bitmap from asset '"
                        + fileName + "'");
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load bitmap from asset '"
                    + fileName + "'");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }

        if (bitmap.getConfig() == Config.RGB_565)
            format = PixmapFormat.RGB565;
        else if (bitmap.getConfig() == Config.ARGB_4444)
            format = PixmapFormat.ARGB4444;
        else
            format = PixmapFormat.ARGB8888;

        return new AndroidPixmap(bitmap, format);
    }

    public void clear(int color) {
        canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8,
                (color & 0xff));
    }

    public void drawPixel(int x, int y, int color) {
        paint.setColor(color);
        canvas.drawPoint(x, y, paint);
    }

    public void drawLine(int x, int y, int x2, int y2, int color) {
        paint.setColor(color);
        canvas.drawLine(x, y, x2, y2, paint);
    }

    public void drawRect(int x, int y, int width, int height, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawRect(x, y, x + width - 1, y + width - 1, paint);
    }

    public void drawArc(int x, int y, int width, int height, int startAngle, int sweepAngle, int color) {
        paint.setColor(color);
        RectF oval = new RectF(x, y, x + width - 1, y + height -1);
        canvas.drawArc(oval, startAngle, sweepAngle, true, paint); // true is usecenter, idk what it does yet
    }

    public void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY,
                           int srcWidth, int srcHeight) {
        srcRect.left = srcX;
        srcRect.top = srcY;
        srcRect.right = srcX + srcWidth - 1;
        srcRect.bottom = srcY + srcHeight - 1;

        dstRect.left = x;
        dstRect.top = y;
        dstRect.right = x + srcWidth - 1;
        dstRect.bottom = y + srcHeight - 1;

        canvas.drawBitmap(((AndroidPixmap) pixmap).bitmap, srcRect, dstRect, null);
    }

    public void drawPixmap(Pixmap pixmap, int x, int y) {
        canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y, null);
    }

    public int getWidth() {
        return frameBuffer.getWidth();
    }

    public int getHeight() {
        return frameBuffer.getHeight();
    }
}

As I said, the drawPixmap class works, as well as the clear class

And here is the relevant part of the implementation

 public void present(float deltaTime){
        Graphics g = game.getGraphics(); 

        g.clear(0xffffff); //From here down works - clears background and makes white
        g.drawPixmap(Assets.getTitle(), 240, 20 ); // Display title
        g.drawPixmap(Assets.getStart(), 540, 300); // Display start button
        g.drawPixmap(Assets.getScore(), 590, 550); // Display score button / connect with google play
        g.drawPixmap(Assets.getThemeScreen(),740, 550); // Display theme screen
        if(Settings.soundEnabled)                        // Display sound icon
            g.drawPixmap(Assets.getSoundOn(), 440, 550);
        else
            g.drawPixmap(Assets.getSoundOff(), 440, 550);

        g.drawLine(100, 200, 300, 400, 0x000000); //This does not work - draws black line
    }
Community
  • 1
  • 1
David Klu
  • 21
  • 5
  • I am not a game developer so I am not sure but just a random thought. What happens if you use "0xFF000000" including alpha value? – Emma Jul 06 '16 at 21:56
  • @Emma, Can I give you a hug, that was it!!!!! You have spared me so many more days of agony, you don't understand how thankful I am! Seriously, thank you! – David Klu Jul 06 '16 at 22:16
  • I am glad to hear that worked out :) – Emma Jul 06 '16 at 22:24

0 Answers0