Flash to iPhone Very Slow Performance - iphone

I'm creating an iPhone app in Flash, and I've run into performance problems. I've stripped the entire thing down to a simple example (below). It draws a box to the screen, and uses TouchEvent to track finger gestures. Here's the problem: it is extremely sluggish on the iPhone 3G I am testing on. The box stutters up and down the page.
GPU mode is enabled in the application.xml, and when I set the -renderingdiagnostics flag, the text turns blue (meaning it is being rendered each time, which is correct), but the square stays white. It doesn't turn any of the three colors of diagnostics mode. Here is a screen of that:
http://whit.info/dev/flashapp/screen.jpg
And here is a video of the sluggishness:
http://vimeo.com/25160240
So, given that this is only one cached sprite moving vertically, am I missing something about enabling the GPU or bitmap caching? Or is this as good as it gets on this hardware? Other apps seem to glide brilliantly.
Can anyone assist?
Many thanks!
-Whit
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
[SWF(width='320', height='480', backgroundColor='#BACC00', frameRate='60')]
public class Main extends MovieClip{
private var square:Sprite;
private var txt:TextField;
private var startDragY:Number;
private var startObjY:Number;
public function Main(){
Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;
stage.addEventListener(TouchEvent.TOUCH_BEGIN, beginhandler);
stage.addEventListener(TouchEvent.TOUCH_MOVE, movehandler);
stage.addEventListener(TouchEvent.TOUCH_END, endhandler);
drawBox(0xffffff);
makeOutput();
}
private function beginhandler(evt:TouchEvent): void {
startDragY = evt.stageY;
startObjY = square.y;
}
private function movehandler(evt:TouchEvent): void {
out(String(evt.stageY));
square.y = startObjY - (startDragY - evt.stageY);
}
private function drawBox(fill:Number):void {
square = new Sprite();
square.graphics.beginFill(fill);
square.graphics.drawRect(20,60,40,40);
square.graphics.endFill();
stage.addChild(square);
square.cacheAsBitmap = true;
}
private function makeOutput():void {
txt = new TextField();
stage.addChild(txt);
txt.selectable = false;
txt.autoSize = TextFieldAutoSize.CENTER;
txt.defaultTextFormat = new TextFormat("Arial", 22, 0x000000);
txt.text = "Touch Screen";
txt.x = stage.stageWidth/2 - txt.width/2;
txt.y = stage.stageHeight/2 - txt.height/2;
}
private function out(str:String):void {
txt.text = str;
}
}
}
Also, here are the commands I'm using to compile:
.amxmlc ~/Files/Code/iOS/MyApp/Main.as
.pfi -package -renderingdiagnostics -target ipa-test -provisioning-profile MyApp.mobileprovision -storetype pkcs12 -keystore Certificates.p12 -storepass MyPass MyApp.ipa application.xml Main.swf Default.png icons

Latest Adobe updates are meant for the devices.
Our team was also facing the same problem and kinda solved the problem by updating the products.
We updated to:
Flash professional CS5.5
AIR 2.7
and the performance difference is noticeable.

Which version of the Flash exporter are you using? The CS5.5 update allegedly came with some speed boosts.
Also, the iPhone 3g on iOS 4 is already memory starved and I wouldn't be surprised if you were running out of memory with AIR loaded.

If you want good animation performance on an iPhone 3G (perhaps any iOS device), write your app in Objective C using Apple's provided Xcode tools. The result will run tons faster and eat up far less of your customer's device's battery and memory.

Related

In Unity, how to detect if window is being resized and if window has stopped resizing

I wanted my UI to not resize when user is still resizing the game (holding click in the window border) and only when the user has released the mouse the resize event will trigger.
I have tried to achieve it on Unity but so far I only able to detect windows size change, which my script checked every 0.5 second and if detected change it will resize the UI. But of course resizing everything caused a heavy lag, so resizing every 0.5 second is not a good option but resizing every 1 second is not a good idea either because 1 second is considered too long.
The question might be too broad but I have specified the problem as small as possible, how do I detect if user is still resizing the window? And how do I detect if user has stopped resizing the window (stop holding click at window border)?
You can't tell when someone stops dragging a window, unless you want to code a low level solution for ever desktop environment and every operating system.
Here's what worked for me with any MonoBehavior class, using the OnRectTransformDimensionsChange event:
public class StackOverflow : MonoBehaviour
{
private const float TimeBetweenScreenChangeCalculations = 0.5f;
private float _lastScreenChangeCalculationTime = 0;
private void Awake()
{
_lastScreenChangeCalculationTime = Time.time;
}
private void OnRectTransformDimensionsChange()
{
if (Time.time - _lastScreenChangeCalculationTime < TimeBetweenScreenChangeCalculations)
return;
_lastScreenChangeCalculationTime = Time.time;
Debug.Log($"Window dimensions changed to {Screen.width}x{Screen.height}");
}
}
I have some good news - sort of.
When the user resizes the window on Mac or PC,
Unity will AUTOMATICALLY re-layout everything.
BUT in fact ONLY when the user is "finished" resizing the Mac/PC window.
I believe that is what the OP is asking for - so the good news, what the OP is asking for is quite automatic.
However. A huge problem in Unity is Unity does not smoothly resize elements as the user is dragging the mouse to expand the Mac/PC window.
I have never found a solution to that problem. (A poor solution often mentioned is to check the size of the window every frame; that seems to be about the only approach.)
Again interestingly, what the OP mentions
" ..and if window has stopped resizing .."
is automatically done in Unity; in fact do nothing to achieve that.
I needed something like this for re generating a line chart, but as it has too many elements, it would be heavy to do it on every update, so I came up with this, which for me worked well:
public class ToRunOnResize : MonoBehaviour
{
private float screenWidth;
private bool screenStartedResizing = false;
private int updateCounter = 0;
private int numberOfUpdatesToRunXFunction = 15; // The number of frames you want your function to run after, (usually 60 per second, so 15 would be .25 seconds)
void Start()
{
screenWidth = Screen.width; // Identifies the screen width
}
private void Update()
{
if (Screen.width != screenWidth) // This will be run and repeated while you resize your screen
{
updateCounter = 0; // This will set 0 on every update, so the counter is reset until you release the resizing.
screenStartedResizing = true; // This lets the application know when to start counting the # of updates after you stopped resizing.
screenWidth = Screen.width;
}
if (screenStartedResizing)
{
updateCounter += 1; // This will count the updates until it gets to the numberOfUpdatesToRunXFunction
}
if (updateCounter == numberOfUpdatesToRunXFunction && screenStartedResizing)
{ // Finally we make the counter stop and run the code, in my case I use it for re-rendering a line chart.
screenStartedResizing = false;
// my re-rendering code...
// my re-rendering code...
}
}
}

How to bind Preview and texture in CameraX

