Thanks, how does this one look?
PHP Code:
/*
This tutorial shows how keyboard input is read in Quad-Ren, the
result is a space ship which can be controlled using the arrow
keys.
Include the Quad-Ren header file and math.h for cos and sin.
*/
#include <quad-ren/quad-ren.h>
#include <math.h>
qr_renderer *renderer;
/*
All input in Quad-Ren is handled using event receiver objects
which can be attached to scene nodes. We create a new event
receiver to move a scene node with the arrow keys. To begin
we derive a class from qr_event_receiver.
This class contains a member variable which will be used to
store the state of keyboard keys.
*/
class new_event_receiver : public qr_event_receiver
{
bool keys_down[QR_KEYSYM_COUNT];
public:
/*
The classes constructor initialises the keys_down array,
setting all elements to false.
*/
new_event_receiver()
{
for(int i = 0; i < QR_KEYSYM_COUNT; i++)
keys_down[i] = false;
}
/*
The virtual method on_event is the core of an event
receiver, this method gets called whenever an event
occurs. The event, a pointer to the scene node
which the event receiver is registered and the time
delta are passed as parameters.
*/
bool on_event(qr_event *event, qr_scene_node *node, float time_delta)
{
/*
Key press/release events are only generated when the key
changes from not pressed to pressed and vice versa, because
of this we use the keys_down array to store the state of
a key.
*/
switch (event -> type)
{
case(QR_KEYDOWN):
keys_down[event -> keysym] = true;
break;
case(QR_KEYUP):
keys_down[event -> keysym] = false;
break;
}
/*
Next we get the location and rotation from the scene node.
*/
vector2d_f pos = node -> get_location();
float rot = (node -> get_rotation() + 90) / (180 / 3.142);
/*
Now we calculate a vector pointing in the forward direction
of the scene node.
*/
vector2d_f forw = vector2d_f((cos(rot) * 0.003) * time_delta, (sin(rot) * 0.003) * time_delta);
/*
Next we check to see what keys are currently pressed, and
calculate the scene nodes new location and rotation.
*/
if(keys_down[QRK_RIGHT])
{
rot -= 0.002 * time_delta;
}
if(keys_down[QRK_LEFT])
{
rot += 0.002 * time_delta;
}
if(keys_down[QRK_UP])
{
pos = vector2d_f(pos.X - forw.X, pos.Y - forw.Y);
}
if(keys_down[QRK_DOWN])
{
pos = vector2d_f(pos.X + forw.X, pos.Y + forw.Y);
}
/*
After we have calculated the nodes new potion all that is
left to do in the event receiver is apply it to the scene
node and return true to inform the engine that it should
not quit.
*/
node -> set_rotation((rot * (180 / 3.142)) - 90);
node -> set_location(pos);
return true;
}
};
int main()
{
/*
Like in the other tutorials we set up the renderer.
*/
renderer = new qr_renderer(vector2d_i(16, 9),
10.0 ,false ,vector2d_i(0, 0));
qr_resource_manager *res_man = renderer ->
get_resource_manager();
renderer -> set_window_title("Event System - Quad-Ren 0.4");
/*
Like the animation example, we load a space image for the background
from a PNG image and attach it to the background quad.
*/
qr_sprite *background = new qr_sprite(res_man, 1);
background -> load_png_frame(
"../media/star_background.png", 1);
background -> convert_data();
renderer -> set_bg_sprite(background);
/*
Now we load the sprite for the space ship from a QRDD file.
*/
qr_sprite *ship_sprite;
ship_sprite = qrdd_file_handler::load_sprite(res_man, "../media/pod.QRDD");
/*
Next we create a quad for the space ship and attach the sprite
to it. We also rotate it by 180 degrees to make it point Upwards.
*/
qr_scene_node *ship = new qr_quad(res_man);
ship -> set_sprite(ship_sprite);
ship -> set_rotation(180);
/*
Here we create an instance of our event receiver and attach it
to the space ship quad.
*/
qr_event_receiver *receiver = new new_event_receiver();
ship -> set_event_receiver(receiver);
/*
As per usual, we run the renderer inside a while loop.
*/
float frame = 1;
while(renderer -> run())
{
/*
We get the time delta for frame rate independent animation.
*/
float time_delta = renderer -> get_time_delta() / 10;
/*
Next we increment the frame count, multiplied by the time delta
to be frame-rate independent. We also set the frame to 1 if it
goes over 20 to make the animation loop.
*/
frame += 0.32 * time_delta;
if(frame > 20)
frame = 1;
ship -> set_frame(frame);
}
/*
Drop the renderer on exit.
*/
renderer -> drop();
}
Bookmarks