Bug 584

Summary: Every several seconds, the whole screen will turn white for about 2 msec
Product: CS3343_Kunkun_Loves_English Reporter: xuezhwang2-c
Component: UtilitiesAssignee: shangpguo2
Status: RESOLVED FIXED    
Severity: blocker    
Priority: ---    
Version: 1.0   
Hardware: PC   
OS: Mac OS   

Description xuezhwang2-c 2021-11-20 11:46:29 HKT
Summary:

In every scene, i.e. login scene, rule scene, playing scene and ending scene, every several time interval (about 3 msec), the screen will turn white and suddenly go back to normal.

Steps to reproduce:

1.Enter the game, and you will observe the result in login scene.
2.Click the button "GRE", and you will observe the result in playing scene.
3.By randomly clicking any basketball and enter the wrong answer, you will be redirected to the result scene. You will observe the result in result scene.

Expected result:

The white interval should not be caught by humans' eyes. We should observe the scene and the animation moves smoothly.

Actual result:

The white interval is caught by humans' eyes. 

Possible bugs:

Since it is a bug in every scene, I don't expect this a particular problem in some low level class. Instead, it may be the problem in the GameApplication.java, which is used by every scene.
Comment 1 shangpguo2 2021-11-20 11:52:46 HKT
(In reply to xuezhwang2-c from comment #0)
> Summary:
> 
> In every scene, i.e. login scene, rule scene, playing scene and ending
> scene, every several time interval (about 3 msec), the screen will turn
> white and suddenly go back to normal.
> 
> Steps to reproduce:
> 
> 1.Enter the game, and you will observe the result in login scene.
> 2.Click the button "GRE", and you will observe the result in playing scene.
> 3.By randomly clicking any basketball and enter the wrong answer, you will
> be redirected to the result scene. You will observe the result in result
> scene.
> 
> Expected result:
> 
> The white interval should not be caught by humans' eyes. We should observe
> the scene and the animation moves smoothly.
> 
> Actual result:
> 
> The white interval is caught by humans' eyes. 
> 
> Possible bugs:
> 
> Since it is a bug in every scene, I don't expect this a particular problem
> in some low level class. Instead, it may be the problem in the
> GameApplication.java, which is used by every scene.

Hello, Xuezhen!

Yes! Your guess is correct! Thank you very much for helping to find the bug so quickly.

The problem is in the "game loop" in the GameApplication.java. Since we don't want our program to keep rerendering the same content on the page, even if we do not make any change. We have a sleep() function in the run loop:

	public void run() {
		running = true;
		timer.Reset();
		currScene.enter();
		while (running) {
			Thread.sleep(100);
			gameloop(timer);
		}
		terminate();
	}

Nevertheless, in the render:
	private void render(){	
		do {
			do {
				Graphics g = null;
				try {
					g = bs.getDrawGraphics();
					renderFrame(g);
					//calculateFrameRate(g);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (g != null) {
						g.dispose();
					}
				}
			} while (bs.contentsRestored());
			bs.show();
		} while (bs.contentsLost());
	}
We have the g.dispose() which will clear all the content in the page.

We then have the trouble, i.e., when sleeping in the game loop, there is nothing displayed on the user's screen, since we use g.dispose to clear all of them which causes the blank that you see.

I fix this bug by moving the g.sleep() to the render function, such that when the thread is sleeping, there is content on the screen.