BBC BASIC for Windows
Programming >> Graphics and Games >> Fading away
http://bb4w.conforums.com/index.cgi?board=graphics&action=display&num=1443521070

Fading away
Post by KenDown on Sep 29th, 2015, 10:04am

Some time ago I copied, from somewhere, a short program written by Richard which faded from one picture to another. I have used it sparingly in my "Display" program - perhaps one or two fades per presentation.

I am working on a different program and this time use lots of fades. I was horrified to discover that after 7 full-screen fades (and my screens are 1920x1080) the fade function just stopped working!

Eventually I tracked the problem down to a missing command. Whether I missed it out when I copied the program or it is missing for some other reason, once it was inserted the fade program appears - as far as I have tested it - to be fixed.

After the line
SYS"DeleteObject",himage%TOdelete%
you must insert
SYS"DeleteDC",hdc%

Apparently DeleteObject on its own will not delete an object that has been turned into a DC (whatever a DC is).

Re: Fading away
Post by DDRM on Sep 29th, 2015, 11:47am

Hi Ken,

DC is a device context - "a structure that defines a set of graphic objects and their associated attributes, as well as the graphic modes that affect output", according MSDN

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183553%28v=vs.85%29.aspx

I can imagine that if you don't delete such a structure once you have finished using it, you may eventually run out of space, but I don't know if that is the problem in your case.

Is it possible to use the same DC for successive fades, by selecting a new bitmap into it?

Best wishes,

D
Re: Fading away
Post by KenDown on Sep 29th, 2015, 6:19pm

Thanks for the explanation.

My experience would seem to indicate that you can't. Without the deletion the fade stopped working. I mentioned that it stopped after 7 full-screen fades. With the program I'm working on the screens were a mere 9th of the full-screen, so I got up to 74 fades before it stopped. I ran it several times and it always stopped at the same number, so yes, I think it was a memory problem.
Re: Fading away
Post by DDRM on Sep 30th, 2015, 4:06pm

Hi Ken,

Here's some basic, if working, code, showing that you can re-use the same device context/bitmap for successive images. I hope I've annotated it enough to make sense.

Basically, you choose the first jpeg from a folder full of them, and it will then plough through the rest, fading from one to the next. First we set up a device context (and associated bitmap) to match the BB4W output window, then we load the images into it. AlphaBlend does the actual magic, copying the image from the "spare" copy into the output window, at ever increasing alpha values (0 = transparent, 255 = solid).

Most of the code is just clumsy ways of getting the file names - so if you have these already you can ditch it.

Does that help with what you want to do?

Best wishes,

D

Edit: Note that I haven't included routines to tidy up by releasing the bitmap and DC when the program finishes, which I should have done - I think it will automatically get released when the program exits, but might not if you ran it repeatedly without restarting.

