|
| 1 | +# Emulation of a 64x64 canvas. Don't change this file unless you know what you're doing :-) |
| 2 | +# Head over to main.rb and study the code there. |
| 3 | + |
| 4 | +LOWREZ_SIZE = 64 |
| 5 | +LOWREZ_ZOOM = 10 |
| 6 | +LOWREZ_ZOOMED_SIZE = LOWREZ_SIZE * LOWREZ_ZOOM |
| 7 | +LOWREZ_X_OFFSET = (1280 - LOWREZ_ZOOMED_SIZE).half |
| 8 | +LOWREZ_Y_OFFSET = ( 720 - LOWREZ_ZOOMED_SIZE).half |
| 9 | + |
| 10 | +LOWREZ_FONT_XL = -1 |
| 11 | +LOWREZ_FONT_XL_HEIGHT = 20 |
| 12 | + |
| 13 | +LOWREZ_FONT_LG = -3.5 |
| 14 | +LOWREZ_FONT_LG_HEIGHT = 15 |
| 15 | + |
| 16 | +LOWREZ_FONT_MD = -6 |
| 17 | +LOWREZ_FONT_MD_HEIGHT = 10 |
| 18 | + |
| 19 | +LOWREZ_FONT_SM = -8.5 |
| 20 | +LOWREZ_FONT_SM_HEIGHT = 5 |
| 21 | + |
| 22 | +class LowrezOutputs |
| 23 | + attr_accessor :width, :height |
| 24 | + |
| 25 | + def initialize args |
| 26 | + @args = args |
| 27 | + @background_color ||= [0, 0, 0] |
| 28 | + @args.outputs.background_color = @background_color |
| 29 | + end |
| 30 | + |
| 31 | + def background_color |
| 32 | + @background_color ||= [0, 0, 0] |
| 33 | + end |
| 34 | + |
| 35 | + def background_color= opts |
| 36 | + @background_color = opts |
| 37 | + @args.outputs.background_color = @background_color |
| 38 | + @args.render_target(:lowrez).solids << [0, 0, LOWREZ_SIZE, LOWREZ_SIZE, @background_color] |
| 39 | + end |
| 40 | + |
| 41 | + def solids |
| 42 | + @args.render_target(:lowrez).solids |
| 43 | + end |
| 44 | + |
| 45 | + def borders |
| 46 | + @args.render_target(:lowrez).borders |
| 47 | + end |
| 48 | + |
| 49 | + def sprites |
| 50 | + @args.render_target(:lowrez).sprites |
| 51 | + end |
| 52 | + |
| 53 | + def labels |
| 54 | + @args.render_target(:lowrez).labels |
| 55 | + end |
| 56 | + |
| 57 | + def default_label |
| 58 | + { |
| 59 | + x: 0, |
| 60 | + y: 63, |
| 61 | + text: "", |
| 62 | + size_enum: LOWREZ_FONT_SM, |
| 63 | + alignment_enum: 0, |
| 64 | + r: 0, |
| 65 | + g: 0, |
| 66 | + b: 0, |
| 67 | + a: 255, |
| 68 | + font: 'fonts/lowrez.ttf' |
| 69 | + } |
| 70 | + end |
| 71 | + |
| 72 | + def lines |
| 73 | + @args.render_target(:lowrez).lines |
| 74 | + end |
| 75 | + |
| 76 | + def primitives |
| 77 | + @args.render_target(:lowrez).primitives |
| 78 | + end |
| 79 | + |
| 80 | + def click |
| 81 | + return nil unless @args.inputs.mouse.click |
| 82 | + mouse |
| 83 | + end |
| 84 | + |
| 85 | + def mouse_click |
| 86 | + click |
| 87 | + end |
| 88 | + |
| 89 | + def mouse_down |
| 90 | + @args.inputs.mouse.down |
| 91 | + end |
| 92 | + |
| 93 | + def mouse_up |
| 94 | + @args.inputs.mouse.up |
| 95 | + end |
| 96 | + |
| 97 | + def mouse |
| 98 | + [ |
| 99 | + ((@args.inputs.mouse.x - LOWREZ_X_OFFSET).idiv(LOWREZ_ZOOM)), |
| 100 | + ((@args.inputs.mouse.y - LOWREZ_Y_OFFSET).idiv(LOWREZ_ZOOM)) |
| 101 | + ] |
| 102 | + end |
| 103 | + |
| 104 | + def mouse_position |
| 105 | + mouse |
| 106 | + end |
| 107 | + |
| 108 | + def keyboard |
| 109 | + @args.inputs.keyboard |
| 110 | + end |
| 111 | +end |
| 112 | + |
| 113 | +class GTK::Args |
| 114 | + def init_lowrez |
| 115 | + return if @lowrez |
| 116 | + @lowrez = LowrezOutputs.new self |
| 117 | + end |
| 118 | + |
| 119 | + def lowrez |
| 120 | + @lowrez |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +module GTK |
| 125 | + class Runtime |
| 126 | + alias_method :__original_tick_core__, :tick_core unless Runtime.instance_methods.include?(:__original_tick_core__) |
| 127 | + |
| 128 | + def tick_core |
| 129 | + @args.init_lowrez |
| 130 | + __original_tick_core__ |
| 131 | + @args.render_target(:lowrez) |
| 132 | + .labels |
| 133 | + .each do |l| |
| 134 | + l.text = l.text.downcase |
| 135 | + l.y += 1 |
| 136 | + end |
| 137 | + |
| 138 | + @args.render_target(:lowrez) |
| 139 | + .lines |
| 140 | + .each do |l| |
| 141 | + l.y += 1 |
| 142 | + l.y2 += 1 |
| 143 | + l.y2 += 1 if l.y1 != l.y2 |
| 144 | + l.x2 += 1 if l.x1 != l.x2 |
| 145 | + end |
| 146 | + |
| 147 | + @args.outputs |
| 148 | + .sprites << { x: 320, |
| 149 | + y: 40, |
| 150 | + w: 640, |
| 151 | + h: 640, |
| 152 | + source_x: 0, |
| 153 | + source_y: 0, |
| 154 | + source_w: 64, |
| 155 | + source_h: 64, |
| 156 | + path: :lowrez } |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments