Re: [cc65-devel] c64 - getting seemingly random chars on screen
cc65 - a freeware C compiler for 6502 based systems
Brought to you by:
gpz
|
From: <sje...@sh...> - 2021-03-04 03:11:07
|
You could also write your own, it's all part of the fun. 😉
I wrote this years ago using conio functions (and I see some things I'd fix now...)
/* text_input
*
* Get some text input from the user. Pass in the max number of characters
* to be accepted, and a pointer to a string that is at least as big plus one
* for the null terminator to hold the string.
*
* Rudimentary editing is supported (backspace, ctrl-arrows) when a non-empty
* string is passed in.
*
* Returns the number of characters entered if everything went ok
* and the user pressed return, 0 if the user pressed Escape.
*/
unsigned char __fastcall__ text_input(unsigned char x, unsigned char y, char *text, unsigned char max)
{
register char c;
register unsigned char i;
//--- Initialize ---//
i = 0;
cursor(1);
cputsxy(x, y, text); // show text
gotoxy(x, y);
while (1) {
c = cgetc();
switch(c) {
case 27: // user pressed escape
text[0] = 27; // flag that user pressed escape
cursor(0); // since a blank string and escape
return(0); // both have a return value of zero
break;
case 155: // user pressed return
text[strlen(text)] = '\0';
cursor(0);
return(strlen(text));
break;
case 126: // user pressed backspace
if (i != 0) {
--i;
if (i == (strlen(text)-1))
text[i] = '\0';
else
text[i] = ' ';
cputcxy(x+i, y, ' ');
gotoxy(x+i, y);
}
break;
case 30: // user pressed left arrow
if (i != 0) {
--i;
gotoxy(x+i, y);
}
break;
case 31: // user pressed right arrow
if (i < strlen(text)) {
++i;
gotoxy(x+i, y);
}
default:
if (i == max) continue; // maxed out
if (isprint(c)) { // is a printable char
text[i] = c;
cputc(c);
++i;
}
break;
}
}
// end of function
}
-----Original Message-----
From: Dirk Jagdmann <do...@cu...>
Sent: March 3, 2021 4:24 PM
To: cc6...@li...
Subject: Re: [cc65-devel] c64 - getting seemingly random chars on screen
> (the highscore was getting saved in a few places it didn't need to), I
> guess I'll look into better ways of reading keyboard input. I have a
> vague memory of being told/reading/somehow learning, that scanf()
> wasn't considered 'safe' anyways.
>
you can start with the textInput() function in
https://github.com/doj/dracopy/blob/master/src/ops.c
--
---> Dirk Jagdmann
----> http://cubic.org/~doj
-----> http://llg.cubic.org
_______________________________________________________
cc6...@li...
https://lists.sourceforge.net/lists/listinfo/cc65-devel
|