vujadin / BabylonHx

Port of Babylon.js 3D engine to Haxe.
http:/paradoxplay.com/babylonhx
Apache License 2.0
189 stars 43 forks source link

openfl legacy compatibility #47

Open ramsestom opened 8 years ago

ramsestom commented 8 years ago

I can't compile anymore with the -Dlegacy flag to use openfl legacy rather than openfl 3. This used to work well but since I updated lime, openfl and babylonHx, there is issues when I try to compile with the -Dlegacy tag. I don't know if it is related to lime that do not consider openfl legacy as a lime alias case for compiler conditionnal flags anymore or if you changed something in the source code of babylonhx but, to make the compiler to work with openfl legacy, I had to perform changes in:

vujadin commented 8 years ago

Hi, you should make that pull request, I'll make it work for both lime and openfl later (and openfl legacy). For loading images in openfl you can look at the code for NME, it should be identical for openfl. Instead of Image in Openfl (as in NME) BitmapData can be used.

On Wed, Sep 9, 2015 at 6:56 PM, ramsestom notifications@github.com wrote:

I can't compile anymore with the -Dlegacy flag to use openfl legacy rather than openfl 3. This used to work well but since I updated lime, openfl and babylonHx, there is issues when I try to compile with the -Dlegacy tag. I don't know if it is related to lime that do not consider openfl legacy as a lime alias case for compiler conditionnal flags anymore or if you changed something in the source code of babylonhx but, to make the compiler to work with openfl legacy, I had to perform changes in:

  • com.babylonhx.tools.Tools -com.babylonhx.Engine - com.babylonhx.utils.GL -all the com.babylonhx.utils.typedarray classes to add "#if openfl" compiler condition flags when necessary. I was able to do it for every classes except at two point of the com.babylonhx.tools.Tools class as openfl do not have an Image class (unlike lime) and I don't know what to use in replacment. Did you have some time to look at it? (I can make a pull request with the changes I already done if you want)

— Reply to this email directly or view it on GitHub https://github.com/vujadin/BabylonHx/issues/47.

vujadin commented 8 years ago

I've just fixed image loading for openfl legacy. it builds now with -Dlegacy flag

ramsestom commented 8 years ago

