LoginLogin

Touchscreen Graphic Button

Root / Programming Questions / [.]

SheridanThorpe75Created:
Curious about something....How can I have the program continue to do whatever it is doing( animation,etc) and at the same time keep checking for the button being used?

Use:
TOUCH OUT TT, TX, TY
to get the current stylus location on the touch screen. TX and TY are the stylus’ X and Y coordinates, while TT equals 0 if nothing is touching the touch screen, and 1 if something is. Then, in your main loop you should add something like:
IF TX < 50 && TX > 100 && TY < 50 && TY > 100 THEN...
Changing the numbers to fit a given box would check if the stylus is within the boundaries of said box (or button) that you have on the touch screen. I’m just assuming this is what you meant from the title of this thread, however.

Program structure is generally a main loop:
WHILE 1
 VSYNC 'this synchronizes your loop with the screen refresh, making sure your program is smooth
 'contents
WEND
with many pieces of a program inside. The main loop runs through its contents from top to bottom, each full iteration only providing enough for 1/60th of a second. So, to make a sprite go in a circle while some the A button is being pressed (you can replace this easily, and this is an example anyways)
SPSET 0,0
WHILE 1
 VSYNC
 SPOFS 200+SIN(MAINCNT/40)*W,120+COS(MAINCNT/40)*W
 INC W, (S-W)/3
 IF BUTTON() AND #A THEN S=40 ELSE S=0
WEND
 

So..if I get this correctly...I have to put such a while/wend loop around the whole of my program in order for the computer to keep tabs on whether I touch a spot corresponding to my graphical button area whilst the program is executing other code?

Well, you have to put your code in a loop, otherwise it'll just run once and then stop. What the program has to do is: - get input - do calculations - display output - input - calculations - output - etc.

If you look up game programming on wikipedia and scroll down to the section Game structure you will find the following
The central component of any game, from a programming standpoint, is the game loop. The game loop allows the game to run smoothly regardless of a user's input or lack thereof. Most traditional software programs respond to user input and do nothing without it. For example, a word processor formats words and text as a user types. If the user doesn't type anything, the word processor does nothing. Some functions may take a long time to complete, but all are initiated by a user telling the program to do something. Games, on the other hand, must continue to operate regardless of a user's input. The game loop allows this. A highly simplified game loop, in pseudocode, might look something like this : while( user doesn't exit ) check for user input run AI move enemies resolve collisions draw graphics play sounds end while The loop may be refined and modified as game development progresses, but most games are based on this basic idea.[15] Game loops differ depending on the platform they are developed for. For example, games written for DOS and many consoles can dominate and exploit available processing resources without restraint. However, games for a modern PC operating system such as Microsoft Windows must operate within the constraints of the process scheduler. Some modern games run multiple threads so that, for example, the computation of character AI can be decoupled from the generation of smooth motion within the game. This has the disadvantage of (slightly) increased overhead, but the game may run more smoothly and efficiently on hyper-threading or multicore processors and on multiprocessor platforms. With the computer industry's focus on CPUs with more cores that can execute more threads, this is becoming increasingly important. Consoles like the Xbox 360 and PlayStation 3 already have more than one core per processor, and execute more than one thread per core.
Now so I am not just quoting wikipedia, let's talk about buttons. If you were on Windows, your application would get a button click event and all you would have to do is respond to the event. We aren't on windows so we need to figure this out ourselves. If we were to describe a button what properties would it have? I would say: x,y position, width, height for size, a label, a function to call when clicked, and button state. Unfortunately SmileBasic doesn't have classes, structs or types, so if you want a bunch of buttons you may have to fake it with arrays. Button state may be something you wonder about. I would simply make it an enumeration of disabled, normal, and held down. If disabled you can't click it and the label is drawn in gray instead of black. Normal is the default state. Held down is for when the user has the stylus held down in the button area. Usually the border is drawn differently and the label is offset by a few pixels. The trick is you want to check for a tap in the button area, followed by a release while in the area. Basically if the button was pressed last frame, but isn't this frame and you are in the button area, then call the click function. If you just check for the stylus held down in the button area you can get dozens of clicks per second. You said you knew python, maybe some python game programming books will help. You can find Invent your Own Computer Games with Python and Making Games with Python & Pygame free to read online at inventwithpython.com

This helpful and I appreciate it. I have run into issues on other programming languages where a button event causes the program to wait until a state is determined and I wasnt sure if while/wend would work. Also I am familiar with the game loop concept, I'm just concerned with making so many button state checks global by including them in the main loop that it will significantly slow the program itself. Any SB programs out there that people would recommend that show this in action so I can study it?

The way BUTTON and all input functions work is, each frame (60 times per second) SmileBASIC will check the state of all the buttons, touch screen, etc. BUTTON, TOUCH, etc. just access the data that was collected during the previous frame, so they won't cause any slowdown.

I have upload a simple program to check the Basic-inputs (Buttons, Cycle Pad, Touchscreen). Use this key for learn.
Spoiler NRNDS4M3
This shoud be run on all systems and is Called "B_INPUT" I hope, i can help you.