libgdx screen flashing on desktop app - java

I am currently working on a simple loading screen for my libgdx game however, it has a problem. The loading screen works flawlessly on the android project but when it comes to the desktop version it is not working. Currently the loading screen should show "LOADING" -> "LOADING." -> "LOADING.." -> "LOADING..." , with 1 second intervals and then repeat. When I load the game on the desktop application, it shows "LOADING" (and it is flashing like crazy) and then proceeds with the periods like it is supposed to. The whole time everything is flashing like the refresh rate is to high or something, and it shows phantom periods in the background when they are not supposed to be there. Can someone tell me when this is happening on the desktop version and not the Android version? Also here is the code from the two functions of my "LoadingScreen implements Screen"
The render(float delta) function:
float updateTextTime = 0;
#Override
public void render(float delta) {
if (updateTextTime >= 1){ //check to see if it has been a second
if (periods == 0){
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //clears the buffer bit
}
textBatch.begin();
font.draw(textBatch,loading.substring(0,7+periods),(Gdx.graphics.getWidth()/2)-(textwidth/2),(Gdx.graphics.getHeight()/2)+(textheight/2));
if (periods < 3){
periods += 1;
}else{
periods = 0;
}
textBatch.end();
updateTextTime = 0;
}else{
updateTextTime += delta; //accumlate
}
}
and the show() function:
#Override
public void show() {
textBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/ArmyChalk.fnt"), Gdx.files.internal("fonts/ArmyChalk.png"),false);
if (game.AppTypeMine == ApplicationType.Android){
font.scale(2.0f);
}
textwidth = font.getBounds(loading).width;
textheight = font.getBounds(loading).height;
}
FINAL SOLUTION:
Changed render() function:
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
textBatch.begin();
font.draw(textBatch,loading.substring(0,7+periods),(Gdx.graphics.getWidth()/2)-(textwidth/2),(Gdx.graphics.getHeight()/2)+(textheight/2));
textBatch.end();
if (updateTextTime >= 0.5){ //check to see if it has been a second
if (periods < 3){
periods += 1;
}else{
periods = 0;
}
updateTextTime = 0;
}else{
updateTextTime += delta; //accumlate
}
}

You should always render something in the render callback.
You are probably seeing double-buffering problems (OpenGL cannot tell that you didn't draw anything into the current framebuffer, so it ends up rendering whatever was left in there).
Change your code so that you always invoke the font.draw call. I think you can just move the textBatch.begin(), font.draw(), and textBatch.end() calls outside the initial if/else block.
If you don't want render to be invoked quite so often, you can switch Libgdx to a non-continuous rendering mode (but you'll need to find a hook to kick the render calls back to life every second or so). See http://bitiotic.com/blog/2012/10/01/enabling-non-continuous-rendering-in-libgdx/.

Related

How to make the screen appear with mousePressed stay on the screen?

When I click screen, the Stop screen flashes on and then it goes back to the Walk screen. How do I get it to stay on the Stop screen until I click my mouse again?
boolean walk;
String textStop;
String textWalk;
float x; //positions
float y; //positions
void setup() {
size (400,500);
stop = false;
textStop = "STOP!";
textWalk = "WALK!";
}
void draw() {
background(0,255,0);
text(textWalk, x , y);
x = 150;
y = 250;
textSize(40);
}
void mousePressed() {
if (stop);{
background(255,0,0);
text(textStop, x , y);
}
} ```
Whenever you call background() you're clearing the screen.
Because this happens in draw() every frame is cleared and redrawn with textWalk (and textStop) only gets displayed when you click (for the short duration of the mousePressed() event).
You could use the walk variable for that. I see you use stop though it's not declared. They seem to be the opposite of each other in context so why not simpify and use just one. Sure you can code this if/else to toggle the value or you can simply use the !(logical not) operator:
void mousePressed() {
walk = !walk;
}
when walk is true render textWalk
otherwise (else) render textStop
something like:
if(walk){
text(textWalk, x , y);
}else{
text(textStop, x , y);
}
Here's a modified version of your code using the notes above:
boolean walk = true;
String textStop = "STOP!";
String textWalk = "WALK!";
float x = 150; //positions
float y = 250; //positions
void setup() {
size (400,500);
textSize(40);
}
void draw() {
if(walk){
background(0,255,0);
text(textWalk, x , y);
}else{
background(255,0,0);
text(textStop, x , y);
}
}
void mousePressed() {
walk = !walk;
}
(Notice I've moved a few lines: in general it's a good idea to initialise values that don't change once at the start of your program rather than continuously. In this basic example it doesn't really affect performance, but for more complex programs it's a good idea to keep track of what changes and what doesn't.
Also, it might help with other programs in the future to have a bit of separation between data that changes on events (e.g. walk on mouse pressed) and how that data gets rendered (e.g. using walk to render in draw())

Why does my drawing disappear from a canvas?

I perform some drawing on a canvas every 10 sec. Unfortunately it disappears before it will be redrawn, so we have 10 sec with a blank screen. I tired to save a canvas before drawing and restore it, did not help.
This bug was appeared after I had shifted a line canvas.drawPath(linePath, linePaint); from outside of a loop into the loop.
CODE:
private void drawLine(Canvas canvas) {
yStep = (yHeight) / fullChargeLevel;
xStep = (xWidth) / timeStampBarsQuantity;
boolean check = false;
float time;
float chrg;
while (batteryUsageHistory != null && batteryUsageHistory.moveToNext()) {
int charge = batteryUsageHistory.getInt(1);
int time_stamp = batteryUsageHistory.getInt(2);
if (charge < 1) {
if(check){
canvas.drawPath(linePath, linePaint); //This line I shifted into here
linePath.reset();
}
check = false;
continue;
}
time = xPos + time_stamp * xStep;
chrg = yPos - (charge * yStep);
if (!check) {
linePath.moveTo(time, chrg);
check = true;
continue;
}
linePath.lineTo(time, chrg);
}
//canvas.drawPath(linePath, linePaint); //This line I shifted from here
}
You should implement your drawing logic in onDraw by extending some View class:
protected void onDraw(Canvas canvas) {
// here goes your custom drawing logic
}
as stated in: https://developer.android.com/training/custom-views/custom-drawing.html
it is because android redraws the component when it needs to. It is always like that you need to implement the drawing method and the GUI framework will call this method when neccessary, it is not the opposite, that the GUI framework displays your painting, it is just calling your painting method when needed.
I just moved back that line canvas.drawPath(linePath, linePaint); I was previously moved. And it works!.

Android SurfaceView slows if no touch events occur

I am making a game and all is going well except for the game loop. I am using a SurfaceView and drawing 2D Sprites (Bitmaps). Currently the game is a ship moving through an asteroid field. The ship stays in the center of the screen and the phone is tilted in either direction to move the the asteroids (asteroids change location instead of player). New asteroids spawn as old ones fall off the screen creating an infinite asteroid field feeling.
Running on my Nexus 5, I noticed that after about 3 seconds, the asteroids moving down the screen became choppy, despite my game loop set to run at 60fps. Here was the code:
#Override
public void run() {
Canvas canvas;
long startTime;
long timeTaken;
long sleepTime;
while (running) {
canvas = null;
//Update and Draw
try {
startTime = System.nanoTime();
canvas = gameView.getHolder().lockCanvas();
//Update
gameModel.update(gameController.getPlayerShots(), gameController.getCurrentTilt());
//Game Over?
if (gameModel.isGameOver()) { running = false; }
//Draw
synchronized (gameView.getHolder()) { gameModel.drawToCanvas(canvas); }
}
finally {
if (canvas != null) {
gameView.getHolder().unlockCanvasAndPost(canvas);
}
}
//Sleep if needed
timeTaken = System.nanoTime() - startTime;
sleepTime = FRAME_TIME - timeTaken;
try {
if (sleepTime > 0) {
sleep(Math.abs((int)sleepTime/1000000), Math.abs((int)sleepTime % 1000000));
}
}
catch (InterruptedException e) {}
}
//Load menu after game over
if (gameModel.isGameOver()) {
gameController.launchContinueMenu();
}
}
I thought the problem may be that my model was doing the drawing to canvas and that I wasn't using my surfaceview's onDraw() method. Refactored to use onDraw() and same result as before. I printed out the sleep time of each frame and my thread was consistently sleeping for 5-10ms (16.667ms in a frame), so my nexus was not short on computing power.
I read on stackoverflow that I shouldn't use synchronize() on the view holder. Still no luck.
Then I read again on stackoverflow that using Thread.sleep() may be causing the issue because it is not always accurate. I did a major refactor as shown below.
#SuppressLint("WrongCall")
#Override
public void run() {
Canvas canvas;
while (running) {
canvas = null;
//Update and Draw
try {
canvas = gameView.getHolder().lockCanvas();
//Calc and smooth time
long currentTimeMS = SystemClock.uptimeMillis();
double currentDeltaTimeMS;
if (lastTimeMS > 0) {
currentDeltaTimeMS = (currentTimeMS - lastTimeMS);
}
else {
currentDeltaTimeMS = smoothedDeltaTimeMS; // just the first time
}
avgDeltaTimeMS = (currentDeltaTimeMS + avgDeltaTimeMS * (avgPeriod - 1)) / avgPeriod;
// Calc a better aproximation for smooth stepTime
smoothedDeltaTimeMS = smoothedDeltaTimeMS + (avgDeltaTimeMS - smoothedDeltaTimeMS) * smoothFactor;
lastTimeMS = currentTimeMS;
//Update
gameModel.update(smoothedDeltaTimeMS / 1000.0d, gameController.getCurrentTilt());
//Game Over?
if (gameModel.isGameOver()) { running = false; }
//Draw
// synchronized (gameView.getHolder()) {
// gameModel.drawToCanvas(canvas);
gameView.onDraw(canvas);
// }
}
//Release canvas
finally {
if (canvas != null) {
gameView.getHolder().unlockCanvasAndPost(canvas);
}
}
}
//Load menu after game over
if (gameModel.isGameOver()) {
gameController.launchContinueMenu();
}
}
This new approach should just draw as fast as possible using delta time to draw next frame instead of a set increment as before. Still no luck.
I changed my bitmap locations from Rect's to a new positions class I created that uses doubles instead of ints to simulate sub-pixel movement. Still no luck.
Recap of things I've tried:
Use onDraw() instead of gameModel drawing to canvas
Don't use synchronize() on the view's holder
Using time since last update to update next frame instead of advancing one frame each update() call.
Remove Thread.sleep()
Sub-pixel movement
After all this, I noticed that the game run fine while the phone was charging. 3 seconds after I unplug the phone, the game becomes choppy again. Then I noticed that if I tap the screen when the game becomes choppy, everything smoothes out again for another 3 seconds. I'm guessing that there is some kind of battery saving feature that is affecting my SurfaceView performance? What is going on here, and how can I disable it? This is quite maddening...
I tested out my first game loop again out of curiosity (1st code block) and tapped the screen while playing and everything ran smoothly. Man... all that work adding complexity for nothing. I guess it's a good learning experience. Any ideas to how I can keep my SurfaceView running at full speed? Help is greatly appreciated.
:)
Your observation is correct. In short, the power management in the N5 aggressively throttles the power down. If your finger is in contact with the screen, the clock speeds are increased to ensure that interactions are smooth. (For more, see my answer to this question.)
The best way to handle this is to recognize that you will need to drop frames sometimes. The "Record GL app" activity in Grafika uses a simple trick with Choreographer to do this. See also this article for some additional thoughts about game loops.
I think you should try to make your game more battery saving,darker the game(using background layout #000) more power for game ,
also you can use Intent flags like clear top to kill previous activity for saving battery , clearing ram(avoid out of memory)
but for your situation you can use wakelock

Processing -- moving image leaving trail

I'm working on trying to make an image move in processing, but the image is leaving a trail. The important part which is tripping me up is that I cannot declare the background in draw(), because I have other functions which place images. Here is the relevant code:
void setup()
{
size(752,500);
background = loadImage("prairie.jpg");
background(background);
noStroke();
animal = loadImage("squirrel.png");
bird = loadImage("bird.gif");
rock = loadImage("rock.png");
cloud = loadImage("cloud.png");
jeep = loadImage("jeep.png");
flower = loadImage("flower.png");
}
float jeepX = 752;
float jeepY = 250;
float size = 100;
void draw()
{
image(jeep,150,350,125,125);
image(jeep,jeepX,jeepY,size,size);
jeepX--;
jeepY = jeepY + .25;
size += .25;
image(jeep,jeepX + 1,jeepY - .25, size -.25, size - .25, 0,0,0,0);
if(jeepY > height)
{
jeepX = 752;
jeepY = 250;
size = 100;
}
}
This is for lab and the TA didn't know how, and I didn't have a chance to ask the professor yet.
If no one knows the answer and/or it has something to do with other functions (which place images), i'll post the relevant code.
For moving objects not to leave a trail, you must first clear the frame before redrawing the picture.(don't forget to reset the background if you don't have one)
As it is, it draws one jeep, then another on top of it.
If you don't want the trail, you got clear the background. If not completely, at least part of it, or redraw every image not supposed to move every frame.
Like this:
Sample Code
PImage bg, still, moving;
void setup() {
while ( bg == null) {// got wait as size depends on this...
println("loading bg image...");
bg = loadImage("http://dc489.4shared.com/img/f9EaWk5w/s3/13757197c08/Black_Background_Metal_Hole_-_.jpg");
}
size(bg.width, bg.height);
still = loadImage("http://www.renderosity.com/mod/bcs/photos/Thumb85619.jpg");
moving = loadImage("https://cdn1.iconfinder.com/data/icons/humano2/128x128/apps/alienblaster.png");
}
void draw() {
background(bg);
image(still, 100, 100);
image(moving, 200, frameCount%height);
}
You need to redraw your background in the 'draw' method. To do this, simply add the following line of code to your 'draw' method:
background(red,green,blue);
You can use the Colour Selector in Processing (found under Tools) to find the correct rgb code for the colour you want.
The reason for this is that the draw method is run 60 times a second, whereas the 'setup' method is only run once when the program is executed. As such, when you move the image, if the background colour is not in the 'draw' method, then it will not be redrawn when the image is moved, thus leaving a trail.

repaint() method in Java will not repaint my Rectangle after translation

I;m making a game in which I have to move little squares on an n by n grid, and they have to transition smoothly. This is the swap method I made which should be able to paint my transition on screen, but for some reason it is not doing it. I tried a much simpler version of my code on a simple project to move a Square back and forth and it worked like a charm, so I'm really not sure why this isn't repainting. This is just a bit of my code so if there's doubts about anything else on my code, ask away.
Thanks in advance. (:
public void swap( int y, int x ) {
long time = System.currentTimeMillis();
int counter = 0;
swapNum = tiles[y][x];
rect = (Rectangle) rectangles[y][x].clone();
while(counter < rect.height) {
if(System.currentTimeMillis() - time > 5) {
rect.translate(this.y-y, this.x-x);
time = System.currentTimeMillis();
counter++;
repaint();
}
}
swapNum = 0;
rect = new Rectangle();
int temporary = tiles[this.y][this.x];
tiles[this.y][this.x] = tiles[y][x];
tiles[y][x] = temporary;
this.x = x;
this.y = y;
}
If this block of code is running on the Event/Dispatch thread, which is used for drawing to the screen, then this will block the screen from updating.
Instead of doing the entire animation in one loop, consider designing an update method to do the animation, that will be called once every 15-30 milliseconds, and update the rectangle's position accordingly. Even better for smooth graphics is to draw to an image buffer and then have the actual draw method paint that buffer to the screen (double buffering).
Java3D has animations built-in, so it may be worth a look.

Categories

Resources