Page 3 of 3

Posted: Fri Mar 18, 2005 7:15 pm
by Fred
Just seen than the "For k=0 to #AppHeight" should be changed by For k=0 to #AppHeight-1" and the same for the width, else it can do a buffer overflow (may be the olrjr bug).

Posted: Fri Mar 18, 2005 8:42 pm
by olejr
Fred wrote:Just seen than the "For k=0 to #AppHeight" should be changed by For k=0 to #AppHeight-1" and the same for the width, else it can do a buffer overflow (may be the olrjr bug).
Yes... :wink:

And i was really starting to wonder,
because it was these kind of lines that caused the crashes:

Code: Select all

*screen1\l = pixel

Posted: Fri Mar 18, 2005 8:52 pm
by Psychophanta
olejr wrote:And i was really starting to wonder,
because it was these kind of lines that caused the crashes:

Code: Select all

*screen1\l = pixel
That's not logical. That code is well done.
I can't understand why on olejr PC it crashes, and doesn't for others :shock:

Posted: Fri Mar 18, 2005 9:58 pm
by olejr
@Psychophanta: It doesn't crash anymore.. Not after I added that '-1' to #AppHeight and #AppWidth..

Posted: Fri Mar 18, 2005 11:16 pm
by Psychophanta
olejr wrote:@Psychophanta: It doesn't crash anymore.. Not after I added that '-1' to #AppHeight and #AppWidth..
yeeees, i know, but why crashed before? Here not crashed :shock:

Posted: Sat Mar 19, 2005 12:41 pm
by Fred
Psychophanta: when you write a long pixel from 0 to 640 (inclusive) on a 640 pixels width screen, you write 641 pixels which means than the last one is outside of the allocated buffer. Now, it can crash or not depending of the hardware/drivers like all buffer overflows..

Posted: Sat Mar 19, 2005 1:13 pm
by Psychophanta
Fred wrote:...it can crash or not depending of the hardware/drivers like all buffer overflows..
I see. Thanx :)

Posted: Sat Mar 19, 2005 4:07 pm
by blueznl
and to add a little... as far as i know windows pads bitmaps, so it would depend on screen resolution nr. of colours etc. etc. it writing beyound the last 'planned' byte would cause a crash or not...

it may actually be fine on some specific machine, just isn't the proper thing to do :-)

Posted: Thu Nov 27, 2008 11:25 pm
by Psychophanta
I get a white blank screen in PB4.30B4, but i find no reason.
This is the tested code:

Code: Select all

; converted from bb code
; edited by webmatze
; turn debugger off for performance..

#appWidth = 640
#appHeight= 480

