Can we use #MagicConstant annotation in our code? - android-studio

When im developing Android Applications i come to realize that the usual is to avoid enums due to performance.
So i would like to know, can we use the #MagicConstant in our own code ?

Found what i was looking for Android Studio since 19.01 of the support library has the annotations library which is handy for catching errors
http://tools.android.com/tech-docs/support-annotations
The #IntDef was what i was looking for

Related

Using haxe as a transpiler between c++ and JavaScript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Firstly I apologise for the long post. I have been asking a few questions about haxe and its suitability as a tool to solve a problem I am facing. I have very limited knowledge of haxe but what I do know is my ideal role for haxe in my project is not the normal use of haxe. So what I'm trying to avoid is wasting time I don't have learning this language (despite it looking really fun and powerful) only to find that it isn't fit for purpose. So I am hoping to get some advice from you haxe seasoned vets on whether it is fit for purpose. I truly hope that it is.
Here is what I am trying to achieve:
I work in the games industry and I need to create a game in both C++ and JS. C++ for an embedded system and JS for online and mobile. I am very experienced in both C++ and JS.
The games I develop I only want to write the once. Now I could use something like emscripten for going from C++ to JS but this will result in compiled JS code that cant be debugged with chrome dev tools easily. I'm converned that I will run into edge case bugs with the compiled JS that I cant easily tace back to the original C++. Plus some platforms for the game would require fairness laboratories to view the source code which would be an issue for compiled JS.
What I really need is a source to source compiler that produces native and human readable c++ and JS code that I can then work with and modify in its native form if necessary, hence haxe. I've looked at the code produced by haxe for C++ and JS. JS looks perfectly easy to understand and work with. C++ not so much but still just about acceptable. I can even stop haxe from compiling and linking C++ which I dont need. I only want the source code.
So far so good.
Now I have a game framework in c++ that uses oxygine 2d engine. It is capable of showing sprites etc as well as a framework I have created for message buses and finite state machines (loads more useful classes too). I also have a similar framework in JS which uses Pixijs for its 2d engine and has its own message bus etc just like the C++ engine.
Now what I want to be able to do is write modules in haxe that when I transpile to both C++ and JS that the code can be included as part of the framework and work with its respected language engine. Each object I create in haxe will be very encapsulated and will just need to subscribe to the message bus, handle messages and send messages back. It may also need to know how to use its engines state machine. So I have no idea if this is even possible with haxe as the message bus (along with other objects) will not be written in haxe but will be supplied to the module after it has been transpiled and built in its native project. Maybe I can write my own haxe library that knows the syntax for my two game engines and can transpile depending on its target language? Not sure it that is possible.
Thanks for taking the time to read, and any advice you can give.
You can use
#if cpp
// c++ implementation
#elseif js
// javascript implementation
#end
to allow some different implementations for different targets, this can go anywhere in your code, so you can pass a Haxe value to different functions for different targets.
To use extern definitions:
http://haxe.org/manual/lf-externs.html
http://old.haxe.org/doc/js/extern_libraries
With c++ specifically it might be more complex you need to look at CFFI or Linc
https://snowkit.github.io/linc/
The complexity is probably getting the types across. Remember Haxe c++ has managed memory your engine might do things differently. Also HL is coming soon and may have some advantages.
Looking at oxygine2d it seems a bit like the Flash API? And I know pixijs is based roughly off the Flash API. I believe that OpenFL now uses pixijs for WebGL rendering. You might want to also look at NME ( has nearly same interface for C++ as OpenFL, but NME has stuff like Cppia setup and is sometimes more stable ). If the JS or C++ is too slow you need to look into shaders and Luxe/Kha for render and you might want to also try Heaps it uses Lime ( openfl ) and some haxe js webgl I think.
If you need 2D physics then use Nape it can be used with any of the Haxe toolkits (Luxe, OpenFL, Kha, Flambe), there is physaxe but that's not used as much. Kha and Nape info eg:
https://github.com/lewislepton/kha-examples/blob/master/NAPE/Sources/Project.hx
I really suspect that your making your life hard by wanting to use different engines for different platforms when probably HaxeFlixel or Punk could give you all you need running with Nape on one of the cross target toolkits, but it's very feasible to do it your way.
Maybe look at Tink or Thx for signals event buses and the like, macros (like used in Tink) allow a lot of structures to be built at compile and so can reduce much of the overhead by doing it before run-time, so tweens can be pre-calculated by the compiler. There is a hxcpp debugger and if you run Haxe in chrome it should give you the Haxe line number via js source mapping.
Hopefully I have covered some aspects of your large question.
Haxe allows you to mix and match with native, but I think to use Pixijs and oxygine 2d is probably not ideal as there is bound to be enough good game tools in pure Haxe with target optimizations built in, you just need to go onto Kha IRC, Luxe gitter, and Haxe IRC and OpenFL forum and ask a few questions.

Kotlin for game dev

