//---------------------------------------------------//
/*
 *Copyright Bryce Thorup, William Thorup, BitJag 2014
 *
 * Contact: contact@bitjag.com
 *
 *All content developed by BitJag (graphics & Code) is
 *for your personal use only.  Feel free to burn it to a CD, 
 *flash to a SkunkBoard, or use in an emulator.  We 
 *hope you also take the chance to learn from it if needed.
 * 
 *Do not redistribute, duplicate, charge for "handling" 
 *or cost to print, or charge for profit.
 * 
 *If there is someone you know who wants a copy,
 *physical or digital, please direct them to our website.
 *Any other questions about the use of the demo, please
 *shoot us an email.  We will be liberal, promise.
 *
 * http://bitjag.com/
 * 
 *This demo was developed with the Removers Library,
 *which is used under the LGPL licence 2.1, included
 *with the source.  The Removers library is free to 
 *download and use at this location.
 * 
 * http://removers.free.fr/softs/download.php?lang=en
 *
//---------------------------------------------------*/

#include "bj_scroll.h"

Scroll* newScroll(Font* font, char* text, int text_len){
  
  Scroll* s = malloc(sizeof(Scroll));
  
  s->font = font;
  
  s->text = text;
  
  s->text_len = text_len;
  
  s->rate = 1;
  
  screen* draw_box = new_screen();
  
  alloc_simple_screen(DEPTH16, 384, s->font->height, draw_box);
  
  clear_screen(draw_box);
  
  s->draw_box= draw_box;
  
  screen* copy_box = new_screen();
  
  alloc_simple_screen(DEPTH16, 384, s->font->height, copy_box);
  
  clear_screen(copy_box);
  
  s->copy_box= copy_box;
  
  s->move_box = sprite_of_screen(0, 0, draw_box);
  
  s->text_idx = 0;
  
  s->char_width = s->font->char_widths[s->text[0]] + s->font->kerning;
  
  s->char_offset = s->font->char_offsets[s->text[0]];
  
  s->loop_count = -1;
  
  s->loop_done = 0;
  
  return s;
  
}

void initScroll(Scroll* s){
  
  clear_screen(s->draw_box);
  
  clear_screen(s->copy_box);
  
  s->text_idx = 0;
  
  s->char_width = s->font->char_widths[s->text[0]] + s->font->kerning;
  
  s->char_offset = s->font->char_offsets[s->text[0]];
  
  s->loop_count = -1;
  
  s->loop_done = 0;
  
}

void updateScroll(Scroll* s){
  
  s->char_width--;
  
  s->draw_box->x = 1;
  
  fb2d_copy_straight(s->draw_box, s->copy_box,
                     383, s->font->height, MODE_S);
  
  s->draw_box->x = 0;
  
  fb2d_copy_straight(s->copy_box, s->draw_box,
                     383, s->font->height, MODE_S);
  
  if(s->char_width < 0){
    
    if(s->loop_count != 0){
    
      int idx = s->text[s->text_idx] - 32;
      
      int w = s->font->char_widths[idx];
      
      s->draw_box->x = 380 - w;
      
      s->font->graphic->x = s->char_offset;
      
      fb2d_copy_straight(s->font->graphic, s->draw_box,
                         w, s->font->height, MODE_S);
      
      s->draw_box->x = 1;
      
      s->text_idx++;
      
      
      
      if(s->text_idx == s->text_len){
        
        if(s->loop_count > 0){
          
          s->loop_count--;
          
        }
        
        if(s->loop_count > 0 || s->loop_count == -1){
          
          s->text_idx = 0;
          
        }
        
      }
      
      if(s->loop_count == 0){
        
        s->char_width = 384;
        
      }
      
      else{
      
        idx = s->text[s->text_idx] - 32;
        
        w = s->font->char_widths[idx];
        
        s->char_width = w + s->font->kerning;
        
        s->char_offset = s->font->char_offsets[idx];
        
      }
      
    }
    
    else{
      
      s->loop_done = 1;
      
    }
    
  }
  
}
