November 24, 2020, 07:15:41 AM

Author Topic: [bb] BankAsImage by jfk EO-11110 [ 1+ years ago ]  (Read 629 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] BankAsImage by jfk EO-11110 [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : BankAsImage
Author : jfk EO-11110
Posted : 1+ years ago

Description : This Method is based on a hack by Tom, so thanks. Make sure to save all your 200 pages letters before you run it - it will crash in 16 Bit mode.

Code :
Code: BlitzBasic
  1. Graphics 640,480,32,2
  2. SetBuffer BackBuffer()
  3.  
  4. ;WARNING - this will crash your PC in any other Mode than 32 Bit Color! If you run this windowed,
  5. ;the desktop needs to be True Color! (not 16 Bit Aka 65535 Colors!)
  6.  
  7.  
  8. ;the physical structure of a bank is as follows:
  9. ; -at the physical adress (bankhandle + 4) is a 32bit pointer to the banks data segment
  10. ; -at the physical adress (bankhandle + 8) is a value that relfects the size of the bank in bytes
  11.  
  12. ;The following example will redirect a banks data pointer to the data of an existing image.
  13. ;This method allows to send the physical adress of an image to an external DLL, so the DLL
  14. ;may write RGB Data directly to the image and therefor allow Blitz to draw the Image as any
  15. ;other Blitz Image.
  16.  
  17. ; There needs to be a kernel32.decls in the userlib folder, containing at least the following
  18. ; declaration:
  19.  
  20.  
  21. ;.lib "kernel32.dll"
  22. ;RtlMoveMemory%(Destination,Source*,Length) : "RtlMoveMemory"
  23. ;RtlMoveMemory2%(Destination*,Source,Length) : "RtlMoveMemory"
  24. ;RtlMoveMemory4%(Destination,Source,Length) : "RtlMoveMemory"
  25.  
  26.  
  27. ; Sending a DLL our Images physical Adress (well infact it's a bank) over a Userlib interface
  28. ; would require to use something like (assuming there is a function "SendImageAdress" in the DLL):
  29.  
  30. ; (In Blitz:)
  31. ; SendImageAdress(FakeBank+4)
  32.  
  33. ; (In the Userlib:)
  34. ; SendImageAdress%(imagepointer)
  35.  
  36. ; Please note: this is an undocumeted hack. It's compatibility with future updates of Blitz3D
  37. ; is in no way ensured.
  38. ; Thanks to Tom who did this hack in the first place.
  39.  
  40. Const IMAGEBUFFER_OFFSET=72
  41.  
  42. w=320
  43. h=240
  44.  
  45. hack_image=CreateImage(w,h)
  46. LockBuffer ImageBuffer(hack_image) ; required : kind of activation
  47. UnlockBuffer ImageBuffer(hack_image)
  48.  
  49. ;------------
  50.  
  51. FakeBank=CreateBank(4) ; create a bank Handle
  52. ; Store the old banks pointer so we can clean up properly
  53. OldBankInfo=CreateBank(8)
  54. StoreBankInfo(FakeBank,OldBankInfo) ; store FakeBanks original size and pointer to its memory
  55. PointBankToImagebuffer(FakeBank,hack_image)
  56.  
  57.  
  58.  
  59. ;testing "plot to the bank": some pink dots
  60. For i=0 To 10000
  61.  PokeImageBuffer(hack_image,FakeBank,Rand(w),Rand(h),$FF00FF)
  62. Next
  63.  
  64. DrawImage hack_image,0,0
  65. Flip
  66. WaitKey()
  67.  
  68.  
  69. ; required(!), never forget this:
  70. RestoreBankInfo(FakeBank,OldBankInfo)
  71. End
  72.  
  73.  
  74.  
  75. Function PokeImageBuffer(image,buffer,x,y,rgba)
  76.  imageH=ImageWidth(image)
  77.  where=(y*imageH*4) + (4*x)
  78.  wmax=(ImageWidth(image)*ImageHeight(image)*16) ; clip to image size
  79.  If where<wmax
  80.   PokeInt( buffer, where, rgba )
  81.  EndIf
  82. End Function
  83.  
  84.  
  85.  
  86. Function StoreBankInfo(b,info)
  87.  Local temp=CreateBank(4)
  88.  RtlMoveMemory2(temp,b+4,4)
  89.  PokeInt(info,0,PeekInt(temp,0)) ; store old pointer
  90.  RtlMoveMemory2(temp,b+8,4)
  91.  PokeInt(info,4,PeekInt(temp,0)) ; store old size
  92.  FreeBank temp
  93. End Function
  94.  
  95.  
  96.  
  97. ; The clever bit, we change 'Bank' to point to the ImageBuffer!
  98. Function PointBankToImagebuffer(b,image)
  99.  RtlMoveMemory4(b+4,ImageBuffer(image)+IMAGEBUFFER_OFFSET,4)
  100.  size=ImageWidth(image) * ImageHeight(image) * 4
  101.  Local temp=CreateBank(4)
  102.  PokeInt(temp,0,size)
  103.  RtlMoveMemory(b+8,temp,4)
  104.  FreeBank temp
  105. End Function
  106.  
  107.  
  108.  
  109. Function RestoreBankInfo(b,info) ; fake,old
  110.  Local temp=CreateBank(4)
  111.  PokeInt(temp,0,PeekInt(info,0))
  112.  RtlMoveMemory(b+4,temp,4); restore old pointer
  113.  PokeInt(temp,0,PeekInt(info,4))
  114.  RtlMoveMemory(b+8,temp,4); restore old size
  115.  FreeBank temp
  116. End Function


Comments :


Mikele(Posted 1+ years ago)

 I would like to know whether it's possible to use this function for textures (by texturebuffer)?


jfk EO-11110(Posted 1+ years ago)

 I think that isn't so easy since textures are managed a bit more complicated (eg. Mipmapping etc.) But you still can use Copyrect from the image to the texture. As long as you use the flag 256 for the texture it's pretty fast.


Mikele(Posted 1+ years ago)

 ahh... ok... then still Copyrect... ;)thanks anyway


Panno(Posted 1+ years ago)

 ah no more  rgb = PeekInt (buffer,pointer) WritePixelFast(y,x,rgb)coolthx


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal