You are not logged in or registered. Please login or register to use the full functionality of this board...
SIGN IN Join Our Community For FREE


UFO In Space with Fading Tail ver 2.0
11-15-2016, 12:16 AM (This post was last modified: 11-15-2016 12:31 AM by Waltersmind.)
Post: #1
 (Print Post)
UFO In Space with Fading Tail ver 2.0
To help speed the rendering up and to lower the CPU usage of the demo at higher frame rates, I decided to use QB64's hardware acceleration on my UFO in Space with Fading Tail demo.

The issue that QB64 has so far, without getting into OpenGL specific keywords, is that you can not alpha fade a hardware image. All alpha transparency must be done before the image is copied into a hardware image, or before it is loaded as one. The only solution I could come up with was to load the UFO image, use the _SETALPHA keyword to set the transparency for the image, and copy that image into a new hardware image. I only needed to load the image once, but copy it many times.

This could take up a good amount of memory depending on how large the image is, and how many copies you make of it, however the memory usage is nominal in this demo.

This demo runs a 60 fps (Frames Per Second) with the 30 UFO's I used, but I could do so many more without slowing it down.

Overall, this is a graphical demo that shows how to use hardware accelerated graphics with transparency. It is so simple but very powerful, and is well documented.

Here is the code if you want to look at it without downloading the attached *.ZIP file:

Code Snippet: [Select]
' #############################################################################
' #                                                                           #
' #  UFO in space with a tail that fades out.                                 #
' #                                                                           #
' #  Created By:  Walter Whitman                                              #
' #               a.k.a. Waltersmind and The Joyful Programmer                #
' #                                                                           #
' #  Date:  11/14/2016                                                        #
' #                                                                           #
' #  This demo on uses hardware acceleration rending to accomplish the        #
' #  animation.                                                               #
' #                                                                           #
' #############################################################################

CONST Radian = 3.141592654 / 180


TYPE UFOS
   x AS INTEGER
   y AS INTEGER
   UFOimage AS LONG
END TYPE


REDIM SHARED UFO(-1) AS UFOS

DIM WindowWidth AS _UNSIGNED INTEGER
DIM WindowHeight AS _UNSIGNED INTEGER
DIM WindowTopLeftX AS _UNSIGNED INTEGER
DIM WindowTopLeftY AS _UNSIGNED INTEGER

DIM NumOfUFOs AS _UNSIGNED LONG
DIM UFOimage AS LONG

DIM MainWindow AS LONG
DIM SHARED BackgroundImage AS LONG
DIM SHARED FastSin(360) AS DOUBLE
DIM SHARED FastCos(360) AS DOUBLE
DIM SteppingSpeed AS _UNSIGNED _BYTE

DesktopImage& = _SCREENIMAGE

WindowWidth = _WIDTH(DesktopImage& - 25
WindowHeight = _HEIGHT(DesktopImage& - 90
WindowTopLeftX = (_WIDTH(DesktopImage& - WindowWidth) / 2 - 3
WindowTopLeftY = (_HEIGHT(DesktopImage& - WindowHeight) / 2 - 35


MainWindow = _NEWIMAGE(WindowWidth, WindowHeight, 32)


'  TRY 3 HERE TO GET A LITTLE MORE REALISTIC LOOK
NumOfUFOs = 30


'  AN OLD HACK TO REMOVE CONSTANT CALCULATIONS OF COMMON SIN & COS VALUES.
'  WE DO NOT NEED A HIGH-RESOLUTION COS/SIN TABLE HERE.
FOR Angle = 0 TO 359
   FastSin(Angle) = SIN(Angle * Radian)
   FastCos(Angle) = COS(Angle * Radian)
NEXT

'  THIS VALUE TELL THE APP HOW MANY TIMES TO CALCULATE THE NEXT (X,Y) COORDINATES. THIS ALLOWS THE
'  UFO IMAGE TO APPEAR TO SPEED UP A BIT. I COULD OF USE SOME MATH INSTEAD OF A LOOP, BUT I WAS IN
'  A HURRY TO GET THIS POSTED.
SteppingSpeed = 2


SCREEN MainWindow
_SCREENMOVE WindowTopLeftX, WindowTopLeftY

_TITLE "UFO In Space with Fading Tail (Using Hardware Acceleration) - By: Waltersmind @ TheJoyfulProgrammer.com"

REDIM UFO(NumOfUFOs - 1) AS UFOS


'  Background image is courtesy of NASA.gov.
'  Image URL: http://www.nasa.gov/image-feature/goddard/2016/hubble-peers-into-the-storm
BackgroundImage = _LOADIMAGE("Background_01.jpg", 33)


'  UFO image was created by Waltersmind for The Joyful Programmer
'  IN THIS DEMO, WE WILL LOAD THE UFO IMAGE INTO THE FIRST UFO INDEX AND JUST COPY IT TO THE OTHERS.
'  THIS WILL KEEP US FROM LOADING IT AND THEN FREEING IT AFTER IT IS PUT IN THE OTHER UFO ARRAY INDEXES.
UFOimage = _LOADIMAGE("UFO-157x87.png", 32)


'  THIS DEMO USES THE BOWDITCH / LISSAJOUS CURVE
PendulumVerticalAngle = 0
PendulumHorizontalAngle = 0


'  DEFINE THE DATA FOR THE FIRST UFO.
UFO(0).x = WindowWidth / 2 + WindowWidth * SIN(PendulumHorizontalAngle * Radian) * COS(PendulumHorizontalAngle * Radian)
UFO(0).y = WindowHeight / 2 + (WindowHeight - 100) * SIN(PendulumVerticalAngle * Radian)
_SETALPHA (255 / (UBOUND(UFO) + 2)) * 2, _RGBA32(0, 0, 0, 1) TO _RGBA32(255, 255, 255, 255), UFOimage
UFO(0).UFOimage = _COPYIMAGE(UFOimage, 33)


'  DEFINE THE DATA FOR ALL THE REST OF THE UFO'S. ALL WE NEED TO DO IS COPY THE (X,Y) COORDINATES AND THE IMAGE
'  USED IN THE FIRST UFO INDEX.
FOR i = 1 TO UBOUND(UFO)
   UFO(i).x = UFO(0).x
   UFO(i).y = UFO(0).y
   _SETALPHA (255 / (UBOUND(UFO) + 2)) * (i + 2), _RGBA32(0, 0, 0, 1) TO _RGBA32(255, 255, 255, 255), UFOimage
   UFO(i).UFOimage = _COPYIMAGE(UFOimage, 33)
NEXT

'  RELEASE THE MEMORY USED BY THE UFOimage IMAGE AS WE NO LONGER NEED IT
_FREEIMAGE UFOimage


DO

   _LIMIT 60


   '  TURN OFF ALL ERROR CHECKING AND EVENTS (tell QB64 not to add such code to final executable)
   $CHECKING:OFF


   '  DRAW THE BACKGROUND IMAGE
   _PUTIMAGE (0, 0)-(WindowWidth - 1, WindowHeight - 1), BackgroundImage, 0, (0, 0)-(_WIDTH(BackgroundImage) - 1, _HEIGHT(BackgroundImage) - 1)


   '  DRAW ALL UFO'S
   FOR i = 0 TO UBOUND(ufo)
       '_SETALPHA UFO(i).imageAlpha, _RGBA32(0, 0, 0, 1) TO _RGBA32(255, 255, 255, 255), UFOimage
       _PUTIMAGE (UFO(i).x - (_WIDTH(UFO(i).UFOimage) / 2), UFO(i).y - (_HEIGHT(UFO(i).UFOimage) / 2)), UFO(i).UFOimage, 0
   NEXT


   '  SINCE EACH UFO UP TO THE ONE BEFORE THE LAST JUST TAKES THE NEXT UFO (X,Y) COORDINATES,
   '  ALL WE NEED TO COPY THE NEXT ONE TO THE CURRENT ONE.
   FOR i = 0 TO UBOUND(ufo) - 1
       UFO(i).x = UFO(i + 1).x
       UFO(i).y = UFO(i + 1).y
   NEXT


   '  CALCULATE THE LAST UFO WITH ALL NEW (X,Y) COORDINATES
   UFO(UBOUND(ufo)).x = WindowWidth / 2 + WindowWidth * SIN(PendulumHorizontalAngle * Radian) * COS(PendulumHorizontalAngle * Radian)
   UFO(UBOUND(ufo)).y = WindowHeight / 2 + (WindowHeight / 2) * SIN(PendulumVerticalAngle * Radian)


   '  A HACK TO SKIP SO MANY STEPS IN OUR ANIMATION. IT HELPS SHOW THE TAIL BETTER
   FOR i = 1 TO SteppingSpeed
       PendulumHorizontalAngle = 1.000013 * PendulumHorizontalAngle + 1.01 '1.0000001# *
       PendulumVerticalAngle = PendulumVerticalAngle + 1
   NEXT


   '  TURN ERROR CHECKING AND EVENTS BACK ON SINCE WE NEED THEM NOW
   $CHECKING:ON


   _DISPLAY


   '  READ THE KEYBOARD TO SEE IF ANY KEY HAS BEEN PRESSED
   k = _KEYHIT


   '  IF THE <ESC> KEY IS PRESSED THEN CLOSED THE PROJECT
   IF k = 27 THEN

       '  FREE BACKGROUND IMAGE
       _FREEIMAGE (BackgroundImage)

       '  EXIT PROJECT TO SYSTEM
       SYSTEM

   END IF

LOOP


Here is a screenshot:

   

My Concerns:

The main problem I can see with setting the alpha transparency manually for an image (in code) is that you loose pre-existing semi-transparent data. Looking at the screen shot above, you can not see behind the glass window of the first UFO. That alpha information was removed. If you look at the original image before it uses the _SETALPHA keyword, you can see the glass dome is semi-transparent. This is fine since it is a simple hardware accelerated graphic demo, but it wouldn't be fine for a real game production.

The best way around that issue is to create a sprite sheet with all the UFO sprites that fade from full opaque to nearly transparent. In a real game production you wouldn't as many UFO images as I am using (unless you wanted them). Normally, 4-5 sprites in the sprite sheet would be sufficient enough to fool the eyes into thinking it is seeing an object fading away.

One final note, it would be nice if QB64 could fade an image correctly, but since it can't we have to deal with work-arounds which make our code larger and memory usage more.

Have fun until next time...


Walter Whitman
The Joyful Programmer


Attached File(s)
.zip  UFO_In_Space_With_Tail2.zip (Size: 223.24 KB / Downloads: 12)

My goal is to bring joy, excitement, fun and education to all computer programming hobbyists, tinkerers, and amateurs. I also enjoy helping and working with those who aspire at becoming masters of their craft.
Find all posts by this user
Like Post



Forum Jump:


User(s) browsing this thread: 1 Guest(s)




QB64 Member Project - Red Scrolling LED Sign
QB64 Member Project - Othello
QB64 Member Project - Qubic
QB64 Member Project - Basic Dithering
QB64 Member Project - Touche
QB64 Member Project - Input
QB64 Member Project - Foursight
QB64 Member Project - Pivet version one
QB64 Member Project - Dakapo
QB64 Member Project - Quarto
QB64 Member Project - Kings Vallery version two
QB64 Member Project - 9 Board
QB64 Member Project - Algeria Weather
QB64 Member Project - STxAxTIC 3D World
QB64 Member Project - Line Thickness
QB64 Member Project - Color Rotating Text
QB64 Member Project - Connect Four
QB64 Member Project - Swirl
QB64 Member Project - Martin Fractals version two
QB64 Member Project - Dreamy Clock
QB64 Member Project - Sabotage
QB64 Member Project - Score 4
QB64 Member Project - Kings Valley verion one
QB64 Member Project - Full Color LED Sign
QB64 Member Project - Domain
QB64 Member Project - Blokus
QB64 Member Project - Spinning Color Wheel
QB64 Member Project - Amazon
QB64 Member Project - Kobolts Monopoly
QB64 Member Project - ARB Checkers
QB64 Member Project - Kings Court
QB64 Member Project - Point Blank
QB64 Member Project - Bowditch curve
QB64 Member Project - Color Triangles
QB64 Member Project - Martin Fractals version four
QB64 Member Project - Martin Fractals version one
QB64 Member Project - Rotating Background
QB64 Member Project - Splatter
QB64 Member Project - Overboard
QB64 Member Project - RGB Color Wheel
QB64 Member Project - Isolation
QB64 Member Project - Pivot version two
QB64 Member Project - MAPTRIANGLE
QB64 Member Project - Martin Fractals version three
QB64 Member Project - OpenGL Triangles
QB64 Member Project - Exit
QB64 Member Project - Inside Moves
QB64 Member Project - Spiro Roses
QB64 Member Project - Rubix's Magic