#29530 - ThUg4LiFe - Sun Nov 21, 2004 4:41 pm
this doesnt necessarily concern a gba development, but im interested to know how you can limit the amount of input a program will accept. scanf() and gets() commands will keep taking input (even if the array is not big enough to store it) and will output all entered characters onto the screen
#29532 - allenu - Sun Nov 21, 2004 5:15 pm
You can't limit input with those two. You can certainly truncate strings input that you feel are too long, but as for limiting input as the user is entering the string, you won't be able to. You'll have to write your own routine that does that or use a different input routine. I think fgets() lets you specify the maximum length to read in, but not the maximum length the user can type in.
#29533 - ThUg4LiFe - Sun Nov 21, 2004 5:20 pm
but (as far as my knowledge so far goes) once an input from keyboard function is called, you lose control of the program until the user hits enter. but it is nothing uncommon for a sophisticated program to have many occasions where input appearing on screen (or being accepted) is limited - possibly done by your "own routine" as you point out - but how can you implement your own routine if (in my words lol) 'you lose control of the program until the user hits enter'
#29534 - identitycrisisuk - Sun Nov 21, 2004 5:32 pm
ThUg4LiFe wrote: |
but (as far as my knowledge so far goes) once an input from keyboard function is called, you lose control of the program until the user hits enter. |
When using functions like scanf yes but that's not always the case. Take something like GetAsyncKeystate (I think that's the right name, not 100% sure) which you use to determine the state of individual keys. Not actually a good example here, but I know that it is possible to read the state of the keyboard from one frame to another. Also I think you can get this in a buffered format incase there were any key up/down changes between tests, and this will give you the exact order keys were pressed in. It's then fairly simple for you to manage a size limited string and make a decision each frame about whether you can put more letters into that string. An example might be the name entry section in a game, so that it stays inside a box that you define. Not really done any of this myself as I've never really needed to.
#29535 - allenu - Sun Nov 21, 2004 5:33 pm
Well, basically, you'll have to write your own routines that interface with the keyboard directly. The thing is, calls like scanf() and gets() are making use of the IO streams that are provided by the operating system. The OS takes care of handling keyboard input and sending it to your application. If you want more control over how the input is handled from the keyboard, you'll have to write code to talk to it yourself.
#29536 - ThUg4LiFe - Sun Nov 21, 2004 5:37 pm
identitycrisisuk wrote: |
ThUg4LiFe wrote: | but (as far as my knowledge so far goes) once an input from keyboard function is called, you lose control of the program until the user hits enter. |
When using functions like scanf yes but that's not always the case. Take something like GetAsyncKeystate (I think that's the right name, not 100% sure) which you use to determine the state of individual keys. Not actually a good example here, but I know that it is possible to read the state of the keyboard from one frame to another. Also I think you can get this in a buffered format incase there were any key up/down changes between tests, and this will give you the exact order keys were pressed in. It's then fairly simple for you to manage a size limited string and make a decision each frame about whether you can put more letters into that string. An example might be the name entry section in a game, so that it stays inside a box that you define. Not really done any of this myself as I've never really needed to. |
kool that sounds great... i think this is where the problem for me with learning C will come in - i want to be able to program to the best of my ability and therefore i need to know all the codes and functions available and how to use them and if i need to include header files and so on.... its one thing learning to program with the code, but there isnt something that could show you "everything" and i dont know where to look for learning about things like this (which arent covered by books to learn C)
still, im sure people from the forums will be able to help and advise in the right direction of where to look and where to learn all the different things
#29537 - ThUg4LiFe - Sun Nov 21, 2004 5:38 pm
allenu wrote: |
Well, basically, you'll have to write your own routines that interface with the keyboard directly. The thing is, calls like scanf() and gets() are making use of the IO streams that are provided by the operating system. The OS takes care of handling keyboard input and sending it to your application. If you want more control over how the input is handled from the keyboard, you'll have to write code to talk to it yourself. |
where can you learn about writing code to do that
#29540 - allenu - Sun Nov 21, 2004 5:52 pm
ThUg4LiFe wrote: |
where can you learn about writing code to do that |
Er, I don't know. Do what I did when I tried learning this, read up on keyboard handling (do a search) and sit down and think about how you want your input system is going to work.
I just realized you can use the getc() function to get one character at a time. You'll also have to handle the backspace character to allow the user to delete input characters. Then have a count to see if the user has typed in more characters than you want. When you get to that stage, don't let the character's typed input affect the string.
#29542 - ThUg4LiFe - Sun Nov 21, 2004 6:11 pm
thanx for the info
i remember when i was a kid and was programming with BASIC on a Amstrad... (and when i was a young teenager and used QBASIC) i used the simple code "INKEY$" which used to take $ (string) INput from the KEYboard. but didnt necessarily output it on the screen - which was good
one way to use it was to make the computer freeze till a certain key or any was depressed (without pressing enter afterwards).. think this was it:
630 IF INKEY$ = "" THEN GOTO 630 ELSE
640 (next line of code)
but to be honest, seeing as i havent used it since i was a kid i cant remember a lot of the code, or how you made the input actually be used, or be output on the screen
not that any of that has anything to do with C..
#29544 - poslundc - Sun Nov 21, 2004 7:13 pm
Here's the thing: functions like printf and scanf belong to a special library of functions called the ANSI standard library. They are made cross-platform compatible so that you can compile your code for any computer that supports the ANSI standard. If you compile it for a Macintosh it will run in a terminal window. If you compile it for Windows it will probaby run in a DOS-emulation window or something. If you compile it for the Apple II it would run in the Apple's native text mode, etc.
The idea behind them is that they are convenient for providing basic functionality to applications: they give you the ingredients to an interface that you need in order to get input and output going and not care what platform you are running on. Beyond that, they are very abstracted from any single operating system and not designed to give you access to anything fancy interface-wise.
If you want to do anything sophisticated with the computer's interface you need to look at the specific features of and libraries for the operating system you are programming for and use them. The ANSI standard library has a very specific way of handling buffers and keypresses that aren't useful for interface-driven applications.
Dan.
#29547 - ThUg4LiFe - Sun Nov 21, 2004 7:22 pm
obviously i want to program with C for the GBA and test with a good GBA emulator in windows
my main reasons for asking about the input thing was i) the thing identitycrisisuk mentioned - to prevent the direct output on the screen appearing longer than it should; and ii) when you are taking character input, it needs to be stored, right?.. but if the desegnated array is not of sufficient size, other data can be overwritten - and i was interested in a way to prevent this altogether (as programs with good coding would)
before the end of the year i should have finished reading the last half of a 600 page book about C, and then i will be ready to learn about how to program with graphics and how to specifically program for the GBA (which im anticipating being more awkward to learn than the basic C skills im learning).. but maybe firstly practice programming for windows
#29553 - tepples - Sun Nov 21, 2004 9:08 pm
If you want to get C console style output (puts(), printf(), etc) on GBA, use something like AGBTTY, using a wrapper for _write() that calls agbtty_write().
Another tip: Use the newlib-specific iprintf() and siprintf() instead of printf() and sprintf() so that you don't bring the floating-point emulation library into your ROM.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.