How to make Powerbar?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 694
Joined: Sun Feb 14, 2010 7:11 pm

How to make Powerbar?

Post by Bugala » Sun Apr 04, 2010 1:07 pm

Im trying to figure out a way to make Powerbar to a game.

Idea is, that when you press right mouse button down, that powerbar keeps increasing.

otherwise that powerbar is actually done alrady, but its not done graphically, since im not quite getting how to actually do it.

One way i can do it already, is that i make it single color rectangle and keep stretching/shrinkin it along the way.

However, i would like it to be so, that at bottom color would be for example blue, and higher the bar goes, more red the end of bar would be.

so how do i do this in practice?

Only way i can figure so far is to make rectangle with gradient coloring and make that gradient coloring everytime again.

In example.

Rectangle width being constant 100,, but then height would move from 1 - 100 and then that gradient coloring would move from 15 amount of blue to 1 - 15 amount of red, depending at what part of powerprogress its going.

User avatar
TheMartian
Posts: 54
Joined: Sun Feb 28, 2010 12:51 pm

Re: How to make Powerbar?

Post by TheMartian » Sun Apr 04, 2010 3:50 pm

Hi

I think that the easiest way to implement such a powerbar would be to first create a brush representing the entire bar with the colorsheme you are looking for and then simply copy as much of it from the left (assuming that 100% is to the right) as your 'powerrating' requires to a layer. This layer can then be displayed on top of the powerbars background starting from left. Then for the next update you draw the modified powerbar to another layer, displays it and delete the original powerbar layer. Aa long as you draw the graphics and position the layer before displaying it and deleting the previous layer you should not experience any flicker. Notice that you don't have to update the background as the layers are not drawn to it but sitting on top of it.

regards
Jesper

User avatar
TheMartian
Posts: 54
Joined: Sun Feb 28, 2010 12:51 pm

Re: How to make Powerbar?

Post by TheMartian » Sun Apr 04, 2010 8:17 pm

Hi again

It occurred to me that it might be better to actually come up with some code. So here we go... :P

regards
Jesper



/* This is an example of one approach to creating a powerbar that is colored
using a gradient running from blue (representing 0%) to red (representing 100%)
*/

/*
First we create a brush representing the entire powermeterbar including the
colorchanges as it progresses towards 100%

Just to prove it is there it is displayed. This is of course not done in actual use.
*/

CreateBrush(1,400,30)
SelectBrush(1)
SetFillStyle(#FILLGRADIENT,#LINEAR,#BLUE,#RED,60)
Box(0,0,400,30,#GREEN)
EndSelect()
DisplayBrush(1,100,100)

/*
Next we set up some variables. No attempt has been made to use local variables
and other examples of good programming practice. This is just a quick and dirty example
*/

EnableLayers()
n1$="MyLayer1"
n2$="MyLayer2"
flag=True
runflag=True
x=0

/*
For this example we make a simple yellow box as background for our powermeterbar
*/

SetFillStyle(#FILLCOLOR)
Box(190,190,420,50,#YELLOW)

/*
We need to have a layer ready for removal inside the function, so the next three lines of
code creates one. Alternatively you could check for the existence of a layer each time
or use some other method of not triggering the removal of a layer before it has been created
*/

DisplayBrushPart(1,0,0,200,200,1,1)
SetLayerName(0,n2$)
b=GetAttribute(#LAYER,n2$,#ATTRLAYERID)


/* Here is our event function to update the powermeter */
Function p_UpdatePowerMeter(msg)
Switch msg.action
Case "OnMouseDown":
x=Min(x+5,400)
Case "OnRightMouseDown":
x=Max(x-5,1)
EndSwitch

DisplayBrushPart(1,0,0,200,200,x,30)
If flag
RemoveLayer(b)
Else
RemoveLayer(a)
EndIf
If flag
SetLayerName(0,n1$)
a=GetAttribute(#LAYER,n1$,#ATTRLAYERID)
Else
SetLayerName(0,n2$)
b=GetAttribute(#LAYER,n2$,#ATTRLAYERID)
EndIf
flag=Not flag
EndFunction

InstallEventHandler({OnMouseDown=p_UpdatePowerMeter,OnRightMouseDown=p_UpdatePowerMeter})

While runflag
WaitEvent
Wend
End

User avatar
Tarzin
Posts: 68
Joined: Mon Feb 15, 2010 11:46 am
Location: Dunkerque / FRANCE
Contact:

Re: How to make Powerbar?

Post by Tarzin » Wed Apr 07, 2010 1:31 pm

@TheMartian
Thanks you for your code, it helped me!
Hope to see other complete code released
A500 / A600 / A1200 / SAM440
WinUAE OS3.9 (AmiKit) / OS4.1FE (FlowerPot)
---
https://twitter.com/TarzinCDK

Post Reply