amelioration du moteur + ajout de DrawableCard pour simplifer la creation de carte + correction du support de texture + correction du support de rectangle

This commit is contained in:
mmarc471@gmail.com
2020-09-08 22:53:14 +02:00
parent 589d6b7a80
commit f121e55071
22 changed files with 495 additions and 121 deletions
@@ -0,0 +1,147 @@
package com.iutlaval.myapplication.Video;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import androidx.annotation.NonNull;
import com.iutlaval.myapplication.GameActivity;
import com.iutlaval.myapplication.InvalidDataException;
public class Drawable {
private float x,y;
private String name;
private Bitmap bitmap;
/**
* cree un drawable a partir de coordonné est d'un nom
* @param x coordoné x compris entre 0 et 100
* @param y coordoné y comrpis entre 0 et 100
* @param name nom du drawable agit comme un identifiant mais doit être unique
*/
protected Drawable(float x,float y,String name)
{
this.x=x;
this.y=y;
this.name=name;
}
/**
* cree un drawable depuis une bitmap est des coordonnés
* @param bitmap bitmap
* @param x coordoné x compris entre 0 et 100
* @param y coordoné y comrpis entre 0 et 100
* @param name nom du drawable agit comme un identifiant mais doit être unique
*
* deprecier car non adapter a la resolution de l'ecran
*/
@Deprecated
public Drawable(Bitmap bitmap,float x,float y,String name){
this(x,y,name);
this.bitmap=bitmap;
}
/**
* cree un drawable depuis une bitmap est des coordonnés
* @param bitmap bitmap
* @param x_pos coordoné x compris entre 0 et 100
* @param y_pos coordoné y comrpis entre 0 et 100
* @param name nom du drawable agit comme un identifiant mais doit être unique
* deprecier car non adapter a la resolution de l'ecran
*
* TODO : echelle dynamique avec l'ecran
*/
public Drawable(@NonNull Bitmap bitmap, float x_pos, float y_pos, String name, float x_size, float y_size) throws InvalidDataException {
this(x_pos,y_pos,name);
float x_scaled_size = x_size* GameActivity.screenWidth/100;
float y_scaled_size = y_size* GameActivity.screenHeight/100;
System.out.println(x_scaled_size + " y: " + y_scaled_size);
System.out.println(y_size);
if(x_scaled_size <=0 || y_scaled_size <=0)
{
throw new InvalidDataException(name,x_scaled_size,y_scaled_size);
}
this.bitmap = Bitmap.createScaledBitmap(bitmap, (int)x_scaled_size, (int)y_scaled_size, GameActivity.bilinearFiltering);
Log.i("loul",""+this.bitmap.getWidth());
}
/**
* ce constructeur n'est pas rapide essayer de l'utiliser le moin possible
* TODO : optimize it and remove the deprecated flag
*
* this contructor create a Drawable object from a rectangle and a color
* @param rectangle recange cooresspondant a la surface a dessiner
* @param name nom du drawable agit comme un identifiant mais doit être unique
* @param color couleur du rectangle
*/
@Deprecated
public Drawable(Rectangle rectangle, String name, int color) throws InvalidDataException {
if(rectangle.getHeight() <= 0 || rectangle.getWidth() <=0)throw new InvalidDataException(name,rectangle);
rectangle.scaleRectangleToScreen();
Bitmap bitmap = Bitmap.createBitmap((int)rectangle.getWidth(),(int)rectangle.getHeight(), Bitmap.Config.ARGB_8888);
//on crée un canevas pour interagir avec la bitmap
RectangleCanevas c = new RectangleCanevas(bitmap);
Paint p = new Paint();
p.setColor(color);
p.setStyle(Paint.Style.FILL_AND_STROKE);
c.drawRect(rectangle,p);
this.bitmap=bitmap;
this.name=name;
this.x=rectangle.getPositionX();
this.y=rectangle.getPositionY();
}
public void drawOn(Canvas c, Paint p)
{
//drawings
c.drawBitmap(getBitmap(),x* GameActivity.screenWidth/100,y* GameActivity.screenHeight/100,p);
}
public Bitmap getBitmap() {
return bitmap;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if(o instanceof Drawable)
{
return ((Drawable)o).getName().equals(name);
}
return false;
}
public void setCoordinates(float x, float y) {
this.x=x;
this.y=y;
}
/**
* cette fonction est appeler quand le drawable est retirer de la liste toDraw
*/
public void onDeletion(Renderer r){}
}
@@ -0,0 +1,42 @@
package com.iutlaval.myapplication.Video;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import com.iutlaval.myapplication.Game.Cards.Card;
import com.iutlaval.myapplication.InvalidDataException;
public class DrawableCard extends Drawable{
private static final int CARD_WITH = 14;
private static final int CARD_HEIGHT = 32;
private String color;
private Drawable OpacityRectangleDrawable;
private Drawable PictureDrawable;
public DrawableCard(Card c, float x, float y, String name, Context context) throws InvalidDataException {
super(c.getFrameBitmap(context),x,y,name,CARD_WITH,CARD_HEIGHT);
color = c.getColor();//CARD_WITH+x
Rectangle cardRect = new Rectangle(x,y,(float)(CARD_WITH+x),(float)(CARD_HEIGHT+x));
OpacityRectangleDrawable = new Drawable(cardRect,toString()+"Opacity", Color.parseColor(c.getColor()));
Bitmap pictureBitmap = BitmapFactory.decodeResource(context.getResources(),c.getCardPicture());
PictureDrawable = new Drawable(pictureBitmap,getX()+1.1F,getY()+2.5F,toString()+"Picture",CARD_WITH-2.4F,CARD_HEIGHT/2 - 4);
}
@Override
public void drawOn(Canvas c, Paint p) {
//draw the frame
super.drawOn(c, p);
OpacityRectangleDrawable.drawOn(c,p);
PictureDrawable.drawOn(c,p);
}
}
@@ -0,0 +1,4 @@
package com.iutlaval.myapplication.Video;
public class DrawableNotFoundException extends RuntimeException{
}
@@ -0,0 +1,19 @@
package com.iutlaval.myapplication.Video;
import android.view.Display;
public class FpsTime {
private static long frametime;
public static void init(Display d)
{
float fps = d.getRefreshRate();
frametime = (long)(1.0F/fps*1000);
}
public static void waitFrameTime(){
try {
Thread.sleep((frametime));
} catch (InterruptedException e) {}
}
}
@@ -0,0 +1,45 @@
package com.iutlaval.myapplication.Video;
import com.iutlaval.myapplication.GameActivity;
public class Rectangle {
float positionX,positionY,width,height;
public Rectangle(float positionX,float positionY,float width,float height)
{
set(positionX,positionY,width,height);
}
public void set(float positionX,float positionY,float width,float height)
{
this.positionX=positionX;
this.positionY=positionY;
this.width=width;
this.height=height;
}
public float getPositionX() {
return positionX;
}
public float getPositionY() {
return positionY;
}
public float getHeight() {
return height;
}
public float getWidth() {
return width;
}
protected void scaleRectangleToScreen()
{
set(getPositionX()* GameActivity.screenWidth/100,
getPositionY()*GameActivity.screenHeight/100,
getWidth()*GameActivity.screenWidth/100,
getHeight()*GameActivity.screenHeight/100
);
}
}
@@ -0,0 +1,23 @@
package com.iutlaval.myapplication.Video;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.NonNull;
public class RectangleCanevas extends Canvas {
public RectangleCanevas(Bitmap bitmap)
{
super(bitmap);
}
public void drawRect(@NonNull Rectangle r, @NonNull Paint paint) {
super.drawRect(r.getPositionX(),
r.getPositionY(),
r.getPositionX()+r.getWidth(),
r.getPositionY()+r.getHeight(),
paint);
}
}
@@ -0,0 +1,166 @@
package com.iutlaval.myapplication.Video;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
public class Renderer extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Paint p;
private DrawingThread drawingThread;
private List<Drawable> toDraw;
public Renderer(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
drawingThread = new DrawingThread();
p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
p.setColor(Color.RED);
p.setStyle(Paint.Style.FILL_AND_STROKE);
toDraw = new ArrayList<>();
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
drawingThread.start();
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
drawingThread.keepDrawing = false;
try {
drawingThread.join();
} catch (InterruptedException e) {}
}
@Override
protected void onDraw(Canvas canvas) {
for(Drawable d : toDraw)
{
d.drawOn(canvas,p);
}
}
private class DrawingThread extends Thread {
public boolean keepDrawing = true;
@SuppressLint("WrongCall")
@Override
public void run() {
while (keepDrawing) {
Canvas canvas = null;
try {
// On récupère le canvas pour dessiner dessus
canvas = holder.lockCanvas();
// On s'assure qu'aucun autre thread n'accède au holder
synchronized (holder) {
// Et on dessine
onDraw(canvas);
}
} finally {
// Notre dessin fini, on relâche le Canvas pour que le dessin s'affiche
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
}
//empecher d'aller plus vite que le taux de rafraichissement de l'ecran
//sauve de la baterie
FpsTime.waitFrameTime();
}
}
}
/**
* permet d'ajouter un element a afficher
* @return retourne faux si l'element existe deja
*/
public boolean addToDraw(Drawable newElement){
for(Drawable d : toDraw)
{
if(d.equals(newElement))
{
return false;
}
}
toDraw.add(newElement);
return true;
}
/**
* permet de changer les coordoné d'un objet a l'ecran
* @param x coordonné en x (-1.0 a 1.0)
* @param y coodonné en y (-1.0 a 1.0)
* @param name nom de l'element a modifier
*/
public void moveToDraw(float x, float y, String name) throws DrawableNotFoundException {
for(Drawable d : toDraw)
{
if(d.getName().equals(name))
{
d.setCoordinates(x,y);
return;
}
}
throw new DrawableNotFoundException();
}
/**
* supprime le drawable possedant le nom indique
* @param name le nom
*/
public void removeToDraw(String name){
Drawable d = getDrawAble(name);
if(d != null)
toDraw.remove(d);
}
/**
* remove the element from the draw list
* @param toRemove the element toRemove
*/
public void removeToDraw(Drawable toRemove){
toRemove.onDeletion(this);
toDraw.remove(toRemove);
}
/**
* retourne le drawable possedant le nom indiqué
* @param name le nom du drawable
* @return
*/
public Drawable getDrawAble(String name){
Drawable drawable = null;
for(Drawable d : toDraw)
{
if(d.getName().equals(name))
{
drawable=d;
break;
}
}
return drawable;
}
}