January 26, 2021, 06:16:28 AM

Author Topic: [bb] fade in/out image by Ross C [ 1+ years ago ]  (Read 456 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] fade in/out image by Ross C [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : fade in/out image
Author : Ross C
Posted : 1+ years ago

Description : this code will fade in/out an image to any backround so long as the backround underneath the image is static. the static part of the backround only needs to the same size as the image.

Code :
Code: BlitzBasic
  1. SetBuffer BackBuffer()
  2.  
  3. x=100:y=100
  4. fade=2; set fade to 2 so that nothing happens
  5. alpha#=0; alpha to zero and make a float number
  6. backround=LoadImage("final.jpg"); replace these images
  7. image=LoadImage("grass.jpg"); with two of your own
  8. ;these are the arrays to store image color information
  9. Dim ir#(ImageWidth(image),ImageHeight(image));use arrays to store color info
  10. Dim ig#(ImageWidth(image),ImageHeight(image));as it is much faster than constantly reading it
  11. Dim ib#(ImageWidth(image),ImageHeight(image));from the screen
  12.  
  13. ;these are the arrays to store backround color information
  14. Dim r#(ImageWidth(image),ImageHeight(image))
  15. Dim g#(ImageWidth(image),ImageHeight(image))
  16. Dim b#(ImageWidth(image),ImageHeight(image))
  17.  
  18. tempr#=0;temp red, green and blue value
  19. tempg#=0
  20. tempb#=0
  21.  
  22. SetBuffer ImageBuffer(image); set the buffer to the image being faded in or out
  23. For loop=1 To ImageWidth(image); set loop so that it is the same width as the image
  24.         For loop1=1 To ImageHeight(image); set loop so that it is the same height as the image
  25.                 rgb=ReadPixel(loop,loop1); read the pixel color from the image
  26.                 ir(loop,loop1)=(rgb And $FF0000)/$10000; seperate the red
  27.                 ig(loop,loop1)=(rgb And $FF00)/$100;green
  28.                 ib(loop,loop1)=rgb And $FF;and blue color information
  29.         Next
  30. Next
  31. SetBuffer BackBuffer(); set the buffer to the backbuffer
  32.  
  33. While Not KeyHit(1)
  34. Cls
  35.  
  36. If KeyDown(200) Then y=y-1; to move image up
  37. If KeyDown(208) Then y=y+1; and down
  38. If KeyHit(3) Then fade=0:alpha=0; set fade to zero and alpha to zero then the image wil fade in
  39. If KeyHit(4) Then fade=1:alpha=1;set fade to 1 and alpha to 1 and the image will fade out
  40. TileImage backround,0,0; the backround MUST be drawn before the color info is taken or it will
  41.                                            ; appear black.
  42. If KeyHit(2) Then; this MUST be called prior to fading an image. grabs screen color information
  43.                                 Gosub getbackroundinfo; from under the image
  44. End If
  45.  
  46.  
  47.  
  48. If fade<>2 Then Gosub updatefade; if fade has been set then goto the fade in/out routine
  49.  
  50. Text 0,0,"PRESS 1 TO GRAB COLOR INFO. 2 TO FADE IN IMAGE. 3 TO FADE OUT IMAGE
  51.  
  52. Flip
  53. Wend
  54.  
  55. .updatefade
  56. ;when alpha=1 then image is fully visible
  57. If fade=0 Then alpha=alpha+0.1:If alpha>1 Then alpha=1:DrawImage image,x,y:Return
  58. If fade=1 Then alpha=alpha-0.1:If alpha<-0 Then alpha=0:Return
  59. LockBuffer BackBuffer()
  60. For loop=1 To ImageWidth(image)-1
  61.  
  62.         For loop1=1 To ImageHeight(image)-1
  63.                 ;fade the red part of the color into the backround red color
  64.                 tempr=Int(r(loop,loop1) + (  ( ir(loop,loop1)-r(loop,loop1) )*alpha  ))
  65.                 ;fade the blue part of the color into the backround blue color
  66.                 tempg=Int(g(loop,loop1) + (  ( ig(loop,loop1)-g(loop,loop1) )*alpha  ))
  67.                 ;fade the blue part of the color into the backround blue color
  68.                 tempb=Int(b(loop,loop1) + (  ( ib(loop,loop1)-b(loop,loop1) )*alpha  ))
  69.                
  70.                 rgb=tempr*65536+tempg*256+tempb; combine the red,green and blue together
  71.                 WritePixelFast x+loop,y+loop1,rgb; write the color info to the screen
  72.         Next
  73. Next
  74. UnlockBuffer BackBuffer()
  75. Return
  76.  
  77. .getbackroundinfo; get the color info from below the image
  78. SetBuffer FrontBuffer()
  79. For loop=1 To ImageWidth(image)
  80.         For loop1=1 To ImageHeight(image)
  81.                 rgb=ReadPixel(x+loop,y+loop1)
  82.                 r(loop,loop1)=(rgb And $FF0000)/$10000;separate out the red
  83.                 g(loop,loop1)=(rgb And $FF00)/$100;green
  84.                 b(loop,loop1)=rgb And $FF;and blue poarts of the color
  85.         Next
  86. Next
  87. SetBuffer BackBuffer(); set buffer back to the back buffer
  88. Return


Comments :


General McHonkadonk(Posted 1+ years ago)

 Oh my god. Thank you SOOOOOO MUCH!!!!!!!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal