ruby-sdl2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/mouse.c ADDED
@@ -0,0 +1,286 @@
1
+ #include "rubysdl2_internal.h"
2
+ #include <SDL_mouse.h>
3
+ #include <SDL_events.h>
4
+
5
+ static VALUE mMouse;
6
+ static VALUE cCursor;
7
+ static VALUE cState;
8
+
9
+ /*
10
+ * Document-module: SDL2::Mouse
11
+ *
12
+ * This module have mouse input handling functions.
13
+ *
14
+ */
15
+
16
+ /*
17
+ * Document-class: SDL2::Mouse::Cursor
18
+ *
19
+ * This class represents mouse cursor shape, and
20
+ * have some class-methods for a mouse cursor.
21
+ */
22
+
23
+ /*
24
+ * Document-class: SDL2::Mouse::State
25
+ *
26
+ * This class represents mouse stetes.
27
+ *
28
+ * You can get a mouse state with {SDL2::Mouse.state}.
29
+ *
30
+ * @attribute x
31
+ * the x coordinate of the mouse cursor.
32
+ *
33
+ * For {SDL2::Mouse.state}, this attribute means the x coordinate
34
+ * relative to the window.
35
+ * For {SDL2::Mouse.relative_state}, this attribute means the x coordinate
36
+ * relative to the previous call of {SDL2::Mouse.relative_state}.
37
+ *
38
+ * @return [Integer]
39
+ *
40
+ * @attribute y
41
+ * the y coordinate of the mouse cursor
42
+ *
43
+ * For {SDL2::Mouse.state}, this attribute means the y coordinate
44
+ * relative to the window.
45
+ * For {SDL2::Mouse.relative_state}, this attribute means the y coordinate
46
+ * relative to the previous call of {SDL2::Mouse.relative_state}.
47
+ *
48
+ * @return [Integer]
49
+ *
50
+ * @attribute button_bits
51
+ * button bits
52
+ * @return [Integer]
53
+ * @see #pressed?
54
+ */
55
+
56
+ static VALUE State_new(int x, int y, Uint32 buttons)
57
+ {
58
+ VALUE state = rb_obj_alloc(cState);
59
+ rb_iv_set(state, "@x", INT2FIX(x));
60
+ rb_iv_set(state, "@y", INT2FIX(y));
61
+ rb_iv_set(state, "@button_bits", UINT2NUM(buttons));
62
+ return state;
63
+ }
64
+
65
+ static VALUE mouse_state(Uint32 (*func)(int*,int*))
66
+ {
67
+ int x, y;
68
+ Uint32 state;
69
+ state = func(&x, &y);
70
+ return State_new(x, y, state);
71
+ }
72
+
73
+ #if SDL_VERSION_ATLEAST(2,0,4)
74
+ /*
75
+ * Get the current mouse state in relation to the desktop.
76
+ *
77
+ * The return value contains the x and y coordinates of the cursor
78
+ * relative to the desktop and the state of mouse buttons.
79
+ *
80
+ * @note This module function is available since SDL 2.0.4.
81
+ * @return [SDL2::Mouse::State] current state
82
+ */
83
+ static VALUE Mouse_s_global_state(VALUE self)
84
+ {
85
+ return mouse_state(SDL_GetGlobalMouseState);
86
+ }
87
+ #endif
88
+
89
+ /*
90
+ * Get the current state of the mouse.
91
+ *
92
+ * The return value contains the x and y coordinates of the cursor and
93
+ * the state of mouse buttons.
94
+ *
95
+ * @return [SDL2::Mouse::State] current state
96
+ */
97
+ static VALUE Mouse_s_state(VALUE self)
98
+ {
99
+ return mouse_state(SDL_GetMouseState);
100
+ }
101
+
102
+ /*
103
+ * Return true if relative mouse mode is enabled.
104
+ *
105
+ * @see .relative_mode=
106
+ */
107
+ static VALUE Mouse_s_relative_mode_p(VALUE self)
108
+ {
109
+ return INT2BOOL(SDL_GetRelativeMouseMode());
110
+ }
111
+
112
+ /*
113
+ * @overload relative_mode=(bool)
114
+ * @param [Boolean] bool true if you want to enable relative mouse mode
115
+ *
116
+ * Set relative mouse mode.
117
+ *
118
+ * While the mouse is in relative mode, the cursor is hidden, and the
119
+ * driver will try to report continuous motion in the current window.
120
+ * Only relative motion events will be delivered, the mouse position
121
+ * will not change.
122
+ *
123
+ * @note This function will flush any pending mouse motion.
124
+ *
125
+ * @return [bool]
126
+ * @see .relative_mode?
127
+ */
128
+ static VALUE Mouse_s_set_relative_mode(VALUE self, VALUE enabled)
129
+ {
130
+ HANDLE_ERROR(SDL_SetRelativeMouseMode(RTEST(enabled)));
131
+ return enabled;
132
+ }
133
+
134
+ /*
135
+ * Get the relative state of the mouse.
136
+ *
137
+ * The button state is same as {.state} and
138
+ * x and y of the returned object are set to the
139
+ * mouse deltas since the last call to this method.
140
+ *
141
+ * @return [SDL2::Mouse::State]
142
+ */
143
+ static VALUE Mouse_s_relative_state(VALUE self)
144
+ {
145
+ return mouse_state(SDL_GetRelativeMouseState);
146
+ }
147
+
148
+ /*
149
+ * Get the window which has mouse focus.
150
+ *
151
+ * @return [SDL2::Window] the window which has mouse focus
152
+ * @return [nil] if no window governed by SDL has mouse focus
153
+ */
154
+ static VALUE Mouse_s_focused_window(VALUE self)
155
+ {
156
+ SDL_Window* window = SDL_GetMouseFocus();
157
+ if (!window)
158
+ return Qnil;
159
+ else
160
+ return find_window_by_id(SDL_GetWindowID(window));
161
+ }
162
+
163
+ /*
164
+ * Show the mouse cursor.
165
+ * @return [nil]
166
+ */
167
+ static VALUE Cursor_s_show(VALUE self)
168
+ {
169
+ HANDLE_ERROR(SDL_ShowCursor(SDL_ENABLE));
170
+ return Qnil;
171
+ }
172
+
173
+ /*
174
+ * Hide the mouse cursor.
175
+ * @return [nil]
176
+ */
177
+ static VALUE Cursor_s_hide(VALUE self)
178
+ {
179
+ HANDLE_ERROR(SDL_ShowCursor(SDL_DISABLE));
180
+ return Qnil;
181
+ }
182
+
183
+ /*
184
+ * Return true if the mouse cursor is shown.
185
+ */
186
+ static VALUE Cursor_s_shown_p(VALUE self)
187
+ {
188
+ return INT2BOOL(HANDLE_ERROR(SDL_ShowCursor(SDL_QUERY)));
189
+ }
190
+
191
+ /*
192
+ * @overload warp(window, x, y)
193
+ * Move the mouse cursor to the given position within the window.
194
+ *
195
+ * @param [SDL::Window] window the window to move the mouse cursor into
196
+ * @param [Integer] x the x coordinate within the window
197
+ * @param [Integer] y the y coordinate within the window
198
+ * @return [nil]
199
+ */
200
+ static VALUE Cursor_s_warp(VALUE self, VALUE window, VALUE x, VALUE y)
201
+ {
202
+ SDL_WarpMouseInWindow(Get_SDL_Window(window), NUM2INT(x), NUM2INT(y));
203
+ return Qnil;
204
+ }
205
+
206
+ #if SDL_VERSION_ATLEAST(2,0,4)
207
+ /*
208
+ * @overload warp_globally(x, y)
209
+ * Move the mouse cursor to the given position within the desktop.
210
+ *
211
+ * @note This class module function is available since SDL 2.0.4.
212
+ * @param [Integer] x the x coordinate within the desktop
213
+ * @param [Integer] y the y coordinate within the desktop
214
+ * @return [nil]
215
+ */
216
+ static VALUE Cursor_s_warp_globally(VALUE self, VALUE x, VALUE y)
217
+ {
218
+ SDL_WarpMouseGlobal(NUM2INT(x), NUM2INT(y));
219
+ return Qnil;
220
+ }
221
+ #endif
222
+
223
+ /*
224
+ * @overload pressed?(index)
225
+ * Return true a mouse button is pressed.
226
+ *
227
+ * @param [Integer] index the index of a mouse button, start at index 0
228
+ *
229
+ */
230
+ static VALUE State_pressed_p(VALUE self, VALUE index)
231
+ {
232
+ int idx = NUM2INT(index);
233
+ if (idx < 0 || idx >= 32)
234
+ rb_raise(rb_eArgError, "button index out of range (%d for 1..32)", idx);
235
+ return INT2BOOL(NUM2UINT(rb_iv_get(self, "@button_bits")) & SDL_BUTTON(idx));
236
+ }
237
+
238
+ /* @return [String] inspection string */
239
+ static VALUE State_inspect(VALUE self)
240
+ {
241
+ int i;
242
+ Uint32 buttons = NUM2UINT(rb_iv_get(self, "@button_bits"));
243
+ VALUE pressed = rb_ary_new();
244
+ VALUE string_for_pressed;
245
+ for (i=1; i<=32; ++i)
246
+ if (buttons & SDL_BUTTON(i))
247
+ rb_ary_push(pressed, rb_sprintf("%d", i));
248
+
249
+ string_for_pressed = rb_ary_join(pressed, rb_str_new2(" "));
250
+ return rb_sprintf("<%s:%p x=%d y=%d pressed=[%s]>",
251
+ rb_obj_classname(self), (void*)self,
252
+ NUM2INT(rb_iv_get(self, "@x")), NUM2INT(rb_iv_get(self, "@y")),
253
+ StringValueCStr(string_for_pressed));
254
+ }
255
+
256
+ void rubysdl2_init_mouse(void)
257
+ {
258
+ mMouse = rb_define_module_under(mSDL2, "Mouse");
259
+
260
+ #if SDL_VERSION_ATLEAST(2,0,4)
261
+ rb_define_module_function(mMouse, "global_state", Mouse_s_global_state, 0);
262
+ #endif
263
+ rb_define_module_function(mMouse, "state", Mouse_s_state, 0);
264
+ rb_define_module_function(mMouse, "relative_mode?", Mouse_s_relative_mode_p, 0);
265
+ rb_define_module_function(mMouse, "relative_mode=", Mouse_s_set_relative_mode, 1);
266
+ rb_define_module_function(mMouse, "relative_state", Mouse_s_relative_state, 0);
267
+ rb_define_module_function(mMouse, "focused_window", Mouse_s_focused_window, 0);
268
+
269
+ cCursor = rb_define_class_under(mMouse, "Cursor", rb_cObject);
270
+
271
+ rb_define_singleton_method(cCursor, "show", Cursor_s_show, 0);
272
+ rb_define_singleton_method(cCursor, "hide", Cursor_s_hide, 0);
273
+ rb_define_singleton_method(cCursor, "shown?", Cursor_s_shown_p, 0);
274
+ rb_define_singleton_method(cCursor, "warp", Cursor_s_warp, 3);
275
+ #if SDL_VERSION_ATLEAST(2,0,4)
276
+ rb_define_singleton_method(cCursor, "warp_globally", Cursor_s_warp_globally, 3);
277
+ #endif
278
+
279
+
280
+ cState = rb_define_class_under(mMouse, "State", rb_cObject);
281
+
282
+ rb_undef_method(rb_singleton_class(cState), "new");
283
+ define_attr_readers(cState, "x", "y", "button_bits", NULL);
284
+ rb_define_method(cState, "pressed?", State_pressed_p, 1);
285
+ rb_define_method(cState, "inspect", State_inspect, 0);
286
+ }
@@ -0,0 +1,125 @@
1
+ /*
2
+ * Ruby/SDL2 Ruby extensiion library for SDL 2.x
3
+ *
4
+ * Copyright (C) 2014 Ippei Obayashi
5
+ *
6
+ * This file is part of Ruby/SDL2.
7
+ *
8
+ * Ruby/SDL2 is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU Lesser General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * Ruby/SDL2 is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public License
19
+ * along with Ruby/SDL2. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ #ifndef RUBYSDL2_INTERNAL_H
22
+ #define RUBYSDL2_INTERNAL_H
23
+
24
+ #include <ruby.h>
25
+ #include <ruby/encoding.h>
26
+ #define SDL_MAIN_HANDLED
27
+ #include <SDL_surface.h>
28
+ #include <SDL_version.h>
29
+ #include <SDL_video.h>
30
+
31
+ #ifndef SDL2_EXTERN
32
+ #define SDL2_EXTERN extern
33
+ #endif
34
+
35
+ /** utility functions */
36
+ int rubysdl2_handle_error(int code, const char* cfunc);
37
+ int rubysdl2_is_active(void);
38
+ void rubysdl2_define_attr_readers(VALUE klass, ...);
39
+ VALUE rubysdl2_utf8str_new_cstr(const char* str);
40
+ VALUE rubysdl2_Surface_new(SDL_Surface* surface);
41
+ SDL_Color rubysdl2_Array_to_SDL_Color(VALUE ary);
42
+ VALUE rubysdl2_SDL_version_to_String(const SDL_version* ver);
43
+ VALUE rubysdl2_SDL_version_to_Array(const SDL_version* ver);
44
+ VALUE rubysdl2_find_window_by_id(Uint32 id);
45
+ SDL_Rect* rubysdl2_Get_SDL_Rect(VALUE);
46
+ SDL_Window* rubysdl2_Get_SDL_Window(VALUE);
47
+ const char* rubysdl2_INT2BOOLCSTR(int);
48
+
49
+ /** initialize interfaces */
50
+ void rubysdl2_init_hints(void);
51
+ void rubysdl2_init_video(void);
52
+ void rubysdl2_init_gl(void);
53
+ void rubysdl2_init_messagebox(void);
54
+ void rubysdl2_init_event(void);
55
+ void rubysdl2_init_key(void);
56
+ void rubysdl2_init_mouse(void);
57
+ void rubysdl2_init_joystick(void);
58
+ void rubysdl2_init_timer(void);
59
+ void rubysdl2_init_image(void);
60
+ void rubysdl2_init_mixer(void);
61
+ void rubysdl2_init_ttf(void);
62
+ void rubysdl2_init_filesystem(void);
63
+ void rubysdl2_init_clipboard(void);
64
+ void rubysdl2_init_gamecontorller(void);
65
+
66
+ /** macros */
67
+ #define HANDLE_ERROR(c) (rubysdl2_handle_error((c), __func__))
68
+ #define SDL_ERROR() (HANDLE_ERROR(-1))
69
+ #define INT2BOOL(x) ((x)?Qtrue:Qfalse)
70
+ #define NUM2UCHAR NUM2UINT
71
+ #define UCHAR2NUM UINT2NUM
72
+ #define rb_str_export_to_utf8(str) rb_str_export_to_enc((str), rb_utf8_encoding())
73
+
74
+ #define DEFINE_GETTER(scope, ctype, var_class, classname) \
75
+ scope ctype* Get_##ctype(VALUE obj) \
76
+ { \
77
+ ctype* s; \
78
+ if (!rb_obj_is_kind_of(obj, var_class)) \
79
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", \
80
+ rb_obj_classname(obj), classname); \
81
+ Data_Get_Struct(obj, ctype, s); \
82
+ \
83
+ return s; \
84
+ }
85
+
86
+ #define DEFINE_WRAP_GETTER(scope, SDL_typename, struct_name, field, classname) \
87
+ scope SDL_typename* Get_##SDL_typename(VALUE obj) \
88
+ { \
89
+ struct_name* s = Get_##struct_name(obj); \
90
+ if (s->field == NULL) \
91
+ HANDLE_ERROR(SDL_SetError(classname " is already destroyed")); \
92
+ \
93
+ return s->field; \
94
+ }
95
+
96
+ #define DEFINE_DESTROY_P(scope, struct_name, field) \
97
+ scope VALUE struct_name##_destroy_p(VALUE self) \
98
+ { \
99
+ return INT2BOOL(Get_##struct_name(self)->field == NULL); \
100
+ }
101
+
102
+ #define DEFINE_WRAPPER(SDL_typename, struct_name, field, var_class, classname) \
103
+ DEFINE_GETTER(static, struct_name, var_class, classname); \
104
+ DEFINE_WRAP_GETTER(static ,SDL_typename, struct_name, field, classname); \
105
+ DEFINE_DESTROY_P(static, struct_name, field);
106
+
107
+ /** classes and modules */
108
+ SDL2_EXTERN VALUE rubysdl2_mSDL2;
109
+ SDL2_EXTERN VALUE rubysdl2_eSDL2Error;
110
+
111
+ /** prefix macros */
112
+ #define define_attr_readers rubysdl2_define_attr_readers
113
+ #define utf8str_new_cstr rubysdl2_utf8str_new_cstr
114
+ #define Surface_new rubysdl2_Surface_new
115
+ #define Get_SDL_Rect rubysdl2_Get_SDL_Rect
116
+ #define Get_SDL_Window rubysdl2_Get_SDL_Window
117
+ #define Array_to_SDL_Color rubysdl2_Array_to_SDL_Color
118
+ #define mSDL2 rubysdl2_mSDL2
119
+ #define eSDL2Error rubysdl2_eSDL2Error
120
+ #define SDL_version_to_String rubysdl2_SDL_version_to_String
121
+ #define SDL_version_to_Array rubysdl2_SDL_version_to_Array
122
+ #define INT2BOOLCSTR rubysdl2_INT2BOOLCSTR
123
+ #define find_window_by_id rubysdl2_find_window_by_id
124
+
125
+ #endif
@@ -0,0 +1,16 @@
1
+ require 'sdl2'
2
+
3
+ SDL2::init(SDL2::INIT_AUDIO)
4
+
5
+ SDL2::Mixer.init(SDL2::Mixer::INIT_FLAC|SDL2::Mixer::INIT_MOD|
6
+ SDL2::Mixer::INIT_MP3|SDL2::Mixer::INIT_OGG)
7
+
8
+ SDL2::Mixer.open(22050, SDL2::Mixer::DEFAULT_FORMAT, 2, 512)
9
+
10
+ chunk = SDL2::Mixer::Chunk.load(ARGV[0])
11
+ p chunk; chunk.destroy; p chunk; p chunk.destroy?
12
+
13
+ mus = SDL2::Mixer::Music.load(ARGV[0])
14
+ p mus; mus.destroy; p mus; p mus.destroy?
15
+
16
+ GC.start
Binary file
@@ -0,0 +1,28 @@
1
+ require "sdl2"
2
+
3
+ SDL2.init(SDL2::INIT_VIDEO|SDL2::INIT_EVENTS)
4
+
5
+ window = SDL2::Window.create("testsprite",
6
+ SDL2::Window::POS_CENTERED, SDL2::Window::POS_CENTERED,
7
+ 640, 480, 0)
8
+
9
+ def f2(renderer)
10
+ surface = SDL2::Surface.load_bmp("icon.bmp")
11
+ 10.times { renderer.create_texture_from(surface) }
12
+ p renderer.debug_info
13
+ end
14
+
15
+ def f1(window)
16
+ renderer = window.create_renderer(-1, 0)
17
+ p window.debug_info
18
+ p renderer.debug_info
19
+ f2(renderer)
20
+ GC.start
21
+ p renderer.debug_info
22
+ end
23
+
24
+ f1(window)
25
+ GC.start
26
+ p window.debug_info
27
+
28
+