diff --git a/ruby/sfml-window/window/Event.cpp b/ruby/sfml-window/window/Event.cpp new file mode 100644 index 00000000..81bbd17d --- /dev/null +++ b/ruby/sfml-window/window/Event.cpp @@ -0,0 +1,90 @@ +/* rbSFML - Copyright (c) 2010 Henrik Valter Vogelius Hansson - groogy@groogy.se + * This software is provided 'as-is', without any express or + * implied warranty. In no event will the authors be held + * liable for any damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute + * it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; + * you must not claim that you wrote the original software. + * If you use this software in a product, an acknowledgment + * in the product documentation would be appreciated but + * is not required. + * + * 2. Altered source versions must be plainly marked as such, + * and must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any + * source distribution. + */ + +#include "Event.hpp" +#include "main.hpp" +#include + +/* SFML::Event holds all the informations about a system event that just happened. + * + * Events are retrieved using the SFML::Window#GetEvent function. + * + * A SFML::Event instance contains the type of the event (mouse moved, key pressed, window closed, ...) + * as well as the details about this particular event. Please note that the event parameters are + * defined in a union, which means that only the member matching the type of the event will be properly + * filled; all other members will have undefined values and must not be read if the type of the event + * doesn't match. For example, if you received a KeyPressed event, then you must read the event.Key + * member, all other members such as event.MouseMove or event.Text will have undefined values. + * + * The ruby version differs from C++ in that the parameters are still stored in a union but that + * the values are directly accessible from the event object. If you try to access any data which + * would be considered undefined then a SFML::SomeKindOfException will be thrown. + * + * Usage example: + * + * while event = window.getEvent() + * + * # Request for closing the window + * if event.type == SFML::Event::Closed + * window.close + * + * # The escape key was pressed + * if ( event.type == sf::Event::KeyPressed ) && ( event.code == SFML::Key::Escape ) + * window.close + * + * # The window was resized + * if event.type == SFML::Event::Resized + * DoSomethingWithTheNewSize(event.width, event.height); + * + * # etc ... + * end + */ +VALUE globalEventClass; + +/* Free a heap allocated object + * Not accessible trough ruby directly! + */ +static void Event_Free( sf::Event *anObject ) +{ + delete anObject; +} + +/* call-seq: + * Event.new() -> event + * + * The constructor creates a new event. + */ +static VALUE Event_New( VALUE aKlass ) +{ + sf::Event *object = new sf::Event(); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Event_Free, object ); + rb_obj_call_init( rbData, 0, 0 ); + return rbData; +} + +void Init_Event( void ) +{ + globalEventClass = rb_define_class_under( GetNamespace(), "Event", rb_cObject ); + + // Class methods + rb_define_singleton_method( globalEventClass, "new", FUNCPTR( Event_New ), 0 ); +} diff --git a/ruby/sfml-window/window/Event.hpp b/ruby/sfml-window/window/Event.hpp new file mode 100644 index 00000000..34a57924 --- /dev/null +++ b/ruby/sfml-window/window/Event.hpp @@ -0,0 +1,31 @@ +/* rbSFML - Copyright (c) 2010 Henrik Valter Vogelius Hansson - groogy@groogy.se + * This software is provided 'as-is', without any express or + * implied warranty. In no event will the authors be held + * liable for any damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute + * it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; + * you must not claim that you wrote the original software. + * If you use this software in a product, an acknowledgment + * in the product documentation would be appreciated but + * is not required. + * + * 2. Altered source versions must be plainly marked as such, + * and must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any + * source distribution. + */ + +#ifndef SFML_RUBYEXT_EVENT_HEADER_ +#define SFML_RUBYEXT_EVENT_HEADER_ + +#include "ruby.h" + +// Ruby initiation function +void Init_Event( void ); + +#endif // SFML_RUBYEXT_EVENT_HEADER_