User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

drawing_to_an_in-memory_bitmap_texture [2020/06/05 11:09] (current)
richardrussell created
Line 1: Line 1:
 +=====Drawing to an in-memory bitmap/​texture=====
 +//by Richard Russell, June 2020//
 +By default, text and graphics output is displayed in the '​main'​ (output) window. ​ The supplied MULTIWIN library (available for both //BBC BASIC for Windows// and //BBC BASIC for SDL 2.0//) allows the output to be redirected to another **window**, but you can alternatively redirect it to an in-memory **bitmap** (BB4W) or **texture** (BBCSDL).
 +The code below illustrates how that can be achieved. ​ It creates a new bitmap (BB4W) or texture (BBCSDL) and draws some blue text into it on a yellow background. ​ Graphics can be drawn in a similar way, but you do need to be careful about the coordinate system because the in-memory bitmap/​texture is mapped to the **top-left** corner of the main window. ​ Unless it happens to be the same size as the main window, you are advised to use an ORIGIN statement to shift the graphics coordinates.
 +The example code does not show the in-memory bitmap/​texture being used for anything. ​ In practice it is likely that you will want to '​blit'​ it (possibly with scaling) to the screen at a later stage, for example using **SYS "​BitBlt"​** (requiring the use of a temporary Device Context) in BB4W or **SYS "​SDL_RenderCopy"​** in BBCSDL.
 +<code bb4w> ​
 +      REM Demo of drawing to an in-memory bitmap (BB4W) or texture (BBCSDL)
 +      bmw% = 160 : REM bitmap/​texture width
 +      bmh% = 120 : REM bitmap/​texture height
 +      REM Create the in-memory bitmap or texture:
 +      IF INKEY$(-256) = "​W"​ THEN
 +        SYS "​CreateCompatibleBitmap",​ @memhdc%, bmw%, bmh% TO newDest%
 +        newDest%% = newDest%
 +      ELSE
 +        SYS "​SDL_CreateTexture",​ @memhdc%, &​16362004,​ 2, bmw%, bmh% TO newDest%%
 +        IF @platform% AND &40 ELSE newDest%% = !^newDest%%
 +      ENDIF
 +      IF newDest%% = 0 THEN ERROR 100, "​Couldn'​t create bitmap/​texture"​
 +      REM Protect from errors using an SEH block:
 +      ok% = FALSE
 +        *REFRESH OFF
 +        REM Save the original destination and select the new destination:​
 +        IF INKEY$(-256) = "​W"​ THEN
 +          SYS "​SelectObject",​ @memhdc%, newDest%% TO oldDest%
 +          oldDest%% = oldDest%
 +        ELSE
 +          SYS "​SDL_GetRenderTarget",​ @memhdc% TO oldDest%%
 +          SYS "​SDL_SetRenderTarget",​ @memhdc%, newDest%%
 +          IF @platform% AND &40 ELSE oldDest%% = !^oldDest%%
 +        ENDIF
 +        REM Draw into the memory bitmap/​texture:​
 +        GCOL 128+11 : GCOL 4
 +        CLG : VDU 5,30 : PRINT "Hello world!";​
 +        ok% = TRUE
 +      REM Restore the original destination:​
 +      IF INKEY$(-256) = "​W"​ THEN
 +        SYS "​SelectObject",​ @memhdc%, oldDest%%
 +      ELSE
 +        SYS "​SDL_SetRenderTarget",​ @memhdc%, oldDest%%
 +      ENDIF
 +      *REFRESH ON
 +      REM Check for an error having occurred:
 +      REM Do something with the in-memory bitmap/​texture:​
 +      REM (left to the imagination)
 +      REM Delete the bitmap/​texture:​
 +      IF INKEY$(-256) = "​W"​ THEN
 +        SYS "​DeleteObject",​ newDest%%
 +      ELSE
 +        SYS "​SDL_DestroyTexture",​ newDest%%, @memhdc%
 +      ENDIF
 +      END
drawing_to_an_in-memory_bitmap_texture.txt ยท Last modified: 2020/06/05 11:09 by richardrussell