Class: Gosu::Graphics

Inherits:
Object
  • Object
show all
Defined in:
lib/gosu_android/graphics/graphics.rb

Constant Summary collapse

MAX_TEXTURE_SIZE =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(android_initializer) ⇒ Graphics

Returns a new instance of Graphics.



20
21
22
# File 'lib/gosu_android/graphics/graphics.rb', line 20

def initialize(android_initializer)
  @android_initializer = android_initializer
end

Instance Attribute Details

#fullscreenObject (readonly)

Returns the value of attribute fullscreen.



15
16
17
# File 'lib/gosu_android/graphics/graphics.rb', line 15

def fullscreen
  @fullscreen
end

#glObject (readonly)

Returns the value of attribute gl.



16
17
18
# File 'lib/gosu_android/graphics/graphics.rb', line 16

def gl
  @gl
end

#heightObject (readonly)

Returns the value of attribute height.



14
15
16
# File 'lib/gosu_android/graphics/graphics.rb', line 14

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



14
15
16
# File 'lib/gosu_android/graphics/graphics.rb', line 14

def width
  @width
end

Instance Method Details

#begin(clear_with_color = Color::BLACK) ⇒ Object

Prepares the graphics object for drawing. Nothing must be drawn without calling begin.



43
44
45
46
47
48
49
50
51
52
# File 'lib/gosu_android/graphics/graphics.rb', line 43

def begin(clear_with_color = Color::BLACK)
  if @gl == nil
    raise "Surface must be created before calling begin"
  end
  @gl.glClearColor(clear_with_color.red / 255.0, clear_with_color.green / 255.0,
    clear_with_color.blue / 255.0, clear_with_color.alpha / 255.0)
  @gl.glClear(JavaImports::GL10::GL_COLOR_BUFFER_BIT | JavaImports::GL10::GL_DEPTH_BUFFER_BIT)

  true
end

#begin_clipping(x, y, width, height) ⇒ Object

Enables clipping to a specified rectangle.



79
# File 'lib/gosu_android/graphics/graphics.rb', line 79

def begin_clipping(x,  y,  width,  height); end

#begin_recordingObject

Starts recording a macro. Cannot be nested.



84
# File 'lib/gosu_android/graphics/graphics.rb', line 84

def begin_recording; end

#beginGLObject

Finishes all pending Gosu drawing operations and executes the following OpenGL code in a clean environment.



67
# File 'lib/gosu_android/graphics/graphics.rb', line 67

def beginGL; end

#create_image(src, src_x, src_y, src_width, src_height, border_flags) ⇒ Object

Turns a portion of a bitmap o something that can be drawn on this graphics object.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
# File 'lib/gosu_android/graphics/graphics.rb', line 137

def create_image(src, src_x,  src_y,  src_width,  src_height, border_flags)
  max_size = MAX_TEXTURE_SIZE
  #Special case: If the texture is supposed to have hard borders,
  #is quadratic, has a size that is at least 64 pixels but less than 256
  #pixels and a power of two, create a single texture just for this image.
  if ((border_flags & BF_TILEABLE) == BF_TILEABLE and src_width == src_height and
    (src_width & (src_width - 1)) == 0 and src_width >= 64)

    texture = Texture.new(src_width, @gl)
    #Use the source bitmap directly if the source area completely covers
    #it.
    if (src_x == 0 and src_width == src.width and src_y == 0 and src_height == src.height)
      data = texture.try_alloc(self, @queues, texture, src, 0)
    else
      trimmed_src = Bitmap.new
      trimmed_src.resize(src_width, src_height)
      trimmed_src.insert(src, 0, 0, src_x, src_y, src_width, src_height)
      data = texture.try_alloc(self, @queues, texture, trimmed_src, 0)
    end

    if data == nil
        raise "Internal texture block allocation error"
    end
    return data
  end

  #Too large to fit on a single texture.
  #TODO LargeImageData not implemented yet
  if (src_width > max_size - 2 || src_height > max_size - 2)
    bmp = Bitmap.new(src_width, src_height)
    bmp.insert(src, 0, 0, src_x, src_y, src_width, src_height)
    lidi = LargeImageData.new(self, bmp, max_size - 2, max_size - 2, border_flags)
    return lidi
  end

  bmp = Bitmap.new
  Gosu::apply_border_flags(bmp, src, src_x, src_y, src_width, src_height, border_flags)

  #Try to put the bitmap into one of the already allocated textures.
  @textures.each do |tex|
    data = tex.try_alloc(self, @queues, tex, bmp, 1)
      return data if data != nil
  end

  #All textures are full: Create a new one.
  texture = Texture.new(max_size, @gl)
  @textures.push texture

  data = texture.try_alloc(self, @queues, texture, bmp, 1)
  if data == nil
    raise "Internal texture block allocation error"
  end
  data
end

#draw_line(x1, y1, c1, x2, y2, c2, z, mode) ⇒ Object

Draws a line from one po to another (last pixel exclusive). Note: OpenGL lines are not reliable at all and may have a missing pixel at the start or end po. Please only use this for debugging purposes. Otherwise, use a quad or image to simulate lines, or contribute a better drawLine to Gosu.



101
102
103
104
105
106
107
108
109
# File 'lib/gosu_android/graphics/graphics.rb', line 101

def draw_line( x1,  y1,  c1, x2,  y2,  c2, z,  mode)
  op = @queues.op_pool.newDrawOp
  op.render_state.mode = mode
  op.vertices_or_block_index = 2
  op.vertices[0].set(x1, y1, c1)
  op.vertices[1].set(x2, y2, c2)
  op.z = z
  @queues.schedule_draw_op op
end

#draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gosu_android/graphics/graphics.rb', line 122

def draw_quad( x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode)
  op = @queues.op_pool.newDrawOp
  op.render_state.mode = mode
  op.vertices_or_block_index = 4
  op.vertices[0].set(x1, y1, c1)
  op.vertices[1].set(x2, y2, c2)
  op.vertices[2].set(x3, y3, c3)
  op.vertices[3].set(x4, y4, c4)
  op.z = z
  @queues.schedule_draw_op op
end

#draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, mode) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/gosu_android/graphics/graphics.rb', line 111

def draw_triangle( x1,  y1,  c1, x2,  y2,  c2, x3,  y3,  c3, z,  mode)
  op = @queues.op_pool.newDrawOp
  op.render_state.mode = mode
  op.vertices_or_block_index = 3
  op.vertices[0].set(x1, y1, c1)
  op.vertices[1].set(x2, y2, c2)
  op.vertices[2].set(x3, y3, c3)
  op.z = z
  @queues.schedule_draw_op op
end

#endObject

Every call to begin must have a matching call to end.



54
55
56
57
# File 'lib/gosu_android/graphics/graphics.rb', line 54

def end
  flush
  @gl.glFlush
end

#end_clippingObject

Disables clipping.



81
# File 'lib/gosu_android/graphics/graphics.rb', line 81

def end_clipping; end

#end_recording(width, height) ⇒ Object

Finishes building the macro and returns it as a drawable object. The width and height affect nothing about the recording process, the resulting macro will simply return these values when you ask it. Most usually, the return value is passed to Image::Image().



90
# File 'lib/gosu_android/graphics/graphics.rb', line 90

def end_recording(width,  height); end

#endGLObject

Resets Gosu o its default rendering state.



69
# File 'lib/gosu_android/graphics/graphics.rb', line 69

def endGL; end

#flushObject

Flushes the Z queue to the screen and starts a new one. Useful for games that are very composite in nature (splitscreen).



60
61
62
63
# File 'lib/gosu_android/graphics/graphics.rb', line 60

def flush
  @queues.perform_draw_ops_and_code
  @queues.clear_queue
end

#initialize_window(physical_width, physical_height, fullscreen, window) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gosu_android/graphics/graphics.rb', line 24

def initialize_window(physical_width, physical_height, fullscreen, window)
  @window = window
  @virt_width = physical_height
  @virt_height = physical_width

  @phys_width = physical_width
  @phys_height = physical_height

  @fullscreen = fullscreen
  #Gl stuff moved to render

  @queues = DrawOpQueue.new(@gl)
  @textures = []
end

#onDrawFrame(gl) ⇒ Object



192
193
194
195
196
197
# File 'lib/gosu_android/graphics/graphics.rb', line 192

def onDrawFrame(gl)
  #gl.glClear(JavaImports::GL10::GL_COLOR_BUFFER_BIT | JavaImports::GL10::GL_DEPTH_BUFFER_BIT)
  @window.do_show
  rescue Exception => e
    puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
end

#onSurfaceChanged(gl, width, height) ⇒ Object



199
200
201
202
# File 'lib/gosu_android/graphics/graphics.rb', line 199

def onSurfaceChanged(gl, width, height)
  @gl = gl
  @gl.glViewport(0, 0, width, height)
end

#onSurfaceCreated(gl, config) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/gosu_android/graphics/graphics.rb', line 204

def onSurfaceCreated(gl, config)
  @gl = gl
  #@queues.gl = @gl
  @android_initializer.on_ready
  #Options to improve performance
  @gl.glDisable(JavaImports::GL10::GL_DITHER)
  @gl.glHint(JavaImports::GL10::GL_PERSPECTIVE_CORRECTION_HINT, JavaImports::GL10::GL_FASTEST)

  @gl.glMatrixMode(JavaImports::GL10::GL_PROJECTION)
  @gl.glLoadIdentity
  @gl.glViewport(0, 0, @window.width, @window.height)

  @gl.glOrthof(0, @window.width, @window.height, 0, -1, 1)


  @gl.glMatrixMode(JavaImports::GL10::GL_MODELVIEW)
  @gl.glLoadIdentity

  @gl.glEnable(JavaImports::GL10::GL_BLEND)
  rescue Exception => e
    puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
end

#pop_transformObject

Pops one transformation from the transformation stack.



95
# File 'lib/gosu_android/graphics/graphics.rb', line 95

def pop_transform; end

#push_transform(transform) ⇒ Object

Pushes one transformation onto the transformation stack.



93
# File 'lib/gosu_android/graphics/graphics.rb', line 93

def push_transform(transform); end

#scheduleGL(functor, z) ⇒ Object

(Experimental) Schedules a custom GL functor to be executed at a certain Z level. The functor is called in a clean GL context (as given by beginGL/endGL). Gosu’s rendering up to the Z level may not yet have been glFlush()ed. Note: You may not call any Gosu rendering functions from within the functor, and you must schedule it from within Window::draw’s call tree.



76
# File 'lib/gosu_android/graphics/graphics.rb', line 76

def scheduleGL(functor, z); end

#set_resolution(virtualWidth, virtualHeight) ⇒ Object



39
# File 'lib/gosu_android/graphics/graphics.rb', line 39

def set_resolution(virtualWidth, virtualHeight); end