TV-Noise

Share your advanced PureBasic knowledge/code with the community.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

TV-Noise

Post 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()

Last edited by benny on Sat Feb 12, 2005 3:18 pm, edited 1 time in total.
regards,
benny!
-
pe0ple ar3 str4nge!!!
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Hey, channel 13 don't come in...lol, jk. Nice work. :D
DriakTravo
Enthusiast
Enthusiast
Posts: 346
Joined: Fri Oct 10, 2003 12:42 am
Location: Tampa,FL,USA
Contact:

Post 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
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post 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!
regards,
benny!
-
pe0ple ar3 str4nge!!!
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post 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]
Last edited by benny on Sat Feb 12, 2005 3:19 pm, edited 1 time in total.
regards,
benny!
-
pe0ple ar3 str4nge!!!
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Post by merendo »

Cant open screen...
The truth is never confined to a single number - especially scientific truth!
DriakTravo
Enthusiast
Enthusiast
Posts: 346
Joined: Fri Oct 10, 2003 12:42 am
Location: Tampa,FL,USA
Contact:

Post 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
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post 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
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

BTW Benny, you can take out the DrawingBuffer() call from the inner-loop, it will be even faster.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@Fred:

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


@flohimself:

n1 :!:
regards,
benny!
-
pe0ple ar3 str4nge!!!
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Do ya can implant a tv-noise sound ?
This would be realistic !
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post 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
regards,
benny!
-
pe0ple ar3 str4nge!!!
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post 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! :)
Good programmers don't comment their code. It was hard to write, should be hard to read.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post 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 ;-)
regards,
benny!
-
pe0ple ar3 str4nge!!!
Post Reply