stan cartwright - 2017-08-23

Some code to put a pre-defined shape on glcd ili9341 and erase it,
This follows on from Anobiums'ssd1306 sprite that used pset.
Any improvements welcome.
The idea is plot the sprite data but if 0 then skip when ploting and erasing.
https://youtu.be/3c5cp1mMzRs

;simple sprite using pset on ILI9341 GLCD
#chip mega328p, 16
#option explicit
#include <glcd.h>
;
#define GLCD_TYPE GLCD_TYPE_ILI9341
#define GLCD_DC   portb.2
#define GLCD_CS   portd.7
#define GLCD_RESET   portd.4 ; Reset line Tie high..not needed
#define GLCD_DO   portb.3 ;  MOSI SDI
#define GLCD_SCK   portb.5 ;    SCK
#define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
#define GLCD_EXTENDEDFONTSET1
;
;now rename colours to make it easier to set up sprite data
#define bk ILI9341_BLACK
#define re ILI9341_RED
#define gr ILI9341_GREEN
#define bl ILI9341_BLUE
#define wh ILI9341_WHITE
#define pu ILI9341_PURPLE
#define ye ILI9341_YELLOW
#define cy ILI9341_CYAN
#define dg ILI9341_D_GRAY
#define lg ILI9341_L_GRAY
#define si ILI9341_SILVER
#define ma ILI9341_MAROON
#define ol ILI9341_OLIVE
#define li ILI9341_LIME
#define aq ILI9341_AQUA
#define te ILI9341_TEAL
#define na ILI9341_NAVY
#define fu ILI9341_FUCHSIA
;
GLCDBackground = ILI9341_BLACK
GLCDRotate (landscape)
GLCDCLS ILI9341_BLACK
;
dim dx,dy as integer
dim temp as word
dim sprite_height,sprite_width as byte ;height and width of sprite in pixels
dim spritedata as byte;data to make sprite
dim sprite_y as byte ;sprite screen y position
dim sprite_x as Word ;sprite x position-screen 320 wide
dim sprite_row,sprite_column,py,ptr as byte ;sprite sub vars
dim pset_sprite as byte ;0 erase sprite. 1 plot sprite
dim pixel as word ;data val for sprite pixel-0 no plot or erase else it's pixel lit
;
;demo vars
dim dx,dy,spx,spy,oldspx,oldspy as byte
dim spx,oldspx as word ;display is 320 pixels
spx=100:spy=100:sprite_height=12:sprite_width=12
dx=1:dy=1
;
do ;demo moving sprite
  if spx> (319-sprite_width) then
    dx=-1
  end if
  if spx=0 then
    dx=1
  end if
  if spy> (239-sprite_height) then
    dy=-1
  end if
  if spy=0 then
    dy=1
  end if
  oldspx=spx:oldspy=spy
  spx=spx+dx:spy=spy+dy
  pset_sprite=0 ;erase sprite
  sprite (oldspx,oldspy,sprite_width,sprite_height,pset_sprite)
  sprite_x=spx:sprite_y=spy
   pset_sprite=1 ;plot sprite
  sprite (spx,spy,sprite_width,sprite_height,pset_sprite) ;redraw in next position
loop ;end demo
;
sub sprite (sprite_x,sprite_y,sprite_width,sprite_height,pset_sprite)
ptr=0:py=sprite_y
for sprite_row=1 to sprite_height ;sprite rows
  for sprite_column=0 to sprite_width-1 ;sprite columns
    ReadTable spritedata,ptr,pixel ;sprite pixel
      if pset_sprite=0 and pixel<>0 then ;erase on and pixel was lit
        pset sprite_x+sprite_column,py,GLCDBackground
      else
      if pixel<>0 then ;pixel lit not background
        pset sprite_x+sprite_column,py,pixel
      end if
    end if
    ptr=ptr+1
  next sprite_column
  py=py+1
next sprite_row
end sub
;
table spritedata
0,0,0,0,0,ye,ye,0,0,0,0,0
0,0,0,ye,ye,wh,wh,ye,ye,0,0,0
0,0,y,bl,bl,wh,wh,bl,bl,0,0
0,ye,bl,bl,bl,wh,wh,bl,bl,bl,ye,0
0,ye,bl,bl,bl,wh,wh,bl,bl,bl,ye,0
ye,wh,wh,wh,wh,wh,wh,wh,wh,wh,wh,ye
ye,re,re,re,re,re,re,re,re,re,re,ye
0,ye,re,wh,wh,wh,wh,wh,wh,re,ye,0
0,ye,re,wh,wh,wh,wh,wh,wh,re,ye,0
0,0,ye,re,re,wh,wh,re,re,ye,0,0
0,0,0,ye,re,re,re,re,ye,0,0,0
0,0,0,0,0,ye,ye,0,0,0,0,0
end table
 

Last edit: stan cartwright 2017-08-23