Advertisement
Shaun_B

Hello World! with scrolly text (if you like).

Jan 3rd, 2012
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | None | 0 0
  1. /** This does something similar to the classic
  2.  *  "Hello World!" code that's a popular starting
  3.  *  point for learning programming. It also adds
  4.  *  a scrolly text at the bottom of the screen.
  5.  *  For the Sinclair ZX Spectrum using z88dk
  6.  *  MMXII Donkeysoft. */
  7. #include <stdio.h>
  8. #include <input.h>
  9. //#include <spectrum.h>
  10. // Here are our default colour attributes
  11. #define INK     7
  12. #define PAP     2
  13. #define FLA     0
  14. #define BRI     1
  15. #define INV     0
  16. #define BOR     5
  17. // This is used in the setup function, which drops into
  18. // machine code - see my blog for more information:
  19. #define COL     128*FLA+64*BRI+8*PAP+INK
  20. // Forward declarations of functions:
  21. static void main(void);
  22. static void setup(void);
  23. static void hello(void);
  24. static void magazine(void);
  25. static void scroll(void);
  26. // Global variables:
  27. static int c=0;
  28. static int i=0;
  29. static int j=0;
  30. // Here is an array which will set up the default colour
  31. // attributes for printing our message to the screen,
  32. // 255 is used as a 'terminator' to say "we've finished here"
  33. static int screensetup[]=
  34. {
  35.     1,32,16,48+INK,17,48+PAP,18,48+FLA,19,BRI,20,48+INV,12,255
  36. };
  37. static int scrollsetup[]=
  38. {
  39.     1,64,16,48+INK-1,22,0,5,255
  40. };
  41. static char text[]="................ This is an example of a simple scrolly text in C           #"; // Hash used as terminator!
  42. static char buffer=' ';
  43. // This is the 'entry point' of our program:
  44. void main(void)
  45. {
  46.     // This will call a routine to set the default
  47.     // colour arrtibutes for the whole screen as
  48.     // defined above:
  49.     setup();
  50.     // This does the same for outputting out character
  51.     // see http://www.z88dk.org/wiki/doku.php?id=platform:zx
  52.     // for more information under the heading "The standard
  53.     // ZX Spectrum console driver" - hopefully, these numbers
  54.     // will now make more sense!
  55.     while(screensetup[i]!=255)
  56.     {
  57.         // The %c means 'print character code' or something similar
  58.         printk("%c",screensetup[i]);
  59.         // Increase i to read the next element of the array:
  60.         i=i+1;
  61.     }
  62.     i=0;
  63.     // Calls our functions, firstly hello:
  64.     hello();
  65.     // and now magazine:
  66.     magazine();
  67.     // This should set up our next lot of text as specified in the
  68.     // look-up table aka array called scrollsetup:
  69.     while(scrollsetup[i]!=255)
  70.     {
  71.         printk("%c",scrollsetup[i]);
  72.         i=i+1;
  73.     }
  74.     // This simply waits for a key to be pressed:
  75.     in_WaitForKey();
  76.     // This invokes an infinite loop:
  77.     while(1)
  78.     {
  79.         // Here is the time we'll wait in milliseconds:
  80.         in_Wait(48);
  81.         // This effectively moves the cursor or something:
  82.         printf("\x16\x37\x20");
  83.         printf(" ");
  84.         // Here's our loop, as we're going to print out 64 characters
  85.         // now instead of 32:
  86.         for(c=0;c<62;c=c+1)
  87.         {
  88.             // Okay, we want to print all of the chatacters in the
  89.             // scrolly text except for the hash as we're using that
  90.             // as a terminator to say 'end of string':
  91.             if(text[c]!="#")
  92.             {
  93.                 printf("%c",text[c]);
  94.             }
  95.         }
  96.         // This calls our scroll function:
  97.         scroll();
  98.     }
  99. }
  100. // This function sets up the default colours for our screen as
  101. // defined above:
  102. void setup(void)
  103. {
  104.     #asm
  105.     // Sets default ink and paper colour, then clears screen
  106.     ld a,COL
  107.     ld (23693),a
  108.     call 3503
  109.     // Sets border colour
  110.     ld a,BOR
  111.     call 8859
  112.     #endasm
  113. }
  114. void hello(void)
  115. {
  116.     // Here is where the magic happens, can you tell what it does?
  117.     printf("Hello ");
  118. }
  119. void magazine(void)
  120. {
  121.     // And what about this bad boy then?
  122.     printf("Magazine!\n");
  123. }
  124. void scroll(void)
  125. {
  126.     // Sets counter to zero
  127.     c=0;
  128.     // The buffer will preserve the first char otherwise the
  129.     // scrolling won't wrap around
  130.     buffer=text[c];
  131.     // Loop will happen whilst the terminator hasn't been reached:
  132.     while(text[c]!='#')
  133.     {
  134.         // Moves each character one element to the left:
  135.         text[c]=text[c+1];
  136.         // Increase our counter
  137.         c=c+1;
  138.     }
  139.     // Puts the old first character from the start to the end so
  140.     // it wraps around:
  141.     text[c-1]=buffer;
  142.     // Checks to see if the terminator has been over-written, if not
  143.     // then it's restored:
  144.     if(text[c]!='#')
  145.     {
  146.         text[c]='#';
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement