Advertisement
ZoriaRPG

DrawPage.zs

Nov 10th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. namespace script strings
  2. {
  3. define H_SPACE = 10;
  4. define V_LINE_W = 180;
  5. define V_LINE_CHARS = V_LINE_W/9;
  6. define PAGE_H = 180;
  7. define MAX_LINES = PAGE_H/H_SPACE;
  8.     //10 px height,
  9.    
  10. define X = 16;
  11. define Y = 16;
  12. define LAYER = 6;
  13. define FONT = FONT_APPLEII;
  14. define FONT_COLOUR = 0x01;
  15. define FONT_BG_COLOUR = -1;
  16. define FONT_OPACITY = OP_OPAQUE;
  17.    
  18.  
  19. define CHAR_W = 8;
  20. define CHAR_H = 8;
  21. define CHAR_KERNING = 1;
  22. define LINE_KERNING = 2;
  23. define TAB_WIDTH = CHAR_W*4;
  24. void DrawPage(int buffer, int dest) //font MUST be monospaced
  25. {
  26.     Screen->SetRenderTarget(dest);
  27.     int x; int y;
  28.     //Using DrawChars()
  29.     for ( int q = 0; buffer[bufferPos]; ++bufferPos )
  30.     {
  31.         if ( buffer[bufferPos] ==  CHAR_CR ) { y += CHAR_H+LINE_KERNING; x = X; }
  32.         if ( buffer[bufferPos] ==  CHAR_LF ) { y += CHAR_H+LINE_KERNING; x = X; }
  33.         if ( buffer[bufferPos] ==  CHAR_TAB ) { x += TAB_WIDTH; }
  34.         Screen->DrawCharacter(LAYER, X+x, Y+y. FONT, FONT_COLOUR, FONT_BG_COLOUR, -1, -1, buffer[bufferPos], FONT_OPACITY);
  35.         x+= CHAR_W+CHAR_KERNING;
  36.     }
  37. }
  38.  
  39. //call for every keypress, and check the rval
  40. int WriteBuffer(int buffer)
  41. {
  42.     int rval;
  43.     int linePos; //vertical:
  44.     int charPos; //the horizontal position. we increment this for every character that we press.
  45.     //This allows tracking with arrow keys.
  46.     //If we hit return, we reset this to 0.
  47.     int bufferEnd; //this tracks the full buffer size at all times.
  48.     if ( ReadKey(KEY_UP) )
  49.     {
  50.         //move back a number of chars equal to the width of one line, minus charPos.
  51.         rval = KEY_DOWN;  //We use this to know that if we are at the head of a drawn area, to reverse the drawn area by one line.
  52.     }
  53.     if ( ReadKey(KEY_UP) )
  54.     {
  55.        
  56.         //move forward a number of chars equal to the width of one line, minus charPos, or tot he END of the buffer.
  57.         rval = KEY_DOWN;  //We use this to know that if we are at the end of a drawn area, to advance the drawn area by one line.
  58.     }
  59.     if ( ReadKey(KEY_LEFT) )
  60.     {
  61.         //adjust bufferPos by -1;
  62.         if ( bufferPos > 0 ) --bufferPos;
  63.         //if we are at the top-left of a draw area, and there is more text above, we return rval to move the lines into view.
  64.         rval = KEY_LEFT;  
  65.     }
  66.     if ( ReadKey(KEY_RIGHT) )
  67.     {
  68.         //adjust bufferPos by +1;
  69.         if ( bufferPos < sizeof(buffer)-1 ) ++bufferPos;
  70.         //if we are at the bottom-right of a draw area, and there is more text below, we return rval to move the lines into view.
  71.         rval = KEY_RIGHT;  
  72.        
  73.     }
  74.     if ( ReadKey(KEY_ENTER) || ReadKey(KEY_ENTER_PAD) )
  75.     {
  76.         buffer[bufferPos] = CHAR_LF;
  77.         ++bufferPos;
  78.         rval = CHAR_LF; //if we are on the lowest line, we use this to pan up one line whwn creating the new line.
  79.     }
  80.     if ( ReadKey(KEY_TAB) )
  81.     {
  82.         buffer[bufferPos] = CHAR_TAB;
  83.         ++bufferPos;
  84.     }
  85.     if ( ReadKey(KEY_BACKSPACE) )
  86.     {
  87.         //shift all chars in the buffer to the lef tone position?
  88.         //that could be horribly slow. Perhaps we insert a blank char, and skip over it when processing?
  89.         //that seems as if it would be far more efficient.
  90.         //When processinmg, if we encounter that char, we decrement the position in the dest buffers, and
  91.         //then we continue.
  92.         rval = CHAR_BACKSPACE;
  93.         //Again, if we are on the left edge of a line, and at the top line drawn, and there is text above, we pan up.
  94.     }
  95.     if ( ReadKey(KEY_DEL) )
  96.     {
  97.         //The logic for this one is more complex, and I may skip it for now.
  98.     }
  99.    
  100.    
  101.    
  102. }
  103.  
  104. void DrawPage(int buffer, int dest, int y) //font MUST be monospaced; specify the BASE y value. This allows paging up and down.
  105. {
  106.     Screen->SetRenderTarget(dest);
  107.     int x;
  108.     //Using DrawChars()
  109.     for ( int q = 0; buffer[bufferPos]; ++bufferPos )
  110.     {
  111.         if ( buffer[bufferPos] ==  CHAR_CR ) { y += CHAR_H+LINE_KERNING; x = X; }
  112.         if ( buffer[bufferPos] ==  CHAR_LF ) { y += CHAR_H+LINE_KERNING; x = X; }
  113.         if ( buffer[bufferPos] ==  CHAR_TAB ) { x += TAB_WIDTH; }
  114.         Screen->DrawCharacter(LAYER, X+x, Y+y. FONT, FONT_COLOUR, FONT_BG_COLOUR, -1, -1, buffer[bufferPos], FONT_OPACITY);
  115.         x+= CHAR_W+CHAR_KERNING;
  116.     }
  117. }
  118.  
  119.  
  120.    
  121. void DrawPage(int buffer)
  122. {
  123.     int bufferPos;
  124.     int x; int y;
  125.     do
  126.     {
  127.         int tempPos;
  128.         int temp[V_LINE_CHARS];
  129.         for ( ; ( buffer[bufferPos] != CHAR_CR || buffer[bufferPos] != CHAR_LF || buffer[bufferPos] != NULL ); ++bufferPos ) //until the position == NULL
  130.         {
  131.             //copy chars to the buffer until we hit a CR
  132.             temp[tempPos] = buffer[bufferPos];
  133.             ++tempPos;
  134.         }
  135.        
  136.         //draw it
  137.         Screen->DrawString(LAYER, x, y, FONT, FONT_COLOUR, FONT_BG_COLOUR, 0, temp, FONT_OPACITY);
  138.        
  139.         //increase Y pos
  140.         y += H_SPACE;
  141.        
  142.         //resume with new string.
  143.     }while(buffer[bufferPos]);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement