UI on Android Game - What are my options? - android-canvas

First of all let me give a bit of context to make the question more precise:
I am developing an Android game (using SurfaceView, Canvas, etc), and it is working perfectly for the gameplay part. I initially tried to use View-derived elements on top of it for menus and other UI elements, and rapidly realized by experimentation and looking at some questions here on Stackoverflow that this was a really bad idea, since they dont mix well (say a LinearLayout on top of a SurfaceView).
I see 3 possible paths:
A) Continue using View elements on top of SurfaceView (and deal with the problems with it, such as horrendous lag)
B) Draw UI elements manually on SurfaceView/Canvas. Something like: canvas.drawBitmap(menuBitmap, posX, posY, ...); and then handle the touches manually, and suffer with screen fragmentation
C) Use a library/framework designed specifically for this that handles all the drawing of UI, touch on buttons, drag to scroll, etc. Something like the View and its derived elements, but designed for games and apps that draw using SurfaceView.
Are there more options that Im not seeing? And "C" seems the best to me, but is there a library for that? Which one?
Edit: forgot to ask the most obvious question, also: How does other professional/commercial games deal with this?
Thanks

I really don't like these game engines (AndEngine, Corona, Unity3D, cocos2d, etc). I want to learn Android, which will be more useful for my professional life than engine X or Y.
All my games were created without game engines. I use option (A) for almost everything and I don't see any lag or sluggishness.
Examples:
The game screen for Minesweeper 3D is a set of layouts and views plus a SurfaceView to hold the OpenGL 3D field.
The game screen for Box Topple is also a set of layouts and views together with a custom view created to handle and display the box2d physics engine.
See my other games as well...

I had these same exact issues.
Then I found a free game engine called AndEngine.
This engine handles just about everything you need.
For example:
OpenGL wrapper classes for simple, efficient drawing
Sprite classes (animated or not)
Particle effects
Cameras to view different parts of the scene you draw on
Scrolling backgrounds
Online multiplayer and Box2D physics engine extensions
etc.
And for your case:
It has a HUD that you attach to a camera, which stays static as the camera moves.
You can then attach buttons (already built-in, with click events) to this.
Here is some example code to create a button:
moveRightButton = new ButtonSprite(10, 10, moveRightButtonTexture, getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(TouchEvent event, float x, float y) {
if (event.isActionDown()) {
player.moveRight();
// Set to 'clicked' image
this.setCurrentTileIndex(1);
}
else if (event.isActionUp()) {
// Set to 'unclicked' image
this.setCurrentTileIndex(0);
}
return super.onAreaTouched(event, x, y);
};
};
You can find the engine and its extensions at https://github.com/nicolasgramlich?tab=repositories.
The main problem with the engine is that there is no documentation.
However, there is a forum dedicated to the engine at http://www.andengine.org/forums/.
Also, you can download AndEngine Examples (via the same repository I mentioned above), created by the developer to give examples of usages of various aspects of the engine.
It takes a bit to get used to, but it has been very rewarding.

Related

Would using cocos2d be easier for a drag and match game?

I am pretty new to making games, but I am pretty familiar with programing iOS. I am creating a shape matching game, so there would be an array of different shapes and the user would drag the shape to the correct corresponding shape if they get it right it would stay and if they get it wrong it would shoot back. Now my question is would that be easier using cocso2d or any game engine or would it be just as easy not using one, just using a touch event?
Since the game you are describing is not graphically intense - I would recommend using UIKit. Couple of reasons why I would use UIKit over cocos2d:
Interface builder / Storyboards are awesome. You can lay out your
screens and game elements on screen. (I know tools exist to do this
using cocos like CocosBuilder, but IMO they just don't compare to
working directly in XCode)
UIKit animations couldn't be easier and you can do some pretty powerful things with minimal code.
You have direct access elements such as UITableView, UICollectionView, UIScrollView, etc. There are cocos nodes that mimic these, but they don't match up in terms of response and behavior.
For more graphically intense games I would still use cocos2d hands down. Some scenarios when you would use it:
You have a large number of sprites with a large number of animations (opengl is fast)
You want to use opengl based effects like particles, lighting, etc.
You need a physics engine
You want to work off a prebuilt game engine (there are tons such as levelsvg, kobold2d, line starter kit, etc)
Hope this helps you.

How to make a custom trackball / eyeball control with Cocoa?

