Page 1 of 2

TV-Noise

Posted: Wed Jan 07, 2004 5:17 pm
by benny
Code updated For 5.20+

Hi,

here is a little code-snippet I translated from C to PureBasic. The original C-Code is from Gaffer's TinyPTC.

[EDIT] U*P*D*A*T*E*D [/EDIT]

Code: Select all

;------------------------------------------------------------------------------------
; TV-Noise
; convert from a C-example by Gaffer
; to PureBasic
; by benny! / weltenkonstrukteur.de
; in 2oo4
;
; Thanks to Deeem2031 for the TransformColor-Procedure
;
; !!!!!!!!!!!!!!!!!! DISABLE DEBUGGER !!!!!!!!!!!!!!!!!!!!!

Procedure.l TransformColor(R.b,G.b,b.b)
  Select DrawingBufferPixelFormat()
    Case #PB_PixelFormat_32Bits_RGB
      ProcedureReturn R+G<<8+b<<16
    Case #PB_PixelFormat_32Bits_BGR
      ProcedureReturn b+G<<8+R<<16
    Case #PB_PixelFormat_24Bits_RGB
      ProcedureReturn R+G<<8+b<<16
    Case #PB_PixelFormat_24Bits_BGR
      ProcedureReturn b+G<<8+R<<16
    Case #PB_PixelFormat_16Bits
      ProcedureReturn b>>3+(G&%11111100)<<3+(R&%11111000)<<8
    Case #PB_PixelFormat_15Bits
      ProcedureReturn R&%11111000>>3+(G&%11111000)<<2+(b&%11111000)<<7
  EndSelect
EndProcedure

; For Speed-Optimation - Original by Danilo!

Procedure InitGameTimer()
  ; initialize highres timing function TimeGetTime_()
  Shared _GT_DevCaps.TIMECAPS
  timeGetDevCaps_(_GT_DevCaps,SizeOf(TIMECAPS))
  timeBeginPeriod_(_GT_DevCaps\wPeriodMin)
EndProcedure

Procedure StopGameTimer()
  ; de-initialize highres timing function TimeGetTime_()
  Shared _GT_DevCaps.TIMECAPS
  timeEndPeriod_(_GT_DevCaps\wPeriodMin)
EndProcedure


#SCREEN_X     = 640
#SCREEN_Y     = 480

#LOOPTIME   = 1000/40  ; 40 Frames in 1000ms (1second)

