Micro Mart


Old Scrolls

 
Published in Micro Mart #1197

Here is the lost page from Shaun's C programming tutorials, which finished a few weeks ago

Retro: Old Scrolls

We're going back a few weeks now to Micro Mart issue 1195, which referred to a tutorial that was lost in the system somewhere. Well, dig out that issue and it will make sense as we've found the page again, so here it is.

From issue 1194, there was some information about loops and how the studio library changes between the default 32-column text and the ANSI 64-column mode, the latter halving the width of each character. Although some characters look a little squashed, it provides perfectly readable text in most cases.

We will take advantage of the 64 characters per line# and use it for a scrolly text routine, as this will give us half-character (4-bit) shifting without any additional coding. This week's example will set up a string array of characters, taking a copy of the first in the array (at position zero), shifting each one place to the left until the end of the message, and then putting the old leading character at the end, to ensure that none of the message is lost.

This isn't the only way to do a horizontally scrolling text display, but it's my preferred method. Note, however, that each change is permanent, so if you recall the routine, it'll continue the text from where it left off. Other methods don't do this. If you can think of another way of achieving the same thing without the permanent change to the data, let me know over on the Retro sub-forums at forum.micromart.co.uk. Here's the code:

	/* Scrolly.c */
	#include 
	#include 
	static void main (void);
	static void scroll (void);
	static char c = 0;
	static char i = 0;
	// Our scrolly text, the hash is used as a terminator
	static char text[ ] = "ro Mart magazine - Computing in the know.........Mic#";
	static char buffer = ' ';
	static char key = 0;
	void main ()
	{
		printf( "%c", 12);
		// Sets a condition to read the value of the variable key
		while( !key )
		{
			in_Wait(64);
			// Sets the cursor position, followed by eight spaces
			printf("\x16\x35\x20");
			// We'll only print out the first 48 characters of the text
			for (i=0;i<24;i=i+1)
			{
				// Stops the terminator bring printed
				if( text[i] != "#" )
				{
					printf("%c",text[i]);
				}
			}
			// Calls our scroll function to shift each character to the left
			scroll();
			// Reads keyboard, if pressed, the value is stored in the key variable
			key = in_Inkey();
		}
	}
	void scroll ()
	{
		c = 0;
		// Puts first character into the buffer
		buffer = text[c];
		while ( text[c] != '#' )
		{
			// Takes the next character and moves it to the current position of c
			text[c]=text[c+1];
			// Increase the counter
			c=c+1;
			// Repeats until the terminator is reached
		}
		// Writes the contents of the buffer to the end of the string, before the terminator
		text[c-1] = buffer;
	}

As you will see from loading the created .tap image into your emulator, the text will 'wrap around', and of course, you may change the message in the text array in the program to say anything you like. One interesting line is the while ( key ). What it's saying is while the variable key is not true. In C and C-derived languages, 0 is not true (or false) and 1 or more is true, so all the code in the paired braces after will repeat until key is true. The value of the variable key will change when you press a key on the keyboard, and this will stop everything and return you to BASIC.

Another thing to note is that there doesn't seem to be a way in some versions of the z88dk that allows easy placement of the cursor, like PRINT AT works in BASIC, so an explanation is on the forums of how it's placing the text two lines from the bottom of the screen. See you there.

Shaun Bebbington