Handles to reused GL buffers and texture

Introduced properties

private var _buffer_array: Int

gamnit :: DynamicContext :: _buffer_array

Buffer name for vertex data
private var _depth_renderbuffer: Int

gamnit :: DynamicContext :: _depth_renderbuffer

Depth renderbuffer attached to dynamic_framebuffer
private var _dynamic_framebuffer: Int

gamnit :: DynamicContext :: _dynamic_framebuffer

Dynamic screen framebuffer
private var _texture: Int

gamnit :: DynamicContext :: _texture

Texture attached to dynamic_framebuffer as color attachment
private fun buffer_array: Int

gamnit :: DynamicContext :: buffer_array

Buffer name for vertex data
private fun buffer_array=(buffer_array: Int)

gamnit :: DynamicContext :: buffer_array=

Buffer name for vertex data
private fun depth_renderbuffer: Int

gamnit :: DynamicContext :: depth_renderbuffer

Depth renderbuffer attached to dynamic_framebuffer
private fun depth_renderbuffer=(depth_renderbuffer: Int)

gamnit :: DynamicContext :: depth_renderbuffer=

Depth renderbuffer attached to dynamic_framebuffer
private fun destroy

gamnit :: DynamicContext :: destroy

private fun destroyed=(destroyed: Bool)

gamnit :: DynamicContext :: destroyed=

private fun dynamic_framebuffer: Int

gamnit :: DynamicContext :: dynamic_framebuffer

Dynamic screen framebuffer
private fun dynamic_framebuffer=(dynamic_framebuffer: Int)

gamnit :: DynamicContext :: dynamic_framebuffer=

Dynamic screen framebuffer
private fun prepare_once(display: GamnitDisplay, max_dynamic_resolution_ratio: Float)

gamnit :: DynamicContext :: prepare_once

Prepare all attributes once per resolution change
private fun resize(display: GamnitDisplay, max_dynamic_resolution_ratio: Float)

gamnit :: DynamicContext :: resize

Init size or resize depth_renderbuffer and texture
private fun texture: Int

gamnit :: DynamicContext :: texture

Texture attached to dynamic_framebuffer as color attachment
private fun texture=(texture: Int)

gamnit :: DynamicContext :: texture=

Texture attached to dynamic_framebuffer as color attachment

Redefined properties

redef type SELF: DynamicContext

gamnit $ DynamicContext :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _buffer_array: Int

gamnit :: DynamicContext :: _buffer_array

Buffer name for vertex data
private var _depth_renderbuffer: Int

gamnit :: DynamicContext :: _depth_renderbuffer

Depth renderbuffer attached to dynamic_framebuffer
private var _dynamic_framebuffer: Int

gamnit :: DynamicContext :: _dynamic_framebuffer

Dynamic screen framebuffer
private var _texture: Int

gamnit :: DynamicContext :: _texture

Texture attached to dynamic_framebuffer as color attachment
private fun buffer_array: Int

gamnit :: DynamicContext :: buffer_array

Buffer name for vertex data
private fun buffer_array=(buffer_array: Int)

gamnit :: DynamicContext :: buffer_array=

Buffer name for vertex data
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
private fun depth_renderbuffer: Int

gamnit :: DynamicContext :: depth_renderbuffer

Depth renderbuffer attached to dynamic_framebuffer
private fun depth_renderbuffer=(depth_renderbuffer: Int)

gamnit :: DynamicContext :: depth_renderbuffer=

Depth renderbuffer attached to dynamic_framebuffer
private fun destroy

gamnit :: DynamicContext :: destroy

private fun destroyed=(destroyed: Bool)

gamnit :: DynamicContext :: destroyed=

private fun dynamic_framebuffer: Int

gamnit :: DynamicContext :: dynamic_framebuffer

Dynamic screen framebuffer
private fun dynamic_framebuffer=(dynamic_framebuffer: Int)

gamnit :: DynamicContext :: dynamic_framebuffer=

Dynamic screen framebuffer
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
private fun prepare_once(display: GamnitDisplay, max_dynamic_resolution_ratio: Float)

gamnit :: DynamicContext :: prepare_once

Prepare all attributes once per resolution change
private fun resize(display: GamnitDisplay, max_dynamic_resolution_ratio: Float)

gamnit :: DynamicContext :: resize

Init size or resize depth_renderbuffer and texture
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
private fun texture: Int

gamnit :: DynamicContext :: texture

Texture attached to dynamic_framebuffer as color attachment
private fun texture=(texture: Int)

gamnit :: DynamicContext :: texture=

Texture attached to dynamic_framebuffer as color attachment
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram gamnit::dynamic_resolution::DynamicContext DynamicContext core::Object Object gamnit::dynamic_resolution::DynamicContext->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

gamnit $ DynamicContext
# Handles to reused GL buffers and texture
private class DynamicContext

	# Dynamic screen framebuffer
	var dynamic_framebuffer: Int = -1

	# Depth renderbuffer attached to `dynamic_framebuffer`
	var depth_renderbuffer: Int = -1

	# Texture attached to `dynamic_framebuffer` as color attachment
	var texture: Int = -1

	# Buffer name for vertex data
	var buffer_array: Int = -1

	# Prepare all attributes once per resolution change
	fun prepare_once(display: GamnitDisplay, max_dynamic_resolution_ratio: Float)
	do
		# TODO enable antialiasing.

		# Framebuffer
		var framebuffer = glGenFramebuffers(1).first
		glBindFramebuffer(gl_FRAMEBUFFER, framebuffer)
		assert glIsFramebuffer(framebuffer)
		self.dynamic_framebuffer = framebuffer
		assert glGetError == gl_NO_ERROR

		# Depth & texture/color
		var depthbuffer = glGenRenderbuffers(1).first
		self.depth_renderbuffer = depthbuffer
		var texture = glGenTextures(1).first
		self.texture = texture
		assert glGetError == gl_NO_ERROR

		resize(display, max_dynamic_resolution_ratio)
		assert glCheckFramebufferStatus(gl_FRAMEBUFFER) == gl_FRAMEBUFFER_COMPLETE

		# Array buffer
		buffer_array = glGenBuffers(1).first
		glBindBuffer(gl_ARRAY_BUFFER, buffer_array)
		assert glIsBuffer(buffer_array)
		assert glGetError == gl_NO_ERROR

		## coord
		var data = new Array[Float]
		data.add_all([-1.0, -1.0, 0.0,
	                   1.0, -1.0, 0.0,
	                  -1.0,  1.0, 0.0,
	                   1.0,  1.0, 0.0])
		## tex_coord
		data.add_all([0.0, 0.0,
		              1.0, 0.0,
		              0.0, 1.0,
		              1.0, 1.0])
		var c_data = new GLfloatArray.from(data)
		glBufferData(gl_ARRAY_BUFFER, data.length*4, c_data.native_array, gl_STATIC_DRAW)

		glBindBuffer(gl_ARRAY_BUFFER, 0)

		assert glGetError == gl_NO_ERROR
	end

	# Init size or resize `depth_renderbuffer` and `texture`
	fun resize(display: GamnitDisplay, max_dynamic_resolution_ratio: Float)
	do
		var width = (display.width.to_f * max_dynamic_resolution_ratio).to_i
		var height = (display.height.to_f * max_dynamic_resolution_ratio).to_i

		glBindFramebuffer(gl_FRAMEBUFFER, dynamic_framebuffer)

		var depthbuffer = self.depth_renderbuffer
		var texture = self.texture

		# Depth
		glBindRenderbuffer(gl_RENDERBUFFER, depthbuffer)
		assert glIsRenderbuffer(depthbuffer)
		glRenderbufferStorage(gl_RENDERBUFFER, gl_DEPTH_COMPONENT16, width, height)
		glFramebufferRenderbuffer(gl_FRAMEBUFFER, gl_DEPTH_ATTACHMENT, gl_RENDERBUFFER, depthbuffer)
		assert glGetError == gl_NO_ERROR

		# Texture
		glBindTexture(gl_TEXTURE_2D, texture)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_LINEAR)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_LINEAR)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_S, gl_CLAMP_TO_EDGE)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_T, gl_CLAMP_TO_EDGE)
		glTexImage2D(gl_TEXTURE_2D, 0, gl_RGB, width, height,
		             0, gl_RGB, gl_UNSIGNED_BYTE, new Pointer.nul)
		glFramebufferTexture2D(gl_FRAMEBUFFER, gl_COLOR_ATTACHMENT0, gl_TEXTURE_2D, texture, 0)

		assert glGetError == gl_NO_ERROR

		# Take down
		glBindRenderbuffer(gl_RENDERBUFFER, 0)
		glBindFramebuffer(gl_FRAMEBUFFER, 0)

		assert glGetError == gl_NO_ERROR
	end

	var destroyed = false
	fun destroy
	do
		if destroyed then return
		destroyed = true

		# Free the buffer
		glDeleteBuffers([buffer_array])
		assert glGetError == gl_NO_ERROR
		buffer_array = -1

		# Free the dynamic framebuffer and its attachments
		glDeleteBuffers([buffer_array])
		glDeleteFramebuffers([dynamic_framebuffer])
		glDeleteRenderbuffers([depth_renderbuffer])
		glDeleteTextures([texture])
	end
end
lib/gamnit/dynamic_resolution.nit:187,1--303,3