Class: Ruby2D::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/window.rb,
ext/ruby2d/ruby2d.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: 640, height: 480, title: "Ruby 2D", fps: 60, vsync: true) ⇒ Window

Returns a new instance of Window.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby2d/window.rb', line 7

def initialize(width: 640, height: 480, title: "Ruby 2D", fps: 60, vsync: true)
  @width, @height, @title = width, height, title
  @mouse_x = @mouse_y = 0
  @fps_cap = fps
  @fps = 60
  @vsync = vsync
  
  @objects = []
  @keys = {}
  @keys_down = {}
  @controller = {}
  @update_proc = Proc.new {}
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/ruby2d/window.rb', line 5

def height
  @height
end

#mouse_xObject (readonly)

Returns the value of attribute mouse_x.



5
6
7
# File 'lib/ruby2d/window.rb', line 5

def mouse_x
  @mouse_x
end

#mouse_yObject (readonly)

Returns the value of attribute mouse_y.



5
6
7
# File 'lib/ruby2d/window.rb', line 5

def mouse_y
  @mouse_y
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/ruby2d/window.rb', line 5

def title
  @title
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/ruby2d/window.rb', line 5

def width
  @width
end

Instance Method Details

#add(o) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby2d/window.rb', line 54

def add(o)
  case o
  when nil
    raise Error, "Cannot add '#{o.class}' to window!"
  when Array
    o.each { |x| add_object(x) }
  else
    add_object(o)
  end
end

#clearObject



78
79
80
# File 'lib/ruby2d/window.rb', line 78

def clear
  @objects.clear
end

#controller_callback(is_axis, axis, val, is_btn, btn) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ruby2d/window.rb', line 119

def controller_callback(is_axis, axis, val, is_btn, btn)
  
  # puts "is_axis: #{is_axis}, axis: #{axis}, val: #{val}, is_btn: #{is_btn}, btn: #{btn}"
  
  if is_axis
    if axis == 0 && val == -32768
      event = 'left'
    elsif axis == 0 && val == 32767
      event = 'right'
    elsif axis == 1 && val == -32768
      event = 'up'
    elsif axis == 1 && val == 32767
      event = 'down'
    end
  elsif is_btn
    event = btn
  end
  
  if @controller.has_key? event
    @controller[event].call
  end
end

#get(sym) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby2d/window.rb', line 21

def get(sym)
  case sym
  when :window
    return self
  when :title
    return @title
  when :width
    return @width
  when :height
    return @height
  when :fps
    return @fps
  when :mouse_x
    return @mouse_x
  when :mouse_y
    return @mouse_y
  end
end

#key_callback(key) ⇒ Object



105
106
107
108
109
110
# File 'lib/ruby2d/window.rb', line 105

def key_callback(key)
  key.downcase!
  if @keys.has_key? key
    @keys[key].call
  end
end

#key_down_callback(key) ⇒ Object



112
113
114
115
116
117
# File 'lib/ruby2d/window.rb', line 112

def key_down_callback(key)
  key.downcase!
  if @keys_down.has_key? key
    @keys_down[key].call
  end
end

#on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby2d/window.rb', line 87

def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
  unless mouse.nil?
    # reg_mouse(btn, &proc)
  end
  
  unless key.nil?
    reg_key(key, &proc)
  end
  
  unless key_down.nil?
    reg_key_down(key_down, &proc)
  end
  
  unless controller.nil?
    reg_controller(controller, &proc)
  end
end

#remove(o) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby2d/window.rb', line 65

def remove(o)
  if o == nil
    raise Error, "Cannot remove '#{o.class}' from window!"
  end
  
  if i = @objects.index(o)
    @objects.slice!(i)
    true
  else
    false
  end
end

#set(opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby2d/window.rb', line 40

def set(opts)
  if opts.include? :title
    @title = opts[:title]
  end
  
  if opts.include? :width
    @width = opts[:width]
  end
  
  if opts.include? :height
    @height = opts[:height]
  end
end

#showObject

Ruby2D::Window#show



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/ruby2d/ruby2d.c', line 250

static VALUE ruby2d_show(VALUE s) {
  self = s;
  
  char *title = RSTRING_PTR(rb_iv_get(self, "@title"));
  int width   = NUM2INT(rb_iv_get(self, "@width"));
  int height  = NUM2INT(rb_iv_get(self, "@height"));
  
  window = S2D_CreateWindow(
    title, width, height, update, render, 0
  );
  
  window->on_key = on_key;
  window->on_key_down = on_key_down;
  window->on_controller = on_controller;
  
  S2D_Show(window);
  
  atexit(close_window);
  return Qnil;
}

#update(&proc) ⇒ Object



82
83
84
85
# File 'lib/ruby2d/window.rb', line 82

def update(&proc)
  @update_proc = proc
  true
end

#update_callbackObject



142
143
144
# File 'lib/ruby2d/window.rb', line 142

def update_callback
  @update_proc.call
end