'---- CMM2 BOOTING BOOT ----


'---- INITIALIZE DEMO BOOT ----

SUB INIT_DEMO_BOOT

 'PERSPECTIVE CENTER
 XP%(0)=7:      YP%(0)=17
 XP%(1)=MAXX-9: YP%(1)=17
 XP%(2)=MAXX-59:YP%(2)=4
 XP%(3)=57:     YP%(3)=4
 CO%(0)=RGB(187,187,187)
 NP%(0)=(4)

 'PERSPECTIVE LEFT
 XP%(4)=0:      YP%(4)=YP%(2) 
 XP%(5)=XP%(3): YP%(5)=YP%(2)
 XP%(6)=XP%(0): YP%(6)=YP%(0)
 XP%(7)=0:      YP%(7)=YP%(0)
 CO%(1)=RGB(17,34,68)
 NP%(1)=(4)

 'PERSPECTIVE RIGHT
 XP%( 8)=XP%(2): YP%( 8)=YP%(2) 
 XP%( 9)=MAXX:   YP%( 9)=YP%(2)
 XP%(10)=MAXX:   YP%(10)=YP%(0)
 XP%(11)=XP%(1): YP%(11)=YP%(0)
 CO%(2)=RGB(17,34,68)
 NP%(2)=(4)

 'LOAD THE CAR NOW TO AVOID STUTTERING IN THE MIDDLE OF DEMO
 PAGE WRITE 6
 LOAD PNG "./ASSETS/paint_car_3d.png"

 'SET TEMPORARY BUFFER ON PAGE 3
 PAGE WRITE 3

 'WRITE CMM2 IMAGE AND BLOCKS
 LOAD PNG "./ASSETS/DEMO_BOOT_640.PNG"

 'WRITE FAKE PERSPECTIVE POLYGON CMM2 CASE
 POLYGON NP%(),XP%(),YP%(),CO%(),CO%()

 'TIMER TO KEEP THE SYNC
 TIMENOW=Timer

END SUB


'--- RUN DEMO BOOT ----

SUB RUN_DEMO_BOOT

 LOCAL T,TT,TTT

 'SET WRITE PAGE
 PAGE WRITE 2

 'COPY TEMPORARY BUFFER FROM PAGE 3 TO 1
 PAGE COPY 3 TO 1,I

 'CLEAR BACKGROUND ABOVE CMM2
 BOX 0,0,MAXX,MAXY,0,,RGB(17,34,68)

 'CMM2 FRONT PLATE
 BLIT 0,0,0,0,MAXX,177,1,0 

 'TURN OFF LIGHT - GREEN
 BLIT 14,376,161,101,18,18,1,0
 'TURN OFF LIGHT - RED
 BLIT 14,376,161,77,18,18,1,0

 'SWITCH OFF
 BLIT 90,276,504,44, 39,65,1,4

 'SAVE CMM2 STATUS
 BLIT READ #1,0,18,MAXX,159

 'COPY CMM2 TO MAIN PAGE
 PAGE COPY 2 TO 0,I

 'SHOW SD CARD ON MAIN PAGE
 PAGE WRITE 0
 BLIT 263,223,263,223,122,260,1,4
 PAGE WRITE 2

 PAUSE(1000)
 
 'SD CARD ANIMATION
 FOR T=1 TO 75 STEP 1+T/5

  FPS(45)

  'RESTORE CMM2
  BOX 80,25,MAXX-160,152,0,,RGB(0,0,0)
  BLIT WRITE #1,0,18

  'RESTORE BACKGROUND BELOW
  BOX 263,177,122,MAXY-177,0,,RGB(17,34,68)

  'SD CARD ANIMATION / FAKE PERSPECTIVE
  TTT=0
  FOR TT=1 TO 200 STEP 1+T/20
   TTT=TTT+1
   BLIT 263,223+TT,263,223-T*1.85+TTT,122,1,1,4
  NEXT TT
  IF T>70 THEN BOX 264,109,24,3,0,,RGB(90,90,90)

  'COPY TO MAIN PAGE
  PAGE WRITE 0
  BLIT 263,111,263,111,263,MAXY-111-T,2,0
  PAGE WRITE 2  
 NEXT T

 PAUSE(500)

 FOR T=1 TO 0 STEP -1
 PAGE WRITE T
  'SWITCH ON
  BLIT 31,281,504,73,39,65,1,4
  BOX 504,39,39,35,0,,RGB(0,0,0)
 NEXT T

 'TURN ON LIGHT - GREEN
 BLIT 161,101,161,101,18,18,1,0
 
 PAUSE(200):T=1:TIMENOW=Timer

 'TURN ON LIGHT - RED
 BLIT 161,77,161,77,18,18,1,0

 'SAVE CMM2 STATUS ON PAGE 1
 PAGE WRITE 1
 BLIT 0,110,0,110,MAXX,309,0,0

 'GREEN BLINKING LIGHT AND SCROLL
 PAGE WRITE 0
 I=0:T=0:II=0

 'FAKE PERSPECTIVE - CMM2
 DO WHILE I<50

  IF T>8 THEN   
   I=I+1
   II=II+0.4
   'FAKE LIGHTNING ON CMM2 BOX
   IF 187+I/2<=255 THEN CO%(0)=RGB(187+I/2,187+I/2,187+I/2)
   TTT=I*10
   IF I<100 THEN
    'FAKE PERSPECTIVE ON THE CMM2 FACE PLATE
    FOR TT=1 TO 197 STEP 1+I/(20.1-(I-1)/2.2)
     TTT=TTT+1
     IF TT<15 THEN
      IF TT<5 THEN
       BOX 7,18+TTT,MAXX-14,3,0,,CO%(0)
       BOX 0,YP%(2)-5,MAXX,5,0,,CO%(1)
       POLYGON NP%(),XP%(),YP%(),CO%(),CO%()   
      ENDIF
      BLIT 0,14+TT,0,14+TTT,MAXX,1,1,4
     ELSE
      BLIT 0,12+TT,0,12+TTT,MAXX,1,1,0
     ENDIF
    NEXT TT   
   ENDIF

   'PERSPECTIVE CENTER
   XP%(0)=7-I/25-10:      YP%(0)=17+I*10+10
   XP%(1)=MAXX-9+I/25+10: YP%(1)=YP%(0)
   XP%(2)=MAXX-59+I*1.5:  YP%(2)=4-I
   XP%(3)=57-I*1.5:       YP%(3)=YP%(2)

   'PERSPECTIVE LEFT
   XP%(4)=0:      YP%(4)=YP%(2) 
   XP%(5)=XP%(3): YP%(5)=YP%(2)
   XP%(6)=XP%(0): YP%(6)=YP%(0)
   XP%(7)=0:      YP%(7)=YP%(0)

   'PERSPECTIVE RIGHT
   XP%( 8)=XP%(2): YP%( 8)=YP%(2) 
   XP%( 9)=MAXX:   YP%( 9)=YP%(2)
   XP%(10)=MAXX:   YP%(10)=YP%(0)
   XP%(11)=XP%(1): YP%(11)=YP%(0)
 
  ENDIF 
 
  IF Timer>TIMENOW+100 AND Timer<TIMENOW+150 AND T<=8 THEN   
   'TURN ON LIGHT - RED
   BLIT 161,77,161,77,18,18,1,0
  ENDIF
 
  IF Timer>TIMENOW+250 THEN
   'TURN OFF LIGHT - RED
   IF T<=8 THEN BLIT 161,77,161,77,18,18,2,0
   T=T+1: TIMENOW=Timer
  ENDIF

  FPS(30)

 LOOP

 BLIT CLOSE #1
 PAGE WRITE 1

 'SPINNING CMM2 (RECTANGLE)
 XT=MAXX/1.5:YT=MAXY/1.2: R=17:G=34:B=68:COT=200:II=0: SN=RAD(-45) 
 DO WHILE COT>0

  XP%(0)=COS(SN         )*(XT)+MAXX/2: YP%(0)=40+SIN(SN         )*(YT)+MAXY/2-II
  XP%(1)=COS(SN+RAD(90) )*(XT)+MAXX/2: YP%(1)=40+SIN(SN+RAD(90) )*(YT)+MAXY/2-II
  XP%(2)=COS(SN+RAD(180))*(XT)+MAXX/2: YP%(2)=40+SIN(SN+RAD(180))*(YT)+MAXY/2-II
  XP%(3)=COS(SN+RAD(270))*(XT)+MAXX/2: YP%(3)=40+SIN(SN+RAD(270))*(YT)+MAXY/2-II
  II=II+1.1
  SN=SN+0.1  
  T=CO%(0)
  XT=XT/1.1:YT=YT/1.1
  TT=(MAXX/2)-XT
  R=R-2: IF R<0 THEN R=0
  G=G-2: IF G<0 THEN G=0
  B=B-2: IF B<0 THEN B=0
  COT=COT-5: IF COT<0 THEN COT=0
  BOX 0,0,MAXX,MAXY,0,,RGB(R,G,B)
  POLYGON (4),XP%(),YP%(),RGB(COT,COT,COT),RGB(COT,COT,COT)
  FPS(30)
  PAGE COPY 1 TO 0,I

 LOOP
 
END SUB

