Fuzziness in drawn frames (using canvas) - android

I'm hobby-writing a 2d game & I stumbled upon a certain problem.
My game is tile based (about 12x9 tiles) generally everything is fine, but from time to time when the character moves the frame becomes fuzzy and black segments appear.
My background is tile-based and I think it is caused by misplacing them.
In my opinion, it is happening because I have an update thread and a drawing thread that both work with the same variables, example:
private int x, y;
public void update() {x++; y += 2;}
public void draw(Canvas canvas) {canvas.drawBitmap(someImage, x, y);}
(Note: I just made this up so it will be a bit easier to understand what I am talking about.)
And the result is that there are (black) gaps between some of the tiles that are updated while drawn.
If I really am right, I guess I need to solve the critical-section problem.
So I tried just mutexing the data by syncing the threads and making them work in a serial way.
The result was really sad: it was REALLY slow on my galaxy S, absolutely unplayable!
My other idea (which I really don't wanna do) is to add a "drawAction" class to every drawable object in the game that will save a "snapshot" of it's object and only this collection of "snapshots" will be mutexed and so I can let the drawing thread do his work without being interrupted by the update thread.
Please share with me your ideas.
I hope you have better solutions or if I'm wrong about the problem, maybe you can help me gat to the source.
Edit:
My game generally works fine, but on slow devices (like the emulator) the interference is more visible.
When the player character doesn't move it is drown fine and almost always while walking.
I prefer to use only my code so I wold like to solve this problem and not just do a work around like using some engine or framework.

This doesn't directly answer your question, but it should help all the same. Consider using the game engine AndEngine. For 2D tile based games it is stupid smooth and really easy to learn.

The problem: I got a "writing" thread and a "reading" thread who used the dame source/destination.
I solved my problem by creating a "snapshot" or a "shadow" (technicly a copy..) of my mutual data right before the reading and hold the writing thriead while doing so, thus some of the read data was "old" but right to the same date with the rest.
The black things I saw were the differance in drawing (x, y) coused by updating the data while drawing it.

Related

How to draw cleverly a complex shape in Android

I'm working on a university project in which I need to visualize on a smartphone datas from pressure sensors built in an insole.
I need to draw on a View, as a background, a footprint, something like the image below but just for one foot.
I don't want to use a static image because with different screens resolution it could lose too much quality, so I'm trying to do it by code.
The main problem is that I'm not very skilled in graphic programming so I have not a smart approach to this problem.
The first and only idea that I had was to take the insole's CAD representation, with its real dimensions, scale it in function of screen's ones and put it together using simple shapes (arc, circle, ecc...) available in Android.
In this way I'm creating a Path which will compose the whole footprint, once I will draw it with a Canvas.
This method will let me to do the work but is awful and needs an exceptional amount of work and time to set every part.
I have searched for some similar questions but I haven't found anything to solve my problem.
Is there (of course there is) a smarter way to do this stuff, saving time and energies?
Thank you
of course, you can always use open gl es
this method might not save your time and energy but will give you a better graphic and scope to improve it for later purpose
the concept is to create a float buffer with all the points of your footwear and then draw is with connected lines on a surfaceView
to get started with open gl es you can use this tutorial : https://developer.android.com/guide/topics/graphics/opengl.html
and here is an example : https://developer.android.com/training/graphics/opengl/index.html

Android 2D animation on characters

I have created a character and I wonder how I have to animate it regarding the fact that :
It has to move when on static position, I mean for example eyes blinking, mouth closing.
It has to move on the screen regarding to player gesture.
It has to be involved in collision.
I read about, AnimationDrawable, SurfaceView, Canvas ... but which way is the best ?
Thank very much !!!
If you're trying to make a game you should consider GLSurfaceView with textures.
This is a pretty good tutorial, though it contains a few coding errors, it makes the concept very clear!
The OpenGLES method would give much much more freedom, than animating Views, but maybe harder to master.

How to make a consistant game loop

So in my game my View gets drawn an inconsistent rates. Which in turn makes it glitchy. Ive been running into alot of problems with the invalidate(); meathod. Any simple ideas- Everywhere i look I get thrown up on by tons of intense code.
You haven't provided us with much information, specifically code.
A few things you could do are:
Set the initial frame rate to the lowest value you observe your application runs at, i.e., if currently set to 1/60, but the frame rate continuously dips to 1/30, set to 1/30 etc.
Rework your drawing calls to be more efficient.
Try to combine multiple transformations into a single transformation by multiplying matrices, i.e. if you need to scale, translate, and rotate, multiply those three matrices together and apply that single transformation to the vertices instead of applying three separate transformations.
Try not to iterate through entire lists/arrays if unnecessary.
Attempt to use the lowest level / most primitive structure possible for anything you have to process in the loop to avoid the overhead of unboxing.
[edit on 2012-08-27]
helpful link for fixing you timestep: http://gafferongames.com/game-physics/fix-your-timestep/
It sounds like your game loop doesn't take into account the actual time that has passed between iterations.
The problem is the assumption that there is a fixed amount of time between loop iterations. But this time can be variable depending on the number of objects in the scene, other processes on the computer, or even the computer itself.
This is a common, somewhat subtle, mistake in game programming, but it can easily be remedied. The trick is to store the time at the end of each draw loop and then take the difference of the last update with the current time at the start. Then you should scale all animations and game changes based on the actual elapsed time.
I've wrote more about this on my blog a while back here: http://laststop.spaceislimited.org/2008/05/17/programming-a-pong-clone-in-c-and-opengl-part-i/
Part II specifically covers this issue:
http://laststop.spaceislimited.org/2008/06/02/programming-pong-in-c-and-opengl-part-ii/

Strange artifacts when rendering lots of bitmaps

I'm writing a simple 2D game engine and rendering various objects to a locked SurfaceVew by calling Canvas.drawBitmap (all the same source bitmap but generally not with 1:1 source to destination scaling).
Everything appears to work as expected until there are a lot of calls per frame to drawBitmap. When this happens I occasionally see a stall (100mS or so) accompanied by an effect that looks as if several frames of rendering have occurred together, i.e. some objects will be drawn twice at two screen locations or a pair of objects moving at the same speed will appear to momentarily get closer together or further apart.
The application is structured as follows (simplified for the sake of example);
initialiseGameObjects();
while(quit==false)
{
processGameLogic(); // update object positions
Canvas c = surface_holder.lockCanvas();
if (c != null) {
drawGameObjects(c); // draw all objects
surface_holder.unlockCanvasAndPost(c);
}
}
As I understand it, the Canvas is a holder for draw requests which get executed when the Post is issued. Presumably there is a limit to the number of requests it can hold and I'm wondering if I'm exceeding this limit (I'm drawing up to a hundred small bitmaps depending on what's on screen at any one time) and inadvertently provoking some kind of flush and upsetting the double buffering in some way, although I've not managed to find any documentation to confirm or disprove this.
Does anyone have any idea what might be going on?
Regards,
Steve
You need to sychronize calls to canvas drawing. Check this example of a game driving thread: OnDraw() is not fired, nothing is drawn in surfaceView - Android
EDIT:
However I think it may be related to this problem:
Android SurfaceView/Canvas flickering after trying to clear it

The proper way to create a 2d android game

After getting help with starting android pong, I realized that I painted myself into a corner.
For reference on the code(Its long) and advice, here is a link to the last thread Updating player score in an android game
Basically the frame rate is halved when i touch the screen. I think it is due to updating everything or drawing on the canvas. This also lends itself to bad communication between the pieces of the application(the view, frame, and oncreate). Is there a recommended way to create an android 2d game? This is based off the lunar landing game and it seemed fine but when i tried to recreate it , it seemed much worse. I could not find a lot of resource on a best practice for drawing and so far this seems a bit discouraging.
So in short
How would you modify the code posted in the link to improve performance? Or did I paint myself into a poorly made corner, and it is time to start a new? If so , how?
Thanks
You don't need invalidate() in onTouch because you are already running a frame animation by the additional thread.
Try to define all the fixed values/variables in the constructor of the SurfaceView or in onSizeChanged() where you know the size of the canvas and bitmaps loaded, so not to make any unneccesary variable creation, requests or calcualtions in onDraw, onTouch or other methods to handle game logic, otherwise it will slow down.

Categories

Resources