text_scroller.c (2265B)
1#include <gb/gb.h> 2#include <stdint.h> 3#include <stdio.h> 4 5#include <gb/emu_debug.h> 6 7const uint8_t scanline_offsets_tbl[] = {0, 1, 2, 3, 3, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 0}; 8const uint8_t * scanline_offsets = scanline_offsets_tbl; 9 10#define SCROLL_POS 15 11#define SCROLL_POS_PIX_START (SCROLL_POS * 8) - 1 12#define SCROLL_POS_PIX_END ((SCROLL_POS + 1) * 8) - 1 13 14uint8_t scroller_x = 0; 15 16void scanline_isr() { 17 switch (LYC_REG) { 18 case 0: 19 SCX_REG = 0; 20 LYC_REG = SCROLL_POS_PIX_START; 21 break; 22 case SCROLL_POS_PIX_START: 23 SCX_REG = scroller_x; 24 LYC_REG = SCROLL_POS_PIX_END; 25 break; 26 case SCROLL_POS_PIX_END: 27 SCX_REG = LYC_REG = 0; 28 break; 29 } 30} 31 32const uint8_t scroller_text[] = "This is a text scroller demo for GBDK-2020. You can use ideas, that are "\ 33"shown in this demo, to make different parallax effects, scrolling of tilemaps which are larger than 32x32 "\ 34"tiles and TEXT SCROLLERS, of course! Need to write something else to make this text longer than 256 characters. "\ 35"The quick red fox jumps over the lazy brown dog. 0123456789. "; 36 37const uint8_t * scroller_next_char = scroller_text; 38uint8_t * scroller_vram_addr; 39uint16_t base, limit; 40 41void main() { 42 printf("Scrolling %d chars", sizeof(scroller_text) - 1); 43 44 CRITICAL { 45 STAT_REG |= STATF_LYC; LYC_REG = 0; 46 add_LCD(scanline_isr); 47 } 48 set_interrupts(VBL_IFLAG | LCD_IFLAG); 49 50 scroller_vram_addr = get_bkg_xy_addr(20, SCROLL_POS); 51 set_vram_byte(scroller_vram_addr, *scroller_next_char - 0x20); 52 53 base = (uint16_t) scroller_vram_addr & 0xffe0; 54 limit = base + 0x20; 55 56 while (1) { 57 scroller_x++; 58 if ((scroller_x & 0x07) == 0) { 59 // next letter 60 scroller_next_char++; 61 if (*scroller_next_char == 0) scroller_next_char = scroller_text; 62 63 // next vram position 64 scroller_vram_addr++; 65 if (scroller_vram_addr == (uint8_t *)limit) scroller_vram_addr = (uint8_t *)base; 66 67 // put next char 68 set_vram_byte(scroller_vram_addr, *scroller_next_char - 0x20); 69 } 70 wait_vbl_done(); 71 } 72}