Structure Help Please
Posted: Tue May 10, 2011 9:53 pm
Being a 'structure novice' when I look at the PB example below, it looks like the author is doing something 'clever' with the structure facility which I can't quite fathom. My non-oop brain fails to get the benefit of things like structures (it thinks, perhaps wrongly, they are doing things the long way when there is a more concise way) so struggles with their use.
However, this code seems to be using one to create a new Type called Pixel which confusingly (to me) defines a long variable called Pixel! So I presume each Pixel will be 32bits in size; fair enough.
I guess what really confuses me is
I understand *line is a pointer to a location in video memory and *Line+4 increments the pointer by 4 bytes
but my brain is failing me with *Line\Pixel. I know an instance of 'line' has been created (and it gets 'created' multiple times it seems - maybe that doesn't matter as it's a pointer) by *Line.Pixel = Buffer+Pitch*y. And it looks like by assigning a value to the pointer in this configuration we are writing the value of ColorTable(pos2) to memory. But as I say, I can't quite see how it works.
If anyone could help clarify this I'd be very grateful. Also, If anyone has the time to show me how the same thing could be done without the use of the Structure (by using Poke) that would help me too.
Finally, for any Mac users - you (we) get the results of this demo in grey not blue. Once I can see what data is being put where I'll try to figure out why.
However, this code seems to be using one to create a new Type called Pixel which confusingly (to me) defines a long variable called Pixel! So I presume each Pixel will be 32bits in size; fair enough.
I guess what really confuses me is
Code: Select all
*Line\Pixel = ColorTable(pos2) ; Write the pixel directly to the memory !
*Line+4
but my brain is failing me with *Line\Pixel. I know an instance of 'line' has been created (and it gets 'created' multiple times it seems - maybe that doesn't matter as it's a pointer) by *Line.Pixel = Buffer+Pitch*y. And it looks like by assigning a value to the pointer in this configuration we are writing the value of ColorTable(pos2) to memory. But as I say, I can't quite see how it works.
If anyone could help clarify this I'd be very grateful. Also, If anyone has the time to show me how the same thing could be done without the use of the Structure (by using Poke) that would help me too.
Finally, for any Mac users - you (we) get the results of this demo in grey not blue. Once I can see what data is being put where I'll try to figure out why.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Drawing via Direct Screen Access (DSA)
;
; (c) 2006 - Fantaisie Software
;
; ------------------------------------------------------------
;
; Note: disable the debugger to run at full speed !
;
#ScreenWidth = 800 ; Feel free to change this to see the pixel filling speed !
#ScreenHeight = 600
If InitSprite() = 0 Or InitKeyboard()=0
MessageRequester("Error","DirectX 7+ is needed.",0)
EndIf
Structure Pixel
Pixel.l
EndStructure
Procedure.f GSin(angle.f)
ProcedureReturn Sin(angle*(2*3.14/360))
EndProcedure
; Pre-calculated values are faster than realtime calculated ones...
; ... so we save them in an array before starting gfx operations
Dim CosTable(#ScreenWidth*2)
Dim ColorTable(255)
For i = 0 To #ScreenWidth*2
CosTable(i) = GSin(360*i/320)* 32 + 32
Next
If OpenScreen(#ScreenWidth, #ScreenHeight, 32, "PB Plasma")
Repeat
Wave+6
If Wave > 320 : Wave = 0 : EndIf
If StartDrawing(ScreenOutput())
Buffer = DrawingBuffer() ; Get the start address of the screen buffer
Pitch = DrawingBufferPitch() ; Get the length (in byte) took by one horizontal line
PixelFormat = DrawingBufferPixelFormat() ; Get the pixel format.
If PixelFormat = #PB_PixelFormat_32Bits_RGB
For i = 0 To 255
ColorTable(i) = i << 16 ; Blue is at the 3th pixel
Next
Else ; Else it's 32bits_BGR
For i = 0 To 255
ColorTable(i) = i ; Blue is at the 1th pixel
Next
EndIf
For y = 0 To #ScreenHeight-1
pos1 = CosTable(y+wave)
*Line.Pixel = Buffer+Pitch*y
For x = 0 To #ScreenWidth-1
pos2 = (CosTable(x+Wave) + CosTable(x+y) + pos1)
*Line\Pixel = ColorTable(pos2) ; Write the pixel directly to the memory !
*Line+4
Next
Next
StopDrawing()
EndIf
ExamineKeyboard()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Else
MessageRequester("Error","Can't open the screen !",0)
EndIf
End