ruby2d 0.0.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08baf6549c1222c6f6abaaea8b243b6b0730c1c6
4
- data.tar.gz: 67937402604698cc1ccc510816b73aac3f148e81
3
+ metadata.gz: b065d85656f0f67ae270a815252551c10985caf1
4
+ data.tar.gz: 6ed2d44d184eb36daabd4711e4ef7c74729d693c
5
5
  SHA512:
6
- metadata.gz: 70c35ab9dc0771d662eccc54ce2d29298a75e512e610420ba5fe4118a25997894d407bdb29b5848c753982b1a98dd958fb2005d3053d27cf490e0ab83153b790
7
- data.tar.gz: 579701806b2a0e4236d0798613626adec74b202d23cad4e434792e1b79149304731156c138f33c287820b1db40721fb4a883fa45ea9efc14f9aeeee793fbf1ef
6
+ metadata.gz: 29b0dff61f4a04bbb2bed6904a5c2c10aa6e1cf43e8114f0fecae6d77b19700ee430b087dec6e193cc6dd4210d5d11379a72ee9bde8b1b52d2ef4102f1a66153
7
+ data.tar.gz: c08fda4f13944a3a3e8709a405f7c6f884596dc63d1be90d096614391468491b4b9d3a0d00aeb2d5617f9d3026d87f9dba382fc89cc99309ee4df94c6f90016b
data/assets/app.icns ADDED
Binary file
data/bin/ruby2d ADDED
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ruby2d/version'
3
+
4
+ class String
5
+ def colorize(c); "\e[#{c}m#{self}\e[0m" end
6
+ def error; colorize('1;31') end
7
+ def bold; colorize('1') end
8
+ end
9
+
10
+
11
+ def build_native(rb_file)
12
+ if `which mruby`.empty?
13
+ puts "",
14
+ " mruby not found!".error, "",
15
+ " mruby is needed to build a native Ruby 2D applications.".bold, "",
16
+ " Install using Homebrew on OS X:",
17
+ " brew install mruby", ""
18
+ exit
19
+ end
20
+
21
+ `mkdir -p build`
22
+ `mrbc -Bruby2d_app_symbol -obuild/app.c #{rb_file}`
23
+
24
+ open('build/app.c', 'a') do |f|
25
+ f << %(
26
+ #include "mruby.h"
27
+ #include "mruby/irep.h"
28
+
29
+ int main(void) {
30
+ mrb_state *mrb = mrb_open();
31
+ if (!mrb) { /* handle error */ }
32
+ mrb_load_irep(mrb, ruby2d_app_symbol);
33
+ mrb_close(mrb);
34
+ }
35
+ )
36
+ end
37
+
38
+ `cc -std=c99 -lmruby build/app.c -o build/app`
39
+ end
40
+
41
+
42
+ def build_package
43
+ require 'fileutils'
44
+
45
+ icon_path = "#{Gem::Specification.find_by_name('ruby2d').gem_dir}/assets/app.icns"
46
+
47
+ FileUtils.mkpath "build/App.app/Contents/MacOS"
48
+ FileUtils.mkpath "build/App.app/Contents/Resources"
49
+ FileUtils.cp icon_path, "build/App.app/Contents/Resources"
50
+
51
+ info_plist = %(
52
+ <?xml version="1.0" encoding="UTF-8"?>
53
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
54
+ <plist version="1.0">
55
+ <dict>
56
+ <key>CFBundleExecutable</key>
57
+ <string>app</string>
58
+ <key>CFBundleIconFile</key>
59
+ <string>app.icns</string>
60
+ <key>CFBundleInfoDictionaryVersion</key>
61
+ <string>1.0</string>
62
+ <key>CFBundlePackageType</key>
63
+ <string>APPL</string>
64
+ <key>CFBundleSignature</key>
65
+ <string>????</string>
66
+ <key>CFBundleVersion</key>
67
+ <string>1.0</string>
68
+ </dict>
69
+ </plist>
70
+ )
71
+
72
+ File.open("build/App.app/Contents/Info.plist", 'w') { |f| f.write(info_plist) }
73
+
74
+ FileUtils.cp "build/app", "build/App.app/Contents/MacOS/"
75
+
76
+ puts "App written to `build/App.app`."
77
+ end
78
+
79
+
80
+ # Command line usage and tree ##################################################
81
+
82
+ usage = "
83
+ Usage: ruby2d [-v|--version]
84
+ <command> <options>
85
+
86
+ Summary of commands and options:
87
+ build Build the application...
88
+ native ...compile with mruby.
89
+ web ...for web platform.
90
+ package Create application package.
91
+ -v|--version Prints the installed version.
92
+
93
+ "
94
+
95
+ case ARGV[0]
96
+ when 'build'
97
+ puts "Running build..."
98
+ case ARGV[1]
99
+ when 'native'
100
+ puts "Running build native..."
101
+ build_native ARGV[2]
102
+ when 'web'
103
+ puts "Running build web..."
104
+ else
105
+ puts usage
106
+ end
107
+ when 'package'
108
+ puts "Running package..."
109
+ build_package
110
+ when '-v', '--version'
111
+ puts Ruby2D::VERSION
112
+ else
113
+ puts usage
114
+ end
115
+
116
+ exit
@@ -0,0 +1,84 @@
1
+ require 'mkmf'
2
+
3
+ class String
4
+ def colorize(c); "\e[#{c}m#{self}\e[0m" end
5
+ def error; colorize('1;31') end
6
+ def bold; colorize('1') end
7
+ end
8
+
9
+ def print_errors(errors)
10
+ puts "
11
+ #{"== Ruby 2D Installation Errors =============================".bold}
12
+
13
+ #{"Ruby 2D found some problems and was not installed:".error}
14
+
15
+ #{errors.join("\n ")}
16
+
17
+ #{"============================================================".bold}"
18
+ end
19
+
20
+ errors = []
21
+
22
+ # Install Simple 2D on supported platforms
23
+
24
+ # OS X
25
+ if RUBY_PLATFORM =~ /darwin/
26
+ unless ARGV.include? "--no-brew"
27
+ # Simple 2D not installed
28
+ if `which simple2d`.empty?
29
+ # Homebrew not installed, print and quit
30
+ if `which brew`.empty?
31
+ errors << "Ruby 2D uses a library called Simple 2D." <<
32
+ "On OS X, this can be installed using Homebrew." <<
33
+ "Install Homebrew, then try installing this gem again.\n" <<
34
+ "Learn more at http://brew.sh"
35
+ print_errors(errors)
36
+ exit
37
+ # Install Simple 2D using Homebrew
38
+ else
39
+ `brew tap simple2d/tap`
40
+ `brew install simple2d`
41
+ end
42
+
43
+ # Simple 2D installed, update to latest version
44
+ else
45
+ # Homebrew not installed
46
+ if `which brew`.empty?
47
+ # TODO: Check for latest version manually and update
48
+ # Homebrew installed, get latest version of Simple 2D
49
+ else
50
+ # An alternative, but slower and updates all formulas:
51
+ # `brew update`
52
+ # `brew upgrade simple2d`
53
+
54
+ `brew untap simple2d/tap`
55
+ `brew tap simple2d/tap`
56
+ `brew upgrade simple2d`
57
+ end
58
+ end
59
+ end
60
+
61
+ # Linux
62
+ elsif RUBY_PLATFORM =~ /linux/
63
+ # If Simple 2D not installed
64
+ if `which simple2d`.empty?
65
+ errors << "Ruby 2D uses a library called Simple 2D." <<
66
+ "There's a script to make installation easy on Linux.\n" <<
67
+ "Follow the instructions in the README to get started:" <<
68
+ " https://github.com/simple2d/simple2d"
69
+ print_errors(errors)
70
+ exit
71
+ end
72
+
73
+ # Windows
74
+ elsif RUBY_PLATFORM =~ /mingw/
75
+ puts "Ruby 2D doesn't support Windows yet :("
76
+ exit
77
+ end
78
+
79
+ # Configure Simple 2D and create Makefile
80
+
81
+ $LDFLAGS << ' ' << `simple2d --libs`
82
+ $LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they cause problems
83
+
84
+ create_makefile('ruby2d/ruby2d')
@@ -0,0 +1,288 @@
1
+ #include <ruby.h>
2
+ #include <simple2d.h>
3
+
4
+ // @type_id values for rendering
5
+ #define TRIANGLE 1
6
+ #define QUAD 2
7
+ #define IMAGE 3
8
+ #define TEXT 4
9
+
10
+ // Ruby 2D window
11
+ VALUE self;
12
+
13
+ // Simple 2D window
14
+ Window *window;
15
+
16
+ // Ruby data types
17
+ static VALUE ruby2d_module;
18
+ static VALUE ruby2d_window_klass;
19
+ static VALUE c_data_klass;
20
+
21
+ // Structures for Ruby 2D classes
22
+ struct image_data {
23
+ Image img;
24
+ };
25
+ struct text_data {
26
+ Text txt;
27
+ };
28
+ struct sound_data {
29
+ Sound snd;
30
+ };
31
+
32
+
33
+ /*
34
+ * Function pointer to close the Simple 2D window
35
+ */
36
+ static void close_window() {
37
+ S2D_Close(window);
38
+ }
39
+
40
+
41
+ /*
42
+ * Free image structure attached to Ruby 2D `Image` class
43
+ */
44
+ static void free_image(struct image_data *data) {
45
+ S2D_FreeImage(data->img);
46
+ xfree(data);
47
+ }
48
+
49
+
50
+ /*
51
+ * Initialize image structure data
52
+ */
53
+ static VALUE init_image(char *path) {
54
+ struct image_data *data = ALLOC(struct image_data);
55
+ data->img = S2D_CreateImage(path);
56
+ return Data_Wrap_Struct(c_data_klass, NULL, free_image, data);
57
+ }
58
+
59
+
60
+ /*
61
+ * Free text structure attached to Ruby 2D `Text` class
62
+ */
63
+ static void free_text(struct text_data *data) {
64
+ S2D_FreeText(data->txt);
65
+ xfree(data);
66
+ }
67
+
68
+
69
+ /*
70
+ * Initialize text structure data
71
+ */
72
+ static VALUE init_text(char *font, char *msg, int size) {
73
+ struct text_data *data = ALLOC(struct text_data);
74
+ data->txt = S2D_CreateText(font, msg, size);
75
+ return Data_Wrap_Struct(c_data_klass, NULL, free_text, data);
76
+ }
77
+
78
+
79
+ /*
80
+ * Simple 2D `on_key` input callback function
81
+ */
82
+ void on_key(const char *key) {
83
+ rb_funcall(self, rb_intern("key_callback"), 1, rb_str_new2(key));
84
+ }
85
+
86
+
87
+ /*
88
+ * Simple 2D `on_key_down` input callback function
89
+ */
90
+ void on_key_down(const char *key) {
91
+ rb_funcall(self, rb_intern("key_down_callback"), 1, rb_str_new2(key));
92
+ }
93
+
94
+
95
+ /*
96
+ * Simple 2D `on_controller` input callback function
97
+ */
98
+ void on_controller(bool is_axis, int axis, int val, bool is_btn, int btn) {
99
+ rb_funcall(self, rb_intern("controller_callback"), 5,
100
+ is_axis ? Qtrue : Qfalse, INT2NUM(axis), INT2NUM(val),
101
+ is_btn ? Qtrue : Qfalse, INT2NUM(btn)
102
+ );
103
+ }
104
+
105
+
106
+ /*
107
+ * Simple 2D `update` callback function
108
+ */
109
+ void update() {
110
+
111
+ // Set the cursor
112
+ rb_iv_set(self, "@mouse_x", INT2NUM(window->mouse.x));
113
+ rb_iv_set(self, "@mouse_y", INT2NUM(window->mouse.y));
114
+
115
+ // Store frame rate
116
+ rb_iv_set(self, "@fps", INT2NUM(window->fps));
117
+
118
+ // Call update proc, `window.update`
119
+ rb_funcall(self, rb_intern("update_callback"), 0);
120
+ }
121
+
122
+
123
+ /*
124
+ * Simple 2D `render` callback function
125
+ */
126
+ void render() {
127
+
128
+ // Read window objects
129
+ VALUE objects = rb_iv_get(self, "@objects");
130
+ int num_objects = NUM2INT(rb_funcall(objects, rb_intern("count"), 0));
131
+
132
+ // Switch on each object type
133
+ for (int i = 0; i < num_objects; ++i) {
134
+
135
+ VALUE el = rb_ary_entry(objects, i);
136
+ int type_id = NUM2INT(rb_iv_get(el, "@type_id"));
137
+
138
+ // Switch on the object's type_id
139
+ switch(type_id) {
140
+
141
+ case TRIANGLE: {
142
+ VALUE c1 = rb_iv_get(el, "@c1");
143
+ VALUE c2 = rb_iv_get(el, "@c2");
144
+ VALUE c3 = rb_iv_get(el, "@c3");
145
+
146
+ S2D_DrawTriangle(
147
+ NUM2DBL(rb_iv_get(el, "@x1")),
148
+ NUM2DBL(rb_iv_get(el, "@y1")),
149
+ NUM2DBL(rb_iv_get(c1, "@r")),
150
+ NUM2DBL(rb_iv_get(c1, "@g")),
151
+ NUM2DBL(rb_iv_get(c1, "@b")),
152
+ NUM2DBL(rb_iv_get(c1, "@a")),
153
+
154
+ NUM2DBL(rb_iv_get(el, "@x2")),
155
+ NUM2DBL(rb_iv_get(el, "@y2")),
156
+ NUM2DBL(rb_iv_get(c2, "@r")),
157
+ NUM2DBL(rb_iv_get(c2, "@g")),
158
+ NUM2DBL(rb_iv_get(c2, "@b")),
159
+ NUM2DBL(rb_iv_get(c2, "@a")),
160
+
161
+ NUM2DBL(rb_iv_get(el, "@x3")),
162
+ NUM2DBL(rb_iv_get(el, "@y3")),
163
+ NUM2DBL(rb_iv_get(c3, "@r")),
164
+ NUM2DBL(rb_iv_get(c3, "@g")),
165
+ NUM2DBL(rb_iv_get(c3, "@b")),
166
+ NUM2DBL(rb_iv_get(c3, "@a"))
167
+ );
168
+ }
169
+ break;
170
+
171
+ case QUAD: {
172
+ VALUE c1 = rb_iv_get(el, "@c1");
173
+ VALUE c2 = rb_iv_get(el, "@c2");
174
+ VALUE c3 = rb_iv_get(el, "@c3");
175
+ VALUE c4 = rb_iv_get(el, "@c4");
176
+
177
+ S2D_DrawQuad(
178
+ NUM2DBL(rb_iv_get(el, "@x1")),
179
+ NUM2DBL(rb_iv_get(el, "@y1")),
180
+ NUM2DBL(rb_iv_get(c1, "@r")),
181
+ NUM2DBL(rb_iv_get(c1, "@g")),
182
+ NUM2DBL(rb_iv_get(c1, "@b")),
183
+ NUM2DBL(rb_iv_get(c1, "@a")),
184
+
185
+ NUM2DBL(rb_iv_get(el, "@x2")),
186
+ NUM2DBL(rb_iv_get(el, "@y2")),
187
+ NUM2DBL(rb_iv_get(c2, "@r")),
188
+ NUM2DBL(rb_iv_get(c2, "@g")),
189
+ NUM2DBL(rb_iv_get(c2, "@b")),
190
+ NUM2DBL(rb_iv_get(c2, "@a")),
191
+
192
+ NUM2DBL(rb_iv_get(el, "@x3")),
193
+ NUM2DBL(rb_iv_get(el, "@y3")),
194
+ NUM2DBL(rb_iv_get(c3, "@r")),
195
+ NUM2DBL(rb_iv_get(c3, "@g")),
196
+ NUM2DBL(rb_iv_get(c3, "@b")),
197
+ NUM2DBL(rb_iv_get(c3, "@a")),
198
+
199
+ NUM2DBL(rb_iv_get(el, "@x4")),
200
+ NUM2DBL(rb_iv_get(el, "@y4")),
201
+ NUM2DBL(rb_iv_get(c4, "@r")),
202
+ NUM2DBL(rb_iv_get(c4, "@g")),
203
+ NUM2DBL(rb_iv_get(c4, "@b")),
204
+ NUM2DBL(rb_iv_get(c4, "@a"))
205
+ );
206
+ }
207
+ break;
208
+
209
+ case IMAGE: {
210
+ if (rb_iv_get(el, "@data") == Qnil) {
211
+ VALUE data = init_image(RSTRING_PTR(rb_iv_get(el, "@path")));
212
+ rb_iv_set(el, "@data", data);
213
+ }
214
+
215
+ struct image_data *data;
216
+ Data_Get_Struct(rb_iv_get(el, "@data"), struct image_data, data);
217
+
218
+ data->img.x = NUM2DBL(rb_iv_get(el, "@x"));
219
+ data->img.y = NUM2DBL(rb_iv_get(el, "@y"));
220
+ S2D_DrawImage(data->img);
221
+ }
222
+ break;
223
+
224
+ case TEXT: {
225
+ if (rb_iv_get(el, "@data") == Qnil) {
226
+ VALUE data = init_text(
227
+ RSTRING_PTR(rb_iv_get(el, "@font")),
228
+ RSTRING_PTR(rb_iv_get(el, "@text")),
229
+ NUM2DBL(rb_iv_get(el, "@size"))
230
+ );
231
+ rb_iv_set(el, "@data", data);
232
+ }
233
+
234
+ struct text_data *data;
235
+ Data_Get_Struct(rb_iv_get(el, "@data"), struct text_data, data);
236
+
237
+ data->txt.x = NUM2DBL(rb_iv_get(el, "@x"));
238
+ data->txt.y = NUM2DBL(rb_iv_get(el, "@y"));
239
+ S2D_DrawText(data->txt);
240
+ }
241
+ break;
242
+ }
243
+ }
244
+ }
245
+
246
+
247
+ /*
248
+ * Ruby2D::Window#show
249
+ */
250
+ static VALUE ruby2d_show(VALUE s) {
251
+ self = s;
252
+
253
+ char *title = RSTRING_PTR(rb_iv_get(self, "@title"));
254
+ int width = NUM2INT(rb_iv_get(self, "@width"));
255
+ int height = NUM2INT(rb_iv_get(self, "@height"));
256
+
257
+ window = S2D_CreateWindow(
258
+ title, width, height, update, render, 0
259
+ );
260
+
261
+ window->on_key = on_key;
262
+ window->on_key_down = on_key_down;
263
+ window->on_controller = on_controller;
264
+
265
+ S2D_Show(window);
266
+
267
+ atexit(close_window);
268
+ return Qnil;
269
+ }
270
+
271
+
272
+ /*
273
+ * Ruby C extension init
274
+ */
275
+ void Init_ruby2d() {
276
+
277
+ // Ruby2D
278
+ ruby2d_module = rb_define_module("Ruby2D");
279
+
280
+ // Ruby2D::Window
281
+ ruby2d_window_klass = rb_define_class_under(ruby2d_module, "Window", rb_cObject);
282
+
283
+ // Ruby2D::Window#show
284
+ rb_define_method(ruby2d_window_klass, "show", ruby2d_show, 0);
285
+
286
+ // Ruby2D::CData
287
+ c_data_klass = rb_define_class_under(ruby2d_module, "CData", rb_cObject);
288
+ }
data/lib/ruby2d.rb CHANGED
@@ -1,5 +1,17 @@
1
- class R2D
2
- def self.hi
3
- puts "Hello world!"
4
- end
5
- end
1
+ # ruby2d.rb
2
+
3
+ require 'ruby2d/window'
4
+ require 'ruby2d/application'
5
+ require 'ruby2d/color'
6
+ require 'ruby2d/quad'
7
+ require 'ruby2d/rectangle'
8
+ require 'ruby2d/square'
9
+ require 'ruby2d/triangle'
10
+ require 'ruby2d/image'
11
+ require 'ruby2d/text'
12
+ require 'ruby2d/dsl'
13
+ require 'ruby2d/exceptions'
14
+ require 'ruby2d/ruby2d' # load native extension
15
+
16
+ include Ruby2D::DSL
17
+ include Ruby2D
@@ -0,0 +1,39 @@
1
+ # application.rb
2
+
3
+ module Ruby2D::Application
4
+ class << self
5
+ @@window = Ruby2D::Window.new
6
+
7
+ def get(sym)
8
+ @@window.get(sym)
9
+ end
10
+
11
+ def set(opts)
12
+ @@window.set(opts)
13
+ end
14
+
15
+ def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
16
+ @@window.on(mouse: mouse, key: key, key_down: key_down, controller: controller, &proc)
17
+ end
18
+
19
+ def add(o)
20
+ @@window.add(o)
21
+ end
22
+
23
+ def remove(o)
24
+ @@window.remove(o)
25
+ end
26
+
27
+ def clear
28
+ @@window.clear
29
+ end
30
+
31
+ def update(&proc)
32
+ @@window.update(&proc)
33
+ end
34
+
35
+ def show
36
+ @@window.show
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,72 @@
1
+ # color.rb
2
+
3
+ module Ruby2D
4
+ class Color
5
+
6
+ attr_reader :r, :g, :b, :a
7
+
8
+ @@colors = {
9
+ 'black' => [ 0, 0, 0, 255],
10
+ 'gray' => [170, 170, 170, 255],
11
+ 'silver' => [221, 221, 221, 255],
12
+ 'white' => [255, 255, 255, 255],
13
+ 'navy' => [ 0, 31, 63, 255],
14
+ 'blue' => [ 0, 116, 217, 255],
15
+ 'aqua' => [127, 219, 255, 255],
16
+ 'teal' => [ 57, 204, 204, 255],
17
+ 'olive' => [ 61, 153, 112, 255],
18
+ 'green' => [ 46, 204, 64, 255],
19
+ 'lime' => [ 1, 255, 112, 255],
20
+ 'yellow' => [255, 220, 0, 255],
21
+ 'orange' => [255, 133, 27, 255],
22
+ 'red' => [255, 65, 54, 255],
23
+ 'maroon' => [133, 20, 75, 255],
24
+ 'fuchsia' => [240, 18, 190, 255],
25
+ 'purple' => [177, 13, 201, 255],
26
+ 'brown' => [102, 51, 0, 255],
27
+ 'random' => []
28
+ }
29
+
30
+ def initialize(c)
31
+ if !self.class.is_valid? c
32
+ raise Error, "`#{c}` is not a valid color"
33
+ else
34
+ case c
35
+ when String
36
+ if c == 'random'
37
+ @r, @g, @b, @a = rand(0..1.0), rand(0..1.0), rand(0..1.0), 1.0
38
+ else
39
+ @r, @g, @b, @a = to_f(@@colors[c])
40
+ end
41
+ when Array
42
+ @r, @g, @b, @a = to_f([c[0], c[1], c[2], c[3]])
43
+ end
44
+ end
45
+ end
46
+
47
+ # Color must be String, like 'red', or Array, like [1.0, 0, 0, 1.0]
48
+ def self.is_valid?(c)
49
+ (c.class == String && @@colors.key?(c)) ||
50
+ (c.class == Array && c.length == 4 &&
51
+ c.all? { |el| el.is_a? Numeric } &&
52
+ c.all? { |el| el.class == Fixnum && (0..255).include?(el) ||
53
+ el.class == Float && (0.0..1.0).include?(el) })
54
+ end
55
+
56
+ private
57
+
58
+ # Convert from Fixnum (0..255) to Float (0.0..1.0)
59
+ def to_f(a)
60
+ b = []
61
+ a.each do |n|
62
+ if n.class == Fixnum
63
+ b.push(n / 255.0)
64
+ else
65
+ b.push(n)
66
+ end
67
+ end
68
+ return b
69
+ end
70
+
71
+ end
72
+ end
data/lib/ruby2d/dsl.rb ADDED
@@ -0,0 +1,31 @@
1
+ # dsl.rb
2
+
3
+ module Ruby2D::DSL
4
+ def hello
5
+ puts "hi"
6
+ end
7
+
8
+ def get(sym)
9
+ Ruby2D::Application.get(sym)
10
+ end
11
+
12
+ def set(opts)
13
+ Ruby2D::Application.set(opts)
14
+ end
15
+
16
+ def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
17
+ Ruby2D::Application.on(mouse: mouse, key: key, key_down: key_down, controller: controller, &proc)
18
+ end
19
+
20
+ def update(&proc)
21
+ Ruby2D::Application.update(&proc)
22
+ end
23
+
24
+ def show
25
+ Ruby2D::Application.show
26
+ end
27
+
28
+ def clear
29
+ Ruby2D::Application.clear
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ # exceptions.rb
2
+
3
+ module Ruby2D
4
+ class Error < StandardError
5
+ def colorize(msg, c); "\e[#{c}m#{msg}\e[0m" end
6
+ def error(msg); colorize(msg, '4;31') end
7
+ def bold(msg); colorize(msg, '1') end
8
+
9
+ def initialize(msg)
10
+ super(msg)
11
+ puts error("\nRuby 2D Error:") << " " << msg
12
+ puts bold("Occurred in:")
13
+ puts bold(" " + caller.last), "\n"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ # image.rb
2
+
3
+ module Ruby2D
4
+ class Image
5
+
6
+ attr_accessor :x, :y
7
+
8
+ def initialize(x, y, path)
9
+
10
+ unless File.exists? path
11
+ raise Error, "Cannot find image file `#{path}`"
12
+ end
13
+
14
+ @type_id = 3
15
+ @x, @y, @path = x, y, path
16
+
17
+ if defined? Ruby2D::DSL
18
+ Ruby2D::Application.add(self)
19
+ end
20
+ end
21
+
22
+ def remove
23
+ if defined? Ruby2D::DSL
24
+ Ruby2D::Application.remove(self)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,62 @@
1
+ # quad.rb
2
+
3
+ module Ruby2D
4
+ class Quad
5
+ # Coordinates in clockwise order, starting at top left:
6
+ # x1,y1 == top left
7
+ # x2,y2 == top right
8
+ # x3,y3 == bottom right
9
+ # x4,y4 == bottom left
10
+ attr_accessor :x1, :y1, :c1,
11
+ :x2, :y2, :c2,
12
+ :x3, :y3, :c3,
13
+ :x4, :y4, :c4
14
+
15
+ attr_reader :color
16
+
17
+ def initialize(x1=0, y1=0, x2=100, y2=0, x3=100, y3=100, x4=100, y4=100, c='white')
18
+ @type_id = 2
19
+ @x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4
20
+ @color = c
21
+ update_color(c)
22
+
23
+ if defined? Ruby2D::DSL
24
+ Ruby2D::Application.add(self)
25
+ end
26
+ end
27
+
28
+ def color=(c)
29
+ @color = c
30
+ update_color(c)
31
+ end
32
+
33
+ def remove
34
+ if defined? Ruby2D::DSL
35
+ Ruby2D::Application.remove(self)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def update_color(c)
42
+
43
+ # If a valid color, use it for each vertex
44
+ if Color.is_valid? c
45
+ @c1 = Ruby2D::Color.new(c)
46
+ @c2 = Ruby2D::Color.new(c)
47
+ @c3 = Ruby2D::Color.new(c)
48
+ @c4 = Ruby2D::Color.new(c)
49
+
50
+ # If a valid array of colors, assign them to each vertex, respectively
51
+ elsif c.all? { |el| Color.is_valid? el }
52
+ @c1 = Ruby2D::Color.new(c[0])
53
+ @c2 = Ruby2D::Color.new(c[1])
54
+ @c3 = Ruby2D::Color.new(c[2])
55
+ @c4 = Ruby2D::Color.new(c[3])
56
+ else
57
+ raise Error, "Not a valid color for #{self.class}"
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,57 @@
1
+ # rectangle.rb
2
+
3
+ module Ruby2D
4
+ class Rectangle < Ruby2D::Quad
5
+
6
+ attr_reader :x, :y, :width, :height
7
+
8
+ def initialize(x=0, y=0, w=200, h=100, c='white')
9
+ @type_id = 2
10
+ @x, @y, @width, @height, @color = x, y, w, h, c
11
+ update_coords(x, y, w, h)
12
+ update_color(c)
13
+
14
+ if defined? Ruby2D::DSL
15
+ Ruby2D::Application.add(self)
16
+ end
17
+ end
18
+
19
+ def x=(x)
20
+ @x = @x1 = x
21
+ @x2 = x + @width
22
+ @x3 = x + @width
23
+ @x4 = x
24
+ end
25
+
26
+ def y=(y)
27
+ @y = @y1 = y
28
+ @y2 = y
29
+ @y3 = y + @height
30
+ @y4 = y + @height
31
+ end
32
+
33
+ def width=(w)
34
+ @width = w
35
+ update_coords(@x, @y, w, @height)
36
+ end
37
+
38
+ def height=(h)
39
+ @height = h
40
+ update_coords(@x, @y, @width, h)
41
+ end
42
+
43
+ private
44
+
45
+ def update_coords(x, y, w, h)
46
+ @x1 = x
47
+ @y1 = y
48
+ @x2 = x + w
49
+ @y2 = y
50
+ @x4 = x
51
+ @y4 = y + h
52
+ @x3 = x + w
53
+ @y3 = y + h
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,19 @@
1
+ # sound.rb
2
+
3
+ module Ruby2D
4
+ class Sound
5
+
6
+ def initialize(window, path)
7
+ unless File.exists? path
8
+ raise Error, "Cannot find sound file!"
9
+ end
10
+ window.create_audio(self, path)
11
+ @window, @path = window, path
12
+ end
13
+
14
+ def play
15
+ @window.play_audio(self)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # square.rb
2
+
3
+ module Ruby2D
4
+ class Square < Ruby2D::Rectangle
5
+
6
+ attr_reader :size
7
+
8
+ def initialize(x=0, y=0, s=100, c='white')
9
+ @type_id = 2
10
+ @x, @y, @color = x, y, c
11
+ @width = @height = @size = s
12
+ update_coords(x, y, s, s)
13
+ update_color(c)
14
+
15
+ if defined? Ruby2D::DSL
16
+ Ruby2D::Application.add(self)
17
+ end
18
+ end
19
+
20
+ def size=(s)
21
+ self.width = self.height = @size = s
22
+ end
23
+
24
+ private :width=, :height=
25
+ end
26
+ end
@@ -0,0 +1,62 @@
1
+ # text.rb
2
+
3
+ module Ruby2D
4
+ class Text
5
+
6
+ attr_accessor :x, :y, :size, :text
7
+
8
+ def initialize(x=0, y=0, size=20, text="Hello World!", font="Arial", c="white")
9
+ if font.include? '.'
10
+ unless File.exists? font
11
+ raise Error, "Cannot find font file!"
12
+ else
13
+ @font = font
14
+ end
15
+ else
16
+ @font = resolve_path(font)
17
+ end
18
+
19
+ @type_id = 4
20
+ @x, @y, @size = x, y, size
21
+ @text, @color = text, c
22
+ update_color(c)
23
+
24
+ if defined? Ruby2D::DSL
25
+ Ruby2D::Application.add(self)
26
+ end
27
+ end
28
+
29
+ def color=(c)
30
+ @color = c
31
+ update_color(c)
32
+ end
33
+
34
+ def text=(t)
35
+
36
+ end
37
+
38
+ def remove
39
+ if defined? Ruby2D::DSL
40
+ Ruby2D::Application.remove(self)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def resolve_path(font)
47
+ # TODO: Consider CSS names, like 'serif', 'san-serif', 'monospace'
48
+ if RUBY_PLATFORM =~ /darwin/
49
+ font_path = "/Library/Fonts/#{font}.ttf"
50
+ unless File.exists? font_path
51
+ raise Error, "Cannot find system font!"
52
+ end
53
+ end
54
+ font_path
55
+ end
56
+
57
+ def update_color(c)
58
+ @c = Ruby2D::Color.new(c)
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,51 @@
1
+ # triangle.rb
2
+
3
+ module Ruby2D
4
+ class Triangle
5
+
6
+ attr_accessor :x1, :y1, :c1,
7
+ :x2, :y2, :c2,
8
+ :x3, :y3, :c3
9
+ attr_reader :color
10
+
11
+ def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white')
12
+ @type_id = 1
13
+ @x1, @y1 = x1, y1
14
+ @x2, @y2 = x2, y2
15
+ @x3, @y3 = x3, y3
16
+ @color = c
17
+ update_color(c)
18
+
19
+ if defined? Ruby2D::DSL
20
+ Ruby2D::Application.add(self)
21
+ end
22
+ end
23
+
24
+ def color=(c)
25
+ update_color(c)
26
+ @color = c
27
+ end
28
+
29
+ def remove
30
+ if defined? Ruby2D::DSL
31
+ Ruby2D::Application.remove(self)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def update_color(c)
38
+ # If a 2D array
39
+ if c.class == Array && c.all? { |el| el.class == Array }
40
+ @c1 = Ruby2D::Color.new(c[0])
41
+ @c2 = Ruby2D::Color.new(c[1])
42
+ @c3 = Ruby2D::Color.new(c[2])
43
+ else
44
+ @c1 = Ruby2D::Color.new(c)
45
+ @c2 = Ruby2D::Color.new(c)
46
+ @c3 = Ruby2D::Color.new(c)
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # version.rb
2
+
3
+ module Ruby2D
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,176 @@
1
+ # window.rb
2
+
3
+ module Ruby2D
4
+ class Window
5
+ attr_reader :title, :width, :height, :mouse_x, :mouse_y
6
+
7
+ def initialize(width: 640, height: 480, title: "Ruby 2D", fps: 60, vsync: true)
8
+ @width, @height, @title = width, height, title
9
+ @mouse_x = @mouse_y = 0
10
+ @fps_cap = fps
11
+ @fps = 60
12
+ @vsync = vsync
13
+
14
+ @objects = []
15
+ @keys = {}
16
+ @keys_down = {}
17
+ @controller = {}
18
+ @update_proc = Proc.new {}
19
+ end
20
+
21
+ def get(sym)
22
+ case sym
23
+ when :window
24
+ return self
25
+ when :title
26
+ return @title
27
+ when :width
28
+ return @width
29
+ when :height
30
+ return @height
31
+ when :fps
32
+ return @fps
33
+ when :mouse_x
34
+ return @mouse_x
35
+ when :mouse_y
36
+ return @mouse_y
37
+ end
38
+ end
39
+
40
+ def set(opts)
41
+ if opts.include? :title
42
+ @title = opts[:title]
43
+ end
44
+
45
+ if opts.include? :width
46
+ @width = opts[:width]
47
+ end
48
+
49
+ if opts.include? :height
50
+ @height = opts[:height]
51
+ end
52
+ end
53
+
54
+ def add(o)
55
+ case o
56
+ when nil
57
+ raise Error, "Cannot add '#{o.class}' to window!"
58
+ when Array
59
+ o.each { |x| add_object(x) }
60
+ else
61
+ add_object(o)
62
+ end
63
+ end
64
+
65
+ def remove(o)
66
+ if o == nil
67
+ raise Error, "Cannot remove '#{o.class}' from window!"
68
+ end
69
+
70
+ if i = @objects.index(o)
71
+ @objects.slice!(i)
72
+ true
73
+ else
74
+ false
75
+ end
76
+ end
77
+
78
+ def clear
79
+ @objects.clear
80
+ end
81
+
82
+ def update(&proc)
83
+ @update_proc = proc
84
+ true
85
+ end
86
+
87
+ def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
88
+ unless mouse.nil?
89
+ # reg_mouse(btn, &proc)
90
+ end
91
+
92
+ unless key.nil?
93
+ reg_key(key, &proc)
94
+ end
95
+
96
+ unless key_down.nil?
97
+ reg_key_down(key_down, &proc)
98
+ end
99
+
100
+ unless controller.nil?
101
+ reg_controller(controller, &proc)
102
+ end
103
+ end
104
+
105
+ def key_callback(key)
106
+ key.downcase!
107
+ if @keys.has_key? key
108
+ @keys[key].call
109
+ end
110
+ end
111
+
112
+ def key_down_callback(key)
113
+ key.downcase!
114
+ if @keys_down.has_key? key
115
+ @keys_down[key].call
116
+ end
117
+ end
118
+
119
+ def controller_callback(is_axis, axis, val, is_btn, btn)
120
+
121
+ # puts "is_axis: #{is_axis}, axis: #{axis}, val: #{val}, is_btn: #{is_btn}, btn: #{btn}"
122
+
123
+ if is_axis
124
+ if axis == 0 && val == -32768
125
+ event = 'left'
126
+ elsif axis == 0 && val == 32767
127
+ event = 'right'
128
+ elsif axis == 1 && val == -32768
129
+ event = 'up'
130
+ elsif axis == 1 && val == 32767
131
+ event = 'down'
132
+ end
133
+ elsif is_btn
134
+ event = btn
135
+ end
136
+
137
+ if @controller.has_key? event
138
+ @controller[event].call
139
+ end
140
+ end
141
+
142
+ def update_callback
143
+ @update_proc.call
144
+ end
145
+
146
+ private
147
+
148
+ def add_object(o)
149
+ if !@objects.include?(o)
150
+ @objects.push(o)
151
+ true
152
+ else
153
+ false
154
+ end
155
+ end
156
+
157
+ # Register key string with proc
158
+ def reg_key(key, &proc)
159
+ @keys[key] = proc
160
+ true
161
+ end
162
+
163
+ # Register key string with proc
164
+ def reg_key_down(key, &proc)
165
+ @keys_down[key] = proc
166
+ true
167
+ end
168
+
169
+ # Register controller string with proc
170
+ def reg_controller(event, &proc)
171
+ @controller[event] = proc
172
+ true
173
+ end
174
+
175
+ end
176
+ end
metadata CHANGED
@@ -1,23 +1,56 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Black
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-27 00:00:00.000000000 Z
12
- dependencies: []
13
- description: A gem for 2D graphics in Ruby.
11
+ date: 2016-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.4'
27
+ description: Make cross-platform 2D applications in Ruby
14
28
  email: "@blacktm"
