Finished the Graphics Module. Gonna go trough all files again now and add the rdoc comments where I've left it out.

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1699 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
groogy 2010-11-23 17:21:20 +00:00
parent dbd8795df8
commit bc24802219
6 changed files with 640 additions and 1 deletions

View File

@ -0,0 +1,317 @@
/* 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 "Sprite.hpp"
#include "Vector2.hpp"
#include "Rect.hpp"
#include "Color.hpp"
#include "main.hpp"
#include <SFML/Graphics.hpp>
VALUE globalSpriteClass;
/* External classes */
extern VALUE globalVector2Class;
extern VALUE globalRectClass;
extern VALUE globalDrawableModule;
extern VALUE globalColorClass;
extern VALUE globalImageClass;
static void Sprite_Free( sf::Sprite *anObject )
{
delete anObject;
}
static VALUE Sprite_Initialize( int argc, VALUE *args, VALUE self )
{
VALUE temp = Qnil;
sf::Image *image = NULL;
sf::Vector2f position = sf::Vector2f( 0, 0 );
sf::Vector2f scale = sf::Vector2f( 1, 1 );
float rotation = 0;
sf::Color color = sf::Color::White;
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
switch( argc )
{
case 5:
temp = Color_ForceType( args[4] );
color.r = FIX2INT( Color_GetR( temp ) );
color.g = FIX2INT( Color_GetG( temp ) );
color.b = FIX2INT( Color_GetB( temp ) );
color.a = FIX2INT( Color_GetA( temp ) );
case 4:
rotation = NUM2DBL( args[3] );
case 3:
temp = Vector2_ForceType( args[2] );
scale.x = NUM2DBL( Vector2_GetX( temp ) );
scale.y = NUM2DBL( Vector2_GetY( temp ) );
case 2:
temp = Vector2_ForceType( args[1] );
position.x = NUM2DBL( Vector2_GetX( temp ) );
position.y = NUM2DBL( Vector2_GetY( temp ) );
case 1:
VALIDATE_CLASS( args[0], globalImageClass, "image" );
Data_Get_Struct( args[0], sf::Image, image );
*object = sf::Sprite( *image, position, scale, rotation, color );
rb_iv_set( self, "@__image_ref", args[0] );
case 0:
break;
default:
rb_raise( rb_eArgError, "Expected 0..5 arguments but was given %d", argc );
}
return self;
}
static VALUE Sprite_SetImage( int argc, VALUE *args, VALUE self )
{
sf::Image *image = NULL;
bool adjustToNewSize = false;
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
switch( argc )
{
case 2:
if( args[1] == Qtrue )
{
adjustToNewSize = true;
}
else if( args[1] == Qfalse )
{
adjustToNewSize = false;
}
else
{
VALIDATE_CLASS( args[1], rb_cTrueClass, "adjustToNewSize" );
}
case 1:
VALIDATE_CLASS( args[0], globalImageClass, "image" );
Data_Get_Struct( args[0], sf::Image, image );
object->SetImage( *image, adjustToNewSize );
rb_iv_set( self, "@__image_ref", args[0] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
return Qnil;
}
static VALUE Sprite_SetSubRect( VALUE self, VALUE aRectangle )
{
VALUE temp = Rect_ForceType( aRectangle );
sf::IntRect rectangle;
rectangle.Left = FIX2INT( Rect_GetLeft( temp ) );
rectangle.Top = FIX2INT( Rect_GetTop( temp ) );
rectangle.Width = FIX2INT( Rect_GetWidth( temp ) );
rectangle.Height = FIX2INT( Rect_GetHeight( temp ) );
return Qnil;
}
static VALUE Sprite_Resize( int argc, VALUE *args, VALUE self )
{
VALUE arg0 = Qnil;
float width = 0.0f;
float height = 0.0f;
switch( argc )
{
case 1:
arg0 = Vector2_ForceType( args[0] );
width = NUM2DBL( Vector2_GetX( arg0 ) );
height = NUM2DBL( Vector2_GetY( arg0 ) );
break;
case 2:
width = NUM2DBL( args[0] );
height = NUM2DBL( args[1] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
object->Resize( width, height );
return Qnil;
}
static VALUE Sprite_FlipX( VALUE self, VALUE aFlippedFlag )
{
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
if( aFlippedFlag == Qtrue )
{
object->FlipX( true );
}
else if( aFlippedFlag == Qfalse )
{
object->FlipX( false );
}
else
{
VALIDATE_CLASS( aFlippedFlag, rb_cTrueClass, "flipped" );
}
return Qnil;
}
static VALUE Sprite_FlipY( VALUE self, VALUE aFlippedFlag )
{
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
if( aFlippedFlag == Qtrue )
{
object->FlipY( true );
}
else if( aFlippedFlag == Qfalse )
{
object->FlipY( false );
}
else
{
VALIDATE_CLASS( aFlippedFlag, rb_cTrueClass, "flipped" );
}
return Qnil;
}
static VALUE Sprite_GetImage( VALUE self )
{
return rb_iv_get( self, "@__image_ref" );
}
static VALUE Sprite_GetSubRect( VALUE self )
{
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
const sf::IntRect &rect = object->GetSubRect();
return rb_funcall( globalRectClass, rb_intern( "new" ), 4,
INT2FIX( rect.Left ), INT2FIX( rect.Top ),
INT2FIX( rect.Width ), INT2FIX( rect.Height ) );
}
static VALUE Sprite_GetSize( VALUE self )
{
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
const sf::Vector2f size = object->GetSize();
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( size.x ), rb_float_new( size.y ) );
}
static VALUE Sprite_GetPixel( VALUE self, VALUE aX, VALUE aY )
{
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
const sf::Color color = object->GetPixel( FIX2UINT( aX ), FIX2UINT( aY ) );
return rb_funcall( globalColorClass, rb_intern( "new" ), 4,
INT2FIX( color.r ), INT2FIX( color.g ),
INT2FIX( color.b ), INT2FIX( color.a ) );
}
static VALUE Sprite_New( int argc, VALUE *args, VALUE aKlass )
{
sf::Sprite *object = new sf::Sprite();
VALUE rbData = Data_Wrap_Struct( aKlass, 0, Sprite_Free, object );
rb_obj_call_init( rbData, argc, args );
return rbData;
}
void Init_Sprite( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Drawable representation of an image, with its own transformations, color, blend mode, etc.
*
* SFML::Sprite is a drawable class that allows to easily display an image (or a part of it) on a render target.
*
* It inherits all the functions from SFML::Drawable: position, rotation, scale, origin, global color and blend mode.
* It also adds sprite-specific properties such as the image to use, the part of it to display, and some convenience
* functions to flip or resize the sprite.
*
* SFML::Sprite works in combination with the SFML::Image class, which loads and provides the pixel data of a
* given image.
*
* The separation of SFML::Sprite and SFML::Image allows more flexibility and better performances: indeed a SFML::Image
* is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side,
* a SFML::Sprite is a lightweight object which can use the pixel data of a SFML::Image and draw it with its own
* transformation / color / blending attributes.
*
* It is important to note that the SFML::Sprite instance doesn't copy the image that it uses, it only keeps a reference
* to it. Thus, a SFML::Image must not be destructed while it is used by a SFML::Sprite (i.e. never write a function that
* uses a local SFML::Image instance for creating a sprite).
*
* NOTE: This is the ruby bindings so the images will be managed by the ruby garbage collector and thus the image won't
* be destructed until all sprites referencing it is destructed. But it's still a good practice to keep in mind.
*
* Usage example:
*
* # Declare and load an image
* image = SFML::Image.new
* image.loadFromFile( "image.png" )
*
* # Create a sprite
* sprite = SFML::Sprite.new
* sprite.image = image
* sprite.subRect = [10, 10, 50, 30]
* sprite.resize( 100, 60 )
*
* # Display it
* window.draw( sprite ) # window is a SFML::RenderWindow
*
*/
globalSpriteClass = rb_define_class_under( sfml, "Sprite", rb_cObject );
rb_include_module( globalSpriteClass, globalDrawableModule );
// Class methods
rb_define_singleton_method( globalSpriteClass, "new", Sprite_New, -1 );
// Instance methods
rb_define_method( globalSpriteClass, "initialize", Sprite_Initialize, -1 );
rb_define_method( globalSpriteClass, "setImage", Sprite_SetImage, -1 );
rb_define_method( globalSpriteClass, "setSubRect", Sprite_SetSubRect, 1 );
rb_define_method( globalSpriteClass, "resize", Sprite_Resize, -1 );
rb_define_method( globalSpriteClass, "flipX", Sprite_FlipX, 1 );
rb_define_method( globalSpriteClass, "flipY", Sprite_FlipY, 1 );
rb_define_method( globalSpriteClass, "getImage", Sprite_GetImage, 0 );
rb_define_method( globalSpriteClass, "getSubRect", Sprite_GetSubRect, 0 );
rb_define_method( globalSpriteClass, "getSize", Sprite_GetSize, 0 );
rb_define_method( globalSpriteClass, "getPixel", Sprite_GetPixel, 2 );
// Instance Aliases
rb_define_alias( globalSpriteClass, "image=", "setImage" );
rb_define_alias( globalSpriteClass, "set_image", "setImage" );
rb_define_alias( globalSpriteClass, "image", "getImage" );
rb_define_alias( globalSpriteClass, "get_image", "getImage" );
rb_define_alias( globalSpriteClass, "subRect=", "setSubRect" );
rb_define_alias( globalSpriteClass, "sub_rect=", "setSubRect" );
rb_define_alias( globalSpriteClass, "subRect", "getSubRect" );
rb_define_alias( globalSpriteClass, "sub_rect", "getSubRect" );
rb_define_alias( globalSpriteClass, "flip_x", "flipX" );
rb_define_alias( globalSpriteClass, "flip_y", "flipY" );
rb_define_alias( globalSpriteClass, "flip_x=", "flipX" );
rb_define_alias( globalSpriteClass, "flip_y=", "flipY" );
rb_define_alias( globalSpriteClass, "flipX=", "flipX" );
rb_define_alias( globalSpriteClass, "flipY=", "flipY" );
rb_define_alias( globalSpriteClass, "get_size", "getSize" );
rb_define_alias( globalSpriteClass, "size", "getSize" );
rb_define_alias( globalSpriteClass, "get_pixel", "getPixel" );
}

View File

@ -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_SPRITE_HEADER_
#define SFML_RUBYEXT_SPRITE_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Sprite( void );
#endif // SFML_RUBYEXT_SPRITE_HEADER_

View File

@ -0,0 +1,248 @@
/* 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 "Text.hpp"
#include "Vector2.hpp"
#include "Rect.hpp"
#include "Color.hpp"
#include "main.hpp"
#include <SFML/Graphics/Text.hpp>
VALUE globalTextClass;
/* External classes */
extern VALUE globalVector2Class;
extern VALUE globalRectClass;
extern VALUE globalDrawableModule;
extern VALUE globalColorClass;
extern VALUE globalFontClass;
static void Text_Free( sf::Text *anObject )
{
delete anObject;
}
static VALUE Text_Initialize( int argc, VALUE *args, VALUE self )
{
VALUE temp = Qnil;
sf::String string = "";
const sf::Font *font = &sf::Font::GetDefaultFont();
unsigned int characterSize = 30;
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
switch( argc )
{
case 3:
characterSize = FIX2UINT( args[2] );
case 2:
VALIDATE_CLASS( args[1], globalFontClass, "font" );
Data_Get_Struct( args[1], sf::Font, font );
rb_iv_set( self, "@__font_ref", args[1] );
case 1:
string = rb_string_value_cstr( &args[0] );
*object = sf::Text( string, *font, characterSize );
case 0:
break;
default:
rb_raise( rb_eArgError, "Expected 0..3 arguments but was given %d", argc );
}
return self;
}
static VALUE Text_SetString( VALUE self, VALUE aString )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
object->SetString( rb_string_value_cstr( &aString ) );
return Qnil;
}
static VALUE Text_SetFont( VALUE self, VALUE aFont )
{
VALIDATE_CLASS( aFont, globalFontClass, "font" );
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
sf::Font *font = NULL;
Data_Get_Struct( self, sf::Font, font );
object->SetFont( *font );
rb_iv_set( self, "@__font_ref", aFont );
return Qnil;
}
static VALUE Text_SetCharacterSize( VALUE self, VALUE aSize )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
object->SetCharacterSize( FIX2UINT( aSize ) );
return Qnil;
}
static VALUE Text_SetStyle( VALUE self, VALUE aStyle )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
object->SetStyle( FIX2UINT( aStyle ) );
return Qnil;
}
static VALUE Text_GetString( VALUE self )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
return rb_str_new2( object->GetString().ToAnsiString().c_str() );
}
static VALUE Text_GetFont( VALUE self )
{
return rb_iv_get( self, "@__font_ref" );
}
static VALUE Text_GetCharacterSize( VALUE self )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
return INT2FIX( object->GetCharacterSize() );
}
static VALUE Text_GetStyle( VALUE self )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
return INT2FIX( object->GetStyle() );
}
static VALUE Text_GetCharacterPos( VALUE self, VALUE anIndex )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
const sf::Vector2f pos = object->GetCharacterPos( FIX2UINT( anIndex ) );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( pos.x ), rb_float_new( pos.y ) );
}
static VALUE Text_GetRect( VALUE self )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
const sf::FloatRect rect = object->GetRect();
return rb_funcall( globalVector2Class, rb_intern( "new" ), 4,
rb_float_new( rect.Left ), rb_float_new( rect.Top ),
rb_float_new( rect.Width ), rb_float_new( rect.Height ) );
}
static VALUE Text_New( int argc, VALUE *args, VALUE aKlass )
{
sf::Text *object = new sf::Text();
VALUE rbData = Data_Wrap_Struct( aKlass, 0, Text_Free, object );
rb_obj_call_init( rbData, argc, args );
return rbData;
}
static void CreateStyleEnum()
{
rb_define_const( globalTextClass, "Regular", INT2FIX( sf::Text::Regular ) );
rb_define_const( globalTextClass, "Bold", INT2FIX( sf::Text::Bold ) );
rb_define_const( globalTextClass, "Italic", INT2FIX( sf::Text::Italic ) );
rb_define_const( globalTextClass, "Underlined", INT2FIX( sf::Text::Underlined ) );
}
void Init_Text( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Drawable representation of an image, with its own transformations, color, blend mode, etc.
*
* SFML::Sprite is a drawable class that allows to easily display an image (or a part of it) on a render target.
*
* It inherits all the functions from SFML::Drawable: position, rotation, scale, origin, global color and blend mode.
* It also adds sprite-specific properties such as the image to use, the part of it to display, and some convenience
* functions to flip or resize the sprite.
*
* SFML::Sprite works in combination with the SFML::Image class, which loads and provides the pixel data of a
* given image.
*
* The separation of SFML::Sprite and SFML::Image allows more flexibility and better performances: indeed a SFML::Image
* is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side,
* a SFML::Sprite is a lightweight object which can use the pixel data of a SFML::Image and draw it with its own
* transformation / color / blending attributes.
*
* It is important to note that the SFML::Sprite instance doesn't copy the image that it uses, it only keeps a reference
* to it. Thus, a SFML::Image must not be destructed while it is used by a SFML::Sprite (i.e. never write a function that
* uses a local SFML::Image instance for creating a sprite).
*
* NOTE: This is the ruby bindings so the images will be managed by the ruby garbage collector and thus the image won't
* be destructed until all sprites referencing it is destructed. But it's still a good practice to keep in mind.
*
* Usage example:
*
* # Declare and load an image
* image = SFML::Image.new
* image.loadFromFile( "image.png" )
*
* # Create a sprite
* sprite = SFML::Sprite.new
* sprite.image = image
* sprite.subRect = [10, 10, 50, 30]
* sprite.resize( 100, 60 )
*
* # Display it
* window.draw( sprite ) # window is a SFML::RenderWindow
*
*/
globalTextClass = rb_define_class_under( sfml, "Text", rb_cObject );
rb_include_module( globalTextClass, globalDrawableModule );
CreateStyleEnum();
// Class methods
rb_define_singleton_method( globalTextClass, "new", Text_New, -1 );
// Instance methods
rb_define_method( globalTextClass, "initialize", Text_Initialize, -1 );
rb_define_method( globalTextClass, "setString", Text_SetString, 1 );
rb_define_method( globalTextClass, "setFont", Text_SetFont, 1 );
rb_define_method( globalTextClass, "setCharacterSize", Text_SetCharacterSize, 1 );
rb_define_method( globalTextClass, "setStyle", Text_SetStyle, 1 );
rb_define_method( globalTextClass, "getString", Text_GetString, 0 );
rb_define_method( globalTextClass, "getFont", Text_GetFont, 0 );
rb_define_method( globalTextClass, "getCharacterSize", Text_GetCharacterSize, 0 );
rb_define_method( globalTextClass, "getStyle", Text_GetStyle, 0 );
rb_define_method( globalTextClass, "getCharacterPos", Text_GetCharacterPos, 1 );
rb_define_method( globalTextClass, "getRect", Text_GetRect, 0 );
// Instance Aliases
rb_define_alias( globalTextClass, "string=", "setString" );
rb_define_alias( globalTextClass, "string", "getString" );
rb_define_alias( globalTextClass, "font=", "setFont" );
rb_define_alias( globalTextClass, "font", "getFont" );
rb_define_alias( globalTextClass, "characterSize=", "setCharacterSize" );
rb_define_alias( globalTextClass, "character_size=", "setCharacterSize" );
rb_define_alias( globalTextClass, "characterSize", "getCharacterSize" );
rb_define_alias( globalTextClass, "character_size", "getCharacterSize" );
rb_define_alias( globalTextClass, "style=", "setStyle" );
rb_define_alias( globalTextClass, "style", "getStyle" );
rb_define_alias( globalTextClass, "get_character_pos", "getCharacterPos" );
rb_define_alias( globalTextClass, "rect", "getRect" );
}

View File

@ -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_TEXT_HEADER_
#define SFML_RUBYEXT_TEXT_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Text( void );
#endif // SFML_RUBYEXT_TEXT_HEADER_

View File

@ -33,6 +33,8 @@
#include "RenderWindow.hpp"
#include "Shape.hpp"
#include "Shader.hpp"
#include "Sprite.hpp"
#include "Text.hpp"
#include "View.hpp"
#include <SFML/Graphics.hpp>
@ -92,5 +94,7 @@ void Init_graphics( void )
Init_RenderWindow();
Init_Shape();
Init_Shader();
Init_Sprite();
Init_Text();
Init_View();
}

View File

@ -10,6 +10,12 @@ input = app.input
shape = SFML::Shape.rectangle( [-10, -10, 20, 20], SFML::Color::White )
image = SFML::Image.new
image.create( 100, 100, [255, 0, 0] )
sprite = SFML::Sprite.new( image, [500, 500] )
text = SFML::Text.new( "This is a test!" )
while app.open?
while event = app.get_event
if event.type == SFML::Event::Closed
@ -20,5 +26,7 @@ while app.open?
app.clear
shape.position = [input.mouseX, input.mouseY]
app.draw shape
app.draw sprite
app.draw text
app.display
end