diff options
Diffstat (limited to 'dragon')
| -rw-r--r-- | dragon/console.rb | 84 | ||||
| -rw-r--r-- | dragon/console_menu.rb | 79 | ||||
| -rw-r--r-- | dragon/docs.rb | 84 | ||||
| -rw-r--r-- | dragon/easing.rb | 82 | ||||
| -rw-r--r-- | dragon/framerate_diagnostics.rb | 165 | ||||
| -rw-r--r-- | dragon/geometry.rb | 4 | ||||
| -rw-r--r-- | dragon/kernel_docs.rb | 8 | ||||
| -rw-r--r-- | dragon/log.rb | 6 | ||||
| -rw-r--r-- | dragon/numeric.rb | 5 | ||||
| -rw-r--r-- | dragon/readme_docs.rb | 259 | ||||
| -rw-r--r-- | dragon/runtime_docs.rb | 17 | ||||
| -rw-r--r-- | dragon/trace.rb | 6 |
12 files changed, 566 insertions, 233 deletions
diff --git a/dragon/console.rb b/dragon/console.rb index 4f47ba0..32deb15 100644 --- a/dragon/console.rb +++ b/dragon/console.rb @@ -292,18 +292,17 @@ S @last_command_errored = false rescue Exception => e string_e = "#{e}" + puts "* EXCEPTION: #{e}" + log "* EXCEPTION: #{e}" @last_command_errored = true if (string_e.include? "wrong number of arguments") method_name = (string_e.split ":")[0].gsub "'", "" - results = Kernel.docs_search method_name - if !results.include "* DOCS: No results found." + results = (Kernel.docs_search method_name).strip + if !results.include? "* DOCS: No results found." puts results log results end end - - puts "#{e}" - log "#{e}" end end end @@ -315,6 +314,10 @@ S (args.inputs.keyboard.key_up.b && args.inputs.keyboard.key_up.control) end + def scroll_to_bottom + @log_offset = 0 + end + def scroll_up_full @log_offset += lines_on_one_page @log_offset = @log.size if @log_offset > @log.size @@ -514,63 +517,6 @@ S render_log_offset args end - def tick_help args - tick_help_debounce args - alpha_rate = 20 - @render_help_target_alpha ||= 255 - @render_help_current_alpha ||= 255 - @render_help_target_alpha += 4 if @render_help_current_alpha == @render_help_target_alpha - @render_help_current_alpha = (@render_help_current_alpha.towards @render_help_target_alpha, 20) - - @render_help_target_alpha = @render_help_target_alpha.clamp(-255, 255) - @render_help_current_alpha = @render_help_current_alpha.clamp(-255, 255) - - [ - "* Prompt Commands: ", - "You can type any of the following ", - "commands in the command prompt. ", - "** docs: Provides API docs. ", - "** $gtk: Accesses the global runtime.", - "* Shortcut Keys: ", - "** full page up: ctrl + b ", - "** full page down: ctrl + f ", - "** half page up: ctrl + u ", - "** half page down: ctrl + d ", - "** clear prompt: ctrl + g ", - "** up arrow: next command ", - "** down arrow: prev command ", - ].each_with_index do |s, i| - args.outputs.reserved << [args.grid.right - 10, - top - 100 - line_height_px * i * 0.8, - s, -3, 2, 180, 180, 180, (@render_help_current_alpha.clamp 0, 255)].label - end - end - - def tick_help_debounce args - hide_log_alpha = -255 - if hidden? - @render_help_current_alpha = -255 - end - - if prompt.last_input_str_changed - @render_help_target_alpha = hide_log_alpha - end - - if args.inputs.mouse.moved - @render_help_target_alpha = hide_log_alpha - end - - if args.inputs.mouse.wheel - @render_help_target_alpha = hide_log_alpha - end - - if @render_help_last_log_invocation_count != @log_invocation_count - @render_help_target_alpha = hide_log_alpha - end - - @render_help_last_log_invocation_count = @log_invocation_count - end - def render_log_offset args return if @log_offset <= 0 args.outputs.reserved << font_style.label( @@ -627,7 +573,6 @@ S process_inputs args return unless should_tick? calc args - tick_help args prompt.tick menu.tick args rescue Exception => e @@ -731,12 +676,11 @@ S end def include_header_marker? log_entry - return false if log_entry.include? "NOTIFY:" - return false if log_entry.include? "INFO:" - return true if log_entry.include? "DOCS:" - (log_entry.start_with? "* ") || - (log_entry.start_with? "** ") || - (log_entry.start_with? "*** ") + return false if (log_entry.strip.include? ".rb") + (log_entry.start_with? "* ") || + (log_entry.start_with? "** ") || + (log_entry.start_with? "*** ") || + (log_entry.start_with? "**** ") end def color_for_log_entry(log_entry) @@ -748,7 +692,7 @@ S @text_color.mult_alpha(0.5) elsif include_header_marker? log_entry @header_color - elsif log_entry.start_with?("====") || log_entry.include?("app") && !log_entry.include?("apple") + elsif log_entry.start_with?("====") @header_color else @text_color diff --git a/dragon/console_menu.rb b/dragon/console_menu.rb index e8ce973..7449f4a 100644 --- a/dragon/console_menu.rb +++ b/dragon/console_menu.rb @@ -5,6 +5,8 @@ module GTK class Console class Menu + attr_accessor :buttons + def initialize console @console = console end @@ -29,22 +31,74 @@ module GTK @console.scroll_down_half end + def show_menu_clicked + @menu_shown = :visible + end + def close_clicked + @menu_shown = :hidden @console.hide end + def hide_menu_clicked + @menu_shown = :hidden + end + + def framerate_diagnostics_clicked + @console.scroll_to_bottom + $gtk.framerate_diagnostics + end + + def itch_wizard_clicked + @console.scroll_to_bottom + $wizards.itch.start + end + + def docs_clicked + @console.scroll_to_bottom + log Kernel.docs_classes + end + + def scroll_end_clicked + @console.scroll_to_bottom + end + + def custom_buttons + [] + end + def tick args return unless @console.visible? - # defaults - @buttons = [ - (button id: :record, row: 0, col: 15, text: "record", method: :record_clicked), - (button id: :replay, row: 0, col: 16, text: "replay", method: :replay_clicked), - (button id: :reset, row: 0, col: 17, text: "reset", method: :reset_clicked), - (button id: :scroll_up, row: 0, col: 18, text: "scroll up", method: :scroll_up_clicked), - (button id: :scroll_down, row: 0, col: 19, text: "scroll down", method: :scroll_down_clicked), - (button id: :close, row: 0, col: 20, text: "close", method: :close_clicked), - ] + @menu_shown ||= :hidden + + if $gtk.production + @buttons = [ + (button id: :record, row: 0, col: 9, text: "record gameplay", method: :record_clicked), + (button id: :replay, row: 0, col: 10, text: "start replay", method: :replay_clicked), + ] + elsif @menu_shown == :hidden + @buttons = [ + (button id: :show_menu, row: 0, col: 10, text: "show menu", method: :show_menu_clicked), + ] + else + @buttons = [ + (button id: :scroll_up, row: 0, col: 6, text: "scroll up", method: :scroll_up_clicked), + (button id: :scroll_down, row: 0, col: 7, text: "scroll down", method: :scroll_down_clicked), + (button id: :scroll_down, row: 0, col: 8, text: "scroll end", method: :scroll_end_clicked), + (button id: :close, row: 0, col: 9, text: "close console", method: :close_clicked), + (button id: :hide, row: 0, col: 10, text: "hide menu", method: :hide_menu_clicked), + + (button id: :record, row: 1, col: 7, text: "record gameplay", method: :record_clicked), + (button id: :replay, row: 1, col: 8, text: "start replay", method: :replay_clicked), + (button id: :record, row: 1, col: 9, text: "framerate diagnostics", method: :framerate_diagnostics_clicked), + (button id: :reset, row: 1, col: 10, text: "reset game", method: :reset_clicked), + + (button id: :reset, row: 2, col: 10, text: "docs", method: :docs_clicked), + (button id: :reset, row: 2, col: 9, text: "itch wizard", method: :itch_wizard_clicked), + *custom_buttons + ] + end # render args.outputs.reserved << @buttons.map { |b| b[:primitives] } @@ -59,13 +113,13 @@ module GTK end def rect_for_layout row, col - col_width = 50 + col_width = 100 row_height = 50 col_margin = 5 row_margin = 5 x = (col_margin + (col * col_width) + (col * col_margin)) y = (row_margin + (row * row_height) + (row * row_margin) + row_height).from_top - { x: x, y: y, w: row_height, h: col_width } + { x: x, y: y, w: col_width, h: row_height } end def button args @@ -75,10 +129,11 @@ module GTK { id: id, rect: (rect_for_layout row, col), + text: text, method: method }.let do |entity| primitives = [] - primitives << entity[:rect].merge(a: 80).solid + primitives << entity[:rect].merge(a: 164).solid primitives << entity[:rect].merge(r: 255, g: 255, b: 255).border primitives << text.wrapped_lines(5) .map_with_index do |l, i| diff --git a/dragon/docs.rb b/dragon/docs.rb index 8f5e8bf..67a50e0 100644 --- a/dragon/docs.rb +++ b/dragon/docs.rb @@ -89,7 +89,7 @@ module Docs def docs_classes DocsOrganizer.sort_docs_classes! - list = $docs_classes.map { |mod| "** #{mod.name}" }.join "\n" + list = $docs_classes.map { |mod| "** #{mod.name}.docs" }.join "\n" <<-S * DOCS: @@ -107,7 +107,7 @@ S end def docs - docs_methods = [DocsOrganizer.find_methods_with_docs(self), :docs_classes].flatten.map { |d| "** #{d}" }.join "\n" + docs_methods = [DocsOrganizer.find_methods_with_docs(self), :docs_classes].flatten.map { |d| "** #{self.name}.#{d}" }.join "\n" if self == Kernel <<-S @@ -201,8 +201,10 @@ S final_string = "* DOCS: No results found." end - $gtk.write_file "docs/search_results.txt", final_string - log "* INFO: Search results have been written to docs/search_results.txt." + $gtk.write_file_root "docs/search_results.txt", final_string + if !final_string.include? "* DOCS: No results found." + log "* INFO: Search results have been written to docs/search_results.txt." + end "\n" + final_string end @@ -216,7 +218,7 @@ S (send m).ltrim + "\n" end.join "\n" file_path = "docs/#{self.name}.txt" - $gtk.write_file "#{file_path}", content + $gtk.write_file_root "#{file_path}", content puts "* INFO: Documentation for #{self.name} has been exported to #{file_path}." $gtk.console.set_system_command file_path nil @@ -235,19 +237,21 @@ S # may god have mercy on your soul if you try to expand this def __docs_to_html__ string parse_log = [] - html_string = <<-S + + html_start_to_toc_start = <<-S <html> <head> <title>DragonRuby Game Toolkit Documentation</title> - <link href="docs.css" rel="stylesheet" type="text/css" media="all"> - <script src="docs.js"></script> + <link href="docs.css?ver=#{Time.now.to_i}" rel="stylesheet" type="text/css" media="all"> </head> <body> <div id='toc'> - {{toc}} +S + html_toc_end_to_content_start = <<-S </div> <div id='content'> - {{content}} +S + html_content_end_to_html_end = <<-S </div> </body> </html> @@ -319,6 +323,11 @@ S __docs_append_true_line__ true_lines, current_true_line, parse_log __docs_append_true_line__ true_lines, l, parse_log current_true_line = "" + elsif l.start_with? "**** " + parse_log << "- Header detected." + __docs_append_true_line__ true_lines, current_true_line, parse_log + __docs_append_true_line__ true_lines, l, parse_log + current_true_line = "" else current_true_line += l.rstrip + " " end @@ -330,7 +339,7 @@ S true_lines = true_lines[1..-1] end - toc = "" + toc_html = "" content_html = "" inside_pre = false @@ -371,7 +380,7 @@ S inside_ol = false inside_ul = false - toc = "<h1>Table Of Contents</h1>\n<ul>\n" + toc_html = "<h1>Table Of Contents</h1>\n<ul>\n" parse_log << "* Processing Html Given True Lines" true_lines.each do |l| parse_log << "** Processing line: ~#{l.rstrip}~" @@ -382,7 +391,7 @@ S inside_ul = false formatted_html = __docs_line_to_html__ l, parse_log link_id = text_to_id.call l - toc += "<li><a href='##{link_id}'>#{formatted_html}</a></li>\n" + toc_html += "<li><a href='##{link_id}'>#{formatted_html}</a></li>\n" content_html += "<h1 id='#{link_id}'>#{formatted_html}</h1>\n" elsif l.start_with? "** " parse_log << "- H2 detected." @@ -391,7 +400,7 @@ S inside_ul = false formatted_html = __docs_line_to_html__ l, parse_log link_id = text_to_id.call l - # toc += "<a href='##{link_id}'>#{formatted_html}</a></br>\n" + # toc_html += "<a href='##{link_id}'>#{formatted_html}</a></br>\n" content_html += "<h2>#{__docs_line_to_html__ l, parse_log}</h2>\n" elsif l.start_with? "*** " parse_log << "- H3 detected." @@ -400,8 +409,17 @@ S inside_ul = false formatted_html = __docs_line_to_html__ l, parse_log link_id = text_to_id.call l - # toc += "<a href='##{link_id}'>#{formatted_html}</a></br>\n" + # toc_html += "<a href='##{link_id}'>#{formatted_html}</a></br>\n" content_html += "<h3>#{__docs_line_to_html__ l, parse_log}</h3>\n" + elsif l.start_with? "**** " + parse_log << "- H4 detected." + content_html += close_list_if_needed.call inside_ul, inside_ol + inside_ol = false + inside_ul = false + formatted_html = __docs_line_to_html__ l, parse_log + link_id = text_to_id.call l + # toc_html += "<a href='##{link_id}'>#{formatted_html}</a></br>\n" + content_html += "<h4>#{__docs_line_to_html__ l, parse_log}</h4>\n" elsif l.strip.length == 0 && !inside_pre # do nothing elsif l.start_with? "#+begin_src" @@ -451,6 +469,8 @@ S l = l[3..-1] elsif l.split(".")[0].length == 3 l = l[4..-1] + elsif l.split(".")[0].length == 4 + l = l[5..-1] end content_html << "<li>#{__docs_line_to_html__ l, parse_log}</li>\n" @@ -493,10 +513,13 @@ S end end end - toc += "</ul>" + toc_html += "</ul>" - final_html = (html_string.gsub "{{toc}}", toc) - final_html = (final_html.gsub "{{content}}", content_html) + final_html = html_start_to_toc_start + + toc_html + + html_toc_end_to_content_start + + content_html + + html_content_end_to_html_end { original: string, @@ -504,15 +527,30 @@ S parse_log: parse_log } rescue Exception => e - $gtk.write_file 'docs/parse_log.txt', (parse_log.join "\n") + $gtk.write_file_root 'docs/parse_log.txt', (parse_log.join "\n") raise "* ERROR in Docs::__docs_to_html__. #{e}" end def __docs_line_to_html__ line, parse_log - line = line.gsub "* DOCS: ", "" if line.start_with? "* DOCS: " - line = line.gsub "* ", "" if line.start_with? "* " - line = line.gsub "** ", "" if line.start_with? "** " - line = line.gsub "*** ", "" if line.start_with? "*** " + parse_log << "- Determining if line is a header." + if line.start_with? "**** " + line = line.gsub "**** ", "" + parse_log << "- Line contains ~**** ~... gsub-ing empty string" + elsif line.start_with? "*** " + line = line.gsub "*** ", "" + parse_log << "- Line contains ~*** ~... gsub-ing empty string" + elsif line.start_with? "** " + line = line.gsub "** ", "" + parse_log << "- Line contains ~** ~... gsub-ing empty string" + elsif line.start_with? "* " + line = line.gsub "* ", "" + parse_log << "- Line contains ~* ~... gsub-ing empty string" + elsif line.start_with? "* DOCS: " + line = line.gsub "* DOCS: ", "" + parse_log << "- Line contains ~* DOCS:~... gsub-ing empty string" + else + parse_log << "- Line does not appear to be a header." + end tilde_count = line.count "~" line_has_link_marker = (line.include? "[[") && (line.include? "]]") diff --git a/dragon/easing.rb b/dragon/easing.rb new file mode 100644 index 0000000..310e7ed --- /dev/null +++ b/dragon/easing.rb @@ -0,0 +1,82 @@ +# coding: utf-8 +# Copyright 2019 DragonRuby LLC +# MIT License +# easing.rb has been released under MIT (*only this file*). + +module GTK + module Easing + def self.ease_extended start_tick, current_tick, end_tick, default_before, default_after, *definitions + definitions.flatten! + definitions = [:identity] if definitions.length == 0 + duration = end_tick - start_tick + elapsed = current_tick - start_tick + y = elapsed.percentage_of(duration).cap_min_max(0, 1) + + definitions.map do |definition| + y = Easing.exec_definition(definition, start_tick, duration, y) + end + + y + end + + def self.ease_spline_extended start_tick, current_tick, end_tick, spline + duration = end_tick - start_tick + t = (current_tick - start_tick).fdiv duration + time_allocation_per_curve = 1.fdiv(spline.length) + curve_index, curve_t = t.fdiv(time_allocation_per_curve).let do |spline_t| + [spline_t.to_i, spline_t - spline_t.to_i] + end + Geometry.cubic_bezier curve_t, *spline[curve_index] + end + + def self.initial_value *definitions + definitions.flatten! + return Easing.exec_definition (definitions.value(-1) || :identity), 0, 10, 0 + end + + def self.final_value *definitions + definitions.flatten! + return Easing.exec_definition (definitions.value(-1) || :identity), 0, 10, 1.0 + end + + def self.exec_definition definition, start_tick, duration, x + if definition.is_a? Symbol + return Easing.send(definition, x).cap_min_max(0, 1) + elsif definition.is_a? Proc + return definition.call(x, start_tick, duration).cap_min_max(0, 1) + end + + raise <<-S +* ERROR: +I don't know how to execute easing function with definition #{definition}. + +S + end + + def self.identity x + x + end + + def self.flip x + 1 - x + end + + def self.quad x + x * x + end + + def self.cube x + x * x * x + end + + def self.quart x + x * x * x * x * x + end + + def self.quint x + x * x * x * x * x * x + end + end +end + +Easing = GTK::Easing diff --git a/dragon/framerate_diagnostics.rb b/dragon/framerate_diagnostics.rb new file mode 100644 index 0000000..6b952bd --- /dev/null +++ b/dragon/framerate_diagnostics.rb @@ -0,0 +1,165 @@ +# Copyright 2019 DragonRuby LLC +# MIT License +# framerate_diagnostics.rb has been released under MIT (*only this file*). + +module GTK + class Runtime + # @visibility private + module FramerateDiagnostics + def framerate_get_diagnostics + <<-S +* INFO: Framerate Diagnostics +You can display these diagnostics using: + +#+begin_src + args.outputs.debug << args.gtk.framerate_diagnostics_primitives +#+end_src + +** Draw Calls: ~<<~ Invocation Perf Counter +Here is how many times ~args.outputs.PRIMITIVE_ARRAY <<~ was called: + + #{$perf_counter_outputs_push_count} times invoked. + +If the number above is high, consider batching primitives so you can lower the invocation of ~<<~. For example. + +Instead of: + +#+begin_src + args.state.enemies.map do |e| + e.alpha = 128 + args.outputs.sprites << e # <-- ~args.outputs.sprites <<~ is invoked a lot + end +#+end_src + +Do this: + +#+begin_src + args.outputs.sprites << args.state + .enemies + .map do |e| # <-- ~args.outputs.sprites <<~ is only invoked once. + e.alpha = 128 + e + end +#+end_src + +** Array Primitives +~Primitives~ represented as an ~Array~ (~Tuple~) are great for prototyping, but are not as performant as using a ~Hash~. + +Here is the number of ~Array~ primitives that were encountered: + + #{$perf_counter_primitive_is_array} Array Primitives. + +If the number above is high, consider converting them to hashes. For example. + +Instead of: + +#+begin_src + args.outputs.sprites << [0, 0, 100, 100, 'sprites/enemy.png'] +#+begin_end + +Do this: + +#+begin_src + args.outputs.sprites << { x: 0, + y: 0, + w: 100, + h: 100, + path: 'sprites/enemy.png' } +#+begin_end + +** Primitive Counts +Here are the draw counts ordered by lowest to highest z order: + +PRIMITIVE COUNT, STATIC COUNT +solids: #{@args.outputs.solids.length}, #{@args.outputs.static_solids.length} +sprites: #{@args.outputs.sprites.length}, #{@args.outputs.static_sprites.length} +primitives: #{@args.outputs.primitives.length}, #{@args.outputs.static_primitives.length} +labels: #{@args.outputs.labels.length}, #{@args.outputs.static_labels.length} +lines: #{@args.outputs.lines.length}, #{@args.outputs.static_lines.length} +borders: #{@args.outputs.borders.length}, #{@args.outputs.static_borders.length} +debug: #{@args.outputs.debug.length}, #{@args.outputs.static_debug.length} +reserved: #{@args.outputs.reserved.length}, #{@args.outputs.static_reserved.length} + +** Additional Help +Come to the DragonRuby Discord channel if you need help troubleshooting performance issues. http://discord.dragonruby.org. + +Source code for these diagnostics can be found at: [[https://github.com/dragonruby/dragonruby-game-toolkit-contrib/]] +S + end + + def framerate_warning_message + <<-S +* WARNING: +Your average framerate dropped below 60 fps for two seconds. + +The average FPS was #{current_framerate}. + +** How To Disable Warning +If this warning is getting annoying put the following in your tick method: + +#+begin_src + args.gtk.log_level = :off +#+end_src + +#{framerate_get_diagnostics} + S + end + + def current_framerate_primitives + framerate_diagnostics_primitives + end + + def framerate_diagnostics_primitives + [ + { x: 0, y: 93.from_top, w: 500, h: 93, a: 128 }.solid, + { + x: 5, + y: 5.from_top, + text: "More Info via DragonRuby Console: $gtk.framerate_diagnostics", + r: 255, + g: 255, + b: 255, + size_enum: -2 + }.label, + { + x: 5, + y: 20.from_top, + text: "FPS: %.2f" % args.gtk.current_framerate, + r: 255, + g: 255, + b: 255, + size_enum: -2 + }.label, + { + x: 5, + y: 35.from_top, + text: "Draw Calls: #{$perf_counter_outputs_push_count}", + r: 255, + g: 255, + b: 255, + size_enum: -2 + }.label, + { + x: 5, + y: 50.from_top, + text: "Array Primitives: #{$perf_counter_primitive_is_array}", + r: 255, + g: 255, + b: 255, + size_enum: -2 + }.label, + { + x: 5, + y: 65.from_top, + text: "Mouse: #{@args.inputs.mouse.point}", + r: 255, + g: 255, + b: 255, + size_enum: -2 + }.label, + ] + end + + end + end +end diff --git a/dragon/geometry.rb b/dragon/geometry.rb index 607520f..b16c5b7 100644 --- a/dragon/geometry.rb +++ b/dragon/geometry.rb @@ -128,10 +128,6 @@ S raise ":angle_given_point has been deprecated use :angle_from instead." end - def distance_to other_point - Geometry.distance(self, other_point) - end - # @gtk def self.shift_line line, x, y if line.is_a?(Array) || line.is_a?(Hash) diff --git a/dragon/kernel_docs.rb b/dragon/kernel_docs.rb index 00eadf6..95e35be 100644 --- a/dragon/kernel_docs.rb +++ b/dragon/kernel_docs.rb @@ -53,13 +53,13 @@ S final_string += k.docs_all end - final_string += "\n" + (($gtk.read_file "docs/source.txt") || "") + final_string += "\n" + (($gtk.read_file_root "docs/source.txt") || "") html_parse_result = (__docs_to_html__ final_string) - $gtk.write_file 'docs/docs.txt', "#{final_string}" - $gtk.write_file 'docs/docs.html', html_parse_result[:html] - $gtk.write_file 'docs/parse_log.txt', (html_parse_result[:parse_log].join "\n") + $gtk.write_file_root 'docs/docs.txt', "#{final_string}" + $gtk.write_file_root 'docs/docs.html', html_parse_result[:html] + $gtk.write_file_root 'docs/parse_log.txt', (html_parse_result[:parse_log].join "\n") log "* INFO: All docs have been exported to docs/docs.txt." log "* INFO: All docs have been exported to docs/docs.html." diff --git a/dragon/log.rb b/dragon/log.rb index b2ee0e1..7bf09fd 100644 --- a/dragon/log.rb +++ b/dragon/log.rb @@ -27,19 +27,19 @@ module GTK class Log def self.write_to_log_and_puts *args return if $gtk.production - $gtk.append_file 'logs/log.txt', args.join("\n") + "\n" + $gtk.append_file_root 'logs/log.txt', args.join("\n") + "\n" args.each { |obj| $gtk.log obj, self } end def self.write_to_log_and_print *args return if $gtk.production - $gtk.append_file 'logs/log.txt', args.join("\n") + $gtk.append_file_root 'logs/log.txt', args.join("\n") Object.print(*args) end def self.puts_important *args return if $gtk.production - $gtk.append_file 'logs/log.txt', args.join("\n") + $gtk.append_file_root 'logs/log.txt', args.join("\n") $gtk.notify! "Important notification occurred." args.each { |obj| $gtk.log obj } end diff --git a/dragon/numeric.rb b/dragon/numeric.rb index 5485ff8..6d8b84d 100644 --- a/dragon/numeric.rb +++ b/dragon/numeric.rb @@ -9,11 +9,6 @@ class Numeric alias_method :gte, :>= alias_method :lte, :<= - - # Converts numeric value to distance from top of stage - def from_top - $gtk.args.grid.h - self - end # Converts a numeric value representing seconds into frames. # diff --git a/dragon/readme_docs.rb b/dragon/readme_docs.rb index 39fc283..f4d3696 100644 --- a/dragon/readme_docs.rb +++ b/dragon/readme_docs.rb @@ -240,13 +240,16 @@ Here's a fun Ruby thing: ~args.state.rotation ||= 0~ is shorthand for nice way to embed your initialization code right next to where you need the variable. -~args.state~ is a place you can hang your own data and have it survive -past the life of the function call. In this case, the current rotation -of our sprite, which is happily spinning at 60 frames per second. If -you don't specify rotation (or alpha, or color modulation, or a source -rectangle, etc), DragonRuby picks a reasonable default, and the array -is ordered by the most likely things you need to tell us: position, -size, name. + +~args.state~ is a place you can hang your own data. It's an open data +structure that allows you to define properties that are arbitrarily +nested. You don't need to define any kind of class. + +In this case, the current rotation of our sprite, which is happily +spinning at 60 frames per second. If you don't specify rotation (or +alpha, or color modulation, or a source rectangle, etc), DragonRuby +picks a reasonable default, and the array is ordered by the most +likely things you need to tell us: position, size, name. ** There Is No Delta Time @@ -335,79 +338,43 @@ around. Experiment a little. Add a few more things and have them interact in small ways. Want something to go away? Just don't add it to ~args.output~ anymore. -** IMPORTANT: Go Through All Of The Sample Apps! Study Them Thoroughly!! +* IMPORTANT: Go through all of the sample apps! Study them thoroughly!! No really, you should definitely do this! Now that you've completed the Hello World tutorial. Head over to the `samples` directory. It is very very important that you study the sample apps thoroughly! Go through them in order. Here is a short description of each sample app. -1. 00_beginner_ruby_primer: This is an interactive tutorial that shows how to render ~solid~s, animated ~sprite~s, ~label~s. -2. 00_intermediate_ruby_primer: This is a set of sample Ruby snippets that give you a high level introduction to the programming language. -3. 01_api_01_labels: Various ways to render ~label~s. -4. 01_api_02_lines: Various ways to render ~line~s. -5. 01_api_03_rects: Sample app shows various ways to render ~solid~s and ~border~s. -6. 01_api_04_sprites: Sample app shows various ways to render ~sprite~s. -7. 01_api_05_keyboard: Hows how to get keyboard input from the user. -8. 01_api_06_mouse: Hows how to get mouse mouse position. -9. 01_api_07_point_to_rect: How to get mouse input from the user and shows collision/hit detection. -10. 01_api_08_rect_to_rect: Hit detection/collision between two rectangles. -11. 01_api_10_controller: Interaction with a USB/Bluetooth controller. -12. 01_api_99_tech_demo: All the different render primitives along with using ~render_targets~. -13. 02_collision_01_simple: Collision detection with dynamically moving bodies. -14. 02_collision_02_moving_objects: Collision detection between many primitives, simple platformer physics, and keyboard input. -15. 02_collision_03_entities: Collision with entities and serves as a small introduction to ECS (entity component system). -16. 02_collision_04_ramp_with_debugging: How ramp trajectory can be calculated. -17. 02_collision_05_ramp_with_debugging_two: How ramp trajectory can be calculated. -18. 02_sprite_animation_and_keyboard_input: How to animate a sprite based off of keyboard input. -19. 03_mouse_click: How to determine what direction/vector a mouse was clicked relative to a player. -20. 04_sounds: How to play sounds and work with buttons. -21. 05_mouse_move: How to determine what direction/vector a mouse was clicked relative to a player. -22. 05_mouse_move_paint_app: Represents a simple paint app. -23. 05_mouse_move_tile_editor: A starting point for a tile editor. -24. 06_coordinate_systems: Shows the two origin systems within Game Toolkit where the origin is in the center and where the origin is at the bottom left. -25. 07_render_targets: Shows a powerful concept called ~render_target~s. You can use this to programatically create sprites (it's also useful for representing parts of a scene as if it was a view port/camera). -26. 07_render_targets_advanced: Advanced usage of ~render_target~s. -27. 08_platformer_collisions: Axis aligned collision along with platformer physics. -28. 08_platformer_collisions_metroidvania: How to save map data and place sprites live within a game. -29. 08_platformer_jumping_inertia: Jump physics and how inertia affects collision. -30. 09_controller_analog_usage_advanced_sprites: Extended properties of a ~sprite~ and how to change the rotation anchor point and render a subset/tile of a sprite. -31. 09_sprite_animation_using_tile_sheet: How to perform sprite animates using a tile sheet. -32. 10_save_load_game: Save and load game data. -33. 11_coersion_of_primitives: How primitives of one specific type can be rendered as another primitive type. -34. 11_hash_primitives: How primitives can be represented using a ~Hash~. -35. 12_controller_input_sprite_sheet_animations: How to leverage vectors to move a player around the screen. -36. 12_top_down_area: How to render a top down map and how to manage collision of a player. -37. 13_01_easing_functions: How to use lerping functions to define animations/movement. -38. 13_02_cubic_bezier: How to create a bezier curve using lines. -39. 13_03_easing_using_spline: How a collection of bezier curves can be used to define an animation. -40. 13_04_parametric_enemy_movement: How to define the movement of enemies and projectiles using lerping/parametric functions. -41. 14_sprite_limits: Upper limit for how many sprites can be rendered to the screen. -42. 14_sprite_limits_static_references: Upper limit for how many sprites can be rendered to the screen using ~static~ output collections (which are updated by reference as opposed to by value). -43. 15_collision_limits: How many collisions can be processed across many primitives. -44. 18_moddable_game: How you can make a game where content is authored by the player (modding support). -45. 19_lowrez_jam: How to use ~render_targets~ to create a low resolution game. -46. 20_roguelike_starting_point: A starting point for a roguelike and explores concepts such as line of sight. -47. 20_roguelike_starting_point_two: A starting point for a roguelike where sprites are provided from a tile map/tile sheet. -48. 21_mailbox_usage: How to do interprocess communication. -49. 22_trace_debugging: Debugging techniques and tracing execution through your game. -50. 22_trace_debugging_classes: Debugging techniques and tracing execution through your game. -51. 23_hexagonal_grid: How to make a tactical grid/map made of hexagons. -52. 23_isometric_grid: How to make a tactical grid/map made of isometric sprites. -53. 24_http_example: How to make http requests. -54. 25_3d_experiment_01_square: How to create 3D objects. -55. 26_jam_craft: Starting point for crafting game. It also shows how to customize the mouse cursor. -56. 99_sample_game_basic_gorillas: Reference implementation of a full game. Topics covered: physics, keyboard input, collision, sprite animation. -57. 99_sample_game_clepto_frog: Reference implementation of a full game. Topics covered: camera control, spring/rope physics, scene orchestration. -58. 99_sample_game_dueling_starships: Reference implementation that shows local multiplayer. Topics covered: vectors, particles, friction, inertia. -59. 99_sample_game_flappy_dragon: Reference implementation that is a clone of Flappy Bird. Topics covered: scene orchestration, collision, sound, sprite animations, lerping. -60. 99_sample_game_pong: Reference implementation of pong. -61. 99_sample_game_return_of_serenity: Reference implementation of low resolution story based game. -62. 99_sample_game_the_little_probe: Reference implementation of a full game. Topics covered: Arbitrary collision detection, loading map data, bounce/ball physics. -63. 99_sample_nddnug_workshop: Reference implementation of a full game. Topics covered: vectors, controller input, sound, trig functions. -64. 99_sample_snakemoji: Shows that Ruby supports coding with emojis. -65. 99_zz_gtk_unit_tests: A collection of unit tests that exercise parts of DragonRuby's API. - +** Guided Samples + +1. ~samples/00_learn_ruby_optional~: This directory contains sample apps that will help you learn the language. +2. ~samples/01_rendering_basics~: This set of samples will show you how to render basic primitives such as ~labels~, ~solids~, ~borders~, ~lines~, ~sprites~, and how to play ~sounds~. +3. ~samples/02_input_basics~: This set of samples show you how to accept input from the ~mouse~, ~keyboard~, and ~controllers~. +4. ~samples/03_rendering_sprites~: This set of samples shows you all the different ways to render sprites (including how to use a sprite sheet). +4. ~samples/04_physics_and_collision~: This set of samples shows how to do various types of collisions and physics. +5. ~samples/05_mouse~: This set of samples show more advanced usages of the mouse. +6. ~samples/06_save_load~: This set of samples show how to save and load game data. +7. ~samples/07_advanced_rendering~: This set of samples show how to programmatically render sprites using render targets. +8. ~samples/08_tweening_lerping_easing_functions~: This set of samples show how to perform animations. +9. ~samples/09_performance~: This set of samples show how to handle performance issues when a large number of sprites on the screen. +10. ~samples/10_advanced_debugging~: This set of samples show how advanced debugging techniques and testing techniques. +11. ~samples/11_http~: This set of samples show how use http. + +** Sample Games + +There are samples that contain the path ~samples/99_*~. The sample apps that are prefixed with ~99_~ show non-trivial implemementations for a real game: + +1. 3D Cube: Shows how to do faux 3D in DragonRuby. +2. Dueling Starships: A two player top-down versus game where each player controls a ship. +3. Flappy Dragon: DragonRuby's clone of Flappy Bird. +4. Pong: A simple implementation of the game Pong. +5. Snakemoji: The classic game of Snake but with all of the code written using emojis (sometimes you just have to have a little fun). +6. Solar System: A simulation of our solar system. +7. Crafting Starting Point: A starting point for those that want to build a crafting game. +8. Dev Tools: A set of sample apps that show how you can extend DragonRuby's Console, starting point for a tile editor, and a starting point for a paint app. +9. LOWREZ: Sample apps that show how to render at different resolutions. +10. RPG: Various sample apps that show how to create narrative, topdown, tactical grid-based, and roguelike RPGs. +11. Platformers: Various sample apps that show how to create different kinds of physics/collision based platformers. S end @@ -441,11 +408,11 @@ make it look like this: NOTE: Remove the ~#~ at the beginning of each line. #+begin_src -devid=bob -devtitle=Bob The Game Developer -gameid=mygame -gametitle=My Game -version=0.1 + devid=bob + devtitle=Bob The Game Developer + gameid=mygame + gametitle=My Game + version=0.1 #+end_src The ~devid~ property is the username you use to log into Itch.io. @@ -459,7 +426,7 @@ The ~version~ can be any ~major.minor~ number format. Open up the terminal and run this from the command line: #+begin_src -./dragonruby-publish --only-package mygame + ./dragonruby-publish --only-package mygame #+end_src (if you're on Windows, don't put the "./" on the front. That's a Mac and @@ -474,7 +441,7 @@ For the HTML version of your game after you upload it. Check the checkbox labele For subsequent updates you can use an automated deployment to Itch.io: #+begin_src -./dragonruby-publish mygame + ./dragonruby-publish mygame #+end_src DragonRuby will package _and publish_ your game to itch.io! Tell your @@ -487,7 +454,7 @@ S def docs_dragonruby_philosophy <<-S -** DragonRuby's Philosophy +* DragonRuby's Philosophy The following tenants of DragonRuby are what set us apart from other game engines. Given that Game Toolkit is a relatively new engine, @@ -495,7 +462,7 @@ there are definitely features that are missing. So having a big check list of "all the cool things" is not this engine's forte. This is compensated with a strong commitment to the following principals. -*** Challenge The Status Quo +** Challenge The Status Quo Game engines of today are in a local maximum and don't take into consideration the challenges of this day and age. Unity and GameMaker @@ -509,7 +476,7 @@ It's a hard pill to swallow, but forget blindly accepted best practices and try to figure out the underlying motivation for a specific approach to game development. Collaborate with us. -*** Continuity of Design +** Continuity of Design There is a programming idiom in software called "the pit of success". The term normalizes up front pain as a necessity in the @@ -524,7 +491,7 @@ render primitives can be represented as tuples/arrays, hashes, open structs/entities, and then finally classes (as opposed to forcing devs to use classes upfront). -*** Release Often And Soon +** Release Often And Soon The biggest mistake game devs make is spending too much time in isolation building their game. Release something, however small, and @@ -541,7 +508,7 @@ Remember: Real artists ship. #+end_quote -*** Sustainable And Ethical Monetization +** Sustainable And Ethical Monetization We all aspire to put food on the table doing what we love. Whether it is building games, writing tools to support game development, or @@ -551,7 +518,7 @@ Charge a fair amount of money for the things you create. It's expected and encouraged within the community. Give what you create away for free to those that can't afford it. -*** Sustainable And Ethical Open Source +** Sustainable And Ethical Open Source This goes hand in hand with sustainable and ethical monetization. The current state of open source is not sustainable. There is an immense @@ -562,7 +529,7 @@ still trying to figure out the best solution). So, don't be "that guy" in the Discord that says "DragonRuby should be free and open source!" You will be personally flogged by Amir. -*** People Over Entities +** People Over Entities We prioritize the endorsement of real people over faceless entities. This game engine, and other products we create, are not @@ -570,13 +537,13 @@ insignificant line items of a large company. And you aren't a generic "commodity" or "corporate resource". So be active in the community Discord and you'll reap the benefits as more devs use DragonRuby. -*** Building A Game Should Be Fun And Bring Happiness +** Building A Game Should Be Fun And Bring Happiness We will prioritize the removal of pain. The aesthetics of Ruby make it such a joy to work with, and we want to capture that within the engine. -*** Real World Application Drives Features +** Real World Application Drives Features We are bombarded by marketing speak day in and day out. We don't do that here. There are things that are really great in the engine, and @@ -894,25 +861,25 @@ sure_ you've initialized a default value. #+begin_src def tick args # initialize your game state ONCE - args.player.x ||= 0 - args.player.y ||= 0 - args.player.hp ||= 100 + args.state.player.x ||= 0 + args.state.player.y ||= 0 + args.state.player.hp ||= 100 # increment the x position of the character by one every frame - args.player.x += 1 + args.state.player.x += 1 # Render a sprite with a label above the sprite args.outputs.sprites << [ - args.player.x, - args.player.y, + args.state.player.x, + args.state.player.y, 32, 32, "player.png" ] args.outputs.labels << [ - args.player.x, - args.player.y - 50, - args.player.hp + args.state.player.x, + args.state.player.y - 50, + args.state.player.hp ] end #+end_src @@ -971,11 +938,9 @@ Under DragonRuby LLP, we offer a number of products (with more on the way): - Game Toolkit (GTK): A 2D game engine that is compatible with modern - gaming platforms. [Home Page]() [FAQ Page]() + gaming platforms. - RubyMotion (RM): A compiler toolchain that allows you to build native, cross-platform mobile - apps. [Home Page]() [FAQ Page]() -- Commandline Toolkit (CTK): A zero dependency, zero installation Ruby - environment that works on Windows, Mac, and Linux. [Home Page]() [FAQ Page]() + apps. [[http://rubymotion.com]] All of the products above leverage a shared core called DragonRuby. @@ -994,7 +959,7 @@ identifier huh? The response to this question requires a few subparts. First we need to clarify some terms. Specifically _language specification_ vs _runtime_. -*** Okay... so what is the difference between a language specification and a runtime? +**** Okay... so what is the difference between a language specification and a runtime? A runtime is an _implementation_ of a language specification. When people say "Ruby," they are usually referring to "the Ruby 3.0+ language @@ -1003,13 +968,13 @@ specification implemented via the CRuby/MRI Runtime." But, there are many Ruby Runtimes: CRuby/MRI, JRuby, Truffle, Rubinius, Artichoke, and (last but certainly not least) DragonRuby. -*** Okay... what language specification does DragonRuby use then? +**** Okay... what language specification does DragonRuby use then? DragonRuby's goal is to be compliant with the ISO/IEC 30170:2012 standard. It's syntax is Ruby 2.x compatible, but also contains semantic changes that help it natively interface with platform specific libraries. -*** So... why another runtime? +**** So... why another runtime? The elevator pitch is: @@ -1018,7 +983,7 @@ within the runtime allows us to target platforms no other Ruby can target: PC, Mac, Linux, Raspberry Pi, WASM, iOS, Android, Nintendo Switch, PS4, Xbox, and Scadia. -*** What does Multilevel Cross-platform mean? +**** What does Multilevel Cross-platform mean? There are complexities associated with targeting all the platforms we support. Because of this, the runtime had to be architected in such a @@ -1055,13 +1020,87 @@ implementations of Ruby; provide fast, native code execution on proprietary platforms; ensure good separation between these two worlds; and provides a means to add new platforms without going insane. -*** Cool cool. So given that I understand everything to this point, can we answer the original question? What is DragonRuby? +**** Cool cool. So given that I understand everything to this point, can we answer the original question? What is DragonRuby? DragonRuby is a Ruby runtime implementation that takes all the lessons we've learned from MRI/CRuby, and merges it with the latest and greatest compiler and OSS technologies. -** Frequent Comments +*** How is DragonRuby different than MRI? + +DragonRuby supports a subset of MRI apis. Our target is to support all +of mRuby's standard lib. There are challenges to this given the number +of platforms we are trying to support (specifically console). + +**** Does DragonRuby support Gems? + +DragonRuby does not support gems because that requires the +installation of MRI Ruby on the developer's machine (which is a +non-starter given that we want DragonRuby to be a zero dependency +runtime). While this seems easy for Mac and Linux, it is much harder +on Windows and Raspberry Pi. mRuby has taken the approach of having a +git repository for compatible gems and we will most likely follow +suite: [[https://github.com/mruby/mgem-list]]. + +**** Does DragonRuby have a REPL/IRB? + +You can use DragonRuby's Console within the game to inspect object and +execute small pieces of code. For more complex pieces of code create a +file called ~repl.rb~ and put it in ~mygame/app/repl.rb~: + +- Any code you write in there will be executed when you change the file. You can organize different pieces of code using the ~repl~ method: + +#+begin_src ruby + repl do + puts "hello world" + puts 1 + 1 + end +#+end_src + +- If you use the `repl` method, the code will be executed and the DragonRuby Console will automatically open so you can see the results (on Mac and Linux, the results will also be printed to the terminal). + +- All ~puts~ statements will also be saved to ~logs/log.txt~. So if you want to stay in your editor and not look at the terminal, or the DragonRuby Console, you can ~tail~ this file. + +4. To ignore code in ~repl.rb~, instead of commenting it out, prefix ~repl~ with the letter ~x~ and it'll be ignored. + +#+begin_src ruby + xrepl do # <------- line is prefixed with an "x" + puts "hello world" + puts 1 + 1 + end + + # This code will be executed when you save the file. + repl do + puts "Hello" + end + + repl do + puts "This code will also be executed." + end + + # use xrepl to "comment out" code + xrepl do + puts "This code will not be executed because of the x infront of repl". + end +#+end_src + +**** Does DragonRuby support ~pry~ or have any other debugging facilities? + +~pry~ is a gem that assumes you are using the MRI Runtime (which is +incompatible with DragonRuby). Eventually DragonRuby will have a pry +based experience that is compatible with a debugging infrastructure +called LLDB. Take the time to read about LLDB as it shows the +challenges in creating something that is compatible. + +You can use DragonRuby's replay capabilities to troubleshoot: + +1. DragonRuby is hot loaded which gives you a very fast feedback loop (if the game throws an exception, it's because of the code you just added). +2. Use ~./dragonruby mygame --record~ to create a game play recording that you can use to find the exception (you can replay a recoding by executing ~./dragonruby mygame --replay last_replay.txt~ or through the DragonRuby Console using ~$gtk.recording.start_replay "last_replay.txt"~. +3. DragonRuby also ships with a unit testing facility. You can invoke the following command to run a test: ~./dragonruby . --eval some_ruby_file.rb --no-tick~. +4. Get into the habit of adding debugging facilities within the game itself. You can add drawing primitives to ~args.outputs.debug~ that will render on top of your game but will be ignored in a production release. +5. Debugging something that runs at 60fps is (imo) not that helpful. The exception you are seeing could have been because of a change that occurred many frames ago. + +** Frequent Comments About Ruby as a Language Choice *** But Ruby is dead. @@ -1113,6 +1152,8 @@ If you have ideas on how we can do this, email us! If the reason above isn't sufficient, then definitely use something else. +All this being said, we do have parts of the engine open sourced on GitHub: [[https://github.com/dragonruby/dragonruby-game-toolkit-contrib/]] + *** DragonRuby is for pay. You should offer a free version. If you can afford to pay for DragonRuby, you should (and will). We don't go diff --git a/dragon/runtime_docs.rb b/dragon/runtime_docs.rb index 09759b4..50734ce 100644 --- a/dragon/runtime_docs.rb +++ b/dragon/runtime_docs.rb @@ -31,6 +31,23 @@ This function returns the width and height of a string. #+end_src S end + + def docs_write_file + <<-S +* DOCS: ~GTK::Runtime#write_file~ +This function takes in two parameters. The first paramter is the file path and assumes the the game +directory is the root. The second parameter is the string that will be written. The method overwrites whatever +is currently in the file. Use ~GTK::Runtime#append_file~ to append to the file as opposed to overwriting. + +#+begin_src ruby + def tick args + if args.inputs.mouse.click + args.gtk.write_file "last-mouse-click.txt", "Mouse was clicked at \#{args.state.tick_count}." + end + end +#+end_src +S + end end class GTK::Runtime diff --git a/dragon/trace.rb b/dragon/trace.rb index 04067bf..ec154f3 100644 --- a/dragon/trace.rb +++ b/dragon/trace.rb @@ -65,7 +65,7 @@ module GTK @traced_classes.clear $trace_enabled = false if !$gtk.production - $gtk.write_file 'logs/trace.txt', "Add trace!(SOMEOBJECT) to the top of ~tick~ and this file will be populated with invocation information.\n" + $gtk.write_file_root 'logs/trace.txt', "Add trace!(SOMEOBJECT) to the top of ~tick~ and this file will be populated with invocation information.\n" end end @@ -87,9 +87,9 @@ module GTK if $trace_puts.length > 0 text = $trace_puts.join("") if pad_with_newline - $gtk.append_file 'logs/trace.txt', "\n" + text.strip + $gtk.append_file_root 'logs/trace.txt', "\n" + text.strip else - $gtk.append_file 'logs/trace.txt', text.strip + $gtk.append_file_root 'logs/trace.txt', text.strip end end $trace_puts.clear |
