Added the trunk/branches/tags directories at repository root, and moved previous root into trunk/
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1002 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
commit
2f524481c1
974 changed files with 295448 additions and 0 deletions
213
ruby/RubySFML/RubySFML.cpp
Normal file
213
ruby/RubySFML/RubySFML.cpp
Normal file
|
@ -0,0 +1,213 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/System.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
#include <SFML/Window/OpenGLCaps.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE NOARGV[1];
|
||||
VALUE g_vModule;
|
||||
|
||||
|
||||
/*
|
||||
* Returns true if the specified OpenGL extension is supported, false otherwise.
|
||||
*/
|
||||
VALUE SFML_checkExtension(VALUE vClass, VALUE vName) {
|
||||
return OpenGLCaps::CheckExtension(STR2CSTR(vName)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the maximum texture size on the current display hardware.
|
||||
*/
|
||||
VALUE SFML_getMaxTextureSize() {
|
||||
return INT2NUM(OpenGLCaps::GetMaxTextureSize());
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the maximum texture units on the current display hardware.
|
||||
*/
|
||||
VALUE SFML_getMaxTextureUnits() {
|
||||
return INT2NUM(OpenGLCaps::GetMaxTextureUnits());
|
||||
}
|
||||
|
||||
extern "C" void Init_RubySFML() {
|
||||
g_vModule = rb_define_module("SFML");
|
||||
DEFINE_MODULE_METHOD(checkExtension, checkExtension, 1);
|
||||
DEFINE_MODULE_METHOD(checkExtension, getMaxTextureSize, 0);
|
||||
DEFINE_MODULE_METHOD(checkExtension, getMaxTextureUnits, 0);
|
||||
|
||||
Init_Clock();
|
||||
Init_Color();
|
||||
Init_Drawable();
|
||||
Init_Event();
|
||||
Init_FloatRect();
|
||||
Init_Image();
|
||||
Init_Input();
|
||||
Init_IntRect();
|
||||
Init_Music();
|
||||
Init_PostFX();
|
||||
Init_RenderWindow();
|
||||
Init_Sound();
|
||||
Init_SoundBuffer();
|
||||
Init_SoundBufferRecorder();
|
||||
Init_Sprite();
|
||||
Init_String();
|
||||
Init_VideoMode();
|
||||
Init_View();
|
||||
Init_Window();
|
||||
|
||||
// Window style constants
|
||||
VALUE vStyle = rb_define_module_under(g_vModule, "Style");
|
||||
rb_define_const(g_cWindow, "Resize", INT2FIX(Style::Resize));
|
||||
rb_define_const(g_cWindow, "Close", INT2FIX(Style::Close));
|
||||
rb_define_const(g_cWindow, "Fullscreen", INT2FIX(Style::Fullscreen));
|
||||
|
||||
// Sound constants
|
||||
rb_define_const(g_cSound, "Stopped", INT2FIX(Sound::Stopped));
|
||||
rb_define_const(g_cSound, "Paused", INT2FIX(Sound::Paused));
|
||||
rb_define_const(g_cSound, "Playing", INT2FIX(Sound::Playing));
|
||||
|
||||
// Event constants
|
||||
rb_define_const(g_cEvent, "Closed", INT2FIX(Event::Closed));
|
||||
rb_define_const(g_cEvent, "Resized", INT2FIX(Event::Resized));
|
||||
rb_define_const(g_cEvent, "LostFocus", INT2FIX(Event::LostFocus));
|
||||
rb_define_const(g_cEvent, "GainedFocus", INT2FIX(Event::GainedFocus));
|
||||
rb_define_const(g_cEvent, "TextEntered", INT2FIX(Event::TextEntered));
|
||||
rb_define_const(g_cEvent, "KeyPressed", INT2FIX(Event::KeyPressed));
|
||||
rb_define_const(g_cEvent, "KeyReleased", INT2FIX(Event::KeyReleased));
|
||||
rb_define_const(g_cEvent, "MouseWheelMoved", INT2FIX(Event::MouseWheelMoved));
|
||||
rb_define_const(g_cEvent, "MouseButtonPressed", INT2FIX(Event::MouseButtonPressed));
|
||||
rb_define_const(g_cEvent, "MouseButtonReleased", INT2FIX(Event::MouseButtonReleased));
|
||||
rb_define_const(g_cEvent, "MouseMoved", INT2FIX(Event::MouseMoved));
|
||||
rb_define_const(g_cEvent, "JoyButtonPressed", INT2FIX(Event::JoyButtonPressed));
|
||||
rb_define_const(g_cEvent, "JoyButtonReleased", INT2FIX(Event::JoyButtonReleased));
|
||||
rb_define_const(g_cEvent, "JoyMoved", INT2FIX(Event::JoyMoved));
|
||||
|
||||
// Mouse constants
|
||||
VALUE vMouse = rb_define_module_under(g_vModule, "Mouse");
|
||||
rb_define_const(vMouse, "Left", INT2FIX(Mouse::Left));
|
||||
rb_define_const(vMouse, "Right", INT2FIX(Mouse::Right));
|
||||
rb_define_const(vMouse, "Middle", INT2FIX(Mouse::Middle));
|
||||
rb_define_const(vMouse, "XButton1", INT2FIX(Mouse::XButton1));
|
||||
rb_define_const(vMouse, "XButton2", INT2FIX(Mouse::XButton2));
|
||||
|
||||
// Joystick constants
|
||||
VALUE vJoy = rb_define_module_under(g_vModule, "Joy");
|
||||
rb_define_const(vJoy, "AxisX", INT2FIX(Joy::AxisX));
|
||||
rb_define_const(vJoy, "AxisY", INT2FIX(Joy::AxisY));
|
||||
rb_define_const(vJoy, "AxisZ", INT2FIX(Joy::AxisZ));
|
||||
rb_define_const(vJoy, "AxisR", INT2FIX(Joy::AxisR));
|
||||
rb_define_const(vJoy, "AxisU", INT2FIX(Joy::AxisU));
|
||||
rb_define_const(vJoy, "AxisV", INT2FIX(Joy::AxisV));
|
||||
rb_define_const(vJoy, "AxisPOV", INT2FIX(Joy::AxisPOV));
|
||||
|
||||
// Keyboard constants
|
||||
VALUE vKey = rb_define_module_under(g_vModule, "Key");
|
||||
rb_define_const(vKey, "A", INT2FIX(Key::A));
|
||||
rb_define_const(vKey, "B", INT2FIX(Key::B));
|
||||
rb_define_const(vKey, "C", INT2FIX(Key::C));
|
||||
rb_define_const(vKey, "D", INT2FIX(Key::D));
|
||||
rb_define_const(vKey, "E", INT2FIX(Key::E));
|
||||
rb_define_const(vKey, "F", INT2FIX(Key::F));
|
||||
rb_define_const(vKey, "G", INT2FIX(Key::G));
|
||||
rb_define_const(vKey, "H", INT2FIX(Key::H));
|
||||
rb_define_const(vKey, "I", INT2FIX(Key::I));
|
||||
rb_define_const(vKey, "J", INT2FIX(Key::J));
|
||||
rb_define_const(vKey, "K", INT2FIX(Key::K));
|
||||
rb_define_const(vKey, "L", INT2FIX(Key::L));
|
||||
rb_define_const(vKey, "M", INT2FIX(Key::M));
|
||||
rb_define_const(vKey, "N", INT2FIX(Key::N));
|
||||
rb_define_const(vKey, "O", INT2FIX(Key::O));
|
||||
rb_define_const(vKey, "P", INT2FIX(Key::P));
|
||||
rb_define_const(vKey, "Q", INT2FIX(Key::Q));
|
||||
rb_define_const(vKey, "R", INT2FIX(Key::R));
|
||||
rb_define_const(vKey, "S", INT2FIX(Key::S));
|
||||
rb_define_const(vKey, "T", INT2FIX(Key::T));
|
||||
rb_define_const(vKey, "U", INT2FIX(Key::U));
|
||||
rb_define_const(vKey, "V", INT2FIX(Key::V));
|
||||
rb_define_const(vKey, "W", INT2FIX(Key::W));
|
||||
rb_define_const(vKey, "X", INT2FIX(Key::X));
|
||||
rb_define_const(vKey, "Y", INT2FIX(Key::Y));
|
||||
rb_define_const(vKey, "Z", INT2FIX(Key::Z));
|
||||
rb_define_const(vKey, "Num0", INT2FIX(Key::Num0));
|
||||
rb_define_const(vKey, "Num1", INT2FIX(Key::Num1));
|
||||
rb_define_const(vKey, "Num2", INT2FIX(Key::Num2));
|
||||
rb_define_const(vKey, "Num3", INT2FIX(Key::Num3));
|
||||
rb_define_const(vKey, "Num4", INT2FIX(Key::Num4));
|
||||
rb_define_const(vKey, "Num5", INT2FIX(Key::Num5));
|
||||
rb_define_const(vKey, "Num6", INT2FIX(Key::Num6));
|
||||
rb_define_const(vKey, "Num7", INT2FIX(Key::Num7));
|
||||
rb_define_const(vKey, "Num8", INT2FIX(Key::Num8));
|
||||
rb_define_const(vKey, "Num9", INT2FIX(Key::Num9));
|
||||
rb_define_const(vKey, "Escape", INT2FIX(Key::Escape));
|
||||
rb_define_const(vKey, "Space", INT2FIX(Key::Space));
|
||||
rb_define_const(vKey, "Return", INT2FIX(Key::Return));
|
||||
rb_define_const(vKey, "Back", INT2FIX(Key::Space));
|
||||
rb_define_const(vKey, "Tab", INT2FIX(Key::Tab));
|
||||
rb_define_const(vKey, "PageUp", INT2FIX(Key::PageUp));
|
||||
rb_define_const(vKey, "PageDown", INT2FIX(Key::PageDown));
|
||||
rb_define_const(vKey, "End", INT2FIX(Key::End));
|
||||
rb_define_const(vKey, "Home", INT2FIX(Key::Home));
|
||||
rb_define_const(vKey, "Insert", INT2FIX(Key::Insert));
|
||||
rb_define_const(vKey, "Delete", INT2FIX(Key::Delete));
|
||||
rb_define_const(vKey, "Add", INT2FIX(Key::Add));
|
||||
rb_define_const(vKey, "Subtract", INT2FIX(Key::Subtract));
|
||||
rb_define_const(vKey, "Multiply", INT2FIX(Key::Multiply));
|
||||
rb_define_const(vKey, "Divide", INT2FIX(Key::Divide));
|
||||
rb_define_const(vKey, "Left", INT2FIX(Key::Left));
|
||||
rb_define_const(vKey, "Right", INT2FIX(Key::Right));
|
||||
rb_define_const(vKey, "Up", INT2FIX(Key::Up));
|
||||
rb_define_const(vKey, "Down", INT2FIX(Key::Down));
|
||||
rb_define_const(vKey, "Numpad0", INT2FIX(Key::Numpad0));
|
||||
rb_define_const(vKey, "Numpad1", INT2FIX(Key::Numpad1));
|
||||
rb_define_const(vKey, "Numpad2", INT2FIX(Key::Numpad2));
|
||||
rb_define_const(vKey, "Numpad3", INT2FIX(Key::Numpad3));
|
||||
rb_define_const(vKey, "Numpad4", INT2FIX(Key::Numpad4));
|
||||
rb_define_const(vKey, "Numpad5", INT2FIX(Key::Numpad5));
|
||||
rb_define_const(vKey, "Numpad6", INT2FIX(Key::Numpad6));
|
||||
rb_define_const(vKey, "Numpad7", INT2FIX(Key::Numpad7));
|
||||
rb_define_const(vKey, "Numpad8", INT2FIX(Key::Numpad8));
|
||||
rb_define_const(vKey, "Numpad9", INT2FIX(Key::Numpad9));
|
||||
rb_define_const(vKey, "F1", INT2FIX(Key::F1));
|
||||
rb_define_const(vKey, "F2", INT2FIX(Key::F2));
|
||||
rb_define_const(vKey, "F3", INT2FIX(Key::F3));
|
||||
rb_define_const(vKey, "F4", INT2FIX(Key::F4));
|
||||
rb_define_const(vKey, "F5", INT2FIX(Key::F5));
|
||||
rb_define_const(vKey, "F6", INT2FIX(Key::F6));
|
||||
rb_define_const(vKey, "F7", INT2FIX(Key::F7));
|
||||
rb_define_const(vKey, "F8", INT2FIX(Key::F8));
|
||||
rb_define_const(vKey, "F9", INT2FIX(Key::F9));
|
||||
rb_define_const(vKey, "F10", INT2FIX(Key::F10));
|
||||
rb_define_const(vKey, "F11", INT2FIX(Key::F11));
|
||||
rb_define_const(vKey, "F12", INT2FIX(Key::F12));
|
||||
rb_define_const(vKey, "F13", INT2FIX(Key::F13));
|
||||
rb_define_const(vKey, "F14", INT2FIX(Key::F14));
|
||||
rb_define_const(vKey, "F15", INT2FIX(Key::F15));
|
||||
rb_define_const(vKey, "Pause", INT2FIX(Key::Pause));
|
||||
}
|
||||
|
88
ruby/RubySFML/RubySFML.h
Normal file
88
ruby/RubySFML/RubySFML.h
Normal file
|
@ -0,0 +1,88 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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 "ruby_helper.h"
|
||||
|
||||
extern VALUE NOARGV[1];
|
||||
|
||||
// SFML Ruby module object
|
||||
extern VALUE g_vModule;
|
||||
|
||||
// SFML Ruby class objects
|
||||
extern VALUE g_cClock;
|
||||
extern VALUE g_cColor;
|
||||
extern VALUE g_cDrawable;
|
||||
extern VALUE g_cEvent;
|
||||
extern VALUE g_cFloatRect;
|
||||
extern VALUE g_cImage;
|
||||
extern VALUE g_cInput;
|
||||
extern VALUE g_cIntRect;
|
||||
extern VALUE g_cMusic;
|
||||
extern VALUE g_cPostFX;
|
||||
extern VALUE g_cRenderWindow;
|
||||
extern VALUE g_cSound;
|
||||
extern VALUE g_cSoundBuffer;
|
||||
extern VALUE g_cSoundBufferRecorder;
|
||||
extern VALUE g_cSprite;
|
||||
extern VALUE g_cString;
|
||||
extern VALUE g_cVideoMode;
|
||||
extern VALUE g_cView;
|
||||
extern VALUE g_cWindow;
|
||||
|
||||
// SMFL Ruby class init functions
|
||||
extern void Init_Clock();
|
||||
extern void Init_Color();
|
||||
extern void Init_Drawable();
|
||||
extern void Init_Event();
|
||||
extern void Init_FloatRect();
|
||||
extern void Init_Image();
|
||||
extern void Init_Input();
|
||||
extern void Init_IntRect();
|
||||
extern void Init_Music();
|
||||
extern void Init_PostFX();
|
||||
extern void Init_RenderWindow();
|
||||
extern void Init_Sound();
|
||||
extern void Init_SoundBuffer();
|
||||
extern void Init_SoundBufferRecorder();
|
||||
extern void Init_Sprite();
|
||||
extern void Init_String();
|
||||
extern void Init_VideoMode();
|
||||
extern void Init_View();
|
||||
extern void Init_Window();
|
||||
|
||||
extern void Color_free(void *p);
|
||||
extern VALUE Color_new(int argc, VALUE *argv, VALUE vClass);
|
||||
|
||||
extern void Event_free(void *p);
|
||||
extern VALUE Event_new(int argc, VALUE *argv, VALUE vClass);
|
||||
|
||||
extern void FloatRect_free(void *p);
|
||||
extern VALUE FloatRect_new(int argc, VALUE *argv, VALUE vClass);
|
||||
|
||||
extern void Image_free(void *p);
|
||||
extern VALUE Image_new(int argc, VALUE *argv, VALUE vClass);
|
||||
|
||||
extern void IntRect_free(void *p);
|
||||
extern VALUE IntRect_new(int argc, VALUE *argv, VALUE vClass);
|
||||
|
83
ruby/RubySFML/RubySFML.rb
Normal file
83
ruby/RubySFML/RubySFML.rb
Normal file
|
@ -0,0 +1,83 @@
|
|||
require "RubySFML.so"
|
||||
|
||||
module SFML
|
||||
|
||||
# A simple class for dealing with tiled sprites.
|
||||
class TSprite < Sprite
|
||||
# Use to set/get the tile column
|
||||
attr_accessor :tx
|
||||
# Use to set/get the tile row
|
||||
attr_accessor :ty
|
||||
|
||||
# Pass in the image, the tile width, and the tile height.
|
||||
def initialize(image, w, h)
|
||||
super(image)
|
||||
@tx, @ty, @tw, @th = 0, 0, w, h
|
||||
setRotationCenter(@tw/2, @th/2)
|
||||
end
|
||||
|
||||
# Overrides Sprite::render to call setSubRect() for you to choose a
|
||||
# tile based on tx and ty before the sprite is rendered.
|
||||
def render(win)
|
||||
l, t = @tw*@tx, @th*@ty
|
||||
setSubRect(IntRect.new(l, t, l+@tw, t+@th))
|
||||
super(win)
|
||||
end
|
||||
end
|
||||
|
||||
# A simple class for dealing with sprites and 2D velocity/acceleration
|
||||
class VSprite < TSprite
|
||||
# Use to get/set the x component of the velocity vector
|
||||
attr_accessor :vx
|
||||
# Use to get/set the y component of the velocity vector
|
||||
attr_accessor :vy
|
||||
|
||||
# Pass in the image, the tile width, and the tile height.
|
||||
def initialize(image, w, h)
|
||||
super(image, w, h)
|
||||
@vx = @vy = 0.0
|
||||
end
|
||||
|
||||
# Specify a number of degrees to rotate left
|
||||
def turn_left(degrees) rotate(degrees); end
|
||||
# Specify a number of degrees to rotate right
|
||||
def turn_right(degrees) rotate(-degrees); end
|
||||
|
||||
# Returns the magnitude of the velocity vector
|
||||
def speed
|
||||
return Math.sqrt(@vx**2 + @vy**2)
|
||||
end
|
||||
|
||||
# Returns the direction of the velocity vector
|
||||
def direction
|
||||
s = speed
|
||||
return [@vx/s, @vy/s]
|
||||
end
|
||||
|
||||
# Accelerates the sprite in the direction vector v by the amount "thrust"
|
||||
def accelerate(v, thrust)
|
||||
@vx += v[0] * thrust
|
||||
@vy += v[1] * thrust
|
||||
end
|
||||
|
||||
# Scales the speed component of the velocity
|
||||
def scale_speed(amount)
|
||||
@vx *= amount
|
||||
@vy *= amount
|
||||
end
|
||||
|
||||
# Moves the sprite in the direction vector v by the amound d
|
||||
def move(v, d)
|
||||
self.x += v[0] * d
|
||||
self.y += v[1] * d
|
||||
end
|
||||
|
||||
# Updates the sprite's position based on its velocity and its time slice
|
||||
def update(time)
|
||||
self.x += @vx * time
|
||||
self.y += @vy * time
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
26
ruby/RubySFML/extconf-linux.rb
Normal file
26
ruby/RubySFML/extconf-linux.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require "mkmf"
|
||||
|
||||
dir_config('SFML')
|
||||
|
||||
libs = [
|
||||
"GL",
|
||||
"GLU",
|
||||
"sfml-system",
|
||||
"sfml-window",
|
||||
"sfml-graphics",
|
||||
"sfml-audio",
|
||||
]
|
||||
|
||||
libs.each {|lib|
|
||||
unless have_library(lib)
|
||||
puts "Unable to find #{lib}.lib!"
|
||||
puts "Please specify the path to them using:"
|
||||
puts "--with-SFML-lib=lib_path"
|
||||
puts "--with-SFML-include=include_path"
|
||||
puts
|
||||
exit
|
||||
end
|
||||
}
|
||||
|
||||
create_makefile("RubySFML")
|
||||
|
30
ruby/RubySFML/extconf-win.rb
Normal file
30
ruby/RubySFML/extconf-win.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
require "mkmf"
|
||||
$CFLAGS = "/MD /O2 /EHsc /DWIN32 /DNDEBUG /D_WINDOWS"
|
||||
#$CFLAGS = "/MD /Zi /EHsc /DWIN32 /DNDEBUG /D_WINDOWS"
|
||||
#$LDFLAGS = "/Zi"
|
||||
|
||||
dir_config('SFML')
|
||||
|
||||
libs = [
|
||||
"gdi32",
|
||||
"opengl32",
|
||||
"glu32",
|
||||
"sfml-system-s",
|
||||
"sfml-window-s",
|
||||
"sfml-graphics-s",
|
||||
"sfml-audio-s",
|
||||
]
|
||||
|
||||
libs.each {|lib|
|
||||
unless have_library(lib)
|
||||
puts "Unable to find #{lib}.lib!"
|
||||
puts "Please specify the path to them using:"
|
||||
puts "--with-SFML-lib=lib_path"
|
||||
puts "--with-SFML-include=include_path"
|
||||
puts
|
||||
exit
|
||||
end
|
||||
}
|
||||
|
||||
create_makefile("RubySFML")
|
||||
|
846
ruby/RubySFML/ruby_helper.h
Normal file
846
ruby/RubySFML/ruby_helper.h
Normal file
|
@ -0,0 +1,846 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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 __ruby_helper_h__
|
||||
#define __ruby_helper_h__
|
||||
#define NOMINMAX
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#if defined(_WIN32) || defined(__WIN32__)
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
#include <ruby.h>
|
||||
|
||||
#if defined(_WIN32) || defined(__WIN32__)
|
||||
#define DECL __cdecl
|
||||
#else
|
||||
#define DECL __attribute__((cdecl))
|
||||
#endif
|
||||
|
||||
// Copies a string, makes the first character upper case, and then returns it
|
||||
inline std::string fupr(const char *p) {
|
||||
std::string str = p;
|
||||
str[0] = toupper(str[0]);
|
||||
return str;
|
||||
}
|
||||
|
||||
#define IS(v, c) rb_obj_class(v) == c
|
||||
#define ISKO(v, c) rb_obj_is_kind_of(v, c)
|
||||
#define ISNUM(v) ISKO(v, rb_cNumeric)
|
||||
#define ISSTR(v) ISKO(v, rb_cString)
|
||||
|
||||
#define DECLARE_PTR_VAR(MyClass, MyVar, expr)\
|
||||
MyClass *p##MyVar = expr;\
|
||||
VALUE v##MyVar = p##MyVar == NULL ? Qnil : Data_Wrap_Struct(g_c##MyClass, 0, 0, p##MyVar)
|
||||
|
||||
#define DECLARE_OBJ_VAR(MyClass, MyVar, expr)\
|
||||
VALUE v##MyVar = MyClass##_new(0, NOARGV, g_c##MyClass);\
|
||||
MyClass *p##MyVar = NULL;\
|
||||
Data_Get_Struct(v##MyVar, MyClass, p##MyVar);\
|
||||
*p##MyVar = expr
|
||||
|
||||
#define GET_OBJ_VAR(MyClass, MyVar)\
|
||||
MyClass *p##MyVar = NULL;\
|
||||
Data_Get_Struct(v##MyVar, MyClass, p##MyVar)
|
||||
|
||||
#define DEFINE_CLASS_METHOD(MyClass, MyMethod, n)\
|
||||
rb_define_singleton_method(g_c##MyClass, #MyMethod,\
|
||||
(unsigned long (DECL *)(...))MyClass##_##MyMethod, n)
|
||||
|
||||
#define DEFINE_CLASS_METHOD2(MyClass, MyMethod, RubyMethod, n)\
|
||||
rb_define_singleton_method(g_c##MyClass, #RubyMethod,\
|
||||
(unsigned long (DECL *)(...))MyClass##_##MyMethod, n)
|
||||
|
||||
#define DEFINE_INSTANCE_METHOD(MyClass, MyMethod, n)\
|
||||
rb_define_method(g_c##MyClass, #MyMethod,\
|
||||
(unsigned long (DECL *)(...))MyClass##_##MyMethod, n)
|
||||
|
||||
#define DEFINE_INSTANCE_METHOD2(MyClass, MyMethod, RubyMethod, n)\
|
||||
rb_define_method(g_c##MyClass, #RubyMethod,\
|
||||
(unsigned long (DECL *)(...))MyClass##_##MyMethod, n)
|
||||
|
||||
// Use to define a standard getter method (for any type of member)
|
||||
// (Defines both obj.variableName and obj.getVariableName)
|
||||
#define DEFINE_GETTER(MyClass, MyField) \
|
||||
rb_define_method(g_c##MyClass, #MyField, \
|
||||
(unsigned long (DECL *)(...))MyClass##_get_##MyField, 0);\
|
||||
rb_define_method(g_c##MyClass, (std::string("get")+fupr(#MyField)).c_str(), \
|
||||
(unsigned long (DECL *)(...))MyClass##_get_##MyField, 0)
|
||||
|
||||
// Use to define a standard setter method (for any type of member)
|
||||
// (Defines both obj.variableName = v and obj.setVariableName v)
|
||||
#define DEFINE_SETTER(MyClass, MyField) \
|
||||
rb_define_method(g_c##MyClass, #MyField "=", \
|
||||
(unsigned long (DECL *)(...))MyClass##_set_##MyField, 1);\
|
||||
rb_define_method(g_c##MyClass, (std::string("set")+fupr(#MyField)).c_str(), \
|
||||
(unsigned long (DECL *)(...))MyClass##_set_##MyField, 1)
|
||||
|
||||
// Use to define standard getter and setter methods (for any type of member)
|
||||
#define DEFINE_RW(MyClass, MyField) \
|
||||
DEFINE_GETTER(MyClass, MyField);\
|
||||
DEFINE_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to define a standard getter method (for any type of member)
|
||||
// (Defines both obj.variableName and obj.getVariableName)
|
||||
#define DEFINE_GETTER2(MyClass, MyField, RubyField) \
|
||||
rb_define_method(g_c##MyClass, #RubyField, \
|
||||
(unsigned long (DECL *)(...))MyClass##_get_##MyField, 0);\
|
||||
rb_define_method(g_c##MyClass, (std::string("get")+fupr(#RubyField)).c_str(), \
|
||||
(unsigned long (DECL *)(...))MyClass##_get_##MyField, 0)
|
||||
|
||||
// Use to define a standard setter method (for any type of member)
|
||||
// (Defines both obj.variableName = v and obj.setVariableName v)
|
||||
#define DEFINE_SETTER2(MyClass, MyField, RubyField) \
|
||||
rb_define_method(g_c##MyClass, #RubyField "=", \
|
||||
(unsigned long (DECL *)(...))MyClass##_set_##MyField, 1);\
|
||||
rb_define_method(g_c##MyClass, (std::string("set")+fupr(#RubyField)).c_str(), \
|
||||
(unsigned long (DECL *)(...))MyClass##_set_##MyField, 1)
|
||||
|
||||
// Use to define standard getter and setter methods (for any type of member)
|
||||
#define DEFINE_RW2(MyClass, MyField, RubyField) \
|
||||
DEFINE_GETTER2(MyClass, MyField, RubyField);\
|
||||
DEFINE_SETTER2(MyClass, MyField, RubyField)
|
||||
|
||||
// Use to define a static getter method (for any type of member)
|
||||
// (Defines both Class.variableName and Class.getVariableName)
|
||||
#define DEFINE_STATIC_GETTER(MyClass, MyField) \
|
||||
rb_define_singleton_method(g_c##MyClass, #MyField, \
|
||||
(unsigned long (DECL *)(...))MyClass##_get_##MyField, 0);\
|
||||
rb_define_singleton_method(g_c##MyClass, (std::string("get")+fupr(#MyField)).c_str(), \
|
||||
(unsigned long (DECL *)(...))MyClass##_get_##MyField, 0)
|
||||
|
||||
// Use to define a static setter method (for any type of member)
|
||||
// (Defines both Class.variableName = v and Class.setVariableName v)
|
||||
#define DEFINE_STATIC_SETTER(MyClass, MyField) \
|
||||
rb_define_singleton_method(g_c##MyClass, #MyField "=", \
|
||||
(unsigned long (DECL *)(...))MyClass##_set_##MyField, 1);\
|
||||
rb_define_singleton_method(g_c##MyClass, (std::string("set")+fupr(#MyField)).c_str(), \
|
||||
(unsigned long (DECL *)(...))MyClass##_set_##MyField, 1)
|
||||
|
||||
// Use to define static getter and setter methods (for any type of member)
|
||||
#define DEFINE_STATIC_RW(MyClass, MyField) \
|
||||
DEFINE_STATIC_GETTER(MyClass, MyField);\
|
||||
DEFINE_STATIC_SETTER(MyClass, MyField)
|
||||
|
||||
#define DEFINE_MODULE_CONST(MyConst)\
|
||||
rb_define_const(g_vModule, #MyConst, INT2FIX(MyConst))
|
||||
|
||||
#define DEFINE_MODULE_METHOD(MyMethod, RubyMethod, n)\
|
||||
rb_define_singleton_method(g_vModule, #RubyMethod,\
|
||||
(unsigned long (DECL *)(...))SFML_##MyMethod, n)
|
||||
|
||||
#define DEFINE_INT_CONST(MyClass, MyConst)\
|
||||
rb_define_const(g_c##MyClass, #MyConst, INT2FIX(MyClass::MyConst))
|
||||
|
||||
#define DEFINE_PTR_CONST(MyClass, MyConst, expr)\
|
||||
rb_define_const(g_c##MyClass, #MyConst, Data_Wrap_Struct(g_c##MyClass, 0, 0, expr))
|
||||
|
||||
|
||||
#define DECLARE_VOID_METHOD(MyClass, MyMethod)\
|
||||
static VALUE MyClass##_##MyMethod(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyMethod();\
|
||||
return vSelf;\
|
||||
}\
|
||||
|
||||
// Use to declare standard free, new, and init methods with 0 parameters
|
||||
#define DECLARE_FREE_NEW_INIT0(MyClass)\
|
||||
static void MyClass##_free(void *p)\
|
||||
{\
|
||||
delete (MyClass *)p;\
|
||||
}\
|
||||
static VALUE MyClass##_new(VALUE vClass)\
|
||||
{\
|
||||
VALUE argv[1];\
|
||||
MyClass *ptr = new MyClass;\
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, MyClass##_free, ptr);\
|
||||
rb_obj_call_init(tData, 0, argv);\
|
||||
return tData;\
|
||||
}\
|
||||
static VALUE MyClass##_initialize(VALUE vSelf)\
|
||||
{\
|
||||
return vSelf;\
|
||||
}
|
||||
|
||||
// Use to declare standard free and new methods with 1 parameter
|
||||
#define DECLARE_FREE_NEW1(MyClass)\
|
||||
static void MyClass##_free(void *p)\
|
||||
{\
|
||||
delete (MyClass *)p;\
|
||||
}\
|
||||
static VALUE MyClass##_new(VALUE vClass, VALUE v1)\
|
||||
{\
|
||||
VALUE argv[1];\
|
||||
argv[0] = v1;\
|
||||
MyClass *ptr = new MyClass;\
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, MyClass##_free, ptr);\
|
||||
rb_obj_call_init(tData, 1, argv);\
|
||||
return tData;\
|
||||
}
|
||||
|
||||
// Use to define standard free, new, and init methods with n parameters
|
||||
// (Also declares a free method)
|
||||
#define DEFINE_CLASS_NEW_INIT(MyClass, n)\
|
||||
g_c##MyClass = rb_define_class(#MyClass, rb_cObject);\
|
||||
DEFINE_CLASS_METHOD(MyClass, new, n);\
|
||||
DEFINE_INSTANCE_METHOD(MyClass, initialize, n);
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public int member
|
||||
#define DECLARE_INT_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return INT2NUM((int)ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public int member
|
||||
#define DECLARE_STATIC_INT_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vClass)\
|
||||
{\
|
||||
return INT2NUM((int)MyClass::MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public int member
|
||||
#define DECLARE_INT_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = NUM2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public int member
|
||||
#define DECLARE_STATIC_INT_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vClass)\
|
||||
{\
|
||||
MyClass::MyField = NUM2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public int member
|
||||
#define DECLARE_INT_RW(MyClass, MyField)\
|
||||
DECLARE_INT_GETTER(MyClass, MyField)\
|
||||
DECLARE_INT_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare static getter and setter methods for a public int member
|
||||
#define DECLARE_STATIC_INT_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_INT_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_INT_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare a standard setter method for a public enum member
|
||||
#define DECLARE_ENUM_SETTER(MyClass, MyField, EnumType)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = (EnumType)NUM2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public enum member
|
||||
#define DECLARE_STATIC_ENUM_SETTER(MyClass, MyField, EnumType)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = (EnumType)NUM2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public enum member
|
||||
#define DECLARE_ENUM_RW(MyClass, MyField, EnumType)\
|
||||
DECLARE_INT_GETTER(MyClass, MyField)\
|
||||
DECLARE_ENUM_SETTER(MyClass, MyField, EnumType)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public enum member
|
||||
#define DECLARE_STATIC_ENUM_RW(MyClass, MyField, EnumType)\
|
||||
DECLARE_STATIC_INT_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_ENUM_SETTER(MyClass, MyField, EnumType)
|
||||
|
||||
// Use to declare a standard getter method for a public bool member
|
||||
#define DECLARE_BOOL_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return ptr->MyField ? Qtrue : Qfalse;\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public bool member
|
||||
#define DECLARE_STATIC_BOOL_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vClass)\
|
||||
{\
|
||||
return MyClass::MyField ? Qtrue : Qfalse;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public bool member
|
||||
#define DECLARE_BOOL_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = RTEST(v) ? true : false;\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public bool member
|
||||
#define DECLARE_STATIC_BOOL_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vClass, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = RTEST(v) ? true : false;\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public int member
|
||||
#define DECLARE_BOOL_RW(MyClass, MyField)\
|
||||
DECLARE_BOOL_GETTER(MyClass, MyField)\
|
||||
DECLARE_BOOL_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public int member
|
||||
#define DECLARE_STATIC_BOOL_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_BOOL_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_BOOL_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare a standard getter method for a public double member
|
||||
#define DECLARE_DOUBLE_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return rb_float_new((double)ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public double member
|
||||
#define DECLARE_STATIC_DOUBLE_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
return rb_float_new((double)MyClass::MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public double member
|
||||
#define DECLARE_DOUBLE_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = (float)NUM2DBL(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public double member
|
||||
#define DECLARE_STATIC_DOUBLE_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = NUM2DBL(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public double member
|
||||
#define DECLARE_DOUBLE_RW(MyClass, MyField)\
|
||||
DECLARE_DOUBLE_GETTER(MyClass, MyField)\
|
||||
DECLARE_DOUBLE_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public double member
|
||||
#define DECLARE_STATIC_DOUBLE_RW(MyClass, MyField)\
|
||||
DECLARE_DOUBLE_STATIC_GETTER(MyClass, MyField)\
|
||||
DECLARE_DOUBLE_STATIC_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare a standard getter method for a public std::string member
|
||||
#define DECLARE_STRING_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return rb_str_new2(ptr->MyField.c_str());\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public std::string member
|
||||
#define DECLARE_STATIC_STRING_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
return rb_str_new2(MyClass::MyField.c_str());\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public std::string member
|
||||
#define DECLARE_STRING_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = STR2CSTR(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public std::string member
|
||||
#define DECLARE_STATIC_STRING_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = STR2CSTR(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public std::string member
|
||||
#define DECLARE_STRING_RW(MyClass, MyField)\
|
||||
DECLARE_STRING_GETTER(MyClass, MyField)\
|
||||
DECLARE_STRING_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public int member
|
||||
#define DECLARE_STATIC_STRING_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_STRING_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_STRING_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare a standard getter method for a public short member
|
||||
#define DECLARE_SHORT_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return INT2FIX((int)ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard getter method for a public short member
|
||||
#define DECLARE_STATIC_SHORT_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
return INT2FIX((int)MyClass->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public short member
|
||||
#define DECLARE_SHORT_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = (short)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public short member
|
||||
#define DECLARE_STATIC_SHORT_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = (short)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public short member
|
||||
#define DECLARE_SHORT_RW(MyClass, MyField)\
|
||||
DECLARE_SHORT_GETTER(MyClass, MyField)\
|
||||
DECLARE_SHORT_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public short member
|
||||
#define DECLARE_STATIC_SHORT_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_SHORT_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_SHORT_SETTER(MyClass, MyField)
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public object member
|
||||
#define DECLARE_OBJ_GETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
VALUE vRet = FieldType##_new(0, NOARGV, g_c##FieldType);\
|
||||
FieldType *pRet;\
|
||||
Data_Get_Struct(vRet, FieldType, pRet);\
|
||||
*pRet = pSelf->MyField;\
|
||||
return vRet;\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public object member
|
||||
#define DECLARE_STATIC_OBJ_GETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
VALUE vRet = FieldType##_new(0, NOARGV, g_c##FieldType);\
|
||||
FieldType *pRet;\
|
||||
Data_Get_Struct(vRet, FieldType, pRet);\
|
||||
*pRet = MyClass::MyField;\
|
||||
return vRet;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public object member
|
||||
#define DECLARE_OBJ_SETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
FieldType *pV;\
|
||||
Data_Get_Struct(v, FieldType, pV);\
|
||||
pSelf->MyField = *pV;\
|
||||
return Qnil;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public object member
|
||||
#define DECLARE_STATIC_OBJ_SETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vClass, VALUE v)\
|
||||
{\
|
||||
FieldType *pV;\
|
||||
Data_Get_Struct(v, FieldType, pV);\
|
||||
MyClass::MyField = *pV;\
|
||||
return Qnil;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public object member
|
||||
#define DECLARE_OBJ_RW(MyClass, MyField, FieldType)\
|
||||
DECLARE_OBJ_GETTER(MyClass, MyField, FieldType)\
|
||||
DECLARE_OBJ_SETTER(MyClass, MyField, FieldType)
|
||||
|
||||
// Use to declare static getter and setter methods for a public object member
|
||||
#define DECLARE_STATIC_OBJ_RW(MyClass, MyField, FieldType)\
|
||||
DECLARE_STATIC_OBJ_GETTER(MyClass, MyField, FieldType)\
|
||||
DECLARE_STATIC_OBJ_SETTER(MyClass, MyField, FieldType)
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public unsigned short member
|
||||
#define DECLARE_USHORT_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return INT2FIX((int)ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard getter method for a public unsigned short member
|
||||
#define DECLARE_STATIC_USHORT_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
return INT2FIX((int)MyClass->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public unsigned short member
|
||||
#define DECLARE_USHORT_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = (unsigned short)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public unsigned short member
|
||||
#define DECLARE_STATIC_USHORT_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = (unsigned short)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public unsigned short member
|
||||
#define DECLARE_USHORT_RW(MyClass, MyField)\
|
||||
DECLARE_USHORT_GETTER(MyClass, MyField)\
|
||||
DECLARE_USHORT_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public unsigned short member
|
||||
#define DECLARE_STATIC_USHORT_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_USHORT_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_USHORT_SETTER(MyClass, MyField)
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public unsigned char member
|
||||
#define DECLARE_UCHAR_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return INT2FIX((int)ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard getter method for a public unsigned char member
|
||||
#define DECLARE_STATIC_UCHAR_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
return INT2FIX((int)MyClass->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public unsigned char member
|
||||
#define DECLARE_UCHAR_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = (unsigned char)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public unsigned char member
|
||||
#define DECLARE_STATIC_UCHAR_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = (unsigned char)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public unsigned char member
|
||||
#define DECLARE_UCHAR_RW(MyClass, MyField)\
|
||||
DECLARE_UCHAR_GETTER(MyClass, MyField)\
|
||||
DECLARE_UCHAR_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public unsigned char member
|
||||
#define DECLARE_STATIC_UCHAR_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_UCHAR_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_UCHAR_SETTER(MyClass, MyField)
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public char member
|
||||
#define DECLARE_CHAR_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return INT2FIX((int)ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard getter method for a public char member
|
||||
#define DECLARE_STATIC_CHAR_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
return INT2FIX((int)MyClass->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public char member
|
||||
#define DECLARE_CHAR_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = (char)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public char member
|
||||
#define DECLARE_STATIC_CHAR_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass::MyField = (char)FIX2INT(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public char member
|
||||
#define DECLARE_CHAR_RW(MyClass, MyField)\
|
||||
DECLARE_CHAR_GETTER(MyClass, MyField)\
|
||||
DECLARE_CHAR_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare standard getter and setter methods for a public char member
|
||||
#define DECLARE_STATIC_CHAR_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_CHAR_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_CHAR_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare a standard getter method for a public int64 member
|
||||
#define DECLARE_INT64_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return LL2NUM(ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public int64 member
|
||||
#define DECLARE_STATIC_INT64_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vClass)\
|
||||
{\
|
||||
return LL2NUM(MyClass::MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public int64 member
|
||||
#define DECLARE_INT64_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = NUM2LL(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public int64 member
|
||||
#define DECLARE_STATIC_INT64_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vClass)\
|
||||
{\
|
||||
MyClass::MyField = NUM2LL(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public int64 member
|
||||
#define DECLARE_INT64_RW(MyClass, MyField)\
|
||||
DECLARE_INT64_GETTER(MyClass, MyField)\
|
||||
DECLARE_INT64_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare static getter and setter methods for a public int64 member
|
||||
#define DECLARE_STATIC_INT64_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_INT64_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_INT64_SETTER(MyClass, MyField)
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public uint64 member
|
||||
#define DECLARE_UINT64_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
return ULL2NUM(ptr->MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a static getter method for a public uint64 member
|
||||
#define DECLARE_STATIC_UINT64_GETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vClass)\
|
||||
{\
|
||||
return ULL2NUM(MyClass::MyField);\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public uint64 member
|
||||
#define DECLARE_UINT64_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *ptr;\
|
||||
Data_Get_Struct(vSelf, MyClass, ptr);\
|
||||
ptr->MyField = NUM2ULL(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare a static setter method for a public uint64 member
|
||||
#define DECLARE_USTATIC_INT64_SETTER(MyClass, MyField)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vClass)\
|
||||
{\
|
||||
MyClass::MyField = NUM2ULL(v);\
|
||||
return v;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public uint64 member
|
||||
#define DECLARE_UINT64_RW(MyClass, MyField)\
|
||||
DECLARE_UINT64_GETTER(MyClass, MyField)\
|
||||
DECLARE_UINT64_SETTER(MyClass, MyField)
|
||||
|
||||
// Use to declare static getter and setter methods for a public uint64 member
|
||||
#define DECLARE_STATIC_UINT64_RW(MyClass, MyField)\
|
||||
DECLARE_STATIC_UINT64_GETTER(MyClass, MyField)\
|
||||
DECLARE_STATIC_UINT64_SETTER(MyClass, MyField)
|
||||
|
||||
|
||||
#define DECLARE_INT_ARR_GETTER(MyClass, MyField, Size)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
VALUE v = rb_ary_new();\
|
||||
for(int i=0; i<Size; i++)\
|
||||
rb_ary_push(v, INT2FIX(pSelf->MyField[i]));\
|
||||
return v;\
|
||||
}
|
||||
|
||||
#define DECLARE_INT_ARR_SETTER(MyClass, MyField, Size)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
for(int i=0; i<Size; i++)\
|
||||
pSelf->MyField[i] = FIX2INT(rb_ary_entry(v, i));\
|
||||
return v;\
|
||||
}
|
||||
|
||||
#define DECLARE_INT_ARR_RW(MyClass, MyField, Size)\
|
||||
DECLARE_INT_ARR_GETTER(MyClass, MyField, Size);\
|
||||
DECLARE_INT_ARR_SETTER(MyClass, MyField, Size)
|
||||
|
||||
|
||||
#define DECLARE_DOUBLE_ARR_GETTER(MyClass, MyField, Size)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
VALUE v = rb_ary_new();\
|
||||
for(int i=0; i<Size; i++)\
|
||||
rb_ary_push(v, rb_float_new(pSelf->MyField[i]));\
|
||||
return v;\
|
||||
}
|
||||
|
||||
#define DECLARE_DOUBLE_ARR_SETTER(MyClass, MyField, Size)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
for(int i=0; i<Size; i++)\
|
||||
pSelf->MyField[i] = NUM2DBL(rb_ary_entry(v, i));\
|
||||
return v;\
|
||||
}
|
||||
|
||||
#define DECLARE_DOUBLE_ARR_RW(MyClass, MyField, Size)\
|
||||
DECLARE_DOUBLE_ARR_GETTER(MyClass, MyField, Size);\
|
||||
DECLARE_DOUBLE_ARR_SETTER(MyClass, MyField, Size)
|
||||
|
||||
|
||||
#define DECLARE_ADDR_GETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
VALUE vRet = Data_Wrap_Struct(g_c##FieldType, 0, 0, &pSelf->MyField);\
|
||||
return vRet;\
|
||||
}
|
||||
|
||||
// Use to declare a standard getter method for a public object pointer member
|
||||
#define DECLARE_PTR_GETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
VALUE vRet = Data_Wrap_Struct(g_c##FieldType, 0, 0, pSelf->MyField);\
|
||||
return vRet;\
|
||||
}
|
||||
|
||||
|
||||
// Use to declare a standard getter method for a public object reference member
|
||||
#define DECLARE_REF_GETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_get_##MyField(VALUE vSelf)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
pSelf->MyField->ReferenceCountedObject_refCount.increment();\
|
||||
VALUE vRet = Data_Wrap_Struct(g_c##FieldType, 0, FieldType##_free, pSelf->MyField.pointer());\
|
||||
return vRet;\
|
||||
}
|
||||
|
||||
// Use to declare a standard setter method for a public object reference member
|
||||
#define DECLARE_REF_SETTER(MyClass, MyField, FieldType)\
|
||||
static VALUE MyClass##_set_##MyField(VALUE vSelf, VALUE v)\
|
||||
{\
|
||||
MyClass *pSelf;\
|
||||
Data_Get_Struct(vSelf, MyClass, pSelf);\
|
||||
FieldType *pV;\
|
||||
Data_Get_Struct(v, FieldType, pV);\
|
||||
pSelf->MyField = pV;\
|
||||
return Qnil;\
|
||||
}
|
||||
|
||||
// Use to declare standard getter and setter methods for a public object member
|
||||
#define DECLARE_REF_RW(MyClass, MyField, FieldType)\
|
||||
DECLARE_REF_GETTER(MyClass, MyField, FieldType)\
|
||||
DECLARE_REF_SETTER(MyClass, MyField, FieldType)
|
||||
|
||||
|
||||
#endif // __ruby_helper_h__
|
||||
|
86
ruby/RubySFML/sfClock.cpp
Normal file
86
ruby/RubySFML/sfClock.cpp
Normal file
|
@ -0,0 +1,86 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cClock;
|
||||
|
||||
void Clock_free(void *p) { delete (Clock *)p; }
|
||||
VALUE Clock_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Clock *ptr = new Clock();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Clock_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Clock_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Clock *pSelf;
|
||||
Data_Get_Struct(vSelf, Clock, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Clock_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Clock *pSelf;
|
||||
Data_Get_Struct(vSelf, Clock, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Elapsed Time=%f", pSelf->GetElapsedTime());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Clock_get_elapsedTime(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Clock *pSelf;
|
||||
Data_Get_Struct(vSelf, Clock, pSelf);
|
||||
return rb_float_new(pSelf->GetElapsedTime());
|
||||
}
|
||||
|
||||
static VALUE Clock_reset(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Clock *pSelf;
|
||||
Data_Get_Struct(vSelf, Clock, pSelf);
|
||||
pSelf->Reset();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
void Init_Clock()
|
||||
{
|
||||
g_cClock = rb_define_class_under(g_vModule, "Clock", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Clock, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Clock, initialize, -1);
|
||||
|
||||
DEFINE_GETTER(Clock, elapsedTime);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Clock, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD2(Clock, get_elapsedTime, to_f, 0);
|
||||
DEFINE_INSTANCE_METHOD(Clock, reset, 0);
|
||||
}
|
||||
|
120
ruby/RubySFML/sfColor.cpp
Normal file
120
ruby/RubySFML/sfColor.cpp
Normal file
|
@ -0,0 +1,120 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cColor;
|
||||
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Black, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, White, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Red, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Green, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Blue, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Yellow, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Magenta, Color);
|
||||
DECLARE_STATIC_OBJ_GETTER(Color, Cyan, Color);
|
||||
|
||||
DECLARE_INT_RW(Color, r);
|
||||
DECLARE_INT_RW(Color, g);
|
||||
DECLARE_INT_RW(Color, b);
|
||||
DECLARE_INT_RW(Color, a);
|
||||
|
||||
void Color_free(void *p) { delete (Color *)p; }
|
||||
VALUE Color_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Color *ptr = new Color();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Color_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Color_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Color *pSelf;
|
||||
Data_Get_Struct(vSelf, Color, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 1 && ISNUM(argv[0])) {
|
||||
DWORD dw = NUM2UINT(argv[0]);
|
||||
*pSelf = Color(
|
||||
(dw >> 24) & 0xFF,
|
||||
(dw >> 16) & 0xFF,
|
||||
(dw >> 8) & 0xFF,
|
||||
(dw >> 0) & 0xFF);
|
||||
} else if(argc >= 3 && argc <= 4 &&
|
||||
ISNUM(argv[0]) && ISNUM(argv[1]) && ISNUM(argv[2]) &&
|
||||
(argc < 4 || ISNUM(argv[3]))) {
|
||||
pSelf->r = (unsigned char)NUM2INT(argv[0]);
|
||||
pSelf->g = (unsigned char)NUM2INT(argv[1]);
|
||||
pSelf->b = (unsigned char)NUM2INT(argv[2]);
|
||||
pSelf->a = argc < 4 ? 255 : (unsigned char)NUM2INT(argv[3]);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Color_to_s(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
Color *pSelf;
|
||||
Data_Get_Struct(vSelf, Color, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "r=%d, g=%d, b=%d, a=%d", pSelf->r, pSelf->g, pSelf->b, pSelf->a);
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Color_toRGBA(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
Color *pSelf;
|
||||
Data_Get_Struct(vSelf, Color, pSelf);
|
||||
return UINT2NUM((pSelf->r << 24) | (pSelf->g << 16) | (pSelf->b << 8) | pSelf->a);
|
||||
}
|
||||
|
||||
void Init_Color()
|
||||
{
|
||||
g_cColor = rb_define_class_under(g_vModule, "Color", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Color, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Color, initialize, -1);
|
||||
|
||||
DEFINE_STATIC_GETTER(Color, Black);
|
||||
DEFINE_STATIC_GETTER(Color, White);
|
||||
DEFINE_STATIC_GETTER(Color, Red);
|
||||
DEFINE_STATIC_GETTER(Color, Green);
|
||||
DEFINE_STATIC_GETTER(Color, Blue);
|
||||
DEFINE_STATIC_GETTER(Color, Yellow);
|
||||
DEFINE_STATIC_GETTER(Color, Magenta);
|
||||
DEFINE_STATIC_GETTER(Color, Cyan);
|
||||
|
||||
DEFINE_RW(Color, r);
|
||||
DEFINE_RW(Color, g);
|
||||
DEFINE_RW(Color, b);
|
||||
DEFINE_RW(Color, a);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Color, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Color, toRGBA, 0);
|
||||
DEFINE_INSTANCE_METHOD2(Color, toRGBA, to_i, 0);
|
||||
}
|
||||
|
285
ruby/RubySFML/sfDrawable.cpp
Normal file
285
ruby/RubySFML/sfDrawable.cpp
Normal file
|
@ -0,0 +1,285 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
#include <math.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define COSF cosf
|
||||
#define SINF sinf
|
||||
#else
|
||||
#define COSF cos
|
||||
#define SINF sin
|
||||
#endif
|
||||
|
||||
using namespace sf;
|
||||
#define PI 3.14159265f
|
||||
|
||||
// Wrapper for Drawable that calls the Ruby version of virtual methods in case a Ruby class derives from this one
|
||||
class rDrawable : public Drawable {
|
||||
protected:
|
||||
VALUE m_vSelf, m_vClass;
|
||||
ID m_idRender;
|
||||
bool m_bRender;
|
||||
|
||||
virtual void Render(const RenderWindow &Window) const {
|
||||
// If this method is overridden in Ruby, call it
|
||||
if(m_bRender) {
|
||||
VALUE vWindow = Data_Wrap_Struct(g_cRenderWindow, 0, 0, (void*)&Window);
|
||||
VALUE vRet = rb_funcall(m_vSelf, m_idRender, 1, vWindow);
|
||||
} else // else raise error (pure virtual method)
|
||||
rb_raise(rb_eRuntimeError, "Pure virtual method Drawable::Render() called, but not defined.");
|
||||
}
|
||||
|
||||
public:
|
||||
// Call as soon as you get a Ruby VALUE pointing to this object
|
||||
void rInit(VALUE vSelf) {
|
||||
// Need these for rb_funcall
|
||||
m_vSelf = vSelf;
|
||||
m_vClass = CLASS_OF(m_vSelf);
|
||||
|
||||
// Initialize members for Render() virtual method
|
||||
m_idRender = rb_intern("render");
|
||||
m_bRender = rb_method_boundp(m_vClass, m_idRender, 0) == Qtrue;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
VALUE g_cDrawable;
|
||||
|
||||
void Drawable_free(void *p) { delete (Drawable *)p; }
|
||||
VALUE Drawable_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
rDrawable *ptr = new rDrawable();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Drawable_free, ptr);
|
||||
ptr->rInit(tData);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Drawable_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc >= 1 && argc <= 5 && (argc < 5 || IS(argv[4], g_cColor))) {
|
||||
if(argc >= 1)
|
||||
pSelf->SetLeft((float)NUM2DBL(argv[0]));
|
||||
if(argc >= 2)
|
||||
pSelf->SetTop((float)NUM2DBL(argv[1]));
|
||||
if(argc >= 3) {
|
||||
if(ISNUM(argv[2])) {
|
||||
float f = (float)NUM2DBL(argv[2]);
|
||||
pSelf->SetScale(f, f);
|
||||
} else if(IS(argv[2], rb_cArray)) {
|
||||
float x = (float)NUM2DBL(rb_ary_entry(argv[2], 0));
|
||||
float y = (float)NUM2DBL(rb_ary_entry(argv[2], 1));
|
||||
pSelf->SetScale(x, y);
|
||||
}
|
||||
}
|
||||
if(argc >= 4)
|
||||
pSelf->SetRotation((float)NUM2DBL(argv[3]));
|
||||
if(argc >= 5)
|
||||
pSelf->SetColor(*(Color *)DATA_PTR(argv[4]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Drawable_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Left=%f, Top=%f, Scale=[%f,%f], Rotation=%f", pSelf->GetLeft(), pSelf->GetTop(), pSelf->GetScaleX(), pSelf->GetScaleX(), pSelf->GetRotation());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Drawable_set_left(VALUE vSelf, VALUE vLeft) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->SetLeft((float)NUM2DBL(vLeft));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_set_top(VALUE vSelf, VALUE vTop) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->SetTop((float)NUM2DBL(vTop));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_set_scale(VALUE vSelf, VALUE vScale) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
if(ISNUM(vScale)) {
|
||||
float f = (float)NUM2DBL(vScale);
|
||||
pSelf->SetScale(f, f);
|
||||
} else if(IS(vScale, rb_cArray)) {
|
||||
float x = (float)NUM2DBL(rb_ary_entry(vScale, 0));
|
||||
float y = (float)NUM2DBL(rb_ary_entry(vScale, 1));
|
||||
pSelf->SetScale(x, y);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_set_rotation(VALUE vSelf, VALUE vRotation) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->SetRotation((float)NUM2DBL(vRotation));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_setRotationCenter(VALUE vSelf, VALUE vX, VALUE vY) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->SetRotationCenter((float)NUM2DBL(vX), (float)NUM2DBL(vY));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_set_color(VALUE vSelf, VALUE vColor) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
if(!IS(vColor, g_cColor))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetColor(*(Color *)DATA_PTR(vColor));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_left(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
return rb_float_new(pSelf->GetLeft());
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_top(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
return rb_float_new(pSelf->GetTop());
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_scale(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
float x = pSelf->GetScaleX();
|
||||
float y = pSelf->GetScaleY();
|
||||
VALUE vArr = rb_ary_new();
|
||||
rb_ary_push(vArr, rb_float_new(x));
|
||||
rb_ary_push(vArr, rb_float_new(y));
|
||||
return vArr;
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_rotation(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
return rb_float_new(pSelf->GetRotation());
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_upVector(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
float f = (pSelf->GetRotation() + 90) * (PI/180.0f);
|
||||
VALUE v = rb_ary_new();
|
||||
rb_ary_push(v, rb_float_new(COSF(f)));
|
||||
rb_ary_push(v, rb_float_new(-SINF(f)));
|
||||
return v;
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_rightVector(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
float f = pSelf->GetRotation() * (PI/180.0f);
|
||||
VALUE v = rb_ary_new();
|
||||
rb_ary_push(v, rb_float_new(COSF(f)));
|
||||
rb_ary_push(v, rb_float_new(-SINF(f)));
|
||||
return v;
|
||||
}
|
||||
|
||||
static VALUE Drawable_get_color(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
DECLARE_OBJ_VAR(Color, Color, pSelf->GetColor());
|
||||
return vColor;
|
||||
}
|
||||
|
||||
static VALUE Drawable_setPosition(VALUE vSelf, float vX, float vY) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->SetPosition((float)NUM2DBL(vX), (float)NUM2DBL(vY));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_move(VALUE vSelf, float vX, float vY) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->Move((float)NUM2DBL(vX), (float)NUM2DBL(vY));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Drawable_rotate(VALUE vSelf, VALUE vRotate) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Drawable *pSelf;
|
||||
Data_Get_Struct(vSelf, Drawable, pSelf);
|
||||
pSelf->Rotate((float)NUM2DBL(vRotate));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
void Init_Drawable()
|
||||
{
|
||||
g_cDrawable = rb_define_class_under(g_vModule, "Drawable", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Drawable, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Drawable, initialize, -1);
|
||||
|
||||
DEFINE_RW(Drawable, left);
|
||||
DEFINE_RW(Drawable, top);
|
||||
DEFINE_RW(Drawable, scale);
|
||||
DEFINE_RW(Drawable, color);
|
||||
DEFINE_RW(Drawable, rotation);
|
||||
DEFINE_GETTER(Drawable, upVector);
|
||||
DEFINE_GETTER(Drawable, rightVector);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Drawable, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Drawable, setRotationCenter, 2);
|
||||
DEFINE_INSTANCE_METHOD(Drawable, setPosition, 2);
|
||||
DEFINE_INSTANCE_METHOD(Drawable, move, 2);
|
||||
DEFINE_INSTANCE_METHOD(Drawable, rotate, 1);
|
||||
}
|
||||
|
270
ruby/RubySFML/sfEvent.cpp
Normal file
270
ruby/RubySFML/sfEvent.cpp
Normal file
|
@ -0,0 +1,270 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cEvent;
|
||||
|
||||
void Event_free(void *p) { delete (Event *)p; }
|
||||
VALUE Event_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Event *ptr = new Event();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Event_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Event_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Event_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
char szBuffer[1024];
|
||||
szBuffer[0] = 0;
|
||||
switch(pSelf->Type) {
|
||||
case Event::Closed:
|
||||
strcpy(szBuffer, "Closed");
|
||||
break;
|
||||
case Event::Resized:
|
||||
sprintf(szBuffer, "Resized: Width=%d, Height=%d", pSelf->Size.Width, pSelf->Size.Height);
|
||||
break;
|
||||
case Event::LostFocus:
|
||||
strcpy(szBuffer, "LostFocus");
|
||||
break;
|
||||
case Event::GainedFocus:
|
||||
strcpy(szBuffer, "GainedFocus");
|
||||
break;
|
||||
case Event::TextEntered:
|
||||
sprintf(szBuffer, "TextEntered: %C", pSelf->Text.Unicode);
|
||||
break;
|
||||
case Event::KeyPressed:
|
||||
sprintf(szBuffer, "KeyPressed: Code=%d, Alt=%s, Ctrl=%s, Shift=%s", pSelf->Key.Code, pSelf->Key.Alt ? "true" : "false", pSelf->Key.Control ? "true" : "false", pSelf->Key.Shift ? "true" : "false");
|
||||
break;
|
||||
case Event::KeyReleased:
|
||||
sprintf(szBuffer, "KeyReleased: Code=%d, Alt=%s, Ctrl=%s, Shift=%s", pSelf->Key.Code, pSelf->Key.Alt ? "true" : "false", pSelf->Key.Control ? "true" : "false", pSelf->Key.Shift ? "true" : "false");
|
||||
break;
|
||||
case Event::MouseWheelMoved:
|
||||
sprintf(szBuffer, "MouseWheelMoved: Delta=%d", pSelf->MouseWheel.Delta);
|
||||
case Event::MouseButtonPressed:
|
||||
sprintf(szBuffer, "MouseButtonPressed: Button=%d", pSelf->MouseButton.Button);
|
||||
break;
|
||||
case Event::MouseButtonReleased:
|
||||
sprintf(szBuffer, "MouseButtonReleased: Button=%d", pSelf->MouseButton.Button);
|
||||
break;
|
||||
case Event::MouseMoved:
|
||||
sprintf(szBuffer, "MouseMoved: X=%d, Y=%d", pSelf->MouseMove.X, pSelf->MouseMove.Y);
|
||||
break;
|
||||
case Event::JoyButtonPressed:
|
||||
sprintf(szBuffer, "JoyButtonPressed: Button=%d", pSelf->JoyButton.Button);
|
||||
break;
|
||||
case Event::JoyButtonReleased:
|
||||
sprintf(szBuffer, "JoyButtonReleased: Button=%d", pSelf->JoyButton.Button);
|
||||
break;
|
||||
case Event::JoyMoved:
|
||||
sprintf(szBuffer, "JoyMoved: Axis=%d, Pos=%f", pSelf->JoyMove.Axis, pSelf->JoyMove.Position);
|
||||
break;
|
||||
default:
|
||||
sprintf(szBuffer, "Unsupported event type: %d", pSelf->Type);
|
||||
break;
|
||||
}
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Event_type(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
return INT2FIX(pSelf->Type);
|
||||
}
|
||||
|
||||
static VALUE Event_char(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
char szBuffer[4];
|
||||
sprintf(szBuffer, "%C", pSelf->Text.Unicode);
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Event_code(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::KeyPressed || pSelf->Type == Event::KeyReleased)
|
||||
return INT2FIX(pSelf->Key.Code);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_alt(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::KeyPressed || pSelf->Type == Event::KeyReleased)
|
||||
return pSelf->Key.Alt ? Qtrue : Qfalse;
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_control(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::KeyPressed || pSelf->Type == Event::KeyReleased)
|
||||
return pSelf->Key.Control ? Qtrue : Qfalse;
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_shift(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::KeyPressed || pSelf->Type == Event::KeyReleased)
|
||||
return pSelf->Key.Shift ? Qtrue : Qfalse;
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_delta(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::MouseWheelMoved)
|
||||
return INT2FIX(pSelf->MouseWheel.Delta);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_buttons(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::MouseButtonPressed || pSelf->Type == Event::MouseButtonReleased)
|
||||
return INT2FIX(pSelf->MouseButton.Button);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_button(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::JoyButtonPressed || pSelf->Type == Event::JoyButtonReleased)
|
||||
return INT2FIX(pSelf->JoyButton.Button);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_x(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::MouseMoved)
|
||||
return INT2FIX(pSelf->MouseMove.X);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_y(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::MouseMoved)
|
||||
return INT2FIX(pSelf->MouseMove.Y);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_axis(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::JoyMoved)
|
||||
return INT2FIX(pSelf->JoyMove.Axis);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_pos(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::JoyMoved)
|
||||
return rb_float_new(pSelf->JoyMove.Position);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_width(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::Resized)
|
||||
return INT2FIX(pSelf->Size.Width);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
static VALUE Event_height(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Event *pSelf;
|
||||
Data_Get_Struct(vSelf, Event, pSelf);
|
||||
if(pSelf->Type == Event::Resized)
|
||||
return INT2FIX(pSelf->Size.Height);
|
||||
rb_raise(rb_eTypeError, "wrong event type");
|
||||
}
|
||||
|
||||
void Init_Event()
|
||||
{
|
||||
g_cEvent = rb_define_class_under(g_vModule, "Event", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Event, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Event, initialize, -1);
|
||||
|
||||
// For any event type
|
||||
DEFINE_INSTANCE_METHOD(Event, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, type, 0);
|
||||
|
||||
// For text events
|
||||
DEFINE_INSTANCE_METHOD(Event, char, 0);
|
||||
|
||||
// For keyboard event types
|
||||
DEFINE_INSTANCE_METHOD(Event, code, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, alt, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, control, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, shift, 0);
|
||||
|
||||
// For mouse/joystick event types
|
||||
DEFINE_INSTANCE_METHOD(Event, delta, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, buttons, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, button, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, x, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, y, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, axis, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, pos, 0);
|
||||
|
||||
// For size event types
|
||||
DEFINE_INSTANCE_METHOD(Event, width, 0);
|
||||
DEFINE_INSTANCE_METHOD(Event, height, 0);
|
||||
}
|
||||
|
155
ruby/RubySFML/sfFloatRect.cpp
Normal file
155
ruby/RubySFML/sfFloatRect.cpp
Normal file
|
@ -0,0 +1,155 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cFloatRect;
|
||||
|
||||
DECLARE_DOUBLE_RW(FloatRect, Left);
|
||||
DECLARE_DOUBLE_RW(FloatRect, Top);
|
||||
DECLARE_DOUBLE_RW(FloatRect, Right);
|
||||
DECLARE_DOUBLE_RW(FloatRect, Bottom);
|
||||
|
||||
|
||||
void FloatRect_free(void *p) { delete (FloatRect *)p; }
|
||||
VALUE FloatRect_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
FloatRect *ptr = new FloatRect();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, FloatRect_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE FloatRect_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 4 &&
|
||||
ISNUM(argv[0]) &&
|
||||
ISNUM(argv[1]) &&
|
||||
ISNUM(argv[2]) &&
|
||||
ISNUM(argv[3])) {
|
||||
pSelf->Left = (float)NUM2DBL(argv[0]);
|
||||
pSelf->Top = (float)NUM2DBL(argv[1]);
|
||||
pSelf->Right = (float)NUM2DBL(argv[2]);
|
||||
pSelf->Bottom = (float)NUM2DBL(argv[3]);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE FloatRect_to_s(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Left=%f, Top=%f, Right=%f, Bottom=%f", pSelf->Left, pSelf->Top, pSelf->Right, pSelf->Bottom);
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE FloatRect_get_Width(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
return rb_float_new(pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE FloatRect_set_Width(VALUE vSelf, VALUE vWidth)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
float nWidth = (float)NUM2DBL(vWidth);
|
||||
pSelf->Right = pSelf->Left + nWidth;
|
||||
return vWidth;
|
||||
}
|
||||
|
||||
static VALUE FloatRect_get_Height(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
return rb_float_new(pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE FloatRect_set_Height(VALUE vSelf, VALUE vHeight)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
float nHeight = (float)NUM2DBL(vHeight);
|
||||
pSelf->Bottom = pSelf->Top + nHeight;
|
||||
return vHeight;
|
||||
}
|
||||
|
||||
static VALUE FloatRect_contains(VALUE vSelf, VALUE x, VALUE y)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
return pSelf->Contains((float)NUM2DBL(x), (float)NUM2DBL(y)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE FloatRect_intersects(VALUE vSelf, VALUE vRect)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
FloatRect *pSelf = NULL, *pRect = NULL;
|
||||
FloatRect r;
|
||||
Data_Get_Struct(vSelf, FloatRect, pSelf);
|
||||
Data_Get_Struct(vRect, FloatRect, pRect);
|
||||
if(!pSelf->Intersects(*pRect, &r))
|
||||
return Qnil;
|
||||
DECLARE_OBJ_VAR(FloatRect, Result, r);
|
||||
return vResult;
|
||||
}
|
||||
|
||||
void Init_FloatRect()
|
||||
{
|
||||
g_cFloatRect = rb_define_class_under(g_vModule, "FloatRect", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(FloatRect, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(FloatRect, initialize, -1);
|
||||
|
||||
DEFINE_RW2(FloatRect, Left, left);
|
||||
DEFINE_RW2(FloatRect, Left, l);
|
||||
DEFINE_RW2(FloatRect, Top, top);
|
||||
DEFINE_RW2(FloatRect, Top, t);
|
||||
DEFINE_RW2(FloatRect, Right, right);
|
||||
DEFINE_RW2(FloatRect, Right, r);
|
||||
DEFINE_RW2(FloatRect, Bottom, bottom);
|
||||
DEFINE_RW2(FloatRect, Bottom, b);
|
||||
DEFINE_RW2(FloatRect, Width, width);
|
||||
DEFINE_RW2(FloatRect, Width, w);
|
||||
DEFINE_RW2(FloatRect, Height, height);
|
||||
DEFINE_RW2(FloatRect, Height, h);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(FloatRect, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(FloatRect, contains, 2);
|
||||
DEFINE_INSTANCE_METHOD(FloatRect, intersects, 1);
|
||||
}
|
||||
|
238
ruby/RubySFML/sfImage.cpp
Normal file
238
ruby/RubySFML/sfImage.cpp
Normal file
|
@ -0,0 +1,238 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cImage;
|
||||
|
||||
void Image_free(void *p) { delete (Image *)p; }
|
||||
VALUE Image_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Image *ptr = new Image();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Image_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Image_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 1 && IS(argv[0], g_cImage)) {
|
||||
*pSelf = *(Image *)DATA_PTR(argv[0]);
|
||||
} else if(argc == 1 && ISSTR(argv[0])) {
|
||||
if(!pSelf->LoadFromFile(STR2CSTR(argv[0])))
|
||||
rb_raise(rb_eRuntimeError, "Failed to load specified image file");
|
||||
} else if(argc >= 2 && argc <= 3 &&
|
||||
ISNUM(argv[0]) &&
|
||||
ISNUM(argv[1]) &&
|
||||
(argc < 3 || IS(argv[2], g_cColor))) {
|
||||
pSelf->Create(NUM2INT(argv[0]), NUM2INT(argv[1]),
|
||||
argc < 3 ? Color::Black : *(Color *)DATA_PTR(argv[2]));
|
||||
} else if(argc == 2 &&
|
||||
ISSTR(argv[0]) &&
|
||||
ISNUM(argv[1])) {
|
||||
pSelf->LoadFromMemory(STR2CSTR(argv[2]), NUM2INT(argv[0]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Image_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Width=%d, Height=%d", pSelf->GetWidth(), pSelf->GetHeight());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Image_loadFromFile(VALUE vSelf, VALUE vPath) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
return pSelf->LoadFromFile(STR2CSTR(vPath)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Image_saveToFile(VALUE vSelf, VALUE vPath) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
return pSelf->SaveToFile(STR2CSTR(vPath)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Image_create(VALUE vSelf, VALUE vWidth, VALUE vHeight, VALUE vColor) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
if(!IS(vColor, g_cColor))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return pSelf->Create(NUM2INT(vWidth), NUM2INT(vHeight), *(Color *)DATA_PTR(vColor));
|
||||
}
|
||||
|
||||
static VALUE Image_loadFromMemory(VALUE vSelf, VALUE vData, VALUE vSize) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
return pSelf->LoadFromMemory(STR2CSTR(vData), NUM2INT(vSize));
|
||||
}
|
||||
|
||||
static VALUE Image_createMaskFromColor(VALUE vSelf, VALUE vColor) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
if(IS(vColor, g_cColor))
|
||||
pSelf->CreateMaskFromColor(*(Color *)DATA_PTR(vColor));
|
||||
else if(ISNUM(vColor)) {
|
||||
DWORD dw = NUM2UINT(vColor);
|
||||
Color c((dw >> 24) & 0xFF, (dw >> 16) & 0xFF, (dw >> 8) & 0xFF, (dw >> 0) & 0xFF);
|
||||
pSelf->CreateMaskFromColor(c);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Image_resize(VALUE vSelf, VALUE vWidth, VALUE vHeight, VALUE vColor) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
if(IS(vColor, g_cColor))
|
||||
return pSelf->Resize(NUM2INT(vWidth), NUM2INT(vHeight), *(Color *)DATA_PTR(vColor));
|
||||
else if(ISNUM(vColor)) {
|
||||
DWORD dw = NUM2UINT(vColor);
|
||||
Color c((dw >> 24) & 0xFF, (dw >> 16) & 0xFF, (dw >> 8) & 0xFF, (dw >> 0) & 0xFF);
|
||||
return pSelf->Resize(NUM2INT(vWidth), NUM2INT(vHeight), c);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Image_setPixel(VALUE vSelf, VALUE vX, VALUE vY, VALUE vColor) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
if(IS(vColor, g_cColor))
|
||||
pSelf->SetPixel(NUM2INT(vX), NUM2INT(vY), *(Color *)DATA_PTR(vColor));
|
||||
else if(ISNUM(vColor)) {
|
||||
DWORD dw = NUM2UINT(vColor);
|
||||
Color c((dw >> 24) & 0xFF, (dw >> 16) & 0xFF, (dw >> 8) & 0xFF, (dw >> 0) & 0xFF);
|
||||
pSelf->SetPixel(NUM2INT(vX), NUM2INT(vY), c);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Image_getPixel(VALUE vSelf, VALUE vX, VALUE vY) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
DECLARE_OBJ_VAR(Color, Color, pSelf->GetPixel(NUM2INT(vX), NUM2INT(vY)));
|
||||
return vColor;
|
||||
}
|
||||
|
||||
static VALUE Image_bind(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
pSelf->Bind();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Image_set_smooth(VALUE vSelf, VALUE vSmooth) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
pSelf->SetSmooth(RTEST(vSmooth));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Image_set_repeat(VALUE vSelf, VALUE vRepeat) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
pSelf->SetRepeat(RTEST(vRepeat));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Image_get_width(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
return INT2FIX(pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE Image_get_height(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
return INT2FIX(pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE Image_getTexCoords(VALUE vSelf, VALUE vRect) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Image *pSelf;
|
||||
Data_Get_Struct(vSelf, Image, pSelf);
|
||||
if(!IS(vRect, g_cIntRect))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
DECLARE_OBJ_VAR(FloatRect, Coord, pSelf->GetTexCoords(*(IntRect *)DATA_PTR(vRect)));
|
||||
return vCoord;
|
||||
}
|
||||
|
||||
static VALUE Image_getValidTextureSize(VALUE vClass, VALUE vSize) {
|
||||
// Get C++ object pointer from vSelf
|
||||
return INT2FIX(Image::GetValidTextureSize(NUM2INT(vSize)));
|
||||
}
|
||||
|
||||
void Init_Image()
|
||||
{
|
||||
g_cImage = rb_define_class_under(g_vModule, "Image", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Image, new, -1);
|
||||
DEFINE_CLASS_METHOD(Image, getValidTextureSize, 1);
|
||||
DEFINE_INSTANCE_METHOD(Image, initialize, -1);
|
||||
|
||||
DEFINE_SETTER(Image, smooth);
|
||||
DEFINE_SETTER(Image, repeat);
|
||||
DEFINE_GETTER(Image, width);
|
||||
DEFINE_GETTER2(Image, width, w);
|
||||
DEFINE_GETTER(Image, height);
|
||||
DEFINE_GETTER2(Image, height, h);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Image, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Image, loadFromFile, 1);
|
||||
DEFINE_INSTANCE_METHOD(Image, saveToFile, 1);
|
||||
DEFINE_INSTANCE_METHOD(Image, create, 3);
|
||||
DEFINE_INSTANCE_METHOD(Image, loadFromMemory, 2);
|
||||
DEFINE_INSTANCE_METHOD(Image, createMaskFromColor, 1);
|
||||
DEFINE_INSTANCE_METHOD(Image, resize, 3);
|
||||
DEFINE_INSTANCE_METHOD(Image, setPixel, 3);
|
||||
DEFINE_INSTANCE_METHOD(Image, getPixel, 2);
|
||||
DEFINE_INSTANCE_METHOD(Image, bind, 0);
|
||||
DEFINE_INSTANCE_METHOD(Image, getTexCoords, 1);
|
||||
DEFINE_INSTANCE_METHOD2(Image, getPixel, [], 2);
|
||||
DEFINE_INSTANCE_METHOD2(Image, setPixel, []=, 2);
|
||||
}
|
||||
|
105
ruby/RubySFML/sfInput.cpp
Normal file
105
ruby/RubySFML/sfInput.cpp
Normal file
|
@ -0,0 +1,105 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cInput;
|
||||
|
||||
void Input_free(void *p) { delete (Input *)p; }
|
||||
VALUE Input_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Input *ptr = new Input();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Input_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Input_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Input_isKeyDown(VALUE vSelf, VALUE vKey) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
return pSelf->IsKeyDown((Key::Code)NUM2INT(vKey)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Input_isMouseButtonDown(VALUE vSelf, VALUE vButton) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
return pSelf->IsMouseButtonDown((Mouse::Button)NUM2INT(vButton)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Input_isJoystickButtonDown(VALUE vSelf, VALUE vJoy, VALUE vButton) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
return pSelf->IsJoystickButtonDown(NUM2INT(vJoy), NUM2INT(vButton)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Input_getMouseX(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
return INT2FIX(pSelf->GetMouseX());
|
||||
}
|
||||
|
||||
static VALUE Input_getMouseY(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
return INT2FIX(pSelf->GetMouseY());
|
||||
}
|
||||
|
||||
static VALUE Input_getJoystickAxis(VALUE vSelf, VALUE vJoy, VALUE vAxis) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Input *pSelf;
|
||||
Data_Get_Struct(vSelf, Input, pSelf);
|
||||
return INT2FIX(pSelf->GetJoystickAxis(NUM2INT(vJoy), (Joy::Axis)NUM2INT(vAxis)));
|
||||
}
|
||||
|
||||
void Init_Input()
|
||||
{
|
||||
g_cInput = rb_define_class_under(g_vModule, "Input", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Input, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Input, initialize, -1);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Input, isKeyDown, 1);
|
||||
DEFINE_INSTANCE_METHOD(Input, isMouseButtonDown, 1);
|
||||
DEFINE_INSTANCE_METHOD(Input, isJoystickButtonDown, 2);
|
||||
DEFINE_INSTANCE_METHOD(Input, getMouseX, 0);
|
||||
DEFINE_INSTANCE_METHOD(Input, getMouseY, 0);
|
||||
DEFINE_INSTANCE_METHOD(Input, getJoystickAxis, 2);
|
||||
}
|
||||
|
157
ruby/RubySFML/sfIntRect.cpp
Normal file
157
ruby/RubySFML/sfIntRect.cpp
Normal file
|
@ -0,0 +1,157 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cIntRect;
|
||||
|
||||
DECLARE_INT_RW(IntRect, Left);
|
||||
DECLARE_INT_RW(IntRect, Top);
|
||||
DECLARE_INT_RW(IntRect, Right);
|
||||
DECLARE_INT_RW(IntRect, Bottom);
|
||||
|
||||
|
||||
void IntRect_free(void *p) { delete (IntRect *)p; }
|
||||
VALUE IntRect_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
// For each version of this method, convert Ruby args to C++ types (applying default values)
|
||||
IntRect *ptr = new IntRect();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, IntRect_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE IntRect_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 4 &&
|
||||
ISNUM(argv[0]) &&
|
||||
ISNUM(argv[1]) &&
|
||||
ISNUM(argv[2]) &&
|
||||
ISNUM(argv[3])) {
|
||||
pSelf->Left = NUM2INT(argv[0]);
|
||||
pSelf->Top = NUM2INT(argv[1]);
|
||||
pSelf->Right = NUM2INT(argv[2]);
|
||||
pSelf->Bottom = NUM2INT(argv[3]);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE IntRect_to_s(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Left=%d, Top=%d, Right=%d, Bottom=%d", pSelf->Left, pSelf->Top, pSelf->Right, pSelf->Bottom);
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE IntRect_get_Width(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
return INT2NUM(pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE IntRect_set_Width(VALUE vSelf, VALUE vWidth)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
int nWidth = NUM2INT(vWidth);
|
||||
pSelf->Right = pSelf->Left + nWidth;
|
||||
return vWidth;
|
||||
}
|
||||
|
||||
static VALUE IntRect_get_Height(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
return INT2NUM(pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE IntRect_set_Height(VALUE vSelf, VALUE vHeight)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
int nHeight = NUM2INT(vHeight);
|
||||
pSelf->Bottom = pSelf->Top + nHeight;
|
||||
return vHeight;
|
||||
}
|
||||
|
||||
static VALUE IntRect_contains(VALUE vSelf, VALUE x, VALUE y)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
return pSelf->Contains(NUM2INT(x), NUM2INT(y)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE IntRect_intersects(VALUE vSelf, VALUE vRect)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
IntRect *pSelf = NULL, *pRect = NULL;
|
||||
IntRect r;
|
||||
Data_Get_Struct(vSelf, IntRect, pSelf);
|
||||
Data_Get_Struct(vRect, IntRect, pRect);
|
||||
if(!pSelf->Intersects(*pRect, &r))
|
||||
return Qnil;
|
||||
DECLARE_OBJ_VAR(IntRect, Result, r);
|
||||
return vResult;
|
||||
}
|
||||
|
||||
void Init_IntRect()
|
||||
{
|
||||
g_cIntRect = rb_define_class_under(g_vModule, "IntRect", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(IntRect, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(IntRect, initialize, -1);
|
||||
|
||||
DEFINE_RW2(IntRect, Left, left);
|
||||
DEFINE_RW2(IntRect, Left, l);
|
||||
DEFINE_RW2(IntRect, Top, top);
|
||||
DEFINE_RW2(IntRect, Top, t);
|
||||
DEFINE_RW2(IntRect, Right, right);
|
||||
DEFINE_RW2(IntRect, Right, r);
|
||||
DEFINE_RW2(IntRect, Bottom, bottom);
|
||||
DEFINE_RW2(IntRect, Bottom, b);
|
||||
DEFINE_RW2(IntRect, Width, width);
|
||||
DEFINE_RW2(IntRect, Width, w);
|
||||
DEFINE_RW2(IntRect, Height, height);
|
||||
DEFINE_RW2(IntRect, Height, h);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(IntRect, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(IntRect, contains, 2);
|
||||
DEFINE_INSTANCE_METHOD(IntRect, intersects, 1);
|
||||
}
|
||||
|
153
ruby/RubySFML/sfMusic.cpp
Normal file
153
ruby/RubySFML/sfMusic.cpp
Normal file
|
@ -0,0 +1,153 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Audio.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cMusic;
|
||||
|
||||
void Music_free(void *p) { delete (Music *)p; }
|
||||
|
||||
VALUE Music_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Music *ptr = NULL;
|
||||
if(argc == 1 && ISNUM(argv[0]))
|
||||
ptr = new Music(NUM2INT(argv[0]));
|
||||
else
|
||||
ptr = new Music();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Music_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Music_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 1 && ISNUM(argv[0])) {
|
||||
// Was forced to set this above in Music_new()
|
||||
} else if(argc == 1 && ISSTR(argv[0])) {
|
||||
if(!pSelf->Open(STR2CSTR(argv[0])))
|
||||
rb_raise(rb_eRuntimeError, "Failed to load specified music file");
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Music_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Channels=%d, SampleRate=%d, Duration=%f, Status=%s", pSelf->GetChannelsCount(), pSelf->GetSampleRate(), pSelf->GetDuration(), pSelf->GetStatus() == Sound::Playing ? "playing" : pSelf->GetStatus() == Sound::Paused ? "paused" : "stopped");
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Music_open(VALUE vSelf, VALUE vFile) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
return pSelf->Open(STR2CSTR(vFile)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Music_play(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
pSelf->Play();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Music_stop(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
pSelf->Stop();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Music_set_loop(VALUE vSelf, VALUE vLoop) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
pSelf->SetLoop(RTEST(vLoop));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Music_get_loop(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
return pSelf->GetLoop() ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Music_get_duration(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
return rb_float_new(pSelf->GetDuration());
|
||||
}
|
||||
|
||||
static VALUE Music_get_channels(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
return INT2FIX(pSelf->GetChannelsCount());
|
||||
}
|
||||
|
||||
static VALUE Music_get_sampleRate(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
return INT2FIX(pSelf->GetSampleRate());
|
||||
}
|
||||
|
||||
static VALUE Music_get_status(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Music *pSelf;
|
||||
Data_Get_Struct(vSelf, Music, pSelf);
|
||||
return INT2FIX(pSelf->GetStatus());
|
||||
}
|
||||
|
||||
void Init_Music()
|
||||
{
|
||||
g_cMusic = rb_define_class_under(g_vModule, "Music", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Music, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Music, initialize, -1);
|
||||
|
||||
DEFINE_RW(Music, loop);
|
||||
DEFINE_GETTER(Music, duration);
|
||||
DEFINE_GETTER(Music, status);
|
||||
DEFINE_GETTER(Music, channels);
|
||||
DEFINE_GETTER(Music, sampleRate);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Music, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Music, open, 1);
|
||||
DEFINE_INSTANCE_METHOD(Music, play, 0);
|
||||
DEFINE_INSTANCE_METHOD(Music, stop, 0);
|
||||
}
|
||||
|
147
ruby/RubySFML/sfPostFX.cpp
Normal file
147
ruby/RubySFML/sfPostFX.cpp
Normal file
|
@ -0,0 +1,147 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
// Wrapper for PostFX that calls the Ruby version of virtual methods in case a Ruby class derives from this one
|
||||
class rPostFX : public PostFX {
|
||||
protected:
|
||||
VALUE m_vSelf, m_vClass;
|
||||
ID m_idRender;
|
||||
bool m_bRender, m_bInRender;
|
||||
|
||||
virtual void Render(const RenderWindow &Window) const {
|
||||
// If this method is overridden in Ruby, call it
|
||||
if(m_bRender) {
|
||||
VALUE vWindow = Data_Wrap_Struct(g_cRenderWindow, 0, 0, (void*)&Window);
|
||||
VALUE vRet = rb_funcall(m_vSelf, m_idRender, 1, vWindow);
|
||||
} else // else call parent
|
||||
PostFX::Render(Window);
|
||||
}
|
||||
|
||||
public:
|
||||
// Damn constructors should be inherited from base class
|
||||
rPostFX() : PostFX() {}
|
||||
rPostFX(const PostFX &Copy) : PostFX(Copy) {}
|
||||
|
||||
// Call as soon as you get a Ruby VALUE pointing to this object
|
||||
void rInit(VALUE vSelf) {
|
||||
// Need these for rb_funcall
|
||||
m_vSelf = vSelf;
|
||||
m_vClass = CLASS_OF(m_vSelf);
|
||||
|
||||
// Initialize members for Render() virtual method
|
||||
m_idRender = rb_intern("render");
|
||||
m_bRender = rb_method_boundp(m_vClass, m_idRender, 0) == Qtrue;
|
||||
}
|
||||
|
||||
// When an overridden method in Ruby calls super(), it is called this way
|
||||
VALUE Render(VALUE vWindow) {
|
||||
PostFX::Render(*(RenderWindow *)DATA_PTR(vWindow));
|
||||
return Qnil;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
VALUE g_cPostFX;
|
||||
|
||||
void PostFX_free(void *p) { delete (PostFX *)p; }
|
||||
VALUE PostFX_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
// For each version of this method, convert Ruby args to C++ types (applying default values)
|
||||
rPostFX *ptr = new rPostFX();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, PostFX_free, ptr);
|
||||
ptr->rInit(tData);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE PostFX_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
PostFX *pSelf;
|
||||
Data_Get_Struct(vSelf, PostFX, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 1 && ISSTR(argv[0])) {
|
||||
pSelf->LoadFromFile(STR2CSTR(argv[0]));
|
||||
} else if(argc == 1 && IS(argv[0], g_cPostFX)) {
|
||||
*pSelf = *(PostFX *)DATA_PTR(argv[0]);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE PostFX_loadFromFile(VALUE vSelf, VALUE vFile) {
|
||||
// Get C++ object pointer from vSelf
|
||||
PostFX *pSelf;
|
||||
Data_Get_Struct(vSelf, PostFX, pSelf);
|
||||
return pSelf->LoadFromFile(STR2CSTR(vFile)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE PostFX_setParameter(int argc, VALUE *argv, VALUE vSelf) {
|
||||
PostFX *pSelf;
|
||||
Data_Get_Struct(vSelf, PostFX, pSelf);
|
||||
if(argc == 2 && ISSTR(argv[0]) && ISNUM(argv[1])) {
|
||||
pSelf->SetParameter(STR2CSTR(argv[0]), (float)NUM2DBL(argv[1]));
|
||||
} else if(argc == 3 && ISSTR(argv[0]) && ISNUM(argv[1]) && ISNUM(argv[2])) {
|
||||
pSelf->SetParameter(STR2CSTR(argv[0]), (float)NUM2DBL(argv[1]), (float)NUM2DBL(argv[2]));
|
||||
} else if(argc == 4 && ISSTR(argv[0]) && ISNUM(argv[1]) && ISNUM(argv[2]) && ISNUM(argv[3])) {
|
||||
pSelf->SetParameter(STR2CSTR(argv[0]), (float)NUM2DBL(argv[1]), (float)NUM2DBL(argv[2]), (float)NUM2DBL(argv[3]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE PostFX_setTexture(VALUE vSelf, VALUE vName, VALUE vImage) {
|
||||
// Get C++ object pointer from vSelf
|
||||
PostFX *pSelf;
|
||||
Data_Get_Struct(vSelf, PostFX, pSelf);
|
||||
if(!IS(vImage, g_cImage))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetTexture(STR2CSTR(vName), (Image *)DATA_PTR(vImage));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE PostFX_render(VALUE vSelf, VALUE vWindow) {
|
||||
// Get C++ object pointer from vSelf
|
||||
rPostFX *pSelf;
|
||||
Data_Get_Struct(vSelf, rPostFX, pSelf);
|
||||
return pSelf->Render(vWindow);
|
||||
}
|
||||
|
||||
void Init_PostFX()
|
||||
{
|
||||
g_cPostFX = rb_define_class_under(g_vModule, "PostFX", g_cDrawable);
|
||||
DEFINE_CLASS_METHOD(PostFX, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(PostFX, initialize, -1);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(PostFX, setParameter, -1);
|
||||
DEFINE_INSTANCE_METHOD(PostFX, loadFromFile, 1);
|
||||
DEFINE_INSTANCE_METHOD(PostFX, setTexture, 2);
|
||||
|
||||
// Virtual method
|
||||
DEFINE_INSTANCE_METHOD(PostFX, render, 1);
|
||||
}
|
||||
|
237
ruby/RubySFML/sfRenderWindow.cpp
Normal file
237
ruby/RubySFML/sfRenderWindow.cpp
Normal file
|
@ -0,0 +1,237 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cRenderWindow;
|
||||
|
||||
void RenderWindow_free(void *p) { delete (RenderWindow *)p; }
|
||||
VALUE RenderWindow_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
// For each version of this method, convert Ruby args to C++ types (applying default values)
|
||||
RenderWindow *ptr = new RenderWindow();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, RenderWindow_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
if(argc >= 2 && argc <= 4 &&
|
||||
IS(argv[0], g_cVideoMode) &&
|
||||
ISSTR(argv[1]) &&
|
||||
(argc < 3 || true) &&
|
||||
(argc < 4 || ISNUM(argv[3]))) {
|
||||
pSelf->Create(
|
||||
*(VideoMode *)DATA_PTR(argv[0]),
|
||||
STR2CSTR(argv[1]),
|
||||
argc < 3 ? (Style::Resize | Style::Close) : NUM2INT(argv[2]),
|
||||
argc < 4 ? 0 : NUM2INT(argv[3]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Height: %d, Width: %d, Depth Bits: %d, Stencil Bits: %d", pSelf->GetWidth(), pSelf->GetHeight(), pSelf->GetDepthBits(), pSelf->GetStencilBits());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_width(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
return INT2FIX(pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_height(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
return INT2FIX(pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_depthBits(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
return INT2FIX(pSelf->GetDepthBits());
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_stencilBits(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
return INT2FIX(pSelf->GetStencilBits());
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_frameTime(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
return rb_float_new(pSelf->GetFrameTime());
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_input(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
DECLARE_PTR_VAR(Input, Input, (Input *)&pSelf->GetInput());
|
||||
return vInput;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_getEvent(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
Event ePoll;
|
||||
if(!pSelf->GetEvent(ePoll))
|
||||
return Qnil;
|
||||
DECLARE_OBJ_VAR(Event, Event, ePoll);
|
||||
return vEvent;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_useVerticalSync(VALUE vSelf, VALUE vEnabled) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
pSelf->UseVerticalSync(RTEST(vEnabled) != 0);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_showMouseCursor(VALUE vSelf, VALUE vShow) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
pSelf->ShowMouseCursor(RTEST(vShow) != 0);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_display(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
pSelf->Display();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_optimize(VALUE vSelf, VALUE vBool) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
pSelf->OptimizeForNonOpenGL(RTEST(vBool));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_capture(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
DECLARE_OBJ_VAR(Image, Image, pSelf->Capture());
|
||||
return vImage;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_get_view(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
DECLARE_OBJ_VAR(FloatRect, Rect, pSelf->GetViewRect());
|
||||
return vRect;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_set_view(VALUE vSelf, VALUE vView) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
View *pView;
|
||||
Data_Get_Struct(vView, View, pView);
|
||||
pSelf->SetView(pView);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_set_backgroundColor(VALUE vSelf, VALUE vColor) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
Color *pColor;
|
||||
Data_Get_Struct(vColor, Color, pColor);
|
||||
pSelf->SetBackgroundColor(*pColor);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_draw(VALUE vSelf, VALUE vObject) {
|
||||
if(!ISKO(vObject, g_cDrawable))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
Drawable *pObject;
|
||||
Data_Get_Struct(vObject, Drawable, pObject);
|
||||
pSelf->Draw(*pObject);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE RenderWindow_setFramerateLimit(VALUE vSelf, VALUE vLimit) {
|
||||
// Get C++ object pointer from vSelf
|
||||
RenderWindow *pSelf;
|
||||
Data_Get_Struct(vSelf, RenderWindow, pSelf);
|
||||
pSelf->SetFramerateLimit((unsigned int)NUM2INT(vLimit));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
void Init_RenderWindow()
|
||||
{
|
||||
g_cRenderWindow = rb_define_class_under(g_vModule, "RenderWindow", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(RenderWindow, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, initialize, -1);
|
||||
|
||||
DEFINE_GETTER(RenderWindow, width);
|
||||
DEFINE_GETTER2(RenderWindow, width, w);
|
||||
DEFINE_GETTER(RenderWindow, height);
|
||||
DEFINE_GETTER2(RenderWindow, height, h);
|
||||
DEFINE_GETTER(RenderWindow, input);
|
||||
DEFINE_GETTER(RenderWindow, frameTime);
|
||||
DEFINE_GETTER(RenderWindow, stencilBits);
|
||||
DEFINE_GETTER(RenderWindow, depthBits);
|
||||
DEFINE_RW(RenderWindow, view);
|
||||
DEFINE_SETTER(RenderWindow, backgroundColor);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, getEvent, 0); // Don't use GETTER
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, useVerticalSync, 1);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, showMouseCursor, 1);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, display, 0);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, optimize, 1);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, capture, 0);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, draw, 1);
|
||||
DEFINE_INSTANCE_METHOD(RenderWindow, setFramerateLimit, 1);
|
||||
}
|
||||
|
219
ruby/RubySFML/sfSound.cpp
Normal file
219
ruby/RubySFML/sfSound.cpp
Normal file
|
@ -0,0 +1,219 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Audio.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cSound;
|
||||
|
||||
void Sound_free(void *p) { delete (Sound *)p; }
|
||||
VALUE Sound_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Sound *ptr = new Sound();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Sound_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Sound_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
if(argc == 1 && IS(argv[0], g_cSound)) {
|
||||
*pSelf = *(Sound *)DATA_PTR(argv[0]);
|
||||
} else if(argc >= 0 && argc <= 7 &&
|
||||
(argc < 1 || IS(argv[0], g_cSoundBuffer))) {
|
||||
if(argc >= 1)
|
||||
pSelf->SetBuffer(*(SoundBuffer *)DATA_PTR(argv[0]));
|
||||
if(argc >= 2)
|
||||
pSelf->SetLoop(RTEST(argv[1]) != 0);
|
||||
if(argc >= 3)
|
||||
pSelf->SetPitch((float)NUM2DBL(argv[2]));
|
||||
if(argc >= 4)
|
||||
pSelf->SetVolume((float)NUM2DBL(argv[3]));
|
||||
if(argc >= 5) {
|
||||
float x = argc < 5 ? 0.0f : (float)NUM2DBL(argv[4]);
|
||||
float y = argc < 6 ? 0.0f : (float)NUM2DBL(argv[5]);
|
||||
float z = argc < 7 ? 0.0f : (float)NUM2DBL(argv[6]);
|
||||
pSelf->SetPosition(x, y, z);
|
||||
}
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Sound_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
char szBuffer[256];
|
||||
float x, y, z;
|
||||
pSelf->GetPosition(x, y, z);
|
||||
sprintf(szBuffer, "Loop=%s, Pitch=%2.2f, Volume=%2.2f, Position=(%2.2f,%2.2f,%2.2f), Status=%s", pSelf->GetLoop() ? "true":"false", pSelf->GetPitch(), pSelf->GetVolume(), x, y, z, pSelf->GetStatus() == Sound::Playing ? "playing" : pSelf->GetStatus() == Sound::Paused ? "paused" : "stopped");
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Sound_play(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->Play();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_pause(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->Pause();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_stop(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->Stop();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_set_buffer(VALUE vSelf, VALUE vBuffer) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
if(!IS(vBuffer, g_cSoundBuffer))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetBuffer(*(SoundBuffer *)DATA_PTR(vBuffer));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_set_loop(VALUE vSelf, VALUE vLoop) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->SetLoop(RTEST(vLoop));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_set_pitch(VALUE vSelf, VALUE vPitch) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->SetLoop((float)NUM2DBL(vPitch));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_set_volume(VALUE vSelf, VALUE vVolume) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->SetVolume((float)NUM2DBL(vVolume));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_set_position(VALUE vSelf, VALUE vX, VALUE vY, VALUE vZ) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
pSelf->SetPosition((float)NUM2DBL(vX), (float)NUM2DBL(vY), (float)NUM2DBL(vZ));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sound_get_buffer(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
DECLARE_PTR_VAR(SoundBuffer, SoundBuffer, (SoundBuffer *)pSelf->GetBuffer());
|
||||
return vSoundBuffer;
|
||||
}
|
||||
|
||||
static VALUE Sound_get_loop(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
return pSelf->GetLoop() ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE Sound_get_pitch(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
return rb_float_new(pSelf->GetPitch());
|
||||
}
|
||||
|
||||
static VALUE Sound_get_volume(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
return rb_float_new(pSelf->GetVolume());
|
||||
}
|
||||
|
||||
static VALUE Sound_get_position(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
float x, y, z;
|
||||
pSelf->GetPosition(x, y, z);
|
||||
VALUE vArray = rb_ary_new();
|
||||
rb_ary_push(vArray, rb_float_new(x));
|
||||
rb_ary_push(vArray, rb_float_new(y));
|
||||
rb_ary_push(vArray, rb_float_new(z));
|
||||
return vArray;
|
||||
}
|
||||
|
||||
static VALUE Sound_get_status(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
return INT2FIX(pSelf->GetStatus());
|
||||
}
|
||||
|
||||
static VALUE Sound_get_playingOffset(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sound *pSelf;
|
||||
Data_Get_Struct(vSelf, Sound, pSelf);
|
||||
return rb_float_new(pSelf->GetPlayingOffset());
|
||||
}
|
||||
|
||||
void Init_Sound()
|
||||
{
|
||||
g_cSound = rb_define_class_under(g_vModule, "Sound", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Sound, new, -1);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Sound, initialize, -1);
|
||||
|
||||
DEFINE_RW(Sound, buffer);
|
||||
DEFINE_RW(Sound, loop);
|
||||
DEFINE_RW(Sound, pitch);
|
||||
DEFINE_RW(Sound, volume);
|
||||
DEFINE_RW(Sound, position);
|
||||
DEFINE_GETTER(Sound, status);
|
||||
DEFINE_GETTER(Sound, playingOffset);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Sound, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Sound, play, 0);
|
||||
DEFINE_INSTANCE_METHOD(Sound, pause, 0);
|
||||
DEFINE_INSTANCE_METHOD(Sound, stop, 0);
|
||||
}
|
||||
|
137
ruby/RubySFML/sfSoundBuffer.cpp
Normal file
137
ruby/RubySFML/sfSoundBuffer.cpp
Normal file
|
@ -0,0 +1,137 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Audio.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cSoundBuffer;
|
||||
|
||||
void SoundBuffer_free(void *p) { delete (SoundBuffer *)p; }
|
||||
VALUE SoundBuffer_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
SoundBuffer *ptr = new SoundBuffer();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, SoundBuffer_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc == 1 && IS(argv[0], g_cSoundBuffer)) {
|
||||
*pSelf = *(SoundBuffer *)DATA_PTR(argv[0]);
|
||||
} else if(argc == 1 && ISSTR(argv[0])) {
|
||||
if(!pSelf->LoadFromFile(STR2CSTR(argv[0])))
|
||||
rb_raise(rb_eRuntimeError, "Failed to load specified sound file");
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Samples=%d, SampleRate=%d, Channels=%d, Duration=%f", pSelf->GetSamplesCount(), pSelf->GetSampleRate(), pSelf->GetChannelsCount(), pSelf->GetDuration());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_loadFromFile(VALUE vSelf, VALUE vPath) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return pSelf->LoadFromFile(STR2CSTR(vPath)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_saveToFile(VALUE vSelf, VALUE vPath) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return pSelf->SaveToFile(STR2CSTR(vPath)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_loadFromMemory(VALUE vSelf, VALUE vSamples, VALUE vSize) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return pSelf->LoadFromMemory(STR2CSTR(vSamples), NUM2INT(vSize)) ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_get_samples(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return rb_str_new((const char *)pSelf->GetSamples(), pSelf->GetSamplesCount()*2);
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_get_samplesCount(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return INT2FIX(pSelf->GetSamplesCount());
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_get_sampleRate(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return INT2FIX(pSelf->GetSampleRate());
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_get_channels(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return INT2FIX(pSelf->GetChannelsCount());
|
||||
}
|
||||
|
||||
static VALUE SoundBuffer_get_duration(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBuffer *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBuffer, pSelf);
|
||||
return rb_float_new(pSelf->GetDuration());
|
||||
}
|
||||
|
||||
void Init_SoundBuffer()
|
||||
{
|
||||
g_cSoundBuffer = rb_define_class_under(g_vModule, "SoundBuffer", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(SoundBuffer, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(SoundBuffer, initialize, -1);
|
||||
|
||||
DEFINE_GETTER(SoundBuffer, samples);
|
||||
DEFINE_GETTER(SoundBuffer, samplesCount);
|
||||
DEFINE_GETTER(SoundBuffer, sampleRate);
|
||||
DEFINE_GETTER(SoundBuffer, channels);
|
||||
DEFINE_GETTER(SoundBuffer, duration);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(SoundBuffer, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(SoundBuffer, loadFromFile, 1);
|
||||
DEFINE_INSTANCE_METHOD(SoundBuffer, saveToFile, 1);
|
||||
DEFINE_INSTANCE_METHOD(SoundBuffer, loadFromMemory, 2);
|
||||
}
|
||||
|
101
ruby/RubySFML/sfSoundBufferRecorder.cpp
Normal file
101
ruby/RubySFML/sfSoundBufferRecorder.cpp
Normal file
|
@ -0,0 +1,101 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Audio.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cSoundBufferRecorder;
|
||||
|
||||
void SoundBufferRecorder_free(void *p) { delete (SoundBufferRecorder *)p; }
|
||||
VALUE SoundBufferRecorder_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
SoundBufferRecorder *ptr = new SoundBufferRecorder();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, SoundBufferRecorder_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE SoundBufferRecorder_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
VALUE SoundBufferRecorder_canCapture(VALUE vClass) {
|
||||
return SoundBufferRecorder::CanCapture() ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
VALUE SoundBufferRecorder_get_buffer(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBufferRecorder *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBufferRecorder, pSelf);
|
||||
DECLARE_PTR_VAR(SoundBuffer, SoundBuffer, (SoundBuffer *)&pSelf->GetBuffer());
|
||||
return vSoundBuffer;
|
||||
}
|
||||
|
||||
VALUE SoundBufferRecorder_start(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBufferRecorder *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBufferRecorder, pSelf);
|
||||
if(argc == 0) {
|
||||
pSelf->Start();
|
||||
} else if(argc == 1) {
|
||||
pSelf->Start(NUM2INT(argv[0]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE SoundBufferRecorder_stop(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBufferRecorder *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBufferRecorder, pSelf);
|
||||
pSelf->Stop();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE SoundBufferRecorder_get_sampleRate(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
SoundBufferRecorder *pSelf;
|
||||
Data_Get_Struct(vSelf, SoundBufferRecorder, pSelf);
|
||||
return INT2FIX(pSelf->GetSampleRate());
|
||||
}
|
||||
|
||||
void Init_SoundBufferRecorder()
|
||||
{
|
||||
g_cSoundBufferRecorder = rb_define_class_under(g_vModule, "SoundBufferRecorder", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(SoundBufferRecorder, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(SoundBufferRecorder, initialize, -1);
|
||||
|
||||
DEFINE_CLASS_METHOD(SoundBufferRecorder, canCapture, 0);
|
||||
|
||||
DEFINE_GETTER(SoundBufferRecorder, sampleRate);
|
||||
DEFINE_GETTER(SoundBufferRecorder, buffer);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(SoundBufferRecorder, start, -1);
|
||||
DEFINE_INSTANCE_METHOD(SoundBufferRecorder, stop, 0);
|
||||
}
|
||||
|
319
ruby/RubySFML/sfSprite.cpp
Normal file
319
ruby/RubySFML/sfSprite.cpp
Normal file
|
@ -0,0 +1,319 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
#include <math.h>
|
||||
using namespace sf;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define SQRTF sqrtf
|
||||
#else
|
||||
#define SQRTF sqrt
|
||||
#endif
|
||||
|
||||
// Wrapper for Sprite that calls the Ruby version of virtual methods in case a Ruby class derives from this one
|
||||
class rSprite : public Sprite {
|
||||
protected:
|
||||
VALUE m_vSelf, m_vClass;
|
||||
ID m_idRender;
|
||||
bool m_bRender, m_bInRender;
|
||||
|
||||
virtual void Render(const RenderWindow &Window) const {
|
||||
// If this method is overridden in Ruby, call it
|
||||
if(m_bRender) {
|
||||
VALUE vWindow = Data_Wrap_Struct(g_cRenderWindow, 0, 0, (void*)&Window);
|
||||
VALUE vRet = rb_funcall(m_vSelf, m_idRender, 1, vWindow);
|
||||
} else // else call parent
|
||||
Sprite::Render(Window);
|
||||
}
|
||||
|
||||
public:
|
||||
// Call as soon as you get a Ruby VALUE pointing to this object
|
||||
void rInit(VALUE vSelf) {
|
||||
// Need these for rb_funcall
|
||||
m_vSelf = vSelf;
|
||||
m_vClass = CLASS_OF(m_vSelf);
|
||||
|
||||
// Initialize members for Render() virtual method
|
||||
m_idRender = rb_intern("render");
|
||||
m_bRender = rb_method_boundp(m_vClass, m_idRender, 0) == Qtrue;
|
||||
}
|
||||
|
||||
// When an overridden method in Ruby calls super(), it is called this way
|
||||
VALUE Render(VALUE vWindow) {
|
||||
Sprite::Render(*(RenderWindow *)DATA_PTR(vWindow));
|
||||
return Qnil;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
VALUE g_cSprite;
|
||||
|
||||
void Sprite_free(void *p) { delete (Sprite *)p; }
|
||||
VALUE Sprite_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
rSprite *ptr = new rSprite();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Sprite_free, ptr);
|
||||
ptr->rInit(tData);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Sprite_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc >= 1 && argc <= 6 && IS(argv[0], g_cImage) && (argc < 6 || IS(argv[5], g_cColor))) {
|
||||
pSelf->SetImage(*(Image *)DATA_PTR(argv[0]));
|
||||
if(argc >= 2)
|
||||
pSelf->SetLeft((float)NUM2DBL(argv[1]));
|
||||
if(argc >= 3)
|
||||
pSelf->SetTop((float)NUM2DBL(argv[2]));
|
||||
if(argc >= 4) {
|
||||
if(ISNUM(argv[3])) {
|
||||
float f = (float)NUM2DBL(argv[3]);
|
||||
pSelf->SetScale(f, f);
|
||||
} else if(IS(argv[3], rb_cArray)) {
|
||||
float x = (float)NUM2DBL(rb_ary_entry(argv[3], 0));
|
||||
float y = (float)NUM2DBL(rb_ary_entry(argv[3], 1));
|
||||
pSelf->SetScale(x, y);
|
||||
}
|
||||
}
|
||||
if(argc >= 5)
|
||||
pSelf->SetRotation((float)NUM2DBL(argv[4]));
|
||||
if(argc >= 6)
|
||||
pSelf->SetColor(*(Color *)DATA_PTR(argv[5]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetRotationCenter(pSelf->GetWidth()/2, pSelf->GetHeight()/2);
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Sprite_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Left=%f, Top=%f, Width=%f, Height=%f, Scale=[%f,%f]", pSelf->GetLeft(), pSelf->GetTop(), pSelf->GetWidth(), pSelf->GetHeight(), pSelf->GetScaleX(), pSelf->GetScaleY());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Sprite_set_image(VALUE vSelf, VALUE vImage) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
if(!IS(vImage, g_cImage))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetImage(*(Image *)DATA_PTR(vImage));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_set_subRect(VALUE vSelf, VALUE vRect) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
if(!IS(vRect, g_cIntRect))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetSubRect(*(IntRect *)DATA_PTR(vRect));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_image(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
DECLARE_PTR_VAR(Image, Image, (Image *)pSelf->GetImage());
|
||||
return vImage;
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_subRect(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
DECLARE_OBJ_VAR(IntRect, Rect, pSelf->GetSubRect());
|
||||
return vRect;
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_width(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
return rb_float_new(pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_height(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
return rb_float_new(pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE Sprite_getPixel(VALUE vSelf, VALUE vX, VALUE vY) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
DECLARE_OBJ_VAR(Color, Color, pSelf->GetPixel(NUM2INT(vX), NUM2INT(vY)));
|
||||
return vColor;
|
||||
}
|
||||
|
||||
static VALUE Sprite_set_right(VALUE vSelf, VALUE v) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
pSelf->SetLeft((float)NUM2DBL(v) - pSelf->GetWidth());
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_set_bottom(VALUE vSelf, VALUE v) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
pSelf->SetTop((float)NUM2DBL(v) - pSelf->GetHeight());
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_set_x(VALUE vSelf, VALUE v) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
pSelf->SetLeft((float)NUM2DBL(v) - pSelf->GetWidth()*0.5f);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_set_y(VALUE vSelf, VALUE v) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
pSelf->SetTop((float)NUM2DBL(v) - pSelf->GetHeight()*0.5f);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_right(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
return rb_float_new(pSelf->GetLeft() + pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_bottom(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
return rb_float_new(pSelf->GetTop() + pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_x(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
return rb_float_new(pSelf->GetLeft() + pSelf->GetWidth()*0.5f);
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_y(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
return rb_float_new(pSelf->GetTop() + pSelf->GetHeight()*0.5f);
|
||||
}
|
||||
|
||||
static VALUE Sprite_get_radius(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
float w = pSelf->GetWidth() * 0.5f;
|
||||
float h = pSelf->GetHeight() * 0.5f;
|
||||
return rb_float_new(SQRTF(w*w + h*h));
|
||||
}
|
||||
|
||||
static VALUE Sprite_flipX(VALUE vSelf, VALUE vBool) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
pSelf->FlipX(RTEST(vBool));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_flipY(VALUE vSelf, VALUE vBool) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
pSelf->FlipY(RTEST(vBool));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Sprite_distance(VALUE vSelf, VALUE vOther) {
|
||||
if(!ISKO(vOther, g_cSprite))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
|
||||
// Get C++ object pointer from vSelf
|
||||
Sprite *pSelf;
|
||||
Data_Get_Struct(vSelf, Sprite, pSelf);
|
||||
Sprite *pOther;
|
||||
Data_Get_Struct(vOther, Sprite, pOther);
|
||||
|
||||
// Get distance from center positions
|
||||
float dx = (pSelf->GetLeft() + pSelf->GetWidth()*0.5f) -
|
||||
(pOther->GetLeft() + pOther->GetWidth()*0.5f);
|
||||
float dy = (pSelf->GetTop() + pSelf->GetHeight()*0.5f) -
|
||||
(pOther->GetTop() + pOther->GetHeight()*0.5f);
|
||||
return rb_float_new(SQRTF(dx*dx + dy*dy));
|
||||
}
|
||||
|
||||
static VALUE Sprite_render(VALUE vSelf, VALUE vWindow) {
|
||||
// Get C++ object pointer from vSelf
|
||||
rSprite *pSelf;
|
||||
Data_Get_Struct(vSelf, rSprite, pSelf);
|
||||
return pSelf->Render(vWindow);
|
||||
}
|
||||
|
||||
void Init_Sprite()
|
||||
{
|
||||
g_cSprite = rb_define_class_under(g_vModule, "Sprite", g_cDrawable);
|
||||
DEFINE_CLASS_METHOD(Sprite, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Sprite, initialize, -1);
|
||||
|
||||
DEFINE_RW(Sprite, right); // Right x position
|
||||
DEFINE_RW(Sprite, bottom); // Bottom x position
|
||||
DEFINE_RW(Sprite, x); // Center x postion
|
||||
DEFINE_RW(Sprite, y); // Center y position
|
||||
DEFINE_RW(Sprite, image);
|
||||
DEFINE_RW(Sprite, subRect);
|
||||
DEFINE_GETTER(Sprite, width);
|
||||
DEFINE_GETTER2(Sprite, width, w);
|
||||
DEFINE_GETTER(Sprite, height);
|
||||
DEFINE_GETTER2(Sprite, height, h);
|
||||
DEFINE_GETTER(Sprite, radius);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Sprite, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Sprite, getPixel, 2);
|
||||
DEFINE_INSTANCE_METHOD2(Sprite, getPixel, [], 2);
|
||||
DEFINE_INSTANCE_METHOD(Sprite, distance, 1);
|
||||
DEFINE_INSTANCE_METHOD2(Sprite, distance, getDistance, 1);
|
||||
DEFINE_INSTANCE_METHOD(Sprite, flipX, 0);
|
||||
DEFINE_INSTANCE_METHOD(Sprite, flipY, 0);
|
||||
|
||||
// Virtual method
|
||||
DEFINE_INSTANCE_METHOD(Sprite, render, 1);
|
||||
}
|
190
ruby/RubySFML/sfString.cpp
Normal file
190
ruby/RubySFML/sfString.cpp
Normal file
|
@ -0,0 +1,190 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
// Wrapper for String that calls the Ruby version of virtual methods in case a Ruby class derives from this one
|
||||
class rString : public String {
|
||||
protected:
|
||||
VALUE m_vSelf, m_vClass;
|
||||
ID m_idRender;
|
||||
bool m_bRender;
|
||||
|
||||
virtual void Render(const RenderWindow &Window) const {
|
||||
// If this method is overridden in Ruby, call it
|
||||
if(m_bRender) {
|
||||
VALUE vWindow = Data_Wrap_Struct(g_cRenderWindow, 0, 0, (void*)&Window);
|
||||
VALUE vRet = rb_funcall(m_vSelf, m_idRender, 1, vWindow);
|
||||
} else // else call parent
|
||||
String::Render(Window);
|
||||
}
|
||||
|
||||
public:
|
||||
// Damn constructors should be inherited from base class
|
||||
rString(const std::string &Text="", const std::string &Font="", float Size=32.f) :String(Text, Font, Size) {}
|
||||
|
||||
// Call as soon as you get a Ruby VALUE pointing to this object
|
||||
void rInit(VALUE vSelf) {
|
||||
// Need these for rb_funcall
|
||||
m_vSelf = vSelf;
|
||||
m_vClass = CLASS_OF(m_vSelf);
|
||||
|
||||
// Initialize members for Render() virtual method
|
||||
m_idRender = rb_intern("render");
|
||||
m_bRender = rb_method_boundp(m_vClass, m_idRender, 0) == Qtrue;
|
||||
}
|
||||
|
||||
// When an overridden method in Ruby calls super(), it is called this way
|
||||
VALUE Render(VALUE vWindow) {
|
||||
String::Render(*(RenderWindow *)DATA_PTR(vWindow));
|
||||
return Qnil;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
VALUE g_cString;
|
||||
|
||||
void String_free(void *p) { delete (String *)p; }
|
||||
VALUE String_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
// For each version of this method, convert Ruby args to C++ types (applying default values)
|
||||
rString *ptr = new rString();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, String_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
ptr->rInit(tData);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE String_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
if(argc >= 0 && argc <= 3 &&
|
||||
(argc < 1 || ISSTR(argv[0])) &&
|
||||
(argc < 2 || ISSTR(argv[1])) &&
|
||||
(argc < 3 || ISNUM(argv[2]))) {
|
||||
if(argc >= 1)
|
||||
pSelf->SetText(STR2CSTR(argv[0]));
|
||||
if(argc >= 2)
|
||||
pSelf->SetFont(STR2CSTR(argv[1]));
|
||||
if(argc >= 3)
|
||||
pSelf->SetSize((float)NUM2DBL(argv[2]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE String_preloadFont(int argc, VALUE *argv, VALUE vClass) {
|
||||
if(argc >= 2 && argc <= 2)
|
||||
String::PreloadFont(STR2CSTR(argv[0]), NUM2INT(argv[1]));
|
||||
else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE String_set_text(VALUE vSelf, VALUE vText) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
if(!ISSTR(vText))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetText(STR2CSTR(vText));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE String_set_font(VALUE vSelf, VALUE vFont) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
if(!ISSTR(vFont))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetText(STR2CSTR(vFont));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE String_set_size(VALUE vSelf, VALUE vSize) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
if(!ISNUM(vSize))
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
pSelf->SetSize((float)NUM2DBL(vSize));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE String_get_text(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
return rb_str_new2(pSelf->GetText().c_str());
|
||||
}
|
||||
|
||||
static VALUE String_get_font(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
return rb_str_new2(pSelf->GetFont().c_str());
|
||||
}
|
||||
|
||||
static VALUE String_get_size(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
return rb_float_new(pSelf->GetSize());
|
||||
}
|
||||
|
||||
static VALUE String_getRect(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
String *pSelf;
|
||||
Data_Get_Struct(vSelf, String, pSelf);
|
||||
DECLARE_OBJ_VAR(FloatRect, Rect, pSelf->GetRect());
|
||||
return vRect;
|
||||
}
|
||||
|
||||
static VALUE String_render(VALUE vSelf, VALUE vWindow) {
|
||||
// Get C++ object pointer from vSelf
|
||||
rString *pSelf;
|
||||
Data_Get_Struct(vSelf, rString, pSelf);
|
||||
return pSelf->Render(vWindow);
|
||||
}
|
||||
|
||||
void Init_String()
|
||||
{
|
||||
g_cString = rb_define_class_under(g_vModule, "Text", g_cDrawable);
|
||||
DEFINE_CLASS_METHOD(String, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(String, initialize, -1);
|
||||
|
||||
DEFINE_CLASS_METHOD(String, preloadFont, -1);
|
||||
|
||||
DEFINE_RW(String, text);
|
||||
DEFINE_RW(String, font);
|
||||
DEFINE_RW(String, size);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(String, getRect, 0);
|
||||
|
||||
// Virtual method
|
||||
DEFINE_INSTANCE_METHOD(String, render, 1);
|
||||
}
|
||||
|
114
ruby/RubySFML/sfVideoMode.cpp
Normal file
114
ruby/RubySFML/sfVideoMode.cpp
Normal file
|
@ -0,0 +1,114 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cVideoMode;
|
||||
|
||||
DECLARE_INT_RW(VideoMode, Width);
|
||||
DECLARE_INT_RW(VideoMode, Height);
|
||||
DECLARE_INT_RW(VideoMode, BitsPerPixel);
|
||||
|
||||
|
||||
void VideoMode_free(void *p) { delete (VideoMode *)p; }
|
||||
VALUE VideoMode_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
VideoMode *ptr = new VideoMode();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, VideoMode_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE VideoMode_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
VideoMode *pSelf;
|
||||
Data_Get_Struct(vSelf, VideoMode, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc >= 2 && argc <= 3 &&
|
||||
ISNUM(argv[0]) &&
|
||||
ISNUM(argv[1]) &&
|
||||
(argc < 3 || ISNUM(argv[2]))) {
|
||||
*pSelf = VideoMode(NUM2INT(argv[0]), NUM2INT(argv[1]), argc < 3 ? 32 : NUM2INT(argv[2]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE VideoMode_desktop(VALUE vClass)
|
||||
{
|
||||
DECLARE_OBJ_VAR(VideoMode, Desktop, VideoMode::GetDesktopMode());
|
||||
return vDesktop;
|
||||
}
|
||||
|
||||
// Ruby each iterator
|
||||
static VALUE VideoMode_each(VALUE vClass)
|
||||
{
|
||||
int nLength = VideoMode::GetModesCount();
|
||||
for(int i=0; i<nLength; i++) {
|
||||
VideoMode mode = VideoMode::GetMode(i);
|
||||
DECLARE_PTR_VAR(VideoMode, Element, &mode);
|
||||
rb_yield(vElement);
|
||||
}
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE VideoMode_isValid(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
VideoMode *pSelf;
|
||||
Data_Get_Struct(vSelf, VideoMode, pSelf);
|
||||
return pSelf->IsValid() ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
static VALUE VideoMode_to_s(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
VideoMode *pSelf;
|
||||
Data_Get_Struct(vSelf, VideoMode, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Width: %d, Height: %d, BPP: %d", pSelf->Width, pSelf->Height, pSelf->BitsPerPixel);
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
void Init_VideoMode()
|
||||
{
|
||||
g_cVideoMode = rb_define_class_under(g_vModule, "VideoMode", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(VideoMode, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(VideoMode, initialize, -1);
|
||||
|
||||
DEFINE_RW2(VideoMode, Width, width);
|
||||
DEFINE_RW2(VideoMode, Width, w);
|
||||
DEFINE_RW2(VideoMode, Height, height);
|
||||
DEFINE_RW2(VideoMode, Height, h);
|
||||
DEFINE_RW2(VideoMode, BitsPerPixel, bitsPerPixel);
|
||||
DEFINE_RW2(VideoMode, BitsPerPixel, bpp);
|
||||
|
||||
DEFINE_CLASS_METHOD(VideoMode, each, 0);
|
||||
DEFINE_CLASS_METHOD(VideoMode, desktop, 0);
|
||||
DEFINE_INSTANCE_METHOD(VideoMode, isValid, 0);
|
||||
DEFINE_INSTANCE_METHOD(VideoMode, to_s, 0);
|
||||
}
|
||||
|
93
ruby/RubySFML/sfView.cpp
Normal file
93
ruby/RubySFML/sfView.cpp
Normal file
|
@ -0,0 +1,93 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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/Graphics.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cView;
|
||||
|
||||
//DECLARE_DOUBLE_RW(View, Left);
|
||||
//DECLARE_DOUBLE_RW(View, Top);
|
||||
//DECLARE_DOUBLE_RW(View, Width);
|
||||
//DECLARE_DOUBLE_RW(View, Height);
|
||||
DECLARE_OBJ_RW(View, Rect, FloatRect);
|
||||
DECLARE_DOUBLE_RW(View, Zoom);
|
||||
|
||||
|
||||
void View_free(void *p) { delete (View *)p; }
|
||||
VALUE View_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
View *ptr = new View();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, View_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE View_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
View *pSelf;
|
||||
Data_Get_Struct(vSelf, View, pSelf);
|
||||
if(argc == 0) {
|
||||
// Nothing to initialize
|
||||
} else if(argc >= 1 && argc <= 2 &&
|
||||
IS(argv[0], g_cFloatRect) &&
|
||||
(argc < 2 || ISNUM(argv[1]))) {
|
||||
pSelf->Rect = *(FloatRect *)DATA_PTR(argv[0]);
|
||||
if(argc >= 2)
|
||||
pSelf->Zoom = (float)NUM2DBL(argv[1]);
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE View_to_s(VALUE vSelf)
|
||||
{
|
||||
// Get C++ object pointer from vSelf
|
||||
View *pSelf;
|
||||
Data_Get_Struct(vSelf, View, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Left=%f, Top=%f, Width=%f, Height=%f, Zoom=%f", pSelf->Rect.Left, pSelf->Rect.Top, pSelf->Rect.GetHeight(), pSelf->Rect.GetWidth(), pSelf->Zoom);
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
void Init_View()
|
||||
{
|
||||
g_cView = rb_define_class_under(g_vModule, "View", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(View, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(View, initialize, -1);
|
||||
|
||||
//DEFINE_RW2(View, Left, l);
|
||||
//DEFINE_RW2(View, Top, top);
|
||||
//DEFINE_RW2(View, Top, t);
|
||||
//DEFINE_RW2(View, Width, width);
|
||||
//DEFINE_RW2(View, Width, w);
|
||||
//DEFINE_RW2(View, Height, height);
|
||||
//DEFINE_RW2(View, Height, h);
|
||||
DEFINE_RW2(View, Rect, rect);
|
||||
DEFINE_RW2(View, Zoom, zoom);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(View, to_s, 0);
|
||||
}
|
||||
|
181
ruby/RubySFML/sfWindow.cpp
Normal file
181
ruby/RubySFML/sfWindow.cpp
Normal file
|
@ -0,0 +1,181 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// RubySFML - Ruby extension for the SFML library
|
||||
// Copyright (C) 2007 Sean O'Neil and Laurent Gomila
|
||||
// (sean.p.oneil@gmail.com and laurent.gom@gmail.com)
|
||||
//
|
||||
// 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.hpp>
|
||||
#include "RubySFML.h"
|
||||
using namespace sf;
|
||||
|
||||
VALUE g_cWindow;
|
||||
|
||||
void Window_free(void *p) { delete (Window *)p; }
|
||||
VALUE Window_new(int argc, VALUE *argv, VALUE vClass) {
|
||||
Window *ptr = new Window();
|
||||
VALUE tData = Data_Wrap_Struct(vClass, 0, Window_free, ptr);
|
||||
rb_obj_call_init(tData, argc, argv);
|
||||
return tData;
|
||||
}
|
||||
|
||||
static VALUE Window_initialize(int argc, VALUE *argv, VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
if(argc >= 2 && argc <= 3 &&
|
||||
IS(argv[0], g_cVideoMode) &&
|
||||
ISSTR(argv[1]) &&
|
||||
(argc < 3 || true) &&
|
||||
(argc < 4 || ISNUM(argv[3]))) {
|
||||
pSelf->Create(*(VideoMode *)DATA_PTR(argv[0]), STR2CSTR(argv[1]),
|
||||
argc < 3 ? (Style::Resize|Style::Close) : NUM2INT(argv[2]),
|
||||
argc < 4 ? 0 : NUM2INT(argv[3]));
|
||||
} else
|
||||
rb_raise(rb_eTypeError, "wrong argument type(s)");
|
||||
return vSelf;
|
||||
}
|
||||
|
||||
static VALUE Window_to_s(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
char szBuffer[256];
|
||||
sprintf(szBuffer, "Height: %d, Width: %d, Depth Bits: %d, Stencil Bits: %d", pSelf->GetWidth(), pSelf->GetHeight(), pSelf->GetDepthBits(), pSelf->GetStencilBits());
|
||||
return rb_str_new2(szBuffer);
|
||||
}
|
||||
|
||||
static VALUE Window_get_width(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
return INT2FIX(pSelf->GetWidth());
|
||||
}
|
||||
|
||||
static VALUE Window_get_height(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
return INT2FIX(pSelf->GetHeight());
|
||||
}
|
||||
|
||||
static VALUE Window_get_depthBits(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
return INT2FIX(pSelf->GetDepthBits());
|
||||
}
|
||||
|
||||
static VALUE Window_get_stencilBits(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
return INT2FIX(pSelf->GetStencilBits());
|
||||
}
|
||||
|
||||
static VALUE Window_get_frameTime(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
return rb_float_new(pSelf->GetFrameTime());
|
||||
}
|
||||
|
||||
static VALUE Window_get_input(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
DECLARE_PTR_VAR(Input, Input, (Input *)&pSelf->GetInput());
|
||||
return vInput;
|
||||
}
|
||||
|
||||
static VALUE Window_getEvent(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
Event ePoll;
|
||||
if(!pSelf->GetEvent(ePoll))
|
||||
return Qnil;
|
||||
DECLARE_OBJ_VAR(Event, Event, ePoll);
|
||||
return vEvent;
|
||||
}
|
||||
|
||||
static VALUE Window_useVerticalSync(VALUE vSelf, VALUE vEnabled) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
pSelf->UseVerticalSync(RTEST(vEnabled) != 0);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Window_showMouseCursor(VALUE vSelf, VALUE vShow) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
pSelf->ShowMouseCursor(RTEST(vShow) != 0);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Window_setFramerateLimit(VALUE vSelf, VALUE vLimit) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
pSelf->SetFramerateLimit((unsigned int)NUM2INT(vLimit));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Window_display(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
pSelf->Display();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static VALUE Window_setCurrent(VALUE vSelf) {
|
||||
// Get C++ object pointer from vSelf
|
||||
Window *pSelf;
|
||||
Data_Get_Struct(vSelf, Window, pSelf);
|
||||
return pSelf->SetCurrent() ? Qtrue : Qfalse;
|
||||
}
|
||||
|
||||
void Init_Window()
|
||||
{
|
||||
g_cWindow = rb_define_class_under(g_vModule, "Window", rb_cObject);
|
||||
DEFINE_CLASS_METHOD(Window, new, -1);
|
||||
DEFINE_INSTANCE_METHOD(Window, initialize, -1);
|
||||
|
||||
DEFINE_GETTER(Window, width);
|
||||
DEFINE_GETTER2(Window, width, w);
|
||||
DEFINE_GETTER(Window, height);
|
||||
DEFINE_GETTER2(Window, height, h);
|
||||
DEFINE_GETTER(Window, input);
|
||||
DEFINE_GETTER(Window, frameTime);
|
||||
DEFINE_GETTER(Window, stencilBits);
|
||||
DEFINE_GETTER(Window, depthBits);
|
||||
|
||||
DEFINE_INSTANCE_METHOD(Window, to_s, 0);
|
||||
DEFINE_INSTANCE_METHOD(Window, getEvent, 0); // Don't use GETTER
|
||||
DEFINE_INSTANCE_METHOD(Window, useVerticalSync, 1);
|
||||
DEFINE_INSTANCE_METHOD(Window, showMouseCursor, 1);
|
||||
DEFINE_INSTANCE_METHOD(Window, setFramerateLimit, 1);
|
||||
DEFINE_INSTANCE_METHOD(Window, display, 0);
|
||||
DEFINE_INSTANCE_METHOD(Window, setCurrent, 0); // Dont use SETTER
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue