jnepo / beginnginandroidgames2

Automatically exported from code.google.com/p/beginnginandroidgames2
0 stars 0 forks source link

Deprecated code in game framework #1 #2

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
In AndroidGame.java you can no longer use 

        int frameBufferWidth = isLandscape ? 480 : 320;
        int frameBufferHeight = isLandscape ? 320 : 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight, Config.RGB_565);

        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();

my fix is this:

i make an Utils class in which i instantiate the screen size at the beginning 
of the game using "initializeScreenSize" . ALSO!!!! i lock down all my 
activities to make sure that they are either portrait or landscape but can't be 
both. this is because orientation changes the values you get for screen size. 
this is one fix, maybe not the best, but it'll certain work for ALL versions of 
Android, and get past the deprecation.

package com.badlogic.androidgames.framework;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;

public class Utils {
    private static int screenWidth = -1;
    private static int screenHeight = -1;

    public static int getScreenWidth(){
        return screenWidth;
    }

    public static int getScreenHeight(){
        return screenHeight;
    }

    public static void initializeScreenSize(Context ctx){
        Point size = new Point();
        Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Utils.instantiateSize(display, size);
        screenWidth = size.x;
        screenHeight = size.y;
    }
    @SuppressWarnings("deprecation")
    private static void instantiateSize(Display display, Point outSize) {
        try {
            // test for new method to trigger exception
            Class<?> pointClass = Class.forName("android.graphics.Point");
            Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass });
            //no exception, so new method is available, just use it
            newGetSize.invoke(display, outSize);
        } catch(NoSuchMethodException ex) {
            // new method is not available, use the old ones
            outSize.x = display.getWidth();
            outSize.y = display.getHeight();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

Original issue reported on code.google.com by davidtia...@gmail.com on 9 Jan 2013 at 7:13

GoogleCodeExporter commented 8 years ago
specifically, u can't call "getWindowManager().getDefaultDisplay().getWidth();"

Original comment by davidtia...@gmail.com on 9 Jan 2013 at 7:14

GoogleCodeExporter commented 8 years ago
Doesnt show me any error on :
  int frameBufferWidth = isLandscape ? 480 : 320;
        int frameBufferHeight = isLandscape ? 320 : 480;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
                frameBufferHeight, Config.RGB_565);
It only says :

        float scaleX = (float) frameBufferWidth
                / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight
                / getWindowManager().getDefaultDisplay().getHeight();
getWidth and getHeight are depracated so i added this:
float scaleX;
        float scaleY;
        if(Build.VERSION.SDK_INT < 13){
            scaleX = (float) frameBufferWidth
                    / getWindowManager().getDefaultDisplay().getWidth();
            scaleY = (float) frameBufferHeight
                    / getWindowManager().getDefaultDisplay().getHeight();
        }
        else{
            Point screenSize = new Point();
            scaleX = (float) frameBufferWidth
                    / screenSize.x;
            scaleY = (float) frameBufferHeight
                    / screenSize.y;
        }
It seems to work for me .

Original comment by skenderi...@gmail.com on 3 Mar 2015 at 5:05