Graphics refresh - best practise?

BlitzMax Forums/BlitzMax Beginners Area/Graphics refresh - best practise?

Qweeg(Posted 2005) [#1]
What is the best way to include independant graphic updates in a program? What I mean is I have a main game loop that has a flip and cls and updates the graphics nicely each loop. I then want to include a graphical effect in certain situations - in this case an image that animates in some way (in my case it appears on the screen gets larger and then disappears).

I can get the image to animate in this way in an isolated test case using a loop which redraws the image in each iteration. But this loop has a flip and cls in it. So I can't embed this bit of code in my main program since it causes all of the other graphics on the screen go screwy, as there would now be 2 flips and cls's. I can't just remove this flip, as the special effect doesn't change quickly enough.

So I guess my questions is can I in some way update some of the graphics on the screen without doing anything to any other grapics? Ors it just a case of structuring the code in some way to get this to work - in which case is there some kind of best practise document.

Any help would be much appreciated.


LarsG(Posted 2005) [#2]
try to stick your (isolated) animation loop in a function.
remove the loop itself, and also remove the cls and flip commands from the function.

add a main loop where you call the function, something like this (pseudo code):
repeat
  cls()
  update_anim()
  ' this is where you can update and draw other game graphics..
  flip()
  flushmem()
until keyhit(KEY_ESCAPE)

function update_anim()
  set alphablend and scale
  drawimage
end function


[edit]
oh.. and a better way if doing this is making an animation type with timer values and frame counter etc.. but that might be abit much to chew over right now
[/edit]


tonyg(Posted 2005) [#3]
I don't completely follow but it sounds like you need an
animation frame timer for your special effect.
Basically, rather than updating the animframe every cycle
you either update every nth cycle or every n millisecs.


amonite(Posted 2005) [#4]
if i understand correctly what you mean which i am not very sure about,
you say :
So I guess my questions is can I in some way update some of the graphics on the screen without doing anything to any other grapics?


1st thing that comes to my mind is embed your command within 2 commands like this:

local alpha# = .25
Repeat
SetAlpha ALPHABLEND
SetAlpha alpha
       drawimage yourIMG,0,0,0

SetAlpha 1
Until KeyHit[any_key]
'same goes for scale :)


should not affect other gfx within your loop


QuietBloke(Posted 2005) [#5]
Hi Qweeg,

Its a little hard to understand exactly what you are trying to achieve. You already have a main loop where you redraw the display the graphics each frame.
The special effect is just going to be another sprite within the game with its own logic which in this case is that it expands up to a certain point then is removed.


Qweeg(Posted 2005) [#6]
Yep - this was a rubbish way of trying to get my problem across - apologies to all. I think I just have badly structured code and too much is happening in each iteration of the main loop.

What was happening was that originally all the graphics refreshed each iteration of the main loop, but the main loop is too slow to make my special effect work properly, so the special effect had a jerky look to it.

So what I wanted to do was to somehow have a loop within the main loop that handled the special effect idependantly - something like...

	For Local i# = 0.1 To 1 Step 0.01 
		
		varScale :+ i# / 500
		varAlpha :+ i# / 50
		tBonusFoul.ScaleAndFade(varScale,varAlpha)
		
		Flip
		Cls
		
	Next


...where the ScaleAndFade method redraws the bonus image using the new new scale and alpha.

But having the flip and cls in this inner loop (as well as in the outer main loop) mucks up all of the rest of the graphics since they haven't been drawn to the back screen.

So my issue really related to having multiple Flips and Cls's in the code.

I guess my poorly structured code really needs to be amended so that in the main loop, if the bonus is being displayed, only this - and the other draw commands should be executed in that iteration.

So then I thought well what is the best way of doing this? How do I make sure that the main loop only executes the minimum amount of code and still draws all the images to the screen; and is there a 'best practise way of coding this?

Should there be a call to a function within this example code above - where the function draws all the other required items to the screen? This would prevent all the rest of the main loop having to execute and should speed the special effect 'animation' up?

Anyway - I think maybe I need to look at how my code is structured (I have spent so much time trying to get a handle on the syntax for this new - to me - language, that I have not paid any attention to how I am structuring my code, which is probably where the problem lies).

Hopefully this makes things a little clearer to everyone, and apologies again to those who have tried to struggle vainly through my ramblings in an effort to help me out.


QuietBloke(Posted 2005) [#7]
maybe a good place to look would be in the blitzmax tutorials forum. There are plenty of examples in there. Recently I saw one on how to write an asteriods game in about a hour. Taking a look at how that is written might be a good place to start.