01/27/99

Tips'n Tricks

[Home of QB][Links to my most wanted sites][Downloads on the house][Tips'n Tricks]

 
 
 
Subjects

Putting pixels on screen 13
Getting a 64Kb String
Assigning Colors
Memory Access Speed Up
Timing/Delays
Music and Sound effects



 
 
 
 
 
 
 
 


Tips'n Tricks by: MRS 
Subject: Putting pixels on screen 13 

There are many ways of putting a pixel on the screen. But I have found that, when it comes to speed, the PSET command is about the fastest there is. The POKE command also makes it possible to draw directly to the screen using this bit of code: 

DEF SEG = &HA000 
POKE X% + (320& * Y%), C% 

This code is similar to PSET, also the speed is about the same, but the code can be optimized: 

DEF SEG = &HA000 + (20 * Y%) 
POKE X%, C% 

The advantage of this code is that it can be used on any segment in memory. To use this code to manipulate a 64000b screen buffer, just change the &HA000 segment to your buffer segment. But be careful one wrong step can be fatal.


Tips'n Tricks by: MRS
Subject: Getting a 64Kb String 

By using a 64Kb string you can easily load a 320x200 picture from a file into memory. This code will allocate the string: 

TYPE DoubleBuffer
 B1 AS String * 32000 
 B2 AS String * 32000 
END TYPE 

REDIM BigBuffer(0) AS DoubleBuffer

As you can see the buffer "BigBuffer(0)" is actually two buffers, just ignore that. To load a picture into the buffer just open a file in BINARY mode and set the filepointer to where the picture begins. You can load a picture from anywhere in the file using GET and PUT, and you can have more then one picture in the same file. 

To load the picture from the buffer to the screen you have to use assembly code and that is another trick. 


Tips'n Tricks by: MRS
Subject: Assigning Colors 

By using the PALETTE statement you can assign your own colors. The number of attributes depends on the screen mode you have selected, in Screen 12 you have 16 attributes to work with, and in Screen 13 you have 256. When selecting a screen mode, all attributes are set to the most common used colors, attribute 0 is black, attribute 1 is blue and so on. 

If you what to change attribute 0, in mode 13, from black 
to red you write: 

SCREEN 13 
PALETTE 0, 63 

Why 63? you may ask. Well - every color on your screen is represented by a unique number, that means that only that color have that number. There are more than one rule of generating a color, but the VGA adapter uses RGB (Red,Green,Blue) to generate it. In the QB environment the range of the three components are 0-63, where 63 is the brightest. The information on the three components have to be made into a unique number before Qbasic can use it. 

Here is how to do just that: 

uniquenum&  = (0 to 63)(R) + (&h100 * (0 to 63))(G) + (&h10000*(0 to 63))(B) 

or 

uniquenum& = &h00003F 'Red 
uniquenum& = &h003F00 'Green 
uniquenum& = &h3F0000 'Blue 

The number of unique colors to generate are  64 * 64 * 64 = 262144. 

The PALETTE USING statement allows you to set all the color attributes. In screen 13 the code is something like this: 

SCREEN 13 
DIM CLUT&(255) 

CLUT&(0)= &h000000 
CLUT&(1)= &h003F3F


CLUT&(254)= &h10103F
CLUT&(255)= &h101010 

PALETTE USING CLUT&(0) 

This will take 1-2 sec. to load the new palette and that is not fast enough, if you want to do something like Fading or Cycling. Therefor I will mention another way of doing the same thing. 

SCREEN 13 
DIM CLUT%(2 , 255) 

Now you don't have to think of generating a unique number. Just think in the range of 0-63 for the three components RGB. 

CLUT%(0 , 0)= 63'R 
CLUT%(1 , 0)= 0 'G 
CLUT%(2 , 0)= 0 'B 

CLUT%(0 , 1)= 13'R 
CLUT%(1 , 1)= 15'G 
CLUT%(2 , 1)= 63'B 


CLUT%(0 , 254)= 26'R 
CLUT%(1 , 254)= 63'G 
CLUT%(2 , 254)= 0 'B 

CLUT%(0 , 255)= 0'R 
CLUT%(1 , 255)= 0'G 
CLUT%(2 , 255)= 0'B 

OUT &H3C8, 0 'start loading at attribute 0 
FOR i% = 0 TO 255 
OUT &H3C9, CLUT%(0 , i%)'R
OUT &H3C9, CLUT%(1 , i%)'G
OUT &H3C9, CLUT%(2 , i%)'B
NEXT 


Tips'n Tricks by: MRS
Subject: Memory Access Speed Up 

If you don't use XMS/EMS memory in your program, try removing your MemoryManager from memory. I got a 200% speed up on some of my programs. 


Tips'n Tricks by: MRS
Subject: Timing/Delays

Setting up a delay timer is actually very simple. Normally the computers timer is updated at a frequency of 18.2 Hz. That means that the TIMER function also have a resolution of 18.2 Hz. Here is an example of how to use the TIMER function as a delay:

SUB delay(Var!)
Temp! = TIMER
DO
LOOP UNTIL TIMER - TEMP! > Var!
END SUB

Because of the clock resolution the minimum possible delay is 0.055s. You can change the clock frequency to get a better resolution, but then the TIMER function will not be working properly. One way of getting a delay below the 0.055s is by changing the clock tick frequency. This code will change the clock tick speed to any frequency:

SUB ChangeClockTickSpeed(Frequency!)
temp& = &H1234DD / Frequency!
OUT &H43, &H34
OUT &H40, temp& AND 255
OUT &H40, temp& \ 256
END SUB

When you change the clock tick speed the computers time will be wrong, and will only be reset if the computer is restarted. Changing the clock tick frequency under windows does not have any effect, it will always be 18.2 Hz. Also before you exit your program remember to reset the frequency to 18.2 Hz. 

You can get the clock ticks by reading memory location 40:6C. Here is an example:

SUB WaitClockTicks(Ticks&)
DEF SEG = &H40
FOR I& = 1 to Ticks&
status% = PEEK(&H6C)
DO
LOOP UNTIL status% <> PEEK(&H6C)
NEXT
END SUB

Now how does this work. Well lets assume the clock tick speed was changed to 100 Hz, writing WaitClocksTicks 100 will then wait 1 sec. and writing WaitClocksTicks 1 will wait 1/100 sec. For higher clock frequencies the code may take longer to execute then the actual delay. So you may want to rewrite the code for more sensitive timing.


Tips'n Tricks by: MRS
Subject: Music and Sound effect

The only way QBasic is able of producing sound is through the pc speaker. The PLAY statement helps you make the sound using the common note system. When the PLAY statement is used, the string send is placed into a buffer from where it is played. The buffer can hold up to 32 notes at the same time. That is useful if you want to play sound/music in the background and do something else in the foreground.
A simple sound effect like the sound of a laser gun, can also be played in the background, here is an eksample: playback.bas

PLAY "MB" makes QBasic go into background mode, in background mode both PLAY and SOUND is played in the background. SOUND can also be used to stop all sound/music that is currently being played in the background, simply by sending a sound duration of zero to the speaker (SOUND 0,0)

To play music in the background you have to set up an event trapping system. When ever a certain amount of notes have been played the event trapping system will jump to a sub routine, and reload the play buffer. 

See the eksample on how to use an event trapping system to play music in the background: backmsc.zip



 

MRS