Class: Budgie::Application
- Inherits:
-
Object
- Object
- Budgie::Application
- Extended by:
- Forwardable
- Includes:
- GL, GLU, GLUT
- Defined in:
- lib/budgie/application.rb
Instance Attribute Summary collapse
-
#camera ⇒ Object
readonly
Returns the value of attribute camera.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
-
#palette ⇒ Object
readonly
Returns the value of attribute palette.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#centered_mouse_mode ⇒ Object
hides cursor image centers cursor rotates camera.
-
#click(button, state, x, y) ⇒ Object
Event: mouse click.
-
#color_at(x, y) {|r, g, b| ... } ⇒ Object
yields pixel color at (x, y) in window coordinates.
-
#display ⇒ Object
Event: redraw.
-
#escape ⇒ Object
User action: exit application.
-
#free_mouse_mode ⇒ Object
draws default cursor.
-
#idle ⇒ Object
Event: no events.
-
#initialize(filename, width = 800, height = 600) ⇒ Application
constructor
A new instance of Application.
- #message ⇒ Object
-
#mouse(x, y) ⇒ Object
Event: mouse move.
- #mouse_mode ⇒ Object
-
#press(key, x, y) ⇒ Object
Event: key press.
-
#release(key, x, y) ⇒ Object
Event: key release.
-
#reshape(width, height) ⇒ Object
Event: window size change.
- #send_key_presses ⇒ Object
- #setup ⇒ Object
-
#update_camera_step(delta) ⇒ Object
smooth camera movement.
-
#update_selection_at(x, y) ⇒ Object
saves (x, y, z) coordinates of the block under cursor.
-
#update_selection_color_at(x, y) ⇒ Object
saves (r, g, b) color of the pixel under cursor.
-
#update_selection_normal_at(x, y) ⇒ Object
saves (x, y, z) normal of the block’s face under cursor.
- #update_window_title(delta) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(filename, width = 800, height = 600) ⇒ Application
Returns a new instance of Application.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/budgie/application.rb', line 25 def initialize(filename, width = 800, height = 600) usage unless filename @filename = filename glutInit glutInitDisplayMode GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH glutInitWindowSize width, height glutInitWindowPosition width / 2, height / 2 @window = glutCreateWindow "ffxel" glutIdleFunc :idle glutMouseFunc :click glutDisplayFunc :display glutReshapeFunc :reshape glutKeyboardFunc :press glutKeyboardUpFunc :release glutPassiveMotionFunc :mouse glutSetKeyRepeat GLUT_KEY_REPEAT_OFF reshape width, height setup glutMainLoop end |
Instance Attribute Details
#camera ⇒ Object (readonly)
Returns the value of attribute camera.
21 22 23 |
# File 'lib/budgie/application.rb', line 21 def camera @camera end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
21 22 23 |
# File 'lib/budgie/application.rb', line 21 def height @height end |
#map ⇒ Object (readonly)
Returns the value of attribute map.
21 22 23 |
# File 'lib/budgie/application.rb', line 21 def map @map end |
#palette ⇒ Object (readonly)
Returns the value of attribute palette.
21 22 23 |
# File 'lib/budgie/application.rb', line 21 def palette @palette end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
21 22 23 |
# File 'lib/budgie/application.rb', line 21 def selection @selection end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
21 22 23 |
# File 'lib/budgie/application.rb', line 21 def width @width end |
Instance Method Details
#centered_mouse_mode ⇒ Object
hides cursor image centers cursor rotates camera
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/budgie/application.rb', line 210 def centered_mouse_mode glutSetCursor GLUT_CURSOR_NONE center_x = @width / 2 center_y = @height / 2 dx = center_x - @mouse_x dy = center_y - @mouse_y return if dx == 0 && dy == 0 glutWarpPointer center_x, center_y quarter_x = center_x / 2 quarter_y = center_y / 2 return if dx.abs > quarter_x || dy.abs > quarter_y dx = 50.0 * dx / @width dy = 50.0 * dy / @height @camera.alpha += dx @camera.beta += dy end |
#click(button, state, x, y) ⇒ Object
Event: mouse click
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/budgie/application.rb', line 131 def click(, state, x, y) return unless state == GLUT_UP update_selection_at x, y if == GLUT_LEFT_BUTTON && glutGetModifiers == GLUT_ACTIVE_SHIFT @scene.replace elsif == GLUT_LEFT_BUTTON && glutGetModifiers == GLUT_ACTIVE_CTRL @scene.remove elsif == GLUT_LEFT_BUTTON @scene.place elsif == GLUT_RIGHT_BUTTON update_selection_color_at x, y end end |
#color_at(x, y) {|r, g, b| ... } ⇒ Object
yields pixel color at (x, y) in window coordinates
155 156 157 158 159 160 |
# File 'lib/budgie/application.rb', line 155 def color_at(x, y, &block) y = @height - y rgb = glReadPixels x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE r, g, b = rgb.unpack 'C*' yield r, g, b end |
#display ⇒ Object
Event: redraw
84 85 86 87 |
# File 'lib/budgie/application.rb', line 84 def display @scene.display glutSwapBuffers end |
#escape ⇒ Object
User action: exit application
148 149 150 151 152 |
# File 'lib/budgie/application.rb', line 148 def escape @store_map.save @map glutDestroyWindow @window exit 0 end |
#free_mouse_mode ⇒ Object
draws default cursor
203 204 205 |
# File 'lib/budgie/application.rb', line 203 def free_mouse_mode glutSetCursor GLUT_CURSOR_INHERIT end |
#idle ⇒ Object
Event: no events
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/budgie/application.rb', line 90 def idle time = Time.now.to_f @time = time unless @time delta = time - @time @time = time update_window_title(delta) update_camera_step(delta) send_key_presses update_selection_at @mouse_x, @mouse_y glutPostRedisplay end |
#message ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/budgie/application.rb', line 259 def %{ Usage ----- $ budgie model-file.png Keyboard controls ----------------- WASD horizontal movement space move up C move down E toggle palette F free mouse mode Mouse controls -------------- Left Click place a block Right Click colorpicker Shift + Left Click replace current block color Control + Left Click remove block } end |
#mouse(x, y) ⇒ Object
Event: mouse move
123 124 125 126 127 128 |
# File 'lib/budgie/application.rb', line 123 def mouse(x, y) @mouse_x = x @mouse_y = y send mouse_mode end |
#mouse_mode ⇒ Object
251 252 253 254 255 256 257 |
# File 'lib/budgie/application.rb', line 251 def mouse_mode if @keyboard[@settings.free_mouse_mode_key] 'free_mouse_mode' else 'centered_mouse_mode' end end |
#press(key, x, y) ⇒ Object
Event: key press
106 107 108 |
# File 'lib/budgie/application.rb', line 106 def press(key, x, y) @keyboard[key.downcase] = true end |
#release(key, x, y) ⇒ Object
Event: key release
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/budgie/application.rb', line 111 def release(key, x, y) @keyboard[key.downcase] = false if key == @settings.palette_key if @scene.fixed? @scene.release else @scene.fix end end end |
#reshape(width, height) ⇒ Object
Event: window size change
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/budgie/application.rb', line 70 def reshape(width, height) @width = width @height = height height = 1 if height == 0 glViewport 0, 0, width, height glMatrixMode GL_PROJECTION glLoadIdentity gluPerspective 90.0, width.to_f / height.to_f, 0.001, 100.0 end |
#send_key_presses ⇒ Object
195 196 197 198 199 200 |
# File 'lib/budgie/application.rb', line 195 def send_key_presses @keyboard.each do |key, pressed| key = @settings.keymap[key] send key if key && pressed end end |
#setup ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/budgie/application.rb', line 54 def setup @settings = Settings.new @keyboard = {} @mouse_x = @mouse_y = 0 @camera = Camera.new @palette = Palette.new @selection = Selection.new @store_map = StoreMap.new @filename, @palette @map = @store_map.load @scene = Scene.new self end |
#update_camera_step(delta) ⇒ Object
smooth camera movement
191 192 193 |
# File 'lib/budgie/application.rb', line 191 def update_camera_step(delta) @camera.step = 30 * delta end |
#update_selection_at(x, y) ⇒ Object
saves (x, y, z) coordinates of the block under cursor
163 164 165 166 167 168 169 |
# File 'lib/budgie/application.rb', line 163 def update_selection_at(x, y) @scene.picker color_at x, y do |r, g, b| @selection.from_color r, g, b end update_selection_normal_at x, y end |
#update_selection_color_at(x, y) ⇒ Object
saves (r, g, b) color of the pixel under cursor
181 182 183 184 185 186 187 188 |
# File 'lib/budgie/application.rb', line 181 def update_selection_color_at(x, y) @scene.color_picker color_at x, y do |r, g, b| @selection.r = r @selection.g = g @selection.b = b end end |
#update_selection_normal_at(x, y) ⇒ Object
saves (x, y, z) normal of the block’s face under cursor
172 173 174 175 176 177 178 |
# File 'lib/budgie/application.rb', line 172 def update_selection_normal_at(x, y) return unless @selection.selected @scene.normal_picker color_at x, y do |r, g, b| @selection.normal_from_color r, g, b end end |
#update_window_title(delta) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/budgie/application.rb', line 235 def update_window_title(delta) @delta = 0 unless @delta @frames = 0 unless @frames @delta += delta @frames += 1 if @delta > 1 s = @selection glutSetWindowTitle "@map.max: #{@map.max}; fps: #{@frames}; selection: #{s.new_x}, #{s.new_y}, #{s.new_z}" @delta = 0 @frames = 0 end end |
#usage ⇒ Object
288 289 290 291 |
# File 'lib/budgie/application.rb', line 288 def usage puts .gsub /^ {4}/, '' exit 0 end |