0

I have 2 question. First why the background is not fullscreen but I have this space bottom? Second why this strange delay of ship in the space when the background is not drawn?

img1 img2

package com.caccagame;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GamePanel extends SurfaceView implements Callback {

    private MainThread thread;
    private boolean pause_game;
    private Background background;
    private float shipSpeed;
    private Ship ship;

    private int x;
    private int y;


    public GamePanel(Context context, Game game, int screenWidth, int screenHeight ) {
        super(context);
        getHolder().addCallback(this);
        thread=new MainThread(getHolder(), this);
        background=new Background(BitmapFactory.decodeResource(getResources(), R.drawable.game_fon), screenWidth, screenHeight, this);
        setFocusable(true);
        ship=new Ship(BitmapFactory.decodeResource(getResources(), R.drawable.player), screenWidth, screenHeight);
        x=100; //posizione iniziale della nave X
        y=screenHeight/2;
        shipSpeed=screenWidth/2.f;      
    }

    /*public boolean onTouchEvent(MotionEvent event){
        if(event.getAction()==MotionEvent.ACTION_DOWN)
            ship.setUp(true);
        if(event.getAction()==MotionEvent.ACTION_UP){
            ship.setUp(false);
        }       
        return true;
    }*/


    public boolean onTouchEvent(MotionEvent event){

        if(event.getAction()==MotionEvent.ACTION_UP){
            return true;
        }       
        if(event.getAction()==MotionEvent.ACTION_MOVE){
            x=(int)event.getX();
            y=(int)event.getY();

        }   
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            return true;
        }

        return true;
    }

    public void draw(Canvas canvas){
        if(!pause_game)
            if(canvas!=null){               
                background.draw(canvas);
                ship.draw(canvas);
            }
    }

    public void update(float dt){
        background.update(dt);
        ship.update(dt, x, y);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        boolean retray=true;
        while(retray){
            try{
                thread.join();
                retray=false;
            }catch(InterruptedException e){
                System.out.println("errore thread");
            }
        }
    }

    public boolean getPauseGame(){
        return this.pause_game;
    }

    public void setPauseGame(boolean isGamePause){
        this.pause_game=isGamePause;
    }

    public float getShipSpeed() {       
        return shipSpeed;
    }

    public void setThread(boolean isRunning){
        thread.setRunning(isRunning);
    }

}

package com.caccagame;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Ship {

    private Bitmap bitmap;
    private int x;
    private int y;
    private int speed;
    private int screenWidth;
    private int screenHight;
    private ArrayList<Bitmap> boom;
    private boolean death;
    //private boolean up;
    //private float verticalSpeed;

    private float animationTime;
    private float animationTotalTime;
    private float numFrame; //e' uguale alla lunghezza dell'array

    public Ship(Bitmap decodeResource, int screenWidth, int screenHight) {
        this.bitmap=decodeResource;
        this.x=0;
        this.y=0;
        this.screenWidth=screenWidth;
        this.screenHight=screenHight;
        speed=1;
        death=false;
        boom=null;
        animationTime=0;
        animationTotalTime=1;
        //verticalSpeed=0;
    }

    public void setBoomAnimation(ArrayList<Bitmap> boomBitmap){
        this.boom=new ArrayList<Bitmap> (boomBitmap);
        numFrame=boom.size()+1;

    }   

    public void draw(Canvas canvas){
        if(!death){
            canvas.drawBitmap(bitmap, x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
        }
        else{
            int index=(int)(animationTime/animationTotalTime*numFrame);
            if(index<numFrame){
                canvas.drawBitmap(boom.get(index), x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
            }
        }
    }

    public void update(float dt, int x, int y){
        if(death){
            animationTime+=dt;
        }
        else{
            this.x=x;
            this.y=y;

        }
    }


    /*public void draw(Canvas canvas){
        if(!death){
            canvas.drawBitmap(bitmap, x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
        }
        else{
            int index=(int)(animationTime/animationTotalTime*numFrame);
            if(index<numFrame){
                canvas.drawBitmap(boom.get(index), x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);
            }
        }
    }*/

    /*public void update(float dt){
        if(death){
            animationTime+=dt;
        }
        else{
            verticalSpeed+=screenHight/2*dt;
            if(up)
                verticalSpeed-=screenHight*dt*2;    
            y+=verticalSpeed*dt;
            if(y-(bitmap.getHeight()/2)>screenWidth)
                y=0-(bitmap.getHeight()/2);

        }
    }*/



    /*public void setUp(boolean b) {
        this.up=b;
    }   */


}

package com.caccagame;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Background {

    Bitmap backBitmap;
    int x;
    int y;
    int screenWidth;
    int countBackground;
    GamePanel gamePanel;
    Paint paint=new Paint();

    public Background(Bitmap bm, int screen_w, int screen_h, GamePanel gamePanel){
        this.backBitmap=bm;
        this.screenWidth=screen_w;
        this.x=0;
        this.y=0;//(screen_h- backBitmap.getHeight())/2 ;
        this.countBackground=screenWidth/backBitmap.getWidth()+1;
        this.gamePanel=gamePanel;
        paint.setFilterBitmap(true);
    }

    public void draw(Canvas canvas){
        for(int i=0; i<countBackground+1; i++){
            if(canvas!=null)
                canvas.drawBitmap(backBitmap, backBitmap.getWidth()*i+x, y, paint);
        }
        if(Math.abs(x)>backBitmap.getWidth())
            x=x+backBitmap.getWidth(); 
    }

    public void update(float dt){
        x=(int)(x-gamePanel.getShipSpeed()*dt);
    }
}
Termininja
  • 6,620
  • 12
  • 48
  • 49

1 Answers1

0

Most likely that the bitmap you are drawing is the wrong size. Try using

drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);

With the src as null

RectF dest = new RectF(backBitmap.getWidth() * i + x, y, backBitmap.getWidth() * i + x + screen_w,y + screen_h);
canvas.drawBitmap(backBitmap, null, dest, paint);
Blindman67
  • 51,134
  • 11
  • 73
  • 136
  • it works well but with low fps. I think thet is because i put the constructor of dest in for cicle. Waht exactly do RectF. Can i Construct it outside the cicle and after pass the X parameters in drawBirmap in the for? – Maksym Bodnar Jan 10 '16 at 20:42
  • @MaksymBodnar Yes sorry put the constructor outside the loop and just set the properties as needed RectF is the floating point equivalent to Rect which just takes integers. It could also be slower because of the render having to scale and translate the images. It may pay to create a new image from the original at the correct size to fit the background when you start, if moving the RectF construtor out of the loop does not improve performance. – Blindman67 Jan 10 '16 at 21:26
  • thx very much! Jas last question. why exactly my code don't print full screen bitmap? – Maksym Bodnar Jan 11 '16 at 07:36