15
- executables: []
16
- extensions: []
29
+ executables:
30
+ - ruby2d
31
+ extensions:
32
+ - ext/ruby2d/extconf.rb
17
33
  extra_rdoc_files: []
18
34
  files:
35
+ - assets/app.icns
36
+ - bin/ruby2d
37
+ - ext/ruby2d/extconf.rb
38
+ - ext/ruby2d/ruby2d.c
19
39
  - lib/ruby2d.rb
20
- homepage: https://github.com/blacktm/ruby2d
40
+ - lib/ruby2d/application.rb
41
+ - lib/ruby2d/color.rb
42
+ - lib/ruby2d/dsl.rb
43
+ - lib/ruby2d/exceptions.rb
44
+ - lib/ruby2d/image.rb
45
+ - lib/ruby2d/quad.rb
46
+ - lib/ruby2d/rectangle.rb
47
+ - lib/ruby2d/sound.rb
48
+ - lib/ruby2d/square.rb
49
+ - lib/ruby2d/text.rb
50
+ - lib/ruby2d/triangle.rb
51
+ - lib/ruby2d/version.rb
52
+ - lib/ruby2d/window.rb
53
+ homepage: http://www.ruby2d.com
21
54
  licenses:
22
55
  - MIT
23
56
  metadata: {}
@@ -29,7 +62,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
29
62
  requirements:
30
63
  - - ">="
31
64
  - !ruby/object:Gem::Version
32
- version: '0'
65
+ version: 2.0.0
33
66
  required_rubygems_version: !ruby/object:Gem::Requirement
34
67
  requirements:
35
68
  - - ">="
@@ -37,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
70
  version: '0'
38
71
  requirements: []
39
72
  rubyforge_project:
40
- rubygems_version: 2.4.6
73
+ rubygems_version: 2.5.1
41
74
  signing_key:
42
75
  specification_version: 4
43
76
  summary: Ruby 2D