I'm writing my first Cocoa app and I would like to make a "trackball / eyeball / arcball / whatever it's called" button to rotate a 3D OpenGL scene.
There's a perfect example of this custom Cocoa control in Pages (Apple iWork suite) when you select a 3D chart. After some hacks, this control seems to be referenced as SFC3DRotateWidget. Here's a screenshot of the control in Pages.
Maybe this widget is reusable, but I didn't find how or where. So I try to recreate it.
I'm inexperienced with Cocoa so I'm not sure how to do that nor exactly where (i.e. what to do with Interface Builder, what to do with code...).
I'm not sure if I need to override the drawing function. I thought to use a textured button (Interface Builder) with a NSTrackingArea (code) to handle mouse events (move, drag, ...) but the area is necessarily rectangular. The interactive zones of the custom control used by Apple seem to follow the shape of the arrows. I've read on S.O. I can use NSBezierPath to create a more specific area (only via code?).
Does it sound good for you?
Do I miss something?
Let met know if you have any tips, tricks or resources you can share!
Thanks!
It sounds like you want to build a custom control. You do this by subclassing NSControl, which there is a guide on how to do. You can control the circular clickable area, and the responses to the mouse events by implementing the various methods. For example you can track mouse events with mouseDown: and the related methods.
You probably do not need to use any custom drawing code, NSImageView subviews with the various arrows will probably suite your purposes fine, unless you'd rather draw them in code.

Objective-C, Methods for animating gui

I've created many types of interfaces using the Cocoa API — some of them using documented basic animation techniques and others simply by experimenting (such as placing an animated .gif inside an NSImage class) — which had somewhat catastrophic consequences. The question I have is what is the correct or the most effective way to create an animated and dynamic GUI so that it runs optimally and properly?
The closest example I can think of that would use a similar type of animation would be something one might see done in flash on any number of interactive websites or interfaces. I'm sure flash can be used in a Cocoa app, although if there is a way to achieve a similar result without re-inventing the wheel, or having to use 3rd party SDKs, I would love to get some input. Keep in mind I'm not just thinking of animation for games, iOS, etc. — I'm most interested in an animated GUI for Mac OS X, and making it 'flow' as one might interact in it.
If u wish to add many graphics animations, then go for OpenGLES based xcode project for iOS. That helps u to reduce performance problem. You can render each of the frames in gif as 2D texture.
I would recommend that you take a look at Core Animation. It is Apples framework for hardware accelerated animations for both OS X and iOS. It's built for making animated GUIs.
You can animate the property changes for things like position, opacity, color, transforms etc and also animate gradients with CAGradientLayer and animate non-rectagunal shapes using CAShapeLayer and a lot of other things.
A good resource to get you started is the Core Animation Programming Guide.

Complex background images for Java2D applications

I am developing a (card) game and for the background I need something fancy. On my research I found Microsoft's Solitaire game with this really nice green background, which tries to imitate a card table. Actually it does this really well.
So my question is, how to replicate this in Java2D with minimum resources.
You can use a background image to accomplish this effect. You can draw it just like a shape using:
Graphics.drawImage(Image img, int x, int y, ImageObserver observer)
If you dont have the resources for this, concider using a texture that you can tile.

How should I design displaying a dynamic map? (Coordinates + Lines)

So I want to have a view (NSView, NSOpenGLView, something CG related?) which basically displays a map. Such as:
http://dump.tanaris4.com/map.png
Obviously that looks horrible, but I did it using an NSView, and it draws SO slow. Clearly not designed for this.
I just need to allow users to click on the individual (x,y) coordinates to make changes, and zoom into a certain area (to see it better).
Should I go the OpenGL route? And if so - any suggestions as to how to get started? (I was able to follow the guide to draw a triangle, so that's good).
I did find this post on zooming in an NSView: How to implement zoom/scale in a Cocoa AppKit-application
My concern is if I'm drawing over 6000 coordinates and the lines connecting them, this isn't efficient at all.
I don't think using OpenGL would be of any good here. The problem does not seem to be the actual painting, but rather the rendering strategy. You would need a scene graph of some kind to dynamically handle level of detail and culling.
Qt has all this packaged in a nice class class QGraphicsScene (see http://doc.qt.nokia.com/latest/qgraphicsscene.html for reference, and http://doc.qt.nokia.com/main-snapshot/demos-chip.html for an example).
Some basic concepts you should consider using:
http://en.wikipedia.org/wiki/Scene_graph
http://en.wikipedia.org/wiki/Quadtree
http://en.wikipedia.org/wiki/Level_of_detail
Try using core graphics for this, really there is so much that could be done. Watch the video Practical Drawing for iOS Developers from WWDC 2011 and it should give an over view of what can be done with CG.
I believe even CoreGraphics will suffice for what you want to achieve, and that should work under a UIView if you draw the rectangle of your view completely under the DrawRect method of your UIView (you must overload this method). Please see the UIView Class Reference. I have a mobile application that logs points on the UIMapKit, kind of like Nike+, and it certainly works well for massive amounts of points/line segments. There is no reason why this simple approach cannot work for you as well.