Code:
      MODE 21 :REM 800 x 600
      xpos%=0
      ypos%=0
      xsize%=@vdu%!208
      ysize%=@vdu%!212

      REM We make a "spare" device context and bitmap the same size as the display
      PROCMakeSpareBitmap(xsize%,ysize%)
      REM AlphaBlend isn't one of the functions we can get by name, so need to find and load it
      SYS "LoadLibrary", "Msimg32.DLL" TO msim%
      SYS "GetProcAddress", msim%, "AlphaBlend" TO alphablend%
      IF alphablend%=0 ERROR 100, "Could not get address of AlphaBlend"

      REM Choose the first jpeg in a folder manually
      picture$=FNGetFileName
      IF picture$<>"" THEN
        REPEAT
          REM We load the chosen picture into our "spare" DC and bitmap
          PROCLoadPictoNewDC(picture$,xpos%,ypos%,xsize%,ysize%)
          FOR x%=1 TO 8
            REM Now we are going to blend it into the screen, at ever increasing densities (range 0-255)
            density%=((2^x%)-1)<<16
            SYS alphablend%,@memhdc%,0,0,xsize%,ysize%,newdc%,0,0,xsize%,ysize%,density%
            REM Tell windows to update the picture
            SYS "InvalidateRect", @hwnd%, 0, 0
            SYS "UpdateWindow", @hwnd%
            WAIT 5 :REM Adjust to give the speed of fade you want
          NEXT x%
        ENDIF
        WAIT 20
        REM Now we'll look automatically for the next jpeg in that folder, and start the whole thing again
        picture$=FNlistdirectory
      UNTIL picture$=""
      ENDIF
      END
      :
      DEFPROCMakeSpareBitmap(xsize%,ysize%)
      SYS "CreateCompatibleDC",@memhdc% TO newdc%
      IF newdc%=0 THEN PRINT "Failed to make DC":END
      SYS "CreateCompatibleBitmap",@memhdc%,xsize%,ysize% TO newbmp%
      IF newbmp%=0  THEN PRINT "Failed to make bmp":END
      SYS "SelectObject",newdc%,newbmp%
      ENDPROC
      :
      DEF PROCLoadPictoNewDC(picture$,xpos%,ypos%,xsize%,ysize%)
      REM This is just PROCdisplay from the manual, adapted to load the image into the spare device context/bitmap
      LOCAL oleaut32%, olpp%, iid%, gpp%, hmw%, hmh%, picture%, res%
      SYS "LoadLibrary", "OLEAUT32.DLL" TO oleaut32%
      SYS "GetProcAddress", oleaut32%, "OleLoadPicturePath" TO olpp%
      IF olpp%=0 ERROR 100, "Could not get address of OleLoadPicturePath"
      DIM iid% LOCAL 15, picture% LOCAL 513
      SYS "MultiByteToWideChar", 0, 0, picture$, -1, picture%, 256
      iid%!0  = &7BF80980
      iid%!4  = &101ABF32
      iid%!8  = &AA00BB8B
      iid%!12 = &AB0C3000
      SYS olpp%, picture%, 0, 0, 0, iid%, ^gpp%
      IF gpp% = 0 ERROR 100, "OleLoadPicturePath failed"
      SYS !(!gpp%+24), gpp%, ^hmw% : REM. IPicture::get_Width
      SYS !(!gpp%+28), gpp%, ^hmh% : REM. IPicture::get_Height
      SYS !(!gpp%+32), gpp%, newdc%, xpos%, ypos%, xsize%, ysize%, 0, \
      \                      hmh%, hmw%, -hmh%, 0 TO res%
      IF res% ERROR 100, "IPicture::Render failed"
      SYS !(!gpp%+8), gpp% : REM. IPicture::Release
      ENDPROC
      :
      DEFFNGetFileName
      REM Basically the routine from the manual made into a function returning the filename
      LOCAL fs{},fp,ff$,filename$
      DIM fs{lStructSize%, hwndOwner%, hInstance%, lpstrFilter%, \
      \      lpstrCustomFilter%, nMaxCustFilter%, nFilterIndex%, \
      \      lpstrFile%, nMaxFile%, lpstrFileTitle%, \
      \      nMaxFileTitle%, lpstrInitialDir%, lpstrTitle%, \
      \      flags%, nFileOffset{l&,h&}, nFileExtension{l&,h&}, \
      \      lpstrDefExt%, lCustData%, lpfnHook%, lpTemplateName%}
      DIM fp{t&(260)}
      REM ff$ = "BMP files"+CHR$0+"*.BMP"+CHR$0+CHR$0
      fs.lStructSize% = DIM(fs{})
      fs.hwndOwner% = @hwnd%
      fs.lpstrFilter% = !^ff$
      fs.lpstrFile% = fp{}
      fs.nMaxFile% = 260
      fs.flags% = 6
      SYS "GetOpenFileName", fs{} TO result%
      IF result% filename$ = $$fp{}  ELSE filename$=""
      =filename$
      :
      DEF FNlistdirectory
      REM Basically the routine in the manual, adapted to give the next jpeg in the directory
      PRIVATE dir%, res%,cdir$,cdirbuff%
      PRIVATE sh%
      IF sh%=0 THEN
      DIM dir% LOCAL 317
      DIM cdirbuff% LOCAL 300
      SYS "GetCurrentDirectory",300,cdirbuff%
      cdir$=$$cdirbuff%+"\"
      SYS "FindFirstFile", "*.jpg", dir% TO sh%
      = cdir$+$$(dir%+44)
      ELSE
      SYS "FindNextFile", sh%, dir% TO res%
      IF res%=0 THEN =""
      = cdir$+$$(dir%+44)
      ENDIF
      =""