nikita36078 / J2ME-Loader

A J2ME emulator for Android.
Apache License 2.0
1.72k stars 195 forks source link

Update DirectGraphicsImp.java #36

Closed dengzhenli closed 6 years ago

dengzhenli commented 6 years ago

Hello,Thank you for your contribution.

When I have used this software,I met a Exception:

W/System.err: java.lang.IllegalArgumentException: x + width must be <= bitmap.width() 11-05 19:49:50.783 17965-18097/ua.naiksoftware.j2meloader W/System.err: at

android.graphics.Bitmap.createBitmap(Bitmap.java:667) 11-05 19:49:50.783 17965-18097/ua.naiksoftware.j2meloader W/System.err: at

javax.microedition.lcdui.Image.createImage(Image.java:84) 11-05 19:49:50.783 17965-18097/ua.naiksoftware.j2meloader W/System.err: at

javax.microedition.lcdui.Graphics.drawRegion(Graphics.java:462) 11-05 19:49:50.783 17965-18097/ua.naiksoftware.j2meloader W/System.err: at

com.nokia.mid.ui.DirectGraphicsImp.drawImage(DirectGraphicsImp.java:81)

It makes me unable to play games normally.So,I debug the program and find the problem in

the method com.nokia.mid.ui.DirectGraphicsImp.drawImage(...),It should start in the

(graphics.getTranslateX(), graphics.getTranslateY()),not(x+graphics.getTranslateX(),

y=graphics.getTranslateY()).It's better to surrounded by a try。

The variable FLIP_HORIZONTAL and FLIP_VERTICAL should exchange,or the image is handstand. And It’s unnecessary to throw a Exception but never deal with it when we development with

Android,just return.

It's my advice.Come on,make it better;

nikita36078 commented 6 years ago

Can you give me any examples of games where this exception throws?

dengzhenli commented 6 years ago

Koudailingshouxx

nikita36078 commented 6 years ago

I think I fixed the problem in the last commits, but additional testing is needed.

dengzhenli commented 6 years ago

I've downloaded it, it's no big problem, but the picture still upside down

dengzhenli commented 6 years ago

There's four status,up,down,left,right.The right status is display upside down.It's no problem when I play the game by j2me runner.

dengzhenli commented 6 years ago

private static int getTransformation(int manipulation) { // manipulations are C-CW and sprite rotations are CW int ret = -1; int rotation = manipulation & 0x0FFF; if ((manipulation & FLIP_HORIZONTAL) != 0) { if ((manipulation & FLIP_VERTICAL) != 0) { // horiz and vertical flipping switch (rotation) { case 0: ret = Sprite.TRANS_NONE; break; case ROTATE_90: ret = Sprite.TRANS_ROT90; break; case ROTATE_180: ret = Sprite.TRANS_ROT180; break; case ROTATE_270: ret = Sprite.TRANS_ROT270; break; default: } } else { // horizontal flipping switch (rotation) { case 0: ret = Sprite.TRANS_MIRROR; //exchang with ROTATE_180 break; case ROTATE_90: ret = Sprite.TRANS_MIRROR_ROT90; break; case ROTATE_180: //exchang with 0 ret = Sprite.TRANS_MIRROR_ROT180; break; case ROTATE_270: ret = Sprite.TRANS_MIRROR_ROT270; break; default: } } } else { if ((manipulation & FLIP_VERTICAL) != 0) { // vertical flipping switch (rotation) { case 0: ret = Sprite.TRANS_MIRROR; //exchang with ROTATE_180 break; case ROTATE_90: ret = Sprite.TRANS_MIRROR_ROT270; break; case ROTATE_180: ret = Sprite.TRANS_MIRROR_ROT180; //exchang with 0 break; case ROTATE_270: ret = Sprite.TRANS_MIRROR_ROT90; break; default: } } else { // no flipping switch (rotation) { case 0: ret = Sprite.TRANS_NONE; break; case ROTATE_90: ret = Sprite.TRANS_ROT270; break; case ROTATE_180: ret = Sprite.TRANS_ROT180; break; case ROTATE_270: ret = Sprite.TRANS_ROT90; break; default: } } } return ret; }

I try to exchange ROTATE_180 and ROTATE_0 in horizontal flipping and vertical flipping,Will it have any impact on the other game?

nikita36078 commented 6 years ago

I use getTransformation method from the Symbian source, so it is definitely correct, problem can be in other place. Unfortunately I can't catch this bug in game which you sent.

Krchi commented 6 years ago

@nikita36078 Hi, this getTransformation function is turely has some error, you should modify it and many games which use this func can be fixed. What he said is right. You can see the api of the DirectGraphics and Sprite, and the implement in kemulater. this is my implement.

    private static int getTransformation(int manipulation) {
        // manipulations are C-CW and sprite rotations are CW
        int rotation = 0;
        switch (manipulation) {
            case 0:
                return Sprite.TRANS_NONE;
            case ROTATE_90:
                return Sprite.TRANS_ROT270;
            case ROTATE_180:
                return Sprite.TRANS_ROT180;
            case ROTATE_270:
                return Sprite.TRANS_ROT90;
            default:
                rotation = manipulation & 0xFFF;
        }
        if (rotation % 90 != 0)
            return 0;

        if ((manipulation & FLIP_VERTICAL) == FLIP_VERTICAL) {
            rotation += ROTATE_180;
        } else if ((manipulation & FLIP_HORIZONTAL) != FLIP_HORIZONTAL) {
            return 0;
        }

        int ret = 0;
        switch (rotation) {
            case 0:
                ret = Sprite.TRANS_MIRROR;
                break;
            case ROTATE_90:
                ret = Sprite.TRANS_MIRROR_ROT90;
                break;
            case ROTATE_180:
                ret = Sprite.TRANS_MIRROR_ROT180;
                break;
            case ROTATE_270:
                ret = Sprite.TRANS_MIRROR_ROT270;
                break;
        }
        return ret;
    }
}

You can also use HashMap to store and tran it.

nikita36078 commented 6 years ago

@Krchi @dengzhenli I guess it's fixed now. f8e6f77b