If InitSprite()=0 Or InitMouse()=0 Or InitKeyboard()=0
  MessageRequester("ERROR","Cant init DirectX !",#MB_ICONERROR):End
EndIf

hWnd = OpenWindow(0, 0, 0, #SCREEN_X, #SCREEN_Y, "TV-Noise by benny! / weltenkonstrukteur.de (c) 2oo4", #PB_Window_SystemMenu | #PB_Window_ScreenCentered )

If OpenWindowedScreen(hWnd, 0, 0, #SCREEN_X, #SCREEN_Y, 0, 0, 0) = 0
  ;If OpenScreen(#SCREEN_X, #SCREEN_Y, 32, "j")=0
  MessageRequester("ERROR","Cant open screen !",#MB_ICONERROR):End
EndIf

StartDrawing(ScreenOutput())
Select DrawingBufferPixelFormat()
  Case #PB_PixelFormat_8Bits       ; 1 bytes per pixel, palettized
    PixelStep = 1
  Case #PB_PixelFormat_15Bits      ; 2 bytes per pixel
    PixelStep = 2
  Case #PB_PixelFormat_16Bits      ; 2 bytes per pixel
    PixelStep = 2
  Case #PB_PixelFormat_24Bits_RGB  ; 3 bytes per pixel (RRGGBB)
    PixelStep = 3
  Case #PB_PixelFormat_24Bits_BGR  ; 3 bytes per pixel (BBGGRR)
    PixelStep = 3
  Case #PB_PixelFormat_32Bits_RGB  ; 4 bytes per pixel (RRGGBB)
    PixelStep = 4
  Case #PB_PixelFormat_32Bits_BGR  ; 4 bytes per pixel (BBGGRR)
    PixelStep = 4
EndSelect

Pitch = DrawingBufferPitch()



White = TransformColor(255,255,255)
Noise = 0
Carry = 0
Index = 0
Seed  = $12345
StopDrawing()

InitGameTimer()

While quit=0
  FlipBuffers()
  ExamineMouse()
  ExamineKeyboard()
  If IsScreenActive()
    
    StartDrawing(ScreenOutput())     
    *Screen = DrawingBuffer()
    For y = 1 To #SCREEN_Y
      *Line.LONG = *Screen
      For x = 2 To #SCREEN_X
        noise = seed;
        noise = noise >> 3
        noise = noise ! seed
        carry = noise & 1
        seed = seed >> 1
        seed = seed | ( carry << 30)
        noise = noise & $FF
        *Line\l = (noise<<16) | (noise << 8) | noise
        *Line + PixelStep
      Next x
      *Screen + Pitch
    Next y
    
    StopDrawing()
  EndIf
  
  Repeat
    Event=WindowEvent()
    If Event=#PB_Event_CloseWindow
      quit = 2
    ElseIf Event=0
      While ( timeGetTime_()-LoopTimer )<#LOOPTIME : Delay(1) : Wend
      LoopTimer = timeGetTime_()
    EndIf
  Until Event=0
  
  If KeyboardPushed(#PB_Key_Escape)
    quit = 2
  EndIf
  
  ;  Delay(80)
  
Wend

StopGameTimer()


Posted: Wed Jan 07, 2004 7:04 pm
by J. Baker
Hey, channel 13 don't come in...lol, jk. Nice work. :D

Posted: Sat Jan 10, 2004 10:42 pm
by DriakTravo
Wow! That really eats up my Processor power, here is a good alternative, create sprites first, and then display them randomly, here is a good example:

Code: Select all

InitSprite()
InitKeyboard()

#SCREEN_X     = 640 
#SCREEN_Y     = 480 

If InitSprite()=0 Or InitMouse()=0 Or InitKeyboard()=0 
  MessageRequester("ERROR","Cant init DirectX !",#MB_ICONERROR):End 
EndIf 

hWnd = OpenWindow(0, 0, 0, #SCREEN_X, #SCREEN_Y, #PB_Window_SystemMenu | #PB_Window_ScreenCentered , "TV-Noise by Benny and Driak Travo! / weltenkonstrukteur.de (c) 2oo4") 

If OpenWindowedScreen(hWnd, 0, 0, #SCREEN_X, #SCREEN_Y, 0, 0, 0) = 0 
  MessageRequester("ERROR","Cant open screen !",#MB_ICONERROR):End 
EndIf 


For X = 0 To 10
  ClearScreen(0,0,0)
  StartDrawing(ScreenOutput())
  For Row = 1 To 640 Step 2
    For Col = 1 To 480 Step 2
      color = Random(255)
      Box(Row,Col,2,2,RGB(color,color,color))
    Next Col
  Next Row
  StopDrawing()
  GrabSprite(X,0,0,640,480)
Next X

Repeat
ClearScreen(0,0,0)

If WindowEvent() = #PB_Event_CloseWindow
  End
EndIf

Repeat
  newsprite = Random(10)
Until newsprite <> oldsprite
DisplaySprite(Random(10),0,0)
oldsprite = newsprite

ExamineKeyboard()

If KeyboardPushed(#PB_Key_ALL)
  End
EndIf

FlipBuffers()
ForEver

Posted: Sat Jan 10, 2004 11:11 pm
by benny
@DriakTravo
Have you checked your cpu power ???
Your examples took 99% :roll:
Although I understand what you have tried ... I thought about pre-calculation before ... however I think that realtime looks a bit more realstic.
To gain more free cpu power, just change in my example the line

Code: Select all

Delay(10)
to e.g.

Code: Select all

Delay(80)
Will add a Time-related version soon ...
Anyway ... nice to see your version!

Posted: Sat Jan 10, 2004 11:24 pm
by benny
Here is a more accurate version, which should run at a constant time on all machine. I use procedures and a system which where explained by Danilo in the german forum.

[Got about 35% cpu-power used on my system (amd duron 800mhz)]

Remember to DISABLE DEBUGGER !

[EDIT] ... use updated code from my first post [/EDIT]

Posted: Sat Jan 10, 2004 11:49 pm
by merendo
Cant open screen...

Posted: Sun Jan 11, 2004 12:21 am
by DriakTravo
@DriakTravo
Have you checked your cpu power ???
Your examples took 99%
Although I understand what you have tried ... I thought about pre-calculation before ... however I think that realtime looks a bit more realstic.
To gain more free cpu power, just change in my example the line
Code:

Delay(10)


to e.g.
Code:

Delay(80)



Will add a Time-related version soon ...
Anyway ... nice to see your version!
Thanks! BTW, the screen opens fine on mine

Posted: Fri Feb 11, 2005 8:15 pm
by FloHimself
sorry havent seen this thread before! i've made a purelib out of the tinyptc source: http://www.florian-s.com/download/PureB ... inyPTC.zip

Posted: Fri Feb 11, 2005 8:21 pm
by Fred
BTW Benny, you can take out the DrawingBuffer() call from the inner-loop, it will be even faster.

Posted: Fri Feb 11, 2005 9:20 pm
by benny
@Fred:

You are right ... bad and wasty coding style on my side :!: Thanks for the hint


@flohimself:

n1 :!:

Posted: Fri Feb 11, 2005 9:30 pm
by ebs
FloHimself wrote:sorry havent seen this thread before! i've made a purelib out of the tinyptc source: http://www.florian-s.com/download/PureB ... inyPTC.zip
VERY NICE! Thanks for your effort!

My USD$ 0.02: you can use the constants #Black and #White and eliminate the calls to RGB().

Regards,
Eric

Posted: Sat Feb 12, 2005 3:58 am
by Hroudtwolf
Do ya can implant a tv-noise sound ?
This would be realistic !

Posted: Sat Feb 12, 2005 11:42 am
by benny
@Hroudtwolf:

Hmm ... if you find a nice noise-sample - feel free to change the code and
add it.


@all:

I already (half a year ago) wrapped the effect in a little screensaver ( I think
in this case the noisy sound would be rather disturbing :roll: ).
You can find it here :

http://www.weltenkonstrukteur.de/tv_noise.php

Posted: Sat Feb 12, 2005 1:20 pm
by traumatic
benny: This is OT but I really enjoyed visiting your website!
Clean layout, loading fast and header-images that made me riddle... I like it! :)

Posted: Sat Feb 12, 2005 2:27 pm
by benny
traumatic wrote:benny: This is OT but I really enjoyed visiting your website!
Clean layout, loading fast and header-images that made me riddle... I like it! :)
@traumatic:

I do not consider myself as a webdesigner ... so I am glad to hear that
someone like the new design - thanks, pal !

Still a bit empty (the blog is going to hold various photos and so on ... ) -
but the content will come ;-)