Removed the ruby binding (it has its own repository now -> https://github.com/Groogy/rbSFML)

This commit is contained in:
Laurent Gomila 2011-03-28 07:50:52 +02:00
parent fb1820862a
commit 4217ea68f6
88 changed files with 0 additions and 13255 deletions

View File

@ -1,194 +0,0 @@
require 'rake'
require 'rubygems'
require 'rdoc/task'
require 'rake/clean'
require 'rake/gempackagetask'
require 'rbconfig'
require 'pp'
include Config
include Rake
# Configurable section
RUBYSFML_VERSION = "2.0"
SO_SRCS = {'audio' => FileList.new('sfml-audio/audio/*.cpp'),
'graphics' => FileList.new('sfml-graphics/graphics/*.cpp'),
'window' => FileList.new('sfml-window/window/*.cpp'),
'system' => FileList.new('sfml-system/system/*.cpp'),
'all' => FileList.new('sfml-all/all/*.cpp') }
OTHER_SRCS = FileList.new('shared/*.cpp')
OBJDIR = 'obj'
SODIR = 'sfml'
spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::CURRENT
s.name = "rbSFML"
s.version = RUBYSFML_VERSION
s.authors = ["Henrik Valter Vogelius Hansson", 'Brandon Whitehead']
s.email = "groogy@groogy.se"
s.homepage = 'http://sfml-dev.org'
s.summary = "Ruby bindings for SFML 2.0"
s.has_rdoc = true
s.requirements << 'none'
s.require_path = ''
s.files = FileList.new do |fl|
fl.include("sfml-audio/audio/*.cpp", "sfml-audio/audio/*.hpp")
fl.include("sfml-graphics/graphics/*.cpp", "sfml-graphics/graphics/*.hpp")
fl.include("sfml-window/window/*.cpp", "sfml-window/window/*.hpp")
fl.include("sfml-system/system/*.cpp", "sfml-system/system/*.hpp")
fl.include("sfml-all/all/*.cpp", "sfml-all/all/*.hpp")
end
s.extensions = ["Rakefile"]
s.description = <<-EOF
rbSFML are bindings for the SFML library version 2.0 for Ruby.
SFML or Simple Fast Multimedia library is is a free multimedia C++ API
that provides you low and high level access to graphics, input, audio, etc.
EOF
s.extra_rdoc_files = FileList.new do |fl|
fl.include "doc/*.rdoc"
end
end
verbose(false)
# Do not touch
SO_OBJS = {}
SO_SRCS.each do |file, list|
SO_OBJS[file] = list.collect { |fn| File.join("#{OBJDIR}/#{file}", File.basename(fn).ext('o')) }
end
OTHER_OBJS = OTHER_SRCS.collect {|fn| File.join("#{OBJDIR}/shared/#{file}", File.basename(fn).ext('o')) }
SO_LIBS = []
SO_SRCS.each_key {|file| SO_LIBS << "#{SODIR}/#{file}.so"}
SO_SRCS.each_key {|dir| CLEAN.include("#{OBJDIR}/#{dir}")}
SO_OBJS.each_value {|list| CLEAN.include(list)}
CLEAN.include(OTHER_OBJS)
CLEAN.include("#{OBJDIR}/shared")
CLEAN.include(OBJDIR)
SO_LIBS.each {|so_file| CLOBBER.include(so_file)}
CLOBBER.include(SODIR)
# Sets the default task to build
task :default => [:build]
desc "Compiles and builds the library"
task :build
desc "Recompiles the library"
task :rebuild
desc "Installs the generated files"
task :install
desc "Uninstalls the generated files"
task :uninstall
task :clean do
puts "Cleaning out temporary generated files"
end
task :clobber do
puts "Cleaning out all generated files"
end
task :rebuild => [:clobber, :build] do
end
Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_tar_bz2 = true
end
RDoc::Task.new do |rd|
rd.title = "RSFML #{RUBYSFML_VERSION} Documentation"
rd.rdoc_files.include(SO_SRCS.values)
rd.options << '--line-numbers' << '--quiet' << '--all'
rd.rdoc_dir = "doc"
end
CFLAGS = CONFIG['CFLAGS']
CC = CONFIG['CC']
# CONFIG['INSTALL_PROGRAM']
INSTALL = "install"
LOCATION = CONFIG['sitearchdir'] + '/sfml'
RUBYSFML_INC = "shared"
SFML_INC = ENV.key?('SFML_INCLUDE') ? ENV['SFML_INCLUDE'] : '../../include'
SFML_LIB = ENV.key?('SFML_LIB') ? ENV['SFML_LIB'] : '../../lib'
SFML_LIBS = '-lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system'
RUBY_INC = CONFIG['rubyhdrdir']
RUBY_LIB = (CONFIG['ENABLE_SHARED'] == 'yes' ? CONFIG['LIBRUBYARG_SHARED'] : CONFIG['LIBRUBYARG_STATIC']) + ' ' + CONFIG['SOLIBS']
RUBY_LIB_PATH = CONFIG['libdir']
LINK = CONFIG['LDSHAREDXX']
# Windows screws up this variable...
LINK.sub!("$(if $(filter-out -g -g0,#{CONFIG["debugflags"]}),,-s)", '')
LINK_FLAGS = CONFIG['DLDFLAGS'] + " " + CONFIG['LDFLAGS']
LINK_FLAGS.sub!("$(DEFFILE)", "")
SO_SRCS.each_key {|dir| directory "#{OBJDIR}/#{dir}"}
directory "#{OBJDIR}/shared"
directory SODIR
directory LOCATION
OTHER_SRCS.each do |srcfile|
objdir = "#{OBJDIR}/shared"
objfile = File.join(objdir, File.basename(srcfile).ext('o'))
file objfile => [srcfile, objdir] do
puts "Compiling #{File.basename(srcfile)}"
sh "#{CC} #{CFLAGS} -c #{srcfile} -o #{objfile} -I#{SFML_INC} -I#{RUBY_INC} -I#{RUBY_INC}/#{CONFIG['arch']} -I#{RUBYSFML_INC}"
end
end
task :shared => OTHER_OBJS
SO_SRCS.each do |so_file, list|
begin
list.each do |srcfile|
objdir = "#{OBJDIR}/#{so_file}"
objfile = File.join(objdir, File.basename(srcfile).ext('o'))
file objfile => [srcfile, objdir] do
puts "Compiling #{File.basename(srcfile)}"
sh "#{CC} #{CFLAGS} -c #{srcfile} -o #{objfile} -I#{SFML_INC} -I#{RUBY_INC} -I#{RUBY_INC}/#{CONFIG['arch']} -I#{RUBYSFML_INC}"
#p "#{CC} #{CFLAGS} -c #{srcfile} -o #{objfile} -I#{SFML_INC} -I#{RUBY_INC} -I#{RUBY_INC}/#{CONFIG['arch']} -I#{RUBYSFML_INC}"
end
end
rescue
end
end
SO_OBJS.each do |so_file, objs|
begin
prestep = "pre#{so_file}".to_sym
task prestep do
puts "Creating #{so_file}.so"
end
file "#{SODIR}/#{so_file}.so" => [prestep, :shared, SODIR, *objs] do
puts "Linking files to create #{so_file}.so"
#p "#{LINK} -o #{SODIR}/#{so_file}.so #{objs} -L. -L#{SFML_LIB} -L#{RUBY_LIB_PATH} #{LINK_FLAGS} #{RUBY_LIB} #{SFML_LIBS}"
sh "#{LINK} -o #{SODIR}/#{so_file}.so #{objs} #{OTHER_OBJS} -L. -L#{SFML_LIB} -L#{RUBY_LIB_PATH} #{LINK_FLAGS} #{RUBY_LIB} #{SFML_LIBS}"
end
rescue
end
end
task :install => [:build, LOCATION] do
puts "Installing library to #{LOCATION}"
begin
SO_SRCS.each_key do |so_file|
sh "#{INSTALL} #{SODIR}/#{so_file}.so #{LOCATION}"
end
rescue Exception=>e
p e
end
end
task :uninstall do
puts "Uninstalling library to #{LOCATION}"
begin
SO_SRCS.each_key do |so_file|
sh "rm -f #{LOCATION}/#{so_file}.so"
end
sh "rm -rf #{LOCATION}"
rescue
end
end
task :build => [:shared, *SO_LIBS] do
end

View File

@ -1,25 +0,0 @@
rbSFML
----
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.

View File

@ -1,12 +0,0 @@
#include "ruby.h"
extern "C"
{
void Init_all( void )
{
rb_require( "sfml/system" );
rb_require( "sfml/window" );
rb_require( "sfml/graphics" );
rb_require( "sfml/audio" );
}
}

View File

@ -1,25 +0,0 @@
# 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.
require 'mkmf'
dir_config("all")
create_makefile("sfml/all", "all")

View File

@ -1,201 +0,0 @@
/* 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 "Listener.hpp"
#include "Vector3.hpp"
#include "main.hpp"
#include <SFML/Audio/Listener.hpp>
VALUE globalListenerModule;
/* External class */
extern VALUE globalVector3Class;
/* call-seq:
* Listener.setGlobalVolume( volume )
*
* Change the global volume of all the sounds and musics.
*
* The volume is a number between 0 and 100; it is combined with the individual volume of each sound / music. The
* default value for the volume is 100 (maximum).
*/
static VALUE Listener_SetGlobalVolume( VALUE self, VALUE aVolume )
{
sf::Listener::SetGlobalVolume( NUM2DBL( aVolume ) );
return Qnil;
}
/* call-seq:
* Listener.getGlobalVolume() -> float
*
* Get the current value of the global volume.
*/
static VALUE Listener_GetGlobalVolume( VALUE self )
{
return rb_float_new( sf::Listener::GetGlobalVolume() );
}
/* call-seq:
* Listener.setPosition( vector3 )
* Listener.setPosition( x, y, z )
*
* Set the position of the listener in the scene.
*
* The default listener's position is (0, 0, 0).
*/
static VALUE Listener_SetPosition( int argc, VALUE *args, VALUE self )
{
VALUE temp;
float x, y, z;
switch( argc )
{
case 3:
x = NUM2DBL( args[0] );
y = NUM2DBL( args[1] );
z = NUM2DBL( args[2] );
break;
case 1:
temp = Vector3_ForceType( args[0] );
x = NUM2DBL( Vector3_GetX( temp ) );
y = NUM2DBL( Vector3_GetY( temp ) );
z = NUM2DBL( Vector3_GetZ( temp ) );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 3 arguments but was given %d", argc );
}
sf::Listener::SetPosition( x, y, z );
return Qnil;
}
/* call-seq:
* Listener.getPosition() -> vector3
*
* Get the current position of the listener in the scene.
*/
static VALUE Listener_GetPosition( VALUE self )
{
const sf::Vector3f pos = sf::Listener::GetPosition();
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, rb_float_new( pos.x ), rb_float_new( pos.y ), rb_float_new( pos.z ) );
}
/* call-seq:
* Listener.setDirection( vector3 )
* Listener.setDirection( x, y, z )
*
* Set the orientation of the listener in the scene.
*
* The orientation defines the 3D axes of the listener (left, up, front) in the scene. The orientation vector doesn't
* have to be normalized. The default listener's orientation is (0, 0, -1).
*/
static VALUE Listener_SetDirection( int argc, VALUE *args, VALUE self )
{
VALUE temp;
float x, y, z;
switch( argc )
{
case 3:
x = NUM2DBL( args[0] );
y = NUM2DBL( args[1] );
z = NUM2DBL( args[2] );
break;
case 1:
temp = Vector3_ForceType( args[0] );
x = NUM2DBL( Vector3_GetX( temp ) );
y = NUM2DBL( Vector3_GetY( temp ) );
z = NUM2DBL( Vector3_GetZ( temp ) );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 3 arguments but was given %d", argc );
}
sf::Listener::SetDirection( x, y, z );
return Qnil;
}
/* call-seq:
* Listener.getDirection() -> vector3
*
* Get the current orientation of the listener in the scene.
*/
static VALUE Listener_GetDirection( VALUE self )
{
const sf::Vector3f pos = sf::Listener::GetDirection();
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, rb_float_new( pos.x ), rb_float_new( pos.y ), rb_float_new( pos.z ) );
}
void Init_Listener( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* The audio listener is the point in the scene from where all the sounds are heard.
*
* The audio listener defines the global properties of the audio environment, it defines where and how sounds and
* musics are heard.
*
* If sf::View is the eyes of the user, then sf::Listener is his ears (by the way, they are often linked together --
* same position, orientation, etc.).
*
* sf::Listener is a simple interface, which allows to setup the listener in the 3D audio environment (position and
* direction), and to adjust the global volume.
*
* Because the listener is unique in the scene, sf::Listener only contains static functions and doesn't have to be
* instanciated.
*
* Usage example:
*
* # Move the listener to the position (1, 0, -5)
* SFML::Listener.setPosition( 1, 0, -5 )
*
* # Make it face the right axis (1, 0, 0)
* SFML::Listener.setDirection( 1, 0, 0 )
*
* # Reduce the global volume
* SFML::Listener.setGlobalVolume( 50 )
*
*/
globalListenerModule = rb_define_module_under( sfml, "Listener" );
// Module methods
rb_define_module_function( globalListenerModule, "setGlobalVolume", Listener_SetGlobalVolume, 1 );
rb_define_module_function( globalListenerModule, "getGlobalVolume", Listener_GetGlobalVolume, 0 );
rb_define_module_function( globalListenerModule, "setPosition", Listener_SetPosition, -1 );
rb_define_module_function( globalListenerModule, "getPosition", Listener_GetPosition, 0 );
rb_define_module_function( globalListenerModule, "setDirection", Listener_SetDirection, -1 );
rb_define_module_function( globalListenerModule, "getDirection", Listener_GetDirection, 0 );
// Aliases
rb_define_alias( CLASS_OF( globalListenerModule ), "set_global_volume", "setGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "globalVolume=", "setGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "global_volume=", "setGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "globalVolume", "getGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "global_volume", "getGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "get_global_volume", "getGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "set_postion", "setGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "position=", "setGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "position", "getGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "get_position", "getGlobalVolume" );
rb_define_alias( CLASS_OF( globalListenerModule ), "set_direction", "setDirection" );
rb_define_alias( CLASS_OF( globalListenerModule ), "direction=", "setDirection" );
rb_define_alias( CLASS_OF( globalListenerModule ), "direction", "getDirection" );
rb_define_alias( CLASS_OF( globalListenerModule ), "get_direction", "getDirection" );
}

View File

@ -1,31 +0,0 @@
/* 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_LISTENER_HEADER_
#define SFML_RUBYEXT_LISTENER_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Listener( void );
#endif // SFML_RUBYEXT_LISTENER_HEADER_

View File

@ -1,150 +0,0 @@
/* 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 "Music.hpp"
#include "main.hpp"
#include <SFML/Audio/Music.hpp>
VALUE globalMusicClass;
/* External classes */
extern VALUE globalSoundStreamClass;
static VALUE Music_Free( sf::Music *anObject )
{
delete anObject;
}
/* call-seq:
* music.openFromFile() -> true or false
*
* Open a music from an audio file.
*
* This function doesn't start playing the music (call Play() to do so). Here is a complete list of all the supported
* audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2,
* caf, wve, mpc2k, rf64.
*/
static VALUE Music_OpenFromFile( VALUE self, VALUE aFilename )
{
sf::Music *object = NULL;
Data_Get_Struct( self, sf::Music, object );
if( object->OpenFromFile( rb_string_value_cstr( &aFilename ) ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* Music.new() -> music
* Music.new( filename ) -> music
*
* Will create a new music instance.
*
* If a filename argument is specified then music#openFromFile will be called on the created instance.
*/
static VALUE Music_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 0 )
{
rb_funcall2( self, rb_intern( "openFromFile" ), argc, args );
}
return self;
}
/* call-seq:
* music.getDuration() -> float
*
* Get the total duration of the music.
*/
static VALUE Music_GetDuration( VALUE self )
{
sf::Music *object = NULL;
Data_Get_Struct( self, sf::Music, object );
return rb_float_new( object->GetDuration() );
}
static VALUE Music_Alloc( VALUE aKlass )
{
sf::Music *object = new sf::Music();
return Data_Wrap_Struct( aKlass, 0, Music_Free, object );
}
void Init_Music( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Streamed music played from an audio file.
*
* Musics are sounds that are streamed rather than completely loaded in memory.
*
* This is especially useful for compressed musics that usually take hundreds of MB when they are uncompressed: by
* streaming it instead of loading it entirely, you avoid saturating the memory and have almost no loading delay.
*
* Apart from that, a sf::Music has almost the same features as the sf::SoundBuffer / sf::Sound pair: you can
* play/pause/stop it, request its parameters (channels, sample rate), change the way it is played (pitch, volume,
* 3D position, ...), etc.
*
* As a sound stream, a music is played in its own thread in order not to block the rest of the program. This means
* that you can leave the music alone after calling Play(), it will manage itself very well.
*
* Usage example:
*
* # Declare a new music
* music = SFML::Music.new
*
* # Open it from an audio file
* if music.openFromFile( "music.ogg" ) == false
* # error...
* end
*
* # Change some parameters
* music.setPosition( 0, 1, 10 ) # change its 3D position
* music.setPitch( 2 ) # increase the pitch
* music.setVolume( 50 ) # reduce the volume
* music.setLoop( true ) # make it loop
*
* # Play it
* music.play()
*/
globalMusicClass = rb_define_class_under( sfml, "Music", globalSoundStreamClass );
// Class methods
//rb_define_singleton_method( globalMusicClass, "new", Music_New, -1 );
rb_define_alloc_func( globalMusicClass, Music_Alloc );
// Instance methods
rb_define_method( globalMusicClass, "initialize", Music_Initialize, -1 );
rb_define_method( globalMusicClass, "openFromFile", Music_OpenFromFile, 1 );
rb_define_method( globalMusicClass, "getDuration", Music_GetDuration, 0 );
// Instance Aliases
rb_define_alias( globalMusicClass, "open_from_file", "openFromFile" );
rb_define_alias( globalMusicClass, "openFile", "openFromFile" );
rb_define_alias( globalMusicClass, "open_file", "openFromFile" );
rb_define_alias( globalMusicClass, "get_duration", "getDuration" );
rb_define_alias( globalMusicClass, "duration", "getDuration" );
}

View File

@ -1,31 +0,0 @@
/* 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_MUSIC_HEADER_
#define SFML_RUBYEXT_MUSIC_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Music( void );
#endif // SFML_RUBYEXT_MUSIC_HEADER_

View File

@ -1,352 +0,0 @@
/* 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 "Sound.hpp"
#include "Vector3.hpp"
#include "main.hpp"
#include <SFML/Audio/Sound.hpp>
VALUE globalSoundClass;
/* External classes */
extern VALUE globalVector3Class;
extern VALUE globalSoundSourceClass;
extern VALUE globalSoundBufferClass;
static void Sound_Free( sf::Sound *anObject )
{
delete anObject;
}
/* call-seq:
* Sound.new() -> sound
*
* Creates a new sound instance.
*/
static VALUE Sound_Initialize( int argc, VALUE *args, VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
switch( argc )
{
case 5:
{
VALUE temp = Vector3_ForceType( args[4] );
sf::Vector3f position;
position.x = NUM2DBL( Vector3_GetX( temp ) );
position.y = NUM2DBL( Vector3_GetY( temp ) );
position.z = NUM2DBL( Vector3_GetZ( temp ) );
object->SetPosition( position );
}
case 4:
object->SetVolume( NUM2DBL( args[3] ) );
case 3:
object->SetPitch( NUM2DBL( args[2] ) );
case 2:
if( args[1] == Qtrue )
{
object->SetLoop( true );
}
else if( args[1] == Qfalse )
{
object->SetLoop( false );
}
else
{
VALIDATE_CLASS( args[1], rb_cTrueClass, "loop" );
}
case 1:
{
VALIDATE_CLASS( args[0], globalSoundBufferClass, "buffer" );
sf::SoundBuffer *buffer = NULL;
Data_Get_Struct( args[0], sf::SoundBuffer, buffer );
object->SetBuffer( *buffer );
rb_iv_set( self, "@__buffer_ref", args[0] );
}
case 0:
break;
default:
rb_raise( rb_eArgError, "Expected 0..5 arguments but was given %d", argc );
}
return self;
}
static VALUE Sound_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Sound *selfObject = NULL;
Data_Get_Struct( self, sf::Sound, selfObject );
sf::Sound *sourceObject = NULL;
Data_Get_Struct( aSource, sf::Sound, sourceObject );
*selfObject = *sourceObject;
return self;
}
/* call-seq:
* sound.play()
*
* Start or resume playing the sound.
*
* This function starts the sound if it was stopped, resumes it if it was paused, and does nothing it is it already
* playing. This function uses its own thread so that it doesn't block the rest of the program while the sound is played.
*/
static VALUE Sound_Play( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
object->Play();
return Qnil;
}
/* call-seq:
* sound.pause()
*
* Pause the sound.
*
* This function pauses the sound if it was playing, otherwise (sound already paused or stopped) it has no effect.
*/
static VALUE Sound_Pause( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
object->Pause();
return Qnil;
}
/* call-seq:
* sound.stop()
*
* Stop playing the sound.
*
* This function stops the sound if it was playing or paused, and does nothing if it was already stopped. It also
* resets the playing position (unlike pause()).
*/
static VALUE Sound_Stop( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
object->Stop();
return Qnil;
}
/* call-seq:
* sound.setBuffer( buffer )
*
* Set the source buffer containing the audio data to play.
*
* It is important to note that the sound buffer is not copied, thus the sf::SoundBuffer instance must remain alive as
* long as it is attached to the sound.
*/
static VALUE Sound_SetBuffer( VALUE self, VALUE aBuffer )
{
VALIDATE_CLASS( aBuffer, globalSoundBufferClass, "buffer" );
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
sf::SoundBuffer *buffer = NULL;
Data_Get_Struct( aBuffer, sf::SoundBuffer, buffer );
object->SetBuffer( *buffer );
rb_iv_set( self, "@__buffer_ref", aBuffer );
return Qnil;
}
/* call-seq:
* sound.setLoop( loop )
*
* Set whether or not the sound should loop after reaching the end.
*
* If set, the sound will restart from beginning after reaching the end and so on, until it is stopped or
* setLoop(false) is called. The default looping state for sound is false.
*/
static VALUE Sound_SetLoop( VALUE self, VALUE aLoop )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
if( aLoop == Qtrue )
{
object->SetLoop( true );
}
else if( aLoop == Qfalse )
{
object->SetLoop( false );
}
else
{
VALIDATE_CLASS( aLoop, rb_cTrueClass, "loop" );
}
return Qnil;
}
/* call-seq:
* sound.setPlayingOffset( offset )
*
* Change the current playing position of the sound.
*
* The playing position can be changed when the sound is either paused or playing.
*/
static VALUE Sound_SetPlayingOffset( VALUE self, VALUE aOffset )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
object->SetPlayingOffset( NUM2DBL( aOffset ) );
return Qnil;
}
/* call-seq:
* sound.getBuffer() -> buffer
*
* Get the audio buffer attached to the sound.
*/
static VALUE Sound_GetBuffer( VALUE self )
{
return rb_iv_get( self, "@__buffer_ref" );
}
/* call-seq:
* sound.getLoop() -> true or false
*
* Tell whether or not the sound is in loop mode.
*/
static VALUE Sound_GetLoop( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
if( object->GetLoop() == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* sound.getPlayingOffset() -> float
*
* Get the current playing position of the sound.
*/
static VALUE Sound_GetPlayingOffset( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
return rb_float_new( object->GetPlayingOffset() );
}
/* call-seq:
* sound.getStatus() -> fixnum
*
* Get the current status of the sound (stopped, paused, playing).
*/
static VALUE Sound_GetStatus( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
return INT2FIX( static_cast< int >( object->GetStatus() ) );
}
/* call-seq:
* sound.resetBuffer()
*
* Reset the internal buffer of the sound.
*
* This function is for internal use only, you don't have to use it. It is called by the SFML::SoundBuffer that this
* sound uses, when it is destroyed in order to prevent the sound from using a dead buffer.
*/
static VALUE Sound_ResetBuffer( VALUE self )
{
sf::Sound *object = NULL;
Data_Get_Struct( self, sf::Sound, object );
object->ResetBuffer();
return Qnil;
}
static VALUE Sound_Alloc( VALUE aKlass )
{
sf::Sound *object = new sf::Sound();
return Data_Wrap_Struct( aKlass, 0, Sound_Free, object );
}
void Init_Sound( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Regular sound that can be played in the audio environment.
*
* SFML::Sound is the class to use to play sounds.
*
* It provides:
*
* - Control (play, pause, stop)
* - Ability to modify output parameters in real-time (pitch, volume, ...)
* - 3D spatial features (position, attenuation, ...).
*
* SFML::Sound is perfect for playing short sounds that can fit in memory and require no latency, like foot steps or
* gun shots. For longer sounds, like background musics or long speeches, rather see sf::Music
* (which is based on streaming).
*
* In order to work, a sound must be given a buffer of audio data to play. Audio data (samples) is stored in
* sf::SoundBuffer, and attached to a sound with the SetBuffer() function. The buffer object attached to a sound
* must remain alive as long as the sound uses it. Note that multiple sounds can use the same sound buffer at the
* same time.
*
* Usage example:
*
* buffer = SFML::SoundBuffer.new
* buffer.loadFromFile( "sound.wav" )
*
* sound = SFML::Sound.new
* sound.setBuffer( buffer )
* sound.play()
*/
globalSoundClass = rb_define_class_under( sfml, "Sound", globalSoundSourceClass );
// Class methods
//rb_define_singleton_method( globalSoundClass, "new", Sound_New, -1 );
rb_define_alloc_func( globalSoundClass, Sound_Alloc );
// Instance methods
rb_define_method( globalSoundClass, "initialize", Sound_Initialize, 0 );
rb_define_method( globalSoundClass, "initialize_copy", Sound_InitializeCopy, 1 );
rb_define_method( globalSoundClass, "play", Sound_Play, 0 );
rb_define_method( globalSoundClass, "pause", Sound_Pause, 0 );
rb_define_method( globalSoundClass, "stop", Sound_Stop, 0 );
rb_define_method( globalSoundClass, "setBuffer", Sound_SetBuffer, 1 );
rb_define_method( globalSoundClass, "getBuffer", Sound_GetBuffer, 0 );
rb_define_method( globalSoundClass, "setLoop", Sound_SetLoop, 1 );
rb_define_method( globalSoundClass, "getLoop", Sound_GetLoop, 0 );
rb_define_method( globalSoundClass, "setPlayingOffset", Sound_SetPlayingOffset, 1 );
rb_define_method( globalSoundClass, "getPlayingOffset", Sound_GetPlayingOffset, 0 );
rb_define_method( globalSoundClass, "getStatus", Sound_GetStatus, 0 );
rb_define_method( globalSoundClass, "resetBuffer", Sound_ResetBuffer, 0 );
// Aliases
rb_define_alias( globalSoundClass, "buffer=", "setBuffer" );
rb_define_alias( globalSoundClass, "buffer", "getBuffer" );
rb_define_alias( globalSoundClass, "loop=", "setLoop" );
rb_define_alias( globalSoundClass, "loop", "getLoop" );
rb_define_alias( globalSoundClass, "playingOffset=", "setPlayingOffset" );
rb_define_alias( globalSoundClass, "playing_offset=", "setPlayingOffset" );
rb_define_alias( globalSoundClass, "playingOffset", "getPlayingOffset" );
rb_define_alias( globalSoundClass, "playing_offset", "getPlayingOffset" );
rb_define_alias( globalSoundClass, "status", "getStatus" );
rb_define_alias( globalSoundClass, "reset_buffer", "resetBuffer" );
}

View File

@ -1,31 +0,0 @@
/* 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_SOUND_HEADER_
#define SFML_RUBYEXT_SOUND_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Sound( void );
#endif // SFML_RUBYEXT_SOUND_HEADER_

View File

@ -1,337 +0,0 @@
/* 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 "SoundBuffer.hpp"
#include "main.hpp"
#include <SFML/Audio/SoundBuffer.hpp>
VALUE globalSoundBufferClass;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void SoundBuffer_Free( sf::SoundBuffer *anObject )
{
delete anObject;
}
/* call-seq:
* sound_buffer.loadFromFile( filename ) -> true or false
*
* Load the sound buffer from a file.
*
* Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc,
* ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
*/
static VALUE SoundBuffer_LoadFromFile( VALUE self, VALUE aFileName )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
if( object->LoadFromFile( rb_string_value_cstr( &aFileName ) ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* sound_buffer.loadFromSamples( samples, samplesCount, channelsCount, sampleRate ) -> true or false
*
* Load the sound buffer from an array of audio samples.
*
* The assumed format of the audio samples is 16 bits signed integer.
*/
static VALUE SoundBuffer_LoadFromSamples( VALUE self, VALUE someSamples, VALUE aSamplesCount, VALUE aChannelsCount, VALUE aSampleRate )
{
const unsigned int rawSamplesCount = FIX2UINT( aSamplesCount );
const unsigned int rawChannelsCount = FIX2UINT( aChannelsCount );
const unsigned int rawSampleRate = FIX2UINT( aSampleRate );
VALIDATE_CLASS( someSamples, rb_cArray, "samples" );
sf::Int16 * const tempData = new sf::Int16[rawSamplesCount];
VALUE samples = rb_funcall( someSamples, rb_intern("flatten"), 0 );
for(unsigned long index = 0; index < rawSamplesCount; index++)
{
sf::Int16 val = NUM2INT( rb_ary_entry( samples, index ) );
tempData[index] = val;
}
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
bool result = object->LoadFromSamples( tempData, rawSamplesCount, rawChannelsCount, rawSampleRate );
delete[] tempData;
if( result == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* sound_buffer.saveToFile( filename ) -> true or false
*
* Save the sound buffer to an audio file.
*
* Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
* w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.
*/
static VALUE SoundBuffer_SaveToFile( VALUE self, VALUE aFileName )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
if( object->SaveToFile( rb_string_value_cstr( &aFileName ) ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* sound_buffer.getSamples() -> array of samples
*
* Get the array of audio samples stored in the buffer.
*
* The format of the returned samples is 16 bits signed integer. The total number of samples in this array is given
* by the getSamplesCount() function.
*/
static VALUE SoundBuffer_GetSamples( VALUE self )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
const unsigned int samplesCount = object->GetSamplesCount();
const sf::Int16 *const samplesPtr = object->GetSamples();
VALUE samples = rb_ary_new2( samplesCount );
for(unsigned long index = 0; index < samplesCount; index++)
{
rb_ary_store( samples, index, INT2FIX( samplesPtr[index] ) );
}
return samples;
}
/* call-seq:
* sound_buffer.getSamplesCount() -> fixnum
*
* Get the number of samples stored in the buffer.
*
* The array of samples can be accessed with the getSamples() function.
*/
static VALUE SoundBuffer_GetSamplesCount( VALUE self )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
return INT2FIX( object->GetSamplesCount() );
}
/* call-seq:
* sound_buffer.getSampleRate() -> fixnum
*
* Get the sample rate of the sound.
*
* The sample rate is the number of samples played per second. The higher, the better the quality (for example,
* 44100 samples/s is CD quality).
*/
static VALUE SoundBuffer_GetSampleRate( VALUE self )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
return INT2FIX( object->GetSampleRate() );
}
/* call-seq:
* sound_buffer.getChannelsCount() -> float
*
* Get the total duration of the sound.
*/
static VALUE SoundBuffer_GetChannelsCount( VALUE self )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
return INT2FIX( object->GetChannelsCount() );
}
/* call-seq:
* sound_buffer.getDuration() -> fixnum
*
* Get the number of channels used by the sound.
*
* If the sound is mono then the number of channels will be 1, 2 for stereo, etc.
*/
static VALUE SoundBuffer_GetDuration( VALUE self )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
return rb_float_new( object->GetDuration() );
}
/* call-seq:
* SoundBuffer.new() -> sound_buffer
* SoundBuffer.new( filename ) -> sound_buffer
* SoundBuffer.new( samples, samplesCount, channelsCount, sampleRate ) -> sound_buffer
*
* Will create a new sound buffer instance.
*
* If a filename argument is specified then sound_buffer#loadFromFile will be called on the created instance. If
* samples, samplesCount, channelsCount and sampleRate are specified then image#loadFromPixels will be called on the
* created instance.
*/
static VALUE SoundBuffer_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 1 )
{
rb_funcall2( self, rb_intern( "loadFromSampels" ), argc, args );
}
else if( argc > 0 )
{
rb_funcall2( self, rb_intern( "loadFromFile" ), argc, args );
}
return self;
}
static VALUE SoundBuffer_InitializeCopy( VALUE self, VALUE aSource )
{
sf::SoundBuffer *object = NULL;
Data_Get_Struct( self, sf::SoundBuffer, object );
sf::SoundBuffer *source = NULL;
Data_Get_Struct( aSource, sf::SoundBuffer, source );
*object = *source;
}
/* call-seq:
* SoundBuffer.new() -> sound_buffer
*
* Creates an sound buffer instance for us.
*/
static VALUE SoundBuffer_Alloc( VALUE aKlass )
{
sf::SoundBuffer *object = new sf::SoundBuffer();
return Data_Wrap_Struct( aKlass, 0, SoundBuffer_Free, object );
}
void Init_SoundBuffer( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Storage for audio samples defining a sound.
*
* A sound buffer holds the data of a sound, which is an array of audio samples.
*
* A sample is a 16 bits signed integer that defines the amplitude of the sound at a given time. The sound is then
* restituted by playing these samples at a high rate (for example, 44100 samples per second is the standard rate used
* for playing CDs). In short, audio samples are like image pixels, and a SFML::SoundBuffer is similar to a SFML::Image.
*
* A sound buffer can be loaded from a file (see loadFromFile() for the complete list of supported formats), from
* memory or directly from an array of samples. It can also be saved back to a file.
*
* Sound buffers alone are not very useful: they hold the audio data but cannot be played. To do so, you need to use
* the SFML::Sound class, which provides functions to play/pause/stop the sound as well as changing the way it is
* outputted (volume, pitch, 3D position, ...). This separation allows more flexibility and better performances:
* indeed a SFML::SoundBuffer is a heavy resource, and any operation on it is slow (often too slow for real-time
* applications). On the other side, a SFML::Sound is a lightweight object, which can use the audio data of a sound
* buffer and change the way it is played without actually modifying that data. Note that it is also possible to bind
* several SFML::Sound instances to the same SFML::SoundBuffer.
*
* It is important to note that the SFML::Sound instance doesn't copy the buffer that it uses, it only keeps a reference
* to it. Thus, a SFML::SoundBuffer must not be destructed while it is used by a SFML::Sound (i.e. never write a function
* that uses a local SFML::SoundBuffer instance for loading a sound).
*
* Usage example:
*
* # Declare a new sound buffer
* buffer = SFML::SoundBuffer.new
*
* # Load it from a file
* if buffer.loadFromFile( "sound.wav" ) == false
* # error...
* end
*
* # Create a sound source and bind it to the buffer
* sound1 = SFML::Sound.new
* sound1.setBuffer( buffer )
*
* # Play the sound
* sound1.play()
*
* # Create another sound source bound to the same buffer
* sound2 = SFML::Sound.new
* sound2.setBuffer( buffer )
*
* # Play it with a higher pitch -- the first sound remains unchanged
* sound2.setPitch( 2 )
* sound2.play()
*/
globalSoundBufferClass = rb_define_class_under( sfml, "SoundBuffer", rb_cObject );
// Class methods
//rb_define_singleton_method( globalSoundBufferClass, "new", SoundBuffer_New, -1 );
rb_define_alloc_func( globalSoundBufferClass, SoundBuffer_Alloc );
// Instance methods
rb_define_method( globalSoundBufferClass, "initialize", SoundBuffer_Initialize, -1 );
rb_define_method( globalSoundBufferClass, "initialize_copy", SoundBuffer_InitializeCopy, 1 );
rb_define_method( globalSoundBufferClass, "loadFromFile", SoundBuffer_LoadFromFile, 1 );
rb_define_method( globalSoundBufferClass, "loadFromSamples", SoundBuffer_LoadFromSamples, 4 );
rb_define_method( globalSoundBufferClass, "saveToFile", SoundBuffer_SaveToFile, 1 );
rb_define_method( globalSoundBufferClass, "getSamples", SoundBuffer_GetSamples, 0 );
rb_define_method( globalSoundBufferClass, "getSamplesCount", SoundBuffer_GetSamplesCount, 0 );
rb_define_method( globalSoundBufferClass, "getSampleRate", SoundBuffer_GetSampleRate, 0 );
rb_define_method( globalSoundBufferClass, "getChannelsCount", SoundBuffer_GetChannelsCount, 0 );
rb_define_method( globalSoundBufferClass, "getDuration", SoundBuffer_GetDuration, 0 );
// Instance Aliases
rb_define_alias( globalSoundBufferClass, "load_from_file", "loadFromFile" );
rb_define_alias( globalSoundBufferClass, "loadFile", "loadFromFile" );
rb_define_alias( globalSoundBufferClass, "load_file", "loadFromFile" );
rb_define_alias( globalSoundBufferClass, "load_from_samples", "loadFromSamples" );
rb_define_alias( globalSoundBufferClass, "loadSamples", "loadFromSamples" );
rb_define_alias( globalSoundBufferClass, "load_samples", "loadFromSamples" );
rb_define_alias( globalSoundBufferClass, "save_to_file", "saveToFile" );
rb_define_alias( globalSoundBufferClass, "save", "saveToFile" );
rb_define_alias( globalSoundBufferClass, "get_samples", "getSamples" );
rb_define_alias( globalSoundBufferClass, "samples", "getSamples" );
rb_define_alias( globalSoundBufferClass, "get_samples_count", "getSamplesCount" );
rb_define_alias( globalSoundBufferClass, "samples_count", "getSamplesCount" );
rb_define_alias( globalSoundBufferClass, "samplesCount", "getSamplesCount" );
rb_define_alias( globalSoundBufferClass, "get_sample_rate", "getSampleRate" );
rb_define_alias( globalSoundBufferClass, "sample_rate", "getSampleRate" );
rb_define_alias( globalSoundBufferClass, "sampleRate", "getSampleRate" );
rb_define_alias( globalSoundBufferClass, "get_channels_count", "getChannelsCount" );
rb_define_alias( globalSoundBufferClass, "channels_count", "getChannelsCount" );
rb_define_alias( globalSoundBufferClass, "channelsCount", "getChannelsCount" );
rb_define_alias( globalSoundBufferClass, "get_duration", "getDuration" );
rb_define_alias( globalSoundBufferClass, "duration", "getDuration" );
}

View File

@ -1,31 +0,0 @@
/* 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_SOUND_BUFFER_HEADER_
#define SFML_RUBYEXT_SOUND_BUFFER_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_SoundBuffer( void );
#endif // SFML_RUBYEXT_SOUND_BUFFER_HEADER_

View File

@ -1,210 +0,0 @@
/* 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 "SoundBufferRecorder.hpp"
#include "main.hpp"
#include <SFML/Audio/SoundBufferRecorder.hpp>
VALUE globalSoundBufferRecorderClass;
/* External classes */
extern VALUE globalSoundRecorderClass;
extern VALUE globalSoundBufferClass;
class rbSoundBufferRecorder : public sf::SoundBufferRecorder
{
public:
rbSoundBufferRecorder()
{
}
void Init( VALUE rubySelf )
{
mySelf = rubySelf;
myOnStartID = rb_intern( "onStart" );
myOnStopID = rb_intern( "onStop" );
myOnProcessSamplesID = rb_intern( "onProcessSamples" );
}
protected:
virtual bool OnStart()
{
if( rb_respond_to( mySelf, myOnStartID ) == 0 )
{
return true;
}
else
{
if( rb_funcall( mySelf, myOnStartID, 0 ) == Qfalse )
{
return false;
}
else
{
return true;
}
}
}
virtual void OnStop()
{
if( rb_respond_to( mySelf, myOnStopID ) != 0 )
{
rb_funcall( mySelf, myOnStopID, 0 );
}
}
virtual bool OnProcessSamples( const sf::Int16 *someSamples, std::size_t someCount )
{
VALUE samples = rb_ary_new2( someCount );
for(unsigned long index = 0; index < someCount; index++)
{
rb_ary_store( samples, index, INT2FIX( someSamples[index] ) );
}
if( rb_funcall( mySelf, myOnProcessSamplesID, 2, samples, INT2FIX( someCount ) ) == Qfalse )
{
return false;
}
else
{
return true;
}
}
VALUE mySelf;
ID myOnStartID;
ID myOnStopID;
ID myOnProcessSamplesID;
};
static void SoundBufferRecorder_Free( rbSoundBufferRecorder * anObject )
{
delete anObject;
}
/* call-seq:
* sound_buffer_recorder.getBuffer() -> sound_buffer
*
* Get the sound buffer containing the captured audio data.
*
* The sound buffer is valid only after the capture has ended. This function provides a read-only access to the internal
* sound buffer, but it can be copied if you need to make any modification to it.
*/
static VALUE SoundBufferRecorder_GetBuffer( VALUE self )
{
sf::SoundBufferRecorder *object = NULL;
Data_Get_Struct( self, sf::SoundBufferRecorder, object );
const sf::SoundBuffer &buffer = object->GetBuffer();
VALUE rbData = Data_Wrap_Struct( globalSoundBufferClass, 0, 0, const_cast< sf::SoundBuffer * >( &buffer ) );
rb_iv_set( rbData, "@__owner_ref", self );
return rbData;
}
/* call-seq:
* SoundBufferRecorder.new() -> sound_buffer_recorder
*
* Creates a sound buffer recorder instance for us.
*/
static VALUE SoundBufferRecorder_Alloc( VALUE aKlass )
{
rbSoundBufferRecorder *object = new rbSoundBufferRecorder();
return Data_Wrap_Struct( aKlass, 0, SoundBufferRecorder_Free, object );
}
void Init_SoundBufferRecorder( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Abstract base class for capturing sound data.
*
* SFML::SoundRecorder provides a simple interface to access the audio recording capabilities of the computer
* (the microphone).
*
* As an abstract base class, it only cares about capturing sound samples, the task of making something useful with
* them is left to the derived class. Note that SFML provides a built-in specialization for saving the captured data
* to a sound buffer (see sf::SoundBufferRecorder).
*
* A derived class has only one virtual function to override:
*
* - onProcessSamples provides the new chunks of audio samples while the capture happens
*
* Moreover, two additionnal virtual functions can be overriden as well if necessary:
*
* - onStart is called before the capture happens, to perform custom initializations
* - onStop is called after the capture ends, to perform custom cleanup
*
* The audio capture feature may not be supported or activated on every platform, thus it is recommended to check
* its availability with the isAvailable() function. If it returns false, then any attempt to use an audio recorder
* will fail.
*
* It is important to note that the audio capture happens in a separate thread, so that it doesn't block the rest of
* the program. In particular, the OnProcessSamples and OnStop virtual functions (but not OnStart) will be called from
* this separate thread. It is important to keep this in mind, because you may have to take care of synchronization
* issues if you share data between threads.
*
* Usage example:
*
* class CustomRecorder < SFML::SoundRecorder
* def onStart() # optional
* # Initialize whatever has to be done before the capture starts
* ...
*
* # Return true to start playing
* return true
* end
*
* def onProcessSamples( samples, samplesCount )
* # Do something with the new chunk of samples (store them, send them, ...)
* ...
*
* # Return true to continue playing
* return true
* end
*
* def onStop() # optional
* # Clean up whatever has to be done after the capture ends
* ...
* end
* end
*
* # Usage
* if CustomRecorder.isAvailable()
* recorder = CustomRecorder.new
* recorder.start()
* ...
* recorder.stop()
* end
*/
globalSoundBufferRecorderClass = rb_define_class_under( sfml, "SoundBufferRecorder", globalSoundRecorderClass );
// Class methods
//rb_define_singleton_method( globalSoundBufferRecorderClass, "new", SoundBufferRecorder_New, -1 );
rb_define_alloc_func( globalSoundBufferRecorderClass, SoundBufferRecorder_Alloc );
// Instance methods
rb_define_method( globalSoundRecorderClass, "getBuffer", SoundBufferRecorder_GetBuffer, 0 );
// Instance Aliases
rb_define_alias( globalSoundRecorderClass, "buffer", "getBuffer" );
}

View File

@ -1,31 +0,0 @@
/* 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_SOUND_BUFFER_RECORDER_HEADER_
#define SFML_RUBYEXT_SOUND_BUFFER_RECORDER_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_SoundBufferRecorder( void );
#endif // SFML_RUBYEXT_SOUND_BUFFER_RECORDER_HEADER_

View File

@ -1,271 +0,0 @@
/* 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 "SoundRecorder.hpp"
#include "main.hpp"
#include <SFML/Audio/SoundRecorder.hpp>
VALUE globalSoundRecorderClass;
/* External classes */
extern VALUE globalNonCopyableModule;
class rbSoundRecorder : public sf::SoundRecorder
{
public:
rbSoundRecorder()
{
}
void Init( VALUE rubySelf )
{
mySelf = rubySelf;
myOnStartID = rb_intern( "onStart" );
myOnStopID = rb_intern( "onStop" );
myOnProcessSamplesID = rb_intern( "onProcessSamples" );
}
protected:
virtual bool OnStart()
{
if( rb_respond_to( mySelf, myOnStartID ) == 0 )
{
return true;
}
else
{
if( rb_funcall( mySelf, myOnStartID, 0 ) == Qfalse )
{
return false;
}
else
{
return true;
}
}
}
virtual void OnStop()
{
if( rb_respond_to( mySelf, myOnStopID ) != 0 )
{
rb_funcall( mySelf, myOnStopID, 0 );
}
}
virtual bool OnProcessSamples( const sf::Int16 *someSamples, std::size_t someCount )
{
VALUE samples = rb_ary_new2( someCount );
for(unsigned long index = 0; index < someCount; index++)
{
rb_ary_store( samples, index, INT2FIX( someSamples[index] ) );
}
if( rb_funcall( mySelf, myOnProcessSamplesID, 2, samples, INT2FIX( someCount ) ) == Qfalse )
{
return false;
}
else
{
return true;
}
}
VALUE mySelf;
ID myOnStartID;
ID myOnStopID;
ID myOnProcessSamplesID;
};
static void SoundRecorder_Free( rbSoundRecorder * anObject )
{
delete anObject;
}
/* call-seq:
* sound_recorder.start()
*
* Start the capture.
*
* The sampleRate parameter defines the number of audio samples captured per second. The higher, the better the
* quality (for example, 44100 samples/sec is CD quality). This function uses its own thread so that it doesn't block
* the rest of the program while the capture runs. Please note that only one capture can happen at the same time.
*/
static VALUE SoundRecorder_Start( int argc, VALUE *args, VALUE self )
{
sf::SoundRecorder *object = NULL;
Data_Get_Struct( self, sf::SoundRecorder, object );
unsigned int sampleRate = 44100;
switch( argc )
{
case 1:
sampleRate = FIX2UINT( args[0] );
case 0:
object->Start( sampleRate );
break;
default:
rb_raise( rb_eArgError, "Expected 0 or 1 arguments but was given %d", argc );
}
return Qnil;
}
/* call-seq:
* sound_recorder.stop()
*
* Stop the capture.
*/
static VALUE SoundRecorder_Stop( VALUE self )
{
sf::SoundRecorder *object = NULL;
Data_Get_Struct( self, sf::SoundRecorder, object );
object->Stop();
return Qnil;
}
/* call-seq:
* sound_recorder.getSampleRate() -> fixnum
*
* Get the sample rate.
*
* The sample rate defines the number of audio samples captured per second. The higher, the better the quality
*(for example, 44100 samples/sec is CD quality).
*/
static VALUE SoundRecorder_GetSampleRate( VALUE self )
{
sf::SoundRecorder *object = NULL;
Data_Get_Struct( self, sf::SoundRecorder, object );
return INT2FIX( object->GetSampleRate() );
}
/* call-seq:
* SoundRecorder.new() -> sound_recorder
*
* Creates a sound recorder instance for us.
*/
static VALUE SoundRecorder_Alloc( VALUE aKlass )
{
rbSoundRecorder *object = new rbSoundRecorder();
return Data_Wrap_Struct( aKlass, 0, SoundRecorder_Free, object );
}
/* call-seq:
* SoundRecorder.isAvailable() -> true or false
*
* Check if the system supports audio capture.
*
* This function should always be called before using the audio capture features. If it returns false, then any attempt
* to use sf::SoundRecorder or one of its derived classes will fail.
*/
static VALUE SoundRecorder_IsAvailable( VALUE aKlass )
{
return ( sf::SoundRecorder::IsAvailable() == true ? Qtrue : Qfalse );
}
void Init_SoundRecorder( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Abstract base class for capturing sound data.
*
* SFML::SoundRecorder provides a simple interface to access the audio recording capabilities of the computer
* (the microphone).
*
* As an abstract base class, it only cares about capturing sound samples, the task of making something useful with
* them is left to the derived class. Note that SFML provides a built-in specialization for saving the captured data
* to a sound buffer (see sf::SoundBufferRecorder).
*
* A derived class has only one virtual function to override:
*
* - onProcessSamples provides the new chunks of audio samples while the capture happens
*
* Moreover, two additionnal virtual functions can be overriden as well if necessary:
*
* - onStart is called before the capture happens, to perform custom initializations
* - onStop is called after the capture ends, to perform custom cleanup
*
* The audio capture feature may not be supported or activated on every platform, thus it is recommended to check
* its availability with the isAvailable() function. If it returns false, then any attempt to use an audio recorder
* will fail.
*
* It is important to note that the audio capture happens in a separate thread, so that it doesn't block the rest of
* the program. In particular, the OnProcessSamples and OnStop virtual functions (but not OnStart) will be called from
* this separate thread. It is important to keep this in mind, because you may have to take care of synchronization
* issues if you share data between threads.
*
* Usage example:
*
* class CustomRecorder < SFML::SoundRecorder
* def onStart() # optional
* # Initialize whatever has to be done before the capture starts
* ...
*
* # Return true to start playing
* return true
* end
*
* def onProcessSamples( samples, samplesCount )
* # Do something with the new chunk of samples (store them, send them, ...)
* ...
*
* # Return true to continue playing
* return true
* end
*
* def onStop() # optional
* # Clean up whatever has to be done after the capture ends
* ...
* end
* end
*
* # Usage
* if CustomRecorder.isAvailable()
* recorder = CustomRecorder.new
* recorder.start()
* ...
* recorder.stop()
* end
*/
globalSoundRecorderClass = rb_define_class_under( sfml, "SoundRecorder", rb_cObject );
rb_include_module( globalSoundRecorderClass, globalNonCopyableModule );
// Class methods
//rb_define_singleton_method( globalSoundRecorderClass, "new", SoundRecorder_New, -1 );
rb_define_alloc_func( globalSoundRecorderClass, SoundRecorder_Alloc );
rb_define_singleton_method( globalSoundRecorderClass, "isAvailable", SoundRecorder_IsAvailable, 0 );
// Instance methods
rb_define_method( globalSoundRecorderClass, "start", SoundRecorder_Start, -1 );
rb_define_method( globalSoundRecorderClass, "stop", SoundRecorder_Stop, 0 );
rb_define_method( globalSoundRecorderClass, "getSampleRate", SoundRecorder_GetSampleRate, 0 );
// Class Aliases
rb_define_alias( CLASS_OF( globalSoundRecorderClass ), "is_available", "isAvailable" );
rb_define_alias( CLASS_OF( globalSoundRecorderClass ), "available?", "isAvailable" );
// Instance Aliases
rb_define_alias( globalSoundRecorderClass, "get_sample_rate", "getSampleRate" );
rb_define_alias( globalSoundRecorderClass, "sampleRate", "getSampleRate" );
rb_define_alias( globalSoundRecorderClass, "sample_rate", "getSampleRate" );
rb_define_alias( globalSoundRecorderClass, "on_start", "on_start" );
rb_define_alias( globalSoundRecorderClass, "on_stop", "on_stop" );
}

View File

@ -1,31 +0,0 @@
/* 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_SOUND_RECORDER_HEADER_
#define SFML_RUBYEXT_SOUND_RECORDER_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_SoundRecorder( void );
#endif // SFML_RUBYEXT_SOUND_RECORDER_HEADER_

View File

@ -1,304 +0,0 @@
/* 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 "SoundSource.hpp"
#include "Vector3.hpp"
#include "main.hpp"
#include <SFML/Audio/SoundSource.hpp>
VALUE globalSoundSourceClass;
/* External classes */
extern VALUE globalVector3Class;
/* call-seq:
* sound_source.getAttenuation() -> float
*
* Get the attenuation factor of the sound.
*/
static VALUE SoundSource_GetAttenuation( VALUE self )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
return rb_float_new( object->GetAttenuation() );
}
/* call-seq:
* sound_source.getMinDistance() -> float
*
* Get the minimum distance of the sound.
*/
static VALUE SoundSource_GetMinDistance( VALUE self )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
return rb_float_new( object->GetMinDistance() );
}
/* call-seq:
* sound_source.getPitch() -> float
*
* Get the pitch of the sound.
*/
static VALUE SoundSource_GetPitch( VALUE self )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
return rb_float_new( object->GetPitch() );
}
/* call-seq:
* sound_source.getPosition() -> vector3
*
* Get the 3D position of the sound in the audio scene.
*/
static VALUE SoundSource_GetPosition( VALUE self )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
sf::Vector3f pos = object->GetPosition();
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, rb_float_new( pos.x ), rb_float_new( pos.y ), rb_float_new( pos.z ) );
}
/* call-seq:
* sound_source.getVolume() -> float
*
* Get the volume of the sound.
*/
static VALUE SoundSource_GetVolume( VALUE self )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
return rb_float_new( object->GetVolume() );
}
/* call-seq:
* sound_source.isRelativeToListener() -> true or false
*
* Tell whether the sound's position is relative to the listener or is absolute.
*/
static VALUE SoundSource_IsRelativeToListener( VALUE self )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
return ( object->IsRelativeToListener() == true ? Qtrue : Qfalse );
}
/* call-seq:
* sound_source.setAttenuation( value )
*
* Set the attenuation factor of the sound.
*
* The attenuation is a multiplicative factor which makes the sound more or less loud according to its distance from
* the listener. An attenuation of 0 will produce a non-attenuated sound, i.e. its volume will always be the same
* whether it is heard from near or from far. On the other hand, an attenuation value such as 100 will make the sound
* fade out very quickly as it gets further from the listener. The default value of the attenuation is 1.
*/
static VALUE SoundSource_SetAttenuation( VALUE self, VALUE aValue )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
object->SetAttenuation( NUM2DBL( aValue ) );
return Qnil;
}
/* call-seq:
* sound_source.setMinDistance( value )
*
* Set the minimum distance of the sound.
*
* The "minimum distance" of a sound is the maximum distance at which it is heard at its maximum volume. Further than
* the minimum distance, it will start to fade out according to its attenuation factor. A value of 0 ("inside the head
* of the listener") is an invalid value and is forbidden. The default value of the minimum distance is 1.
*/
static VALUE SoundSource_SetMinDistance( VALUE self, VALUE aValue )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
object->SetMinDistance( NUM2DBL( aValue ) );
return Qnil;
}
/* call-seq:
* sound_source.setPitch( value )
*
* Set the pitch of the sound.
*
* The pitch represents the perceived fundamental frequency of a sound; thus you can make a sound more acute or grave
* by changing its pitch. A side effect of changing the pitch is to modify the playing speed of the sound as well. The
* default value for the pitch is 1.
*/
static VALUE SoundSource_SetPitch( VALUE self, VALUE aValue )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
object->SetPitch( NUM2DBL( aValue ) );
return Qnil;
}
/* call-seq:
* sound_source.setPosition( x, y, z )
* sound_source.setPosition( vector3 )
*
* Set the 3D position of the sound in the audio scene.
*
* Only sounds with one channel (mono sounds) can be spatialized. The default position of a sound is (0, 0, 0).
*/
static VALUE SoundSource_SetPosition( int argc, VALUE *args, VALUE self )
{
float x, y, z;
switch( argc )
{
case 3:
{
x = NUM2DBL( args[0] );
y = NUM2DBL( args[1] );
z = NUM2DBL( args[2] );
break;
}
case 1:
{
VALUE temp = Vector3_ForceType( args[0] );
x = NUM2DBL( Vector3_GetX( temp ) );
y = NUM2DBL( Vector3_GetY( temp ) );
z = NUM2DBL( Vector3_GetZ( temp ) );
break;
}
default:
rb_raise( rb_eArgError, "Expected 1 or 3 arguments but was given %d", argc );
}
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
object->SetPosition( x, y, z );
return Qnil;
}
/* call-seq:
* sound_source.setRelativeToListener( value )
*
* Make the sound's position relative to the listener or absolute.
*
* Making a sound relative to the listener will ensure that it will always be played the same way regardless the
* position of the listener. This can be useful for non-spatialized sounds, sounds that are produced by the listener,
* or sounds attached to it. The default value is false (position is absolute).
*/
static VALUE SoundSource_SetRelativeToListener( VALUE self, VALUE aValue )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
if( aValue == Qtrue )
{
object->SetRelativeToListener( true );
}
else if( aValue == Qfalse )
{
object->SetRelativeToListener( false );
}
else
{
VALIDATE_CLASS( aValue, rb_cTrueClass, "relative" );
}
return Qnil;
}
/* call-seq:
* sound_source.setVolume( value )
*
* Set the volume of the sound.
*
* The volume is a value between 0 (mute) and 100 (full volume). The default value for the volume is 100.
*/
static VALUE SoundSource_SetVolume( VALUE self, VALUE aValue )
{
sf::SoundSource *object = NULL;
Data_Get_Struct( self, sf::SoundSource, object );
object->SetVolume( NUM2DBL( aValue ) );
return Qnil;
}
static VALUE SoundSource_Initialize( VALUE self )
{
rb_raise( rb_eNotImpError, "Trying to construct instance of abstract class" );
}
static void DefineStatusEnum( void )
{
rb_define_const( globalSoundSourceClass, "Stopped", INT2FIX( sf::SoundSource::Stopped ) );
rb_define_const( globalSoundSourceClass, "Paused", INT2FIX( sf::SoundSource::Paused ) );
rb_define_const( globalSoundSourceClass, "Playing", INT2FIX( sf::SoundSource::Playing ) );
}
void Init_SoundSource( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Base class defining a sound's properties.
*
* SFML::SoundSource is not meant to be used directly, it only serves as a common base for all audio objects that can
* live in the audio environment.
*
* It defines several properties for the sound: pitch, volume, position, attenuation, etc. All of them can be changed
* at any time with no impact on performances.
*/
globalSoundSourceClass = rb_define_class_under( sfml, "SoundSource", rb_cObject );
DefineStatusEnum();
// Instance methods
rb_define_method( globalSoundSourceClass, "getAttenuation", SoundSource_GetAttenuation, 0 );
rb_define_method( globalSoundSourceClass, "getMinDistance", SoundSource_GetMinDistance, 0 );
rb_define_method( globalSoundSourceClass, "getPitch", SoundSource_GetPitch, 0 );
rb_define_method( globalSoundSourceClass, "getPosition", SoundSource_GetPosition, 0 );
rb_define_method( globalSoundSourceClass, "getVolume", SoundSource_GetVolume, 0 );
rb_define_method( globalSoundSourceClass, "isRelativeToListener", SoundSource_IsRelativeToListener, 0 );
rb_define_method( globalSoundSourceClass, "setAttenuation", SoundSource_GetAttenuation, 1 );
rb_define_method( globalSoundSourceClass, "setMinDistance", SoundSource_GetMinDistance, 1 );
rb_define_method( globalSoundSourceClass, "setPitch", SoundSource_GetPitch, 1 );
rb_define_method( globalSoundSourceClass, "setPosition", SoundSource_GetPosition, -1 );
rb_define_method( globalSoundSourceClass, "setRelativeToListener", SoundSource_IsRelativeToListener, 1 );
rb_define_method( globalSoundSourceClass, "setVolume", SoundSource_GetVolume, 1 );
// Aliases
rb_define_alias( globalSoundSourceClass, "attenuation", "getAttenuation" );
rb_define_alias( globalSoundSourceClass, "attenuation=", "setAttenuation" );
rb_define_alias( globalSoundSourceClass, "minDistance", "getMinDistance" );
rb_define_alias( globalSoundSourceClass, "minDistance=", "setMinDistance" );
rb_define_alias( globalSoundSourceClass, "min_distance", "getMinDistance" );
rb_define_alias( globalSoundSourceClass, "min_distance=", "setMinDistance" );
rb_define_alias( globalSoundSourceClass, "pitch", "getPitch" );
rb_define_alias( globalSoundSourceClass, "pitch=", "setPitch" );
rb_define_alias( globalSoundSourceClass, "position", "getPosition" );
rb_define_alias( globalSoundSourceClass, "position=", "setPosition" );
rb_define_alias( globalSoundSourceClass, "volume", "getVolume" );
rb_define_alias( globalSoundSourceClass, "volume=", "setVolume" );
rb_define_alias( globalSoundSourceClass, "relativeToListener?", "isRelativeToListener" );
rb_define_alias( globalSoundSourceClass, "is_relative_to_listener", "isRelativeToListener" );
rb_define_alias( globalSoundSourceClass, "relative_to_listener?", "isRelativeToListener" );
rb_define_alias( globalSoundSourceClass, "relativeToListener=", "setRelativeToListener" );
rb_define_alias( globalSoundSourceClass, "relative_to_listener=", "setRelativeToListener" );
}

View File

@ -1,31 +0,0 @@
/* 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_SOUND_SOURCE_HEADER_
#define SFML_RUBYEXT_SOUND_SOURCE_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_SoundSource( void );
#endif // SFML_RUBYEXT_SOUND_SOURCE_HEADER_

View File

@ -1,400 +0,0 @@
/* 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 "SoundStream.hpp"
#include "main.hpp"
#include <SFML/Audio/SoundStream.hpp>
VALUE globalSoundStreamClass;
/* External classes */
extern VALUE globalSoundSourceClass;
extern VALUE globalNonCopyableModule;
class rbSoundStream : public sf::SoundStream
{
public:
rbSoundStream()
{
}
~rbSoundStream()
{
if( myData != NULL )
{
delete[] myData;
}
}
void Init( VALUE rubySelf )
{
mySelf = rubySelf;
myOnGetDataID = rb_intern( "onGetData" );
myOnSeekID = rb_intern( "onSeek" );
myData = NULL;
}
void Initialize ( unsigned int channelsCount, unsigned int sampleRate )
{
sf::SoundStream::Initialize( channelsCount, sampleRate );
}
protected:
virtual bool OnGetData( Chunk& aData )
{
if( myData != NULL )
{
delete[] myData;
myData = NULL;
}
VALUE chunk = rb_funcall( mySelf, myOnGetDataID, 0 );
if( chunk == Qnil )
{
return false;
}
else
{
VALIDATE_CLASS( chunk, rb_cArray, "chunk" );
const unsigned int rawSamplesCount = FIX2UINT( rb_funcall( chunk, rb_intern( "size" ), 0 ) );
myData = new sf::Int16[rawSamplesCount];
VALUE samples = rb_ary_entry( chunk, 0 );
for(unsigned long index = 0; index < rawSamplesCount; index++)
{
const sf::Int16 val = NUM2INT( rb_ary_entry( samples, index ) );
myData[index] = val;
}
aData.Samples = myData;
aData.NbSamples = rawSamplesCount;
return true;
}
}
virtual void OnSeek( float anOffset )
{
rb_funcall( mySelf, myOnSeekID, 1, rb_float_new( anOffset ) );
}
VALUE mySelf;
ID myOnGetDataID;
ID myOnSeekID;
sf::Int16 *myData;
};
static VALUE SoundStream_Free( rbSoundStream *anObject )
{
delete anObject;
}
/* call-seq:
* sound_stream.play()
*
* Start or resume playing the audio stream.
*
* This function starts the stream if it was stopped, resumes it if it was paused, and does nothing it is it already
* playing. This function uses its own thread so that it doesn't block the rest of the program while the stream is played.
*/
static VALUE SoundStream_Play( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
object->Play();
return Qnil;
}
/* call-seq:
* sound_stream.pause()
*
* Start or resume playing the audio stream.
*
* This function starts the stream if it was stopped, resumes it if it was paused, and does nothing it is it already
* playing. This function uses its own thread so that it doesn't block the rest of the program while the stream is played.
*/
static VALUE SoundStream_Pause( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
object->Pause();
return Qnil;
}
/* call-seq:
* sound_stream.stop()
*
* Stop playing the audio stream.
*
* This function stops the stream if it was playing or paused, and does nothing if it was already stopped. It also
* resets the playing position (unlike pause()).
*/
static VALUE SoundStream_Stop( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
object->Stop();
return Qnil;
}
/* call-seq:
* sound_stream.getChannelsCount() -> fixnum
*
* Return the number of channels of the stream.
*
* 1 channel means a mono sound, 2 means stereo, etc.
*/
static VALUE SoundStream_GetChannelsCount( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
return INT2FIX( object->GetChannelsCount() );
}
/* call-seq:
* sound_stream.getSampleRate() -> fixnum
*
* Get the stream sample rate of the stream.
*
* The sample rate is the number of audio samples played per second. The higher, the better the quality.
*/
static VALUE SoundStream_GetSampleRate( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
return INT2FIX( object->GetSampleRate() );
}
/* call-seq:
* sound_stream.getStatus() -> fixnum
*
* Get the current status of the stream (stopped, paused, playing).
*/
static VALUE SoundStream_GetStatus( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
return INT2FIX( static_cast< int >( object->GetStatus() ) );
}
/* call-seq:
* sound_stream.setPlayingOffset( offset )
*
* Change the current playing position of the stream.
*
* The playing position can be changed when the stream is either paused or playing.
*/
static VALUE SoundStream_SetPlayingOffset( VALUE self, VALUE anOffset )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
object->SetPlayingOffset( NUM2DBL( anOffset ) );
return Qnil;
}
/* call-seq:
* sound_stream.getPlayingOffset() -> float
*
* Get the current playing position of the stream.
*/
static VALUE SoundStream_GetPlayingOffset( VALUE self, VALUE anOffset )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
return rb_float_new( object->GetPlayingOffset() );
}
/* call-seq:
* sound_stream.setLoop( loop )
*
* Set whether or not the stream should loop after reaching the end.
*
* If set, the stream will restart from beginning after reaching the end and so on, until it is stopped or
* SetLoop(false) is called. The default looping state for streams is false.
*/
static VALUE SoundStream_SetLoop( VALUE self, VALUE aLoop )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
if( aLoop == Qtrue )
{
object->SetLoop( true );
}
else if( aLoop == Qfalse )
{
object->SetLoop( false );
}
else
{
VALIDATE_CLASS( aLoop, rb_cTrueClass, "loop" );
}
return Qnil;
}
/* call-seq:
* sound_stream.getLoop() -> true or false
*
* Tell whether or not the stream is in loop mode.
*/
static VALUE SoundStream_GetLoop( VALUE self )
{
sf::SoundStream *object = NULL;
Data_Get_Struct( self, sf::SoundStream, object );
if( object->GetLoop() == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* sound_stream.initialize()
*
* This is a direct binding to the sf::SoundStream::Initialize function.
*
* Define the audio stream parameters.
*
* This function must be called by derived classes as soon as they know the audio settings of the stream to play. Any
* attempt to manipulate the stream (play(), ...) before calling this function will fail. It can be called multiple
* times if the settings of the audio stream change, but only when the stream is stopped.
*/
static VALUE SoundStream_Initialize( VALUE self, VALUE channelsCount, VALUE sampleRate )
{
rbSoundStream *object = NULL;
Data_Get_Struct( self, rbSoundStream, object );
object->Initialize( FIX2UINT( channelsCount ), FIX2UINT( sampleRate ) );
return self;
}
static VALUE SoundStream_Alloc( VALUE aKlass )
{
rbSoundStream *object = new rbSoundStream();
VALUE rbData = Data_Wrap_Struct( aKlass, 0, SoundStream_Free, object );
object->Init( rbData );
return rbData;
}
void Init_SoundStream( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Abstract base class for streamed audio sources.
*
* Unlike audio buffers (see SFML::SoundBuffer), audio streams are never completely loaded in memory.
*
* Instead, the audio data is acquired continuously while the stream is playing. This behaviour allows to play a sound
* with no loading delay, and keeps the memory consumption very low.
*
* Sound sources that need to be streamed are usually big files (compressed audio musics that would eat hundreds of MB
* in memory) or files that would take a lot of time to be received (sounds played over the network).
*
* SFML::SoundStream is a base class that doesn't care about the stream source, which is left to the derived class.
* SFML provides a built-in specialization for big files (see SFML::Music). No network stream source is provided, but you
* can write your own by combining this class with the network module.
*
* A derived class has to override two virtual functions:
*
* - onGetData fills a new chunk of audio data to be played
* - onSeek changes the current playing position in the source
*
* It is important to note that each SoundStream is played in its own separate thread, so that the streaming loop
* doesn't block the rest of the program. In particular, the onGetData and onSeek virtual functions may sometimes be
* called from this separate thread. It is important to keep this in mind, because you may have to take care of
* synchronization issues if you share data between threads.
*
* Usage example:
*
* class CustomStream < SFML::SoundStream
* def open( location )
* # Open the source and get audio settings
* ...
* channelsCount = ...
* sampleRate = ...
*
* # Initialize the stream -- important!
* initialize( channelsCount, sampleRate )
* end
*
* def onGetData( data )
* # Fill the chunk with audio data from the stream source
* data.Samples = ...;
* data.NbSamples = ...;
*
* # Return true to continue playing
* return true;
* end
*
* def onSeek( timeOffset )
* # Change the current position in the stream source
* ...
* end
* end
*
* # Usage
* CustomStream stream;
* stream.open( "path/to/stream" )
* stream.play
*/
globalSoundStreamClass = rb_define_class_under( sfml, "SoundStream", globalSoundSourceClass );
rb_include_module( globalSoundStreamClass, globalNonCopyableModule );
// Class methods
//rb_define_singleton_method( globalSoundStreamClass, "new", SoundStream_New, -1 );
rb_define_alloc_func( globalSoundStreamClass, SoundStream_Alloc );
// Instance methods
rb_define_method( globalSoundStreamClass, "initialize", SoundStream_Initialize, 2 );
rb_define_method( globalSoundStreamClass, "play", SoundStream_Play, 0 );
rb_define_method( globalSoundStreamClass, "pause", SoundStream_Pause, 0 );
rb_define_method( globalSoundStreamClass, "stop", SoundStream_Stop, 0 );
rb_define_method( globalSoundStreamClass, "getChannelsCount", SoundStream_GetChannelsCount, 0 );
rb_define_method( globalSoundStreamClass, "getSampleRate", SoundStream_GetSampleRate, 0 );
rb_define_method( globalSoundStreamClass, "getStatus", SoundStream_GetStatus, 0 );
rb_define_method( globalSoundStreamClass, "setPlayingOffset", SoundStream_SetPlayingOffset, 1 );
rb_define_method( globalSoundStreamClass, "getPlayingOffset", SoundStream_GetPlayingOffset, 0 );
rb_define_method( globalSoundStreamClass, "setLoop", SoundStream_SetLoop, 1 );
rb_define_method( globalSoundStreamClass, "getLoop", SoundStream_GetLoop, 0 );
// Instance Aliases
rb_define_alias( globalSoundStreamClass, "get_channels_count", "getChannelsCount" );
rb_define_alias( globalSoundStreamClass, "channelsCount", "getChannelsCount" );
rb_define_alias( globalSoundStreamClass, "channels_count", "getChannelsCount" );
rb_define_alias( globalSoundStreamClass, "get_sample_rate", "getSampleRate" );
rb_define_alias( globalSoundStreamClass, "sampleRate", "getSampleRate" );
rb_define_alias( globalSoundStreamClass, "sample_rate", "getSampleRate" );
rb_define_alias( globalSoundStreamClass, "status", "getStatus" );
rb_define_alias( globalSoundStreamClass, "get_playing_offset", "getPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "playingOffset", "getPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "playing_offset", "getPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "set_playing_offset", "setPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "playingOffset=", "setPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "playing_offset=", "setPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "loop", "getPlayingOffset" );
rb_define_alias( globalSoundStreamClass, "loop=", "setPlayingOffset" );
}

View File

@ -1,31 +0,0 @@
/* 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_SOUND_STREAM_HEADER_
#define SFML_RUBYEXT_SOUND_STREAM_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_SoundStream( void );
#endif // SFML_RUBYEXT_SOUND_STREAM_HEADER_

View File

@ -1,51 +0,0 @@
/* 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 "main.hpp"
#include "global.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "NonCopyable.hpp"
static bool CheckDependencies( void )
{
if( rb_cvar_defined( globalSFMLNamespace, rb_intern( "SystemLoaded" ) ) == Qtrue )
{
return true;
}
return false;
}
void Init_audio( void )
{
/* SFML namespace which contains the classes of this module. */
globalSFMLNamespace = rb_define_module( "SFML" );
if( CheckDependencies() == false )
{
rb_raise( rb_eRuntimeError, "This module depends on sfml-window" );
}
/*globalVector2Class = RetrieveSFMLClass( "Vector2" );
globalVector3Class = RetrieveSFMLClass( "Vector3" );
globalNonCopyableModule = RetrieveSFMLClass( "NonCopyable" );*/
rb_define_const(globalSFMLNamespace, "AudioLoaded", Qtrue);
}

View File

@ -1,32 +0,0 @@
/* 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_AUDIO_MAIN_HEADER_
#define SFML_RUBYEXT_AUDIO_MAIN_HEADER_
#include "ruby.h"
#include "global.hpp"
// Ruby initiation function
extern "C" void Init_audio( void );
#endif // SFML_RUBYEXT_AUDIO_MAIN_HEADER_

View File

@ -1,27 +0,0 @@
# 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.
require 'mkmf'
dir_config("audio")
have_library("sfml-audio-s")
find_header("main.hpp", "../sfml-system/system")
create_makefile("sfml/audio", "audio")

View File

@ -1,42 +0,0 @@
# 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.
require 'mkmf'
require 'rbconfig'
on_windows = Config::CONFIG['host_os'] =~ /mswin|mingw/
dir_config("graphics")
if (on_windows)
have_library("winmm")
have_library("jpeg")
have_library("gdi32")
have_library("opengl32")
have_library("glew")
have_library("sfml-system")
have_library("sfml-window")
have_library("sfml-graphics")
else
have_library("sfml-graphics")
end
find_header("main.hpp", "../sfml-system/system")
create_makefile("sfml/graphics", "graphics")

View File

@ -1,307 +0,0 @@
/* 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 "Color.hpp"
#include "main.hpp"
#include <SFML/Graphics/Color.hpp>
VALUE globalColorClass;
/* Internal function
* Forces the argument someValue to be a Color. IF it can convert it then it will.
* So you can always safely asume that this function returns a Color object.
* If it fails then an exception will be thrown.
*/
VALUE Color_ForceType( VALUE someValue )
{
if( rb_obj_is_kind_of( someValue, rb_cArray ) == Qtrue )
{
VALUE arg1 = rb_ary_entry( someValue, 0 );
VALUE arg2 = rb_ary_entry( someValue, 1 );
VALUE arg3 = rb_ary_entry( someValue, 2 );
if( FIX2INT( rb_funcall( someValue, rb_intern( "size" ), 0 ) ) == 4 )
{
VALUE arg4 = rb_ary_entry( someValue, 3 );
return rb_funcall( globalColorClass, rb_intern( "new" ), 4, arg1, arg2, arg3, arg4 );
}
return rb_funcall( globalColorClass, rb_intern( "new" ), 3, arg1, arg2, arg3 );
}
else if( rb_obj_is_kind_of( someValue, globalColorClass ) == Qtrue )
{
return someValue;
}
else
{
rb_raise( rb_eRuntimeError, "expected Array or Color" );
}
}
VALUE Color_GetR( VALUE self )
{
static ID id = rb_intern( "r" );
return rb_funcall( self, id, 0 );
}
VALUE Color_GetG( VALUE self )
{
static ID id = rb_intern( "g" );
return rb_funcall( self, id, 0 );
}
VALUE Color_GetB( VALUE self )
{
static ID id = rb_intern( "b" );
return rb_funcall( self, id, 0 );
}
VALUE Color_GetA( VALUE self )
{
static ID id = rb_intern( "a" );
return rb_funcall( self, id, 0 );
}
VALUE Color_SetR( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "r=" );
return rb_funcall( self, id, 1, aVal );
}
VALUE Color_SetG( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "g=" );
return rb_funcall( self, id, 1, aVal );
}
VALUE Color_SetB( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "b=" );
return rb_funcall( self, id, 1, aVal );
}
VALUE Color_SetA( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "a=" );
return rb_funcall( self, id, 1, aVal );
}
/* Internal function
* Will copy the x and y from aSource to self.
*/
static void Color_internal_CopyFrom( VALUE self, VALUE aSource )
{
VALUE source = Color_ForceType( aSource );
VALUE r = Color_GetR( source );
VALUE g = Color_GetG( source );
VALUE b = Color_GetB( source );
VALUE a = Color_GetA( source );
Color_SetR( self, r );
Color_SetG( self, g );
Color_SetB( self, b );
Color_SetA( self, a );
}
/* call-seq:
* color1 + color2 -> color
*
* This operator returns the component-wise sum of two colors. Components that exceed 255 are clamped to 255.
*/
static VALUE Color_Add( VALUE self, VALUE aRightOperand )
{
VALUE right = Color_ForceType( aRightOperand );
// Get values
unsigned int leftR = FIX2INT( Color_GetR( self ) );
unsigned int leftG = FIX2INT( Color_GetG( self ) );
unsigned int leftB = FIX2INT( Color_GetB( self ) );
unsigned int leftA = FIX2INT( Color_GetA( self ) );
unsigned int rightR = FIX2INT( Color_GetR( right ) );
unsigned int rightG = FIX2INT( Color_GetG( right ) );
unsigned int rightB = FIX2INT( Color_GetB( right ) );
unsigned int rightA = FIX2INT( Color_GetA( right ) );
// Do calculation
unsigned int newR = MIN( leftR + rightR, 255 );
unsigned int newG = MIN( leftG + rightG, 255 );
unsigned int newB = MIN( leftB + rightB, 255 );
unsigned int newA = MIN( leftA + rightA, 255 );
return rb_funcall( globalColorClass, rb_intern( "new" ), 4, newR, newG, newB, newA );
}
/* call-seq:
* color1 * color2 -> color
*
* This operator returns the component-wise multiplication (also called "modulation") of two colors. Components are
* then divided by 255 so that the result is still in the range [0, 255].
*/
static VALUE Color_Multiply( VALUE self, VALUE aRightOperand )
{
VALUE right = Color_ForceType( aRightOperand );
// Get values
unsigned int leftR = FIX2INT( Color_GetR( self ) );
unsigned int leftG = FIX2INT( Color_GetG( self ) );
unsigned int leftB = FIX2INT( Color_GetB( self ) );
unsigned int leftA = FIX2INT( Color_GetA( self ) );
unsigned int rightR = FIX2INT( Color_GetR( right ) );
unsigned int rightG = FIX2INT( Color_GetG( right ) );
unsigned int rightB = FIX2INT( Color_GetB( right ) );
unsigned int rightA = FIX2INT( Color_GetA( right ) );
// Do calculation
unsigned int newR = leftR * rightR / 255;
unsigned int newG = leftG * rightG / 255;
unsigned int newB = leftB * rightB / 255;
unsigned int newA = leftA * rightA / 255;
return rb_funcall( globalColorClass, rb_intern( "new" ), 4, newR, newG, newB, newA );
}
/* call-seq:
* color1 == color2 -> true or false
*
* This operator compares two colors and check if they are equal.
*/
static VALUE Color_Equal( VALUE self, VALUE anArgument )
{
VALUE right = Color_ForceType( anArgument );
// Get values
unsigned int leftR = FIX2INT( Color_GetR( self ) );
unsigned int leftG = FIX2INT( Color_GetG( self ) );
unsigned int leftB = FIX2INT( Color_GetB( self ) );
unsigned int leftA = FIX2INT( Color_GetA( self ) );
unsigned int rightR = FIX2INT( Color_GetR( right ) );
unsigned int rightG = FIX2INT( Color_GetG( right ) );
unsigned int rightB = FIX2INT( Color_GetB( right ) );
unsigned int rightA = FIX2INT( Color_GetA( right ) );
// Do calculation
if( leftR == rightR && leftG == rightG && leftB == rightB && leftA == rightA )
{
return Qtrue;
}
return Qfalse;
}
/* call-seq:
* Color.new() -> color
* Color.new([r,g,b,a=255]) -> color
* Color.new(vector) -> color
* Color.new(r,g,b,a=255) -> color
*
* Create a new color instance.
*/
static VALUE Color_Initialize( int argc, VALUE * args, VALUE self )
{
rb_iv_set( self, "@r", INT2NUM( 0 ) );
rb_iv_set( self, "@g", INT2NUM( 0 ) );
rb_iv_set( self, "@b", INT2NUM( 0 ) );
rb_iv_set( self, "@a", INT2NUM( 255 ) );
switch( argc )
{
case 0:
// Nothing needs to be done
break;
case 1:
Color_internal_CopyFrom( self, args[0] );
break;
case 4:
VALIDATE_CLASS( args[3], rb_cFixnum, "alpha" );
rb_iv_set( self, "@a", args[3]);
case 3:
VALIDATE_CLASS( args[0], rb_cFixnum, "red" );
VALIDATE_CLASS( args[1], rb_cFixnum, "green" );
VALIDATE_CLASS( args[2], rb_cFixnum, "blue" );
rb_iv_set( self, "@r", args[0]);
rb_iv_set( self, "@g", args[1]);
rb_iv_set( self, "@b", args[2]);
break;
default:
rb_raise( rb_eArgError, "Expected 0, 3 or 4 arguments but was given %d", argc );
}
return self;
}
void Init_Color( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Utility class for manpulating RGBA colors.
*
* SFML::Color is a simple color class composed of 4 components:
*
* - Red
* - Green
* - Blue
* - Alpha (opacity)
*
* Each component is a public member, an unsigned integer in the range [0, 255]. Thus, colors can be constructed and manipulated very easily:
*
* c1 = SFML::Color.new(255, 0, 0) # red
* c1.red = 0 # make it black
* c1.blue = 128 # make it dark blue
*
* The fourth component of colors, named "alpha", represents the opacity of the color. A color with an alpha value of
* 255 will be fully opaque, while an alpha value of 0 will make a color fully transparent, whatever the value of the
* other components.
*
* The most common colors are already defined as class constants:
*
* black = SFML::Color::Black
* white = SFML::Color::White
* red = SFML::Color::Red
* green = SFML::Color::Green
* blue = SFML::Color::Blue
* yellow = SFML::Color::Yellow
* magenta = SFML::Color::Magenta
* cyan = SFML::Color::Cyan
*
* Colors can also be added and modulated (multiplied) using the overloaded operators + and *.
*/
globalColorClass = rb_define_class_under( sfml, "Color", rb_cObject );
// Instance methods
rb_define_method( globalColorClass, "initialize", Color_Initialize, -1 );
rb_define_method( globalColorClass, "+", Color_Add, 1 );
rb_define_method( globalColorClass, "*", Color_Multiply, 1 );
rb_define_method( globalColorClass, "==", Color_Equal, 1 );
// Attribute accessors
rb_define_attr( globalColorClass, "r", 1, 1 );
rb_define_attr( globalColorClass, "g", 1, 1 );
rb_define_attr( globalColorClass, "b", 1, 1 );
rb_define_attr( globalColorClass, "a", 1, 1 );
// Class constants
rb_define_const( globalColorClass, "Black", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 0 ), INT2FIX( 0 ), INT2FIX( 0 ) ) );
rb_define_const( globalColorClass, "White", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 255 ), INT2FIX( 255 ), INT2FIX( 255 ) ) );
rb_define_const( globalColorClass, "Red", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 255 ), INT2FIX( 0 ), INT2FIX( 0 ) ) );
rb_define_const( globalColorClass, "Green", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 0 ), INT2FIX( 255 ), INT2FIX( 0 ) ) );
rb_define_const( globalColorClass, "Blue", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 0 ), INT2FIX( 0 ), INT2FIX( 255 ) ) );
rb_define_const( globalColorClass, "Yellow", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 255 ), INT2FIX( 255 ), INT2FIX( 0 ) ) );
rb_define_const( globalColorClass, "Magneta", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 255 ), INT2FIX( 0 ), INT2FIX( 255 ) ) );
rb_define_const( globalColorClass, "Cyan", rb_funcall( globalColorClass, rb_intern( "new" ), 3, INT2FIX( 0 ), INT2FIX( 255 ), INT2FIX( 255 ) ) );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Black" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "White" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Red" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Green" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Blue" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Yellow" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Magneta" ) ), rb_intern( "freeze" ), 0 );
rb_funcall( rb_cvar_get( globalColorClass, rb_intern( "Cyan" ) ), rb_intern( "freeze" ), 0 );
}

View File

@ -1,43 +0,0 @@
/* 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_COLOR_HEADER_
#define SFML_RUBYEXT_COLOR_HEADER_
#include "ruby.h"
VALUE Color_ForceType( VALUE someValue );
VALUE Color_GetR( VALUE self );
VALUE Color_GetG( VALUE self );
VALUE Color_GetB( VALUE self );
VALUE Color_GetA( VALUE self );
VALUE Color_SetR( VALUE self, VALUE aVal );
VALUE Color_SetG( VALUE self, VALUE aVal );
VALUE Color_SetB( VALUE self, VALUE aVal );
VALUE Color_SetA( VALUE self, VALUE aVal );
// Ruby initiation function
void Init_Color( void );
#endif // SFML_RUBYEXT_COLOR_HEADER_

View File

@ -1,681 +0,0 @@
/* 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 "Drawable.hpp"
#include "Vector2.hpp"
#include "Color.hpp"
#include "main.hpp"
#include <SFML/Graphics/Drawable.hpp>
VALUE globalDrawableModule;
/* External classes */
extern VALUE globalVector2Class;
extern VALUE globalColorClass;
extern VALUE globalRenderTargetInstanceClass;
extern VALUE globalRendererClass;
class rbDrawable : public sf::Drawable
{
public:
void Init( VALUE aSelf )
{
myRubySelf = aSelf;
myRenderID = rb_intern( "render" );
}
protected:
VALUE myRubySelf;
ID myRenderID;
virtual void Render( sf::RenderTarget& aTarget, sf::Renderer& aRenderer ) const
{
VALUE targetWrap = Data_Wrap_Struct( globalRenderTargetInstanceClass, 0, 0, &aTarget );
VALUE rendererWrap = Data_Wrap_Struct( globalRendererClass, 0, 0, &aRenderer );
rb_funcall( myRubySelf, myRenderID, 2, targetWrap, rendererWrap );
}
};
static void Drawable_Free( rbDrawable *object )
{
delete object;
}
/* call-seq:
* drawable.setPosition( x, y )
* drawable.setPosition( vector2 )
*
* Set the position of the object.
*
* This function completely overwrites the previous position. See Move to apply an offset based on the previous
* position instead. The default position of a drawable object is (0, 0).
*/
static VALUE Drawable_SetPosition( int argc, VALUE *args, VALUE self )
{
rbDrawable *object = NULL;
VALUE arg0 = Qnil;
float positionX = 0.0f;
float positionY = 0.0f;
switch( argc )
{
case 1:
arg0 = Vector2_ForceType( args[0] );
positionX = NUM2DBL( Vector2_GetX( arg0 ) );
positionY = NUM2DBL( Vector2_GetY( arg0 ) );
break;
case 2:
positionX = NUM2DBL( args[0] );
positionY = NUM2DBL( args[1] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
Data_Get_Struct( self, rbDrawable, object );
object->SetPosition( positionX, positionY );
return Qnil;
}
/* call-seq:
* drawable.setX( x )
*
* Set the X position of the object.
*/
static VALUE Drawable_SetX( VALUE self, VALUE aX )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetX( NUM2DBL( aX ) );
return Qnil;
}
/* call-seq:
* drawable.setY( y )
*
* Set the Y position of the object.
*/
static VALUE Drawable_SetY( VALUE self, VALUE aY )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetY( NUM2DBL( aY ) );
return Qnil;
}
/* call-seq:
* drawable.setScale( x, y )
* drawable.setScale( vector2 )
*
* Set the scale factors of the object.
*
* scale.x and scale.y must be strictly positive, otherwise they are ignored. This function completely overwrites
* the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable
* object is (1, 1).
*/
static VALUE Drawable_SetScale( int argc, VALUE *args, VALUE self )
{
rbDrawable *object = NULL;
VALUE arg0 = Qnil;
float scaleX = 0.0f;
float scaleY = 0.0f;
switch( argc )
{
case 1:
arg0 = Vector2_ForceType( args[0] );
scaleX = NUM2DBL( Vector2_GetX( arg0 ) );
scaleY = NUM2DBL( Vector2_GetY( arg0 ) );
break;
case 2:
scaleX = NUM2DBL( args[0] );
scaleY = NUM2DBL( args[1] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
Data_Get_Struct( self, rbDrawable, object );
object->SetScale( scaleX, scaleY );
return Qnil;
}
/* call-seq:
* drawable.setScaleX( factor )
*
* Set the X scale factor of the object.
*
* factor must be strictly positive, otherwise it is ignored.
*/
static VALUE Drawable_SetScaleX( VALUE self, VALUE aX )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetScaleX( NUM2DBL( aX ) );
return Qnil;
}
/* call-seq:
* drawable.setScaleY( factor )
*
* Set the Y scale factor of the object.
*
* factor must be strictly positive, otherwise it is ignored.
*/
static VALUE Drawable_SetScaleY( VALUE self, VALUE aY )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetScaleY( NUM2DBL( aY ) );
return Qnil;
}
/* call-seq:
* drawable.setOrigin( x, y )
* drawable.setOrigin( vector2 )
*
* Set the local origin of the object.
*
* The origin of an object defines the center point for all transformations (position, scale, rotation). The
* coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations
* (position, scale, rotation). The default origin of a drawable object is (0, 0).
*/
static VALUE Drawable_SetOrigin( int argc, VALUE *args, VALUE self )
{
rbDrawable *object = NULL;
VALUE arg0 = Qnil;
float originX = 0.0f;
float originY = 0.0f;
switch( argc )
{
case 1:
arg0 = Vector2_ForceType( args[0] );
originX = NUM2DBL( Vector2_GetX( arg0 ) );
originY = NUM2DBL( Vector2_GetY( arg0 ) );
break;
case 2:
originX = NUM2DBL( args[0] );
originY = NUM2DBL( args[1] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
Data_Get_Struct( self, rbDrawable, object );
object->SetOrigin( originX, originY );
return Qnil;
}
/* call-seq:
* drawable.setRotation( angle )
*
* Set the orientation of the object.
*
* This function completely overwrites the previous rotation. See Rotate to add an angle based on the previous
* rotation instead. The default rotation of a drawable object is 0.
*/
static VALUE Drawable_SetRotation( VALUE self, VALUE aRotation )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetRotation( NUM2DBL( aRotation ) );
return Qnil;
}
/* call-seq:
* drawable.setColor( color )
*
* Set the global color of the object.
*
* This global color affects the entire object, and modulates (multiplies) its original pixels.
* The default color is white.
*/
static VALUE Drawable_SetColor( VALUE self, VALUE aColor )
{
VALUE color = Color_ForceType( aColor );
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetColor( sf::Color( Color_GetR( color ), Color_GetG( color ), Color_GetB( color ), Color_GetA( color ) ) );
return Qnil;
}
/* call-seq:
* drawable.setBlendMode( mode )
*
* Set the blending mode of the object.
*
* This property defines how the pixels of an object are blended with the pixels of the render target to which it is
* drawn. To know more about the blending modes available, see the SFML::Blend module. The default blend mode is
* SFML::Blend::Alpha.
*/
static VALUE Drawable_SetBlendMode( VALUE self, VALUE aMode )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->SetBlendMode( static_cast<sf::Blend::Mode>( FIX2INT( aMode ) ) );
return Qnil;
}
/* call-seq:
* drawable.getPosition() -> vector2
*
* Get the position of the object.
*/
static VALUE Drawable_GetPosition( VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
const sf::Vector2f &vector = object->GetPosition();
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( vector.x ), rb_float_new( vector.y ) );
}
/* call-seq:
* drawable.getScale() -> vector2
*
* Get the current scale of the object.
*/
static VALUE Drawable_GetScale( VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
const sf::Vector2f &vector = object->GetScale();
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( vector.x ), rb_float_new( vector.y ) );
}
/* call-seq:
* drawable.getOrigin() -> vector2
*
* Get the local origin of the object.
*/
static VALUE Drawable_GetOrigin( VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
const sf::Vector2f &vector = object->GetOrigin();
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( vector.x ), rb_float_new( vector.y ) );
}
/* call-seq:
* drawable.getRotation() -> float
*
* Get the orientation of the object.
*
* The rotation is always in the range [0, 360].
*/
static VALUE Drawable_GetRotation( VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
return rb_float_new( object->GetRotation() );
}
/* call-seq:
* drawable.getColor() -> color
*
* Get the color of the object.
*/
static VALUE Drawable_GetColor( VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
const sf::Color &color = object->GetColor();
return rb_funcall( globalColorClass, rb_intern( "new" ), 4, INT2FIX( color.r ), INT2FIX( color.g ),
INT2FIX( color.b ), INT2FIX( color.a ) );
}
/* call-seq:
* drawable.getBlendMode() -> mode
*
* Get the blend mode of the object.
*/
static VALUE Drawable_GetBlendMode( VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
return INT2FIX( object->GetBlendMode() );
}
/* call-seq:
* drawable.move( x, y )
* drawable.move( vector2 )
*
* Move the object by a given offset.
*
* This function adds to the current position of the object, unlike setPosition which overwrites it.
*/
static VALUE Drawable_Move( int argc, VALUE *args, VALUE self )
{
rbDrawable *object = NULL;
VALUE arg0 = Qnil;
float moveX = 0.0f;
float moveY = 0.0f;
switch( argc )
{
case 1:
arg0 = Vector2_ForceType( args[0] );
moveX = NUM2DBL( Vector2_GetX( arg0 ) );
moveY = NUM2DBL( Vector2_GetY( arg0 ) );
break;
case 2:
moveX = NUM2DBL( args[0] );
moveY = NUM2DBL( args[1] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
Data_Get_Struct( self, rbDrawable, object );
object->Move( moveX, moveY );
return Qnil;
}
/* call-seq:
* drawable.scale( x, y )
* drawable.scale( vector2 )
*
* Scale the object.
*
* This function multiplies the current scale of the object, unlike setScale which overwrites it.
*/
static VALUE Drawable_Scale( int argc, VALUE *args, VALUE self )
{
rbDrawable *object = NULL;
VALUE arg0 = Qnil;
float scaleX = 0.0f;
float scaleY = 0.0f;
switch( argc )
{
case 1:
arg0 = Vector2_ForceType( args[0] );
scaleX = NUM2DBL( Vector2_GetX( arg0 ) );
scaleY = NUM2DBL( Vector2_GetY( arg0 ) );
break;
case 2:
scaleX = NUM2DBL( args[0] );
scaleY = NUM2DBL( args[1] );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
Data_Get_Struct( self, rbDrawable, object );
object->Scale( scaleX, scaleY );
return Qnil;
}
/* call-seq:
* drawable.rotate( angle )
*
* Rotate the object.
*
* This function ads to the current rotation of the object, unlike setRotation which overwrites it
*/
static VALUE Drawable_Rotate( VALUE self, VALUE aRotation )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
object->Rotate( NUM2DBL( aRotation ) );
return Qnil;
}
/* call-seq:
* drawable.transformToLocal( vector2 ) -> vector2
*
* Transform a point in object local coordinates.
*
* This function takes a point in global coordinates, and transforms it in coordinates local to the object. In other
* words, it applies the inverse of all the transformations applied to the object (origin, translation, rotation
* and scale).
*/
static VALUE Drawable_TransformToLocal( VALUE self, VALUE aPoint )
{
VALUE point = Vector2_ForceType( aPoint );
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
sf::Vector2f newPoint = object->TransformToLocal( sf::Vector2f( NUM2DBL( Vector2_GetX( point ) ),
NUM2DBL( Vector2_GetY( point ) ) )
);
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( newPoint.x ), rb_float_new( newPoint.y ) );
}
/* call-seq:
* drawable.transformToGlobal( vector2 ) -> vector2
*
* Transform a local point in global coordinates.
*
* This function takes a point in local coordinates, and transforms it in global coordinates. In other words, it
* applies the same transformations that are applied to the object (origin, translation, rotation and scale).
*/
static VALUE Drawable_TransformToGlobal( VALUE self, VALUE aPoint )
{
VALUE point = Vector2_ForceType(aPoint);
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
sf::Vector2f newPoint = object->TransformToGlobal( sf::Vector2f( NUM2DBL( Vector2_GetX( point ) ),
NUM2DBL( Vector2_GetY( point ) ) )
);
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( newPoint.x ), rb_float_new( newPoint.y ) );
}
/* call-seq:
* Rect.new() -> rect
* Rect.new( [left, top, width, height] ) -> rect
* Rect.new( rect ) -> rect
* Rect.new( left, top, width, height ) -> rect
* Rect.new( position, size ) -> rect
*
* Create a new rect instance.
*/
static VALUE Drawable_Initialize( int argc, VALUE *args, VALUE self )
{
rbDrawable *object = NULL;
Data_Get_Struct( self, rbDrawable, object );
VALUE aPosition = Qnil;
VALUE aScale = Qnil;
VALUE aColor = Qnil;
int colorRed = 0;
int colorGreen = 0;
int colorBlue = 0;
int colorAlpha = 0;
switch( argc )
{
case 0:
// Nothing to do
break;
case 4:
aColor = Color_ForceType( args[3] );
colorRed = FIX2INT( rb_funcall( aColor, rb_intern( "r" ), 0 ) );
colorGreen = FIX2INT( rb_funcall( aColor, rb_intern( "g" ), 0 ) );
colorBlue = FIX2INT( rb_funcall( aColor, rb_intern( "b" ), 0 ) );
colorAlpha = FIX2INT( rb_funcall( aColor, rb_intern( "a" ), 0 ) );
object->SetColor( sf::Color( colorRed, colorGreen, colorBlue, colorAlpha ) );
case 3:
object->SetRotation( NUM2DBL( args[2] ) );
case 2:
aScale = Vector2_ForceType( args[1] );
object->SetScaleX( Vector2_GetX( aScale ) );
object->SetScaleY( Vector2_GetY( aScale ) );
case 1:
aPosition = Vector2_ForceType( args[0] );
object->SetX( Vector2_GetX( aPosition ) );
object->SetY( Vector2_GetY( aPosition ) );
break;
default:
rb_raise( rb_eArgError, "Expected 0..4 arguments but was given %d", argc );
}
return rb_call_super( argc, args );
}
static VALUE Drawable_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Drawable *selfDrawable = NULL;
Data_Get_Struct( self, sf::Drawable, selfDrawable );
sf::Drawable *sourceDrawable = NULL;
Data_Get_Struct( aSource, sf::Drawable, sourceDrawable );
*selfDrawable = *sourceDrawable;
return self;
}
static VALUE Drawable_Allocate( VALUE aKlass )
{
rbDrawable *object = new rbDrawable();
VALUE rbData = Data_Wrap_Struct( aKlass, 0, Drawable_Free, object );
object->Init( rbData );
return rbData;
}
static VALUE Drawable_Included( VALUE aModule, VALUE aBase )
{
//rb_define_singleton_method( aBase, "allocate", Drawable_Allocate, 0 );
rb_define_alloc_func( aBase, Drawable_Allocate );
return Qnil;
}
void Init_Drawable( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Abstract base class for objects that can be drawn to a render target.
*
* SFML::Drawable defines the attributes and operations that are common to all the drawable classes:
*
* - transformations (position, rotation, scale, local origin)
* - global overlay color
* - blending mode with background pixels
* - the ability to be drawn on either RenderWindow or RenderImage
*
* Please note that all these attributes are hardware accelerated, therefore they are extremely cheap to use
* (unlike older libraries that perform slow transformations on the CPU, such as rotation or scale).
*
* Usage example:
*
* # Here we'll use a SFML::Sprite to demonstrate the features of SFML::Drawable
* drawable = SFML::Sprite.new( ...whatever... )
*
* drawable.SetOrigin(10, 20) # set its origin to the local point (10, 20)
* drawable.SetPosition(100, 100) # set its position to (100, 100)
* drawable.SetRotation(45) # set its orientation to 45 degrees
* drawable.SetColor(sf::Color::Red) # set its global color to red
* drawable.SetBlendingMode(sf::Blend::Add) # set an additive blend mode
*
* window.draw( drawable ) # finally draw it (window is a SFML::RenderWindow)
*
* Deriving your own class from SFML::Drawable is possible, however you have to use the SFML::Renderer class instead of
* direct OpenGL calls, which is more limited. To create a derived drawable class, all you have to do is to override the virtual Render function.
*
* One of the main benefits of creating your own drawable class is that you can build hierarchies of drawable objects.
* Indeed, when you draw a drawable inside the Render function of another drawable, the former inherits the
* transformations and color of the latter and combines them with its own attributes. This way, you can apply global
* transformations/color to a set of drawables as if it was a single entity.
*
* Example:
*
* class MyDrawable
* include SFML::Drawable
* # ...
*
* def initialize
* @myTexture = SFML::Image.new( ...whatever... )
* @mySubSprite = SFML::Sprite.new( ...whatever... )
* end
*
* def render( target, renderer )
* # Low-level geometry rendering
* renderer.setTexture( @myTexture )
* renderer.begin( SFML::Renderer::QuadList )
* renderer.addVertex(...)
* renderer.addVertex(...)
* renderer.addVertex(...)
* renderer.addVertex(...)
* renderer.end()
*
* # High-level drawable rendering
* target.draw(@mySubSprite);
* end
* end
*
*/
globalDrawableModule = rb_define_module_under( sfml, "Drawable" );
// Class methods
rb_define_module_function( globalDrawableModule, "included", Drawable_Included, 1 );
// Instance methods
rb_define_method( globalDrawableModule, "initialize", Drawable_Initialize, -1 );
rb_define_method( globalDrawableModule, "initialize_copy", Drawable_Initialize, 1 );
rb_define_method( globalDrawableModule, "setPosition", Drawable_SetPosition, -1 );
rb_define_method( globalDrawableModule, "setX", Drawable_SetX, 1 );
rb_define_method( globalDrawableModule, "setY", Drawable_SetY, 1 );
rb_define_method( globalDrawableModule, "setScale", Drawable_SetScale, -1 );
rb_define_method( globalDrawableModule, "setScaleX", Drawable_SetScaleX, 1 );
rb_define_method( globalDrawableModule, "setScaleY", Drawable_SetScaleY, 1 );
rb_define_method( globalDrawableModule, "setOrigin", Drawable_SetOrigin, -1 );
rb_define_method( globalDrawableModule, "setRotation", Drawable_SetRotation, 1 );
rb_define_method( globalDrawableModule, "setColor", Drawable_SetColor, 1 );
rb_define_method( globalDrawableModule, "setBlendMode", Drawable_SetBlendMode, 1 );
rb_define_method( globalDrawableModule, "getPosition", Drawable_GetPosition, 0 );
rb_define_method( globalDrawableModule, "getScale", Drawable_GetScale, 0 );
rb_define_method( globalDrawableModule, "getOrigin", Drawable_GetOrigin, 0 );
rb_define_method( globalDrawableModule, "getRotation", Drawable_GetRotation, 0 );
rb_define_method( globalDrawableModule, "getColor", Drawable_GetColor, 0 );
rb_define_method( globalDrawableModule, "getBlendMode", Drawable_GetBlendMode, 0 );
rb_define_method( globalDrawableModule, "move", Drawable_Move, -1 );
rb_define_method( globalDrawableModule, "scale", Drawable_Scale, -1 );
rb_define_method( globalDrawableModule, "rotate", Drawable_Rotate, 1 );
rb_define_method( globalDrawableModule, "transformToLocal", Drawable_TransformToLocal, 1 );
rb_define_method( globalDrawableModule, "transformToGlobal", Drawable_TransformToGlobal, 1 );
// Aliases
rb_define_alias( globalDrawableModule, "position=", "setPosition" );
rb_define_alias( globalDrawableModule, "position", "getPosition" );
rb_define_alias( globalDrawableModule, "x=", "setX" );
rb_define_alias( globalDrawableModule, "y=", "setY" );
rb_define_alias( globalDrawableModule, "scale=", "setScale" );
rb_define_alias( globalDrawableModule, "scale", "getScale" );
rb_define_alias( globalDrawableModule, "scaleX=", "setScaleX" );
rb_define_alias( globalDrawableModule, "scale_x=", "setScaleX" );
rb_define_alias( globalDrawableModule, "scaleY=", "setScaleY" );
rb_define_alias( globalDrawableModule, "scale_y=", "setScaleY" );
rb_define_alias( globalDrawableModule, "origin=", "setOrigin" );
rb_define_alias( globalDrawableModule, "origin", "getOrigin" );
rb_define_alias( globalDrawableModule, "rotation=", "setRotation" );
rb_define_alias( globalDrawableModule, "rotation", "getRotation" );
rb_define_alias( globalDrawableModule, "color=", "setColor" );
rb_define_alias( globalDrawableModule, "color", "getColor" );
rb_define_alias( globalDrawableModule, "blendMode=", "setBlendMode" );
rb_define_alias( globalDrawableModule, "blend_mode=", "setBlendMode" );
rb_define_alias( globalDrawableModule, "blendMode", "getBlendMode" );
rb_define_alias( globalDrawableModule, "blend_mode", "getBlendMode" );
rb_define_alias( globalDrawableModule, "transform_to_local", "transformToLocal" );
rb_define_alias( globalDrawableModule, "transform_to_global", "transformToGlobal" );
}

View File

@ -1,30 +0,0 @@
/* 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_DRAWABLE_HEADER_
#define SFML_RUBYEXT_DRAWABLE_HEADER_
#include "ruby.h"
void Init_Drawable( void );
#endif // SFML_RUBYEXT_DRAWABLE_HEADER_

View File

@ -1,265 +0,0 @@
/* 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 <SFML/Graphics/Font.hpp>
VALUE globalFontClass;
/* External classes */
extern VALUE globalGlyphClass;
extern VALUE globalRectClass;
extern VALUE globalImageClass;
static void Font_Free( sf::Font *anObject )
{
delete anObject;
}
/* call-seq:
* font.loadFromFile( filename ) -> true or false
*
* Load the font from a file.
*
* The supported font formats are: TrueType, Type 1, CFF, OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and
* Type 42. Note that this function know nothing about the standard fonts installed on the user's system, thus
* you can't load them directly.
*/
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;
}
}
/* call-seq:
* font.getGlyph( codePoint, characterSize, boldFlag ) -> glyph
*
* Retrieve a glyph of the font.
*/
static VALUE Font_GetGlyph( VALUE self, VALUE aCodePoint, VALUE aCharacterSize, VALUE aBoldFlag )
{
sf::Font *object = NULL;
Data_Get_Struct( self, sf::Font, object );
const sf::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;
}
/* call-seq:
* font.getKerning( first, size, characterSize ) -> fixnum
*
* Get the kerning offset of two glyphs.
*
* The kerning is an extra offset (negative) to apply between two glyphs when rendering them, to make the pair
* look more "natural". For example, the pair "AV" have a special kerning to make them closer than other characters.
* Most of the glyphs pairs have a kerning offset of zero, though.
*/
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 ) ) );
}
/* call-seq:
* font.getLineSpacing( characterSize ) -> fixnum
*
* Get the line spacing.
*
* Line spacing is the vertical offset to apply between two consecutive lines of text.
*/
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 ) ) );
}
/* call-seq:
* font.getImage( characterSize ) -> image
*
* Retrieve the image containing the loaded glyphs of a certain size.
*
* The contents of the returned image changes as more glyphs are requested, thus it is not very relevant.
* It is mainly used internally by SFML::Text.
*/
static VALUE Font_GetImage( VALUE self, VALUE aCharacterSize )
{
sf::Font *object = NULL;
Data_Get_Struct( self, sf::Font, object );
const sf::Image& image = object->GetImage( FIX2UINT( aCharacterSize ) );
VALUE rbImage = Data_Wrap_Struct( globalImageClass, 0, 0, const_cast<sf::Image *>( &image ) );
rb_iv_set( rbImage, "@__owner_ref", self );
return rbImage;
}
/* call-seq:
* Font.new() -> font
* Font.new( filename ) -> font
*
* Will create a new font instance.
*
* If a filename argument is specified then font#loadFromFile will be called on the created instance.
*/
static VALUE Font_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 0 )
{
rb_funcall2( self, rb_intern( "loadFromFile" ), argc, args );
}
return self;
}
static VALUE Font_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Font *object = NULL;
Data_Get_Struct( self, sf::Font, object );
sf::Font *source = NULL;
Data_Get_Struct( aSource, sf::Font, source );
*object = *source;
}
static VALUE Font_Alloc( VALUE aKlass )
{
sf::Font *object = new sf::Font();
return Data_Wrap_Struct( aKlass, 0, Font_Free, object );
}
/* call-seq:
* Font.getDefaultFont() -> font
*
* Return the default built-in font.
*
* This font is provided for convenience, it is used by SFML::Text instances by default. It is provided so that users
* don't have to provide and load a font file in order to display text on screen. The font used is Arial.
*/
static VALUE Font_GetDefaultFont( VALUE aKlass )
{
const sf::Font& font = sf::Font::GetDefaultFont();
VALUE rbFont = Data_Wrap_Struct( globalFontClass, 0, 0, const_cast<sf::Font *>( &font ) );
rb_obj_call_init( rbFont, 0, 0 );
return rbFont;
}
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 );
// Class methods
//rb_define_singleton_method( globalFontClass, "new", Font_New, -1 );
rb_define_alloc_func( globalFontClass, Font_Alloc );
rb_define_singleton_method( globalFontClass, "getDefaultFont", Font_GetDefaultFont, 0 );
// Instance methods
rb_define_method( globalFontClass, "initialize", Font_Initialize, -1 );
rb_define_method( globalFontClass, "initialize_copy", Font_InitializeCopy, 1 );
rb_define_method( globalFontClass, "loadFromFile", Font_LoadFromFile, 1 );
rb_define_method( globalFontClass, "getGlyph", Font_GetGlyph, 3 );
rb_define_method( globalFontClass, "getKerning", Font_GetKerning, 3 );
rb_define_method( globalFontClass, "getLineSpacing", Font_GetLineSpacing, 1 );
rb_define_method( globalFontClass, "getImage", Font_GetImage, 1 );
// Class Aliases
rb_define_alias( CLASS_OF( globalFontClass ), "get_default_font", "getDefaultFont" );
rb_define_alias( CLASS_OF( globalFontClass ), "defaultFont", "getDefaultFont" );
rb_define_alias( CLASS_OF( globalFontClass ), "default_font", "getDefaultFont" );
rb_define_alias( CLASS_OF( globalFontClass ), "DefaultFont", "getDefaultFont" );
// Instance Aliases
rb_define_alias( globalFontClass , "load_from_file", "loadFromFile" );
rb_define_alias( globalFontClass , "loadFile", "loadFromFile" );
rb_define_alias( globalFontClass , "load_file", "loadFromFile" );
rb_define_alias( globalFontClass , "get_glyph", "getGlyph" );
rb_define_alias( globalFontClass , "get_kerning", "getKerning" );
rb_define_alias( globalFontClass , "get_line_spacing", "getLineSpacing" );
rb_define_alias( globalFontClass , "get_image", "getImage" );
}

View File

@ -1,30 +0,0 @@
/* 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_

View File

@ -1,73 +0,0 @@
/* 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"
#include <SFML/Graphics/Glyph.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 );
// Instance methods
rb_define_method( globalGlyphClass, "initialize", Glyph_Initialize, 0 );
// 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=" );
}

View File

@ -1,30 +0,0 @@
/* 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_

View File

@ -1,688 +0,0 @@
/* 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 "Rect.hpp"
#include "main.hpp"
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Rect.hpp>
VALUE globalImageClass;
/* External classes */
extern VALUE globalColorClass;
extern VALUE globalRectClass;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void Image_Free( sf::Image *anObject )
{
delete anObject;
}
/* call-seq:
* image.loadFromFile( filename ) -> true or false
*
* Load the image from a file on disk.
*
* The supported image formats are bmp, png, tga, jpg, dds and psd. Some format options are not supported, like
* progressive jpeg. The maximum size for an image depends on the graphics driver and can be retrieve with the
* GetMaximumSize function.
*/
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;
}
}
/* call-seq:
* image.loadFromPixels( width, height, pixels ) -> true or false
*
* Load the image from an array of pixels.
*
* The pixels argument must point to an array of 32 bits RGBA pixels. In other words, the pixel array must have
* this memory layout:
*
* [r0 g0 b0 a0 r1 g1 b1 a1 r2...]
*/
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;
}
}
/* call-seq:
* image.saveToFile( filename ) -> true or false
*
* Save the image to a file on disk.
*
* The format of the image is automatically deduced from the extension. The supported image formats are bmp, png,
* tga, jpg, dds and psd. The destination file is overwritten if it already exists.
*/
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;
}
}
/* call-seq:
* image.create( width, height, color = SFML::Color::Black ) -> true or false
*
* Create the image and fill it with a unique color.
*/
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 = FIX2INT( Color_GetR( rubyColor ) );
color.g = FIX2INT( Color_GetG( rubyColor ) );
color.b = FIX2INT( Color_GetB( rubyColor ) );
color.a = FIX2INT( 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 );
}
/* call-seq:
* image.createMaskFromColor( color, alpha = 0 )
*
* Create a transparency mask from a specified colorkey.
*
* This function sets the alpha value of every pixel matching the given color to alpha (0 by default),
* so that they become transparent.
*/
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 = FIX2INT( Color_GetR( rubyColor ) );
color.g = FIX2INT( Color_GetG( rubyColor ) );
color.b = FIX2INT( Color_GetB( rubyColor ) );
color.a = FIX2INT( 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.copy( source, destX, destY, sourceRect = [0, 0, 0, 0], applyAlpha = false )
*
* Copy pixels from another image onto this one.
*
* This function does a slow pixel copy and should only be used at initialization time. It can be used to prepare
* a complex static image from several others, but if you need this kind of feature in real-time you'd better use
* SFML::RenderImage. If sourceRect is empty, the whole image is copied. If applyAlpha is set to true, the
* transparency of source pixels is applied. If it is false, the pixels are copied unchanged with their alpha value.
*/
static VALUE Image_Copy( int argc, VALUE *args, VALUE self )
{
sf::Image *source;
unsigned int destX = 0;
unsigned int destY = 0;
sf::IntRect sourceRect = sf::IntRect(0, 0, 0, 0);
VALUE rubySourceRect = Qnil;
bool applyAlpha = false;
switch( argc )
{
case 5:
if( args[4] == Qtrue )
{
applyAlpha = true;
}
else if( args[4] == Qfalse )
{
applyAlpha = false;
}
else
{
VALIDATE_CLASS( args[4], rb_cTrueClass, "applyAlpha" );
}
case 4:
rubySourceRect = Rect_ForceType( args[3] );
sourceRect.Left = FIX2INT( Rect_GetLeft( rubySourceRect ) );
sourceRect.Top = FIX2INT( Rect_GetTop( rubySourceRect ) );
sourceRect.Width = FIX2INT( Rect_GetWidth( rubySourceRect ) );
sourceRect.Height = FIX2INT( Rect_GetHeight( rubySourceRect ) );
case 3:
VALIDATE_CLASS( args[0], globalImageClass, "source" );
Data_Get_Struct( args[0], sf::Image, source );
destX = FIX2UINT( args[1] );
destX = FIX2UINT( args[2] );
break;
default:
rb_raise( rb_eArgError, "Expected 3..5 arguments but was given %d", argc );
}
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
object->Copy( *source, destX, destY, sourceRect, applyAlpha );
return Qnil;
}
/* call-seq:
* image.copyScreen( window, sourceRect = [0, 0, 0, 0] ) -> true or false
*
* Copy the contents of a window to the image.
*
* If sourceRect is empty, the whole window is copied. Warning: this is a slow operation, if you need to draw dynamic
* contents to an image then use SFML::RenderImage.
*/
static VALUE Image_CopyScreen( int argc, VALUE *args, VALUE self )
{
sf::RenderWindow *source;
sf::IntRect sourceRect = sf::IntRect(0, 0, 0, 0);
VALUE rubySourceRect = Qnil;
switch( argc )
{
case 2:
rubySourceRect = Rect_ForceType( args[3] );
sourceRect.Left = FIX2INT( Rect_GetLeft( rubySourceRect ) );
sourceRect.Top = FIX2INT( Rect_GetTop( rubySourceRect ) );
sourceRect.Width = FIX2INT( Rect_GetWidth( rubySourceRect ) );
sourceRect.Height = FIX2INT( Rect_GetHeight( rubySourceRect ) );
case 1:
VALIDATE_CLASS( args[0], globalImageClass, "source" );
Data_Get_Struct( args[0], sf::RenderWindow, source );
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
if( object->CopyScreen( *source, sourceRect ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* image.setPixel( x, y, color )
*
* Change the color of a pixel.
*
* This function doesn't check the validity of the pixel coordinates, using out-of-range values will
* result in an undefined behaviour.
*/
static VALUE Image_SetPixel( VALUE self, VALUE aX, VALUE aY, VALUE aColor )
{
VALUE rbColor = Color_ForceType( aColor );
sf::Color color;
color.r = FIX2INT( Color_GetR( rbColor ) );
color.g = FIX2INT( Color_GetG( rbColor ) );
color.b = FIX2INT( Color_GetB( rbColor ) );
color.a = FIX2INT( Color_GetA( rbColor ) );
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
object->SetPixel( FIX2INT( aX ), FIX2INT( aY ), color );
return Qnil;
}
/* call-seq:
* image.getPixel( x, y ) -> color
*
* Get the color of a pixel.
*
* This function doesn't check the validity of the pixel coordinates, using out-of-range values will
* result in an undefined behaviour.
*/
static VALUE Image_GetPixel( VALUE self, VALUE aX, VALUE aY )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
const sf::Color color = object->GetPixel( FIX2INT( aX ), FIX2INT( aY ) );
return rb_funcall( globalColorClass, rb_intern( "new" ), 4,
INT2FIX( color.r ), INT2FIX( color.g ),
INT2FIX( color.b ), INT2FIX( color.a ) );
}
/* call-seq:
* image.getPixelsPtr() -> array of pixels
*
* Get a read-only pointer to the array of pixels.
*
* The returned value points to an array of RGBA pixels made of 8 bits integers components.
* The size of the array is width * height * 4. Warning: the returned pointer may become invalid if
* you modify the image, so you should never store it for too long.
*/
static VALUE Image_GetPixelsPtr( VALUE self )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
const unsigned int rawWidth = object->GetWidth();
const unsigned int rawHeight = object->GetHeight();
const unsigned long dataSize = rawWidth * rawHeight * 4;
VALUE pixels = rb_ary_new2( dataSize );
const sf::Uint8 *const pixelPointer = object->GetPixelsPtr();
for(unsigned long index = 0; index < dataSize; index++)
{
rb_ary_store( pixels, index, CHR2FIX( pixelPointer[index] ) );
}
return pixels;
}
/* call-seq:
* image.updatePixels( pixels, rectangle = [0, 0, image.width, image.height] )
*
* Update a sub-rectangle of the image from an array of pixels.
*
* The pixels array is assumed to store RGBA 32 bits pixels. Warning: for performances reasons, this function doesn't
* perform any check; thus you're responsible of ensuring that rectangle does not exceed the image size, and that
* pixels contains enough elements.
*/
static VALUE Image_UpdatePixels( int argc, VALUE *args, VALUE self )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
VALUE somePixels = Qnil;
VALUE aRectangle = Qnil;
sf::IntRect rectangle = sf::IntRect(0, 0, object->GetWidth(), object->GetHeight() );
switch( argc )
{
case 2:
aRectangle = Rect_ForceType( args[1] );
rectangle.Left = FIX2INT( Rect_GetLeft( aRectangle ) );
rectangle.Top = FIX2INT( Rect_GetTop( aRectangle ) );
rectangle.Width = FIX2INT( Rect_GetWidth( aRectangle ) );
rectangle.Height = FIX2INT( Rect_GetHeight( aRectangle ) );
case 1:
VALIDATE_CLASS( args[0], rb_cArray, "pixels" );
somePixels = args[0];
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
const unsigned int rawWidth = FIX2UINT( rectangle.Width );
const unsigned int rawHeight = FIX2UINT( rectangle.Height );
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;
}
object->UpdatePixels( tempData, rectangle );
delete[] tempData;
return Qnil;
}
/* call-seq:
* image.bind()
*
* Activate the image for rendering.
*
* This function is mainly used internally by the SFML render system. However it can be useful when
* using SFML::Image together with OpenGL code (it calls glBindTexture).
*/
static VALUE Image_Bind( VALUE self )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
object->Bind();
return Qnil;
}
/* call-seq:
* image.setSmooth( smooth )
*
* Enable or disable the smooth filter.
*
* When the filter is activated, the image appears smoother so that pixels are less noticeable. However if you want
* the image to look exactly the same as its source file, you should disable it. The smooth filter is enabled
* by default.
*/
static VALUE Image_SetSmooth( VALUE self, VALUE aSmoothFlag )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
if( aSmoothFlag == Qtrue )
{
object->SetSmooth( true );
}
else if( aSmoothFlag == Qfalse )
{
object->SetSmooth( false );
}
else
{
VALIDATE_CLASS( aSmoothFlag, rb_cTrueClass, "smoothFlag" );
}
return Qnil;
}
/* call-seq:
* image.isSmooth() -> true or false
*
* Tell whether the smooth filter is enabled or not.
*/
static VALUE Image_IsSmooth( VALUE self )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
return ( object->IsSmooth() == true ? Qtrue : Qfalse );
}
/* call-seq:
* image.getWidth() -> width
*
* Return the width of the image.
*/
static VALUE Image_GetWidth( VALUE self )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
return INT2FIX( object->GetWidth() );
}
/* call-seq:
* image.getHeight() -> height
*
* Return the height of the image.
*/
static VALUE Image_GetHeight( VALUE self )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
return INT2FIX( object->GetHeight() );
}
/* call-seq:
* image.getTexCoords( rectangle ) -> tex coordinates rectangle
*
* Convert a rectangle of pixels into texture coordinates.
*
* This function is used by code that needs to map the image to some OpenGL geometry. It converts the source
* rectangle, expressed in pixels, to float coordinates in the range [0, 1].
*/
static VALUE Image_GetTexCoords( VALUE self, VALUE aRectangle )
{
VALUE rubyRectangle = Rect_ForceType( aRectangle );
sf::IntRect rectangle;
rectangle.Left = FIX2INT( Rect_GetLeft( aRectangle ) );
rectangle.Top = FIX2INT( Rect_GetTop( aRectangle ) );
rectangle.Width = FIX2INT( Rect_GetWidth( aRectangle ) );
rectangle.Height = FIX2INT( Rect_GetHeight( aRectangle ) );
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
sf::FloatRect result = object->GetTexCoords( rectangle );
return rb_funcall( globalRectClass, rb_intern( "new" ), 4,
rb_float_new( result.Left ), rb_float_new( result.Top ),
rb_float_new( result.Width ), rb_float_new( result.Height ) );
}
/* call-seq:
* Image.new() -> image
* Image.new( filename ) -> image
* Image.new( width, height, pixels ) -> image
*
* Will create a new image instance.
*
* If a filename argument is specified then Image#loadFromFile will be called on the created instance. If width, height
* and pixels are specified then Image#loadFromPixels will be called on the created instance.
*/
static VALUE Image_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 1 )
{
rb_funcall2( self, rb_intern( "loadFromPixels" ), argc, args );
}
else if( argc > 0 )
{
rb_funcall2( self, rb_intern( "loadFromFile" ), argc, args );
}
return self;
}
static VALUE Image_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Image *object = NULL;
Data_Get_Struct( self, sf::Image, object );
sf::Image *source = NULL;
Data_Get_Struct( aSource, sf::Image, source );
*object = *source;
}
static VALUE Image_Alloc( VALUE aKlass )
{
sf::Image *object = new sf::Image();
return Data_Wrap_Struct( aKlass, 0, Image_Free, object );
}
/* call-seq:
* Image.getMaximumSize() -> size
*
* Get the maximum image size allowed.
*
* This maximum size is defined by the graphics driver. You can expect a value of 512 pixels for low-end graphics
* card, and up to 8192 pixels for newer hardware.
*/
static VALUE Image_GetMaximumSize( VALUE aKlass )
{
return INT2FIX( sf::Image::GetMaximumSize() );
}
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, -1 );
rb_define_alloc_func( globalImageClass, Image_Alloc );
rb_define_singleton_method( globalImageClass, "getMaximumSize", Image_GetMaximumSize, 0 );
// Instance methods
rb_define_method( globalImageClass, "initialize", Image_Initialize, -1 );
rb_define_method( globalImageClass, "initialize_copy", Image_InitializeCopy, 1 );
rb_define_method( globalImageClass, "loadFromFile", Image_LoadFromFile, 1 );
rb_define_method( globalImageClass, "loadFromPixels", Image_LoadFromPixels, 3 );
rb_define_method( globalImageClass, "saveToFile", Image_SaveToFile, 1 );
rb_define_method( globalImageClass, "create", Image_Create, -1 );
rb_define_method( globalImageClass, "createMaskFromColor", Image_CreateMaskFromColor, -1 );
rb_define_method( globalImageClass, "copy", Image_Copy, -1 );
rb_define_method( globalImageClass, "copyScreen", Image_CopyScreen, -1 );
rb_define_method( globalImageClass, "setPixel", Image_SetPixel, 3 );
rb_define_method( globalImageClass, "getPixel", Image_GetPixel, 2 );
rb_define_method( globalImageClass, "getPixelsPtr", Image_GetPixelsPtr, 0 );
rb_define_method( globalImageClass, "updatePixels", Image_UpdatePixels, -1 );
rb_define_method( globalImageClass, "bind", Image_Bind, 0 );
rb_define_method( globalImageClass, "setSmooth", Image_SetSmooth, 1 );
rb_define_method( globalImageClass, "isSmooth", Image_IsSmooth, 0 );
rb_define_method( globalImageClass, "getWidth", Image_GetWidth, 0 );
rb_define_method( globalImageClass, "getHeight", Image_GetHeight, 0 );
rb_define_method( globalImageClass, "getTexCoords", Image_GetTexCoords, 1 );
// Class aliases
rb_define_alias( CLASS_OF( globalImageClass ), "maximumSize", "getMaximumSize" );
rb_define_alias( CLASS_OF( globalImageClass ), "maximum_size", "getMaximumSize" );
// Instance Aliases
rb_define_alias( globalImageClass, "load_from_file", "loadFromFile");
rb_define_alias( globalImageClass, "loadFile", "loadFromFile");
rb_define_alias( globalImageClass, "load_file", "loadFromFile");
rb_define_alias( globalImageClass, "load_from_pixels", "loadFromPixels");
rb_define_alias( globalImageClass, "loadPixels", "loadFromPixels");
rb_define_alias( globalImageClass, "load_pixels", "loadFromPixels");
rb_define_alias( globalImageClass, "save_to_file", "saveToFile");
rb_define_alias( globalImageClass, "save", "saveToFile");
rb_define_alias( globalImageClass, "create_mask_from_color", "createMaskFromColor");
rb_define_alias( globalImageClass, "create_mask", "createMaskFromColor");
rb_define_alias( globalImageClass, "createMask", "createMaskFromColor");
rb_define_alias( globalImageClass, "copy_screen", "copyScreen");
rb_define_alias( globalImageClass, "set_pixel", "setPixel");
rb_define_alias( globalImageClass, "get_pixel", "getPixel");
rb_define_alias( globalImageClass, "get_pixels_ptr", "getPixelsPtr");
rb_define_alias( globalImageClass, "pixelsPtr", "getPixelsPtr");
rb_define_alias( globalImageClass, "pixels_ptr", "getPixelsPtr");
rb_define_alias( globalImageClass, "getPixels", "getPixelsPtr");
rb_define_alias( globalImageClass, "get_pixels", "getPixelsPtr");
rb_define_alias( globalImageClass, "pixels", "getPixelsPtr");
rb_define_alias( globalImageClass, "update_pixels", "updatePixels");
rb_define_alias( globalImageClass, "set_smooth", "setSmooth");
rb_define_alias( globalImageClass, "smooth=", "setSmooth");
rb_define_alias( globalImageClass, "is_smooth", "isSmooth");
rb_define_alias( globalImageClass, "smooth?", "isSmooth");
rb_define_alias( globalImageClass, "smooth", "isSmooth");
rb_define_alias( globalImageClass, "get_width", "getWidth");
rb_define_alias( globalImageClass, "width", "getWidth");
rb_define_alias( globalImageClass, "get_height", "getHeight");
rb_define_alias( globalImageClass, "height", "getHeight");
rb_define_alias( globalImageClass, "get_tex_coords", "getTexCoords");
}

View File

@ -1,31 +0,0 @@
/* 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_

View File

@ -1,352 +0,0 @@
/* 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 "Rect.hpp"
#include "Vector2.hpp"
#include "main.hpp"
VALUE globalRectClass;
/* Internal function
* Forces the argument someValue to be a Vector2. IF it can convert it then it will.
* So you can always safely asume that this function returns a Vector2 object.
* If it fails then an exception will be thrown.
*/
VALUE Rect_ForceType( VALUE someValue )
{
if( rb_obj_is_kind_of( someValue, rb_cArray ) == Qtrue )
{
VALUE arg1 = rb_ary_entry( someValue, 0 );
VALUE arg2 = rb_ary_entry( someValue, 1 );
VALUE arg3 = rb_ary_entry( someValue, 2 );
VALUE arg4 = rb_ary_entry( someValue, 3 );
return rb_funcall( globalRectClass, rb_intern( "new" ), 4, arg1, arg2, arg3, arg4 );
}
else if( rb_obj_is_kind_of( someValue, globalRectClass ) == Qtrue )
{
return someValue;
}
else
{
rb_raise( rb_eRuntimeError, "expected Array or Rect" );
}
}
VALUE Rect_GetLeft( VALUE self )
{
static ID id = rb_intern( "left" );
return rb_funcall( self, id, 0 );
}
VALUE Rect_GetTop( VALUE self )
{
static ID id = rb_intern( "top" );
return rb_funcall( self, id, 0 );
}
VALUE Rect_GetWidth( VALUE self )
{
static ID id = rb_intern( "width" );
return rb_funcall( self, id, 0 );
}
VALUE Rect_GetHeight( VALUE self )
{
static ID id = rb_intern( "height" );
return rb_funcall( self, id, 0 );
}
VALUE Rect_SetLeft( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "left=" );
return rb_funcall( self, id, 1, aVal );
}
VALUE Rect_SetTop( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "top=" );
return rb_funcall( self, id, 1, aVal );
}
VALUE Rect_SetWidth( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "width=" );
return rb_funcall( self, id, 1, aVal );
}
VALUE Rect_SetHeight( VALUE self, VALUE aVal )
{
static ID id = rb_intern( "height=" );
return rb_funcall( self, id, 1, aVal );
}
/* Internal function
* Will copy the x and y from aSource to self.
*/
static void Rect_internal_CopyFrom( VALUE self, VALUE aSource )
{
VALUE rect = Rect_ForceType( aSource );
VALUE left = rb_funcall( rect, rb_intern( "left" ), 0 );
VALUE top = rb_funcall( rect, rb_intern( "top" ), 0 );
VALUE width = rb_funcall( rect, rb_intern( "width" ), 0 );
VALUE height = rb_funcall( rect, rb_intern( "height" ), 0 );
rb_funcall( self, rb_intern( "left=" ), 1, left );
rb_funcall( self, rb_intern( "top=" ), 1, top );
rb_funcall( self, rb_intern( "width=" ), 1, width );
rb_funcall( self, rb_intern( "height=" ), 1, height );
rb_iv_set( self, "@dataType", rb_iv_get( rect, "@dataType" ) );
}
/* Internal function
* Validate that the passed types are the same and numeric.
*/
static void Rect_internal_ValidateTypes( VALUE aFirst, VALUE aSecond, VALUE aThird, VALUE aFourth )
{
if( CLASS_OF( aFirst ) != CLASS_OF( aSecond ) || CLASS_OF( aFirst ) != CLASS_OF( aThird ) || CLASS_OF( aFirst ) != CLASS_OF( aFourth ) )
{
rb_raise( rb_eRuntimeError, "left, top, width and height must be of same type" );
}
if( rb_obj_is_kind_of( aFirst, rb_cNumeric ) == Qfalse )
{
rb_raise( rb_eRuntimeError, "left, top, width and height must be numeric!" );
}
}
/* call-seq:
* rect.contains( x, y ) -> true or false
* rect.contains( vector2 ) -> true or false
*
* Check if a point is inside the rectangle's area.
*/
static VALUE Rect_Contains( int argc, VALUE * args, VALUE self )
{
VALUE pointX = Qnil;
VALUE pointY = Qnil;
VALUE left = rb_funcall( self, rb_intern( "left" ), 0 );
VALUE top = rb_funcall( self, rb_intern( "top" ), 0 );
VALUE width = rb_funcall( self, rb_intern( "width" ), 0 );
VALUE height = rb_funcall( self, rb_intern( "height" ), 0 );
switch( argc )
{
case 1:
pointX = Vector2_GetX( args[0] );
pointY = Vector2_GetY( args[0] );
break;
case 2:
VALIDATE_CLASS( args[0], rb_cNumeric, "x" );
VALIDATE_CLASS( args[1], rb_cNumeric, "y" );
pointX = args[0];
pointY = args[1];
break;
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
VALUE first = rb_funcall( pointX, rb_intern( ">=" ), 1, left );
VALUE second = rb_funcall( pointX, rb_intern( "<" ), 1, rb_funcall( left, rb_intern( "+" ), 1, width ) );
VALUE third = rb_funcall( pointY, rb_intern( ">=" ), 1, top );
VALUE fourth = rb_funcall( pointY, rb_intern( "<" ), 1, rb_funcall( top, rb_intern( "+" ), 1, height ) );
if( first == Qtrue && second == Qtrue && third == Qtrue && fourth == Qtrue )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* rect.intersects( rectangle ) -> intersection rectangel or nil
*
* Check the intersection between two rectangles.
*
* This method returns the overlapped rectangle if intersecting otherwise nil.
*/
static VALUE Rect_Intersects( VALUE self, VALUE aRect )
{
VALUE selfLeft = rb_funcall( self, rb_intern( "left" ), 0 );
VALUE selfTop = rb_funcall( self, rb_intern( "top" ), 0 );
VALUE selfWidth = rb_funcall( self, rb_intern( "width" ), 0 );
VALUE selfHeight = rb_funcall( self, rb_intern( "height" ), 0 );
VALUE selfRight = rb_funcall( selfLeft, rb_intern( "+" ), 1, selfWidth );
VALUE selfBottom = rb_funcall( selfTop, rb_intern( "+" ), 1, selfHeight );
VALUE rectLeft = rb_funcall( aRect, rb_intern( "left" ), 0 );
VALUE rectTop = rb_funcall( aRect, rb_intern( "top" ), 0 );
VALUE rectWidth = rb_funcall( aRect, rb_intern( "width" ), 0 );
VALUE rectHeight = rb_funcall( aRect, rb_intern( "height" ), 0 );
VALUE rectRight = rb_funcall( rectLeft, rb_intern( "+" ), 1, rectWidth );
VALUE rectBottom = rb_funcall( rectTop, rb_intern( "+" ), 1, rectHeight );
VALUE left, top, right, bottom;
if( rb_funcall( selfLeft, rb_intern( ">" ), 1, rectLeft ) == Qtrue )
{
left = selfLeft;
}
else
{
left = rectLeft;
}
if( rb_funcall( selfTop, rb_intern( ">" ), 1, rectTop ) == Qtrue )
{
top = selfTop;
}
else
{
top = rectTop;
}
if( rb_funcall( selfRight , rb_intern( "<" ), 1, rectRight ) == Qtrue )
{
right = selfRight;
}
else
{
right = rectRight;
}
if( rb_funcall( selfBottom , rb_intern( "<" ), 1, rectBottom ) == Qtrue )
{
bottom = selfBottom;
}
else
{
bottom = rectBottom;
}
if( rb_funcall( left, rb_intern( "<" ), 1, right) == Qtrue && rb_funcall( top, rb_intern( "<" ), 1, bottom) == Qtrue )
{
VALUE newWidth = rb_funcall( right, rb_intern( "-" ), 1, left );
VALUE newHeight = rb_funcall( bottom, rb_intern( "-" ), 1, top );
return rb_funcall( globalRectClass, rb_intern( "new" ), 4, left, top, newWidth, newHeight );
}
else
{
return Qnil;
}
}
/* call-seq:
* Rect.new() -> rect
* Rect.new( [left, top, width, height] ) -> rect
* Rect.new( rect ) -> rect
* Rect.new( left, top, width, height ) -> rect
* Rect.new( position, size ) -> rect
*
* Create a new rect instance.
*/
static VALUE Rect_Initialize( int argc, VALUE *args, VALUE self )
{
VALUE arg0 = Qnil;
VALUE arg1 = Qnil;
switch( argc )
{
case 0:
rb_iv_set( self, "@left", INT2NUM( 0 ) );
rb_iv_set( self, "@top", INT2NUM( 0 ) );
rb_iv_set( self, "@width", INT2NUM( 0 ) );
rb_iv_set( self, "@height", INT2NUM( 0 ) );
break;
case 1:
Rect_internal_CopyFrom( self, args[0] );
break;
case 2:
arg0 = Vector2_ForceType( args[0] );
arg1 = Vector2_ForceType( args[1] );
rb_iv_set( self, "@left", Vector2_GetX( arg0 ) );
rb_iv_set( self, "@top", Vector2_GetY( arg0 ) );
rb_iv_set( self, "@width", Vector2_GetX( arg1 ) );
rb_iv_set( self, "@height", Vector2_GetY( arg1 ) );
break;
case 4:
Rect_internal_ValidateTypes( args[0], args[1], args[2], args[3] );
rb_iv_set( self, "@left", args[0]);
rb_iv_set( self, "@top", args[1]);
rb_iv_set( self, "@width", args[2]);
rb_iv_set( self, "@height", args[3]);
break;
default:
rb_raise( rb_eArgError, "Expected 0, 1, 2 or 4 arguments but was given %d", argc );
}
rb_iv_set( self, "@dataType", CLASS_OF( rb_iv_get( self, "@left" ) ) );
return self;
}
void Init_Rect( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Utility class for manipulating 2D axis aligned rectangles.
*
* A rectangle is defined by its top-left corner and its size.
*
* It is a very simple class defined for convenience, so its member variables (left, top, width and height) are public
* and can be accessed directly, just like the vector classes (SFML::Vector2 and SFML::Vector3).
*
* To keep things simple, SFML::Rect doesn't define functions to emulate the properties that are not directly members
* (such as right, bottom, center, etc.), it rather only provides intersection functions.
*
* SFML::Rect uses the usual rules for its boundaries:
*
* - The left and top edges are included in the rectangle's area
* - The right (left + width) and bottom (top + height) edges are excluded from the rectangle's area
*
* This means that SFML::Rect.new(0, 0, 1, 1) and SFML::Rect.new(1, 1, 1, 1) don't intersect.
*
* SFML::Rect works just like SFML::Vector2 and SFML::Vector3 when it comes to types. It will accept any value that is
* Numeric but all values must be of the same class.
*
* Usage example:
*
* # Define a rectangle, located at (0, 0) with a size of 20x5
* r1 = SFML::Rect.new( 0, 0, 20, 5 )
*
* # Define another rectangle, located at (4, 2) with a size of 18x10
* position = SFML::Vector2.new( 4, 2 )
* size = SFML::Vector2.new( 18, 10 )
* r2 = SFML::Rect.new( position, size )
*
* # Test intersections with the point (3, 1)
* b1 = r1.contains( 3, 1 ) # true
* b2 = r2.contains( 3, 1 ) # false
*
* # Test the intersection between r1 and r2
* result = r1.intersects( r2 ) # If r1 don't intersect r2 then result would be nil
* # result == (4, 2, 16, 3)
*
*/
globalRectClass = rb_define_class_under( sfml, "Rect", rb_cObject );
// Instance methods
rb_define_method( globalRectClass, "initialize", Rect_Initialize, -1 );
rb_define_method( globalRectClass, "contains", Rect_Contains, -1 );
rb_define_method( globalRectClass, "intersects", Rect_Intersects, 1 );
// Instance operators
// Attribute accessors
rb_define_attr( globalRectClass, "left", 1, 1 );
rb_define_attr( globalRectClass, "top", 1, 1 );
rb_define_attr( globalRectClass, "width", 1, 1 );
rb_define_attr( globalRectClass, "height", 1, 1 );
}

View File

@ -1,43 +0,0 @@
/* 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_RECT_HEADER_
#define SFML_RUBYEXT_RECT_HEADER_
#include "ruby.h"
VALUE Rect_ForceType( VALUE someValue );
VALUE Rect_GetLeft( VALUE self );
VALUE Rect_GetTop( VALUE self );
VALUE Rect_GetWidth( VALUE self );
VALUE Rect_GetHeight( VALUE self );
VALUE Rect_SetLeft( VALUE self, VALUE aVal );
VALUE Rect_SetTop( VALUE self, VALUE aVal );
VALUE Rect_SetWidth( VALUE self, VALUE aVal );
VALUE Rect_SetHeight( VALUE self, VALUE aVal );
void Init_Rect( void );
#endif // SFML_RUBYEXT_RECT_HEADER_

View File

@ -1,435 +0,0 @@
/* 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 "RenderImage.hpp"
#include "main.hpp"
#include <SFML/Graphics/RenderImage.hpp>
VALUE globalRenderImageClass;
/* External classes */
extern VALUE globalRenderTargetModule;
extern VALUE globalImageClass;
extern VALUE globalDrawableModule;
extern VALUE globalShaderClass;
extern VALUE globalViewClass;
static void RenderImage_Free( sf::RenderImage *anObject )
{
delete anObject;
}
static void View_Free( sf::View *anObject )
{
delete anObject;
}
/* call-seq:
* render_image.create( width, height, depthBuffer = false ) -> true or false
*
* Create the render-image.
*
* Before calling this function, the render-image is in an invalid state, thus it is mandatory to call it before
* doing anything with the render-image. The last parameter, depthBuffer, is useful if you want to use the render-image
* for 3D OpenGL rendering that requires a depth-buffer. Otherwise it is unnecessary, and you should leave this
* parameter to false (which is its default value).
*/
static VALUE RenderImage_Create( int argc, VALUE *args, VALUE self )
{
unsigned int width = 0;
unsigned int height = 0;
bool depthBuffer = false;
switch( argc )
{
case 3:
if( args[2] == Qtrue )
{
depthBuffer = true;
}
else if( args[2] == Qfalse )
{
depthBuffer = false;
}
else
{
VALIDATE_CLASS( args[2], rb_cTrueClass, "depthBuffer" );
}
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 );
}
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
if( object->Create( width, height, depthBuffer ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
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;
}
/* call-seq:
* render_image.display()
*
* Update the contents of the target image.
*
* This function updates the target image with what has been drawn so far. Like for windows, calling this function is
* mandatory at the end of rendering. Not calling it may leave the image in an undefined state.
*/
static VALUE RenderImage_Display( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
object->Display();
return Qnil;
}
/* call-seq:
* render_image.getImage() -> image
*
* Get a read-only reference to the target image.
*
* After drawing to the render-image and calling display, you can retrieve the updated image using this function, and
* draw it using a sprite (for example). The internal SFML::Image of a render-image is always the same instance, so that
* it is possible to call this function once and keep a reference to the image even after is it modified.
*/
static VALUE RenderImage_GetImage( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
const sf::Image &image = object->GetImage();
VALUE rbData = Data_Wrap_Struct( globalImageClass, 0, 0, const_cast< sf::Image * >( &image ) );
rb_obj_call_init( rbData, 0, 0 );
rb_iv_set( rbData, "@__owner_ref", self );
return rbData;
}
/* call-seq:
* render_image.isSmooth() -> true or false
*
* Tell whether the smooth filtering is enabled or not.
*/
static VALUE RenderImage_IsSmooth( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
return ( object->IsSmooth() == true ? Qtrue : Qfalse );
}
/* call-seq:
* render_image.setActive( active )
*
* Activate of deactivate the render-image for rendering.
*
* This function makes the render-image's context current for future OpenGL rendering operations (so you shouldn't
* care about it if you're not doing direct OpenGL stuff). Only one context can be current on a thread, so if you want
* to draw OpenGL geometry to another render target (like a RenderWindow) don't forget to activate it again.
*/
static VALUE RenderImage_SetActive( int argc, VALUE *args, VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
bool flag = true;
switch( argc )
{
case 1:
if( args[0] == Qtrue )
{
flag = true;
}
else if( args[0] == Qfalse )
{
flag = false;
}
else
{
VALIDATE_CLASS( args[0], rb_cTrueClass, "active" );
}
case 0:
break;
default:
rb_raise( rb_eArgError, "Expected 0 or 1 arguments but was given %d", argc );
}
object->SetActive( flag );
return Qnil;
}
/* call-seq:
* render_image.setSmooth( smooth )
*
* Enable or disable image smoothing.
*
* This function is similar to SFML::Image#setSmooth. This parameter is enabled by default.
*/
static VALUE RenderImage_SetSmooth( VALUE self, VALUE aSmoothFlag )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
if( aSmoothFlag == Qtrue )
{
object->SetSmooth( true );
}
else if( aSmoothFlag == Qfalse )
{
object->SetSmooth( false );
}
else
{
VALIDATE_CLASS( aSmoothFlag, rb_cTrueClass, "smooth" );
}
return Qnil;
}
static VALUE RenderImage_SetView( VALUE self, VALUE aView )
{
VALIDATE_CLASS( aView, globalViewClass, "view" );
sf::View *view = NULL;
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
Data_Get_Struct( aView, sf::View, view );
object->SetView( *view );
return Qnil;
}
static VALUE RenderImage_GetView( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
const sf::View &original = object->GetView();
sf::View * view = new sf::View( original );
VALUE rbData = Data_Wrap_Struct( globalViewClass, 0, View_Free, view );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
static VALUE RenderImage_GetDefaultView( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
const sf::View &original = object->GetDefaultView();
sf::View * view = new sf::View( original );
VALUE rbData = Data_Wrap_Struct( globalViewClass, 0, View_Free, view );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
static VALUE RenderImage_GetWidth( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
return INT2FIX( object->GetWidth() );
}
static VALUE RenderImage_GetHeight( VALUE self )
{
sf::RenderImage *object = NULL;
Data_Get_Struct( self, sf::RenderImage, object );
return INT2FIX( object->GetHeight() );
}
/* call-seq:
* RenderImage.new() -> render_image
* RenderImage.new( width, height, depthBuffer = false ) -> render_image
*
* Will create a new render image instance.
*
* If any arguments are specified then a call to the #create method will be made passing the arguments to it.
*/
static VALUE RenderImage_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 0 )
{
rb_funcall2( self, rb_intern( "create" ), argc, args );
}
return self;
}
static VALUE RenderImage_Alloc( VALUE aKlass )
{
sf::RenderImage *object = new sf::RenderImage();
return Data_Wrap_Struct( aKlass, 0, RenderImage_Free, object );
}
/* call-seq:
* RenderImage.isAvailable() -> true or false
*
* Check whether the system supports render images or not.
*
* It is very important to always call this function before trying to use the RenderImage class, as the feature may
* not be supported on all platforms (especially very old ones). If this function returns false, then you won't be
* able to use the class at all.
*/
static VALUE RenderImage_IsAvailable( VALUE aKlass )
{
return ( sf::RenderImage::IsAvailable() == true ? Qtrue : Qfalse );
}
void Init_RenderImage( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Target for off-screen 2D rendering into an image.
*
* sf::RenderImage is the little brother of sf::RenderWindow.
*
* It implements the same 2D drawing and OpenGL-related functions (see their base class sf::RenderTarget for
* more details), the difference is that the result is stored in an off-screen image rather than being show in a window.
*
* Rendering to an image can be useful in a variety of situations:
*
* - precomputing a complex static image (like a level's background from multiple tiles)
* - applying post-effects to the whole scene with shaders
* - creating a sprite from a 3D object rendered with OpenGL
* - etc.
*
* Usage example:
*
* # First of all: make sure that rendering to image is supported
* if SFML::RenderImage.available? == false
* # Handle error
* end
*
* # Create a new render-window
* window = SFML::RenderWindow.new( SFML::VideoMode.new(800, 600), "SFML window" )
*
* # Create a new render-image
* image = SFML::RenderImage.new
* if image.create( 500, 500 ) == false
* # Handle error
* end
*
* # The main loop
* while window.open?
* # Event processing
* # ...
*
* # Clear the whole image with red color
* image.clear( SFML::Color::Red )
*
* # Draw stuff to the image
* image.draw( sprite ) # sprite is a SFML::Sprite
* image.draw( shape ) # shape is a SFML::Shape
* image.draw( text ) # text is a SFML::Text
*
* # We're done drawing to the image
* image.display()
*
* # Now we start rendering to the window, clear it first
* window.clear()
*
* # Draw the image
* sprite = SFML::Sprite.new( image.getImage() )
* window.draw( sprite )
*
* # End the current frame and display its contents on screen
* window.display()
* end
*
* Like SFML::RenderWindow, SFML::RenderImage is still able to render direct OpenGL stuff. It is even possible to mix
* together OpenGL calls and regular SFML drawing commands. If you need a depth buffer for 3D rendering, don't
* forget to request it when calling SFML::RenderImage#create.
*/
globalRenderImageClass = rb_define_class_under( sfml, "RenderImage", rb_cObject );
rb_include_module( globalRenderImageClass, globalRenderTargetModule );
// Class methods
//rb_define_singleton_method( globalRenderImageClass, "new", RenderImage_New, 0 );
rb_define_alloc_func( globalRenderImageClass, RenderImage_Alloc );
rb_define_singleton_method( globalRenderImageClass, "isAvailable", RenderImage_IsAvailable, 0 );
// Instance methods
rb_define_method( globalRenderImageClass, "initialize", RenderImage_Initialize, -1 );
rb_define_method( globalRenderImageClass, "draw", RenderImage_Create, -1 );
rb_define_method( globalRenderImageClass, "create", RenderImage_Create, -1 );
rb_define_method( globalRenderImageClass, "display", RenderImage_Display, 0 );
rb_define_method( globalRenderImageClass, "getImage", RenderImage_GetImage, 0 );
rb_define_method( globalRenderImageClass, "isSmooth", RenderImage_IsSmooth, 0 );
rb_define_method( globalRenderImageClass, "setActive", RenderImage_SetActive, -1 );
rb_define_method( globalRenderImageClass, "setSmooth", RenderImage_SetSmooth, 1 );
rb_define_method( globalRenderImageClass, "getView", RenderImage_GetView, 0 );
rb_define_method( globalRenderImageClass, "setView", RenderImage_SetView, 1 );
rb_define_method( globalRenderImageClass, "getDefaultView", RenderImage_GetDefaultView, 0 );
rb_define_method( globalRenderImageClass, "getWidth", RenderImage_GetWidth, 0 );
rb_define_method( globalRenderImageClass, "getHeight", RenderImage_GetHeight, 0 );
// Class Aliases
rb_define_alias( CLASS_OF( globalRenderImageClass ), "is_available", "isAvailable" );
rb_define_alias( CLASS_OF( globalRenderImageClass ), "available?", "isAvailable" );
// Instance Aliases
rb_define_alias( globalRenderImageClass, "image", "getImage" );
rb_define_alias( globalRenderImageClass, "is_smooth", "isSmooth" );
rb_define_alias( globalRenderImageClass, "smooth?", "isSmooth" );
rb_define_alias( globalRenderImageClass, "set_active", "setActive" );
rb_define_alias( globalRenderImageClass, "activate", "setActive" );
rb_define_alias( globalRenderImageClass, "active=", "setActive" );
rb_define_alias( globalRenderImageClass, "smooth=", "setSmooth" );
rb_define_alias( globalRenderImageClass, "view=", "setView" );
rb_define_alias( globalRenderImageClass, "view", "getView" );
rb_define_alias( globalRenderImageClass, "defaultView", "getDefaultView" );
rb_define_alias( globalRenderImageClass, "default_view", "getDefaultView" );
rb_define_alias( globalRenderImageClass, "width", "getWidth" );
rb_define_alias( globalRenderImageClass, "height", "getHeight" );
}

View File

@ -1,31 +0,0 @@
/* 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_IMAGE_HEADER_
#define SFML_RUBYEXT_RENDER_IMAGE_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_RenderImage( void );
#endif // SFML_RUBYEXT_RENDER_IMAGE_HEADER_

View File

@ -1,356 +0,0 @@
/* 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 "RenderTarget.hpp"
#include "Color.hpp"
#include "main.hpp"
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RenderImage.hpp>
VALUE globalRenderTargetModule;
VALUE globalRenderTargetInstanceClass;
/* External classes */
extern VALUE globalRenderWindowClass;
extern VALUE globalVector2Class;
extern VALUE globalRectClass;
extern VALUE globalDrawableModule;
extern VALUE globalShaderClass;
extern VALUE globalViewClass;
extern VALUE globalNonCopyableModule;
static VALUE View_Free( sf::View *anObject )
{
delete anObject;
}
/* call-seq:
* render_target.clear( color = SFML::Color::Black )
*
* Clear the entire target with a single color.
*
* This function is usually called once every frame, to clear the previous contents of the target.
*/
static VALUE RenderTarget_Clear( int argc, VALUE *args, VALUE self )
{
sf::Color color = sf::Color::Black;
switch( argc )
{
case 1:
{
VALUE temp = Color_ForceType( args[0] );
color.r = FIX2UINT( Color_GetR( temp ) );
color.g = FIX2UINT( Color_GetG( temp ) );
color.b = FIX2UINT( Color_GetB( temp ) );
color.a = FIX2UINT( Color_GetA( temp ) );
}
case 0:
break;
default:
rb_raise( rb_eArgError, "Expected 0 or 1 arguments but was given %d", argc );
}
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
object->Clear( color );
return Qnil;
}
/* call-seq:
* render_target.draw( drawable )
* render_target.draw( drawable, shader )
*
* Draw an object into the target with a shader.
*
* This function draws anything that inherits from the SFML::Drawable base class (SFML::Sprite, SFML::Shape, SFML::Text,
* or even your own derived classes). The shader alters the way that the pixels are processed right before being written
* to the render target.
*/
static VALUE RenderTarget_Draw( int argc, VALUE *args, VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
VALUE targetWrap = Qnil;
sf::RenderWindow *window = NULL;
sf::RenderImage *image = NULL;
if( CLASS_OF( self ) == globalRenderWindowClass )
{
Data_Get_Struct( self, sf::RenderWindow, window );
}
else
{
Data_Get_Struct( self, sf::RenderImage, image );
}
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 );
( window != NULL ? window->Draw( *drawable, *shader ) : image->Draw( *drawable, *shader ) );
break;
}
case 1:
{
VALIDATE_CLASS( args[0], globalDrawableModule, "object" );
sf::Drawable *drawable = NULL;
Data_Get_Struct( args[0], sf::Drawable, drawable );
( window != NULL ? window->Draw( *drawable ) : image->Draw( *drawable ) );
break;
}
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
return Qnil;
}
/* call-seq:
* render_target.getWidth() -> fixnum
*
* Return the width of the rendering region of the target.
*/
static VALUE RenderTarget_GetWidth( VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
return INT2FIX( object->GetWidth() );
}
/* call-seq:
* render_target.getHeight() -> fixnum
*
* Return the height of the rendering region of the target.
*/
static VALUE RenderTarget_GetHeight( VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
return INT2FIX( object->GetHeight() );
}
/* call-seq:
* render_target.setView( view )
*
* Change the current active view.
*
* The new view will affect everything that is drawn, until another view is activated. The render target keeps its own
* copy of the view object, so it is not necessary to keep the original one alive as long as it is in use. To restore
* the original view of the target, you can pass the result of getDefaultView() to this function.
*/
static VALUE RenderTarget_SetView( VALUE self, VALUE aView )
{
VALIDATE_CLASS( aView, globalViewClass, "view" );
sf::View *view = NULL;
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
Data_Get_Struct( aView, sf::View, view );
object->SetView( *view );
return Qnil;
}
/* call-seq:
* render_target.getView() -> view
*
* Retrieve the view currently in use in the render target.
*/
static VALUE RenderTarget_GetView( VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
const sf::View &original = object->GetView();
sf::View * view = new sf::View( original );
VALUE rbData = Data_Wrap_Struct( globalViewClass, 0, View_Free, view );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
/* call-seq:
* render_target.getDefaultView() -> VIEW
*
* Get the default view of the render target.
*
* The default view has the initial size of the render target, and never changes after the target has been created.
*/
static VALUE RenderTarget_GetDefaultView( VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
const sf::View &view = object->GetDefaultView();
VALUE rbData = Data_Wrap_Struct( globalViewClass, 0, 0, const_cast< sf::View * >( &view ) );
rb_obj_call_init( rbData, 0, 0 );
rb_iv_set( rbData, "@__owner_ref", self );
return rbData;
}
/* call-seq:
* render_target.getViewport( view ) -> rectangle
*
* Get the viewport of a view, applied to this render target.
*
* The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions
* of the render target to calculate the pixels rectangle that the viewport actually covers in the target.
*/
static VALUE RenderTarget_GetViewport( VALUE self, VALUE aView )
{
VALIDATE_CLASS( aView, globalViewClass, "view" );
sf::View *view = NULL;
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
Data_Get_Struct( aView, sf::View, view );
sf::IntRect viewport = object->GetViewport( *view );
return rb_funcall( globalRectClass, rb_intern( "new" ), 4,
INT2FIX( viewport.Left ), INT2FIX( viewport.Top ),
INT2FIX( viewport.Width ), INT2FIX( viewport.Height ) );
}
/* call-seq:
* render_target.convertCoords( x, y ) -> vector2
* render_target.convertCoords( x, y, view ) -> vector2
*
* Convert a point from target coordinates to view coordinates.
*
* Initially, a unit of the 2D world matches a pixel of the render target. But if you define a custom view, this
* assertion is not true anymore, ie. a point located at (10, 50) in your render target (for example a window) may
* map to the point (150, 75) in your 2D world -- for example if the view is translated by (140, 25).
*
* For render windows, this function is typically used to find which point (or object) is located below the mouse cursor.
*
*/
static VALUE RenderTarget_ConvertCoords( int argc, VALUE *args, VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
sf::Vector2f result;
switch( argc )
{
case 2:
{
result = object->ConvertCoords( FIX2UINT( args[0] ), FIX2UINT( args[1] ) );
break;
}
case 3:
{
VALIDATE_CLASS( args[2], globalViewClass, "view" );
sf::View *view = NULL;
Data_Get_Struct( args[2], sf::View, view );
result = object->ConvertCoords( FIX2UINT( args[0] ), FIX2UINT( args[1] ), *view );
}
default:
rb_raise( rb_eArgError, "Expected 2 or 3 arguments but was given %d", argc );
}
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( result.x ), rb_float_new( result.y ) );
}
/* call-seq:
* render_target.saveGLStates()
*
* Save the current OpenGL render states and matrices.
*
* This function can be used when you mix SFML drawing and direct OpenGL rendering. Combined with RestoreGLStates,
* it ensures that:
*
* - SFML's internal states are not messed up by your OpenGL code
* - your OpenGL states are not modified by a call to a SFML function
*
* More specifically, it must be used around code that calls Draw functions. Example:
*
* # OpenGL code here...
* window.saveGLStates()
* window.draw(...)
* window.draw(...)
* window.restoreGLStates()
* # OpenGL code here...
*
* Note that this function is quite expensive and should be used wisely. It is provided for convenience, and the
* best results will be achieved if you handle OpenGL states yourself (because you really know which states have
* really changed, and need to be saved / restored).
*/
static VALUE RenderTarget_SaveGLStates( VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
object->SaveGLStates();
return Qnil;
}
/* call-seq:
* render_target.restoreGLStates()
*
* Restore the previously saved OpenGL render states and matrices.
*
* See the description of saveGLStates to get a detailed description of these functions.
*/
static VALUE RenderTarget_RestoreGLStates( VALUE self )
{
sf::RenderTarget *object = NULL;
Data_Get_Struct( self, sf::RenderTarget, object );
object->RestoreGLStates();
return Qnil;
}
void Init_RenderTarget( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* This is not the same class as the one specified in the C++ library! This class acts as a wrapper around the
* classes that inherits from sf::RenderTarget(sf::RenderWindow, sf::RenderImage) and you will only use it as the
* first argument in the SFML::Drawable#render method.
*/
globalRenderTargetModule = rb_define_module_under( sfml, "RenderTarget" );
rb_include_module( globalRenderTargetModule, globalNonCopyableModule );
globalRenderTargetInstanceClass = rb_define_class_under( globalRenderTargetModule, "Instance", rb_cObject );
rb_include_module( globalRenderTargetInstanceClass, globalRenderTargetModule );
// Instance methods
rb_define_method( globalRenderTargetModule, "clear", RenderTarget_Clear, -1 );
rb_define_method( globalRenderTargetModule, "draw", RenderTarget_Draw, -1 );
rb_define_method( globalRenderTargetModule, "getWidth", RenderTarget_GetWidth, 0 );
rb_define_method( globalRenderTargetModule, "getHeight", RenderTarget_GetHeight, 0 );
rb_define_method( globalRenderTargetModule, "setView", RenderTarget_SetView, 1 );
rb_define_method( globalRenderTargetModule, "getView", RenderTarget_GetView, 0 );
rb_define_method( globalRenderTargetModule, "getDefaultView", RenderTarget_GetDefaultView, 0 );
rb_define_method( globalRenderTargetModule, "getViewport", RenderTarget_GetViewport, 1 );
rb_define_method( globalRenderTargetModule, "convertCoords", RenderTarget_ConvertCoords, -1 );
rb_define_method( globalRenderTargetModule, "saveGLStates", RenderTarget_SaveGLStates, 0 );
rb_define_method( globalRenderTargetModule, "restoreGLStates", RenderTarget_RestoreGLStates, 0 );
// Instance Aliases
rb_define_alias( globalRenderTargetModule, "width", "getWidth" );
rb_define_alias( globalRenderTargetModule, "height", "getHeight" );
rb_define_alias( globalRenderTargetModule, "view=", "setView" );
rb_define_alias( globalRenderTargetModule, "view", "getView" );
rb_define_alias( globalRenderTargetModule, "defaultView", "getDefaultView" );
rb_define_alias( globalRenderTargetModule, "default_view", "getDefaultView" );
rb_define_alias( globalRenderTargetModule, "get_viewport", "getViewport" );
rb_define_alias( globalRenderTargetModule, "convert_cords", "convertCoords" );
rb_define_alias( globalRenderTargetModule, "save_gl_states", "saveGLStates" );
rb_define_alias( globalRenderTargetModule, "restore_gl_states", "restoreGLStates" );
}

View File

@ -1,31 +0,0 @@
/* 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_TARGET_HEADER_
#define SFML_RUBYEXT_RENDER_TARGET_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_RenderTarget( void );
#endif // SFML_RUBYEXT_RENDER_TARGET_HEADER_

View File

@ -1,241 +0,0 @@
/* 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 <SFML/Graphics/RenderWindow.hpp>
VALUE globalRenderWindowClass;
/* External classes */
extern VALUE globalRenderTargetModule;
extern VALUE globalWindowClass;
extern VALUE globalDrawableModule;
extern VALUE globalShaderClass;
extern VALUE globalViewClass;
static void RenderWindow_Free( sf::RenderWindow *anObject )
{
delete anObject;
}
static void View_Free( sf::View *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_SetView( VALUE self, VALUE aView )
{
VALIDATE_CLASS( aView, globalViewClass, "view" );
sf::View *view = NULL;
sf::RenderWindow *object = NULL;
Data_Get_Struct( self, sf::RenderWindow, object );
Data_Get_Struct( aView, sf::View, view );
object->SetView( *view );
return Qnil;
}
static VALUE RenderWindow_GetView( VALUE self )
{
sf::RenderWindow *object = NULL;
Data_Get_Struct( self, sf::RenderWindow, object );
const sf::View &original = object->GetView();
sf::View * view = new sf::View( original );
VALUE rbData = Data_Wrap_Struct( globalViewClass, 0, View_Free, view );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
static VALUE RenderWindow_GetDefaultView( VALUE self )
{
sf::RenderWindow *object = NULL;
Data_Get_Struct( self, sf::RenderWindow, object );
const sf::View &original = object->GetDefaultView();
sf::View * view = new sf::View( original );
VALUE rbData = Data_Wrap_Struct( globalViewClass, 0, View_Free, view );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
static VALUE RenderWindow_GetWidth( VALUE self )
{
sf::RenderWindow *object = NULL;
Data_Get_Struct( self, sf::RenderWindow, object );
return INT2FIX( object->GetWidth() );
}
static VALUE RenderWindow_GetHeight( VALUE self )
{
sf::RenderWindow *object = NULL;
Data_Get_Struct( self, sf::RenderWindow, object );
return INT2FIX( object->GetHeight() );
}
static VALUE RenderWindow_Alloc( VALUE aKlass )
{
sf::RenderWindow *object = new sf::RenderWindow();
return Data_Wrap_Struct( aKlass, 0, RenderWindow_Free, object );
}
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 );
rb_define_alloc_func( globalRenderWindowClass, RenderWindow_Alloc );
// Instance methods
rb_define_method( globalRenderWindowClass, "draw", RenderWindow_Draw, -1 );
rb_define_method( globalRenderWindowClass, "getView", RenderWindow_GetView, 0 );
rb_define_method( globalRenderWindowClass, "setView", RenderWindow_SetView, 1 );
rb_define_method( globalRenderWindowClass, "getDefaultView", RenderWindow_GetDefaultView, 0 );
rb_define_method( globalRenderWindowClass, "getWidth", RenderWindow_GetWidth, 0 );
rb_define_method( globalRenderWindowClass, "getHeight", RenderWindow_GetHeight, 0 );
// Alias
rb_define_alias( globalRenderWindowClass, "view=", "setView" );
rb_define_alias( globalRenderWindowClass, "view", "getView" );
rb_define_alias( globalRenderWindowClass, "defaultView", "getDefaultView" );
rb_define_alias( globalRenderWindowClass, "default_view", "getDefaultView" );
rb_define_alias( globalRenderWindowClass, "width", "getWidth" );
rb_define_alias( globalRenderWindowClass, "height", "getHeight" );
}

View File

@ -1,31 +0,0 @@
/* 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_

View File

@ -1,373 +0,0 @@
/* 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 "Renderer.hpp"
#include "Color.hpp"
#include "Rect.hpp"
#include "main.hpp"
#include <SFML/Graphics/Renderer.hpp>
VALUE globalRendererClass;
/* External classes */
extern VALUE globalColorClass;
extern VALUE globalImageClass;
extern VALUE globalShaderClass;
extern VALUE globalNonCopyableModule;
/* call-seq:
* renderer.saveGLStates()
*
* Save the current OpenGL render states and matrices.
*/
static VALUE Renderer_SaveGLStates( VALUE self )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->SaveGLStates();
return Qnil;
}
/* call-seq:
* renderer.restoreGLStates()
*
* Restore the previously saved OpenGL render states and matrices.
*/
static VALUE Renderer_RestoreGLStates( VALUE self )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->RestoreGLStates();
return Qnil;
}
/* call-seq:
* renderer.clear()
*
* Clear the color buffer.
*/
static VALUE Renderer_Clear( VALUE self, VALUE aColor )
{
VALUE temp = Color_ForceType( aColor );
sf::Color color;
color.r = FIX2UINT( Color_GetR( temp ) );
color.g = FIX2UINT( Color_GetG( temp ) );
color.b = FIX2UINT( Color_GetB( temp ) );
color.a = FIX2UINT( Color_GetA( temp ) );
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->Clear( color );
return Qnil;
}
/* call-seq:
* renderer.pushStates()
*
* Save the current render states.
*/
static VALUE Renderer_PushStates( VALUE self )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->PushStates();
return Qnil;
}
/* call-seq:
* renderer.popStates()
*
* Restore the previously saved render states.
*/
static VALUE Renderer_PopStates( VALUE self )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->PopStates();
return Qnil;
}
/* call-seq:
* renderer.setColor( color )
*
* Set the current global color.
*
* This color will be modulated with each vertex's color. Note: any call to this function after a call to BeginBatch
* will be ignored, and delayed until BeginBatch is called again.
*/
static VALUE Renderer_SetColor( VALUE self, VALUE aColor )
{
VALUE temp = Color_ForceType( aColor );
sf::Color color;
color.r = FIX2UINT( Color_GetR( temp ) );
color.g = FIX2UINT( Color_GetG( temp ) );
color.b = FIX2UINT( Color_GetB( temp ) );
color.a = FIX2UINT( Color_GetA( temp ) );
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->SetColor( color );
return Qnil;
}
/* call-seq:
* renderer.applyColor( color )
*
* Modulate the current global color with a new one.
*
* This color will be modulated with each vertex's color. Note: any call to this function after a call to BeginBatch
* will be ignored, and delayed until BeginBatch is called again.
*/
static VALUE Renderer_ApplyColor( VALUE self, VALUE aColor )
{
VALUE temp = Color_ForceType( aColor );
sf::Color color;
color.r = FIX2UINT( Color_GetR( temp ) );
color.g = FIX2UINT( Color_GetG( temp ) );
color.b = FIX2UINT( Color_GetB( temp ) );
color.a = FIX2UINT( Color_GetA( temp ) );
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->ApplyColor( color );
return Qnil;
}
/* call-seq:
* renderer.setViewport( rectangle )
*
* Set the current viewport.
*
* Note: any call to this function after a call to BeginBatch will be ignored, and delayed until BeginBatch is called again.
*/
static VALUE Renderer_SetViewport( VALUE self, VALUE aRect )
{
VALUE temp = Rect_ForceType( aRect );
sf::IntRect rectangle;
rectangle.Left = FIX2UINT( Rect_GetLeft( temp ) );
rectangle.Top = FIX2UINT( Rect_GetTop( temp ) );
rectangle.Width = FIX2UINT( Rect_GetWidth( temp ) );
rectangle.Height = FIX2UINT( Rect_GetHeight( temp ) );
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->SetViewport( rectangle );
return Qnil;
}
/* call-seq:
* renderer.setBlendMode( mode )
*
* Set the current alpha-blending mode.
*
* Note: any call to this function after a call to BeginBatch will be ignored, and delayed until BeginBatch is called again.
*/
static VALUE Renderer_SetBlendMode( VALUE self, VALUE aMode )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->SetBlendMode( static_cast< sf::Blend::Mode >( FIX2INT( aMode ) ) );
return Qnil;
}
/* call-seq:
* renderer.setTexture( texture )
*
* Set the current texture.
*
* Note: any call to this function after a call to BeginBatch will be ignored, and delayed until BeginBatch is called again.
*/
static VALUE Renderer_SetTexture( VALUE self, VALUE aTexture )
{
VALIDATE_CLASS( aTexture, globalImageClass, "texture" );
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
sf::Image * texture = NULL;
Data_Get_Struct( aTexture, sf::Image, texture );
object->SetTexture( texture );
return Qnil;
}
/* call-seq:
* renderer.setShader( shader )
*
* Set the current shader.
*
* Note: any call to this function after a call to BeginBatch will be ignored, and delayed until BeginBatch is called again.
*/
static VALUE Renderer_SetShader( VALUE self, VALUE aShader )
{
VALIDATE_CLASS( aShader, globalShaderClass, "shader" );
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
sf::Shader * shader = NULL;
Data_Get_Struct( aShader, sf::Shader, shader );
object->SetShader( shader );
return Qnil;
}
/* call-seq:
* renderer.begin( type )
*
* Begin rendering a new geometry batch.
*
* You need to call SFML::Renderer#end to complete the batch and trigger the actual rendering of the geometry that you
* passed between begin() and end().
*
* Usage:
*
* renderer.begin( SFML::Renderer::TriangleList )
* renderer.addVertex(...)
* renderer.addVertex(...)
* renderer.addVertex(...)
* renderer.end()
*
*/
static VALUE Renderer_Begin( VALUE self, VALUE aType )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->Begin( static_cast< sf::Renderer::PrimitiveType >( FIX2INT( aType ) ) );
return Qnil;
}
/* call-seq:
* renderer.end()
*
* End the current geometry batch and render it.
*/
static VALUE Renderer_End( VALUE self )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
object->End();
return Qnil;
}
/* call-seq:
* renderer.addVertex( x, y )
* renderer.addVertex( x, y, u, v )
* renderer.addVertex( x, y color )
* renderer.addVertex( x, y, u, v, color )
*
* This function adds a new vertex to the current batch.
*/
static VALUE Renderer_AddVertex( int argc, VALUE *args, VALUE self )
{
sf::Renderer *object = NULL;
Data_Get_Struct( self, sf::Renderer, object );
switch( argc )
{
case 2:
{
object->AddVertex( NUM2DBL( args[0] ), NUM2DBL( args[1] ) );
break;
}
case 3:
{
VALUE temp = Color_ForceType( args[2] );
sf::Color color;
color.r = FIX2UINT( Color_GetR( temp ) );
color.g = FIX2UINT( Color_GetG( temp ) );
color.b = FIX2UINT( Color_GetB( temp ) );
color.a = FIX2UINT( Color_GetA( temp ) );
object->AddVertex( NUM2DBL( args[0] ), NUM2DBL( args[1] ), color );
break;
}
case 4:
{
object->AddVertex( NUM2DBL( args[0] ), NUM2DBL( args[1] ), NUM2DBL( args[2] ), NUM2DBL( args[3] ) );
break;
}
case 5:
{
VALUE temp = Color_ForceType( args[4] );
sf::Color color;
color.r = FIX2UINT( Color_GetR( temp ) );
color.g = FIX2UINT( Color_GetG( temp ) );
color.b = FIX2UINT( Color_GetB( temp ) );
color.a = FIX2UINT( Color_GetA( temp ) );
object->AddVertex( NUM2DBL( args[0] ), NUM2DBL( args[1] ), NUM2DBL( args[2] ), NUM2DBL( args[3] ), color );
break;
}
default:
rb_raise( rb_eArgError, "Expected 2..5 arguments but was given %d", argc );
}
return Qnil;
}
static void DefinePrimitiveTypesEnum( void )
{
rb_define_const( globalRendererClass, "TriangleList", INT2FIX( sf::Renderer::TriangleList ) );
rb_define_const( globalRendererClass, "TriangleStrip", INT2FIX( sf::Renderer::TriangleStrip ) );
rb_define_const( globalRendererClass, "TriangleFan", INT2FIX( sf::Renderer::TriangleFan ) );
rb_define_const( globalRendererClass, "QuadList", INT2FIX( sf::Renderer::QuadList ) );
}
void Init_Renderer( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Handles the low-level rendering (states and geometry).
*
* SFML::Renderer is the abstraction layer between SFML code and the low-level drawing API (OpenGL).
*
* It manages render states efficiently, and provides a lightweight abstraction for rendering geometry.
*
* The purpose of this class is to provide a single abstract entry point for everything related to low-level
* rendering. Hiding everything behind SFML::Renderer makes optimizing easy, as well as porting to other technologies
* in the future (like OpenGL ES or OpenGL 3.x).
*
* This class is mainly meant for internal usage, you should never care about it unless you write your own
* SFML::Drawable class that uses raw geometry in its Render function.
*/
globalRendererClass = rb_define_class_under( sfml, "Renderer", rb_cObject );
rb_include_module( globalRendererClass, globalNonCopyableModule );
DefinePrimitiveTypesEnum();
// Instance methods
rb_define_method( globalRendererClass, "saveGLStates", Renderer_SaveGLStates, 0 );
rb_define_method( globalRendererClass, "restoreGLStates", Renderer_RestoreGLStates, 0 );
rb_define_method( globalRendererClass, "clear", Renderer_Clear, 1 );
rb_define_method( globalRendererClass, "pushStates", Renderer_PushStates, 0 );
rb_define_method( globalRendererClass, "popStates", Renderer_PopStates, 0 );
rb_define_method( globalRendererClass, "setColor", Renderer_SetColor, 1 );
rb_define_method( globalRendererClass, "applyColor", Renderer_ApplyColor, 1 );
rb_define_method( globalRendererClass, "setViewport", Renderer_SetViewport, 1 );
rb_define_method( globalRendererClass, "setBlendMode", Renderer_SetBlendMode, 1 );
rb_define_method( globalRendererClass, "setTexture", Renderer_SetTexture, 1 );
rb_define_method( globalRendererClass, "setShader", Renderer_SetShader, 1 );
rb_define_method( globalRendererClass, "begin", Renderer_Begin, 1 );
rb_define_method( globalRendererClass, "end", Renderer_End, 0 );
rb_define_method( globalRendererClass, "addVertex", Renderer_AddVertex, -1 );
// Instance Aliases
rb_define_alias( globalRendererClass, "save_gl_states", "saveGLStates" );
rb_define_alias( globalRendererClass, "restore_gl_states", "restoreGLStates" );
rb_define_alias( globalRendererClass, "push_states", "pushStates" );
rb_define_alias( globalRendererClass, "pop_states", "popStates" );
rb_define_alias( globalRendererClass, "color=", "setColor" );
rb_define_alias( globalRendererClass, "apply_color", "applyColor" );
rb_define_alias( globalRendererClass, "viewport=", "setViewport" );
rb_define_alias( globalRendererClass, "blendMode=", "setBlendMode" );
rb_define_alias( globalRendererClass, "blend_mode=", "setBlendMode" );
rb_define_alias( globalRendererClass, "texture=", "setTexture" );
rb_define_alias( globalRendererClass, "shader=", "setShader" );
rb_define_alias( globalRendererClass, "add_vertex", "addVertex" );
}

View File

@ -1,31 +0,0 @@
/* 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_RENDERER_HEADER_
#define SFML_RUBYEXT_RENDERER_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Renderer( void );
#endif // SFML_RUBYEXT_RENDERER_HEADER_

View File

@ -1,370 +0,0 @@
/* 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 "Shader.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "Image.hpp"
#include "main.hpp"
#include <SFML/Graphics/Shader.hpp>
VALUE globalShaderClass;
/* External classes */
extern VALUE globalVector2Class;
extern VALUE globalVector3Class;
extern VALUE globalImageClass;
static void Shader_Free( sf::Shader *anObject )
{
delete anObject;
}
/* call-seq:
* shader.loadFromFile( filename ) -> true or false
*
* Load the shader from a file.
*
* The source must be a text file containing a valid fragment shader in GLSL language. GLSL is a C-like language
* dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.
*/
static VALUE Shader_LoadFromFile( VALUE self, VALUE aFileName )
{
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
if( object->LoadFromFile( rb_string_value_cstr( &aFileName ) ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* shader.loadFromMemory( filename ) -> true or false
*
* Load the shader from a source code in memory.
*
* The source code must be a valid fragment shader in GLSL language. GLSL is a C-like language dedicated to OpenGL
* shaders; you'll probably need to read a good documentation for it before writing your own shaders.
*/
static VALUE Shader_LoadFromMemory( VALUE self, VALUE aShader )
{
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
if( object->LoadFromMemory( rb_string_value_cstr( &aShader ) ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* shader.setParameter( name, x )
* shader.setParameter( name, x, y )
* shader.setParameter( name, x, y, z )
* shader.setParameter( name, x, y, z, w )
* shader.setParameter( name, vector2 )
* shader.setParameter( name, vector3 )
*
* Change a vector parameter of the shader.
*/
static VALUE Shader_SetParameter( int argc, VALUE *args, VALUE self )
{
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
const char * name;
switch( argc )
{
case 2:
{
name = rb_string_value_cstr( &args[0] );
if( rb_obj_is_kind_of( args[1], rb_cFloat ) == Qtrue )
{
object->SetParameter( name, NUM2DBL( args[1] ) );
}
else if( rb_obj_is_kind_of( args[1], globalVector2Class ) == Qtrue ||
( rb_obj_is_kind_of( args[1], rb_cArray ) == Qtrue &&
FIX2INT( rb_funcall( args[1], rb_intern( "size" ), 0 ) ) == 2 )
)
{
VALUE arg1 = Vector2_ForceType( args[1] );
sf::Vector2f vector;
vector.x = NUM2DBL( Vector2_GetX( args[1] ) );
vector.y = NUM2DBL( Vector2_GetY( args[1] ) );
object->SetParameter( name, vector );
}
else if( rb_obj_is_kind_of( args[1], globalVector3Class ) == Qtrue ||
( rb_obj_is_kind_of( args[1], rb_cArray ) == Qtrue &&
FIX2INT( rb_funcall( args[1], rb_intern( "size" ), 0 ) ) == 3 )
)
{
VALUE arg1 = Vector3_ForceType( args[1] );
sf::Vector3f vector;
vector.x = NUM2DBL( Vector3_GetX( args[1] ) );
vector.y = NUM2DBL( Vector3_GetY( args[1] ) );
vector.z = NUM2DBL( Vector3_GetZ( args[1] ) );
object->SetParameter( name, vector );
}
break;
}
case 3:
name = rb_string_value_cstr( &args[0] );
object->SetParameter( name, NUM2DBL( args[1] ), NUM2DBL( args[2] ) );
break;
case 4:
name = rb_string_value_cstr( &args[0] );
object->SetParameter( name, NUM2DBL( args[1] ), NUM2DBL( args[2] ), NUM2DBL( args[3] ) );
break;
case 5:
name = rb_string_value_cstr( &args[0] );
object->SetParameter( name, NUM2DBL( args[1] ), NUM2DBL( args[2] ), NUM2DBL( args[3] ), NUM2DBL( args[4] ) );
break;
default:
rb_raise( rb_eArgError, "Expected 2..5 arguments but was given %d", argc );
}
return Qnil;
}
/* call-seq:
* shader.setParameter( name, texture )
*
* Change a texture parameter of the shader.
*
* name is the name of the texture to change in the shader. To tell the shader to use the current texture of the object
* being drawn, pass Shader::CurrentTexture. Example:
*
* # These are the variables in the pixel shader
* uniform sampler2D current;
* uniform sampler2D other;
*
* image = SFML::Image.new
* ...
* shader.setParameter( "current", SFML::Shader::CurrentTexture )
* shader.setParameter( "other", image )
*
* It is important to note that texture must remain alive as long as the shader uses it, no copy is made internally.
*/
static VALUE Shader_SetTexture( VALUE self, VALUE aName, VALUE aTexture )
{
VALIDATE_CLASS( aName, rb_cString, "name" );
VALIDATE_CLASS( aTexture, globalImageClass, "texture" );
sf::Image *texture = NULL;
Data_Get_Struct( self, sf::Image, texture );
const char * name = rb_string_value_cstr( &aName );
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
object->SetTexture( name, *texture );
return Qnil;
}
/* call-seq:
* shader.bind()
*
* Bind the shader for rendering (activate it).
*
* This function is normally for internal use only, unless you want to use the shader with a custom OpenGL rendering
* instead of a SFML drawable.
*/
static VALUE Shader_Bind( VALUE self )
{
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
object->Bind();
return Qnil;
}
/* call-seq:
* shader.unbind()
*
* Unbind the shader (deactivate it).
*
* This function is normally for internal use only, unless you want to use the shader with a custom OpenGL rendering
* instead of a SFML drawable.
*/
static VALUE Shader_Unbind( VALUE self )
{
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
object->Unbind();
return Qnil;
}
/* call-seq:
* Shader.new() -> shader
* Shader.new( filename ) -> shader
*
* Will create a new shader instance.
*
* If a filename argument is specified then shader#loadFromFile will be called on the created instance.
*/
static VALUE Shader_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 0 )
{
rb_funcall2( self, rb_intern( "loadFromFile" ), argc, args );
}
return self;
}
static VALUE Shader_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Shader *object = NULL;
Data_Get_Struct( self, sf::Shader, object );
sf::Shader *source = NULL;
Data_Get_Struct( aSource, sf::Shader, source );
*object = *source;
}
/* call-seq:
* Shader.new()
*
* Create a new shader.
*/
static VALUE Shader_Alloc( VALUE aKlass )
{
sf::Shader *object = new sf::Shader();
return Data_Wrap_Struct( aKlass, 0, Shader_Free, object );
}
/* call-seq:
* Shader.isAvailable()
*
* Tell whether or not the system supports shaders.
*
* This function should always be called before using the shader features. If it returns false, then any attempt to
* use SFML::Shader will fail.
*/
static VALUE Shader_IsAvailable( VALUE aKlass )
{
return ( sf::Shader::IsAvailable() == true ? Qtrue : Qfalse );
}
static VALUE CreateCurrentTextureWrapper( void )
{
sf::Image * image = const_cast< sf::Image * >( &sf::Shader::CurrentTexture );
VALUE rbData = Data_Wrap_Struct( globalImageClass, 0, 0, image );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
void Init_Shader( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Pixel/fragment shader class.
*
* Pixel shaders (or fragment shaders) are programs written using a specific language, executed directly by the
* graphics card and allowing to apply per-pixel real-time operations to the rendered entities.
*
* Pixel shaders are written in GLSL, which is a C-like language dedicated to OpenGL shaders. You'll probably need
* to learn its basics before writing your own shaders for SFML.
*
* Like any program, a shader has its own variables that you can set from your Ruby application. SFML::Shader
* handles 3 different types of variables:
*
* - floats
* - vectors (2, 3 or 4 components)
* - textures
*
* The value of the variables can be changed at any time with either SFML::Shader#setParameter or SFML::Shader#SetTexture:
*
* shader.setParameter( "offset", 2.0 )
* shader.setParameter( "color", 0.5, 0.8, 0.3 )
* shader.setTexture( "image", image ) # image is a SFML::Image
* shader.setTexture( "current", SFML::Shader::CurrentTexture )
*
* Shader::CurrentTexture is a special value that represents the texture that the object being drawn is using.
*
* To apply a shader to a drawable, you must pass it as an additional parameter to the Draw function:
*
* window.draw( sprite, shader )
*
* Shaders can be used on any drawable, but they are mainly made for SFML::Sprite. Using a shader on a SFML::String is
* more limited, because the texture of the string is not the actual text that you see on screen, it is a big image
* containing all the characters of the font in an arbitrary order. Thus, texture lookups on pixels other than the
* current one may not give you the expected result. Using a shader with SFML::Shape is even more limited, as shapes
* don't use any texture.
*
* Shaders can also be used to apply global post-effects to the current contents of the target
* (like the old sf::PostFx class in SFML 1). This can be done in two different ways:
*
* - draw everything to a SFML::RenderImage, then draw it to the main target using the shader
* - draw everything directly to the main target, then use SFML::Image::CopyScreen to copy its contents to an image
* and draw it to the main target using the shader
*
* The first technique is more optimized because it doesn't involve retrieving the target's pixels to system memory,
* but the second one doesn't impact the rendering process and can be easily inserted anywhere.
*
* Like SFML::Image that can be used as a raw OpenGL texture, SFML::Shader can also be used directly as a raw fragment
* shader for custom OpenGL geometry.
*
* window.setActive()
* shader.bind()
* # ... render OpenGL geometry ...
* shader.unbind()
*
*/
globalShaderClass = rb_define_class_under( sfml, "Shader", rb_cObject );
// Class methods
//rb_define_singleton_method( globalShaderClass, "new", Shader_New, -1 );
rb_define_alloc_func( globalShaderClass, Shader_Alloc );
rb_define_singleton_method( globalShaderClass, "isAvailable", Shader_IsAvailable, 0 );
// Class Constants
rb_define_const( globalShaderClass, "CurrentTexture", CreateCurrentTextureWrapper() );
// Instance methods
rb_define_method( globalShaderClass, "initialize", Shader_Initialize, -1 );
rb_define_method( globalShaderClass, "initialize_copy", Shader_InitializeCopy, 1 );
rb_define_method( globalShaderClass, "loadFromFile", Shader_LoadFromFile, 1 );
rb_define_method( globalShaderClass, "loadFromMemory", Shader_LoadFromMemory, 1 );
rb_define_method( globalShaderClass, "setParameter", Shader_SetParameter, -1 );
rb_define_method( globalShaderClass, "setTexture", Shader_SetTexture, 2 );
rb_define_method( globalShaderClass, "bind", Shader_Bind, 0 );
rb_define_method( globalShaderClass, "unbind", Shader_Unbind, 0 );
// Class Aliases
rb_define_alias( CLASS_OF( globalShaderClass ), "is_available", "isAvailable" );
rb_define_alias( CLASS_OF( globalShaderClass ), "available?", "isAvailable" );
// Instance Aliases
rb_define_alias( globalShaderClass, "load_from_file", "loadFromFile" );
rb_define_alias( globalShaderClass, "loadFile", "loadFromFile" );
rb_define_alias( globalShaderClass, "load_file", "loadFromFile" );
rb_define_alias( globalShaderClass, "load_from_memory", "loadFromMemory" );
rb_define_alias( globalShaderClass, "loadMemory", "loadFromMemory" );
rb_define_alias( globalShaderClass, "load_memory", "loadFromMemory" );
rb_define_alias( globalShaderClass, "set_parameter", "setParameter" );
rb_define_alias( globalShaderClass, "[]=", "setParameter" );
rb_define_alias( globalShaderClass, "set_texture", "setTexture" );
}

View File

@ -1,31 +0,0 @@
/* 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_SHADER_HEADER_
#define SFML_RUBYEXT_SHADER_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Shader( void );
#endif // SFML_RUBYEXT_SHADER_HEADER_

View File

@ -1,686 +0,0 @@
/* 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 <SFML/Graphics/Shape.hpp>
VALUE globalShapeClass;
/* External classes */
extern VALUE globalVector2Class;
extern VALUE globalDrawableModule;
extern VALUE globalColorClass;
static void Shape_Free( sf::Shape *anObject )
{
delete anObject;
}
/* call-seq:
* shape.addPoint( x, y, color, outlineColor )
* shape.addPoint( position, color, outlineColor )
*
* Add a new point to the shape.
*
* The new point is inserted at the end of the shape.
*/
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;
}
/* call-seq:
* shape.getPointsCount() -> fixnum
*
* Get the number of points composing the shape.
*/
static VALUE Shape_GetPointsCount( VALUE self )
{
sf::Shape *object = NULL;
Data_Get_Struct( self, sf::Shape, object );
return INT2FIX( object->GetPointsCount() );
}
/* call-seq:
* shape.enableFill( enable )
*
* Enable or disable the shape's filling.
*
* This option is enabled by default.
*/
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;
}
/* call-seq:
* shape.enableOutline( enable )
*
* Enable or disable the shape's outline.
*
* This option is enabled by default.
*/
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;
}
/* call-seq:
* shape.setPointPosition( index, position )
* shape.setPointPosition( index, x, y )
*
* Change the position of a point.
*
* Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range
* [0, GetPointscount() - 1]) the behaviour is undefined.
*/
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;
}
/* call-seq:
* shape.setPointColor( index, color )
*
* Change the color of a point.
*
* Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range
* [0, GetPointscount() - 1]) the behaviour is undefined.
*/
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;
}
/* call-seq:
* shape.setPointOutlineColor( index, color )
*
* Change the outline color of a point.
*
* Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range
* [0, GetPointscount() - 1]) the behaviour is undefined.
*/
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;
}
/* call-seq:
* shape.setOutlineThickness( width )
*
* Change the thickness of the shape outline.
*/
static VALUE Shape_SetOutlineThickness( VALUE self, VALUE aWidth )
{
sf::Shape *object = NULL;
Data_Get_Struct( self, sf::Shape, object );
object->SetOutlineThickness( NUM2DBL( aWidth ) );
return Qnil;
}
/* call-seq:
* shape.getPointPosition( index ) -> vector2
*
* Get the position of a point.
*
* Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range
* [0, GetPointscount() - 1]) the behaviour is undefined.
*/
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 ) );
}
/* call-seq:
* shape.getPointColor( index ) -> color
*
* Get the color of a point.
*
* Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range
* [0, GetPointscount() - 1]) the behaviour is undefined.
*/
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 ) );
}
/* call-seq:
* shape.getPointOutlineColor( index ) -> color
*
* Get the outline color of a point.
*
* Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range
* [0, GetPointscount() - 1]) the behaviour is undefined.
*/
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 ) );
}
/* call-seq:
* shape.getOutlineThickness() -> float
*
* Get the thickness of the shape outline.
*/
static VALUE Shape_GetOutlineThickness( VALUE self )
{
sf::Shape *object = NULL;
Data_Get_Struct( self, sf::Shape, object );
return rb_float_new( object->GetOutlineThickness() );
}
static VALUE Shape_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Shape *object = NULL;
Data_Get_Struct( self, sf::Shape, object );
sf::Shape *source = NULL;
Data_Get_Struct( aSource, sf::Shape, source );
*object = *source;
}
/* call-seq:
* Shape.new() -> shape
*
* Create an empty shape.
*/
static VALUE Shape_Alloc( VALUE aKlass )
{
sf::Shape *object = new sf::Shape();
return Data_Wrap_Struct( aKlass, 0, Shape_Free, object );
}
/* call-seq:
* Shape.line( p1x, p1y, p2x, p2y, thickness, color, outline = 0.0, outlineColor = SFML::Color::Black) -> shape
* Shape.line( start, end, thickness, color, outline = 0.0, outlineColor = SFML::Color::Black) -> shape
*
* Create a new line.
*/
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;
}
/* call-seq:
* Shape.rectangle( left, top, width, height, color, outline = 0.0, outlineColor = SFML::Color::Black) -> shape
* Shape.rectangle( rectangle, color, outline = 0.0, outlineColor = SFML::Color::Black) -> shape
*
* Create a new rectangular shape.
*/
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;
}
/* call-seq:
* Shape.circle( x, y, radius, color, outline = 0.0, outlineColor = SFML::Color::Black) -> shape
* Shape.circle( center, radius, color, outline = 0.0, outlineColor = SFML::Color::Black) -> shape
*
* Create a new circular shape.
*/
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_alloc_func( globalShapeClass, Shape_Alloc );
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, "initialize_copy", Shape_InitializeCopy, 1 );
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, "setOutlineThickness", Shape_SetOutlineThickness, 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, "getOutlineThickness", Shape_GetOutlineThickness, 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, "outlineThickness=", "setOutlineThickness" );
rb_define_alias( globalShapeClass, "outline_thickness=", "setOutlineThickness" );
rb_define_alias( globalShapeClass, "outlineThickness", "getOutlineThickness" );
rb_define_alias( globalShapeClass, "outline_thickness", "getOutlineThickness" );
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" );
}

View File

@ -1,31 +0,0 @@
/* 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_

View File

@ -1,402 +0,0 @@
/* 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;
}
/* call-seq:
* Sprite.new() -> sprite
* Sprite.new( image, position = [0, 0], scale = [1, 1], rotation = 0.0, color = SFML::Color::White ) -> sprite
*
* Construct the sprite from a source image.
*/
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_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
sf::Sprite *source = NULL;
Data_Get_Struct( aSource, sf::Sprite, source );
*object = *source;
}
/* call-seq:
* sprite.setImage( image, adjustToNewSize = false)
*
* Change the source image of the sprite.
*
* The image argument refers to an image that must exist as long as the sprite uses it. Indeed, the sprite doesn't
* store its own copy of the image, but rather keeps a pointer to the one that you passed to this function. If the
* source image is destroyed and the sprite tries to use it, it may appear as a white rectangle. If adjustToNewSize is
* true, the SubRect property of the sprite is adjusted to the size of the new image. If it is false, the SubRect
* is unchanged.
*/
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 );
rb_iv_set( self, "@__image_ref", Qnil );
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;
}
/* call-seq:
* sprite.setSubRect( rectangle )
*
* Set the part of the image that the sprite will display.
*
* The sub-rectangle is useful when you don't want to display the whole image, but rather a part of it. By default,
* the sub-rectangle covers the entire image.
*/
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 ) );
sf::Sprite *object = NULL;
Data_Get_Struct( self, sf::Sprite, object );
object->SetSubRect( rectangle );
return Qnil;
}
/* call-seq:
* sprite.resize( width, height )
* sprite.resize( vector2 )
*
* Change the size of the sprite.
*
* This function is just a shortcut that calls SetScale with the proper values, calculated from the size of the current
* subrect. If width or height is not strictly positive, this functions does nothing.
*/
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;
}
/* call-seq:
* sprite.flipX( flipped )
*
* Flip the sprite horizontally.
*/
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;
}
/* call-seq:
* sprite.flipY( flipped )
*
* Flip the sprite vertically.
*/
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;
}
/* call-seq:
* sprite.getImage() -> image or nil
*
* Get the source image of the sprite.
*
* If the sprite has no source image, or if the image doesn't exist anymore, nil is returned.
*/
static VALUE Sprite_GetImage( VALUE self )
{
return rb_iv_get( self, "@__image_ref" );
}
/* call-seq:
* sprite.getSubRect() -> rectangle
*
* Get the region of the image displayed by the sprite.
*/
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 ) );
}
/* call-seq:
* sprite.getSize() -> vector2
*
* Get the global size of the sprite.
*
* This function is a shortcut that multiplies the size of the subrect by the scale factors.
*/
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 ) );
}
/* call-seq:
* sprite.getPixel( x, y ) -> color
*
* Get the color of a given pixel in the sprite.
*
* This function returns the source image pixel, multiplied by the global color of the sprite. The input point must
* be in local coordinates. If you have a global point, you can use the TransformToLocal function to make it local.
* This function doesn't perform any check, you must ensure that the x and y coordinates are not out of bounds.
*/
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_Alloc( VALUE aKlass )
{
sf::Sprite *object = new sf::Sprite();
return Data_Wrap_Struct( aKlass, 0, Sprite_Free, object );
}
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 );
rb_define_alloc_func( globalSpriteClass, Sprite_Alloc );
// Instance methods
rb_define_method( globalSpriteClass, "initialize", Sprite_Initialize, -1 );
rb_define_method( globalSpriteClass, "initialize_copy", Sprite_InitializeCopy, 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

@ -1,31 +0,0 @@
/* 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

@ -1,329 +0,0 @@
/* 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;
}
/* call-seq:
* Text.new() -> text
* Text.new( string, font = SFML::Font::DefaultFont, characterSize = 30 ) -> text
*
* Create a text instance
*/
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] );
object->SetCharacterSize( characterSize );
case 2:
VALIDATE_CLASS( args[1], globalFontClass, "font" );
Data_Get_Struct( args[1], sf::Font, font );
rb_iv_set( self, "@__font_ref", args[1] );
object->SetFont( *font );
case 1:
string = rb_string_value_cstr( &args[0] );
object->SetString( string );
case 0:
break;
default:
rb_raise( rb_eArgError, "Expected 0..3 arguments but was given %d", argc );
}
return self;
}
static VALUE Text_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
sf::Text *source = NULL;
Data_Get_Struct( aSource, sf::Text, source );
*object = *source;
}
/* call-seq:
* text.setString( string )
*
* Set the text's string.
*/
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;
}
/* call-seq:
* text.setFont( font )
*
* Set the text's font.
*
* Texts have a valid font by default, which the built-in SFML::Font::DefaultFont.
*/
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;
}
/* call-seq:
* text.setCharacterSize( size )
*
* Set the character size.
*
* The default size is 30.
*/
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;
}
/* call-seq:
* text.setStyle( style )
*
* Set the text's style.
*
* You can pass a combination of one or more styles, for example sf::Text::Bold | sf::Text::Italic. The default style
* is sf::Text::Regular.
*/
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;
}
/* call-seq:
* text.getString() -> string
*
* Get the text's string.
*/
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() );
}
/* call-seq:
* text.getFont() -> font
*
* Get the text's font.
*/
static VALUE Text_GetFont( VALUE self )
{
return rb_iv_get( self, "@__font_ref" );
}
/* call-seq:
* text.getCharacterSize() -> fixnum
*
* Get the character size
*/
static VALUE Text_GetCharacterSize( VALUE self )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
return INT2FIX( object->GetCharacterSize() );
}
/* call-seq:
* text.getStyle() -> fixnum
*
* Get the text's style.
*/
static VALUE Text_GetStyle( VALUE self )
{
sf::Text *object = NULL;
Data_Get_Struct( self, sf::Text, object );
return INT2FIX( object->GetStyle() );
}
/* call-seq:
* text.getCharacterPos( index ) -> vector2
*
* Return the position of the index-th character.
*
* This function computes the visual position of a character from its index in the string. The returned position is in
* local coordinates (translation, rotation, scale and origin are not applied). You can easily get the corresponding
* global position with the TransformToGlobal function. If index is out of range, the position of the end of the
* string is returned.
*/
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 ) );
}
/* call-seq:
* text.getRect() -> rectangle
*
* Get the bounding rectangle of the text.
*
* The returned rectangle is in global coordinates.
*/
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( globalRectClass, 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_Alloc( VALUE aKlass )
{
sf::Text *object = new sf::Text();
return Data_Wrap_Struct( aKlass, 0, Text_Free, object );
}
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" );
/* Graphical text that can be drawn to a render target.
*
* SFML::Text is a drawable class that allows to easily display some text with custom style and color on a render target.
*
* It inherits all the functions from SFML::Drawable: position, rotation, scale, origin, global color and blend mode.
* It also adds text-specific properties such as the font to use, the character size, the font style (bold, italic,
* underlined), and the text to display of course. It also provides convenience functions to calculate the graphical
* size of the text, or to get the visual position of a given character.
*
* SFML::Text works in combination with the sf::Font class, which loads and provides the glyphs (visual characters) of
* a given font.
*
* The separation of SFML::Font and SFML::Text allows more flexibility and better performances: indeed a SFML::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.
*
* It is important to note that the SFML::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).
*
* NOTE: This is the ruby bindings so the fonts will be managed by the ruby garbage collector and thus the font 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 a font
* font = SFML::Font.new
* font.loadFromFile( "arial.ttf" )
*
* # Create a text
* text SFML::Text.new( "hello" )
* text.setFont( font )
* text.SetCharacterSize( 30 )
* text.setStyle( SFML::Text::Regular )
*
* # Display it
* window.draw( text ) # 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 );
rb_define_alloc_func( globalTextClass, Text_Alloc );
// Instance methods
rb_define_method( globalTextClass, "initialize", Text_Initialize, -1 );
rb_define_method( globalTextClass, "initialize_copy", Text_InitializeCopy, 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

@ -1,31 +0,0 @@
/* 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

@ -1,440 +0,0 @@
/* 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 "View.hpp"
#include "Vector2.hpp"
#include "Rect.hpp"
#include "main.hpp"
#include <SFML/Graphics/View.hpp>
VALUE globalViewClass;
/* External classes */
extern VALUE globalRectClass;
extern VALUE globalVector2Class;
static void View_Free( sf::View *anObject )
{
delete anObject;
}
/* call-seq:
* View.new() -> view
* View.new( rectangle ) -> view
* View.new( center, size ) -> view
*
* Construct a view.
*/
static VALUE View_Initialize( int argc, VALUE *args, VALUE self )
{
VALUE temp = Qnil;
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
switch( argc )
{
case 2:
{
temp = Vector2_ForceType( args[0] );
sf::Vector2f center;
center.x = NUM2DBL( Vector2_GetX( temp ) );
center.y = NUM2DBL( Vector2_GetY( temp ) );
temp = Vector2_ForceType( args[1] );
sf::Vector2f size;
size.x = NUM2DBL( Vector2_GetX( temp ) );
size.y = NUM2DBL( Vector2_GetY( temp ) );
object->SetCenter( center );
object->SetSize( size );
break;
}
case 1:
{
temp = Rect_ForceType( args[0] );
sf::FloatRect rectangle;
rectangle.Left = NUM2DBL( Rect_GetLeft( temp ) );
rectangle.Top = NUM2DBL( Rect_GetTop( temp ) );
rectangle.Width = NUM2DBL( Rect_GetWidth( temp ) );
rectangle.Height = NUM2DBL( Rect_GetHeight( temp ) );
object->Reset( rectangle );
break;
}
case 0:
// Do nothing
break;
default:
rb_raise( rb_eArgError, "Expected 0..2 arguments but was given %d", argc );
}
return self;
}
static VALUE View_InitializeCopy( VALUE self, VALUE aSource )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
sf::View *source = NULL;
Data_Get_Struct( aSource, sf::View, source );
*object = *source;
}
/* call-seq:
* view.getCenter() -> vector2
*
* Get the center of the view.
*/
static VALUE View_GetCenter( VALUE self )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
const sf::Vector2f& center = object->GetCenter();
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, rb_float_new( center.x ), rb_float_new( center.y ) );
}
/* call-seq:
* view.getRotation() -> float
*
* Get the current orientation of the view.
*/
static VALUE View_GetRotation( VALUE self )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
return rb_float_new( object->GetRotation() );
}
/* call-seq:
* view.getSize() -> vector2
*
* Get the size of the view.
*/
static VALUE View_GetSize( VALUE self )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, 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 ) );
}
/* call-seq:
* view.getViewport() -> rectangle
*
* Get the target viewport rectangle of the view.
*/
static VALUE View_GetViewport( VALUE self )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
const sf::FloatRect& viewport = object->GetViewport();
return rb_funcall( globalRectClass, rb_intern( "new" ), 4,
rb_float_new( viewport.Left ), rb_float_new( viewport.Top ),
rb_float_new( viewport.Width ), rb_float_new( viewport.Height ) );
}
/* call-seq:
* view.move( x, y )
* view.move( offset )
*
* Move the view relatively to its current position.
*/
static VALUE View_Move( int argc, VALUE * args, VALUE self )
{
float offsetX = 0;
float offsetY = 0;
switch( argc )
{
case 1:
{
VALUE temp = Vector2_ForceType( args[0] );
offsetX = NUM2DBL( Vector2_GetX( temp ) );
offsetY = NUM2DBL( Vector2_GetY( temp ) );
break;
}
case 2:
{
offsetX = NUM2DBL( args[0] );
offsetY = NUM2DBL( args[1] );
break;
}
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
object->Move( offsetX, offsetY );
return Qnil;
}
/* call-seq:
* view.reset( rectangle )
*
* Reset the view to the given rectangle.
*
* Note that this function resets the rotation angle to 0.
*/
static VALUE View_Reset( VALUE self, VALUE aRectangle )
{
VALUE temp = Rect_ForceType( aRectangle );
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
sf::FloatRect rectangle;
rectangle.Left = NUM2DBL( Rect_GetLeft( temp ) );
rectangle.Top = NUM2DBL( Rect_GetTop( temp ) );
rectangle.Width = NUM2DBL( Rect_GetWidth( temp ) );
rectangle.Height = NUM2DBL( Rect_GetHeight( temp ) );
object->Reset( rectangle );
return Qnil;
}
/* call-seq:
* view.rotate( angle )
*
* Rotate the view relatively to its current orientation.
*/
static VALUE View_Rotate( VALUE self, VALUE anAngle )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
object->Rotate( NUM2DBL( anAngle ) );
return Qnil;
}
/* call-seq:
* view.setCenter( center )
* view.setCenter( x, y )
*
* Set the center of the view.
*/
static VALUE View_SetCenter( int argc, VALUE * args, VALUE self )
{
float x = 0;
float y = 0;
switch( argc )
{
case 1:
{
VALUE temp = Vector2_ForceType( args[0] );
x = NUM2DBL( Vector2_GetX( temp ) );
y = NUM2DBL( Vector2_GetY( temp ) );
break;
}
case 2:
{
x = NUM2DBL( args[0] );
y = NUM2DBL( args[1] );
break;
}
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
object->SetCenter( x, y );
return Qnil;
}
/* call-seq:
* view.setRotation( angle )
*
* Set the orientation of the view.
*
* The default rotation of a view is 0 degree.
*/
static VALUE View_SetRotation( VALUE self, VALUE anAngle )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
object->SetRotation( NUM2DBL( anAngle ) );
return Qnil;
}
/* call-seq:
* view.setSize( size )
* view.setSize( width, height )
*
* Set the center of the view.
*/
static VALUE View_SetSize( int argc, VALUE * args, VALUE self )
{
float x = 0;
float y = 0;
switch( argc )
{
case 1:
{
VALUE temp = Vector2_ForceType( args[0] );
x = NUM2DBL( Vector2_GetX( temp ) );
y = NUM2DBL( Vector2_GetY( temp ) );
break;
}
case 2:
{
x = NUM2DBL( args[0] );
y = NUM2DBL( args[1] );
break;
}
default:
rb_raise( rb_eArgError, "Expected 1 or 2 arguments but was given %d", argc );
}
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
object->SetSize( x, y );
return Qnil;
}
/* call-seq:
* view.setViewport( rectangle )
*
* Set the target viewport.
*
* The viewport is the rectangle into which the contents of the view are displayed, expressed as a factor
* (between 0 and 1) of the size of the RenderTarget to which the view is applied. For example, a view which takes the
* left side of the target would be defined with SFML::View.setViewport( [0.0, 0.0, 0.5, 1.0] ). By default, a view has
* a viewport which covers the entire target.
*/
static VALUE View_SetViewport( VALUE self, VALUE aRectangle )
{
VALUE temp = Rect_ForceType( aRectangle );
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
sf::FloatRect viewport;
viewport.Left = NUM2DBL( Rect_GetLeft( temp ) );
viewport.Top = NUM2DBL( Rect_GetTop( temp ) );
viewport.Width = NUM2DBL( Rect_GetWidth( temp ) );
viewport.Height = NUM2DBL( Rect_GetHeight( temp ) );
object->SetViewport( viewport );
return Qnil;
}
/* call-seq:
* view.zoom( factor )
*
* Resize the view rectangle relatively to its current size.
*
* Resizing the view simulates a zoom, as the zone displayed on screen grows or shrinks. factor is a multiplier:
*
* - 1 keeps the size unchanged
* - > 1 makes the view bigger (objects appear smaller)
* - < 1 makes the view smaller (objects appear bigger)
*
*/
static VALUE View_Zoom( VALUE self, VALUE aFactor )
{
sf::View *object = NULL;
Data_Get_Struct( self, sf::View, object );
object->Zoom( NUM2DBL( aFactor ) );
return Qnil;
}
static VALUE View_Alloc( VALUE aKlass )
{
sf::View *object = new sf::View();
return Data_Wrap_Struct( aKlass, 0, View_Free, object );
}
void Init_View( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* 2D camera that defines what region is shown on screen
*
* sf::View defines a camera in the 2D scene.
*
* This is a very powerful concept: you can scroll, rotate or zoom the entire scene without altering the way that your
* drawable objects are drawn.
*
* A view is composed of a source rectangle, which defines what part of the 2D scene is shown, and a target viewport,
* which defines where the contents of the source rectangle will be displayed on the render target
* (window or render-image).
*
* The viewport allows to map the scene to a custom part of the render target, and can be used for split-screen or
* for displaying a minimap, for example. If the source rectangle has not the same size as the viewport, its contents
* will be stretched to fit in.
*
* To apply a view, you have to assign it to the render target. Then, every objects drawn in this render target will
* be affected by the view until you use another view.
*
* Usage example:
*
* window = SFML::RenderWindow.new
* view = SFML::View.new
*
* # Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
* view.reset( [100.0, 100.0, 400.0, 200.0] )
*
* # Rotate it by 45 degrees
* view.rotate( 45 )
*
* # Set its target viewport to be half of the window
* view.viewport = [0.0, 0.0, 0.5, 1.0 ]
*
* # Apply it
* window.view = view
*
* # Render stuff
* window.draw( someSprite )
*
* # Set the default view back
* window.view = window.defaultView
*
* # Render stuff not affected by the view
* window.draw( someText )
*/
globalViewClass = rb_define_class_under( sfml, "View", rb_cObject );
// Class methods
//rb_define_singleton_method( globalViewClass, "new", View_New, -1 );
rb_define_alloc_func( globalViewClass, View_Alloc );
// Instance methods
rb_define_method( globalViewClass, "initialize", View_Initialize, -1 );
rb_define_method( globalViewClass, "initialize_copy", View_InitializeCopy, 1 );
rb_define_method( globalViewClass, "setCenter", View_SetCenter, -1 );
rb_define_method( globalViewClass, "setSize", View_SetSize, -1 );
rb_define_method( globalViewClass, "setRotation", View_SetRotation, 1 );
rb_define_method( globalViewClass, "setViewport", View_SetViewport, 1 );
rb_define_method( globalViewClass, "reset", View_Reset, 1 );
rb_define_method( globalViewClass, "getCenter", View_GetCenter, 0 );
rb_define_method( globalViewClass, "getSize", View_GetSize, 0 );
rb_define_method( globalViewClass, "getRotation", View_GetRotation, 0 );
rb_define_method( globalViewClass, "getViewport", View_GetViewport, 0 );
rb_define_method( globalViewClass, "move", View_Move, -1 );
rb_define_method( globalViewClass, "rotate", View_Rotate, 1 );
rb_define_method( globalViewClass, "zoom", View_Zoom, 1 );
// Instance Aliases
rb_define_alias( globalViewClass, "center=", "setCenter" );
rb_define_alias( globalViewClass, "center", "getCenter" );
rb_define_alias( globalViewClass, "size=", "setSize" );
rb_define_alias( globalViewClass, "size", "getSize" );
rb_define_alias( globalViewClass, "rotation=", "setRotation" );
rb_define_alias( globalViewClass, "rotation", "getRotation" );
rb_define_alias( globalViewClass, "viewport=", "setViewport" );
rb_define_alias( globalViewClass, "viewport", "getViewport" );
}

View File

@ -1,31 +0,0 @@
/* 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_VIEW_HEADER_
#define SFML_RUBYEXT_VIEW_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_View( void );
#endif // SFML_RUBYEXT_VIEW_HEADER_

View File

@ -1,101 +0,0 @@
/* 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 "main.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "NonCopyable.hpp"
#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 "Sprite.hpp"
#include "Text.hpp"
#include "View.hpp"
#include <SFML/Graphics.hpp>
VALUE globalBlendNamespace;
/* External classes */
VALUE globalWindowClass;
static bool CheckDependencies( void )
{
if( rb_cvar_defined( globalSFMLNamespace, rb_intern( "WindowLoaded" ) ) == Qtrue )
{
return true;
}
return false;
}
/* Available blending modes for drawable objects. */
static void CreateBlendEnum( void )
{
globalBlendNamespace = rb_define_module_under( globalSFMLNamespace, "Blend" );
rb_define_const( globalBlendNamespace, "Alpha", INT2FIX( sf::Blend::Alpha ) );
rb_define_const( globalBlendNamespace, "Add", INT2FIX( sf::Blend::Add ) );
rb_define_const( globalBlendNamespace, "Multiply", INT2FIX( sf::Blend::Multiply ) );
rb_define_const( globalBlendNamespace, "None", INT2FIX( sf::Blend::None ) );
}
void Init_graphics( void )
{
/* SFML namespace which contains the classes of this module. */
globalSFMLNamespace = rb_define_module( "SFML" );
if( CheckDependencies() == false )
{
rb_raise( rb_eRuntimeError, "This module depends on sfml-window" );
}
globalVector2Class = RetrieveSFMLClass( "Vector2" );
globalVector3Class = RetrieveSFMLClass( "Vector3" );
globalWindowClass = RetrieveSFMLClass( "Window" );
globalNonCopyableModule = RetrieveSFMLClass( "NonCopyable" );
rb_define_const(globalSFMLNamespace, "GraphicsLoaded", Qtrue);
CreateBlendEnum();
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_Sprite();
Init_Text();
Init_View();
}

View File

@ -1,32 +0,0 @@
/* 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_GRAPHICS_MAIN_HEADER_
#define SFML_RUBYEXT_GRAPHICS_MAIN_HEADER_
#include "ruby.h"
#include "global.hpp"
// Ruby initiation function
extern "C" void Init_graphics( void );
#endif // SFML_RUBYEXT_GRAPHICS_MAIN_HEADER_

View File

@ -1,26 +0,0 @@
# 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.
require 'mkmf'
dir_config("system")
have_library("sfml-system-s")
create_makefile("sfml/system", "system")

View File

@ -1,108 +0,0 @@
/* 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 "Clock.hpp"
#include "main.hpp"
#include <SFML/System/Clock.hpp>
VALUE globalClockClass;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void Clock_Free( sf::Clock *anObject )
{
delete anObject;
}
/* call-seq:
* clock.getElapsedTime() -> Float
*
* This function returns the time elapsed since the last call to Reset()
* (or the construction of the instance if Reset() has not been called) in seconds.
*/
static VALUE Clock_GetElapsedTime( VALUE self )
{
sf::Clock *object = NULL;
Data_Get_Struct( self, sf::Clock, object );
return rb_float_new( object->GetElapsedTime() );
}
/* call-seq:
* clock.reset() -> nil
*
* This function puts the time counter back to zero.
*/
static VALUE Clock_Reset( VALUE self )
{
sf::Clock *object = NULL;
Data_Get_Struct( self, sf::Clock, object );
object->Reset();
return Qnil;
}
static VALUE Clock_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Clock *object = NULL;
Data_Get_Struct( self, sf::Clock, object );
sf::Clock *source = NULL;
Data_Get_Struct( aSource, sf::Clock, source );
*object = *source ;
return self;
}
/* call-seq:
* Clock.new() -> clock
*
* The clock starts automatically after being constructed.
*/
static VALUE Clock_Allocate( VALUE aKlass )
{
sf::Clock *object = new sf::Clock();
return Data_Wrap_Struct( aKlass, 0, Clock_Free, object );
}
void Init_Clock( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Utility class for manipulating time.
*
* sf::Clock is a lightweight class for measuring time.
*
* Its resolution depends on the underlying OS, but you can generally expect a 1 ms resolution.
*/
globalClockClass = rb_define_class_under( sfml, "Clock", rb_cObject );
rb_define_alloc_func( globalClockClass, Clock_Allocate );
// Class methods
//rb_define_singleton_method( globalClockClass, "new", Clock_New, -1 );
// Instance methods
rb_define_method( globalClockClass, "initialize_copy", Clock_InitializeCopy, 1 );
rb_define_method( globalClockClass, "elapsed_time", Clock_GetElapsedTime, 0 );
rb_define_method( globalClockClass, "reset", Clock_Reset, 0 );
// Aliases
rb_define_alias( globalClockClass, "elapsedTime", "elapsed_time" );
rb_define_alias( globalClockClass, "getElapsedTime", "elapsed_time" );
}

View File

@ -1,31 +0,0 @@
/* 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_CLOCK_HEADER_
#define SFML_RUBYEXT_CLOCK_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Clock( void );
#endif // SFML_RUBYEXT_CLOCK_HEADER_

View File

@ -1,41 +0,0 @@
/* 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 "main.hpp"
#include "global.hpp"
#include "Clock.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "NonCopyable.hpp"
void Init_system( void )
{
/* SFML namespace which contains the classes of this module. */
globalSFMLNamespace = rb_define_module( "SFML" );
rb_define_const(globalSFMLNamespace, "SystemLoaded", Qtrue);
rb_define_const(globalSFMLNamespace, "Version", rb_str_new2(LIB_VERSION));
rb_define_const(globalSFMLNamespace, "BindingVersion", rb_str_new2(BINDING_VERSION));
Init_Clock();
Init_Vector2();
Init_Vector3();
Init_NonCopyable();
}

View File

@ -1,53 +0,0 @@
/* 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_SYSTEM_MAIN_HEADER_
#define SFML_RUBYEXT_SYSTEM_MAIN_HEADER_
#include "ruby.h"
#define SFML_STATIC
VALUE RetrieveSFMLClass( const char * aName );
// Ruby initiation function
extern "C" void Init_system( void );
typedef VALUE ( *RubyFunctionPtr )( ... );
#define BINDING_VERSION "development"
#define LIB_VERSION "2.0"
#define MAX( x, y ) ( ( x ) < ( y ) ? ( y ) : ( x ) )
#define MIN( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
#define VALIDATE_CLASS( variable, type, name ) \
if( rb_obj_is_kind_of( variable, type ) != Qtrue ) \
{ \
rb_raise( rb_eTypeError, "%s argument must be instance of %s", name, rb_string_value_cstr ( &type ) ); \
}
#define rb_define_module_function( klass, name, func, argc, ... ) rb_define_module_function( klass, name, reinterpret_cast< RubyFunctionPtr >( func ), argc, ##__VA_ARGS__ )
#define rb_define_singleton_method( klass, name, func, argc, ... ) rb_define_singleton_method( klass, name, reinterpret_cast< RubyFunctionPtr >( func ), argc, ##__VA_ARGS__ )
#define rb_define_method( klass, name, func, argc, ... ) rb_define_method( klass, name, reinterpret_cast< RubyFunctionPtr >( func ), argc, ##__VA_ARGS__ )
#endif // SFML_RUBYEXT_SYSTEM_MAIN_HEADER_

View File

@ -1,27 +0,0 @@
# 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.
require 'mkmf'
dir_config("window")
have_library("sfml-window-s")
find_header("main.hpp", "../sfml-system/system")
create_makefile("sfml/window", "window")

View File

@ -1,123 +0,0 @@
/* 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 <SFML/Window/Context.hpp>
VALUE globalContextClass;
/* External classes */
extern VALUE globalNonCopyableModule;
/* 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_eTypeError, "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;
}
}
static VALUE Context_Alloc( VALUE aKlass )
{
sf::Context *object = new sf::Context();
return Data_Wrap_Struct( aKlass, 0, Context_Free, object );
}
void Init_Context( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* 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.
*/
globalContextClass = rb_define_class_under( sfml, "Context", rb_cObject );
rb_include_module( globalContextClass, globalNonCopyableModule );
// Class methods
//rb_define_singleton_method( globalContextClass, "new", Context_New, 0 );
rb_define_alloc_func( globalContextClass, Context_Alloc );
rb_define_singleton_method( globalContextClass, "setReferenceActive", Context_SetReferenceActive, 0 );
// Instance methods
rb_define_method( globalContextClass, "setActive", 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" );
}

View File

@ -1,31 +0,0 @@
/* 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_

View File

@ -1,286 +0,0 @@
/* 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 "ContextSettings.hpp"
#include "main.hpp"
#include <SFML/Window/ContextSettings.hpp>
#include <iostream>
VALUE globalContextSettingsClass;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void ContextSettings_Free( sf::ContextSettings *anObject )
{
delete anObject;
}
/* call-seq:
* settings.depthBits -> depth
*
* Bits of the depth buffer
*/
static VALUE ContextSettings_GetDepth( VALUE self )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->DepthBits );
}
/* call-seq:
* settings.depthBits=(new_depth) -> new_depth
*
* Bits of the depth buffer
*/
static VALUE ContextSettings_SetDepth( VALUE self, VALUE aValue )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->DepthBits = NUM2UINT( aValue ) );
}
/* call-seq:
* settings.stencilBits -> stencil
*
* Bits of the stencil buffer
*/
static VALUE ContextSettings_GetStencil( VALUE self )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->StencilBits );
}
/* call-seq:
* settings.stencilBits=(new_stencil) -> new_stencil
*
* Bits of the stencil buffer
*/
static VALUE ContextSettings_SetStencil( VALUE self, VALUE aValue )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->StencilBits = NUM2UINT( aValue ) );
}
/* call-seq:
* settings.antialiasingLevel -> antialiasing
*
* Level of antialiasing
*/
static VALUE ContextSettings_GetAntialiasing( VALUE self )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->AntialiasingLevel );
}
/* call-seq:
* settings.antialiasingLevel=(new_antialiasing) -> new_antialiasing
*
* Level of antialiasing
*/
static VALUE ContextSettings_SetAntialiasing( VALUE self, VALUE aValue )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->AntialiasingLevel = NUM2UINT( aValue ) );
}
/* call-seq:
* settings.majorVersion -> major
*
* Major number of the context version to create
*/
static VALUE ContextSettings_GetMajorVersion( VALUE self )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->MajorVersion );
}
/* call-seq:
* settings.majorVersion=(new_major) -> new_major
*
* Major number of the context version to create
*/
static VALUE ContextSettings_SetMajorVersion( VALUE self, VALUE aValue )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->MajorVersion = NUM2UINT( aValue ) );
}
/* call-seq:
* settings.minorVersion -> minor
*
* Minor number of the context version to create
*/
static VALUE ContextSettings_GetMinorVersion( VALUE self )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->MinorVersion );
}
/* call-seq:
* settings.minorVersion=(new_minor) -> new_minor
*
* Minor number of the context version to create
*/
static VALUE ContextSettings_SetMinorVersion( VALUE self, VALUE aValue )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
return INT2FIX( object->MinorVersion = NUM2UINT( aValue ) );
}
static VALUE ContextSettings_InitializeCopy( VALUE self, VALUE aSource )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
sf::ContextSettings *source = NULL;
Data_Get_Struct( aSource, sf::ContextSettings, source );
*object = *source;
return self;
}
/* call-seq:
* ContextSettings.new( depth = 24, stencil = 8, antialiasing = 0, major = 2, minor = 0) -> settings
*
* The constructor creates the settings
*/
static VALUE ContextSettings_Alloc( VALUE aKlass )
{
sf::ContextSettings *object = new sf::ContextSettings();
return Data_Wrap_Struct( aKlass, 0, ContextSettings_Free, object );;
}
static VALUE ContextSettings_Initialize( int argc, VALUE *args, VALUE self )
{
sf::ContextSettings *object = NULL;
Data_Get_Struct( self, sf::ContextSettings, object );
switch( argc )
{
case 0:
break;
case 5:
object->MinorVersion = NUM2UINT( args[4] );
case 4:
object->MajorVersion = NUM2UINT( args[3] );
case 3:
object->AntialiasingLevel = NUM2UINT( args[2] );
case 2:
object->StencilBits = NUM2UINT( args[1] );
case 1:
object->DepthBits = NUM2UINT( args[0] );
break;
default:
rb_raise( rb_eArgError, "Expected 0..5 arguments but was given %d", argc );
return Qnil;
}
return self;
}
void Init_ContextSettings( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* ContextSettings allows to define several advanced settings of the OpenGL
* context attached to a window.
*
* All these settings have no impact on the regular SFML rendering
* (graphics module) -- except the anti-aliasing level, so you may need to use
* this structure only if you're using SFML as a windowing system for custom
* OpenGL rendering.
*
* The DepthBits and StencilBits members define the number of bits per pixel
* requested for the (respectively) depth and stencil buffers.
*
* AntialiasingLevel represents the requested number of multisampling levels
* for anti-aliasing.
*
* MajorVersion and MinorVersion define the version of the OpenGL context that
* you want. Only versions greater or equal to 3.0 are relevant; versions
* lesser than 3.0 are all handled the same way (i.e. you can use any version
* < 3.0 if you don't want an OpenGL 3 context).
*
* Please note that these values are only a hint. No failure will be reported
* if one or more of these values are not supported by the system; instead,
* SFML will try to find the closest valid match. You can then retrieve the
* settings that the window actually used to create its context, with
* Window::GetSettings().
*/
globalContextSettingsClass = rb_define_class_under( sfml, "ContextSettings", rb_cObject );
// Class methods
//rb_define_singleton_method( globalContextSettingsClass, "new", ContextSettings_New, -1 );
rb_define_alloc_func( globalContextSettingsClass, ContextSettings_Alloc );
// Instance methods
rb_define_method( globalContextSettingsClass, "initialize", ContextSettings_Initialize, -1 );
rb_define_method( globalContextSettingsClass, "initialize_copy", ContextSettings_InitializeCopy, 1 );
rb_define_method( globalContextSettingsClass, "depthBits", ContextSettings_GetDepth, 0 );
rb_define_method( globalContextSettingsClass, "depthBits=", ContextSettings_SetDepth, 1 );
rb_define_method( globalContextSettingsClass, "stencilBits", ContextSettings_GetStencil, 0 );
rb_define_method( globalContextSettingsClass, "stencilBits=", ContextSettings_SetStencil, 1 );
rb_define_method( globalContextSettingsClass, "antialiasingLevel", ContextSettings_GetAntialiasing, 0 );
rb_define_method( globalContextSettingsClass, "antialiasingLevel=", ContextSettings_SetAntialiasing, 1 );
rb_define_method( globalContextSettingsClass, "majorVersion", ContextSettings_GetMajorVersion, 0 );
rb_define_method( globalContextSettingsClass, "majorVersion=", ContextSettings_SetMajorVersion, 1 );
rb_define_method( globalContextSettingsClass, "minorVersion", ContextSettings_GetMinorVersion, 0 );
rb_define_method( globalContextSettingsClass, "minorVersion=", ContextSettings_SetMinorVersion, 1 );
// Aliases
rb_define_alias( globalContextSettingsClass, "depth", "depthBits" );
rb_define_alias( globalContextSettingsClass, "depth=", "depthBits=" );
rb_define_alias( globalContextSettingsClass, "depth_bits", "depthBits" );
rb_define_alias( globalContextSettingsClass, "depth_bits=", "depthBits=" );
rb_define_alias( globalContextSettingsClass, "stencil", "stencilBits" );
rb_define_alias( globalContextSettingsClass, "stencil=", "stencilBits=" );
rb_define_alias( globalContextSettingsClass, "stencil_bits", "stencilBits" );
rb_define_alias( globalContextSettingsClass, "stencil_bits=", "stencilBits=" );
rb_define_alias( globalContextSettingsClass, "antialiasing", "antialiasingLevel" );
rb_define_alias( globalContextSettingsClass, "antialiasing=", "antialiasingLevel=" );
rb_define_alias( globalContextSettingsClass, "antialiasing_level", "antialiasingLevel" );
rb_define_alias( globalContextSettingsClass, "antialiasing_level=", "antialiasingLevel=" );
rb_define_alias( globalContextSettingsClass, "major", "majorVersion" );
rb_define_alias( globalContextSettingsClass, "major=", "majorVersion=" );
rb_define_alias( globalContextSettingsClass, "major_version", "majorVersion" );
rb_define_alias( globalContextSettingsClass, "major_version=", "majorVersion=" );
rb_define_alias( globalContextSettingsClass, "minor", "minorVersion" );
rb_define_alias( globalContextSettingsClass, "minor=", "minorVersion=" );
rb_define_alias( globalContextSettingsClass, "minor_version", "minorVersion" );
rb_define_alias( globalContextSettingsClass, "minor_version=", "minorVersion=" );
}

View File

@ -1,31 +0,0 @@
/* 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_SETTINGS_HEADER_
#define SFML_RUBYEXT_CONTEXT_SETTINGS_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_ContextSettings( void );
#endif // SFML_RUBYEXT_CONTEXT_SETTINGS_HEADER_

View File

@ -1,360 +0,0 @@
/* 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/Window/Event.hpp>
VALUE globalEventClass;
/* Joystick buttons events parameters (JoyButtonPressed, JoyButtonReleased). */
VALUE globalJoyButtonEventClass;
/* Joystick axis move event parameters (JoyMoved). */
VALUE globalJoyMoveEventClass;
/* Keyboard event parameters (KeyPressed, KeyReleased). */
VALUE globalKeyEventClass;
/* Mouse buttons events parameters (MouseButtonPressed, MouseButtonReleased). */
VALUE globalMouseButtonEventClass;
/* Mouse move event parameters (MouseMoved). */
VALUE globalMouseMoveEventClass;
/* Mouse wheel events parameters (MouseWheelMoved). */
VALUE globalMouseWheelEventClass;
/* Size events parameters (Resized). */
VALUE globalSizeEventClass;
/* Text event parameters (TextEntered). */
VALUE globalTextEventClass;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void Event_Free( sf::Event *anObject )
{
delete anObject;
}
#define AXIS2NUM( x ) INT2NUM( static_cast< int > ( x ) )
#define NUM2AXIS( x ) static_cast< sf::Joy::Axis >( NUM2INT( x ) )
#define KEY2NUM( x ) INT2NUM( static_cast< int > ( x ) )
#define NUM2KEY( x ) static_cast< sf::Key::Code >( NUM2INT( x ) )
#define MOUSE2NUM( x ) INT2NUM( static_cast< int > ( x ) )
#define NUM2MOUSE( x ) static_cast< sf::Mouse::Button >( NUM2INT( x ) )
#define EVENT_TYPE_ACCESSORS( a, b, conv1 ) \
{ \
sf::Event:: a##Event * object = NULL; \
Data_Get_Struct( self, sf::Event:: a##Event, object ); \
return conv1 ( object-> b ); \
}
#define EVENT_TYPE_BOOL_ACCESSORS( a, b ) \
{ \
sf::Event:: a##Event * object = NULL; \
Data_Get_Struct( self, sf::Event:: a##Event, object ); \
if( object-> b == true ) \
return Qtrue; \
else \
return Qfalse; \
}
/* Index of the joystick (0 or 1). */
static VALUE JoyButtonEvent_GetJoystickId( VALUE self )
EVENT_TYPE_ACCESSORS( JoyButton, JoystickId, INT2NUM )
/* Index of the button that has been pressed. */
static VALUE JoyButtonEvent_GetButton( VALUE self )
EVENT_TYPE_ACCESSORS( JoyButton, Button, INT2NUM )
/* Index of the joystick (0 or 1). */
static VALUE JoyMoveEvent_GetJoystickId( VALUE self )
EVENT_TYPE_ACCESSORS( JoyMove, JoystickId, INT2NUM )
/* Axis on which the joystick moved. */
static VALUE JoyMoveEvent_GetAxis( VALUE self )
EVENT_TYPE_ACCESSORS( JoyMove, Axis, AXIS2NUM )
/* New position on the axis (in range [-100, 100]). */
static VALUE JoyMoveEvent_GetPosition( VALUE self )
EVENT_TYPE_ACCESSORS( JoyMove, Position, rb_float_new )
/* Code of the key that has been pressed. */
static VALUE KeyEvent_GetCode( VALUE self )
EVENT_TYPE_ACCESSORS( Key, Code, KEY2NUM )
/* Is the Alt key pressed? */
static VALUE KeyEvent_GetAlt( VALUE self )
EVENT_TYPE_BOOL_ACCESSORS( Key, Alt )
/* Is the Control key pressed? */
static VALUE KeyEvent_GetControl( VALUE self )
EVENT_TYPE_BOOL_ACCESSORS( Key, Control )
/* Is the Shift key pressed? */
static VALUE KeyEvent_GetShift( VALUE self )
EVENT_TYPE_BOOL_ACCESSORS( Key, Shift )
/* Code of the button that has been pressed. */
static VALUE MouseButtonEvent_GetButton( VALUE self )
EVENT_TYPE_ACCESSORS( MouseButton, Button, MOUSE2NUM )
/* X position of the mouse pointer, relative to the left of the owner window */
static VALUE MouseButtonEvent_GetX( VALUE self )
EVENT_TYPE_ACCESSORS( MouseButton, X, INT2NUM )
/* Y position of the mouse pointer, relative to the top of the owner window */
static VALUE MouseButtonEvent_GetY( VALUE self )
EVENT_TYPE_ACCESSORS( MouseButton, Y, INT2NUM )
/* X position of the mouse pointer, relative to the left of the owner window */
static VALUE MouseMoveEvent_GetX( VALUE self )
EVENT_TYPE_ACCESSORS( MouseMove, X, INT2NUM )
/* Y position of the mouse pointer, relative to the top of the owner window */
static VALUE MouseMoveEvent_GetY( VALUE self )
EVENT_TYPE_ACCESSORS( MouseMove, Y, INT2NUM )
/* Number of ticks the wheel has moved (positive is up, negative is down). */
static VALUE MouseWheelEvent_GetDelta( VALUE self )
EVENT_TYPE_ACCESSORS( MouseWheel, Delta, INT2NUM )
/* X position of the mouse pointer, relative to the left of the owner window */
static VALUE MouseWheelEvent_GetX( VALUE self )
EVENT_TYPE_ACCESSORS( MouseWheel, X, INT2NUM )
/* Y position of the mouse pointer, relative to the top of the owner window */
static VALUE MouseWheelEvent_GetY( VALUE self )
EVENT_TYPE_ACCESSORS( MouseWheel, Y, INT2NUM )
/* New width, in pixels. */
static VALUE SizeEvent_GetWidth( VALUE self )
EVENT_TYPE_ACCESSORS( Size, Width, INT2NUM )
/* New height, in pixels. */
static VALUE SizeEvent_GetHeight( VALUE self )
EVENT_TYPE_ACCESSORS( Size, Height, INT2NUM )
/* UTF-32 unicode value of the character. */
static VALUE TextEvent_GetUnicode( VALUE self )
EVENT_TYPE_ACCESSORS( Text, Unicode, INT2NUM )
/* call-seq:
* Event.new(type) -> event
*
* You should never call this function directly. You should only aquire event's trough
* SFML::Window#getEvent or SFML::Window#waitEvent, if you need to pass data to a method
* that takes an event instance then make a proxy instance to simulate an event.
* NOTE: Using this method works but it will act constant as you can't access any values.
*/
static VALUE Event_Initialize( VALUE self, VALUE aType )
{
sf::Event * object = NULL;
Data_Get_Struct( self, sf::Event, object );
int typeNum = FIX2INT( aType );
if(typeNum >= 0 && typeNum < sf::Event::Count)
{
rb_iv_set( self, "@type", aType );
object->Type = static_cast< sf::Event::EventType >( typeNum );
}
else
{
rb_raise( rb_eTypeError, "expected Fixnum in range of 0...SFML::Event::Count" );
}
bool noSpecialType = false;
VALUE eventType;
const char * name = NULL;
switch( object->Type )
{
case sf::Event::JoyButtonPressed:
case sf::Event::JoyButtonReleased:
eventType = Data_Wrap_Struct( globalJoyButtonEventClass, 0, 0, &object->JoyButton );
name = "@joyButton";
break;
case sf::Event::JoyMoved:
eventType = Data_Wrap_Struct( globalJoyMoveEventClass, 0, 0, &object->JoyMove );
name = "@joyMove";
break;
case sf::Event::KeyPressed:
case sf::Event::KeyReleased:
eventType = Data_Wrap_Struct( globalKeyEventClass, 0, 0, &object->Key );
name = "@key";
break;
case sf::Event::MouseButtonPressed:
case sf::Event::MouseButtonReleased:
eventType = Data_Wrap_Struct( globalMouseButtonEventClass, 0, 0, &object->MouseButton );
name = "@mouseButton";
break;
case sf::Event::MouseMoved:
eventType = Data_Wrap_Struct( globalMouseMoveEventClass, 0, 0, &object->MouseMove );
name = "@mouseMove";
break;
case sf::Event::MouseWheelMoved:
eventType = Data_Wrap_Struct( globalMouseWheelEventClass, 0, 0, &object->MouseWheel );
name = "@mouseWheel";
break;
case sf::Event::Resized:
eventType = Data_Wrap_Struct( globalSizeEventClass, 0, 0, &object->Size );
name = "@resized";
break;
case sf::Event::TextEntered:
eventType = Data_Wrap_Struct( globalTextEventClass, 0, 0, &object->Text );
name = "@text";
break;
default:
noSpecialType = true;
};
if( noSpecialType == false )
{
rb_obj_call_init( eventType, 0, 0 );
rb_iv_set( eventType, "@internal__parent_ref", self );
rb_iv_set( self, name, eventType );
}
return self;
}
static VALUE Event_InitializeCopy( VALUE self, VALUE aSource )
{
sf::Event *object = NULL;
Data_Get_Struct( self, sf::Event, object );
sf::Event *source = NULL;
Data_Get_Struct( aSource, sf::Event, source );
*object = *source;
return Event_Initialize( self, INT2FIX( object->Type ) );
}
static VALUE Event_Alloc( VALUE aKlass )
{
sf::Event *object = new sf::Event();
return Data_Wrap_Struct( aKlass, 0, Event_Free, object );
}
void Init_Event( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* 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 can be directly accessed from the event object. If you try to access any data which
* would be considered undefined then 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.size);
*
* # etc ...
* end
*/
globalEventClass = rb_define_class_under( sfml, "Event", rb_cObject );
globalJoyButtonEventClass = rb_define_class_under( globalEventClass, "JoyButton", rb_cObject );
globalJoyMoveEventClass = rb_define_class_under( globalEventClass, "JoyMove", rb_cObject );
globalKeyEventClass = rb_define_class_under( globalEventClass, "Key", rb_cObject );
globalMouseButtonEventClass = rb_define_class_under( globalEventClass, "MouseButton", rb_cObject );
globalMouseMoveEventClass = rb_define_class_under( globalEventClass, "MouseMove", rb_cObject );
globalMouseWheelEventClass = rb_define_class_under( globalEventClass, "MouseWheel", rb_cObject );
globalSizeEventClass = rb_define_class_under( globalEventClass, "Size", rb_cObject );
globalTextEventClass = rb_define_class_under( globalEventClass, "Text", rb_cObject );
rb_define_const( globalEventClass, "Closed", INT2NUM( sf::Event::Closed ) );
rb_define_const( globalEventClass, "Resized", INT2NUM( sf::Event::Resized ) );
rb_define_const( globalEventClass, "LostFocus", INT2NUM( sf::Event::LostFocus ) );
rb_define_const( globalEventClass, "GainedFocus", INT2NUM( sf::Event::GainedFocus ) );
rb_define_const( globalEventClass, "TextEntered", INT2NUM( sf::Event::TextEntered ) );
rb_define_const( globalEventClass, "KeyPressed", INT2NUM( sf::Event::KeyPressed ) );
rb_define_const( globalEventClass, "KeyReleased", INT2NUM( sf::Event::KeyReleased ) );
rb_define_const( globalEventClass, "MouseWheelMoved", INT2NUM( sf::Event::MouseWheelMoved ) );
rb_define_const( globalEventClass, "MouseButtonPressed", INT2NUM( sf::Event::MouseButtonPressed ) );
rb_define_const( globalEventClass, "MouseButtonReleased", INT2NUM( sf::Event::MouseButtonReleased ) );
rb_define_const( globalEventClass, "MouseMoved", INT2NUM( sf::Event::MouseMoved ) );
rb_define_const( globalEventClass, "MouseEntered", INT2NUM( sf::Event::MouseEntered ) );
rb_define_const( globalEventClass, "MouseLeft", INT2NUM( sf::Event::MouseLeft ) );
rb_define_const( globalEventClass, "JoyButtonPressed", INT2NUM( sf::Event::JoyButtonPressed ) );
rb_define_const( globalEventClass, "JoyButtonReleased", INT2NUM( sf::Event::JoyButtonReleased ) );
rb_define_const( globalEventClass, "JoyMoved", INT2NUM( sf::Event::JoyMoved ) );
rb_define_const( globalEventClass, "Count", INT2NUM( sf::Event::Count ) );
// Class methods
//rb_define_singleton_method( globalEventClass, "new", Event_New, -1 );
rb_define_alloc_func( globalEventClass, Event_Alloc );
// Instance methods
rb_define_method( globalEventClass, "initialize", Event_Initialize, 1 );
rb_define_method( globalEventClass, "initialize_copy", Event_InitializeCopy, 1 );
rb_define_attr( globalEventClass, "type", 1, 0 );
rb_define_attr( globalEventClass, "joyButton", 1, 0 );
rb_define_attr( globalEventClass, "joyMove", 1, 0 );
rb_define_attr( globalEventClass, "key", 1, 0 );
rb_define_attr( globalEventClass, "mouseButton", 1, 0 );
rb_define_attr( globalEventClass, "mouseMove", 1, 0 );
rb_define_attr( globalEventClass, "mouseWheel", 1, 0 );
rb_define_attr( globalEventClass, "size", 1, 0 );
rb_define_attr( globalEventClass, "text", 1, 0 );
// JoyButton methods
rb_define_method( globalJoyButtonEventClass, "joystickId", JoyButtonEvent_GetJoystickId, 0 );
rb_define_method( globalJoyButtonEventClass, "button", JoyButtonEvent_GetButton, 0 );
// JoyMove methods
rb_define_method( globalJoyMoveEventClass, "joystickId", JoyMoveEvent_GetJoystickId, 0 );
rb_define_method( globalJoyMoveEventClass, "axis", JoyMoveEvent_GetAxis, 0 );
rb_define_method( globalJoyMoveEventClass, "position", JoyMoveEvent_GetPosition, 0 );
// Key methods
rb_define_method( globalKeyEventClass, "code", KeyEvent_GetCode, 0 );
rb_define_method( globalKeyEventClass, "alt", KeyEvent_GetAlt, 0 );
rb_define_method( globalKeyEventClass, "control", KeyEvent_GetControl, 0 );
rb_define_method( globalKeyEventClass, "shift", KeyEvent_GetShift, 0 );
// MouseButton methods
rb_define_method( globalMouseButtonEventClass, "button", MouseButtonEvent_GetButton, 0 );
rb_define_method( globalMouseButtonEventClass, "x", MouseButtonEvent_GetX, 0 );
rb_define_method( globalMouseButtonEventClass, "y", MouseButtonEvent_GetY, 0 );
// MouseMove methods
rb_define_method( globalMouseMoveEventClass, "x", MouseMoveEvent_GetX, 0 );
rb_define_method( globalMouseMoveEventClass, "y", MouseMoveEvent_GetY, 0 );
// MouseWheel methods
rb_define_method( globalMouseWheelEventClass, "delta", MouseWheelEvent_GetDelta, 0 );
rb_define_method( globalMouseWheelEventClass, "x", MouseWheelEvent_GetX, 0 );
rb_define_method( globalMouseWheelEventClass, "y", MouseWheelEvent_GetY, 0 );
// Size methods
rb_define_method( globalSizeEventClass, "width", SizeEvent_GetWidth, 0 );
rb_define_method( globalSizeEventClass, "height", SizeEvent_GetWidth, 0 );
// Text methods
rb_define_method( globalTextEventClass, "unicode", TextEvent_GetUnicode, 0 );
}

View File

@ -1,31 +0,0 @@
/* 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_

View File

@ -1,208 +0,0 @@
/* 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 "Input.hpp"
#include "main.hpp"
#include <SFML/Window/Input.hpp>
VALUE globalInputClass;
/* External classes */
extern VALUE globalNonCopyableModule;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void Input_Free( sf::Event *anObject )
{
delete anObject;
}
/* call-seq:
* input.isKeyDown( keycode ) -> true or false
*
* Get the current state of a key (pressed or released).
*/
static VALUE Input_IsKeyDown( VALUE self, VALUE aKeyCode )
{
sf::Input *object = NULL;
Data_Get_Struct( self, sf::Input, object );
sf::Key::Code rawCode = static_cast< sf::Key::Code > ( NUM2INT( aKeyCode ) );
if( object->IsKeyDown( rawCode ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* input.isMouseButtonDown( keycode ) -> true or false
*
* Get the current state of a mouse button (pressed or released).
*/
static VALUE Input_IsMouseButtonDown( VALUE self, VALUE aMouseButton )
{
sf::Input *object = NULL;
Data_Get_Struct( self, sf::Input, object );
sf::Mouse::Button rawButton = static_cast< sf::Mouse::Button > ( NUM2INT( aMouseButton ) );
if( object->IsMouseButtonDown( rawButton ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* input.isJoystickButtonDown( joystick, button ) -> true or false
*
* Get the current state of a joystick button (pressed or released).
*/
static VALUE Input_IsJoystickButtonDown( VALUE self, VALUE aJoystick, VALUE aButton )
{
sf::Input *object = NULL;
Data_Get_Struct( self, sf::Input, object );
unsigned int rawJoystick = NUM2UINT( aJoystick );
unsigned int rawButton = NUM2UINT( aButton );
if( object->IsJoystickButtonDown( aJoystick, rawButton ) == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* input.getMouseX() -> fixnum
*
* The returned position is relative to the left border of the owner window.
*/
static VALUE Input_GetMouseX( VALUE self, VALUE aMouseButton )
{
sf::Input *object = NULL;
Data_Get_Struct( self, sf::Input, object );
return INT2FIX( object->GetMouseX() );
}
/* call-seq:
* input.getMouseY() -> fixnum
*
* The returned position is relative to the top border of the owner window.
*/
static VALUE Input_GetMouseY( VALUE self, VALUE aMouseButton )
{
sf::Input *object = NULL;
Data_Get_Struct( self, sf::Input, object );
return INT2FIX( object->GetMouseY() );
}
/* call-seq:
* input.getJoystickAxis( joystick, axis ) -> true or false
*
* The returned position is in the range [-100, 100], except the POV which is an angle and is thus defined in [0, 360].
*/
static VALUE Input_GetJoystickAxis( VALUE self, VALUE aJoystick, VALUE anAxis )
{
sf::Input *object = NULL;
Data_Get_Struct( self, sf::Input, object );
unsigned int rawJoystick = NUM2UINT( aJoystick );
sf::Joy::Axis rawAxis = static_cast< sf::Joy::Axis >( NUM2INT( anAxis ) );
return rb_float_new( object->GetJoystickAxis( rawJoystick, rawAxis ) );
}
static VALUE Input_Alloc( VALUE aKlass )
{
sf::Input *object = new sf::Input();
return Data_Wrap_Struct( aKlass, 0, Input_Free, object );
}
void Init_Input( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* The SFML::Input class provides a way to access the state of keys, mouse buttons, mouse position, joystick
* buttons and jostick axis.
*
* SFML::Input provides the same informations as the event system, but these informations can be accessed at any time,
* which is more convenient in many situations.
*
* For example, to move an entity you can decide to catch the SFML::Event::KeyPressed event on arrow keys. But if you
* do so, you will only receive one event when the key gets pressed (or repeated events if you activated this feature),
* thus the entity will not move smoothly. The best solution here is to use SFML::Input#isKeyDown so that you can
* update your entity's position at every iteration of your game loop, not only when you catch a KeyPressed event.
*
* Note that instances of SFML::Input cannot be created directly, they must be retrieved from a window (SFML::Window)
* with the SFML::Window#input method.
*
* Usage example:
*
* # Retrieve the input object attached to our window
* input = window.input
*
* # Move an entity according to the current keys state
* offset = 5.0 * window.frameTime # 5 pixels/sec
* entity.move( -offset, 0 ) if input.keyDown?( SFML::Key::Left )
* entity.move( offset, 0 ) if input.keyDown?( SFML::Key::Right )
* entity.move( 0, -offset ) if input.keyDown?( SFML::Key::Up )
* entity.move( 0, offset ) if input.keyDown?( SFML::Key::Down )
*/
globalInputClass = rb_define_class_under( sfml, "Input", rb_cObject );
rb_include_module( globalInputClass, globalNonCopyableModule );
// Class methods
//rb_define_singleton_method( globalInputClass, "new", Input_New, -1 );
rb_define_alloc_func( globalInputClass, Input_Alloc );
// Instance methods
rb_define_method( globalInputClass, "isKeyDown", Input_IsKeyDown, 1 );
rb_define_method( globalInputClass, "isMouseButtonDown", Input_IsMouseButtonDown, 1 );
rb_define_method( globalInputClass, "isJoystickButtonDown", Input_IsJoystickButtonDown, 2 );
rb_define_method( globalInputClass, "getMouseX", Input_GetMouseX, 0 );
rb_define_method( globalInputClass, "getMouseY", Input_GetMouseY, 0 );
rb_define_method( globalInputClass, "getJoystickAxis", Input_GetJoystickAxis, 2 );
// Aliases
rb_define_alias( globalInputClass, "key_down?", "isKeyDown");
rb_define_alias( globalInputClass, "keyDown?", "isKeyDown");
rb_define_alias( globalInputClass, "mouse_button_down?", "isMouseButtonDown");
rb_define_alias( globalInputClass, "mouseButtonDown?", "isMouseButtonDown");
rb_define_alias( globalInputClass, "joystick_button_down?", "isJoystickButtonDown");
rb_define_alias( globalInputClass, "joystickButtonDown?", "isJoystickButtonDown");
rb_define_alias( globalInputClass, "mouseX", "getMouseX");
rb_define_alias( globalInputClass, "mouse_x", "getMouseX");
rb_define_alias( globalInputClass, "mouseY", "getMouseY");
rb_define_alias( globalInputClass, "mouse_y", "getMouseY");
rb_define_alias( globalInputClass, "joystickAxis", "getJoystickAxis");
rb_define_alias( globalInputClass, "joystick_axis", "getJoystickAxis");
}

View File

@ -1,31 +0,0 @@
/* 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_INPUT_HEADER_
#define SFML_RUBYEXT_INPUT_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Input( void );
#endif // SFML_RUBYEXT_INPUT_HEADER_

View File

@ -1,305 +0,0 @@
/* 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 "VideoMode.hpp"
#include "main.hpp"
#include <SFML/Window/VideoMode.hpp>
VALUE globalVideoModeClass;
/* Internal function
* Forces the argument someValue to be a VideoMode. If it can convert it then it will.
* So you can always safely asume that this function returns a VideoMode object.
* If it fails then an exception will be thrown.
*/
VALUE VideoMode_ForceType( VALUE someValue )
{
if( rb_obj_is_kind_of( someValue, rb_cArray ) == Qtrue )
{
VALUE arg1 = rb_ary_entry( someValue, 0 );
VALUE arg2 = rb_ary_entry( someValue, 1 );
if( FIX2INT( rb_funcall( someValue, rb_intern( "size" ), 0 ) ) == 3 )
{
VALUE arg3 = rb_ary_entry( someValue, 2 );
return rb_funcall( globalVideoModeClass, rb_intern( "new" ), 3, arg1, arg2, arg3 );
}
else
{
return rb_funcall( globalVideoModeClass, rb_intern( "new" ), 2, arg1, arg2 );
}
}
else if( rb_obj_is_kind_of( someValue, globalVideoModeClass ) == Qtrue )
{
return someValue;
}
else
{
rb_raise( rb_eRuntimeError, "expected Array[width, height, bpp] or VideoMode" );
}
}
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void VideoMode_Free( sf::VideoMode *anObject )
{
delete anObject;
}
/* call-seq:
* mode.width -> width
*
* Video mode width, in pixels.
*/
static VALUE VideoMode_GetWidth( VALUE self )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return INT2FIX( object->Width );
}
/* call-seq:
* mode.width=(new_width) -> new_width
*
* Video mode width, in pixels.
*/
static VALUE VideoMode_SetWidth( VALUE self, VALUE aValue )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return INT2FIX( object->Width = NUM2UINT( aValue ) );
}
/* call-seq:
* mode.height -> height
*
* Video mode height, in pixels.
*/
static VALUE VideoMode_GetHeight( VALUE self )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return INT2FIX( object->Height );
}
/* call-seq:
* mode.height=(new_height) -> new_height
*
* Video mode height, in pixels.
*/
static VALUE VideoMode_SetHeight( VALUE self, VALUE aValue )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return INT2FIX( object->Height = NUM2UINT( aValue ) );
}
/* call-seq:
* mode.bitsPerPixel -> bpp
*
* Video mode pixel depth, in bits per pixels.
*/
static VALUE VideoMode_GetBitsPerPixel( VALUE self )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return INT2FIX( object->BitsPerPixel );
}
/* call-seq:
* mode.bitsPerPixel=(new_bpp) -> new_bpp
*
* Video mode pixel depth, in bits per pixels.
*/
static VALUE VideoMode_SetBitsPerPixel( VALUE self, VALUE aValue )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return INT2FIX( object->BitsPerPixel = NUM2UINT( aValue ) );
}
/* call-seq:
* mode.isValid() -> true or false
*
* Tell whether or not the video mode is valid.
*
* The validity of video modes is only relevant when using fullscreen windows; otherwise any video mode can be used
* with no restriction.
*/
static VALUE VideoMode_IsValid( VALUE self )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
return ( object->IsValid() == true ? Qtrue : Qfalse );
}
static VALUE VideoMode_InitializeCopy( VALUE self, VALUE aSource )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
sf::VideoMode *source = NULL;
Data_Get_Struct( aSource, sf::VideoMode, source );
*object = *source;
}
/* call-seq:
* VideoMode.getDesktopMode -> desktop_mode
*
* Get the current desktop video mode.
*/
static VALUE VideoMode_GetDesktopMode( VALUE aKlass )
{
sf::VideoMode *object = new sf::VideoMode( sf::VideoMode::GetDesktopMode() );
VALUE rbData = Data_Wrap_Struct( aKlass, 0, VideoMode_Free, object );
rb_obj_call_init( rbData, 0, 0 );
return rbData;
}
/* call-seq:
* VideoMode.getFullscreenModes -> [supported_modes]
*
* Retrieve all the video modes supported in fullscreen mode.
*
* When creating a fullscreen window, the video mode is restricted to be compatible with what the graphics driver and
* monitor support. This function returns the complete list of all video modes that can be used in fullscreen mode.
* The returned array is sorted from best to worst, so that the first element will always give the best mode
* (higher width, height and bits-per-pixel).
*/
static VALUE VideoMode_GetFullscreenModes( VALUE aKlass )
{
const std::vector< sf::VideoMode >& modes = sf::VideoMode::GetFullscreenModes();
VALUE array = rb_ary_new();
for( std::vector< sf::VideoMode >::const_iterator it = modes.begin(), end = modes.end(); it != end; it++ )
{
sf::VideoMode *object = new sf::VideoMode( *it );
VALUE rbData = Data_Wrap_Struct( aKlass, 0, VideoMode_Free, object );
rb_obj_call_init( rbData, 0, 0 );
rb_ary_push( array, rbData );
}
return array;
}
static VALUE VideoMode_Alloc( VALUE aKlass )
{
sf::VideoMode *object = new sf::VideoMode();
return Data_Wrap_Struct( aKlass, 0, VideoMode_Free, object );
}
/* call-seq:
* VideoMode.new() -> mode
* VideoMode.new( width, height, bpp = 32 ) -> mode
*
* Create a new mode.
*/
static VALUE VideoMode_Initialize( int argc, VALUE *args, VALUE self )
{
sf::VideoMode *object = NULL;
Data_Get_Struct( self, sf::VideoMode, object );
switch( argc )
{
case 0:
break;
case 3:
object->BitsPerPixel = NUM2UINT( args[2] );
case 2:
object->Height = NUM2UINT( args[1] );
object->Width = NUM2UINT( args[0] );
break;
default:
rb_raise( rb_eArgError, "Expected 0..3 arguments but was given %d", argc );
return Qnil;
}
return self;
}
void Init_VideoMode( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* A video mode is defined by a width and a height (in pixels) and a depth (in bits per pixel).
*
* Video modes are used to setup windows (SFML::Window) at creation time.
*
* The main usage of video modes is for fullscreen mode: indeed you must use one of the valid
* video modes allowed by the OS (which are defined by what the monitor and the graphics card support),
* otherwise your window creation will just fail.
*
* SFML::VideoMode provides a static function for retrieving the list of all the video modes supported by
* the system: getFullscreenModes().
*
* A custom video mode can also be checked directly for fullscreen compatibility with its isValid() function.
*
* Additionnally, SFML::VideoMode provides a static function to get the mode currently used by the desktop:
* getDesktopMode(). This allows to build windows with the same size or pixel depth as the current resolution.
*
* Usage example:
*
* # Display the list of all the video modes available for fullscreen
* modes = SFML::VideoMode.getFullscreenModes()
* i = 0
* modes.each do | mode |
* puts "Mode #" + i + ": " + mode.width + "x" + mode.height + " - " + mode.bitsPerPixel + " bpp"
*
* end
*
* # Create a window with the same pixel depth as the desktop
* desktop = SFML::VideoMode.getDesktopMode()
* window.create( SFML::VideoMode.new( 1024, 768, desktop.BitsPerPixel ), "SFML window" )
*/
globalVideoModeClass = rb_define_class_under( sfml, "VideoMode", rb_cObject );
// Class methods
//rb_define_singleton_method( globalVideoModeClass, "new", VideoMode_New, -1 );
rb_define_alloc_func( globalVideoModeClass, VideoMode_Alloc );
rb_define_singleton_method( globalVideoModeClass, "getDesktopMode", VideoMode_GetDesktopMode, 0 );
rb_define_singleton_method( globalVideoModeClass, "getFullscreenModes", VideoMode_GetFullscreenModes, 0 );
// Instance methods
rb_define_method( globalVideoModeClass, "initialize", VideoMode_Initialize, -1 );
rb_define_method( globalVideoModeClass, "initialize_copy", VideoMode_InitializeCopy, 1 );
rb_define_method( globalVideoModeClass, "width", VideoMode_GetWidth, 0 );
rb_define_method( globalVideoModeClass, "width=", VideoMode_SetWidth, 1 );
rb_define_method( globalVideoModeClass, "height", VideoMode_GetHeight, 0 );
rb_define_method( globalVideoModeClass, "height=", VideoMode_SetHeight, 1 );
rb_define_method( globalVideoModeClass, "bitsPerPixel", VideoMode_GetBitsPerPixel, 0 );
rb_define_method( globalVideoModeClass, "bitsPerPixel=", VideoMode_SetBitsPerPixel, 1 );
rb_define_method( globalVideoModeClass, "isValid", VideoMode_IsValid, 0 );
// Class aliases
rb_define_alias( CLASS_OF( globalVideoModeClass ), "desktopMode", "getDesktopMode" );
rb_define_alias( CLASS_OF( globalVideoModeClass ), "desktop_mode", "getDesktopMode" );
rb_define_alias( CLASS_OF( globalVideoModeClass ), "fullscreenModes", "getFullscreenModes" );
rb_define_alias( CLASS_OF( globalVideoModeClass ), "fullscreen_modes", "getFullscreenModes" );
// Aliases
rb_define_alias( globalVideoModeClass, "bits_per_pixel", "bitsPerPixel" );
rb_define_alias( globalVideoModeClass, "bits_per_pixel=", "bitsPerPixel=" );
rb_define_alias( globalVideoModeClass, "bpp", "bitsPerPixel" );
rb_define_alias( globalVideoModeClass, "bpp=", "bitsPerPixel=" );
rb_define_alias( globalVideoModeClass, "is_valid", "isValid" );
rb_define_alias( globalVideoModeClass, "valid?", "isValid" );
}

View File

@ -1,33 +0,0 @@
/* 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_VIDEO_MODE_HEADER_
#define SFML_RUBYEXT_VIDEO_MODE_HEADER_
#include "ruby.h"
VALUE VideoMode_ForceType( VALUE someValue );
// Ruby initiation function
void Init_VideoMode( void );
#endif // SFML_RUBYEXT_VIDEO_MODE_HEADER_

View File

@ -1,758 +0,0 @@
/* 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 <SFML/Window/Window.hpp>
#include "Window.hpp"
#include "VideoMode.hpp"
#include "Vector2.hpp"
#include "main.hpp"
VALUE globalWindowClass;
/* External classes */
extern VALUE globalVideoModeClass;
extern VALUE globalContextSettingsClass;
extern VALUE globalEventClass;
extern VALUE globalInputClass;
extern VALUE globalVector2Class;
extern VALUE globalNonCopyableModule;
/* Free a heap allocated object
* Not accessible trough ruby directly!
*/
static void Window_Free( sf::Window *anObject )
{
delete anObject;
}
/* call-seq:
* window.close()
*
* Close the window and destroy all the attached resources.
*
* After calling this function, the SFML::Window instance remains valid and you can call SFML::Window#create to recreate
* the window. All other functions such as getEvent or display will still work (i.e. you don't have to test
* isOpened every time), and will have no effect on closed windows.
*/
static VALUE Window_Close( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->Close();
return Qnil;
}
/* call-seq:
* window.create( mode, title, style = SFML::Style::Default, settings = SFML::ContextSettings.new )
*
* Create (or recreate) the window.
*
* If the window was already created, it closes it first. If style contains Style::Fullscreen,
* then mode must be a valid video mode.
*/
static VALUE Window_Create( int argc, VALUE *args, VALUE self )
{
sf::Window *object = NULL;
sf::VideoMode *mode = NULL;
sf::ContextSettings *settings = NULL;
VALUE arg0 = Qnil;
Data_Get_Struct( self, sf::Window, object );
switch( argc )
{
case 2:
arg0 = VideoMode_ForceType( args[0] );
VALIDATE_CLASS( arg0, globalVideoModeClass, "first" );
VALIDATE_CLASS( args[1], rb_cString, "second" );
Data_Get_Struct( arg0, sf::VideoMode, mode );
object->Create( *mode ,rb_string_value_cstr( &args[1] ) );
break;
case 3:
arg0 = VideoMode_ForceType( args[0] );
VALIDATE_CLASS( arg0, globalVideoModeClass, "first" );
VALIDATE_CLASS( args[1], rb_cString, "second" );
VALIDATE_CLASS( args[2], rb_cFixnum, "third" );
Data_Get_Struct( arg0, sf::VideoMode, mode );
object->Create( *mode, rb_string_value_cstr( &args[1] ), FIX2INT( args[2] ) );
break;
case 4:
arg0 = VideoMode_ForceType( args[0] );
VALIDATE_CLASS( arg0, globalVideoModeClass, "first" );
VALIDATE_CLASS( args[1], rb_cString, "second" );
VALIDATE_CLASS( args[2], rb_cFixnum, "third" );
VALIDATE_CLASS( args[3], globalContextSettingsClass, "fourth" );
Data_Get_Struct( arg0, sf::VideoMode, mode );
Data_Get_Struct( args[3], sf::ContextSettings, settings );
object->Create( *mode, rb_string_value_cstr( &args[1] ), FIX2INT( args[2] ), *settings );
break;
default:
rb_raise( rb_eArgError, "Expected 2..4 arguments but was given %d", argc );
break;
}
return Qnil;
}
/* call-seq:
* window.display()
*
* Display on screen what has been rendered to the window so far.
*
* This function is typically called after all OpenGL rendering has been done for the current frame, in order to show
* it on screen.
*/
static VALUE Window_Display( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->Display();
return Qnil;
}
/* call-seq:
* window.enableKeyRepeat( enable )
*
* Enable or disable automatic key-repeat.
*
* If key repeat is enabled, you will receive repeated KeyPress events while keeping a key pressed. If it is disabled,
* you will only get a single event when the key is pressed.
*
* Key repeat is enabled by default.
*/
static VALUE Window_EnableKeyRepeat( VALUE self, VALUE anEnableFlag )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
if( anEnableFlag == Qfalse )
{
object->EnableKeyRepeat( false );
}
else if( anEnableFlag == Qtrue )
{
object->EnableKeyRepeat( true );
}
else
{
rb_raise( rb_eTypeError, "Expected true or false" );
}
return Qnil;
}
/* call-seq:
* window.getEvent() -> event or nil
*
* Pop the event on top of events stack, if any, and return it.
*
* This function is not blocking: if there's no pending event then it will return nil. Note that more than the returned
* event may be present in the events stack, thus you should always call this function in a loop to make sure that you
* process every pending event.
*/
static VALUE Window_GetEvent( VALUE self )
{
sf::Event event;
sf::Window *window = NULL;
Data_Get_Struct( self, sf::Window, window );
if( window->GetEvent( event ) == true )
{
VALUE rbObject = rb_funcall( globalEventClass, rb_intern( "new" ), 1, INT2FIX( event.Type ) );
sf::Event *rubyRawEvent = NULL;
Data_Get_Struct( rbObject, sf::Event, rubyRawEvent );
*rubyRawEvent = event;
return rbObject;
}
else
{
return Qnil;
}
}
/* call-seq:
* window.getFrameTime() -> float
*
* This function returns the time elapsed during the last frame. This can be useful for calculating the framerate, or
* for updating the application's objects.
*/
static VALUE Window_GetFrameTime( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
return rb_float_new( object->GetFrameTime() );
}
/* call-seq:
* window.getHeight() -> fixnum
*
* Get the height of the rendering region of the window.
*
* The height doesn't include the titlebar and borders of the window.
*/
static VALUE Window_GetHeight( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
return INT2FIX( object->GetHeight() );
}
/* call-seq:
* window.getInput() -> input
*
* This input gives access to the real-time state of keyboard, mouse and joysticks for this window
*/
static VALUE Window_GetInput( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
VALUE rbData = Data_Wrap_Struct( globalInputClass, 0, 0, const_cast< sf::Input * >( &object->GetInput() ) );
rb_obj_call_init( rbData, 0, 0 );
rb_iv_set( rbData, "@__owner_ref", self );
return rbData;
}
/* call-seq:
* window.getSettings() -> settings
*
* Get the settings of the OpenGL context of the window.
*
* Note that these settings may be different from what was passed to the constructor or the Create() function, if one
* or more settings were not supported. In this case, SFML chose the closest match.
*/
static VALUE Window_GetSettings( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
VALUE rbData = Data_Wrap_Struct( globalContextSettingsClass, 0, 0, const_cast<sf::ContextSettings *>( &object->GetSettings() ) );
rb_obj_call_init( rbData, 0, 0 );
rb_iv_set( rbData, "@__owner_ref", self );
return rbData;
}
/* call-seq:
* window.getWidth() -> fixnum
*
* Get the width of the rendering region of the window.
*
* The width doesn't include the titlebar and borders of the window.
*/
static VALUE Window_GetWidth( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
return INT2FIX( object->GetWidth() );
}
/* call-seq:
* window.isOpened() -> true or false
*
* This function returns whether or not the window exists. Note that a hidden window (Show(false)) will return true.
*/
static VALUE Window_IsOpened( VALUE self )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
if( object->IsOpened() == true )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* window.setActive( activate ) -> true or false
*
* Activate or deactivate the window as the current target for OpenGL rendering.
*
* A window is active only on the current thread, if you want to make it active on another thread you have to
* deactivate it on the previous thread first if it was active. Only one window can be active on a thread at a time,
* thus the window previously active (if any) automatically gets deactivated.
*/
static VALUE Window_SetActive( VALUE self, VALUE anActiveFlag )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
if( anActiveFlag == Qfalse )
{
object->SetActive( false );
}
else if( anActiveFlag == Qtrue )
{
object->SetActive( true );
}
else
{
rb_raise( rb_eTypeError, "Expected true or false" );
}
return Qnil;
}
/* call-seq:
* window.setCursorPosition( new_x, new_y )
*
* Change the position of the mouse cursor.
*/
static VALUE Window_SetCursorPosition( VALUE self, VALUE aX, VALUE aY )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetCursorPosition( FIX2UINT( aX ), FIX2UINT( aY ) );
return Qnil;
}
/* call-seq:
* window.cursorPosition=( vector2 )
*
* Change the position of the mouse cursor.
*/
static VALUE Window_SetCursorPosition2( VALUE self, VALUE anArgument )
{
VALUE argument = Vector2_ForceType( anArgument );
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
VALUE argumentX = Vector2_GetX( argument );
VALUE argumentY = Vector2_GetY( argument );
object->SetCursorPosition( FIX2UINT( argumentX ), FIX2UINT( argumentY ) );
return Qnil;
}
/* call-seq:
* window.setFramerateLimit( new_limit )
*
* Limit the framerate to a maximum fixed frequency.
*
* If a limit is set, the window will use a small delay after each call to Display() to ensure that the current frame
* lasted long enough to match the framerate limit.
*/
static VALUE Window_SetFramerateLimit( VALUE self, VALUE aLimit )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetFramerateLimit( FIX2UINT( aLimit ) );
return Qnil;
}
/* call-seq:
* window.setIcon( width, height, pixels )
*
* Change the window's icon.
*
* pixels must be an array of width x height pixels in 32-bits RGBA format. In the ruby binding the array will be
* flattened so you can have array's up to 3 dimensions(or more) to represent each pixel component. The size of the
* array will be assumed to be width * height * 4.
*
* The OS default icon is used by default.
*
* Usage example:
* pixels = [
* [[255, 0, 0, 255], [0, 0, 255, 255]],
* [[0, 255, 0, 255], [0, 0, 0, 255]]
* ]
*
* window.setIcon( 2, 2, pixels )
*/
static VALUE Window_SetIcon( 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::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetIcon( rawWidth, rawHeight, tempData );
delete[] tempData;
return Qnil;
}
/* call-seq:
* window.setJoystickThreshold( new_threshold )
*
* Change the joystick threshold.
*
* The joystick threshold is the value below which no JoyMoved event will be generated.
*
* The threshold value is 0.1 by default. The threshold has to be in the range 0..100
*/
static VALUE Window_SetJoystickThreshold( VALUE self, VALUE aThreshold )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetJoystickThreshold( rb_float_new( aThreshold ) );
return Qnil;
}
/* call-seq:
* window.setPosition( new_x, new_y )
*
* Change the position of the window on screen.
*
* This function only works for top-level windows (i.e. it will be ignored for windows created from the handle of a
* child window/control).
*/
static VALUE Window_SetPosition( VALUE self, VALUE aX, VALUE aY )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetPosition( FIX2INT( aX ), FIX2INT( aY ) );
return Qnil;
}
/* call-seq:
* window.position=( vector2 )
*
* Change the position of the window on screen.
*
* This function only works for top-level windows (i.e. it will be ignored for windows created from the handle of a
* child window/control).
*/
static VALUE Window_SetPosition2( VALUE self, VALUE anArgument )
{
VALUE argument = Vector2_ForceType( anArgument );
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
VALUE argumentX = Vector2_GetX( argument );
VALUE argumentY = Vector2_GetY( argument );
object->SetPosition( FIX2UINT( argumentX ), FIX2INT( argumentY ) );
return Qnil;
}
/* call-seq:
* window.setSize( new_width, new_height )
*
* Change the size of the rendering region of the window.
*/
static VALUE Window_SetSize( VALUE self, VALUE aWidth, VALUE aHeight )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetSize( FIX2UINT( aWidth ), FIX2UINT( aHeight ) );
return Qnil;
}
/* call-seq:
* window.size=( vector2 )
*
* Change the size of the rendering region of the window.
*/
static VALUE Window_SetSize2( VALUE self, VALUE anArgument )
{
VALUE argument = Vector2_ForceType( anArgument );
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
VALUE argumentX = Vector2_GetX( argument );
VALUE argumentY = Vector2_GetY( argument );
object->SetSize( FIX2UINT( argumentX ), FIX2UINT( argumentY ) );
return Qnil;
}
/* call-seq:
* window.setTitle( new_title )
*
* Change the title of the window.
*/
static VALUE Window_SetTitle( VALUE self, VALUE aTitle )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
object->SetTitle( rb_string_value_cstr( &aTitle ) );
return Qnil;
}
/* call-seq:
* window.show( show )
*
* Show or hide the window.
*
* The window is shown by default.
*/
static VALUE Window_Show( VALUE self, VALUE aShowFlag )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
if( aShowFlag == Qfalse )
{
object->Show( false );
}
else if( aShowFlag == Qtrue )
{
object->Show( true );
}
else
{
rb_raise( rb_eTypeError, "Expected true or false" );
}
return Qnil;
}
/* call-seq:
* window.showMouseCursor( show )
*
* Show or hide the mouse cursor.
*
* The mouse cursor is shown by default.
*/
static VALUE Window_ShowMouseCursor( VALUE self, VALUE aShowFlag )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
if( aShowFlag == Qfalse )
{
object->ShowMouseCursor( false );
}
else if( aShowFlag == Qtrue )
{
object->ShowMouseCursor( true );
}
else
{
rb_raise( rb_eTypeError, "Expected true or false" );
}
return Qnil;
}
/* call-seq:
* window.enableVerticalSync( enabled )
*
* Enable or disable vertical synchronization.
*
* Activating vertical synchronization will limit the number of frames displayed to the refresh rate of the monitor.
* This can avoid some visual artifacts, and limit the framerate to a good value (but not constant across different
* computers).
*
* Vertical synchronization is disabled by default.
*/
static VALUE Window_EnableVerticalSync( VALUE self, VALUE aEnableFlag )
{
sf::Window *object = NULL;
Data_Get_Struct( self, sf::Window, object );
if( aEnableFlag == Qfalse )
{
object->EnableVerticalSync( false );
}
else if( aEnableFlag == Qtrue )
{
object->EnableVerticalSync( true );
}
else
{
rb_raise( rb_eTypeError, "Expected true or false" );
}
return Qnil;
}
/* call-seq:
* window.waitEvent() -> event or nil
*
* Wait for an event and return it.
*
* This function is blocking: if there's no pending event then it will wait until an event is received. After this
* function returns (and no error occured), the event object is always valid and filled properly. This function is
* typically used when you have a thread that is dedicated to events handling: you want to make this thread sleep as
* long as no new event is received.
*/
static VALUE Window_WaitEvent( VALUE self )
{
sf::Event event;
sf::Window *window = NULL;
Data_Get_Struct( self, sf::Window, window );
if( window->WaitEvent( event ) == true )
{
VALUE rbObject = rb_funcall( globalEventClass, rb_intern( "new" ), 1, INT2FIX( event.Type ) );
sf::Event *rubyRawEvent = NULL;
Data_Get_Struct( rbObject, sf::Event, rubyRawEvent );
*rubyRawEvent = event;
return rbObject;
}
else
{
return Qnil;
}
}
/* call-seq:
* Window.new() -> window
* Window.new( mode, title, style = SFML::Style::Default, settings = SFML::ContextSettings.new ) -> window
*
* Construct a new window.
*
* The first form of new doesn't actually create the visual window, use the other form of new or call
* SFML::Window#create to do so.
*
* The second form of new creates the window with the size and pixel depth defined in mode. An optional style can be passed
* to customize the look and behaviour of the window (borders, title bar, resizable, closable, ...). If style contains
* Style::Fullscreen, then mode must be a valid video mode.
*
* The fourth parameter is an optional structure specifying advanced OpenGL context settings such as antialiasing,
* depth-buffer bits, etc. You shouldn't care about these parameters for a regular usage of the graphics module.
*/
static VALUE Window_Initialize( int argc, VALUE *args, VALUE self )
{
if( argc > 0 )
{
rb_funcall2( self, rb_intern( "create" ), argc, args );
}
return self;
}
static VALUE Window_Alloc( VALUE aKlass )
{
sf::Window *object = new sf::Window();
return Data_Wrap_Struct( aKlass, 0, Window_Free, object );
}
void Init_Window( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* SFML::Window is the main class of the Window module.
*
* It defines an OS window that is able to receive an OpenGL rendering.
*
* A SFML::Window can create its own new window, but using an already created window trough a handle is not supported
* in the ruby bindings yet.
*
* The SFML::Window class provides a simple interface for manipulating the window: move, resize, show/hide, control mouse
* cursor, etc. It also provides event handling through its getEvent() function, and real-time state handling with its
* attached SFML::Input object (see getInput()).
*
* Note that OpenGL experts can pass their own parameters (antialiasing level, bits for the depth and stencil buffers,
* etc.) to the OpenGL context attached to the window, with the SFML::ContextSettings structure which is passed as an
* optional argument when creating the window.
*
* Usage example:
*
* # Declare and create a new window
* window = SFML::Window.new( SFML::VideoMode.new( 800, 600 ), "SFML window" )
*
* # Limit the framerate to 60 frames per second (this step is optional)
* window.setFramerateLimit( 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
*
* # Activate the window for OpenGL rendering
* window.setActive()
*
* # OpenGL drawing commands go here...
*
* # End the current frame and display its contents on screen
* window.display()
* end
*/
globalWindowClass = rb_define_class_under( sfml, "Window", rb_cObject );
rb_include_module( globalWindowClass, globalNonCopyableModule );
// Class methods
//rb_define_singleton_method( globalWindowClass, "new", Window_New , -1 );
rb_define_alloc_func( globalWindowClass, Window_Alloc );
// Instance methods
rb_define_method( globalWindowClass, "initialize", Window_Initialize, -1 );
rb_define_method( globalWindowClass, "close", Window_Close, 0 );
rb_define_method( globalWindowClass, "create", Window_Create, -1 );
rb_define_method( globalWindowClass, "display", Window_Display, 0 );
rb_define_method( globalWindowClass, "enableKeyRepeat", Window_EnableKeyRepeat, 1 );
rb_define_method( globalWindowClass, "getEvent", Window_GetEvent, 0 );
rb_define_method( globalWindowClass, "getFrameTime", Window_GetFrameTime , 0 );
rb_define_method( globalWindowClass, "getHeight", Window_GetHeight, 0 );
rb_define_method( globalWindowClass, "getInput", Window_GetInput, 0 );
rb_define_method( globalWindowClass, "getSettings", Window_GetSettings, 0 );
rb_define_method( globalWindowClass, "getWidth", Window_GetWidth, 0 );
rb_define_method( globalWindowClass, "isOpened", Window_IsOpened, 0 );
rb_define_method( globalWindowClass, "setActive", Window_SetActive, 1 );
rb_define_method( globalWindowClass, "setCursorPosition", Window_SetCursorPosition, 2 );
rb_define_method( globalWindowClass, "cursorPosition=", Window_SetCursorPosition2, 1 );
rb_define_method( globalWindowClass, "setFramerateLimit", Window_SetFramerateLimit, 1 );
rb_define_method( globalWindowClass, "setIcon", Window_SetIcon, 3 );
rb_define_method( globalWindowClass, "setJoystickThreshold", Window_SetJoystickThreshold, 1 );
rb_define_method( globalWindowClass, "setPosition", Window_SetPosition, 2 );
rb_define_method( globalWindowClass, "position=", Window_SetPosition2, 1 );
rb_define_method( globalWindowClass, "setSize", Window_SetSize, 2 );
rb_define_method( globalWindowClass, "size=", Window_SetSize2, 1 );
rb_define_method( globalWindowClass, "setTitle", Window_SetTitle, 1 );
rb_define_method( globalWindowClass, "show", Window_Show, 1 );
rb_define_method( globalWindowClass, "showMouseCursor", Window_ShowMouseCursor, 1 );
rb_define_method( globalWindowClass, "enableVerticalSync", Window_EnableVerticalSync, 1 );
rb_define_method( globalWindowClass, "waitEvent", Window_WaitEvent, 0 );
// Aliases
rb_define_alias( globalWindowClass, "keyRepeat=", "enableKeyRepeat" );
rb_define_alias( globalWindowClass, "key_repeat=", "enableKeyRepeat" );
rb_define_alias( globalWindowClass, "get_event", "getEvent" );
rb_define_alias( globalWindowClass, "frameTime", "getFrameTime" );
rb_define_alias( globalWindowClass, "frame_time", "getFrameTime" );
rb_define_alias( globalWindowClass, "height", "getHeight" );
rb_define_alias( globalWindowClass, "input", "getInput" );
rb_define_alias( globalWindowClass, "settings", "getSettings" );
rb_define_alias( globalWindowClass, "width", "getWidth" );
rb_define_alias( globalWindowClass, "opened?", "isOpened" );
rb_define_alias( globalWindowClass, "open?", "isOpened" );
rb_define_alias( globalWindowClass, "active=", "setActive" );
rb_define_alias( globalWindowClass, "set_cursor_position", "setCursorPosition" );
rb_define_alias( globalWindowClass, "cursor_position=", "cursorPosition=" );
rb_define_alias( globalWindowClass, "framerateLimit=", "setFramerateLimit" );
rb_define_alias( globalWindowClass, "framerate_limit=", "setFramerateLimit" );
rb_define_alias( globalWindowClass, "framerate=", "setFramerateLimit" );
rb_define_alias( globalWindowClass, "set_icon", "setIcon" );
rb_define_alias( globalWindowClass, "joystickThreshold=", "setJoystickThreshold" );
rb_define_alias( globalWindowClass, "joystick_threshold=", "setJoystickThreshold" );
rb_define_alias( globalWindowClass, "set_position", "setPosition" );
rb_define_alias( globalWindowClass, "set_size", "setSize" );
rb_define_alias( globalWindowClass, "title=", "setTitle" );
rb_define_alias( globalWindowClass, "showMouseCursor=", "showMouseCursor" );
rb_define_alias( globalWindowClass, "show_mouse_cursor", "showMouseCursor" );
rb_define_alias( globalWindowClass, "show_mouse_cursor=", "showMouseCursor" );
rb_define_alias( globalWindowClass, "enableVerticalSync=", "enableVerticalSync" );
rb_define_alias( globalWindowClass, "enable_vertical_sync", "enableVerticalSync" );
rb_define_alias( globalWindowClass, "enable_vertical_sync=", "enableVerticalSync" );
rb_define_alias( globalWindowClass, "vertical_sync=", "enableVerticalSync" );
rb_define_alias( globalWindowClass, "wait_event", "waitEvent" );
}

View File

@ -1,31 +0,0 @@
/* 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_WINDOW_HEADER_
#define SFML_RUBYEXT_WINDOW_HEADER_
#include "ruby.h"
// Ruby initiation function
void Init_Window( void );
#endif // SFML_RUBYEXT_WINDOW_HEADER_

View File

@ -1,182 +0,0 @@
/* 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 "main.hpp"
#include "Vector2.hpp"
#include "NonCopyable.hpp"
#include "Context.hpp"
#include "ContextSettings.hpp"
#include "Event.hpp"
#include "Input.hpp"
#include "VideoMode.hpp"
#include "Window.hpp"
#include <SFML/Window.hpp>
#include <iostream>
/* SFML Namespace which contains all classes in this module. */
VALUE globalKeyNamespace;
VALUE globalMouseNamespace;
VALUE globalJoyNamespace;
VALUE globalStyleNamespace;
extern VALUE globalVector2Class;
extern VALUE globalNonCopyableModule;
static const char * keyNamesMisc[] =
{
"Escape", "LControl", "LShift", "LAlt", "LSystem", "RControl", "RShift", "RAlt", "RSystem",
"Menu", "LBracket", "RBracket", "SemiColon", "Comma", "Period", "Quote", "Slash",
"BackSlash", "Tilde", "Equal", "Dash", "Space", "Return", "Back", "Tab", "PageUp",
"PageDown", "End", "Home", "Insert", "Delete", "Add", "Subtract", "Multiply",
"Divide", "Left", "Right", "Up", "Down", "Numpad0", "Numpad1", "Numpad2", "Numpad3",
"Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9", "F1", "F2", "F3",
"F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "Pause",
"Count"
};
/* Definition of key codes for keyboard events.
*
* All SFML::Key constants exists, I just haven't written them all so Rdoc can interpret them yet.
*/
static void CreateKeyEnum( void )
{
globalKeyNamespace = rb_define_module_under( globalSFMLNamespace, "Key" );
rb_define_const( globalKeyNamespace, "A", INT2FIX( sf::Key::A ) );
rb_define_const( globalKeyNamespace, "B", INT2FIX( sf::Key::B ) );
rb_define_const( globalKeyNamespace, "C", INT2FIX( sf::Key::C ) );
rb_define_const( globalKeyNamespace, "D", INT2FIX( sf::Key::D ) );
rb_define_const( globalKeyNamespace, "E", INT2FIX( sf::Key::E ) );
rb_define_const( globalKeyNamespace, "F", INT2FIX( sf::Key::F ) );
rb_define_const( globalKeyNamespace, "G", INT2FIX( sf::Key::G ) );
rb_define_const( globalKeyNamespace, "H", INT2FIX( sf::Key::H ) );
rb_define_const( globalKeyNamespace, "I", INT2FIX( sf::Key::I ) );
rb_define_const( globalKeyNamespace, "J", INT2FIX( sf::Key::J ) );
rb_define_const( globalKeyNamespace, "K", INT2FIX( sf::Key::K ) );
rb_define_const( globalKeyNamespace, "L", INT2FIX( sf::Key::L ) );
rb_define_const( globalKeyNamespace, "M", INT2FIX( sf::Key::M ) );
rb_define_const( globalKeyNamespace, "N", INT2FIX( sf::Key::N ) );
rb_define_const( globalKeyNamespace, "O", INT2FIX( sf::Key::O ) );
rb_define_const( globalKeyNamespace, "P", INT2FIX( sf::Key::P ) );
rb_define_const( globalKeyNamespace, "Q", INT2FIX( sf::Key::Q ) );
rb_define_const( globalKeyNamespace, "R", INT2FIX( sf::Key::R ) );
rb_define_const( globalKeyNamespace, "S", INT2FIX( sf::Key::S ) );
rb_define_const( globalKeyNamespace, "T", INT2FIX( sf::Key::T ) );
rb_define_const( globalKeyNamespace, "U", INT2FIX( sf::Key::U ) );
rb_define_const( globalKeyNamespace, "W", INT2FIX( sf::Key::W ) );
rb_define_const( globalKeyNamespace, "V", INT2FIX( sf::Key::V ) );
rb_define_const( globalKeyNamespace, "X", INT2FIX( sf::Key::X ) );
rb_define_const( globalKeyNamespace, "Y", INT2FIX( sf::Key::Y ) );
rb_define_const( globalKeyNamespace, "Z", INT2FIX( sf::Key::Z ) );
rb_define_const( globalKeyNamespace, "Num0", INT2FIX( sf::Key::Num0 ) );
rb_define_const( globalKeyNamespace, "Num1", INT2FIX( sf::Key::Num1 ) );
rb_define_const( globalKeyNamespace, "Num2", INT2FIX( sf::Key::Num2 ) );
rb_define_const( globalKeyNamespace, "Num3", INT2FIX( sf::Key::Num3 ) );
rb_define_const( globalKeyNamespace, "Num4", INT2FIX( sf::Key::Num4 ) );
rb_define_const( globalKeyNamespace, "Num5", INT2FIX( sf::Key::Num5 ) );
rb_define_const( globalKeyNamespace, "Num6", INT2FIX( sf::Key::Num6 ) );
rb_define_const( globalKeyNamespace, "Num7", INT2FIX( sf::Key::Num7 ) );
rb_define_const( globalKeyNamespace, "Num8", INT2FIX( sf::Key::Num8 ) );
rb_define_const( globalKeyNamespace, "Num9", INT2FIX( sf::Key::Num9 ) );
for( int index = static_cast< int >( sf::Key::Escape ); index <= sf::Key::Count; index++ )
{
rb_define_const( globalKeyNamespace, keyNamesMisc[ index - sf::Key::Escape ], INT2FIX( index ) );
}
}
/* Definition of button codes for mouse events. */
static void CreateMouseEnum( void )
{
globalMouseNamespace = rb_define_module_under( globalSFMLNamespace, "Mouse" );
rb_define_const( globalMouseNamespace, "Left", INT2FIX( sf::Mouse::Left ) );
rb_define_const( globalMouseNamespace, "Right", INT2FIX( sf::Mouse::Right ) );
rb_define_const( globalMouseNamespace, "Middle", INT2FIX( sf::Mouse::Middle ) );
rb_define_const( globalMouseNamespace, "XButton1", INT2FIX( sf::Mouse::XButton1 ) );
rb_define_const( globalMouseNamespace, "XButton2", INT2FIX( sf::Mouse::XButton2 ) );
rb_define_const( globalMouseNamespace, "ButtonCount", INT2FIX( sf::Mouse::ButtonCount ) );
}
/* Definition of joystick axis for joystick events. */
static void CreateJoyEnum( void )
{
globalJoyNamespace = rb_define_module_under( globalSFMLNamespace, "Joy" );
rb_define_const( globalJoyNamespace, "AxisX", INT2FIX( sf::Joy::AxisX ) );
rb_define_const( globalJoyNamespace, "AxisY", INT2FIX( sf::Joy::AxisY ) );
rb_define_const( globalJoyNamespace, "AxisZ", INT2FIX( sf::Joy::AxisZ ) );
rb_define_const( globalJoyNamespace, "AxisR", INT2FIX( sf::Joy::AxisR ) );
rb_define_const( globalJoyNamespace, "AxisU", INT2FIX( sf::Joy::AxisU ) );
rb_define_const( globalJoyNamespace, "AxisV", INT2FIX( sf::Joy::AxisV ) );
rb_define_const( globalJoyNamespace, "AxisPOV", INT2FIX( sf::Joy::AxisPOV ) );
rb_define_const( globalJoyNamespace, "AxisCount", INT2FIX( sf::Joy::AxisCount ) );
}
/* Enumeration of the window styles. */
static void CreateStyleEnum( void )
{
globalStyleNamespace = rb_define_module_under( globalSFMLNamespace, "Style" );
rb_define_const( globalStyleNamespace, "None", INT2FIX( sf::Style::None ) );
rb_define_const( globalStyleNamespace, "Titlebar", INT2FIX( sf::Style::Titlebar ) );
rb_define_const( globalStyleNamespace, "Resize", INT2FIX( sf::Style::Resize ) );
rb_define_const( globalStyleNamespace, "Close", INT2FIX( sf::Style::Close ) );
rb_define_const( globalStyleNamespace, "Fullscreen", INT2FIX( sf::Style::Fullscreen ) );
rb_define_const( globalStyleNamespace, "Default", INT2FIX( sf::Style::Default ) );
}
static bool CheckDependencies( void )
{
if( rb_cvar_defined( globalSFMLNamespace, rb_intern( "SystemLoaded" ) ) == Qtrue )
{
return true;
}
return false;
}
void Init_window( void )
{
/* SFML namespace which contains the classes of this module. */
globalSFMLNamespace = rb_define_module( "SFML" );
if( CheckDependencies() == false )
{
rb_raise( rb_eRuntimeError, "This module depends on sfml-system" );
}
globalVector2Class = rb_define_class_under(globalSFMLNamespace, "Vector2", rb_cObject );
globalNonCopyableModule = rb_define_module_under(globalSFMLNamespace, "NonCopyable");
rb_define_const( globalSFMLNamespace, "WindowLoaded", Qtrue );
CreateKeyEnum();
CreateMouseEnum();
CreateJoyEnum();
CreateStyleEnum();
Init_Context();
Init_ContextSettings();
Init_Event();
Init_Input();
Init_VideoMode();
Init_Window();
}

View File

@ -1,32 +0,0 @@
/* 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_WINDOW_MAIN_HEADER_
#define SFML_RUBYEXT_WINDOW_MAIN_HEADER_
#include "ruby.h"
#include "global.hpp"
// Ruby initiation function
extern "C" void Init_window( void );
#endif // SFML_RUBYEXT_WINDOW_MAIN_HEADER_

View File

@ -1,65 +0,0 @@
/* 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 "NonCopyable.hpp"
#include "global.hpp"
VALUE globalNonCopyableModule;
/* call-seq:
* non_copyable.clone( source ) -> copy
* non_copyable.dup( source ) -> copy
*
* Override the copy methods so that we can't copy these instances..
*/
static VALUE NonCopyable_InitializeCopy( VALUE self, VALUE aSource)
{
rb_raise( rb_eRuntimeError, "The object you tried to copy is NonCopyable" );
return self;
}
void Init_NonCopyable( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* Utility mixin-module that makes any derived class non-copyable.
* This module makes its instances non-copyable, by explicitely disabling its initialize_copy method.
*
* To create a non-copyable class, simply include the SFML::NonCopyable module.
*
* Usage example:
*
* class MyNonCopyableClass
* include SFML::NonCopyable
* ...
* end
* Deciding whether the instances of a class can be copied or not is a very important design choice. You are strongly
* encouraged to think about it before writing a class, and to use SFML::NonCopyable when necessary to prevent many
* potential future errors when using it. This is also a very important indication to users of your class.
*/
globalNonCopyableModule = rb_define_module_under( sfml, "NonCopyable" );
// Instance methods
rb_define_method( globalNonCopyableModule, "initialize_copy", NonCopyable_InitializeCopy, 1 );
}

View File

@ -1,32 +0,0 @@
/* 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_NON_COPYABLE_HEADER_
#define SFML_RUBYEXT_NON_COPYABLE_HEADER_
#include "ruby.h"
extern VALUE globalNonCopyableModule;
void Init_NonCopyable( void );
#endif // SFML_RUBYEXT_NON_COPYABLE_HEADER_

View File

@ -1,294 +0,0 @@
/* 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 "Vector2.hpp"
#include "global.hpp"
VALUE globalVector2Class;
VALUE Vector2_GetX( VALUE self )
{
static ID idX = rb_intern( "x" );
return rb_funcall( self, idX, 0 );
}
VALUE Vector2_GetY( VALUE self )
{
static ID idY = rb_intern( "y" );
return rb_funcall( self, idY, 0 );
}
VALUE Vector2_SetX( VALUE self, VALUE aVal )
{
static ID idX = rb_intern( "x=" );
return rb_funcall( self, idX, 1, aVal );
}
VALUE Vector2_SetY( VALUE self, VALUE aVal )
{
static ID idY = rb_intern( "y=" );
return rb_funcall( self, idY, 1, aVal );
}
/* Internal function
* Forces the argument someValue to be a Vector2. IF it can convert it then it will.
* So you can always safely asume that this function returns a Vector2 object.
* If it fails then an exception will be thrown.
*/
VALUE Vector2_ForceType( VALUE someValue )
{
if( rb_obj_is_kind_of( someValue, rb_cArray ) == Qtrue )
{
VALUE arg1 = rb_ary_entry( someValue, 0 );
VALUE arg2 = rb_ary_entry( someValue, 1 );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, arg1, arg2 );
}
else if( rb_obj_is_kind_of( someValue, globalVector2Class ) == Qtrue )
{
return someValue;
}
else
{
rb_raise( rb_eRuntimeError, "expected Array or Vector2" );
}
}
/* Internal function
* Will copy the x and y from aSource to self.
*/
static void Vector2_internal_CopyFrom( VALUE self, VALUE aSource )
{
VALUE vectorSource = Vector2_ForceType( aSource );
VALUE x = Vector2_GetX( self );
VALUE y = Vector2_GetY( self );
Vector2_SetX( self, x );
Vector2_SetY( self, y );
rb_iv_set( self, "@dataType", rb_iv_get( vectorSource, "@dataType" ) );
}
/* Internal function
* Validate that the passed types are the same and numeric.
*/
static void Vector2_internal_ValidateTypes( VALUE aFirst, VALUE aSecond )
{
if( CLASS_OF( aFirst ) != CLASS_OF( aSecond ) )
{
rb_raise( rb_eRuntimeError, "x and y must be of same type" );
}
if( rb_obj_is_kind_of( aFirst, rb_cNumeric ) == Qfalse )
{
rb_raise( rb_eRuntimeError, "x and y must be numeric!" );
}
}
/* */
static VALUE Vector2_Negate( VALUE self )
{
VALUE x = Vector2_GetX( self );
VALUE y = Vector2_GetY( self );
VALUE negatedX = rb_funcall( x, rb_intern( "-@" ), 0 );
VALUE negatedY = rb_funcall( y, rb_intern( "-@" ), 0 );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, negatedX, negatedY );
}
/* */
static VALUE Vector2_Add( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector2_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector2_GetX( self );
VALUE leftY = Vector2_GetY( self );
VALUE rightX = Vector2_GetX( rightVector );
VALUE rightY = Vector2_GetY( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "+" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "+" ), 1, rightY );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
}
/* */
static VALUE Vector2_Subtract( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector2_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector2_GetX( self );
VALUE leftY = Vector2_GetY( self );
VALUE rightX = Vector2_GetX( rightVector );
VALUE rightY = Vector2_GetY( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "-" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "-" ), 1, rightY );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
}
/* */
static VALUE Vector2_Multiply( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector2_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector2_GetX( self );
VALUE leftY = Vector2_GetY( self );
VALUE rightX = Vector2_GetX( rightVector );
VALUE rightY = Vector2_GetY( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "*" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "*" ), 1, rightY );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
}
/* */
static VALUE Vector2_Divide( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector2_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector2_GetX( self );
VALUE leftY = Vector2_GetY( self );
VALUE rightX = Vector2_GetX( rightVector );
VALUE rightY = Vector2_GetY( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "/" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "/" ), 1, rightY );
return rb_funcall( globalVector2Class, rb_intern( "new" ), 2, newX, newY );
}
/* */
static VALUE Vector2_Equal( VALUE self, VALUE anArgument )
{
VALUE aVector = Vector2_ForceType( anArgument );
VALUE leftX = Vector2_GetX( self );
VALUE leftY = Vector2_GetY( self );
VALUE rightX = Vector2_GetX( aVector );
VALUE rightY = Vector2_GetY( aVector );
if( rb_funcall( leftX, rb_intern( "==" ), 1, rightX ) == Qtrue &&
rb_funcall( leftY, rb_intern( "==" ), 1, rightY ) == Qtrue )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* */
static VALUE Vector2_StrictEqual( VALUE self, VALUE anArgument )
{
VALUE aVector = Vector2_ForceType( anArgument );
VALUE leftX = Vector2_GetX( self );
VALUE leftY = Vector2_GetY( self );
VALUE rightX = Vector2_GetX( aVector );
VALUE rightY = Vector2_GetY( aVector );
if( rb_funcall( leftX, rb_intern( "eql?" ), 1, rightX ) == Qtrue &&
rb_funcall( leftY, rb_intern( "eql?" ), 1, rightY ) == Qtrue )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* Vector2.new() -> vector
* Vector2.new([x,y]) -> vector
* Vector2.new(vector) -> vector
* Vector2.new(x,y) -> vector
*
* Create a new vector instance.
*/
static VALUE Vector2_Initialize( VALUE self, VALUE someArgs )
{
long arrayLength = RARRAY_LEN( someArgs );
rb_iv_set( self, "@x", INT2NUM( 0 ) );
rb_iv_set( self, "@y", INT2NUM( 0 ) );
if( arrayLength == 0 )
{
// Nothing needs to be done
}
else if( arrayLength == 1 )
{
Vector2_internal_CopyFrom( self, rb_ary_entry( someArgs, 0 ) );
}
else if( arrayLength == 2 )
{
VALUE arg1 = rb_ary_entry( someArgs, 0 );
VALUE arg2 = rb_ary_entry( someArgs, 1 );
Vector2_internal_ValidateTypes( arg1, arg2 );
rb_iv_set( self, "@x", arg1 );
rb_iv_set( self, "@y", arg2 );
}
rb_iv_set( self, "@dataType", CLASS_OF( rb_iv_get( self, "@x" ) ) );
return self;
}
void Init_Vector2( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* SFML::Vector2 is a simple class that defines a mathematical vector with two coordinates (x and y).
*
* It can be used to represent anything that has two dimensions: a size, a point, a velocity, etc.
*
* This class differs from the C++ version. It will accept any value that is Numeric and both x and y must be of the same class.
*
* v1 = SFML::Vector2.new(16.5, 24.0)
* v1.x = 18.2
* y = v1.y
*
* v2 = v1 * v1;
* v3 = SFML::Vector2.new
* v3 = v1 + v2
*
* different = (v2 != v3);
*/
globalVector2Class = rb_define_class_under( sfml, "Vector2", rb_cObject );
// Instance methods
rb_define_method( globalVector2Class, "initialize", Vector2_Initialize, -2 );
rb_define_method( globalVector2Class, "eql?", Vector2_StrictEqual, 1 );
// Instance operators
rb_define_method( globalVector2Class, "-@", Vector2_Negate, 0 );
rb_define_method( globalVector2Class, "+", Vector2_Add, 1 );
rb_define_method( globalVector2Class, "-", Vector2_Subtract, 1 );
rb_define_method( globalVector2Class, "*", Vector2_Multiply, 1 );
rb_define_method( globalVector2Class, "/", Vector2_Divide, 1 );
rb_define_method( globalVector2Class, "==", Vector2_Equal, 1 );
// Attribute accessors
rb_define_attr( globalVector2Class, "x", 1, 1 );
rb_define_attr( globalVector2Class, "y", 1, 1 );
}

View File

@ -1,40 +0,0 @@
/* 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_VECTOR2_HEADER_
#define SFML_RUBYEXT_VECTOR2_HEADER_
#include "ruby.h"
extern VALUE globalVector2Class;
VALUE Vector2_GetX( VALUE self );
VALUE Vector2_GetY( VALUE self );
VALUE Vector2_SetX( VALUE self, VALUE aVal );
VALUE Vector2_SetY( VALUE self, VALUE aVal );
VALUE Vector2_ForceType( VALUE someValue );
void Init_Vector2( void );
#endif // SFML_RUBYEXT_VECTOR2_HEADER_

View File

@ -1,331 +0,0 @@
/* 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 "Vector3.hpp"
#include "global.hpp"
VALUE globalVector3Class;
VALUE Vector3_GetX( VALUE self )
{
static ID idX = rb_intern( "x" );
return rb_funcall( self, idX, 0 );
}
VALUE Vector3_GetY( VALUE self )
{
static ID idY = rb_intern( "y" );
return rb_funcall( self, idY, 0 );
}
VALUE Vector3_GetZ( VALUE self )
{
static ID idZ = rb_intern( "z" );
return rb_funcall( self, idZ, 0 );
}
VALUE Vector3_SetX( VALUE self, VALUE aVal )
{
static ID idX = rb_intern( "x=" );
return rb_funcall( self, idX, 1, aVal );
}
VALUE Vector3_SetY( VALUE self, VALUE aVal )
{
static ID idY = rb_intern( "y=" );
return rb_funcall( self, idY, 1, aVal );
}
VALUE Vector3_SetZ( VALUE self, VALUE aVal )
{
static ID idZ = rb_intern( "z=" );
return rb_funcall( self, idZ, 1, aVal );
}
/* Internal function
* Forces the argument someValue to be a Vector3. IF it can convert it then it will.
* So you can always safely asume that this function returns a Vector3 object.
* If it fails then an exception will be thrown.
*/
VALUE Vector3_ForceType( VALUE someValue )
{
if( rb_obj_is_kind_of( someValue, rb_cArray ) == Qtrue )
{
VALUE arg1 = rb_ary_entry( someValue, 0 );
VALUE arg2 = rb_ary_entry( someValue, 1 );
VALUE arg3 = rb_ary_entry( someValue, 2 );
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, arg1, arg2, arg3 );
}
else if( rb_obj_is_kind_of( someValue, globalVector3Class ) == Qtrue )
{
return someValue;
}
else
{
rb_raise( rb_eRuntimeError, "expected Array or Vector3" );
}
}
/* Internal function
* Will copy the x, y and z from aSource to self.
*/
static void Vector3_internal_CopyFrom( VALUE self, VALUE aSource )
{
VALUE vectorSource = Vector3_ForceType( aSource );
VALUE x = Vector3_GetX( self );
VALUE y = Vector3_GetY( self );
VALUE z = Vector3_GetZ( self );
Vector3_SetX( self, x );
Vector3_SetY( self, y );
Vector3_SetZ( self, z );
rb_iv_set( self, "@dataType", rb_iv_get( vectorSource, "@dataType" ) );
}
/* Internal function
* Validate that the passed values types are the same and numeric.
*/
static void Vector3_internal_ValidateTypes( VALUE aFirst, VALUE aSecond, VALUE aThird )
{
if( CLASS_OF( aFirst ) != CLASS_OF( aSecond ) && CLASS_OF( aFirst ) != CLASS_OF( aThird ) )
{
rb_raise( rb_eRuntimeError, "x, y and z must be of same type" );
}
if( rb_obj_is_kind_of( aFirst, rb_cNumeric ) == Qfalse )
{
rb_raise( rb_eRuntimeError, "x, y and z must be numeric!" );
}
}
/* */
static VALUE Vector3_Negate( VALUE self )
{
VALUE x = Vector3_GetX( self );
VALUE y = Vector3_GetY( self );
VALUE z = Vector3_GetZ( self );
VALUE negatedX = rb_funcall( x, rb_intern( "-@" ), 0 );
VALUE negatedY = rb_funcall( y, rb_intern( "-@" ), 0 );
VALUE negatedZ = rb_funcall( z, rb_intern( "-@" ), 0 );
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, negatedX, negatedY, negatedZ );
}
/* */
static VALUE Vector3_Add( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector3_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector3_GetX( self );
VALUE leftY = Vector3_GetY( self );
VALUE leftZ = Vector3_GetZ( self );
VALUE rightX = Vector3_GetX( rightVector );
VALUE rightY = Vector3_GetY( rightVector );
VALUE rightZ = Vector3_GetZ( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "+" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "+" ), 1, rightY );
VALUE newZ = rb_funcall( leftZ, rb_intern( "+" ), 1, rightZ );
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, newX, newY, newZ );
}
/* */
static VALUE Vector3_Subtract( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector3_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector3_GetX( self );
VALUE leftY = Vector3_GetY( self );
VALUE leftZ = Vector3_GetZ( self );
VALUE rightX = Vector3_GetX( rightVector );
VALUE rightY = Vector3_GetY( rightVector );
VALUE rightZ = Vector3_GetZ( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "-" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "-" ), 1, rightY );
VALUE newZ = rb_funcall( leftZ, rb_intern( "-" ), 1, rightZ );
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, newX, newY, newZ );
}
/* */
static VALUE Vector3_Multiply( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector3_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector3_GetX( self );
VALUE leftY = Vector3_GetY( self );
VALUE leftZ = Vector3_GetZ( self );
VALUE rightX = Vector3_GetX( rightVector );
VALUE rightY = Vector3_GetY( rightVector );
VALUE rightZ = Vector3_GetZ( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "*" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "*" ), 1, rightY );
VALUE newZ = rb_funcall( leftZ, rb_intern( "*" ), 1, rightZ );
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, newX, newY, newZ );
}
/* */
static VALUE Vector3_Divide( VALUE self, VALUE aRightOperand )
{
VALUE rightVector = Vector3_ForceType( aRightOperand );
// Get values
VALUE leftX = Vector3_GetX( self );
VALUE leftY = Vector3_GetY( self );
VALUE leftZ = Vector3_GetZ( self );
VALUE rightX = Vector3_GetX( rightVector );
VALUE rightY = Vector3_GetY( rightVector );
VALUE rightZ = Vector3_GetZ( rightVector );
// Do calculation
VALUE newX = rb_funcall( leftX, rb_intern( "/" ), 1, rightX );
VALUE newY = rb_funcall( leftY, rb_intern( "/" ), 1, rightY );
VALUE newZ = rb_funcall( leftZ, rb_intern( "/" ), 1, rightZ );
return rb_funcall( globalVector3Class, rb_intern( "new" ), 3, newX, newY, newZ );
}
/* */
static VALUE Vector3_Equal( VALUE self, VALUE anArgument )
{
VALUE aVector = Vector3_ForceType( anArgument );
VALUE leftX = Vector3_GetX( self );
VALUE leftY = Vector3_GetY( self );
VALUE leftZ = Vector3_GetZ( self );
VALUE rightX = Vector3_GetX( aVector );
VALUE rightY = Vector3_GetY( aVector );
VALUE rightZ = Vector3_GetZ( aVector );
if( rb_funcall( leftX, rb_intern( "==" ), 1, rightX ) == Qtrue &&
rb_funcall( leftY, rb_intern( "==" ), 1, rightY ) == Qtrue &&
rb_funcall( leftZ, rb_intern( "==" ), 1, rightZ ) == Qtrue )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* */
static VALUE Vector3_StrictEqual( VALUE self, VALUE anArgument )
{
VALUE aVector = Vector3_ForceType( anArgument );
VALUE leftX = Vector3_GetX( self );
VALUE leftY = Vector3_GetY( self );
VALUE leftZ = Vector3_GetZ( self );
VALUE rightX = Vector3_GetX( aVector );
VALUE rightY = Vector3_GetY( aVector );
VALUE rightZ = Vector3_GetZ( aVector );
if( rb_funcall( leftX, rb_intern( "eql?" ), 1, rightX ) == Qtrue &&
rb_funcall( leftY, rb_intern( "eql?" ), 1, rightY ) == Qtrue &&
rb_funcall( leftZ, rb_intern( "eql?" ), 1, rightZ ) == Qtrue )
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/* call-seq:
* Vector3.new() -> vector
* Vector3.new([x,y,z]) -> vector
* Vector3.new(vector) -> vector
* Vector3.new(x,y,z) -> vector
*
* Create a new vector instance.
*/
static VALUE Vector3_Initialize( VALUE self, VALUE someArgs )
{
long arrayLength = RARRAY_LEN( someArgs );
rb_iv_set( self, "@x", INT2NUM( 0 ) );
rb_iv_set( self, "@y", INT2NUM( 0 ) );
rb_iv_set( self, "@z", INT2NUM( 0 ) );
if( arrayLength == 0 )
{
// Nothing needs to be done
}
else if( arrayLength == 1 )
{
Vector3_internal_CopyFrom( self, rb_ary_entry( someArgs, 0 ) );
}
else if( arrayLength == 3 )
{
VALUE arg1 = rb_ary_entry( someArgs, 0 );
VALUE arg2 = rb_ary_entry( someArgs, 1 );
VALUE arg3 = rb_ary_entry( someArgs, 1 );
Vector3_internal_ValidateTypes( arg1, arg2, arg3 );
rb_iv_set( self, "@x", arg1 );
rb_iv_set( self, "@y", arg2 );
rb_iv_set( self, "@z", arg3 );
}
rb_iv_set( self, "@dataType", CLASS_OF( rb_iv_get( self, "@x" ) ) );
return self;
}
void Init_Vector3( void )
{
/* SFML namespace which contains the classes of this module. */
VALUE sfml = rb_define_module( "SFML" );
/* SFML::Vector3 is a simple class that defines a mathematical vector with three coordinates (x, y and z).
*
* It can be used to represent anything that has three dimensions: a size, a point, a velocity, etc.
*
* This class differs from the C++ version. It will accept any value that is Numeric and both x, y an z must be of the same class.
*
* v1 = SFML::Vector3.new(16.5, 24.0, -8.2)
* v1.z = 18.2
* y = v1.y
*
* v2 = v1 * v1;
* v3 = SFML::Vector3.new
* v3 = v1 + v2
*
* different = (v2 != v3);
*/
globalVector3Class = rb_define_class_under( sfml, "Vector3", rb_cObject );
// Instance methods
rb_define_method( globalVector3Class, "initialize", Vector3_Initialize, -2 );
rb_define_method( globalVector3Class, "eql?", Vector3_StrictEqual, 1 );
// Instance operators
rb_define_method( globalVector3Class, "-@", Vector3_Negate, 0 );
rb_define_method( globalVector3Class, "+", Vector3_Add, 1 );
rb_define_method( globalVector3Class, "-", Vector3_Subtract, 1 );
rb_define_method( globalVector3Class, "*", Vector3_Multiply, 1 );
rb_define_method( globalVector3Class, "/", Vector3_Divide, 1 );
rb_define_method( globalVector3Class, "==", Vector3_Equal, 1 );
// Attribute accessors
rb_define_attr( globalVector3Class, "x", 1, 1 );
rb_define_attr( globalVector3Class, "y", 1, 1 );
rb_define_attr( globalVector3Class, "z", 1, 1 );
}

View File

@ -1,42 +0,0 @@
/* 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_VECTOR3_HEADER_
#define SFML_RUBYEXT_VECTOR3_HEADER_
#include "ruby.h"
extern VALUE globalVector3Class;
VALUE Vector3_GetX( VALUE self );
VALUE Vector3_GetY( VALUE self );
VALUE Vector3_GetZ( VALUE self );
VALUE Vector3_SetX( VALUE self, VALUE aVal );
VALUE Vector3_SetY( VALUE self, VALUE aVal );
VALUE Vector3_SetZ( VALUE self, VALUE aVal );
VALUE Vector3_ForceType( VALUE someValue );
void Init_Vector3( void );
#endif // SFML_RUBYEXT_VECTOR3_HEADER_

View File

@ -1,36 +0,0 @@
/* 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 "global.hpp"
VALUE globalSFMLNamespace;
VALUE RetrieveSFMLClass( const char * aName )
{
ID name = rb_intern( aName );
if( rb_cvar_defined( globalSFMLNamespace, name ) == Qfalse )
{
rb_raise( rb_eRuntimeError, "This module depends on SFML::%s", aName );
}
return rb_cvar_get( globalSFMLNamespace, name );
}

View File

@ -1,49 +0,0 @@
/* 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_GLOBAL_HEADER_
#define SFML_RUBYEXT_GLOBAL_HEADER_
#include "ruby.h"
#define SFML_DYNAMIC
extern VALUE globalSFMLNamespace;
VALUE RetrieveSFMLClass( const char * aName );
typedef VALUE ( *RubyFunctionPtr )( ... );
#define MAX( x, y ) ( ( x ) < ( y ) ? ( y ) : ( x ) )
#define MIN( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
#define VALIDATE_CLASS( variable, type, name ) \
if( rb_obj_is_kind_of( variable, type ) != Qtrue ) \
{ \
rb_raise( rb_eTypeError, "%s argument must be instance of %s", name, rb_string_value_cstr ( &type ) ); \
}
#define rb_define_module_function( klass, name, func, argc, ... ) rb_define_module_function( klass, name, reinterpret_cast< RubyFunctionPtr >( func ), argc, ##__VA_ARGS__ )
#define rb_define_singleton_method( klass, name, func, argc, ... ) rb_define_singleton_method( klass, name, reinterpret_cast< RubyFunctionPtr >( func ), argc, ##__VA_ARGS__ )
#define rb_define_method( klass, name, func, argc, ... ) rb_define_method( klass, name, reinterpret_cast< RubyFunctionPtr >( func ), argc, ##__VA_ARGS__ )
#endif // SFML_RUBYEXT_SYSTEM_MAIN_HEADER_

View File

@ -1,11 +0,0 @@
require 'sfml/system'
require 'sfml/window'
require 'sfml/graphics'
class MyDrawable
include SFML::Drawable
end
drawable = MyDrawable.new
p drawable.position
p drawable.is_a?( SFML::Drawable )

View File

@ -1,33 +0,0 @@
require 'sfml/system'
require 'sfml/window'
require 'sfml/graphics'
app = SFML::RenderWindow.new
app.create( [800, 600], "My Ruby SFML" )
app.framerate = 100
app.position = [300, 300]
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!" )
text.position = [ 20, 20 ]
while app.open?
while event = app.get_event
if event.type == SFML::Event::Closed
app.close
end
end
app.clear
shape.position = [input.mouseX, input.mouseY]
app.draw shape
app.draw sprite
app.draw text
app.display
end

View File

@ -1,77 +0,0 @@
class Vector2
attr_accessor :x, :y
def initialize( *args )
if args.size == 0
@x = 0
@y = 0
elsif args.size == 1
copyFrom( args[0] )
elsif args.size == 2
Vector2.valid? args[0], args[1]
@x = args[0]
@y = args[1]
else
raise ArgumentError.new( "invalid argument list" )
end
@dataType = x.class
end
def copyFrom( source )
unless source.is_a?( Array ) || source.is_a?( Vector2 )
raise ArgumentError.new( "expected Array or Vector2" )
end
Vector2.valid? source[0], source[1]
@x = source[0]
@y = source[1]
end
def -@
Vector2.new( -x, -y )
end
def +( right )
Vector2.new( x + right.x, y + right.y )
end
def -( right )
Vector2.new( x - right.x, y - right.y )
end
def *( right )
Vector2.new( x * right.x, y * right.y )
end
def /( right )
Vector2.new( x / right.x, y / right.y )
end
def ==( right )
x == right.x && y == right.y
end
def []( index )
if index == 0 || index == :x
return x
elsif index == 1 || index == :y
return y
end
raise ArgumentError.new( "Expected index to be either 0..1 or :x and :y" )
end
def self.valid?( x, y )
if x.class != y.class
raise RuntimeError.new( "x and y must be of same type" )
end
if x.is_a?( Numeric ) == false
raise RuntimeError.new( "x and y must be numeric!" )
end
true
end
end

View File

@ -1,19 +0,0 @@
require 'sfml/system'
require 'sfml/window'
app = SFML::Window.new( [800, 600], "My Ruby SFML" )
app.framerate = 100
app.position = [300, 300]
input = app.input
while app.open?
while event = app.get_event
if event.type == SFML::Event::Closed
app.close
end
end
p [input.mouse_x, input.mouse_y]
app.display
end