User Tools

Site Tools


drawing_to_an_in-memory_bitmap_texture

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.

      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
      ON ERROR LOCAL IF FALSE THEN
        *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
 
      ENDIF : RESTORE ERROR
 
      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:
      IF NOT ok% ERROR ERR, REPORT$
 
      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
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
drawing_to_an_in-memory_bitmap_texture.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1