Liberty BASIC Help Online

Start with the Background
 
    WindowHeight = 320
    WindowWidth = 400
    graphicbox #w.g, 0, 0, 400, 300
    open "sprite test" for window_nf as #w
 
A window that contains sprites must include a graphicbox, or it must be a graphics window.  Only one graphicbox or graphics window in a program may use sprites.
 
BACKGROUND FROM LOADED BITMAP
There are several ways to designate a background image.  One way is to use a loaded bitmap as the background.  The bitmap must first be loaded with the LOADBMP command:
 
    loadbmp "landscape", "bg1.bmp"
 
 
The BACKGROUND command sets the bitmap called "landscape" to be the background:
 
    print #w.g, "background landscape";
   
Note that simply setting the background bitmap does not cause the background image to display on the screen.  See the section on Drawing and Collision Detection to find out about updating the display.
 
NOTE ABOUT BACKGROUND BITMAP SIZE
If the loaded bitmap is the same width and height as the graphicbox, or the client area of the graphics window, it is used just as it is.  If the width of the bitmap is less than the width of the graphicbox or graphics window, then it is stretched to fit.  If the height of the bitmap is less than the height of the graphicbox or graphics window, then it is stretched to fit.
 
Here is an example.  The width of the following bitmap is greater than the width of the graphicbox, so it remains unchanged.  The height of the bitmap is less than the height of the graphicbox, so it is stretched to fit.
 
Image bm13.GIF
 
IMPORTANT NOTE ABOUT BACKGROUND SIZE
The background bitmap image will be stretched to fit the width of the graphicbox if the width is smaller than the width of the graphicbox, and the height is stretched if the height of the bitmap is smaller than the height of the graphicbox.  THESE NUMBERS REFERS TO THE HEIGHT AND WIDTH INDICATED WHEN THE GRAPHICBOX IS CREATED, NOT THE VISIBLE PORTION OF THE GRAPHICBOX.  It is possible to create a graphicbox whose dimensions are much larger than the window that contains it.  The background image will be stretched to fit the given dimensions, not the visible dimensions of the graphicbox.
 
 
BACKGROUND FROM SCREEN
The background may be drawn into the graphicbox or graphics window with Liberty BASIC graphics commands, such as FILL, LINE, BOXFILLED, DRAWBMP, etc.  This bitmap is loaded into memory and given a name with the GETBMP command.  This example uses a graphicbox whose width is 400 and height is 300.  The example gets the bitmap from the graphicbox at 0,0 and with a width of 399 pixels and a height of 299 pixels, and gives it the name "landscape".  (Note that the width and height appear to be less than the width and height of the graphicbox, but the graphicbox dimensions include the frame, and the background bitmap should only include the inside area.)  It can then be used as the designated background bitmap in the same way as a bitmap loaded with the LOADBMP command:
 
 
    print #w.g, "getbmp landscape 0 0 399 299";
    print #w.g, "background landscape";
 
 
DEFAULT BACKGROUND
If no BACKGROUND command is issued, a plain white background will be used.
 
CHANGING THE BACKGROUND
The background can be changed at any time.  It may be changed to a bitmap that has been loaded into memory with the LOADBMP command, or with the GETBMP command.  It is also possible to draw on the screen to create a new GETBMP bitmap during program execution, and then designate it to be the background.
 
To set a new bitmap called "mountains" as the background, just use the BACKGROUND command after loading the bitmap:
 
   loadbmp "mountains", "mts.bmp"
   print #w.g, "background mountains";
 
 
IMPORTANT!
The background will not appear in the graphicbox or graphics window until  the DRAWSPRITES command is given.  This command updates the display.  Even if there are no sprites in use, or if no sprites are visible, the DRAWSPRITES command must be issued to display the background onscreen.  Whenever the BACKGROUND command is issued, it must be followed by a DRAWSPRITES command to cause it to show on the screen.
 
 
SCROLLING THE BACKGROUND
The background image can be moved within the graphicbox or graphics window with the BACKGROUNDXY command.  The x, y location specified on the background image will be placed at point 0, 0 of the graphicbox or graphics window.  Positive and negative numbers are acceptable for the x and y locations.
 
  print #handle.ext, "backgroundxy 25 20"
 
OR
  x=25:y=20
  print #handle.ext, "backgroundxy ";x;" ";y
 
OR
  x=x+5:y=y-10
 
  print #handle.ext, "backgroundxy ";x;" ";y
 
See how to set up the sprites in Designate Sprites.
 
Here is a small program that uses GETBMP to get a background, and then scrolls it.  Notice that no sprites have been added to the program, but to update the background a DRAWSPRITES command is issued.
 
nomainwin
WindowWidth=410
WindowHeight=340
graphicbox #w.g, 0,0,400,300
open "Window" for window_nf as #w
 
print #w.g, "down;fill blue"
print #w.g, "color red;backcolor red"
 
print #w.g, "boxfilled 200 150"
print #w.g, "getbmp landscape 0 0 399 299";
print #w.g, "background landscape";
print #w, "trapclose [quit]"
 
timer 100,[scroll]
wait
 
[scroll]
x=x+5:y=y+5
print #w.g, "backgroundxy ";x;" ";y
print #w.g, "drawsprites"
[loop]
wait
 
[quit]
close #w:end


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/