I recently tried to develop a flutter plugin with cameraX, but I found that there was no way to simply bind Preview to flutter's Texture.
In the past, I only needed use camera.setPreviewTexture(surfaceTexture.surfaceTexture()) to bind camera and texture, now I can't find the api.
camera.setPreviewTexture(surfaceTexture.surfaceTexture())
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
}.build()
// Build the viewfinder use case
val preview = Preview(previewConfig).also{
}
preview.setOnPreviewOutputUpdateListener {
// it.surfaceTexture = this.surfaceTexture.surfaceTexture()
}
// how to bind the CameraX Preview surfaceTexture and flutter surfaceTexture?
I think you can bind texture by Preview.SurfaceProvider.
final CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
final ListenableFuture<ProcessCameraProvider> listenableFuture = ProcessCameraProvider.getInstance(appCompatActivity.getBaseContext());
listenableFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = listenableFuture.get();
Preview preview = new Preview.Builder()
.setTargetResolution(new Size(720, 1280))
.build();
cameraProvider.unbindAll();
Camera camera = cameraProvider.bindToLifecycle(appCompatActivity, cameraSelector, preview);
Preview.SurfaceProvider surfaceProvider = request -> {
Size resolution = request.getResolution();
surfaceTexture.setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
Surface surface = new Surface(surfaceTexture);
request.provideSurface(surface, ContextCompat.getMainExecutor(appCompatActivity.getBaseContext()), result -> {
});
};
preview.setSurfaceProvider(surfaceProvider);
} catch (Exception e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(appCompatActivity.getBaseContext()));
Update: CameraX has added functionality which will now allow this since this answer was written, but this might still be useful to someone. See this answer for details.
It seems as though using CameraX is difficult to impossible due to it abstracting the more complicated things away and so not exposing things you need like being able to pass in your own SurfaceTexture (which is normally created by Flutter).
So the simple answer is that you can't use CameraX.
That being said, with some work you may be able to get this to work, but I have no idea if it will work for sure. It's ugly and hacky so I wouldn't recommend it. YMMV.
If we're going to do this, let's first look at how the flutter view creates a texture
#Override
public TextureRegistry.SurfaceTextureEntry createSurfaceTexture() {
final SurfaceTexture surfaceTexture = new SurfaceTexture(0);
surfaceTexture.detachFromGLContext();
final SurfaceTextureRegistryEntry entry = new SurfaceTextureRegistryEntry(nextTextureId.getAndIncrement(),
surfaceTexture);
mNativeView.getFlutterJNI().registerTexture(entry.id(), surfaceTexture);
return entry;
}
Most of that is replicable, so we may be able to do it with the surface texture the camera gives us.
You can get ahold of the texture the camera creates this way:
preview.setOnPreviewOutputUpdateListener { previewOutput ->
SurfaceTexture texture = previewOutput.surfaceTexture
}
What you're going to have to do now is to pass a reference to your FlutterView into your plugin (I'll leave that for you to figure out). Then call flutterView.getFlutterNativeView() to get ahold of the FlutterNativeView.
Unfortunately, FlutterNativeView's getFlutterJni is package private. So this is where it gets really hacky - you can create a class in that same package that calls that package-private method in a publicly accesible method. It's super ugly, and you may have to fiddle around with Gradle to get the compilation security settings to allow it, but it should be possible.
After that, it should be simple enough to create a SurfaceTextureRegistryEntry and to register the texture with the flutter jni. I don't think you want to detach from the opengl context, and I really have no idea if this will actually work. But if you want to try it out and report back what you find I would be interested in hearing the result!

Using BluetoothLEAdvertisementWatcher in background application?

I try to scan BLE devices in my Windows 10 IOT background (headless) app running on Raspberry PI 3.
I also tried to use BluetoothLEAdvertisementWatcher in a headed app (with UI) on the same RaspBerry PI machine and it worked.
My headless app is the simplest as it can be:
public sealed class StartupTask : IBackgroundTask
{
private readonly BluetoothLEAdvertisementWatcher _bleWatcher =
new BluetoothLEAdvertisementWatcher();
public void Run(IBackgroundTaskInstance taskInstance)
{
_bleWatcher.Received += _bleWatcher_Received;
_bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
_bleWatcher.Start();
}
private void _bleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
}
}
_bleWatcher_Received is never hit. Capabilities are set (Bluetooth, Internet, Proximity).
What is the problem? What do I miss?
You app shuts down when the run method completes. That's why _bleWatcher_Received is never hit.
To prevent you app from exiting you need call the “GetDeferral” method like this:
public void Run(IBackgroundTaskInstance taskInstance)
{
deferral = taskInstance.GetDeferral();
//YOUR CODE HERE
}
For more information please reference "Developing Background Applications".

Resetting after 3 seconds unity

I have a board manager in my game, that shows what is happening, for example, if I want to buy a car and you dont have the necessary money in this board appears a message saying "You dont have enough gold".
public void setTextMonitor (string mensaje) {
textScreen = mensaje;
}
public IEnumerator DoTheDance() {
yield return new WaitForSeconds(5); // Esperar 3 seconds
textScreen = "";
}
This is my code and it's working properly, when the board shows a message after 5 seconds it's deleted but the board doesn't shows new messages and I dont know why, probably because the couroutine doesn't let setTextMonitor method to work properly dunno.
If you have another idea or other method for doing this,
I'd be really grateful
For such behavior really easy to use LeanTween asset. It's free in asset store.
Code will be close to the following:
textScreen = "You have not enough of gold";
LeanTween.DelayedCall(5f,()=>{textScreen = "";});

JavaFX Canvas Update

I've been working on switching my applications from Swing to JavaFX. I've been working on a room escape game which displays a description of the item on which the user clicks. In Swing, I'd subclass JComponent and override the paintComponent(Graphics) method. I could draw the text there, knowing that the method is constantly called to update the screen. However, using the JavaFX Canvas, there is no method that is called constantly, which makes this task harder. I attempted save()ing the GraphicsContext after I drew the images and called restore() when I wanted to remove the text, but to no avail. Here's the important code:
package me.nrubin29.jescape;
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.shape.Rectangle;
import java.util.Timer;
import java.util.TimerTask;
public class RoomPane extends Canvas {
private Room room;
private Toast toast;
public RoomPane() {
super(640, 480);
setOnMouseClicked(e -> {
for (JObject o : room.getObjects()) {
if (o.getBounds().contains(e.getX(), e.getY())) {
toast = new Toast(o.getDescription());
}
}
});
new Timer().schedule(new TimerTask() {
#Override
public void run() {
if (toast == null) {
return;
}
if (toast.decrement()) { // Decrements the internal counter. If the count is 0, this method returns true.
toast = null;
Platform.runLater(() -> getGraphicsContext2D().restore());
}
else {
Platform.runLater(() -> getGraphicsContext2D().strokeText(toast.getText(), 300, 100));
}
}
}, 0, 1000);
}
public void changeRoom(Room room) {
this.room = room;
GraphicsContext g = getGraphicsContext2D();
g.drawImage(room.getBackground(), 0, 0);
for (JObject o : room.getObjects()) {
g.drawImage(o.getImage(), getCenterX(o.getBounds()), getCenterY(o.getBounds()));
}
g.save();
}
}
I attempted save()ing the GraphicsContext after I drew the images and called restore() when I wanted to remove the text, but to no avail.
save and restore have nothing to with removing things like text, what they do is save in a stack the state of various settings like a stroke or fill to use to draw shapes and allow them to be popped off the stack for application later. Those routines don't effect the pixels drawn on the canvas at all.
To remove something from a GraphicsContext, you can either draw over the of it, or clear it. For your code, what you could do is snapshot the canvas node where you are trying to save it, then draw your snapshot image onto the canvas where you are trying to restore it. It is probably not the most efficient way of handling drawing (a smarter routine which just draws only damaged area where the text is would be better, but probably not required for your simple game).
However, using the JavaFX Canvas, there is no method that is called constantly
Rather than using a timer to trigger canvas calls, use a AnimationTimer or a Timeline. The AnimationTimer has a callback method which is invoked every pulse (60 times a second, or as fast as JavaFX can render frames, whichever is the lesser), so it gives you an efficient hook into the JavaFX pulse based rendering system. The Timeline can have keyframes which are invoked at user specified durations and each keyframe can have an event handler callback which is invoked at that duration.
Using the built-in JavaFX animation framework, you don't have to worry about multi-threading issues and doing things like Platform.runLater which overly complicate your code and can easily lead to subtle and serious errors.
On a kind of unrelated note, for a simple game like this, IMO you are probably better off recoding it completely to use the JavaFX scene graph rather than a canvas. That way you will be working at a higher level of abstraction rather than clip areas and repainting damaged paint components.