Background:
I'm always searching for a language to replace Java for game development. Kotlin looks promising with a good IDE support and Java interop. But one of the FPS killers for a game (on Android especially) is GC usage. So, some libraries (like libgdx) are using pools of objects, custom collections and other tricks to avoid frequent GC run. For Java that can be done in a clear way. Some other JVM languages espesially with functional support using a lot of GC by it's nature, so it is hard to avoid.
Questions:
Does Kotlin creates any invisible GC overhead in comparison to Java?
Which features of Kotlin is better to avoid to have less GC work?
You can write Kotlin Code for the JVM which causes the same allocations than the Java corresponding logic. In both cases you have to carefully check if a library call allocates new memory on the heap, or not. Using Kotlin in combination with LibGDX doesn't introduce any invisible GC overhead. It's an effective way and works well (especially with the ktx extension.
But there are Kotlin language features which may help you to write your code with fewer allocations.
Singletons are a language feature. (Object declarations, companion object )
You can create wrapper classes for primitive types which compile to primitives. But you get the power of type safety and rich domain models (Inline classes).
With the combination of Operator overloading and Inline Functions you can build nice APIs which modify objects without allocating new ones. (Example: Allocation-free Vectorial operations using custom operators)
If you use any kind of dependency injection mechanism or object pooling to connect your game logic and reuse objects, then Reified type parameters may help to use it in a very elegant an short way. You can skip a class as type parameter, if the compiler knows the actual type.
But there is also another option which indeed gives you a different behavior in memory management. Thanks to Kotlin Multiplatform, you can write your game logic as Kotlin common module and cross compile it to native code or to Javascript.
I did this in a sample Game project Candy Crush Clone. It works with Korge a Modern Multiplatform Game Engine for Kotlin. The game runs on the JVM, as HTML web app and as Native binary in Win, Linux, Mac, Android or IOS.
The native compiled code has its own simpler garbage collection and can run faster. So the speed-increase and the different memory management may give you the power reserve to bother even less with the GC.
In conclusion I can recommend Kotlin for Game dev, also for GC critical scenarios. In my projects I tend to create more classes and allocate more memory when I write Kotlin code. But this is a question of programming style, not a technical one.
As a rule of thumb, Kotlin generates bytecode as close as possible to the one generated by Java. So, for example, if you use a function as a value, an inner class will be created, like in Java, but no more. There are also some optimization tricks like IntArray and inline to perform even better.
And as #Peter-Lawrey said, it's always a better idea to measure the values for your specific case.
Technically, your questions comparing Kotlin to Java are moot, they will perform the same. But Kotlin will be a better development experience.
If Java is good for writing Games, then Kotlin would only better due to developer productivity.
Note: the gaming library LWJGL 3 uses Kotlin in part, with GitHub stats showing 67.3% of the code being Kotlin (template module looks to be mostly Kotlin). So asking people who work with LWJGL will give you the best answer to this question since they have a lot of experience in this area.

how to target haskell to android using jhc?

I want to write a Haskell function (module) of type: String -> String to call in Android. The easiest method seems to use JHC to generate C code, then use Android NDK to generate a shared library, but I could not find any documentation for JHC. Does JHC also use Cabal to build? Is JHC stable enough to use Parsec or Attoparsec library?
Back in 2011 I had limited success using JHC in a similar way, but targeting iOS instead of Android. Initial results were good in just getting the thing running, but we ended up discarding JHC in favor of GHC precisely because we started getting weird compile-time errors on programs that used Parsec. Bear in mind, this was in 2011 so JHC might have improved by a lot since.
If you want to give GHC a chance, I'd recommend looking at this example which uses GHC 7.8 to compile a game for Android. I haven't used it in anger yet, but I did manage to get it working on Docker, getting as far as rebuilding the game from scratch and installing it on a real Android device, so the approach definitely has merit.
UPDATE as of August 2017: Moritz Angermann has posted detailed instructions on targeting Android with a GHC cross-compiler.
Well a compiler called eta maybe the most convenient way now.it targets jvm and it will produce a jar file so you can directly put it in your project

Why does iosched use getFragmentManager and not getSupportFragmentManager?

Why does the iosched app use getFragmentManager instead of getSupportFragmentManager? I was under the assumption that if you're using the support classes you should 'go all the way'. Can someone explain the reasoning behind this?
I'm trying to move my app to Material Design and assumed that even though I support 4.0 and up I would have to use the support fragments.
My assumption was wrong. You don't need to use support fragments, native fragments work just fine.

How do you make a call to a new Android Honeycomb sdk method without breaking backwards compatibility?

I am not worried about screen real estate. They have changed behavior and added new methods for tablet support. If I call one of these I assume that would cause a run-time failure for the older SDKs. Does this cause me to need a separate tablet version? This is the first time I have had to deal with this on Android and it wasn't clear to me what to do.
Documentation was incorrect and it worked. Don't know why I didn't think of reflection (doh!).
If those methods resides in some classes, which are not the part of pre-honeycomb SDK's you can try to use reflection to get (or not to in case of older devices) a honeycomb's class instance and call it's methods.
If you tell what methods/classes you are trying to use, i can try to make my answer more specific.

Resources