diff --git a/bindings/ruby/sfml-graphics/graphics/Rect.cpp b/bindings/ruby/sfml-graphics/graphics/Rect.cpp index 1950e3f20..e6c5ea71f 100644 --- a/bindings/ruby/sfml-graphics/graphics/Rect.cpp +++ b/bindings/ruby/sfml-graphics/graphics/Rect.cpp @@ -330,8 +330,8 @@ void Init_Rect( void ) globalRectClass = rb_define_class_under( sfml, "Rect", rb_cObject ); // Instance methods - rb_define_method( globalRectClass, "initialize", Rect_Initialize, -2 ); - rb_define_method( globalRectClass, "contains", Rect_Contains, -2 ); + rb_define_method( globalRectClass, "initialize", Rect_Initialize, -1 ); + rb_define_method( globalRectClass, "contains", Rect_Contains, -1 ); rb_define_method( globalRectClass, "intersects", Rect_Contains, 1 ); // Instance operators diff --git a/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp b/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp index 2bad1d5a1..6ee9bac87 100644 --- a/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp +++ b/bindings/ruby/sfml-graphics/graphics/RenderImage.cpp @@ -75,6 +75,37 @@ static VALUE RenderImage_Create( int argc, VALUE *args, VALUE self ) } } +static VALUE RenderImage_Draw( int argc, VALUE *args, VALUE self ) +{ + sf::RenderImage *object = NULL; + Data_Get_Struct( self, sf::RenderImage, object ); + switch( argc ) + { + case 2: + { + VALIDATE_CLASS( args[0], globalDrawableModule, "object" ); + VALIDATE_CLASS( args[1], globalShaderClass, "shader" ); + sf::Drawable *drawable = NULL; + Data_Get_Struct( args[0], sf::Drawable, drawable ); + sf::Shader *shader = NULL; + Data_Get_Struct( args[1], sf::Shader, shader ); + object->Draw( *drawable, *shader ); + break; + } + case 1: + { + VALIDATE_CLASS( args[0], globalDrawableModule, "object" ); + sf::Drawable *drawable = NULL; + Data_Get_Struct( args[0], sf::Drawable, drawable ); + object->Draw( *drawable ); + break; + } + default: + rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc ); + } + return Qnil; +} + static VALUE RenderImage_Display( VALUE self ) { sf::RenderImage *object = NULL; diff --git a/bindings/ruby/sfml-graphics/graphics/RenderTarget.cpp b/bindings/ruby/sfml-graphics/graphics/RenderTarget.cpp index 40c01bf0d..37f77764e 100644 --- a/bindings/ruby/sfml-graphics/graphics/RenderTarget.cpp +++ b/bindings/ruby/sfml-graphics/graphics/RenderTarget.cpp @@ -24,6 +24,7 @@ #include "Color.hpp" #include "main.hpp" #include +#include VALUE globalRenderTargetModule; VALUE globalRenderTargetInstanceClass; diff --git a/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp b/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp new file mode 100644 index 000000000..166ebe7e1 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/RenderWindow.cpp @@ -0,0 +1,181 @@ + /* 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 "RenderWindow.hpp" +#include "main.hpp" +#include + +VALUE globalRenderWindowClass; + +/* External classes */ +extern VALUE globalRenderTargetModule; +extern VALUE globalWindowClass; +extern VALUE globalDrawableModule; +extern VALUE globalShaderClass; + +static void RenderWindow_Free( sf::RenderWindow *anObject ) +{ + delete anObject; +} + +static VALUE RenderWindow_Draw( int argc, VALUE *args, VALUE self ) +{ + sf::RenderWindow *object = NULL; + Data_Get_Struct( self, sf::RenderWindow, object ); + switch( argc ) + { + case 2: + { + VALIDATE_CLASS( args[0], globalDrawableModule, "object" ); + VALIDATE_CLASS( args[1], globalShaderClass, "shader" ); + sf::Drawable *drawable = NULL; + Data_Get_Struct( args[0], sf::Drawable, drawable ); + sf::Shader *shader = NULL; + Data_Get_Struct( args[1], sf::Shader, shader ); + object->Draw( *drawable, *shader ); + break; + } + case 1: + { + VALIDATE_CLASS( args[0], globalDrawableModule, "object" ); + sf::Drawable *drawable = NULL; + Data_Get_Struct( args[0], sf::Drawable, drawable ); + object->Draw( *drawable ); + break; + } + default: + rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc ); + } + return Qnil; +} + +static VALUE RenderWindow_Display( VALUE self ) +{ + sf::RenderWindow *object = NULL; + Data_Get_Struct( self, sf::RenderWindow, object ); + object->Display(); +} + +static VALUE RenderWindow_New( int argc, VALUE *args, VALUE aKlass ) +{ + sf::RenderWindow *object = new sf::RenderWindow(); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, RenderWindow_Free, object ); + rb_obj_call_init( rbData, argc, args ); + return rbData; +} + +void Init_RenderWindow( void ) +{ +/* SFML namespace which contains the classes of this module. */ + VALUE sfml = rb_define_module( "SFML" ); +/* Window that can serve as a target for 2D drawing. + * + * SFML::RenderWindow is the main class of the Graphics module. + * + * It defines an OS window that can be painted using the other classes of the graphics module. + * + * SFML::RenderWindow is derived from SFML::Window, thus it inherits all its features: mouse/keyboard/joystick input, + * events, window handling, OpenGL rendering, etc. See the documentation of SFML::Window for a more complete description + * of all these features and code samples. + * + * On top of that, SFML::RenderWindow adds more features related to 2D drawing with the graphics module (see its base + * class SFML::RenderTarget for more details). Here is a typical rendering / event loop with a SFML::RenderWindow: + * + * # Declare and create a new render-window + * window = SFML::RenderWindow.new( SFML::VideoMode.new( 800, 600 ), "SFML window" ) + * + * # Limit the framerate to 60 frames per second (this step is optional) + * window.framerateLimit = 60 + * + * # The main loop - ends as soon as the window is closed + * while window.open? + * # Event processing + * while event = window.getEvent + * # Request for closing the window + * if event.type == SFML::Event::Closed) + * window.close + * end + * end + * + * # Clear the whole window before rendering a new frame + * window.clear + * + * # Draw some sprites / shapes / texts + * window.draw( sprite ) # sprite is a SFML::Sprite + * window.draw( shape ) # shape is a SFML::Shape + * window.draw( text ) # text is a SFML::Text + * + * # End the current frame and display its contents on screen + * window.display + * end + * + * Like SFML::Window, SFML::RenderWindow is still able to render direct OpenGL stuff. It is even possible to mix + * together OpenGL calls and regular SFML drawing commands. When doing so, make sure that OpenGL states are not messed + * up by calling the saveGLStates / restoreGLStates functions. + * + * # Create the render window + * window = SFML::RenderWindow.new( SFML::VideoMode( 800, 600 ), "SFML OpenGL" ) + * + * # Create a sprite and a text to display + * sprite = SFML::Sprite.new + * text = SFML::Sprite.new + * # ... + * + * # Perform OpenGL initializations + * glMatrixMode( GL_PROJECTION ) + * # ... + * + * # Start the rendering loop + * while window.open? + * # Process events + * # ... + * + * # Draw a background sprite + * window.saveGLStates + * window.draw( sprite ) + * window.restoreGLStates + * + * # Draw a 3D object using OpenGL + * glBegin( GL_QUADS ) + * glVertex3f( ... ) + * # ... + * glEnd() + * + * # Draw text on top of the 3D object + * window.saveGLStates + * window.draw( text ) + * window.restoreGLStates + * + * # Finally, display the rendered frame on screen + * window.display + * end + * + */ + globalRenderWindowClass = rb_define_class_under( sfml, "RenderWindow", globalWindowClass ); + rb_include_module( globalRenderWindowClass, globalRenderTargetModule ); + + // Class methods + rb_define_singleton_method( globalRenderWindowClass, "new", RenderWindow_New, -1 ); + + // Instance methods + rb_define_method( globalRenderWindowClass, "draw", RenderWindow_Draw, -1 ); +} diff --git a/bindings/ruby/sfml-graphics/graphics/RenderWindow.hpp b/bindings/ruby/sfml-graphics/graphics/RenderWindow.hpp new file mode 100644 index 000000000..8c538caca --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/RenderWindow.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_RENDER_WINDOW_HEADER_ +#define SFML_RUBYEXT_RENDER_WINDOW_HEADER_ + +#include "ruby.h" + +// Ruby initiation function +void Init_RenderWindow( void ); + +#endif // SFML_RUBYEXT_RENDER_WINDOW_HEADER_ diff --git a/bindings/ruby/sfml-graphics/graphics/Shape.cpp b/bindings/ruby/sfml-graphics/graphics/Shape.cpp new file mode 100644 index 000000000..8c58e6d60 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Shape.cpp @@ -0,0 +1,568 @@ +/* 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 "Shape.hpp" +#include "Vector2.hpp" +#include "Rect.hpp" +#include "Color.hpp" +#include "main.hpp" +#include + +VALUE globalShapeClass; +/* External classes */ +extern VALUE globalVector2Class; +extern VALUE globalDrawableModule; +extern VALUE globalColorClass; + +static void Shape_Free( sf::Shape *anObject ) +{ + delete anObject; +} + +static VALUE Shape_AddPoint( int argc, VALUE *args, VALUE self ) +{ + VALUE temp = Qnil; + float x = 0; + float y = 0; + sf::Color color = sf::Color::White; + sf::Color outlineColor = sf::Color::Black; + + if( argc > 0 && rb_obj_is_kind_of( args[0], rb_cFloat ) == Qtrue ) + { + switch( argc ) + { + case 4: + temp = Color_ForceType( args[3] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 3: + temp = Color_ForceType( args[2] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + case 2: + x = NUM2DBL( args[0] ); + y = NUM2DBL( args[1] ); + default: + rb_raise( rb_eArgError, "Expected 2..4 arguments but was given %d", argc ); + + } + } + else + { + switch( argc ) + { + case 3: + temp = Color_ForceType( args[2] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 2: + temp = Color_ForceType( args[1] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + case 1: + temp = Vector2_ForceType( args[0] ); + x = NUM2DBL( Vector2_GetX( temp ) ); + y = NUM2DBL( Vector2_GetY( temp ) ); + default: + rb_raise( rb_eArgError, "Expected 1..3 arguments but was given %d", argc ); + + } + } + + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + object->AddPoint( x, y, color, outlineColor ); + return Qnil; +} + +static VALUE Shape_GetPointsCount( VALUE self ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + return INT2FIX( object->GetPointsCount() ); +} + +static VALUE Shape_EnableFill( VALUE self, VALUE anEnableFlag ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + if( anEnableFlag == Qtrue ) + { + object->EnableFill( true ); + } + else if( anEnableFlag == Qfalse ) + { + object->EnableFill( false ); + } + else + { + VALIDATE_CLASS( anEnableFlag, rb_cTrueClass, "enable" ); + } + return Qnil; +} + +static VALUE Shape_EnableOutline( VALUE self, VALUE anEnableFlag ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + if( anEnableFlag == Qtrue ) + { + object->EnableOutline( true ); + } + else if( anEnableFlag == Qfalse ) + { + object->EnableOutline( false ); + } + else + { + VALIDATE_CLASS( anEnableFlag, rb_cTrueClass, "enable" ); + } + return Qnil; +} + +static VALUE Shape_SetPointPosition( int argc, VALUE *args, VALUE self ) +{ + VALUE temp = Qnil; + float x = 0; + float y = 0; + + switch( argc ) + { + case 2: + temp = Vector2_ForceType( args[1] ); + x = NUM2DBL( Vector2_GetX( temp ) ); + y = NUM2DBL( Vector2_GetY( temp ) ); + break; + case 3: + x = NUM2DBL( args[1] ); + y = NUM2DBL( args[2] ); + break; + default: + rb_raise( rb_eArgError, "Expected 2..3 arguments but was given %d", argc ); + } + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + object->SetPointPosition( FIX2UINT( args[0] ), x, y ); + return Qnil; +} + +static VALUE Shape_SetPointColor( VALUE self, VALUE anIndex, VALUE aColor ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + VALUE temp = Color_ForceType( aColor ); + sf::Color color; + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + object->SetPointColor( FIX2UINT( anIndex ), color ); + return Qnil; +} + +static VALUE Shape_SetPointOutlineColor( VALUE self, VALUE anIndex, VALUE aColor ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + VALUE temp = Color_ForceType( aColor ); + sf::Color color; + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + object->SetPointOutlineColor( FIX2UINT( anIndex ), color ); + return Qnil; +} + +static VALUE Shape_SetOutlineWidth( VALUE self, VALUE aWidth ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + object->SetOutlineWidth( NUM2DBL( aWidth ) ); + return Qnil; +} + +static VALUE Shape_GetPointPosition( VALUE self, VALUE anIndex ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + const sf::Vector2f &vector = object->GetPointPosition( FIX2UINT( anIndex ) ); + return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( vector.x ), rb_float_new( vector.y ) ); +} + +static VALUE Shape_GetPointColor( VALUE self, VALUE anIndex ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + const sf::Color &color = object->GetPointColor( FIX2UINT( anIndex ) ); + return rb_funcall( globalColorClass, rb_intern( "new" ), 4, + INT2FIX( color.r ), INT2FIX( color.g ), + INT2FIX( color.b ), INT2FIX( color.a ) ); +} + +static VALUE Shape_GetPointOutlineColor( VALUE self, VALUE anIndex ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + const sf::Color &color = object->GetPointOutlineColor( FIX2UINT( anIndex ) ); + return rb_funcall( globalColorClass, rb_intern( "new" ), 4, + INT2FIX( color.r ), INT2FIX( color.g ), + INT2FIX( color.b ), INT2FIX( color.a ) ); +} + +static VALUE Shape_GetOutlineWidth( VALUE self ) +{ + sf::Shape *object = NULL; + Data_Get_Struct( self, sf::Shape, object ); + return rb_float_new( object->GetOutlineWidth() ); +} + +static VALUE Shape_New( int argc, VALUE *args, VALUE aKlass ) +{ + sf::Shape *object = new sf::Shape(); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Shape_Free, object ); + rb_obj_call_init( rbData, argc, args ); + return rbData; +} + +static VALUE Shape_Line( int argc, VALUE *args, VALUE aKlass ) +{ + VALUE temp = Qnil; + float p1x = 0, p1y = 0; + float p2x = 0, p2y = 0; + float thickness = 0; + sf::Color color; + float outline = 0.0; + sf::Color outlineColor = sf::Color::Black; + + if( argc > 0 && rb_obj_is_kind_of( args[0], rb_cFloat ) == Qtrue ) + { + switch( argc ) + { + case 8: + temp = Color_ForceType( args[7] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 7: + outline = NUM2DBL( args[6] ); + case 6: + p1x = NUM2DBL( args[0] ); p1y = NUM2DBL( args[1] ); + p2x = NUM2DBL( args[2] ); p2y = NUM2DBL( args[3] ); + thickness = NUM2DBL( args[4] ); + temp = Color_ForceType( args[5] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + break; + default: + rb_raise( rb_eArgError, "Expected 6..8 arguments but was given %d", argc ); + } + } + else + { + switch( argc ) + { + case 6: + temp = Color_ForceType( args[5] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 5: + outline = NUM2DBL( args[4] ); + case 4: + temp = Vector2_ForceType( args[0] ); + p1x = NUM2DBL( Vector2_GetX( temp ) ); p1y = NUM2DBL( Vector2_GetY( temp ) ); + temp = Vector2_ForceType( args[1] ); + p2x = NUM2DBL( Vector2_GetX( temp ) ); p2y = NUM2DBL( Vector2_GetY( temp ) ); + thickness = NUM2DBL( args[2] ); + temp = Color_ForceType( args[3] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + break; + default: + rb_raise( rb_eArgError, "Expected 6..8 arguments but was given %d", argc ); + } + } + + sf::Shape * shape = new sf::Shape( sf::Shape::Line( p1x, p1y, p2x, p2y, thickness, color, outline, outlineColor ) ); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Shape_Free, shape ); + rb_obj_call_init( rbData, 0, 0 ); + return rbData; +} + +static VALUE Shape_Rectangle( int argc, VALUE *args, VALUE aKlass ) +{ + VALUE temp = Qnil; + float p1x = 0, p1y = 0; + float p2x = 0, p2y = 0; + sf::Color color; + float outline = 0.0; + sf::Color outlineColor = sf::Color::Black; + + if( argc > 0 && rb_obj_is_kind_of( args[0], rb_cFloat ) == Qtrue ) + { + switch( argc ) + { + case 7: + temp = Color_ForceType( args[6] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 6: + outline = NUM2DBL( args[5] ); + case 5: + p1x = NUM2DBL( args[0] ); p1y = NUM2DBL( args[1] ); + p2x = NUM2DBL( args[2] ); p2y = NUM2DBL( args[3] ); + temp = Color_ForceType( args[4] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + break; + default: + rb_raise( rb_eArgError, "Expected 5..7 arguments but was given %d", argc ); + } + } + else + { + switch( argc ) + { + case 4: + temp = Color_ForceType( args[3] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 3: + outline = NUM2DBL( args[2] ); + case 2: + temp = Rect_ForceType( args[0] ); + p1x = NUM2DBL( Rect_GetLeft( temp ) ); p1y = NUM2DBL( Rect_GetTop( temp ) ); + p2x = NUM2DBL( Rect_GetWidth( temp ) ); p2y = NUM2DBL( Rect_GetHeight( temp ) ); + temp = Color_ForceType( args[1] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + break; + default: + rb_raise( rb_eArgError, "Expected 2..4 arguments but was given %d", argc ); + } + } + + sf::Shape * shape = new sf::Shape( sf::Shape::Rectangle( p1x, p1y, p2x, p2y, color, outline, outlineColor ) ); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Shape_Free, shape ); + rb_obj_call_init( rbData, 0, 0 ); + return rbData; +} + +static VALUE Shape_Circle( int argc, VALUE *args, VALUE aKlass ) +{ + VALUE temp = Qnil; + float x = 0, y = 0; + float radius = 0; + sf::Color color; + float outline = 0.0; + sf::Color outlineColor = sf::Color::Black; + + if( argc > 0 && rb_obj_is_kind_of( args[0], rb_cFloat ) == Qtrue ) + { + switch( argc ) + { + case 6: + temp = Color_ForceType( args[5] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 5: + outline = NUM2DBL( args[4] ); + case 4: + x = NUM2DBL( args[0] ); y = NUM2DBL( args[1] ); + radius = NUM2DBL( args[2] ); + temp = Color_ForceType( args[3] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + break; + default: + rb_raise( rb_eArgError, "Expected 4..6 arguments but was given %d", argc ); + } + } + else + { + switch( argc ) + { + case 5: + temp = Color_ForceType( args[4] ); + outlineColor.r = INT2FIX( Color_GetR( temp ) ); + outlineColor.g = INT2FIX( Color_GetG( temp ) ); + outlineColor.b = INT2FIX( Color_GetB( temp ) ); + outlineColor.a = INT2FIX( Color_GetA( temp ) ); + case 4: + outline = NUM2DBL( args[3] ); + case 3: + temp = Vector2_ForceType( args[0] ); + x = NUM2DBL( Vector2_GetX( temp ) ); + y = NUM2DBL( Vector2_GetY( temp ) ); + radius = NUM2DBL( args[1] ); + temp = Color_ForceType( args[2] ); + color.r = INT2FIX( Color_GetR( temp ) ); + color.g = INT2FIX( Color_GetG( temp ) ); + color.b = INT2FIX( Color_GetB( temp ) ); + color.a = INT2FIX( Color_GetA( temp ) ); + break; + default: + rb_raise( rb_eArgError, "Expected 3..5 arguments but was given %d", argc ); + } + } + + sf::Shape * shape = new sf::Shape( sf::Shape::Circle( x, y, radius, color, outline, outlineColor ) ); + VALUE rbData = Data_Wrap_Struct( aKlass, 0, Shape_Free, shape ); + rb_obj_call_init( rbData, 0, 0 ); + return rbData; +} + + +void Init_Shape( void ) +{ +/* SFML namespace which contains the classes of this module. */ + VALUE sfml = rb_define_module( "SFML" ); +/* A convex, colored polygon with an optional outline. + * + * SFML::Shape is a drawable class that allows to define and display a custom convex shape on a render target. + * + * It is important to keep in mind that shapes must always be convex, otherwise they may not be drawn correctly. + * Moreover, the points must be added in the right order; using a random order would also result in an incorrect shape. + * + * A shape is made of points that have their own individual attributes: + * + * - position (relative to the origin of the shape) + * - color + * - outline color + * + * Shapes have an outline that can be enabled or not. You can control the thickness of the outline with the + * setOutlineWidth function. + * + * They also inherits all the functions from SFML::Drawable: position, rotation, scale, origin, global color + * and blend mode. + * + * Some static functions are provided to directly create common shapes such as lines, rectangles and circles: + * + * line = SFML::Shape.line( start, end, thickness, color ) + * rectangle = SFML::Shape.rectangle( rect, thickness ) + * circle = SFML::Shape.circle( center, radius, color ) + * + * A common mistake is to mix the individual points positions / colors and the global position / color of the shape. + * They are completely separate attributes that are combined when the shape is drawn (positions are added, colors are + * multiplied). + * + * line = SFML::Shape.line( [100, 100], [200, 200], 10, SFML::Color::Red ) + * + * # --> line.getPosition() is (0, 0), *not* (100, 100) + * # --> line.getColor() is white, *not* red + * + * So if you plan to change the position / color of your shape after it is created, you'd better create the points + * around the origin and with white color, and use only the global position / color (SetPosition, SetColor). + * + * Usage example: + * + * # Create a shape + * shape = SFML::Shape.new + * + * # Define its points + * shape.addPoint( 10, 10, SFML::Color::White, SFML::Color::Red ) + * shape.addPoint( 50, 10, SFML::Color::White, SFML::Color::Green ) + * shape.addPoint( 10, 50, SFML::Color::White, SFML::Color::Blue ) + * + * # Enable outline only + * shape.enableFill( false ) + * shape.enableOutline( true ) + * shape.setOutlineWidth( 10 ) + * + * # Display it + * window.draw( shape ) # window is a SFML::RenderWindow + * + * # Display static shapes + * window.draw( SFML::Shape.line( 0, 0, 10, 20, SFML::Color::Red ) ) + * window.draw( SFML::Shape.rectangle( 100, 1000, 50, 20, SFML::Color::Green ) ) + * window.draw( SFML::Shape.circle( 500, 500, 20, SFML::Color::Blue, 5, SFML::Color::Black ) ) + * + */ + globalShapeClass = rb_define_class_under( sfml, "Shape", rb_cObject ); + rb_include_module( globalShapeClass, globalDrawableModule ); + + // Class methods + rb_define_singleton_method( globalShapeClass, "new", Shape_New, -1 ); + rb_define_singleton_method( globalShapeClass, "line", Shape_Line, -1 ); + rb_define_singleton_method( globalShapeClass, "rectangle", Shape_Rectangle, -1 ); + rb_define_singleton_method( globalShapeClass, "circle", Shape_Circle, -1 ); + + // Instance methods + rb_define_method( globalShapeClass, "addPoint", Shape_AddPoint, -1 ); + rb_define_method( globalShapeClass, "getPointsCount", Shape_GetPointsCount, 0 ); + rb_define_method( globalShapeClass, "enableFill", Shape_EnableFill, 1 ); + rb_define_method( globalShapeClass, "enableOutline", Shape_EnableOutline, 1 ); + rb_define_method( globalShapeClass, "setPointPosition", Shape_SetPointPosition, -1 ); + rb_define_method( globalShapeClass, "setPointColor", Shape_SetPointColor, 2 ); + rb_define_method( globalShapeClass, "setPointOutlineColor", Shape_SetPointOutlineColor, 2 ); + rb_define_method( globalShapeClass, "setOutlineWidth", Shape_SetOutlineWidth, 1 ); + rb_define_method( globalShapeClass, "getPointPosition", Shape_GetPointPosition, 1 ); + rb_define_method( globalShapeClass, "getPointColor", Shape_GetPointColor, 1 ); + rb_define_method( globalShapeClass, "getPointOutlineColor", Shape_GetPointOutlineColor, 1 ); + rb_define_method( globalShapeClass, "getOutlineWidth", Shape_GetOutlineWidth, 0 ); + + // Instance Aliases + rb_define_alias( globalShapeClass, "add_point", "addPoint" ); + rb_define_alias( globalShapeClass, "pointsCount", "getPointsCount" ); + rb_define_alias( globalShapeClass, "points_count", "getPointsCount" ); + rb_define_alias( globalShapeClass, "enable_fill", "enableFill" ); + rb_define_alias( globalShapeClass, "fill=", "enableFill" ); + rb_define_alias( globalShapeClass, "enable_outline", "enableOutline" ); + rb_define_alias( globalShapeClass, "outline=", "enableOutline" ); + rb_define_alias( globalShapeClass, "set_point_position", "setPointPosition" ); + rb_define_alias( globalShapeClass, "set_point_color", "setPointColor" ); + rb_define_alias( globalShapeClass, "set_point_outline_color", "setPointOutlineColor" ); + rb_define_alias( globalShapeClass, "outlineWidth=", "setOutlineWidth" ); + rb_define_alias( globalShapeClass, "outline_width=", "setOutlineWidth" ); + rb_define_alias( globalShapeClass, "outlineWidth", "getOutlineWidth" ); + rb_define_alias( globalShapeClass, "outline_width", "getOutlineWidth" ); + rb_define_alias( globalShapeClass, "get_point_position", "getPointPosition" ); + rb_define_alias( globalShapeClass, "get_point_color", "getPointColor" ); + rb_define_alias( globalShapeClass, "get_point_outline_color", "getPointOutlineColor" ); +} diff --git a/bindings/ruby/sfml-graphics/graphics/Shape.hpp b/bindings/ruby/sfml-graphics/graphics/Shape.hpp new file mode 100644 index 000000000..1d1b024f9 --- /dev/null +++ b/bindings/ruby/sfml-graphics/graphics/Shape.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_SHAPE_HEADER_ +#define SFML_RUBYEXT_SHAPE_HEADER_ + +#include "ruby.h" + +// Ruby initiation function +void Init_Shape( void ); + +#endif // SFML_RUBYEXT_SHAPE_HEADER_ diff --git a/bindings/ruby/sfml-graphics/graphics/main.cpp b/bindings/ruby/sfml-graphics/graphics/main.cpp index 7214d3e38..5892f8b4e 100644 --- a/bindings/ruby/sfml-graphics/graphics/main.cpp +++ b/bindings/ruby/sfml-graphics/graphics/main.cpp @@ -24,6 +24,16 @@ #include "Color.hpp" #include "Rect.hpp" #include "Drawable.hpp" +#include "Font.hpp" +#include "Glyph.hpp" +#include "Image.hpp" +#include "Renderer.hpp" +#include "RenderImage.hpp" +#include "RenderTarget.hpp" +#include "RenderWindow.hpp" +#include "Shape.hpp" +#include "Shader.hpp" +#include "View.hpp" #include @@ -73,4 +83,14 @@ void Init_graphics( void ) Init_Color(); Init_Rect(); Init_Drawable(); + Init_Glyph(); + Init_Font(); + Init_Image(); + Init_Renderer(); + Init_RenderTarget(); + Init_RenderImage(); + Init_RenderWindow(); + Init_Shape(); + Init_Shader(); + Init_View(); }