BG_VIEW


 [Features] The background graphics, display position and display size are set up.

 [Format] BG_VIEW [StartX],[StartY],[ViewsizeX],[ViewsizeY],[Magnifying display],[Graphic screen sync switch]

 [Explanation]
    In the initial state because 'BG' is hidden, in use, it turn on display by 'SCREEN' statement.
    The background graphics, display position and display size are set up.
    The background graphic is comprised of the row of parts of the graphic buffer, the parts are 16x16 pixel each.
    One screen 16x16parts as a base unit, It sets up square so that the whole area may become 256 screens. (BG_SCALE)
    Indication starting coordinate (upper left) is set up by [StartX],[StartY]. (default 0,0)
    The starting point coordinates(0,0) of sprite are also synchronized to this start coordinate.
    Display portion extracted from Background graphics, it specify by [ViewsizeX],[ViewsizeY]. (default 256,256)
    As 'Magnifying display', the whole(BG & Sprite) magnifying power is specified.
    It specify the number according to machine's resolution.
    It can be specified in decimal units. (reduce in less than 1. max 64)

    [Resolution x2 switch] can be specified as the 7th parameter.
    n=0 or unspecified: nomal mode.
    n=1: Sprite x2 resolution.
    n=2: Sprite & BG x2 resolution.

    [Graphic screen sync switch]
    It use, when using Graphic screen as the back and making it 2screens-scrolling.
    To specify whether to make graphic screen synchronize with BG screen size.
    0 Synchronize-ON.
     Graphic screen is full screen from the top left.(default)
    1 Synchronize-OFF.
     Graphic screen is the same size & position as BG screen frame.
     Expansion rate also becomes the same.
     The display reading position of graphic screen can be specified by 'roll ,,vx,vy'.
     (Virtual hardware scroll feature)
    e.g. sample: "smp_2roll.bas"
    Also, by adding 2 to this value (n=2,n=3),
     it's possible to mask the outside of 'BG_VIEW' frame with black.
    With this, covering Sprite that overflow outside the frame.

    [supplementation]
    If parameter are omitted, it will keep the previous values. (Initial state 0,0,256,256)
     Graphic screen size is ensured as much as display screen size.
     and, for models with extra memory, 1536Pixel(H*V) is ensured over the display screen.
     It can use for double scrolling within BG-frame.
     Also if [sync switch] value=1 then, the text screen is synchronized similarly with display position and size.

    There is currently no command to expand the Graphics buffer size,
     by making a dummy picture of plain be hope size and 'gload'(command) it,
     the size can be expanded up to 3840x3840.
    For machines with less memory and resolution, up to 2160x2160.
    For more than 4K or higher resolution machines, it corresponds up to the resolution.

    [Resolution x2 switch] Supplement and Details.
    For example, when BG-picture made with 'bg_view ,,256,256' and zoom-in for full screen,
     usually, if [ Sprite definition range of 'sp_def' is 16x16 ]
     then [ display range of 'sp_put' is 16x16(BGpaets-1piece) ],
    This switch on then,
     contents of the buffer[definition range 32x32] can be displayed into (BGpaets-1piece) size.
    Even in this display, the BG-Graphic's collision judgment is the same as in the case of [16x16].
    [2] = Sprite x2 resolution and BG-Graphic x2 resolution.
    Normaly, Buffer-load source is [1part=16pixel units] in BG, this will be double 32pixel.
    (The buffer's definition picture also prepares what was drawn at 1block=32x32)
    With this, [movement unit] and [collision judgment] become the same treatment as case 16.
    As a usage example, 5th-parameter[Magnifying display]=2 and this[Resolution x2 switch]=2 then,
     display is 32x32pixel resolution, [Coordinate specification] and
     [various judgment] can be handled in the same way as [16x16].
    (In OpenGL mode, because huge images are necessary, can not be specified this BG double, value=2)

    e.g.
     screen ,,,1
     bg_view 0,0,256,256,2


  BG_SCALE


 [Features] The size of the whole background graphics is specified.

 [Format] BG_SCALE x,y

 [Explanation]
    The number of horizontal screens and the number of vertical screens are specified.
    The gross area (x*y) must be for 256 screens or less.
    One screen consists of 16x16 parts.

 [e.g.]
  bg_scale 16,16
  bg_scale 8,1



  BG_PUT


 [Features] A parts is drawn on background graphics by coordinates specification of a screen.

 [Format] BG_PUT x,y,n

 [Explanation]
    The parts pattern n is written in the background graphics of specification coordinates.
    Writing is immediately reflected in the display.
    This coordinate isn't coordinate of the whole background graphics(not BG_FILL type),
     it becomes coordinate of the pixel, displayed on a screen. (same as the coordinate of sprite.)
    A pattern number is a pattern of a graphic buffer(it is divided per 16x16 pixels.).
    Pattern number n = BG Horizontal position(0~) + BG Vertical position(0~) x256.
    e.g.  If position(4,2), the Pattern number is 516.

    In the case of reversing display.
     X-Reversing is further add +128 into pattern number.(8th bit 1)
     Y-Reversing is further add -32768 into pattern number.(16th bit 1)

 [e.g.]
    bg_put 128,96,4
    bg_put 128,96,4+128 :'x-reverse



  BG_FILL


 [Features] Background graphics are filled up with the specified parts.

 [Format] BG_FILL n,x1,y1 [,x2,y2] [,Copy-switch]

 [Explanation]
    Pattern number n = BG Horizontal position(0~) + BG Vertical position(0~) x256.
    When 'Copy-switch' is omitted, to be filled with pattern n. The range is (x1,y1)-(x2,y2).
    When (x2,y2) is omitted, it becomes the writing of (x1,y1) independently.
    The coordinate used here becomes the coordinate of the parts unit of whole background graphic.
    When BG_SCALE 4,4 is specified, the range becomes 0~63,0~63. (with 1 screen the row of 16 parts)
    A display isn't updated until 'BG_SCENE' is performed.

    When 'Copy-switch' is specified, the parts side of buffer also moves according with the x, y moves.
    Buffer's image of the range appears in the BackgroundGraphics side, in appearance as it is.
    'CopySwitch' value.
     0.No reverse.
     1.X-reverse. Order of parts is from x2 to x1. Value is added +128.
     2.Y-reverse. Order of parts is from y2 to y1. Value is added -32768.
     3.XY-reverse. It becomes the order which combined 1 and 2.

 [e.g.]
    bg_fill 520,10,10,20,20
    bg_fill 520,10,10,20,20,3 :'xy-reverse



  BG_SCENE


 [Features] To specify first start screen of Background graphics.

 [Format] BG_SCENE x,y

 [Explanation]
    Let be a unit, 1screen= 16x16parts. (same as 'BG_SCALE')
    The range which can be specified: 0~(BG_SCALE value-1).

 [e.g.]
  bg_scene 0,3



  BG_ROLL


 [Features] To make Background graphic map scroll.

 [Format] BG_ROLL [Direction-X],[Direction-Y] [,n] [,Absolutely-AX] [,Absolutely-AY]

 [Explanation]
    The scroll direction is specified by positive number(right,up) and negative number(left,down). Value 0 doesn't scroll.
    Scrollable range of pixels that can be specified at one time is (-16~0~16).
    Even if it carries out specification beyond it, the absolute value 16 doesn't exceed.
    The vertical and horizontal end is connected.(spherical scroll)
    If specified as n= 1, sprite will also move according to scrolling.

    In OpenGL mode, it can specify absolutely coordinate AX,AY.
    (In this case, Relative-posXY and n-switch is not used)
    The range is 'bg_scale'width * 256 pixel.
    (Example: [bg_scale 4,2] - The range: AX:0~1023 AY:0~511)
    Although the portion beyond the range is looping,
     in OpenGL mode, it will not be spherical scroll display.

 [e.g.]
     bg_roll 1,-1    :'Relative direction
     bg_roll ,,,200,100    :'Absolutely pos



  BG_BORDER


 [Features] The boundary of background graphics's parts with a collision judging is specified.

 [Format] BG_BORDER n

 [Explanation]
    The boundary of the parts that conflict with the sprite is specified by a pattern number.
    Pattern number n = BG Horizontal position(0~) + BG Vertical position(0~) x256.
    Type:
     0: Before specified parts. There is no collision.
     1: A specified parts position.
     2: After specified parts. There is a collision.
 [e.g.]
  bg_border 8



  BG_LOAD


 [Features] Background graphics data is read from a file.

 [Format] BG_LOAD "Filename"

 [Explanation]
    Although it is necessary to make it the same setup by 'BG_SCALE' for reproducing the same screen as origin,
     since it is saved per screen, the reading is possible by the different setting only by the row of the screen changing.

 [e.g.]
  bg_load "map1.bg"



  BG_SAVE


 [Features] Background graphics data is saved at a file.

 [Format] BG_SAVE "Filename"

 [Explanation]
    The range specified by 'BG_SCALE' is saved per screen.

 [e.g.]
  bg_save "map1.bg"



  BG_GET


 [Features] The pattern number of the parts of background graphics is acquired.

 [Format] BG_GET(x, y [,n])

 [Explanation]
    There are two coordinates which can be specified.
     n= 0: Coordinate in the same pixel unit as a display. (It is the same as 'BG_PUT')
     n= 1: Coordinate in the parts unit of the whole background graphics. (It is the same as 'BG_FILL')
    If 3rd parameter 'n' omit, it will become the same as 0.
    'n' is the number 0 or 1 only, as switch.
    Can not specify variables and formula.
    Return value:
    Pattern number = BG Horizontal position(0~) + BG Vertical position(0~) x256.

 [e.g.]
  a=bg_get(24,16,1)
  b=bg_get(8,3)



  BG_MAP


 [Features] The scroll coordinate of background graphics are acquired.

 [Format] BG_MAP(n)

 [Explanation]
    The current scroll position is returned. (upper left 0,0).
     n=0: Scroll position of horizontal. 
     n=1: Scroll position of vertical.

 [e.g.]
  a=bg_map(0)



  SP_MAPX  SP_MAPY


 [Features] The arbitrary display position, 'which part(BG-graphics) corresponds to?', the position is returned.

 [Format] SP_MAPX(n)  SP_MAPY(n)

 [Explanation]
    The arbitrary display position 'n',
    'which part(BG-graphics) corresponds to?'
    The position is returned.
    Even while BG-graphics is scrolling, it's calculated and the position is returned.

    SP_MAPX(n): give pixel pos-X to 'n', and returned BG-parts pos-X.
    SP_MAPY(n): give pixel pos-Y to 'n', and returned BG-parts pos-Y.

    Sprite and BG-graphics processing can be handled concisely
     with 'bg_get()','sp_bghit()', and (writting)'bg_put','bg_fill'
     but this function use when more complex processing.
    The acquired value can be used for 'bg_fill' as it is.

 [e.g.]  Write Data-16 to 'BG-graphics of Sprite-No.4 pos'.
     x=sp_mapx(sp_stat(4,0))
     y=sp_mapy(sp_stat(4,1))
     bg_fill 16,x,y


  BG_GENE


 [Features] To acquire enumeration of newly appearing BG-parts numbers by BG-scroll, and assist with the appearance of Sprite-characters.

 [Format] BG_GENE(n)

 [Explanation]
    When want to generate sprite at a specific position in BG-scroll screen,
     data is embedded at BG map and used.
    When BG-screen scroll on all directions,
     newly parts group occur at once every 16 pixels, with horizontal or vertical row.
    (When BG view size is 256pixel, 16parts and a little something extra occur)
    It's possible to program which generates Sprite-characters
     according to the part number obtained from there.
    (Parts number in buffer) n = BG Horizontal position(0~) + BG Vertical position(0~) x256.
    n:
    0: The number of emerging parts
    1: The first part X-coordinate
    2: The first part Y-coordinate
    3: To return horizontalscroll=0, verticalscroll=1
    4: To return verticalscroll=0, horizontalscroll=1
    [5 or later]: To return the emerging parts numbers
    The parts data falls within the range 5 to 4+bg_gene(0).
    As soon as the last position data is read out, the value of bg_gene(0) is cleared, it becomes 0.

    However, when horizontal and vertical parts occur at the same time in HV-simultaneous scrolling,
    first, horizontal scrolling data is acquired, (arrangement of parts is vertical row)
     and after that, the value of bg_gene(0) is not cleared by last value acquisition,
     vertical scrolling data will be acquired immediately.
    When this second vertical final data is acquired, the value of bg_gene(0) become 0.
    By horizontal scroll, appearance of parts is left or right,
    drawing of parts run vertical, so the data obtained is vertical row.
    By vertical scroll, appearance of parts is top or bottom,
    drawing of parts run horizontal, so the data obtained is horizontal row.

    For left scroll, the first part is top-left, the next parts will be located towards the bottom.
    The first parts coordinates is (bg_gene(1),bg_gene(2)), and
     to finding a solution of the coordinates of (i)th parts,
    x=bg_gene(1)+i*16*bg_gene(3)
    y=bg_gene(2)+i*16*bg_gene(4)
    this is the coordinate of the upper-left corner of the (i)th parts.
    By either horizontal or vertical scroll direction,
    bg_gene(3) and bg_gene(4) are counteracting the amplification of value of X or Y.
    The other directions are below.
    For right scroll, the first part is top-right, the parts drawing run towards the bottom.
    For up scroll, the first part is top-left, the parts drawing run towards the right.
    For down scroll, the first part is bottom-left, the parts drawing run towards the right.
    Please refer to included sample "smp_gene.bas" for actual usage.

    *The places(parts number) of buffer assigned to the Sprite generating,
     for actual usage, to keep the places plain color.
     This indicate the kind of Sprite to generate,
      and it differs from actual displayed Sprite patterns.

 [e.g.]  "smp_gene.bas"  partial excerpt
    250 lp01:
    260 for i=1 to ec
    270 sp_put i
    280 cx(i)+=1 :'move sprite according to scroll
    290 if sp_out(i)thenkn=i:i-=1:gosub ckill: :continue :'erase it if outside of screen
    300 next
    310 bg_roll -1,0  :'scroll -occur bg_gene data in here for every 16 pixels
    320 if bg_gene(0)>0 then :'if data is newly generated
    330 lp02:
    340 for i=0 to bg_gene(0)-1  :'only the number of parts
    350 if bg_gene(i+5)>=2 then :'if each data is 2 or more, sprite generation according to it
    360 ec+=1
    370 nx=bg_gene(1)+i*16*bg_gene(3) :'finding solution of X-coordinates of each parts
    380 ny=bg_gene(2)+i*16*bg_gene(4) :'and Y-coordinates
    390 na=bg_gene(i+5)-2:gosub cmake: :'sprite generation according to it
    400 endif
    410 next
    420 endif
    430 if bg_gene(0)>0 then goto lp02: :'if vertical data also occurs simultaneously, onemore processing
    440 vsync
    450 goto lp01:

    Square:BG-blocks, Circle:Sprite occurrence
    Blue circle appear many times.
    Red circle is deleted from the map by 'BG_GDSET' setting when it appears, so it doesn't appear after the second lap.


  BG_GDSET


 [Features] For make sure that the Sprite which appeared by 'BG_GENE' doesn't come out repeatedly,
                       to specify the range of parts number that will be erased from BG-map simultaneously with appearing.

 [Format] BG_GDSET [start-parts-number],[end-parts-number] [,'SP_OUT' extended range]

 [Explanation]
    The parts specified by this, it appears once, it will be cleared with 0 on the BG-map.
    The range of parts number is specified.

    About 'SP_OUT': the spread of screen range,
     it can be specified per pixel with 'BG_GDSET' 3rd, 4th parameter.
     (3rd-horizontally, 4th-vertically)
    Please refer to 'SP_OUT' linked with this setting.

 [e.g.]  "smp_gene.bas"  partial excerpt
     190 bg_gdset 3,10
     'When parts of the number from 3 to 10 appears with scrolling,
      leave the data in 'BG_GENE', and the data is cleared by 0 from BG-map.


  SP_BGHIT


 [Features] Returns the value of the collision detection between sprite and background graphics's parts.

 [Format] SP_BGHIT(SpriteNo [, DirectionX, DirectionY] )

 [Explanation]
    It check whether a specified side is the parts of the collision.
    The side is 'SP_MARGIN'judgment position of the direction + 'SP_BGHIT'specified amount.

    When there is at least one collision part, the return value=2 is returned.
    When all are the boundary parts of 'BG_BORDER', the return value=1 is returned.
    Otherwise, The return value=0 is returned.
    In the reversal parts, not to able to distinguish properly, because it contain reversal value.

    When DirectionX,DirectionY is (0,0), The collision parts inside sprite(inside MARGIN) is investigated.
    The return value is the same as the case of the side. 

    In the case of the description from which (DirectionX,DirectionY) was omitted,
    [only one parameter - e.g.: sp_bghit(spriteNO) ]
     to read the central point of whole size of Sprite and,
     to distinguish whether or not it is collision parts specified by 'BG_BORDER'.
    If collision-parts(including the specified position) then retuen the BG-pattern-number,
     otherwise non-collision-parts then return 0.

 [e.g.]
  a=sp_bghit(0,1,0)
    In the case of [SpriteSize16(Coordinate0,0), MARGIN4, SP_BGHIT(0,1,0)]
    x=12 position on the right-hand side of sprite investigates whether it is a part of a collision judging.
    When the direction X is a minus value,
     the margin position on the left of sprite, direction X, the position is investigated.

 [e.g.] Background graphic & collision sample.
   [smp_bghit.bas]

    100 line(16,0)-(31,15),col(14),b
    110 line(32,0)-(47,15),col(10),b
    120 line(48,0)-(63,15),col(13),b
    130 line(51,3)-(60,12),col(9),b
    140 get@(0,0),64,16,(0,0)
    150 sp_def 0,(48,0),16,16
    160 cls 3 : print"Touch!"
    170 sp_on 0,1 : sp_margin 0,3
    180 bg_scale 2,2
    190 bg_view 0,0,256,256
    200 bg_border 1
    210 bg_fill 2,0,0,15,0:bg_fill 2,0,15,15,15
    220 bg_fill 2,0,0,0,15:bg_fill 2,15,0,15,15
    230 bg_fill 1,5,5,9,9
    240 bg_scene 0,0
    250 screen1,1,1,1
   260 x=80:y=128:mx=1:my=-1
    270 '
    280 for i=1 to 4000
    290 if touch(2)>0 and touch(1)<256 then bg_put touch(0),touch(1),1
    300 sp_put 0,(x,y),0
    310 h1=sp_bghit(0,mx,0)
    320 h2=sp_bghit(0,0,my)
    330 h4=h3 : h3=sp_bghit(0,0,0)
    340 if h3<>h4 then print h3
    350 if h1=2 then mx=-mx:print h1
    360 if h2=2 then my=-my:print h2
    370 bg_roll mx,my
    380 vsync 1
    390 next



  OPENGL


 [Features] It change to the drawing display mode by OpenGL.

 [Format] OPENGL ON|OFF|0~4

 [Explanation]
    High-speed displaying is enabled by the OpenGL mode.
    It corresponds to all the displays of Text, Graphics, Sprite, and BG-graphics.
    It exert the effect especially in display with motion of sprite and BG-graphics.
    Although it can shift to OpenGL mode from program as it is, as procedure,
     to execution 'opengl on' after finishing all the BG-graphics setup by 'bg_' command.
    ('bg_scene' is possible at a later)
    It becomes 'opengl off' automatically at the time of program end.

    The notes in OpenGL mode.
    Although drawing to real screen can be performed, display is not updated.(bg_put..etc)
    It is necessary to reconstruct the screen individually
     by 'opengl [number]'command in described below for update.
    (It take many processing and stop for a moment, as battery, continuous update is not recommended)
    There is not spherical connection display at edge of the map.
    (The outside of range is not displayed properly)
    In OpenGL mode, a lot of memory is consumed by BG-graphics display.
    If specify large size of 'bg_scale' in low memory & low spec machine,
     it may become 'Out of memory error' or end processing by memory breakage.

   
ON  To set OpenGL drawing display mode.
    OFF To set Normal drawing display mode.
    Number 0~4 
     From the real screen updated on each screen, 
      to reconstruct the screen for OpenGL and make it reflect in display.
     0 'Text screen' is reconstructed. (after 'print' command ..etc)
      1 'Graphic screen' is reconstructed. (after 'line','circle' command ..etc)
      2 'Graphic buffer' is reconstructed. (after 'gload' command ..etc)
      3 'BG-graphics map' is reconstructed. (after 'bg_put','bg_fill' command ..etc)
    e.g.  opengl 3

 [e.g.]
    'Sample program: 'acsamp.bas' - Line 250
     250 opengl on
    Please change in this way and run.

    The sample high-speed scrolling of BG-graphics screen by flick.
    [smp_opengl.bas]

    100 '
    110 cls3:screen,,,1< br>    120sx=12:sy=2
    130 for i=0 to 15
    140 line(0,i)-(15,i),rgb(200-i*6,48+i*8,64+i)
    150 next
    160 get@(0,0),16,16,(16,0):cls 2
    170 if size(0)+size(1)>800 then sz=2 else sz=1
    180 bg_scale sx,sy
    190 bg_view 0,0,256,256,sz
    200 for y=0 to sy-1
    210 for x=0 to sx-1
    220 symbol(x*16,y*16),hex$(x)+hex$(y),12
    230 next
    240 next
    250 getbitmap:cls 2
    260 for x=0 to sx*16-1
    270 for y=0 to sy*16-1
    280 if pixc(x,y)<>0 then bg_fill 1,x,y
    290 next
    300 next
    310 init"kb:0"
    320 opengl on :ui_msg"Flick"
    330 'loop
    340 vsync
    350x=-touch(3):y=-touch(4)
    360 bg_roll x+mx,y+my
    370 mx=mx+x/16-sgn(mx)/10
    380 my=my+y/16-sgn(my)/10
    390 goto 340