gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Determine Area of screen to touch with stylus?

#135548 - zaczac - Tue Jul 24, 2007 1:40 pm

Ive been searching for this a little while now, and its getting boring! I found an old archive of
Code:
touchPosition tp = touchReadXY();

if (tp.py >= 32 && tp.py < 160 &&     // stylus.y = [32, 159]

    tp.px >= 32 && tp.px < 224)     // stylus.x = [32, 223]
{

    // code

}


I cant seem to get this to work, i combined it with a PAlib example to load a gif, it works normally, i inputted this code and put the sprite loading part at //code. I was expecting the sprites to show up when i touched the given area on the screen? However the topic i found this in didnt seem to say it was working, do i have some dummy code? :P
Could someone please help me out a little? Im aiming to make a main menu, with touch screen 'areas' to go to the main game, level select etc.

#135569 - josath - Tue Jul 24, 2007 6:23 pm

a couple suggestions for debugging:

1. print out the touch values, so that you can see what they are, and make sure they match what you expect
2. try testing the load sprite code separately from the touch code, to make sure it works correctly.

#135579 - zaczac - Tue Jul 24, 2007 8:14 pm

Thanks for the debugging tips! Im sure they will be useful to me later, and it makes sense to print out the locations! Thanks for that

As for 2) I can load the sprite correctly without the touch code, when i inserted the touch code, it didnt, i tried changing the range to touch from 0 to 256 and it displayed the sprite...Wierd.. Without even touching the screen... Maybe my entire code is screwed up. Ill post back tommorow when i have the time, i think all im doing is just modifying a PAlib example, but maybe i've made one or two small errors. Thanks!

#135661 - zaczac - Wed Jul 25, 2007 9:58 am

Alright, i removed the touch code = displayed gif correctly...added it on and it doesnt load them no matter where i touch! Its a modded example of palib examples 'loadgif'

Code:
// Include any GIF file and output it on an 8bit or 16bit screen !

// Includes
#include <PA9.h>       // Include for PA_Lib

#include "Mollusk.h" // gif to include
#include "trans.h" // gif to include

// Function: main()
int main(int argc, char ** argv){

   PA_Init();    // PA Init...
   PA_InitVBL();   // VBL Init...
   
   PA_Init8bitBg(0, 3);    // Init a 16 bit Bg on screen 0
   PA_Init8bitBg(1, 3);    // Init a 8 bit Bg on screen 1   

   touchPosition tp = touchReadXY();

if (tp.py >= 3 && tp.py < 4 &&     // stylus.y = [32, 159]

    tp.px >= 3 && tp.px < 4)     // stylus.x = [32, 223]
{

   PA_LoadGif(   1, // Screen, which is 8 bit...
            (void*)Mollusk); // Gif File

   PA_LoadGifXY(   0, 100, 60, // Screen, which is 16 bit, and at position 100, 60
            (void*)trans); // Gif File
}


  while(1)  {
      PA_WaitForVBL();
   }
      
   return 0;
} // End of main()



Is there anything suspicious in there? Im thinking maybe that i need something in pa_waitforvbl so that it can continue to use the touch screen, but im a newb to DS programming. Thanks for the help

#135668 - memoni - Wed Jul 25, 2007 1:01 pm

In the code below you are testing super tiny rectangle and you do it only once!

Try something like the following code. I'm not familiar how the PAlib works, so it may or may not display the image. The structure of the program should be fine, though.

The program loops forever, and checks the position of the stylus every frame. If the stylus position is in the upper left quarter of the screen, the program checks if the images are loaded already. If they are not, the images are loaded and marked as loaded. If the images are loaded already, nothing happens.

Code:
// Function: main()
int main(int argc, char ** argv)
{
   PA_Init();    // PA Init...
   PA_InitVBL();   // VBL Init...
   
   PA_Init8bitBg(0, 3);    // Init a 16 bit Bg on screen 0
   PA_Init8bitBg(1, 3);    // Init a 8 bit Bg on screen 1   

   int loaded = 0;

   while (1)
   {
      // Frame start.

      touchPosition tp = touchReadXY();

      // If touched the upper left quarter of the screen
      if (tp.px >= 0 && tp.px < 128 && tp.py >= 0 && tp.py < 96)
      {
         // Check to load the images only once
         if (loaded == 0)
         {
            PA_LoadGif(   1, // Screen, which is 8 bit...
                     (void*)Mollusk); // Gif File

            PA_LoadGifXY(   0, 100, 60, // Screen, which is 16 bit, and at position 100, 60
                     (void*)trans); // Gif File

            loaded = 1;
         }
      }

      // Frame ends, wait for next frame.
      PA_WaitForVBL();
   }

   return 0;
} // End of main()

#135674 - zaczac - Wed Jul 25, 2007 2:43 pm

Thanks for the reply, I was eager to try it out, but it failed. I turned it on and it just loaded up the sprites regardless of me touching the screen. I'll have a mess about with the code and see if i can get it to work, and then post it again,
It annoys me when you find a post with the answer you need and someone says "NVM I GOTS IT!" :I

thanks for the help though! Especially since your not familiar with Palib! :P

#135788 - mml - Thu Jul 26, 2007 9:58 am

Try throwing in an extra call to touchReadXY somewhere before your main loop and just ignore the value. The first time it's called it might be getting junk back from some artifact of non-zeroed memory leftover from your cart's loader or something. I've had similar weirdness happen when reading the state of the X/Y buttons.

#135811 - LiraNuna - Thu Jul 26, 2007 3:37 pm

Code:
      // If touched the upper left quarter of the screen
      if (tp.px >= 0 && tp.px < 128 && tp.py >= 0 && tp.py < 96)
      {
         // Check to load the images only once
         if (loaded == 0)
         {
            PA_LoadGif(   1, // Screen, which is 8 bit...
                     (void*)Mollusk); // Gif File

            PA_LoadGifXY(   0, 100, 60, // Screen, which is 16 bit, and at position 100, 60
                     (void*)trans); // Gif File

            loaded = 1;
         }
      }


Just a note about this code - efficiency wise (sorry, I can't watch this kind of code without giving tips..), but I would advise you to change the way you run your ifs when they are nasted. since if(loaded==0) is likely to return false more then true (it changes it's value), and since your second if is rather heavy (has 4 checks), I would suggest:
Code:

bool isLoaded = false;

   // First check if we need to enter the long if
if (!isLoaded) {
      //Read touch input
   touchPosition tp = touchReadXY();
      
      // If touched the upper left quarter of the screen
   if (tp.px >= 0 && tp.px < 128 && tp.py >= 0 && tp.py < 96) {
      PA_DoStuffHere( /*PA_Args*/ );
         
      isLoaded= true;
   }
}


Also note that I read the touch input only if needed, to save processing time.

Now you elders can flame me about being a lame asshole when giving novice programmers some tips...
_________________
Private property.
Violators will be shot, survivors will be shot again.

#135812 - mastertop101 - Thu Jul 26, 2007 3:42 pm

Why don't you use Stylus.X and Stylus.Y if you're using palib?

#135814 - zaczac - Thu Jul 26, 2007 3:57 pm

Quote:
Why don't you use Stylus.X and Stylus.Y if you're using palib?


Because Im Stupid.I Didnt Realise That One! Thanks. Ill try it and post back if that did anything, then try LiraNuna's Idea. Thanks A Lot For the help guys.