diff --git a/bindings/ruby/sfml-graphics/graphics/Drawable.cpp b/bindings/ruby/sfml-graphics/graphics/Drawable.cpp index 5ca34c944..09ec8c7c7 100644 --- a/bindings/ruby/sfml-graphics/graphics/Drawable.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Drawable.cpp @@ -335,7 +335,7 @@ static VALUE Drawable_TransformToGlobal( VALUE self, VALUE aPoint ) */ static VALUE Drawable_Initialize( int argc, VALUE *args, VALUE self ) { - rbDrawable * object = NULL; + rbDrawable *object = NULL; Data_Get_Struct( self, rbDrawable, object ); VALUE aPosition = Qnil; VALUE aScale = Qnil; diff --git a/bindings/ruby/sfml-graphics/graphics/Font.cpp b/bindings/ruby/sfml-graphics/graphics/Font.cpp new file mode 100644 index 000000000..2152d5589 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Font.cpp @@ -0,0 +1,146 @@ +/* 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 "Font.hpp" +#include "main.hpp" +#include + +VALUE globalFontClass; + +/* External classes */ +extern VALUE globalGlyphClass; +extern VALUE globalRectClass; + +static void Font_Free( sf::Font *anObject ) +{ + delete anObject; +} + +static VALUE Font_LoadFromFile( VALUE self, VALUE aFileName ) +{ + sf::Font *object = NULL; + Data_Get_Struct( self, sf::Font, object ); + if( object->LoadFromFile( rb_string_value_cstr( aFileName ) ) == true ) + { + return Qtrue; + } + else + { + return Qfalse; + } +} + +static VALUE Font_GetGlyph( VALUE self, VALUE aCodePoint, VALUE aCharacterSize VALUE aBoldFlag ) +{ + sf::Font *object = NULL; + Data_Get_Struct( self, sf::Font, object ); + const Glyph& glyph = object->GetGlyph( FIX2UINT( aCodePoint ), FIX2UINT( aCharacterSize ), aBoldFlag != Qfalse ); + VALUE rbGlyph = rb_funcall( globalGlyphClass, rb_intern( "new" ), 0 ); + VALUE bounds = rb_funcall( globalRectClass, rb_intern( "new" ), 4, + INT2FIX( glyph.Bounds.Left ), INT2FIX( glyph.Bounds.Top ), + INT2FIX( glyph.Bounds.Width ), INT2FIX( glyph.Bounds.Height ) ); + VALUE subRect = rb_funcall( globalRectClass, rb_intern( "new" ), 4, + INT2FIX( glyph.SubRect.Left ), INT2FIX( glyph.SubRect.Top ), + INT2FIX( glyph.SubRect.Width ), INT2FIX( glyph.SubRect.Height ) ); + rb_funcall( rbGlyph, rb_intern( "advance=" ), 1, INT2FIX( glyph.Advance ) ); + rb_funcall( rbGlyph, rb_intern( "bounds=" ), 1, bounds ); + rb_funcall( rbGlyph, rb_intern( "subRect=" ), 1, subRect ); + return rbGlyph; +} + +static VALUE Font_GetKerning( VALUE self, VALUE aFirst, VALUE aSecond, VALUE aCharacterSize ) +{ + sf::Font *object = NULL; + Data_Get_Struct( self, sf::Font, object ); + return INT2FIX( object->GetKerning( FIX2UINT( aFirst ), FIX2UINT( aSecond ), FIX2UINT( aCharacterSize ) ) ); +} + +static VALUE Font_GetLineSpacing( VALUE self, VALUE aCharacterSize ) +{ + sf::Font *object = NULL; + Data_Get_Struct( self, sf::Font, object ); + return INT2FIX( object->GetLineSpacing( FIX2UINT( aCharacterSize ) ) ); +} + +static VALUE Font_New( int argc, VALUE *args, VALUE aKlass ) +{ + sf::Font *object = new sf::Font(); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Font_Free, object ); + rb_obj_call_init( rbData, argc, args ); + return rbData; +} + +void Init_Font( void ) +{ +/* SFML namespace which contains the classes of this module. */ + VALUE sfml = rb_define_module( "SFML" ); +/* Class for loading and manipulating character fonts. + * + * Fonts can be loaded from a file or from memory, from the most common types of fonts. + * + * See the loadFromFile method for the complete list of supported formats. + * + * Once it is loaded, a SFML::Font instance provides three types of informations about the font: + * + * - Global metrics, such as the line spacing + * - Per-glyph metrics, such as bounding box or kerning + * - Pixel representation of glyphs + * + * Fonts alone are not very useful: they hold the font data but cannot make anything useful of it. To do so you need + * to use the SFML::Text class, which is able to properly output text with several options such as character size, style, + * color, position, rotation, etc. This separation allows more flexibility and better performances: indeed a sf::Font + * is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, + * a SFML::Text is a lightweight object which can combine the glyphs data and metrics of a SFML::Font to display any text + * on a render target. Note that it is also possible to bind several SFML::Text instances to the same sf::Font. + * + * It is important to note that the sf::Text instance doesn't copy the font that it uses, it only keeps a reference to + * it. Thus, a SFML::Font must not be destructed while it is used by a SFML::Text (i.e. never write a function that uses + * a local SFML::Font instance for creating a text). + * + * Usage example: + * + * # Declare a new font + * font = SFML::Font.new + * + * # Load it from a file + * if font.LoadFromFile("arial.ttf") == false + * # error... + * end + * + * # Create a text which uses our font + * text1 = SFML::Text.new + * text1.setFont( font ) + * text1.setCharacterSize( 30 ) + * text1.setStyle( sf::Text::Regular ) + * + * # Create another text using the same font, but with different parameters + * text2 = SFML::Text.new + * text2.setFont( font ) + * text2.setCharacterSize( 50 ) + * text2.setStyle( SFML::Text::Italic ) + * + * Apart from loading font files, and passing them to instances of SFML::Text, you should normally not have to deal + * directly with this class. However, it may be useful to access the font metrics or rasterized glyphs for advanced + * usage. + */ + globalFontClass = rb_define_class_under( sfml, "Font", rb_cObject ); +} diff --git a/bindings/ruby/sfml-graphics/graphics/Font.hpp b/bindings/ruby/sfml-graphics/graphics/Font.hpp new file mode 100644 index 000000000..393097128 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Font.hpp @@ -0,0 +1,30 @@ +/* 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_FONT_HEADER_ +#define SFML_RUBYEXT_FONT_HEADER_ + +#include "ruby.h" + +void Init_Font( void ); + +#endif // SFML_RUBYEXT_FONT_HEADER_ diff --git a/bindings/ruby/sfml-graphics/graphics/Glyph.cpp b/bindings/ruby/sfml-graphics/graphics/Glyph.cpp new file mode 100644 index 000000000..ac89e5eb6 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Glyph.cpp @@ -0,0 +1,69 @@ +/* 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 "Glyph.hpp" +#include "Rect.hpp" +#include "main.hpp" + +VALUE globalGlyphClass; + +/* External classes */ +extern VALUE globalRectClass; + +/* call-seq: + * Glyph.new() -> glyph + * + * Create a new glyph instance. + */ +static VALUE Glyph_Initialize( VALUE self ) +{ + rb_iv_set( self, "@advance", INT2FIX( 0 ) ); + rb_iv_set( self, "@bounds", rb_funcall( globalRectClass, rb_intern( "new" ), 0 ) ); + rb_iv_set( self, "@subRect", rb_funcall( globalRectClass, rb_intern( "new" ), 0 ) ); + return self; +} + +void Init_Glyph( void ) +{ +/* SFML namespace which contains the classes of this module. */ + VALUE sfml = rb_define_module( "SFML" ); +/* Structure describing a glyph. + * + * A glyph is the visual representation of a character. + * + * The SFML::Glyph structure provides the information needed to handle the glyph: + * + * - its coordinates in the font's image + * - its bounding rect + * - the offset to apply to get the starting position of the next glyph + */ + globalGlyphClass = rb_define_class_under( sfml, "Glyph", rb_cObject ); + + // Attribute accessors + rb_define_attr( globalGlyphClass, "advance", 1, 1 ); + rb_define_attr( globalGlyphClass, "bounds", 1, 1 ); + rb_define_attr( globalGlyphClass, "subRect", 1, 1 ); + + // Aliases + rb_define_alias( globalGlyphClass, "sub_rect", "subRect" ); + rb_define_alias( globalGlyphClass, "sub_rect=", "subRect=" ); +} diff --git a/bindings/ruby/sfml-graphics/graphics/Glyph.hpp b/bindings/ruby/sfml-graphics/graphics/Glyph.hpp new file mode 100644 index 000000000..33c33863a --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Glyph.hpp @@ -0,0 +1,30 @@ +/* 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_GLYPH_HEADER_ +#define SFML_RUBYEXT_GLYPH_HEADER_ + +#include "ruby.h" + +void Init_Glyph( void ); + +#endif // SFML_RUBYEXT_GLYPH_HEADER_ diff --git a/bindings/ruby/sfml-graphics/graphics/Image.cpp b/bindings/ruby/sfml-graphics/graphics/Image.cpp new file mode 100644 index 000000000..1809b37cf --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Image.cpp @@ -0,0 +1,222 @@ +/* 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 "Image.hpp" +#include "Color.hpp" +#include "main.hpp" +#include + +VALUE globalImageClass; + +/* External classes */ +extern VALUE globalColorClass; + +/* Free a heap allocated object + * Not accessible trough ruby directly! + */ +static void Image_Free( sf::Image *anObject ) +{ + delete anObject; +} + +static VALUE Image_LoadFromFile( VALUE self, VALUE aFileName ) +{ + sf::Image *object = NULL; + Data_Get_Struct( self, sf::Image, object ); + if( object->LoadFromFile( rb_string_value_cstr( aFileName ) ) == true ) + { + return Qtrue; + } + else + { + return Qfalse; + } +} + +static VALUE Image_LoadFromPixels( VALUE self, VALUE aWidth, VALUE aHeight, VALUE somePixels ) +{ + const unsigned int rawWidth = FIX2UINT( aWidth ); + const unsigned int rawHeight = FIX2UINT( aHeight ); + VALIDATE_CLASS( somePixels, rb_cArray, "pixels" ); + const unsigned long dataSize = rawWidth * rawHeight * 4; + sf::Uint8 * const tempData = new sf::Uint8[dataSize]; + VALUE pixels = rb_funcall( somePixels, rb_intern("flatten"), 0 ); + for(unsigned long index = 0; index < dataSize; index++) + { + sf::Uint8 val = NUM2CHR( rb_ary_entry( pixels, index ) ); + tempData[index] = val; + } + + sf::Image *object = NULL; + Data_Get_Struct( self, sf::Image, object ); + bool result = object->LoadFromPixels( rawWidth, rawHeight, tempData ); + delete[] tempData; + + if( result == true ) + { + return Qtrue; + } + else + { + return Qfalse; + } +} + +static VALUE Image_SaveToFile( VALUE self, VALUE aFileName ) +{ + sf::Image *object = NULL; + Data_Get_Struct( self, sf::Image, object ); + if( object->SaveToFile( rb_string_value_cstr( aFileName ) ) == true ) + { + return Qtrue; + } + else + { + return Qfalse; + } +} + +static VALUE Image_Create( int argc, VALUE *args, VALUE self ) +{ + sf::Image *object = NULL; + Data_Get_Struct( self, sf::Image, object ); + + unsigned int width = 0; + unsigned int height = 0; + VALUE rubyColor = Qnil; + sf::Color color; + + switch( argc ) + { + case 3: + rubyColor = Color_ForceType( args[2] ); + color.r = Color_GetR( rubyColor ); + color.g = Color_GetG( rubyColor ); + color.b = Color_GetB( rubyColor ); + color.a = Color_GetA( rubyColor ); + case 2: + width = FIX2UINT( args[0] ); + height = FIX2UINT( args[1] ); + break; + default: + rb_raise( rb_eArgError, "Expected 2 or 3 arguments but was given %d", argc ); + } + + return ( object->Create( width, height, color ) == true ? Qtrue : Qfalse ); +} + +static VALUE Image_CreateMaskFromColor( int argc, VALUE *args, VALUE self ) +{ + sf::Image *object = NULL; + Data_Get_Struct( self, sf::Image, object ); + + sf::Uint8 alpha = 0; + VALUE rubyColor = Qnil; + sf::Color color; + + switch( argc ) + { + case 2: + alpha = FIX2UINT( alpha ); + case 1: + rubyColor = Color_ForceType( args[0] ); + color.r = Color_GetR( rubyColor ); + color.g = Color_GetG( rubyColor ); + color.b = Color_GetB( rubyColor ); + color.a = Color_GetA( rubyColor ); + break; + default: + rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc ); + } + + object->CreateMaskFromColor( color, alpha ); + return Qnil; +} + +/* call-seq: + * Image.new() -> image + * + * The clock starts automatically after being constructed. + */ +static VALUE Image_New( VALUE aKlass ) +{ + sf::Image *object = new sf::Image(); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Image_Free, object ); + rb_obj_call_init( rbData, 0, 0 ); + return rbData; +} + +void Init_Image( void ) +{ +/* SFML namespace which contains the classes of this module. */ + VALUE sfml = rb_define_module( "SFML" ); +/* Class for loading, manipulating and saving images. + * + * SFML::Image is an abstraction to manipulate images as bidimensional arrays of pixels. + * + * The class provides functions to load, read, write and save pixels, as well as many other useful functions to... + * + * SFML::Image can handle a unique internal representation of pixels, which is RGBA 32 bits. This means that a pixel + * must be composed of 8 bits red, green, blue and alpha channels -- just like a SFML::Color. All the functions that + * return an array of pixels follow this rule, and all parameters that you pass to sf::Image functions (such as + * loadFromPixels or updatePixels) must use this representation as well. + * + * A SFML::Image can be copied, but it is a heavy resource and if possible you should always use [const] references + * to pass or return them to avoid useless copies. + * + * Usage example: + * + * # Load an image file + * background = SFML::Image.new; + * if background.loadFromFile( "background.jpg" ) == false + * # Error + * end + * + * # Create a 20x20 image filled with black color + * image = SFML::Image.new; + * if image.create( 20, 20, SFML::Color::Black ) == false + * # Error + * end + * + * # Copy image1 on image2 at position (10, 10) + * image.copy( background, 10, 10 ) + * + * # Make the top-left pixel transparent + * color = image.getPixel( 0, 0 ) + * color.a = 0 + * image.setPixel( 0, 0, color ) + * + * # Save the image to a file + * if image.saveToFile( "result.png" ) == false + * # Error + * end + + */ + globalImageClass = rb_define_class_under( sfml, "Image", rb_cObject ); + + // Class methods + rb_define_singleton_method( globalImageClass, "new", Image_New, 0 ); + + // Instance methods + + // Aliases +} diff --git a/bindings/ruby/sfml-graphics/graphics/Image.hpp b/bindings/ruby/sfml-graphics/graphics/Image.hpp new file mode 100644 index 000000000..630969955 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Image.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_IMAGE_HEADER_ +#define SFML_RUBYEXT_IMAGE_HEADER_ + +#include "ruby.h" + +// Ruby initiation function +void Init_Image( void ); + +#endif // SFML_RUBYEXT_IMAGE_HEADER_ diff --git a/bindings/ruby/sfml-window/window/Window.cpp b/bindings/ruby/sfml-window/window/Window.cpp index 58c1d067a..e65ab4ff1 100644 --- a/bindings/ruby/sfml-window/window/Window.cpp +++ b/bindings/ruby/sfml-window/window/Window.cpp @@ -368,7 +368,7 @@ static VALUE Window_SetIcon( VALUE self, VALUE aWidth, VALUE aHeight, VALUE some { const unsigned int rawWidth = FIX2UINT( aWidth ); const unsigned int rawHeight = FIX2UINT( aHeight ); - VALIDATE_CLASS( somePixels, rb_cArray, "third" ); + VALIDATE_CLASS( somePixels, rb_cArray, "pixels" ); const unsigned long dataSize = rawWidth * rawHeight * 4; sf::Uint8 * const tempData = new sf::Uint8[dataSize]; VALUE pixels = rb_funcall( somePixels, rb_intern("flatten"), 0 ); diff --git a/bindings/ruby/testing/drawable-mixin.rb b/bindings/ruby/testing/drawable-mixin.rb index 9de445501..e88f1f0b0 100644 --- a/bindings/ruby/testing/drawable-mixin.rb +++ b/bindings/ruby/testing/drawable-mixin.rb @@ -8,3 +8,4 @@ end drawable = MyDrawable.new p drawable.position +p drawable.is_a?( SFML::Drawable )