Gameloop accelerated rendering conflict, time vs frames

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
JohnA
Posts: 3
Joined: Tue Mar 11, 2014 6:18 pm

Gameloop accelerated rendering conflict, time vs frames

Post by JohnA » Mon Jul 28, 2014 2:07 pm

Hi all,
I'm a beginning programmer using livecode (v6.6.1 on Windows) for some time now and like to make games with nice annimations but running into some apparent limitation of options.
At the moment I'm learning to animate and move objects around using the accelerated rendering loop as shown in the gaming acadamy but it seems that all time related commands do not
work or work well while using AccRen.
For exsample "move in time", if I want to move an image or a button over in a line, pattern or over movepoints ass shown here -revolution.byu.edu/animation/motion.php,
all movement is from the startpoint to the end in one go or it won't work or it works badly with stalling of the frames.
As soon as I want to use any "move in time" type of command the game freezes over the set time and then the object jumps to the set endpoint.
Also if I use "wait" in any handler all motion just stops.
With other things like a scrollbarr timer time related commands don't work either but I found my way around that using two handlers and the "send" command.
In short there seems to be a conflict ussing movement by frames (with "set object to point") and movement in time.

My questions are:
Is it possible to make accelerated rendering work with time related commands?
If not is it possible to isolate accelerated rendering to a specific group of objects, for example just the background?
(like a background with moving grass, flowers and a smoking chimney while using "move in time" for a moving character and enemys)
Thanks in advance for any help with these questions and any other thought on this that can help would also be much apreciated.
JohnA

Klaus
Posts: 13871
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Gameloop accelerated rendering conflict, time vs frames

Post by Klaus » Mon Jul 28, 2014 2:55 pm

Hi John,

a look into the dcitionary will help very often! 8)

"acceleratedrendering" is a property of a STACK, so you cannot apply this to any other object.
We need to take a look at your script(s), guessing is ineffective!


Best

Klaus

JohnA
Posts: 3
Joined: Tue Mar 11, 2014 6:18 pm

Re: Gameloop accelerated rendering conflict, time vs frames

Post by JohnA » Sat Aug 02, 2014 1:44 pm

Hi KLaus, thanks for the fast reply!

I had to make time to recreate some of the scripts I've been trying, to show what works and what not.
I don't understand all the terms used in the dictionary at AccRen, compositor is a new term to me, but I do understand that it sets some graphical atributes to a platform optimized setting.
Sorry for the confusion but I got my terms mixed up in the questioning, it's not the accelerated renderring but the gamming loop from the gamming accademy that I think creates a framebased loop.
Honestly I don't fully understand the gameloop itself yet, (it is not explained in the accadamy) I've been checking the explanations in the dictionary and get the most parts of it though.

So the questions should be;
Is it possible to make the gamming loop work with time related commands?
If not why not?
Is there an other way to make a gameloop where you can use the move and time related command?

I've added the gameloopscript (complete stack)
From the cardscript (with comments) I've added the parts which are gameloop related, movement that works and some I tried but don't work ussing move or time related commands.

My goals are to make the enemy move in different patterns and to make the rocket (playercontroled button) move in different ways, for example with a little jump when clicked instead of a straight line up or down if pushed.

Again thanks for replying / any thoughts on this that help.

Greets,
JohnA
--------------------------------------
//Stack script complete

local sStartUpdateTime
local sFrameRate
local sUpdateMessageId

on activateScreenUpdates

stopUpdatingTheScreen
startUpdatingTheScreen 50

end activateScreenUpdates

on startUpdatingTheScreen pFrameRate
put pFrameRate into sFrameRate
put 0 into sStartUpdateTime
send "dispatchUpdateScreen" to me in 0 millisecs
put the result into sUpdateMessageId
end startUpdatingTheScreen

on stopUpdatingTheScreen
if sUpdateMessageId is not empty then
cancel sUpdateMessageId
end if
put empty into sUpdateMessageId
end stopUpdatingTheScreen

on dispatchUpdateScreen
local tThisFrameTime
put the long seconds into tThisFrameTime

if sStartUpdateTime is 0 then
put the long seconds into sStartUpdateTime
end if

lock screen
dispatch "updateScreen" to this card with tThisFrameTime
unlock screen

local tTheTimeNow
put the long seconds into tTheTimeNow

local tNextFrameCount
put round((tTheTimeNow - sStartUpdateTime) * sFrameRate + 0.5) into tNextFrameCount
send "dispatchUpdateScreen" to me in (sStartUpdateTime + (tNextFrameCount * (1 / sFrameRate)) - tTheTimeNow) seconds
put the result into sUpdateMessageId
end dispatchUpdateScreen
-----------------------------------------------------------

//Relevant cardscript

//Some handlers related to the gameloop/ general game

on preOpencard
set the acceleratedRendering of this stack to true
end preOpencard

on startGame

activateScreenUpdates
Clearenemies
ClearPU

if sGamerunning is false then
set the location of btn "Rocket" to 160,300
end if

put true into sGamerunning

end startGame

on stopGame
put false into sGamerunning
end stopGame

on updateScreen

if sGamerunning is true then

moveTerrain
moveRocket
HandleEnemys
DetectCollisions
Updatescore
HandlePUs

end if

end updateScreen

//movement handlers which work ussing the gameloop

on moveRocket

if the mouse is down then
if the top of btn "Rocket" - 10 > 0 then
set the top of btn "Rocket" to (the top of btn "Rocket" - 10)
end if
else
if the bottom of btn "Rocket" - 10 < the bottom of this card then
set the top of btn "Rocket" to (the top of btn "Rocket" + 10)
end if
end if

end moveRocket

on HandleEnemys

if random(100) is 1 then
CreateEnemy
end if

if random(100) is 1 then
CreateEnemyb
end if

if random(100) is 1 then
CreateEnemyc
end if

//(diagonal moving enemy)
repeat for each key tEnemy in sEnemynames
set the right of btn tEnemy to (the right of btn tEnemy - 5)
set the top of btn tEnemy to (the top of btn tEnemy - 1)
if the right of btn tEnemy < 0 then
delete btn tEnemy
delete variable sEnemynames[tEnemy]
end if
end repeat

//(straight moving enemy)
repeat for each key tEnemyb in sEnemynamesb
set the right of btn tEnemyb to (the right of btn tEnemyb - 5)
if the right of btn tEnemyb < 0 then
delete btn tEnemyb
delete variable sEnemynamesb[tEnemyb]
end if
end repeat

//(enemy moving in a (limited) pattern)
repeat for each key tEnemyc in sEnemynamesc
if the right of btn tEnemyc > (0.8 * the width of this card) then
set the right of btn tEnemyc to (the right of btn tEnemyc - 5)
set the top of btn tEnemyc to (the top of btn tEnemyc + 2)
end if
if the right of btn tEnemyc <= (0.8 * the width of this card) then
set the right of btn tEnemyc to (the right of btn tEnemyc - 5)
set the top of btn tEnemyc to (the top of btn tEnemyc - 4)
end if
if the right of btn tEnemyc <= (0.5 * the width of this card) then
set the right of btn tEnemyc to (the right of btn tEnemyc - 5)
set the top of btn tEnemyc to (the top of btn tEnemyc + 8)
end if
if the right of btn tEnemyc < (0.1 * the width of this card) then
set the right of btn tEnemyc to (the right of btn tEnemyc - 5)
end if
if the right of btn tEnemyc < 0 then
delete btn tEnemyc
delete variable sEnemynamesc[tEnemyc]
end if
end repeat

end HandleEnemys

//this handler works with the gameloop to create a timer scrollbar I tried a number of time related commands to create this but nothing worked eventualy I found this option that enabels me to time a powerup for 20 seconds and then stop it using the stopghost handler

on Gtimer1
set the thumbposition of sb "ghostscrollbar" to tghostsec
send "GTimer2" to me in 1 seconds
end Gtimer1

on Gtimer2
if the thumbposition of sb "ghostscrollbar" < 20 then
add 1 to tghostsec
Gtimer1
else
Stopghost
end if
end Gtimer2

//These are handlers I tried but don't work (properly) ussing move or time related commands

on moveRocket
set the bottomleft of graphic "Path" to the loc of btn "Rocket" //- sets a hand drawn curved line graphic in position, does not work for the game because the rocket should not move forward but I wanted to experiment on this.
if the mouse is down then
if the top of btn "Rocket" - 10 > 0 then
move btn "Rocket" to the points of graphic "Path" in 120 ticks without messages //(moves in on go from beginning to end of line instead of (what I wanted) point to point per frame, also time freezes the gameloop for 120 ticks (2 seconds))
else
if the bottom of btn "Rocket" - 10 < the bottom of this card then
set the top of btn "Rocket" to (the top of btn "Rocket" + 5)
end if
end if
end moveRocket

//(another option I tried)

on moveRocket
if the mouseClick is true then // (here I tried to create a little jump with a mouseclick or tap on the sreen)
if the top of btn "Rocket" - 10 > 0 then
set the movespeed to 500
move btn "Rocket" relative 0, -50 in 60 ticks without messages
end if
else
if the bottom of btn "Rocket" - 10 < the bottom of this card then
set the top of btn "Rocket" to (the top of btn "Rocket" + 5)
end if
end if
end moveRocket

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Gameloop accelerated rendering conflict, time vs frames

Post by Newbie4 » Sat Aug 02, 2014 9:02 pm

Here are some suggestions that I have for your game loop...You are welcome to give them a try:

1) I would simplify the game loop and do it yourself instead of using code that you do not understand. First remove all the stack scripts except for
--------------------
on openStack
set the acceleratedRendering of this stack to true
end openStack
--------------------

2) Then create your own game loop by having “updateScreen” send a message to itself. The “updateScreen” becomes your game loop. You can then do everything from that loop. (my changes are marked with *** )
———————————
on preOpencard
*** The following line should go into the “openStack” handler ** instead of here
set the acceleratedRendering of this stack to true
end preOpencard
---------------------
on startGame
activateScreenUpdates
Clearenemies
ClearPU
if sGamerunning is false then
set the location of btn "Rocket" to 160,300
end if
put true into sGamerunning
*** add the following line
updateScreen
end startGame
-------------------------
on updateScreen
if sGamerunning is true then
moveTerrain
moveRocket
HandleEnemys
DetectCollisions
Updatescore
HandlePUs
*** add the following line:
send "updateScreen" to me in 10 millisecs //This is your game loop. Experiment with the number
end if
end updateScreen
------------------
on HandleEnemys

*** // I combined the 3 identical “if” statements into 1 if statement below
if random(100) is 1 then
CreateEnemy
CreateEnemyb
CreateEnemyc
end if
...(the rest of the code is yours...)
end HandleEnemys
----------------------------

3) Optionally, you can have different game loops (handlers that 'call themselves' to run at different intervals) For instance you can take "HandleEnemies" out of the "updateScreen" loop and have it's own loop that runs more often e.g. in "HandleEnemies" add the following lines:
---------------------------
on HandleEnemys
if sGamerunning is true then
... (your current code here)
send "HandleEnemys" to me in 5 millisecs //This will run more often than your other code. Experiment with the number
end if
end HandleEnemies
--------------

If you want to look at simpler examples of games go to http://erhs.shaosean.tk/ - See "Action Games" You can download the scripts and look at the code
If you are looking for some help in writing the code https://sites.google.com/a/pgcps.org/li ... rogramming - See section 3 on game loops

other notes:
1) the move commands, esp the move “relative” and move “along the points…” seem to work slower. Use the “set” command for better response.
2) when you are moving objects on the screen, it might help to add “lock screen” and “unlock screen” around the code
3) You do a number of calculations over and over (e.g. 0.8 * the width of this card, etc). What you can do is do the calc once in the “startGame” handler and put the answer into a variable. then use that variable over and over in your code
4) You will need to cancel pending messages if you go to another level (card)

Have fun and Good Luck
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

JohnA
Posts: 3
Joined: Tue Mar 11, 2014 6:18 pm

Re: Gameloop accelerated rendering conflict, time vs frames

Post by JohnA » Sat Aug 02, 2014 11:20 pm

Thanks for the reply Newbie4, gives me something to try for a while :).
Best,
JohnA

Post Reply

Return to “Games”