This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
es:orx:tutorials:clock [2009/08/14 09:49 (16 years ago)] – created orgos | es:orx:tutorials:clock [2020/08/19 21:11 (5 years ago)] (current) – Old content sausage | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Clock tutorial ====== | ||
- | ===== Summary ===== | ||
- | |||
- | See the [[Object|object tutorial]] for more info about basic object creation. | ||
- | |||
- | Here we register the processing callback on two different clocks for didactic purposes only. | ||
- | Of course, all objects can be updated from the same clock. ((The given clock context is also used here | ||
- | for demonstration only.)) | ||
- | |||
- | The first clock runs at 0.01s per tick (100 Hz) and the second one at 0.2s per tick (5 Hz). | ||
- | |||
- | If you press the arrow keys up, down and right, you can alter the time stretching of the first clock. | ||
- | It'll still be updated at the same rate, but the time information that the clock will pass to the callback will be stretched. | ||
- | |||
- | This provides an easy way for adding time distortion and having parts of your logic code updated at different frequencies. | ||
- | One clock can have as many registered callbacks as you want, with an optional context parameter. | ||
- | |||
- | For example, the FPS displayed in the top left corner is calculated with a non-stretched clock that runs at 1Hz. | ||
- | |||
- | ===== Details ===== | ||
- | |||
- | When using orx, we don't have to write a global <code c> | ||
- | Instead we create a clock ((or we register to an existing one, such as the core or the physics clock)), specifying its update frequency. | ||
- | |||
- | As we can create as many clocks as we want, we can make sure that the most important part of our logic (player, NPCs, ...) will be updated very often, whereas low priority code will be called once in a while (non-interactive/ | ||
- | For example, physics and render use two different clocks that have different frequencies. | ||
- | |||
- | There is also another big advantage in using separate clocks: we can easily achieve time stretching. | ||
- | |||
- | In this tutorial, we create two clocks, one that runs at 100Hz (period=0.01s) and the other at 5Hz (period=0.2s). | ||
- | |||
- | <code c> | ||
- | |||
- | pstClock1 = orxClock_Create(orx2F(0.01f), | ||
- | |||
- | pstClock2 = orxClock_Create(orx2F(0.2f), | ||
- | |||
- | Note that we gave the type '' | ||
- | Actually, any value higher than this one is valid. The ones lesser than this are reserved for engine internal use. | ||
- | |||
- | Now we'll use the same update callback on both clocks. However, we'll given them different context so that the first clock callback registration applies to our first object, and the second one on the other object: | ||
- | |||
- | <code c> | ||
- | |||
- | orxClock_Register(pstClock2, | ||
- | |||
- | This means our callback will be called 100 times per second with pstObject1 and 5 times per second with pstObject2. | ||
- | |||
- | As our update callback just rotates the object it gets from the context parameter, we'll have, as a result, both objects turning as the same speed. | ||
- | However, the rotation of the second one will be far less smooth (5 Hz) than the first one's (100 Hz). | ||
- | |||
- | Now let's have a look at the callback code itself. | ||
- | |||
- | First thing: we need to get our object from the extra context parameters.\\ | ||
- | As orx is using [[wp> | ||
- | |||
- | <code c> | ||
- | |||
- | If this returns '' | ||
- | |||
- | Our next step will be to apply the rotation to the object. | ||
- | |||
- | <code c> | ||
- | |||
- | We see here that we use the time taken from our clock' | ||
- | That's because all our logic code is wrapped in clocks' | ||
- | |||
- | Of course, there are far better ways of making an object rotate on itself ((by giving it an angular velocity for example, or even by using an '' | ||
- | But let's back to our current matter: clock and time stretching! | ||
- | |||
- | In our update callback, we also polls for active inputs. Inputs are merely character strings that are bound, either in config file or by code at runtime, to key presses, mouse buttons or even joystick buttons. | ||
- | |||
- | In our case, if the up or down arrow keys are pressed, we'll strecthed the time for the first clock that has been created.\\ | ||
- | If left or right arrow keys are pressed, we'll remove the stretching and go back to the original frequency. | ||
- | |||
- | As we didn't store our first created clock ((on purpose, so as to show how to retrieve it)), we need to get it back! | ||
- | |||
- | <code c> | ||
- | |||
- | Specifying '' | ||
- | It'll return the first clock created with the '' | ||
- | |||
- | Now, if the ''" | ||
- | |||
- | <code c> | ||
- | { | ||
- | /* Makes this clock go four time faster */ | ||
- | orxClock_SetModifier(pstClock, | ||
- | }</ | ||
- | |||
- | In the same way we make it 4X slower than originally by changing its modifier when ''" | ||
- | |||
- | <code c>else if(orxInput_IsActive(" | ||
- | { | ||
- | /* Makes this clock go four time slower */ | ||
- | orxClock_SetModifier(pstClock, | ||
- | }</ | ||
- | |||
- | Lastly, we want to set it back to normal, when the ''" | ||
- | |||
- | <code c>else if(orxInput_IsActive(" | ||
- | { | ||
- | /* Removes modifier from this clock */ | ||
- | orxClock_SetModifier(pstClock, | ||
- | }</ | ||
- | |||
- | And here we are! :-)\\ | ||
- | As you can see, time stretching is achieved with a single line of code. As our logic code to rotate our object will use the clock' | ||
- | |||
- | This can be used in the exact same way to slow down monsters while the player will still move as the same pace, for example. | ||
- | There are other clock modifiers type but they' | ||
- | |||
- | |||
- | ===== Resources ===== | ||
- | |||
- | Source code: [[https:// | ||
- | |||
- | Config file: [[https:// |