Mr. Twinkle 0.1 Beta

Miscellaneous Forums/Blitz Showcase/Mr. Twinkle 0.1 Beta

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

There were some funcs for this in the bc showcase, but links are down... :(

Only found this one:

; =========
; VRAM Saver
; =========
; By turtle1776

;Video memory limitations can prevent you from using
;as many images as you would like in your Blitz project.
;This demo shows how to get around that limit so you can
;get more graphics in your game.

;Specifically, this demo shows how to:

;(1) using the imageToRAM function, load a
;    graphic from your hard drive into system
;    RAM, rather than video memory (VRAM), 
;    which is more limited  

;(2) using the RAMtoVRAM function, load the 
;     graphic from system RAM to VRAM when needed.
;     This is usually 2-3 times faster than loading directly from the
;     hard drive during run time.

;Instructions:

;(1) put a graphic of your choosing in the same folder as this program
;(2) change the name of the graphic at the top of the program where
;    it says usedGraphic$ = "yourGraphic.bmp"

;------------


     usedGraphic$ = "yourGraphic.bmp"     
     numberGraphicsLoadedtoRAM = 1
     
;1.     Launch the program
     Graphics 800,600
     SetBuffer BackBuffer()
     
;2.     Dimension two arrays for the RAM and VRAM versions of the graphics
     Dim RAMgraphic(numberGraphicsLoadedtoRAM+1)
     Dim graphic(numberGraphicsLoadedtoRAM+1) ;this is the array that stores our graphics     

;3.     Load the image from the hard drive to the bank (in RAM)
     imageToRAM(1,usedGraphic$)      
     
;4.  Load the graphic from RAM to VRAM when you need it (do this during runtime).
;     This will be 2-3 times faster than loading from the hard drive.
     RAMtoVRAM(1)

;5.     Print to screen
     While Not KeyDown(1) ;while escape key isn't pressed
          Cls
          DrawImage graphic(1),30,30                              
          Flip
     Wend
     End


;=================================
;FUNCTIONS START HERE

     
;This function loads an image file directly into RAM.
;No VRAM is used (other than temporarily). It is saved in a bank.
Function imageToRAM(imageNumber,fileName$,maskR=255,maskG=255,maskB=255) 

     ;Load the graphic
     image = LoadImage(fileName$)
     pixelWidth = ImageWidth(image)
     pixelHeight = ImageHeight(image)
     
     ;Create the bank to store the graphic and save some stats
     RAMgraphic(imageNumber) = CreateBank(pixelWidth*pixelHeight*8+50)     
     PokeInt RAMgraphic(imageNumber),0,pixelWidth ;save image width
     PokeInt RAMgraphic(imageNumber),8,pixelHeight     ;save image height
     PokeInt RAMgraphic(imageNumber),16,maskR ;save mask R value
     PokeInt RAMgraphic(imageNumber),24,maskG ;save mask G value
     PokeInt RAMgraphic(imageNumber),32,maskB ;save mask B value          
     
     ;Store the graphic pixel-by-pixel in the bank
     newImage = CreateImage(pixelWidth,pixelHeight) ;create a new map image.
     SetBuffer ImageBuffer(newImage)     
     DrawImage image,0,0          
     LockBuffer ImageBuffer(newImage)
     storeLocation = 40     
     For a = 0 To pixelWidth-1
     For b = 0 To pixelHeight-1          
          PokeInt RAMgraphic(imageNumber),storeLocation,ReadPixelFast(a,b)
          storeLocation = storeLocation + 8
     Next
     Next
     UnlockBuffer ImageBuffer(newImage)
     SetBuffer BackBuffer()

     ;Free up the video memory created in this process     
     FreeImage image
     FreeImage newImage 
     
End Function


;This function loads the digitized image from RAM to VRAM
;where it can actually be used.
Function RAMtoVRAM(imageNumber)

     ;Look up the size and stats of the graphic     
     pixelWidth = PeekInt (RAMgraphic(imageNumber),0)
     pixelHeight = PeekInt (RAMgraphic(imageNumber),8)
     maskR = PeekInt (RAMgraphic(imageNumber),16)
     maskG = PeekInt (RAMgraphic(imageNumber),24)
     maskB = PeekInt (RAMgraphic(imageNumber),32)
     maskRGB = getRGB (maskR,maskG,maskB)     
     
     ;Generate the image from the data stored in the bank
     graphic(imageNumber) = CreateImage(pixelWidth,pixelHeight) ;create a new image.
     SetBuffer ImageBuffer(graphic(imageNumber))
     Color maskR,maskG,maskB
     Rect 0,0,pixelWidth,pixelHeight,1 ;cover the background of the image with the mask color
     LockBuffer ImageBuffer(graphic(imageNumber))
     storeLocation = 40
     For a = 0 To pixelWidth-1
     For b = 0 To pixelHeight-1
          rgb = PeekInt (RAMgraphic(imageNumber),storeLocation)
          If rgb <> maskRGB Then WritePixelFast a,b,rgb ;write pixel if not the mask color (fewer WritePixelFast that way)
          storeLocation = storeLocation + 8                                                  
     Next
     Next
     UnlockBuffer ImageBuffer(graphic(imageNumber))          
     SetBuffer BackBuffer()
     MaskImage graphic(imageNumber),maskR,maskG,maskB

End Function

;Thanks to Cyberseth and his collection of "Really Useful Functions" for this one.
Function GetRGB(r,g,b)
     Return b Or (g Shl 8) Or (r Shl 16)
End Function


The idea is: while the gfx mode starts you load all tmp images into system ram. When needed switch them to vram.