Better view for games: View vs SurfaceView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Better view for games: View vs SurfaceView

Postby Donald_W » Tue Apr 07, 2009 3:43 pm

Hi,

Newbie question, which view is better for games: View or SurfaceView? I've examined examples from SDK (Snake & Lunar Lander) and got brainwashed a bit.

Another question is why do we have to synchronize by SurfaceHolder in the Lunar Lander? And isn't there an elegant way of separating view and game logics? :)

Thanks in advance,
Tomek
Donald_W
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 01, 2009 9:29 am
Location: Poland / Szczecin

Top

Postby andreleitao » Tue Apr 07, 2009 8:54 pm

Hi, Donald!
SurfaceView is more recommended for games than a simple View.
Answering the second question, we have to synchronize by SurfaceHolder to avoid inconsistent states. When you are touching the screen, for example, it is an asynchronous action. That is, no matter what your main game loop (or painting, or whatever) is doing, the touch event will be processed. Depending on the action you wrote for the touch event, it can change something (a value of an atribute, for example) while the update method (for example) is executing.

EX:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //********** update() is in execution ***********
  2.  
  3. var1 = 100;
  4.  
  5. //AT THIS MOMENT YOU TOUCHED THE SCREEN AND CHANGED var1 VALUE TO 500.
  6.  
  7. var3 = var1;
  8.  
  9. //Normally the var3 should have the value 100. But after touched it changed to 500 (it was not expected)--> //INCONSISTENCY!!!
Parsed in 0.029 seconds, using GeSHi 1.0.8.4




Synchronizing by SurfaceHolder you assure that nothing will be processed until the synchronized block code is over. For example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. synchronized (surfaceHolder) {
  3.  
  4.  
  5.  
  6.         update();                      
  7.  
  8.         draw();
  9.  
  10.                                
  11.  
  12. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


The update() and draw() methods will not be affected by any event while they are in execution.

I hope I was usefull. :D

Best Regards!
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

Postby Donald_W » Wed Apr 08, 2009 7:40 am

Thanks for explanation, It helped me a lot :)
Now I'll try to write a "view separated from game logics) template for a game (It don't feel that putting all logic stuff into view's thread is a good idea).
Donald_W
Freshman
Freshman
 
Posts: 5
Joined: Wed Apr 01, 2009 9:29 am
Location: Poland / Szczecin

Postby andreleitao » Wed Apr 08, 2009 1:25 pm

You're right. Put game's logic into view's thread is not a good idea. Try to separate them.

:D
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests