Image:

1 - The square on screen (see Image) should be only gray but there is an artifact with different color!
2 - If the square goes out the top right corner the program will crash!
Can someone confirm ?
Code:
Code: Select all
EnableExplicit
Procedure.i main()
Protected.i exit,x,y,px,py,sx,sy,ex,ey,bytes
Protected.long *s,*d,*w,*h
If InitSprite() And InitKeyboard()
If OpenWindow(0,0,0,800,600,"Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0))
SetFrameRate(60)
x = WindowWidth(0) >> 1
y = WindowHeight(0) >> 1
Repeat
Repeat
Select WindowEvent()
Case #PB_Event_None
Break
Case #PB_Event_CloseWindow
exit = #True
EndSelect
ForEver
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
x - 2
EndIf
If KeyboardPushed(#PB_Key_Right)
x + 2
EndIf
If KeyboardPushed(#PB_Key_Up)
y - 2
EndIf
If KeyboardPushed(#PB_Key_Down)
y + 2
EndIf
ClearScreen(0)
StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_AllChannels)
*w = ?red_square
*h = ?red_square + 4
*s = ?red_square + 8
*d = DrawingBuffer()
sx = x - *w\l >> 1
sy = y - *h\l >> 1
ex = sx + *w\l - 1
ey = sy + *h\l - 1
If ex - sx <> 9
Debug "THIS SHOULD NEVER SHOW!"
EndIf
If ey - sy <> 7
Debug "THIS SHOULD NEVER SHOW!"
EndIf
bytes = DrawingBuffer() + (OutputHeight() * DrawingBufferPitch()) - (OutputDepth() >> 3)
For py = sy To ey
For px = sx To ex
If px >= 0 And px < WindowWidth(0) And py >= 0 And py < WindowHeight(0)
Debug Str(px) + " x " + Str(py);<- this will never exceed the screen coordinates!
*d = DrawingBuffer() + (py * DrawingBufferPitch()) + (px * (OutputDepth() >> 3))
*d\l = *s\l
EndIf
If *s\l <> $FF444444
Debug "THIS WILL NEVER SHOW BECAUSE WE DONT DRAW A WRONG COLOR!"
EndIf
If *d > bytes
Debug "THIS WILL NEVER SHOW BECAUSE WE DRAW INSIDE THE SCREEN ONLY!"
EndIf
*s + 4
Next
Next
StopDrawing()
FlipBuffers()
Until exit = #True Or KeyboardReleased(#PB_Key_Escape)
CloseScreen()
EndIf
CloseWindow(0)
EndIf
EndIf
ProcedureReturn #Null
EndProcedure
End main()
DataSection
red_square:
Data.l 10,8
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
Data.l $FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444,$FF444444
EndDataSection