Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich way is better for taking User Input in an OpenGL Android Game?
    text
    copied!<p>I am developing an OpenGL Android Game and I am right now dealing with the User Input.</p> <p>I have two main ideas for implementing it. The two of them I think that they work perfectly but I do not know which one is "better" (in performance/speed).</p> <p>The basics are that there are several "buttons" drawn in Opengl (such as four arrow keys, action button...) on some specific positions around the screen. These buttons do not change of place during gameplay.</p> <p><strong>FIRST IDEA</strong></p> <p>My first implementation was to create a 2D-matrix with references to each button. Something like: </p> <pre><code>InputObject matrix = new InputObject[Width_of_screen][Height_of_screen] </code></pre> <p>At the loading stage each button inserts in the Matrix its references for each pixel where they appear.</p> <p>So each time the user touches the screen I can look directly on the matrix which button he has clicked (with <code>e.getX()</code> and <code>e.getY()</code>).</p> <p><strong>Pros:</strong> "Fast" to know which button to call.</p> <p><strong>Cons:</strong> (At common screens) 800*480 ~ 300K references, with 80% of them being <code>null</code> (But memory heap already taken)</p> <p><strong>SECOND IDEA</strong></p> <p>My second plan is to make an <code>ArrayList</code> of the Input Objects and ask each one if its theirs. Something like:</p> <pre><code>for(InputObject ob : TheArrayList){ if (ob.for_him(e.getX(),e.getY())){ ob.do_it(); break; //They cannot overlap } } </code></pre> <p><strong>Pros:</strong> Less heap space taken (no <code>null</code> references at all...)</p> <p><strong>Cons:</strong> Have to detect for each object if its for itself, so they need to do comparisons on <code>for_him</code> method. Taking more CPU time.</p> <p>With these said, which one may be the best idea for Android Phones (and other Smartphones) due to having not much processor time for user input.</p> <p>If there is a third and best approach please I would love to know about it.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload