Top Level Namespace

Extended by:
Ruby2D::DSL
Includes:
Ruby2D

Defined Under Namespace

Modules: Ruby2D Classes: String

Constant Summary

Constants included from Ruby2D

Ruby2D::Colour, Ruby2D::VERSION

Instance Method Summary collapse

Methods included from Ruby2D::DSL

clear, close, get, off, on, render, set, show, update, window, window=

Methods included from Ruby2D

assets, assets=, #draw

Instance Method Details

#add_flags(type, flags) ⇒ Object

Add compiler and linker flags



36
37
38
39
40
41
42
43
# File 'ext/ruby2d/extconf.rb', line 36

def add_flags(type, flags)
  case type
  when :c
    $CFLAGS << " #{flags} "
  when :ld
    $LDFLAGS << " #{flags} "
  end
end

#build_ios_tvos(rb_file, device) ⇒ Object

Build an iOS or tvOS app



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ruby2d/cli/build.rb', line 164

def build_ios_tvos(rb_file, device)
  check_build_src_file(rb_file)

  # Check if MRuby exists; if not, quit
  if `which mruby`.empty?
    puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
    exit
  end

  # Add debugging information to produce backtrace
  if @debug then debug_flag = '-g' end

  # Assemble the Ruby 2D library in one `.rb` file and compile to bytecode
  make_lib
  `mrbc #{debug_flag} -Bruby2d_lib -obuild/lib.c build/lib.rb`

  # Read the provided Ruby source file, copy to build dir and compile to bytecode
  File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) }
  `mrbc #{debug_flag} -Bruby2d_app -obuild/src.c build/src.rb`

  # Copy over iOS project
  FileUtils.cp_r "#{@gem_dir}/assets/#{device}", "build"

  # Combine contents of C source files and bytecode into one file
  File.open("build/#{device}/main.c", 'w') do |f|
    f << "#define RUBY2D_IOS_TVOS 1" << "\n\n"
    f << "#define MRUBY 1" << "\n\n"
    f << File.read("build/lib.c") << "\n\n"
    f << File.read("build/src.c") << "\n\n"
    f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
  end

  # TODO: Need add this functionality to the gem
  # Build the Xcode project
  `simple2d build --#{device} build/#{device}/MyApp.xcodeproj`

  # Clean up
  clean_up unless @debug

  # Success!
  puts "App created: `build/#{device}`"
end

#build_macos(rb_file) ⇒ Object

Build an app bundle for macOS



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruby2d/cli/build.rb', line 118

def build_macos(rb_file)

  # Build native app for macOS
  build_native(rb_file)

  # Property list source for the bundle
  info_plist = %(
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleExecutable</key>
  <string>app</string>
  <key>CFBundleIconFile</key>
  <string>app.icns</string>
  <key>CFBundleInfoDictionaryVersion</key>
  <string>6.0</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
  <key>CFBundleVersion</key>
  <string>1</string>
  <key>NSHighResolutionCapable</key>
  <string>True</string>
</dict>
</plist>
)

  # Create directories
  FileUtils.mkpath 'build/App.app/Contents/MacOS'
  FileUtils.mkpath 'build/App.app/Contents/Resources'

  # Create Info.plist and copy over assets
  File.open('build/App.app/Contents/Info.plist', 'w') { |f| f.write(info_plist) }
  FileUtils.cp 'build/app', 'build/App.app/Contents/MacOS/'
  # Consider using an icon:
  #   FileUtils.cp "#{@gem_dir}/assets/app.icns", 'build/App.app/Contents/Resources'

  # Clean up
  FileUtils.rm_f 'build/app' unless @debug

  # Success!
  puts 'macOS app bundle created: `build/App.app`'
end

#build_native(rb_file) ⇒ Object

Build a native version of the provided Ruby application



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby2d/cli/build.rb', line 72

def build_native(rb_file)
  check_build_src_file(rb_file)

  # Check if MRuby exists; if not, quit
  if `which mruby`.empty?
    puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
    exit
  end

  # Add debugging information to produce backtrace
  if @debug then debug_flag = '-g' end

  # Assemble the Ruby 2D library in one `.rb` file and compile to bytecode
  make_lib
  `mrbc #{debug_flag} -Bruby2d_lib -obuild/lib.c build/lib.rb`

  # Read the provided Ruby source file, copy to build dir and compile to bytecode
  File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) }
  `mrbc #{debug_flag} -Bruby2d_app -obuild/src.c build/src.rb`

  # Combine contents of C source files and bytecode into one file
  open('build/app.c', 'w') do |f|
    f << "#define MRUBY 1" << "\n\n"
    f << File.read("build/lib.c") << "\n\n"
    f << File.read("build/src.c") << "\n\n"
    f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
  end

  # Compile to a native executable
  `cc build/app.c -lmruby -o build/app`

  # Clean up
  clean_up unless @debug

  # Success!
  puts "Native app created at `build/app`"
end

#build_web(rb_file) ⇒ Object

Build a web-based version of the provided Ruby application



112
113
114
# File 'lib/ruby2d/cli/build.rb', line 112

def build_web(rb_file)
  puts "Warning: ".warn + "This feature is currently disabled while it's being upgraded."
end

#check_build_src_file(rb_file) ⇒ Object

Check if source file provided is good



35
36
37
38
39
40
41
42
43
# File 'lib/ruby2d/cli/build.rb', line 35

def check_build_src_file(rb_file)
  if !rb_file
    puts "Please provide a Ruby file to build"
    exit
  elsif !File.exist? rb_file
    puts "Can't find file: #{rb_file}"
    exit
  end
end

#check_sdlObject

Check for SDL libraries



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/ruby2d/extconf.rb', line 47

def check_sdl
  unless have_library('SDL2') && have_library('SDL2_image') && have_library('SDL2_mixer') && have_library('SDL2_ttf')

    $errors << "Couldn't find packages needed by Ruby 2D."

    case $platform
    when :linux, :linux_rpi
      # Fedora and CentOS
      if system('which yum')
        $errors << "Install the following packages using `yum` (or `dnf`) and try again:\n" <<
        "  SDL2-devel SDL2_image-devel SDL2_mixer-devel SDL2_ttf-devel".bold

      # Arch
      elsif system('which pacman')
        $errors << "Install the following packages using `pacman` and try again:\n" <<
        "  sdl2 sdl2_image sdl2_mixer sdl2_ttf".bold

      # openSUSE
      elsif system('which zypper')
        $errors << "Install the following packages using `zypper` and try again:\n" <<
        "  libSDL2-devel libSDL2_image-devel libSDL2_mixer-devel libSDL2_ttf-devel".bold

      # Ubuntu, Debian, and Mint
      # `apt` must be last because openSUSE has it aliased to `zypper`
      elsif system('which apt')
        $errors << "Install the following packages using `apt` and try again:\n" <<
        "  libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev".bold
      end
    when :bsd
      $errors << "Install the following packages using `pkg` and try again:\n" <<
      "  sdl2 sdl2_image sdl2_mixer sdl2_ttf".bold
    end

    $errors << "" << "See #{"ruby2d.com".bold} for additional help."
    print_errors; exit
  end
end

#clean_up(cmd = nil) ⇒ Object

Clean up unneeded build files



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ruby2d/cli/build.rb', line 209

def clean_up(cmd = nil)
  FileUtils.rm(
    Dir.glob('build/{src,lib}.{rb,c,js}') +
    Dir.glob('build/app.c')
  )
  if cmd == :all
    puts "cleaning up..."
    FileUtils.rm_f 'build/app'
    FileUtils.rm_f 'build/app.js'
    FileUtils.rm_f 'build/app.html'
    FileUtils.rm_rf 'build/App.app'
    FileUtils.rm_rf 'build/ios'
    FileUtils.rm_rf 'build/tvos'
  end
end

#launch_apple(device) ⇒ Object

Launch an iOS or tvOS app in a simulator



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby2d/cli/launch.rb', line 31

def launch_apple(device)
  case device
  when 'ios'
    if !File.exist? 'build/ios/build/Release-iphonesimulator/MyApp.app'
      puts "No iOS app built!"
      exit
    end
    puts `simple2d simulator --open "iPhone X" &&
          simple2d simulator --install "build/ios/build/Release-iphonesimulator/MyApp.app" &&
          simple2d simulator --launch "Ruby2D.MyApp"`
  when 'tvos'
    if !File.exist? 'build/tvos/build/Release-appletvsimulator/MyApp.app'
      puts "No tvOS app built!"
      exit
    end
    puts `simple2d simulator --open "Apple TV 4K" &&
          simple2d simulator --install "build/tvos/build/Release-appletvsimulator/MyApp.app" &&
          simple2d simulator --launch "Ruby2D.MyApp"`
  end
end

#launch_nativeObject

Launch a native app



4
5
6
7
8
9
10
# File 'lib/ruby2d/cli/launch.rb', line 4

def launch_native
  if !File.exist? 'build/app'
    puts "No native app built!"
    exit
  end
  `( cd build && ./app )`
end

#launch_webObject

Launch a web app



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby2d/cli/launch.rb', line 14

def launch_web
  if !File.exist? 'build/app.html'
    puts "No web app built!"
    exit
  end
  open_cmd = 'open'
  case RUBY_PLATFORM
  when /linux/
    open_cmd = "xdg-#{open_cmd}"
  when /mingw/
    open_cmd = "start"
  end
  system "#{open_cmd} build/app.html"
end

#make_libObject

Assemble the Ruby 2D library in one ‘.rb` file



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby2d/cli/build.rb', line 47

def make_lib
  FileUtils.mkdir_p 'build'

  lib_dir = "#{@gem_dir}/lib/ruby2d/"

  lib = ''
  @lib_files.each do |f|
    lib << File.read("#{lib_dir + f}.rb") + "\n\n"
  end

  File.write('build/lib.rb', lib)
end

Print installation errors



27
28
29
30
31
32
# File 'ext/ruby2d/extconf.rb', line 27

def print_errors
  puts "
#{"== #{"Ruby 2D Installation Errors".error} =======================================\n"}
  #{$errors.join("\n  ")}\n
#{"======================================================================"}"
end

#set_rpi_flagsObject

Set Raspberry Pi flags



87
88
89
90
91
92
# File 'ext/ruby2d/extconf.rb', line 87

def set_rpi_flags
  if $platform == :linux_rpi
    add_flags(:c, '-I/opt/vc/include')
    add_flags(:ld, '-L/opt/vc/lib -lbrcmGLESv2')
  end
end

#strip_require(file) ⇒ Object

Remove ‘require ’ruby2d’‘ from source file



62
63
64
65
66
67
68
# File 'lib/ruby2d/cli/build.rb', line 62

def strip_require(file)
  output = ''
  File.foreach(file) do |line|
    output << line unless line =~ /require ('|")ruby2d('|")/
  end
  return output
end

#use_usr_libsObject

Use SDL and other libraries installed by the user (not those bundled with the gem)



96
97
98
99
100
# File 'ext/ruby2d/extconf.rb', line 96

def use_usr_libs
  # Add flags
  set_rpi_flags
  add_flags(:c, '-I/usr/local/include')
end