SCREENPTR
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgScreenptr
- 最后更新: 2023-07-09
返回当前工作页面帧缓冲区的指针
语法
declare function Screenptr ( ) as any ptr用法
result = Screenptr返回值
指向当前工作页面帧缓冲区内存的指针,如果未设置图形模式则返回 NULL(0)。
说明
Screenptr 提供了一种直接读/写工作页面帧缓冲区的方法。在尝试任何读写之前应使用 Screenlock。返回的指针有效期到下一次对 Screen (Graphics) 或 Screenres 的调用为止,那时它将失效。
Screenptr 也可用于测试对 Screen (Graphics) 或 Screenres 的调用是否成功,非 NULL(<> 0)的返回值表示成功。
要访问屏幕缓冲区中的像素,需要知道屏幕的每像素字节数和行间距(每行字节数),以及宽度和高度以避免越界。这些信息可以使用 Screeninfo 获取。
帧缓冲区中的每一行长 pitch 字节。帧缓冲区由 height 行组成,按照它们在屏幕上的位置顺序存储,从上到下、从左到右。
由于 FreeBASIC 图形库的设计,Screenptr(如果非 NULL)将始终指向后备缓冲区,而不是实际的视频 RAM。
示例
start GeSHi
vb
Const SCREEN_WIDTH = 640, SCREEN_HEIGHT = 480
Dim As Long w, h, bypp, pitch
'' Make 8-bit screen.
ScreenRes SCREEN_WIDTH, SCREEN_HEIGHT, 8
'' Get screen info (w and h should match the constants above, bypp should be 1)
ScreenInfo w, h, , bypp, pitch
'' Get the address of the frame buffer. An Any Ptr
'' is used here to allow simple pointer arithmetic
Dim buffer As Any Ptr = ScreenPtr()
If (buffer = 0) Then
Print "Error: graphics screen not initialized."
Sleep
End -1
End If
'' Lock the screen to allow direct frame buffer access
ScreenLock()
'' Find the address of the pixel in the centre of the screen
'' It's an 8-bit pixel, so use a UByte Ptr.
Dim As Integer x = w \ 2, y = h \ 2
Dim As UByte Ptr pixel = buffer + (y * pitch) + (x * bypp)
'' Set the center pixel color to 10 (light green).
*pixel = 10
'' Unlock the screen.
ScreenUnlock()
'' Wait for the user to press a key before closing the program
Sleepend GeSHi
start GeSHi
vb
Const SCREEN_WIDTH = 256, SCREEN_HEIGHT = 256
Dim As Long w, h, bypp, pitch
'' Make 32-bit screen.
ScreenRes SCREEN_WIDTH, SCREEN_HEIGHT, 32
'' Get screen info (w and h should match the constants above, bypp should be 4)
ScreenInfo w, h, , bypp, pitch
'' Get the address of the frame buffer. An Any Ptr
'' is used here to allow simple pointer arithmetic
Dim buffer As Any Ptr = ScreenPtr()
If (buffer = 0) Then
Print "Error: graphics screen not initialized."
Sleep
End -1
End If
'' Lock the screen to allow direct frame buffer access
ScreenLock()
'' Set row address to the start of the buffer
Dim As Any Ptr row = buffer
'' Iterate over all the pixels in the screen:
For y As Integer = 0 To h - 1
'' Set pixel address to the start of the row
'' It's a 32-bit pixel, so use a ULong Ptr
Dim As ULong Ptr pixel = row
For x As Integer = 0 To w - 1
'' Set the pixel value
*pixel = RGB(x, x Xor y, y)
'' Get the next pixel address
'' (ULong Ptr will increment by 4 bytes)
pixel += 1
Next x
'' Go to the next row
row += pitch
Next y
'' Unlock the screen.
ScreenUnlock()
'' Wait for the user to press a key before closing the program
Sleepend GeSHi
方言差异
- 在 -lang qb 方言中不可用,除非使用别名
__Screenptr引用。
与 QB 的区别
- FreeBASIC 新增
另请参阅
Screen (Graphics)ScreenresScreenInfoScreenlockScreenunlock
返回 目录