Global Dim Palette.l(255)
Global Dim table.l(#appWidth-1, #appWidth-1)

Procedure SetupPalette()
  ; create a temporary image...
  CreateImage(0, #appWidth, #appHeight)
  ; ...to draw a nice colour-pattern onto it
  StartDrawing(ImageOutput(0))
    For i = 0 To 63
     LineXY(i,0,i,200, RGB(0, 0, i*4))
    Next

    For i = 0 To 127
     LineXY(i+64,0,i+64,200, RGB(0, (i/2)*4, 63*4))
    Next

    For i = 0 To 63
     LineXY(i+192,0,i+192,200,RGB(i*4, 63*4, 63*4))
    Next
  ; grab palette data
  For x = 0 To 255
   Palette(x) = Point(x,0)
  Next
  StopDrawing()
  ; clean-up
  FreeImage(0)
EndProcedure

Procedure PreCalc()
  For y = 0 To #appHeight-1
   For x = 0 To #appWidth-1
    If x = 0 And y = 0
     table(y,x) = 255
    Else
     table(y,x) = (9000000000 / (Sqr(x*x + y*y) * 250000))
    EndIf
   Next
  Next
EndProcedure

If InitSprite()=0
  End
EndIf

OpenWindow(0, 0, 0, #appWidth, #appHeight, "testing...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #appWidth, #appHeight, 0, 0 ,0)

SetupPalette()
PreCalc()

halfW.l = #appWidth/2
halfH.l = #appHeight/2

While WindowEvent()<>#PB_Event_CloseWindow
  ; fps
  If GetTickCount_() => zeit + 1000
    FrameSek = Frames
    Frames = 0
    zeit = GetTickCount_()
  Else
    Frames + 1
  EndIf

  FlipBuffers():Delay(16)

  cosAlfa.f = Cos (alfa.f)
  cosAlfam.f = Cos (-alfa)
  cosAlfa2.f = Cos (alfa*2)
  cosAlfam2.f = Cos (-alfa*2)
  sinAlfa.f = Sin (alfa)
  sinAlfam.f = Sin (-alfa)
  sinAlfa2.f = Sin (alfa*2)

  x1.f = 60 * cosAlfa  + 30 * sinAlfam  + halfW
  y1.f = 30 * cosAlfam2 + 60 * sinAlfa   + halfH
  x2.f = 30 * cosAlfa    + 60 * sinAlfa2 + halfW
  y2.f = 60 * cosAlfa    + 30 * sinAlfa   + halfH
  x3.f = 45 * cosAlfam   + 45 * sinAlfa   + halfW
  y3.f = 45 * cosAlfa2  + 45 * sinAlfam  + halfH
  x4.f = 75 * cosAlfa    + 15 * sinAlfa2 + halfW
  y4.f = 15 * cosAlfam   + 75 * sinAlfa2 + halfH
  x5.f = 35 * cosAlfa    + 10 * sinAlfa   + halfW
  y5.f = 10 * cosAlfa2  + 35 * sinAlfam  + halfH
  x6.f = 40 * cosAlfam   + 30 * sinAlfa2 + halfW
  y6.f = 40 * cosAlfa    + 10 * sinAlfa   + halfH

  alfa.f+0.05

  If StartDrawing(ScreenOutput())
    *Screen.LONG = DrawingBuffer()

    byte_pixel.b = Round((DrawingBufferPixelFormat() + 1)/2, 1)
    byte_line.l = DrawingBufferPitch()

    For y = 0 To #appHeight-1 Step 2
      a1.l = Abs(y1-y)
      a3.l = Abs(y2-y)
      a5.l = Abs(y3-y)
      a7.l = Abs(y4-y)
      a9.l = Abs(y5-y)
      a11.l = Abs(y6-y)
      *screen1.LONG = *Screen.LONG + y*byte_line
      *screen2.LONG = *screen1.LONG + byte_line
      For x = 0 To #appWidth-1 Step 2
          a2.l = Abs(x1-x)
          a4.l = Abs(x2-x)
          a6.l = Abs(x3-x)
          a8.l = Abs(x4-x)
         a10.l = Abs(x5-x)
         a12.l = Abs(x6-x)
        pixel.l = table(a1, a2) + table(a3, a4) + table(a5, a6) + table(a7,a8) + table(a9, a10) + table(a11, a12)
        If pixel>255:pixel=255:EndIf
        pixel = palette(pixel)
        *screen1.LONG\l = pixel
        *screen1.LONG + byte_pixel
        *screen1.LONG\l = pixel
        *screen2.LONG\l = pixel
        *screen2.LONG + byte_pixel
        *screen2.LONG\l = pixel
        *screen1.LONG + byte_pixel
        *screen2.LONG + byte_pixel
      Next
    Next
    DrawingMode(0)
    DrawText(5,452,"FPS: "+Str(FrameSek),0)
    StopDrawing()
  EndIf
Wend

Posted: Fri Nov 28, 2008 6:57 am
by IceSoft
Maybe this changed line is helping you do understand why it is white:

I changed only this line:
If pixel>255:pixel%255:EndIf

The problem seems to be:
The result of this operation

Code: Select all

 pixel.l = table(a1, a2) + table(a3, a4) + table(a5, a6) + table(a7,a8) + table(a9, a10) + table(a11, a12) 
seems to be 'always' > 255

Code: Select all

; converted from bb code 
; edited by webmatze 
; turn debugger off for performance.. 

#appWidth = 640 
#appHeight= 480 

Global Dim Palette.l(255) 
Global Dim table.l(#appWidth-1, #appWidth-1) 

Procedure SetupPalette() 
  ; create a temporary image... 
  CreateImage(0, #appWidth, #appHeight) 
  ; ...to draw a nice colour-pattern onto it 
  StartDrawing(ImageOutput(0)) 
    For i = 0 To 63 
     LineXY(i,0,i,200, RGB(0, 0, i*4)) 
    Next 

    For i = 0 To 127 
     LineXY(i+64,0,i+64,200, RGB(0, (i/2)*4, 63*4)) 
    Next 

    For i = 0 To 63 
     LineXY(i+192,0,i+192,200,RGB(i*4, 63*4, 63*4)) 
    Next 
  ; grab palette data 
  For x = 0 To 255 
   Palette(x) = Point(x,0) 
  Next 
  StopDrawing() 
  ; clean-up 
  FreeImage(0) 
EndProcedure 

Procedure PreCalc() 
  For y = 0 To #appHeight-1 
   For x = 0 To #appWidth-1 
    If x = 0 And y = 0 
     table(y,x) = 255 
    Else 
     table(y,x) = (9000000000 / (Sqr(x*x + y*y) * 250000)) 
    EndIf 
   Next 
  Next 
EndProcedure 

If InitSprite()=0 
  End 
EndIf 

OpenWindow(0, 0, 0, #appWidth, #appHeight, "testing...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
OpenWindowedScreen(WindowID(0), 0, 0, #appWidth, #appHeight, 0, 0 ,0) 

SetupPalette() 
PreCalc() 

halfW.l = #appWidth/2 
halfH.l = #appHeight/2 

While WindowEvent()<>#PB_Event_CloseWindow 
  ; fps 
  If GetTickCount_() => zeit + 1000 
    FrameSek = Frames 
    Frames = 0 
    zeit = GetTickCount_() 
  Else 
    Frames + 1 
  EndIf 

  FlipBuffers():Delay(16) 

  cosAlfa.f = Cos (alfa.f) 
  cosAlfam.f = Cos (-alfa) 
  cosAlfa2.f = Cos (alfa*2) 
  cosAlfam2.f = Cos (-alfa*2) 
  sinAlfa.f = Sin (alfa) 
  sinAlfam.f = Sin (-alfa) 
  sinAlfa2.f = Sin (alfa*2) 

  x1.f = 60 * cosAlfa  + 30 * sinAlfam  + halfW 
  y1.f = 30 * cosAlfam2 + 60 * sinAlfa   + halfH 
  x2.f = 30 * cosAlfa    + 60 * sinAlfa2 + halfW 
  y2.f = 60 * cosAlfa    + 30 * sinAlfa   + halfH 
  x3.f = 45 * cosAlfam   + 45 * sinAlfa   + halfW 
  y3.f = 45 * cosAlfa2  + 45 * sinAlfam  + halfH 
  x4.f = 75 * cosAlfa    + 15 * sinAlfa2 + halfW 
  y4.f = 15 * cosAlfam   + 75 * sinAlfa2 + halfH 
  x5.f = 35 * cosAlfa    + 10 * sinAlfa   + halfW 
  y5.f = 10 * cosAlfa2  + 35 * sinAlfam  + halfH 
  x6.f = 40 * cosAlfam   + 30 * sinAlfa2 + halfW 
  y6.f = 40 * cosAlfa    + 10 * sinAlfa   + halfH 

  alfa.f+0.05 

  If StartDrawing(ScreenOutput()) 
    *Screen.LONG = DrawingBuffer() 

    byte_pixel.b = Round((DrawingBufferPixelFormat() + 1)/2, 1) 
    byte_line.l = DrawingBufferPitch() 

    For y = 0 To #appHeight-1 Step 2 
      a1.l = Abs(y1-y) 
      a3.l = Abs(y2-y) 
      a5.l = Abs(y3-y) 
      a7.l = Abs(y4-y) 
      a9.l = Abs(y5-y) 
      a11.l = Abs(y6-y) 
      *screen1.LONG = *Screen.LONG + y*byte_line 
      *screen2.LONG = *screen1.LONG + byte_line 
      For x = 0 To #appWidth-1 Step 2 
          a2.l = Abs(x1-x) 
          a4.l = Abs(x2-x) 
          a6.l = Abs(x3-x) 
          a8.l = Abs(x4-x) 
         a10.l = Abs(x5-x) 
         a12.l = Abs(x6-x) 
        pixel.l = table(a1, a2) + table(a3, a4) + table(a5, a6) + table(a7,a8) + table(a9, a10) + table(a11, a12) 
        If pixel>255:pixel%255:EndIf ; <<<<<<<<<<<<<< CHANGED 
        pixel = palette(pixel) 
        *screen1.LONG\l = pixel 
        *screen1.LONG + byte_pixel 
        *screen1.LONG\l = pixel 
        *screen2.LONG\l = pixel 
        *screen2.LONG + byte_pixel 
        *screen2.LONG\l = pixel 
        *screen1.LONG + byte_pixel 
        *screen2.LONG + byte_pixel 
      Next 
    Next 
    DrawingMode(0) 
    DrawText(5,452,"FPS: "+Str(FrameSek),0) 
    StopDrawing() 
  EndIf 
Wend 

Posted: Fri Nov 28, 2008 11:50 pm
by Psychophanta
Thanks,
allow me to point that:

Code: Select all

If pixel>255:pixel%255:EndIf
has the same effect than just a simple

Code: Select all

pixel%255
.
It just wraps the value between 0 and 255. So then the 'If' is a surplus.

I replaced the 3 lines by:

Code: Select all

pixel.l=palette((table(a1,a2)+table(a3,a4)+table(a5,a6)+table(a7,a8)+table(a9,a10)+table(a11,a12))%255)
But well...
http://www.purebasic.fr/english/viewtopic.php?p=268845 :arrow: