From f2176a98b4e4bb23f8a44859fc7344a40fe7cad2 Mon Sep 17 00:00:00 2001 From: groogy Date: Wed, 3 Nov 2010 16:53:48 +0000 Subject: [PATCH] Added SFML::Context to the sfml-window library. git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1600 4e206d99-4929-0410-ac5d-dfc041789085 --- ruby/sfml-window/window/Context.cpp | 122 ++++++++++++++++++++++++++++ ruby/sfml-window/window/Context.hpp | 31 +++++++ ruby/sfml-window/window/main.cpp | 2 + 3 files changed, 155 insertions(+) create mode 100644 ruby/sfml-window/window/Context.cpp create mode 100644 ruby/sfml-window/window/Context.hpp diff --git a/ruby/sfml-window/window/Context.cpp b/ruby/sfml-window/window/Context.cpp new file mode 100644 index 000000000..787d02efb --- /dev/null +++ b/ruby/sfml-window/window/Context.cpp @@ -0,0 +1,122 @@ +/* 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 "Context.hpp" +#include "main.hpp" +#include + +/* If you need to make OpenGL / graphics calls without having an active window + * (like in a thread), you can use an instance of this class to get a valid context. + * + * Having a valid context is necessary for *every* OpenGL call, and for most of + * the classes from the Graphics package. + * + * Note that a context is only active in its current thread, if you create a new + * thread it will have no valid context by default. + * + * To use a sf::Context instance, just construct it and let it live as long as + * you need a valid context. No explicit activation is needed, all it has to do + * is to exist. Its destructor will take care of deactivating and freeing all + * the attached resources. + */ +VALUE globalContextClass; + +/* Free a heap allocated object + * Not accessible trough ruby directly! + */ +static void Context_Free( sf::Context *anObject ) +{ + delete anObject; +} + +/* call-seq: + * context.SetActive(bool) -> nil + * + * Activate or deactivate explicitely the context. + */ +static VALUE Context_SetActive( VALUE self, VALUE anArgument ) +{ + sf::Context *object = NULL; + Data_Get_Struct( self, sf::Context, object ); + switch( anArgument ) + { + case Qtrue: + object->SetActive( true ); + break; + case Qfalse: + object->SetActive( false ); + break; + default: + rb_raise( rb_eRuntimeError, "expected true or false" ); + } + return Qnil; +} + +/* call-seq: + * Context.SetReferenceActive() -> true or false + * + * This function is meant to be called internally; it is used to deactivate the + * current context by activating another one (so that we still have an active + * context on the current thread). + */ +static VALUE Context_SetReferenceActive( VALUE aKlass ) +{ + if( sf::Context::SetReferenceActive() == true ) + { + return Qtrue; + } + else + { + return Qfalse; + } +} + +/* call-seq: + * Context.new() -> context + * + * The constructor creates and activates the context + */ +static VALUE Context_New( VALUE aKlass ) +{ + sf::Context *object = new sf::Context(); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Context_Free, object ); + rb_obj_call_init( rbData, 0, 0 ); + return rbData; +} + +void Init_Context( void ) +{ + globalContextClass = rb_define_class_under( GetNamespace(), "Context", rb_cObject ); + + // Class methods + rb_define_singleton_method( globalContextClass, "new", FUNCPTR( Context_New ), 0 ); + rb_define_singleton_method( globalContextClass, "setReferenceActive", FUNCPTR( Context_SetReferenceActive ), 0 ); + + // Instance methods + rb_define_method( globalContextClass, "setActive", FUNCPTR( Context_SetActive ), 1 ); + + // Aliases + rb_define_alias( globalContextClass, "active=", "setActive" ); + rb_define_alias( globalContextClass, "set_active", "setActive" ); + + rb_define_alias( CLASS_OF( globalContextClass ), "set_reference_active", "setReferenceActive" ); +} diff --git a/ruby/sfml-window/window/Context.hpp b/ruby/sfml-window/window/Context.hpp new file mode 100644 index 000000000..7631df9dd --- /dev/null +++ b/ruby/sfml-window/window/Context.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_CONTEXT_HEADER_ +#define SFML_RUBYEXT_CONTEXT_HEADER_ + +#include "ruby.h" + +// Ruby initiation function +void Init_Context( void ); + +#endif // SFML_RUBYEXT_CONTEXT_HEADER_ diff --git a/ruby/sfml-window/window/main.cpp b/ruby/sfml-window/window/main.cpp index 56a278a22..5a31950c8 100644 --- a/ruby/sfml-window/window/main.cpp +++ b/ruby/sfml-window/window/main.cpp @@ -21,6 +21,7 @@ */ #include "main.hpp" +#include "Context.hpp" VALUE globalSFMLNamespace; @@ -33,4 +34,5 @@ void Init_window( void ) { globalSFMLNamespace = rb_define_module( "SFML" ); rb_define_const(globalSFMLNamespace, "WindowLoaded", Qtrue); + Init_Context(); }