direct_20screen_20memory_20access
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| direct_20screen_20memory_20access [2018/03/31 13:19] – external edit 127.0.0.1 | direct_20screen_20memory_20access [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| =====Direct screen memory access===== | =====Direct screen memory access===== | ||
| - | //by Richard Russell, March 2007//\\ \\ Normally, your BASIC program doesn' | + | //by Richard Russell, March 2007//\\ \\ Normally, your BASIC program doesn' |
| + | |||
| + | <code bb4w> | ||
| DIM BITMAPINFOHEADER{Size%, | DIM BITMAPINFOHEADER{Size%, | ||
| \ Compression%, | \ Compression%, | ||
| Line 13: | Line 15: | ||
| bmi.Header.Planes.l& | bmi.Header.Planes.l& | ||
| bmi.Header.BitCount.l& | bmi.Header.BitCount.l& | ||
| - | Here an 8 bits-per-pixel bitmap has been defined; if you want a different format set the value of **bmi.Header.BitCount.l& | + | </ |
| + | |||
| + | Here an 8 bits-per-pixel bitmap has been defined; if you want a different format set the value of **bmi.Header.BitCount.l& | ||
| + | |||
| + | <code bb4w> | ||
| FOR I% = 0 TO 255 | FOR I% = 0 TO 255 | ||
| r% = I% | r% = I% | ||
| Line 20: | Line 26: | ||
| bmi.Palette%(I%) = b% + (g% << 8) + (r% << 16) | bmi.Palette%(I%) = b% + (g% << 8) + (r% << 16) | ||
| NEXT | NEXT | ||
| - | Here, for simplicity, a grey-scale has been created, where each palette entry contains the same values for red, green and blue. In practice it is more likely that your program will need a selection of colours. You might need to store the colours in, for example, **DATA** statements.\\ \\ Now the bitmap format and colour table have been defined, you can create the bitmap itself:\\ \\ | + | </ |
| + | |||
| + | Here, for simplicity, a grey-scale has been created, where each palette entry contains the same values for red, green and blue. In practice it is more likely that your program will need a selection of colours. You might need to store the colours in, for example, **DATA** statements.\\ \\ Now the bitmap format and colour table have been defined, you can create the bitmap itself: | ||
| + | |||
| + | <code bb4w> | ||
| SYS " | SYS " | ||
| IF hbitmap% = 0 ERROR 100, " | IF hbitmap% = 0 ERROR 100, " | ||
| Line 27: | Line 37: | ||
| SYS " | SYS " | ||
| CLS | CLS | ||
| - | On successful completion of this code the variable **bits%** will contain the address in memory of the bitmap, hence **?bits%** would be the pixel value of the //bottom left// pixel in the window (i.e. it is a **bottom up** bitmap).\\ \\ It is most likely that you will want to ' | + | </ |
| + | |||
| + | On successful completion of this code the variable **bits%** will contain the address in memory of the bitmap, hence **?bits%** would be the pixel value of the //bottom left// pixel in the window (i.e. it is a **bottom up** bitmap).\\ \\ It is most likely that you will want to ' | ||
| + | |||
| + | <code bb4w> | ||
| bytesperpixel% = bmi.Header.BitCount.l& | bytesperpixel% = bmi.Header.BitCount.l& | ||
| bytesperline% = ((bmi.Header.Width% * bytesperpixel%) + 3) AND -4 | bytesperline% = ((bmi.Header.Width% * bytesperpixel%) + 3) AND -4 | ||
| Line 37: | Line 51: | ||
| NEXT | NEXT | ||
| SYS " | SYS " | ||
| - | The variable **bytesperpixel%** contains the number of bytes comprising each pixel (in this example **1**) and the variable **bytesperline%** contains the number of bytes in one line (row) of the bitmap, which must always be rounded up to a multiple of 4.\\ \\ The **InvalidateRect** is necessary to cause the screen to be updated from the new bitmap contents. For best performance you should invalidate only the smallest rectangle containing the changed pixels, but for convenience the code shown invalidates the entire window. If you //do// want to invalidate only a rectangle use code similar to the following:\\ \\ | + | </ |
| + | |||
| + | The variable **bytesperpixel%** contains the number of bytes comprising each pixel (in this example **1**) and the variable **bytesperline%** contains the number of bytes in one line (row) of the bitmap, which must always be rounded up to a multiple of 4.\\ \\ The **InvalidateRect** is necessary to cause the screen to be updated from the new bitmap contents. For best performance you should invalidate only the smallest rectangle containing the changed pixels, but for convenience the code shown invalidates the entire window. If you //do// want to invalidate only a rectangle use code similar to the following: | ||
| + | |||
| + | <code bb4w> | ||
| DIM rc{l%, t%, r%, b%} | DIM rc{l%, t%, r%, b%} | ||
| REM Load rectangle dimensions here (left, top, right, bottom) | REM Load rectangle dimensions here (left, top, right, bottom) | ||
| SYS " | SYS " | ||
| - | If you are performing some kind of animation, it is quite likely that you will want to force an immediate screen refresh:\\ \\ | + | </ |
| + | |||
| + | If you are performing some kind of animation, it is quite likely that you will want to force an immediate screen refresh: | ||
| + | |||
| + | <code bb4w> | ||
| *REFRESH | *REFRESH | ||
| + | </ | ||
| + | |||
| You can still use the majority of the standard BASIC or Windows API methods of writing to the output bitmap, in addition to the direct memory access provided by this technique. For example if you want to output text you can use **PRINT**.\\ \\ Note that this technique may not work succesfully if the PC's display is itself set to a paletted (e.g. 256 colour) mode. | You can still use the majority of the standard BASIC or Windows API methods of writing to the output bitmap, in addition to the direct memory access provided by this technique. For example if you want to output text you can use **PRINT**.\\ \\ Note that this technique may not work succesfully if the PC's display is itself set to a paletted (e.g. 256 colour) mode. | ||
direct_20screen_20memory_20access.1522502355.txt.gz · Last modified: 2024/01/05 00:18 (external edit)