Ok thanks. autocompletion on flashdevelopp did not propose me getRGBAPixels for openfl.display.BitmapData so I thought openfl BitmapData did not have the getRGBAPixels function ;) (wich was my issue to completely fix the openfl legacy problem in the Tool class). But actually this function seems to exists as, whith your changes, I was able to compile my test sample with -Dlegacy flag without any compilation error. Thanks. However, my working sample with openfl 3 now render as a black screen when compiled with openfl legacy :(

vujadin commented 8 years ago

Does your sample have any 2D content ? With legacy I do get 3D content rendered but not sprite on top of it. If I add FPS counter the app crashes.

ramsestom commented 8 years ago

No. it has no 2d content. it is only the CSGDemo scene. Here is my main openfl class:

package;

import openfl.display.Bitmap; import openfl.display.BitmapData; import openfl.display.Sprite; import openfl.Assets; import openfl.text.TextField; import openfl.text.TextFormat;

import com.babylonhx.Engine; import com.babylonhx.Scene;

class Main extends Sprite {

private var scene:Scene;
private var engine:Engine;
private var _babylonSprite:Sprite;

public function new () {

    super ();

    _babylonSprite = new Sprite();
    engine = new Engine(this, false);   
    scene = new Scene(engine);
    engine.width = stage.stageWidth;
    engine.height = stage.stageHeight;
    new CSGDemo(scene); 
    addChild(_babylonSprite);

    #if legacy
        trace("Legacy");
    #end

}

}

vujadin commented 8 years ago

Can you try this (it works for me):

package;

import openfl.display.Stage; import openfl.display.Sprite; import openfl.events.Event; import openfl.events.MouseEvent; import openfl.events.KeyboardEvent; import openfl.events.TouchEvent; import openfl.display.OpenGLView; import openfl.Lib; import openfl.display.FPS;

import com.babylonhx.Engine; import com.babylonhx.Scene; import com.babylonhx.utils.Keycodes;

/**

class MainOpenFL extends Sprite {

var scene:Scene;
var engine:Engine;

public function new() {
    super();

    Lib.current.stage.addChild(this);

    engine = new Engine(this, false);   
    scene = new Scene(engine);

    engine.width = Lib.current.stage.stageWidth;
    engine.height = Lib.current.stage.stageHeight;

    Lib.current.stage.addEventListener(Event.RESIZE, resize);
    Lib.current.stage.addEventListener(Event.ENTER_FRAME, update);

    #if desktop
    Lib.current.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    Lib.current.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    Lib.current.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    Lib.current.stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
    Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
    Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    #elseif mobile
    Lib.current.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchStart);
    Lib.current.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
    Lib.current.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
    #end

    new samples.CSGDemo(scene);
}

function resize(e){
    engine.width = Lib.current.stage.stageWidth;
    engine.height = Lib.current.stage.stageHeight;
}

function onKeyDown(e:KeyboardEvent) {
    for(f in Engine.keyDown) {
        f(e.charCode);
    }       
}   

function onKeyUp(e:KeyboardEvent) {
    for(f in Engine.keyUp) {
        f(e.charCode);
    }
}   

function onMouseDown(e:MouseEvent) {
    for(f in Engine.mouseDown) {
        f(e.localX, e.localY, 0);
    }
}   

function onMouseMove(e:MouseEvent) {
    for(f in Engine.mouseMove) {
        f(e.localX, e.localY);
    }
}   

function onMouseUp(e:MouseEvent) {
    for(f in Engine.mouseUp) {
        f(e.localX, e.localY, 0);
    }
}

function onMouseWheel(e:MouseEvent) {
    for (f in Engine.mouseWheel) {
        f(e.delta);
    }
}

function onTouchStart(e:TouchEvent) {
    for(f in Engine.touchDown) {
        f(e.localX, e.localY, 0);
    }
}

function onTouchEnd(e:TouchEvent) {     
    for(f in Engine.touchUp) {
        f(e.localX, e.localY, 0);
    }
}   

function onTouchMove(e:TouchEvent) {
    for(f in Engine.touchMove) {
        f(e.localX, e.localY);
    }       
}

function update(e) {
    engine._renderLoop();
}

}

ramsestom commented 8 years ago

Ok. I tested with and without the -Dlegacy flag. And the result is the same as with my code, it works with openfl 3 but do not work with legacy (black screen). Could you add

if legacy

trace("Legacy");

end

and retest with -Dlegacy just to be sure that your compiler do not ignore the -Dlegacy flag?

Also, which version of lime/openfl do you use. Mine: lime 2.6.2 and openfl 3.3.3

vujadin commented 8 years ago

It definitely trace "Legacy". But why do you need legacy in the first place ?

ramsestom commented 8 years ago

I need legacy because haxeflixel only works with openfl legacy for now (many things are not implemented yet in openfl 3 or are bugged whereas openfl legacy is "completely" mature). In the future, haxeflixel should be able to use openfl 3 but I think it is not before months and I need some 3D in my haxeflixel projects now ;) ...

ramsestom commented 8 years ago

One thing really stange I just noticed. When compiled without the -dlegacy flag, my android app only trace the engine start message (BabylonHx - Cross-Platform 3D Engine | 2015 | www.babylonhx.com) once. Whereas when compiled with legacy, it is outputed twice, with two different process ids: I/trace (32107): Engine.hx:166: BabylonHx - Cross-Platform 3D Engine | 2015 | www.babylonhx.com I/trace ( 1977): Engine.hx:166: BabylonHx - Cross-Platform 3D Engine | 2015 | www.babylonhx.com

vujadin commented 8 years ago

I'm not familiar with HaxeFlixel but I just looked at their site. It says that latest version is compatible with openfl 3.3.3 so I tried the simplest example I could find to mix with BHx and it works. Here's the screenshot: http://www.pixhost.org/show/3678/29338804_openfl-haxeflixel.jpg You can also see that yellow sprite and fps counter are rendered correctly above 3D content and flixel content. I've built the project with -Dnext flag

vujadin commented 8 years ago

And FYI, "invisible mesh dirty hack" is replaced with "even dirtier hack with bug in edge renderer". EdgeRenderer is one of the latest features in BabylonJS which I ported to BHx, its in the latest code submitted yesterday. It renders the "edges" so you can see the polygons of your mesh. In the screenshot you can see the sphere with EdgeRendering turned on, but the thing is when you turn it on on whatever mesh you have, OpenFL 2D content is rendered perfectly ! And the mesh doesn't have to be in frustum at all. And, as a bonus, EdgeRendereing doesn't work for plane mesh (its a bug in my port) so simply turning it on for a plane mesh will render your 2D content right and you won't even see (ugly) edges on your plane :)

ramsestom commented 8 years ago

If you compile with the -Dnext flag, then you use openfl 3 and not openfl legacy. This is why it works probably. The problem is that, currently, if you use the -Dnext flag, some of the haxeflixel features would be broken (for example, openfl 3 do not implement sharedobjects yet (or at least a few days ago it didn't). As a consequence, you can't save any objects and save game scores or data locally for example. Which is pretty annoying...)

vujadin commented 8 years ago

If sharedobject is your only concern, as far as I know its in openfl 3 both -next and -legacy and never have moved or not been implemented for any version of openfl. its inherited from NME and its in openfl from its begining. A friend has released this game not too long ago: https://play.google.com/store/apps/details?id=com.phyziko.truck He's using sharedobject to keep track of game levels that the player has passed, he also keeps scores in it etc...

ramsestom commented 8 years ago

The sharedobject is not the only concern and there were many issues when using openfl "next" with haxeflixel untill recently. I don't know if everything as been fixed with the new version of openfl but I can ensure you that SharedObject was not correctly implemented in openfl 3 until recently: https://github.com/openfl/openfl/issues/593 Anyway, I tried to build one of my haxeflixel project with -Dnext and the build failed so it doen't seems to be an option for me now. (will look more in detail at the error, might be "easy" to fix but I doubt it)

EDIT: OK, by performing some minor changes in my code, I was able to build my haxeflixel project with -Dnext without error. But unfortunately, there is too much things not working in the final app for me to consider this as a viable option for now (sounds crash, font an textfield issues, bitmap colortransform not working properly and more...)

ramsestom commented 8 years ago

When you said the openfl sample was working with openfl legacy, did you tested it on android or on windows? Because I just tested it on windows and it is working fine with the -Dlegacy flag. So, the black screen issue with -Dlegacy seems to be limited to android (and maybe other mobile platforms like ios. I can't test it) but isn't general to all cpp targets... And this seems to be a bug different form the "first frame only bug" as i can't see any display before the black screen (That is completely black whereas with the "first frame only bug", it seems more "darkgrey"). The strange thing though is that it used to work (with the Fog or BumpMap sample, I don't remember) on android with both openfl legacy and openfl 3 before I performed the lime, openfl and babylonHx update yesterday... So something must have changed in latest versions of lime/openfl or in babylonHx that introduced this bug. I will try to come back to previous versions of lime and openfl to see if I can make it work again with this or if the issue might be in babylonhx changes.

dresch86 commented 8 years ago

@vujadin I'm not familiar with HaxeFlixel but I just looked at their site. It says that latest version is compatible with openfl 3.3.3 so I tried the simplest example I could find to mix with BHx and it works. Here's the screenshot: http://www.pixhost.org/show/3678/29338804_openfl-haxeflixel.jpg You can also see that yellow sprite and fps counter are rendered correctly above 3D content and flixel content. I've built the project with -Dnext flag

Do you have the code for this post? I'm trying to get OpenFL to render on top of BabylonHx, but the display object won't show up for some reason. I think the object is there under the scene because I used a TextField and set it to type input and see the cursor change when I mouse over where it should be. I cannot however see the text. Also, if I build without rendering the scene (still initialize the engine though) I see everything just fine so I must be missing something.

ramsestom commented 8 years ago

@vujadin: Can you share your haxeflixel sample source?

vujadin commented 8 years ago

Sure, later today when I get back from work I'll commit latest BHx changes/fixes together with that example

ramsestom commented 8 years ago

Thanks

vujadin commented 8 years ago

I've pushed latest changes/updates right now. You'll find example that draws HaxeFlixel content on top of 3D here: https://github.com/vujadin/BabylonHx/blob/master/src/MainOpenFL.hx There is a FD project file too.