This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
es:orx:tutorials:clock [2009/08/23 16:17 (16 years ago)] – external edit 127.0.0.1 | es:orx:tutorials:clock [2020/08/19 21:11 (5 years ago)] (current) – Old content sausage | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Tutorial de Reloj ====== | ||
- | ===== Resumen ===== | ||
- | |||
- | Vea el [[Object|Tutorial de Objeto]] para más información sobre la creación básica de un objeto. | ||
- | |||
- | Aquí vamos a registrar el proceso de llamada de retorno en dos relojes diferentes solo con propósitos didácticos. | ||
- | Todos los objetos son actualizados desde el mismo reloj. ((El contexto del reloj es además usado aquí solo para demostración)) | ||
- | |||
- | El primer reloj corre a 0.01s por tictac (100 Hz) y el segundo corre a 0.2s por tictac (5 Hz). | ||
- | |||
- | Sí presionas las teclas ARRIBA, ABAJO y DERECHA, podrás alterar el tiempo del primer reloj. | ||
- | Este será actualizado al mismo tiempo, pero el tiempo para la llamada de retorno del reloj será modificado. | ||
- | |||
- | Esto permite de forma fácil adicionar distorsión al tiempo y tener varias partes de la lógica actualizándose en diferentes frecuencias. | ||
- | Un reloj puede tener tantas llamadas de retornos registradas como quieras, con un parámetro de contexto opcional. | ||
- | |||
- | Por ejemplo, el contador FPS mostrado en la esquina arriba izquierda es calculado con un reloj no-alterado que corre a 1Hz. | ||
- | |||
- | |||
- | |||
- | ===== Detalles ===== | ||
- | |||
- | Cuando usamos orx, no necesitamos escribir un <code c> | ||
- | Lo que hacemos es crear un reloj ((o usamos uno existente, como el del núcleo o el reloj de la física)), especificando su frecuencia de actualización. | ||
- | |||
- | Podemos crear cuantos relojes querramos, debemos asegurarnos que la parte mas importante de nuestra lógica (jugadores, enemigos ...) serán actulizados frecuentemente, | ||
- | Por ejemplo, la física y la representación usan dos relojes diferentes que tiene frecuencias distintas. | ||
- | |||
- | Existe además otra gran ventaja en usar varios relojes: podemos facilmente obtener distorción del tiempo. | ||
- | |||
- | En este tutorial, crearemos dos relojes, uno que corre a 100Hz (período = 0.01s) y otro a 5Hz (período = 0.2s). | ||
- | |||
- | <code c> | ||
- | |||
- | pstClock1 = orxClock_Create(orx2F(0.01f), | ||
- | |||
- | pstClock2 = orxClock_Create(orx2F(0.2f), | ||
- | |||
- | Note que pasamos el tipo '' | ||
- | Cualquier valor por encima de este es válido. Los más bajos que estos son reservados para uso interno del motor. | ||
- | |||
- | Ahora usaremos el mismo actualizador de llamada de retorno para los dos relojes. Sin embargo, vamos a proveer diferentes contextos, por lo tanto el primer reloj modificará el primer objeto y el segundo reloj el otro objeto: | ||
- | |||
- | <code c> | ||
- | |||
- | orxClock_Register(pstClock2, | ||
- | |||
- | Esto significa que nuestra llamada de retorno será ejecutada 100 veces por segundo con pstObject1 y el segundo será ejecutado 5 veces por segundo con el objeto 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:// |