rilo

Textbox with Word Wrap for KickC

Jun 3rd, 2019
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. /*
  2.  
  3. Textbox routine with unsigned int wrap for KickC by Scan/Desire
  4.  
  5. Use KickC 0.8 or newer to compile
  6.  
  7. */
  8. #include "c64.h"
  9.  
  10. const char* screen = $0400;
  11. const char text[] = "this is a small test with word wrap, if a word is too long it moves it to the next line. isn't that supercalifragilisticexpialidocious? i think it's cool!@";
  12. const char text2[] = "textbox by scan of desire@";
  13.  
  14. void main() {
  15.     for (char x = 0; x < 15; x += 1) {
  16.         textbox(x,x,x+x+1,x+10,text2);
  17.         for (unsigned int wait = 0; wait < 15000; wait++) {}
  18.     }
  19.     textbox(30,8,39,24,text);
  20.     textbox(1,3,38,9,text);
  21.     textbox(0,12,20,24,text);
  22.     do {} while (true);
  23. }
  24.  
  25. void textbox(char x1, char y1, char x2, char y2, char* text) {
  26.     draw_window(x1, y1, x2, y2);
  27.     char y = y1+1;
  28.     char x = x1+1;
  29.     unsigned int z = y * 40;
  30.     unsigned int i = 0;
  31.     if (x == x2 || y == y2) {
  32.         // no room to draw text, simply return
  33.         return;
  34.     }
  35.     do {
  36.         char character = text[i];
  37.         screen[z+x] = character;
  38.         if (character == $20) {
  39.             // scan ahead to determine next unsigned int length
  40.             char c = 0;
  41.             unsigned int ls = i+1;
  42.             while (text[ls] != $20 && text[ls] != $00) {
  43.                 ls++;
  44.                 c++;
  45.             }
  46.             // if it's too big to fit but not bigger than the whole box width, move to next line
  47.             if (c+x >= x2 && c < x2-x1) {
  48.               x = x1;
  49.               y++;
  50.               if (y == y2) {
  51.                   // text too long for textbox
  52.                   return;
  53.               }
  54.               z = y * 40;
  55.             }
  56.         }
  57.         i++;
  58.         // only move x if the the character was not a space and not at the beginning of a box
  59.         if (character == $20 && x == x1+1) {} else {
  60.             x++;
  61.         }
  62.      
  63.         // this is in case the unsigned int is too long for one line and needs to be cut
  64.         if (x == x2) {
  65.             x = x1+1;
  66.             y++;
  67.             if (y == y2) {
  68.                 // text too long for textbox
  69.                 return;
  70.             }
  71.             z = y * 40;
  72.         }
  73.     } while (text[i] != 0);
  74. }
  75.  
  76. void draw_window(char x1, char y1, char x2, char y2) {
  77.     unsigned int z = y1 * 40;
  78.     unsigned int q = y2 * 40;
  79.  
  80.     // draw horizontal lines
  81.     for (char x = x1+1; x < x2; x++) {
  82.         screen[z+x] = 64;
  83.         screen[q+x] = 64;
  84.     }
  85.  
  86.     // draw upper corners
  87.     screen[z+x1] = 112;
  88.     screen[z+x2] = 110;
  89.  
  90.     // draw vertical lines
  91.     for (char y = y1+1; y < y2; y++) {
  92.         z = y * 40;
  93.         screen[z+x1] = 93;
  94.         screen[z+x2] = 93;
  95.     }
  96.  
  97.     // draw lower corners
  98.     screen[q+x1] = 109;
  99.     screen[q+x2] = 125;
  100.  
  101.     // window big enough to have an inside?
  102.     if (x2-x1 > 1 && y2-y1 > 1) {
  103.         // blank inside
  104.         for(char y = y1+1; y < y2; y++) {
  105.             z = y * 40;
  106.             for(char x = x1+1; x < x2; x++) {
  107.                 screen[z+x] = $20;
  108.            }
  109.         }
  110.     }
  111. }
Add Comment
Please, Sign In to add comment