Code archives/3D Graphics - Misc/Transparent 2D GUI Example
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This method is another which uses 3D sprites, except that it will uses no VRAM up, and it looks completely 2D. These functions aren't meant to be a complete GUI system however, they are just meant to be a base so that you can create your own. The really major limitation of this system is that you can't have any shape of window other than rectangular, but for my purposes anyway, that is fine :-) I will be developing this into a complete GUI system in the future, however I'm afraid that this is the limit of the code I am willing to give out, because it is for my commercial game - Unity. | |||||
;--------------------------; ; -- Unity -- ; ; -- Joseph Humfrey -- ; ; -- phish@aelius.com -- ; ; -- February 2002 -- ; ;--------------------------; ;--------------------- CODE FOR GUI DRAWING --------------------------- ;-- INCLUDED: 3D TRANSPARENT BACKGROUND CODE + 2D DRAWING ------------ ;---------------------------------------------------------------------- ;----------------------------------- ;---- D A T A H A N D L I N G ---- ;----------------------------------- ;GUI distance from the camera ;Change this depending on the scale of your game world Const GUI_Distance# = 1000 ;If you use the entity order for any other stuff, ;you may wish to change this. Otherwise, just leave it. Const GUI_DrawOrder = -1 ;Transparency of the windows Const GUI_Alpha# = 0.9 ;Blend mode of the windows. ;3 is nifty effect, but if your window is ;on top of a white background, and you have ;white text in the window, it will not be readable Const GUI_BlendMode = 3 Type GUI_Window Field entity ;sprite entity for background Field width Field height Field x ;position Field y End Type Global GUI_windowPrototype ;---------------------------- ;---- F U N C T I O N S ----- ;---------------------------- ; -------------------------------------------------------- Function InitialiseGUI() ; -------------------------------------------------------- ;If you want to give your window a background texture, ;put it in here GUI_windowPrototype = CreateSprite() EntityColor GUI_windowPrototype, 20, 20, 50 HideEntity GUI_windowPrototype End Function ; -------------------------------------------------------- Function UpdateGUI() ; -------------------------------------------------------- ;Draw the border around each window Color 100, 100, 125 For w.GUI_Window = Each GUI_Window Line w\x, w\y, w\x+w\width, w\y Line w\x, w\y, w\x, w\y+w\height Line w\x+w\width, w\y, w\x+w\width, w\y+w\height Line w\x, w\y+w\height, w\x+w\width, w\y+w\height Next End Function ; -------------------------------------------------------- Function CreateNewWindow.GUI_window(width, height) ; -------------------------------------------------------- window.GUI_Window = New GUI_Window window\entity = CopyEntity(GUI_windowPrototype, camera) window\width = width window\height = height ;make it drawn after everything else (on top) EntityOrder window\entity, GUI_DrawOrder ;Set the transparency EntityAlpha window\entity, GUI_Alpha ;Set the blend mode EntityBlend window\entity, GUI_BlendMode ;Scale the window, depending on the screen resolution ScaleSprite window\entity, Float(width)/GraphicsWidth()*GUI_Distance, Float(height)/GraphicsWidth()*GUI_Distance ;Move into position - to the origin 0, 0 PositionWindow(window, 0, 0) Return window End Function ; -------------------------------------------------------- Function PositionWindow(window.GUI_window, x#, y#) ; -------------------------------------------------------- ;Store the values passed in window\x = x window\y = y ;Resolution independant coordinates xPos# = (x/GraphicsWidth()) * 2*GUI_Distance - GUI_Distance xWidth# = (Float(window\width)/GraphicsWidth()) * 2*GUI_Distance yDist# = GUI_Distance * 0.75 yPos# = (-y/GraphicsHeight()) * 2*yDist + yDist yHeight# = (Float(window\height)/GraphicsHeight()) * 2*yDist ;Move into position PositionEntity window\entity, (xPos + xWidth/2), (yPos - yHeight/2), GUI_Distance End Function ; -------------------------------------------------------- Function SizeWindow(window.GUI_window, width#, height#) ; -------------------------------------------------------- ;Store the values passed in window\width = width window\height = height ;And resize with respect to the current graphics resolution ScaleSprite window\entity, Float(width)/GraphicsWidth()*GUI_Distance, Float(height)/GraphicsWidth()*GUI_Distance ;We have to put it in the right postion, so that it scales towards ;the top left. Try commenting this line out if you don't understand... PositionWindow(window, window\x, window\y) End Function |
Comments
None.
Code Archives Forum