Class: OpenGL::Buffer

Inherits:
Object
  • Object
show all
Defined in:
ext/opengl/gl_buffer.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.map(_target, _access) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'ext/opengl/gl_buffer.c', line 32

VALUE
rb_gl_buffer_s_map(VALUE klass, VALUE _target, VALUE _access) {
	struct buffer *buf = ALLOC(struct buffer);
	LOAD_GL_FUNC(glMapBuffer, "1.5");

	buf->target = RUBY2GLENUM(_target);
	buf->len		= 0;

	buf->ptr = fptr_glMapBuffer(buf->target, RUBY2GLENUM(_access));

	if (buf->ptr == NULL) {
		xfree(buf);
		CHECK_GLERROR_FROM("glMapBuffer");
	}

	return TypedData_Wrap_Struct(klass, &buffer_type, buf);
}

Instance Method Details

#addrObject



50
51
52
53
54
55
56
57
# File 'ext/opengl/gl_buffer.c', line 50

static VALUE
rb_gl_buffer_addr(VALUE self) {
	struct buffer *buf;

	TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);

	return SIZET2NUM((size_t)buf->ptr);
}

#lengthObject



59
60
61
62
63
64
65
66
# File 'ext/opengl/gl_buffer.c', line 59

static VALUE
rb_gl_buffer_length(VALUE self) {
	struct buffer *buf;

	TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);

	return RETCONV_GLsizeiptr(buf->len);
}

#read(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/opengl/gl_buffer.c', line 68

static VALUE
rb_gl_buffer_read(int argc, VALUE *argv, VALUE self) {
	struct buffer *buf;
	VALUE _length, _offset;
	GLsizeiptr offset, length;

	TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);

	rb_scan_args(argc, argv, "02", &_length, &_offset);

	if (buf->len == 0 && NIL_P(_length))
		rb_raise(rb_eArgError, "length must be provided for unbounded buffer");

	length = NUM2SIZET(_length);

	if (NIL_P(_offset)) {
		offset = 0;
	} else {
		offset = NUM2SIZET(_offset);
	}

	if (buf->len != 0 && length + offset > buf->len)
		rb_raise(rb_eArgError, "read to %lu past end of buffer %lu",
				(unsigned long)(length + offset), (unsigned long)buf->len);

	return rb_str_new((char *)buf->ptr + offset, length);
}

#targetObject



96
97
98
99
100
101
102
103
# File 'ext/opengl/gl_buffer.c', line 96

static VALUE
rb_gl_buffer_target(VALUE self) {
	struct buffer *buf;

	TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);

	return RETCONV_GLenum(buf->target);
}

#unmapObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/opengl/gl_buffer.c', line 105

static VALUE
rb_gl_buffer_unmap(VALUE self) {
	struct buffer *buf;
	LOAD_GL_FUNC(glUnmapBuffer, "1.5");

	TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);

	if (!buf->ptr)
		return self;

	fptr_glUnmapBuffer(buf->target);

	CHECK_GLERROR_FROM("glUnmapBuffer");

	buf->ptr = NULL;
	buf->len = 0;
	buf->target = 0;

	return self;
}

#write(*args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/opengl/gl_buffer.c', line 126

static VALUE
rb_gl_buffer_write(int argc, VALUE *argv, VALUE self) {
	struct buffer *buf;
	VALUE _data, _offset;
	GLsizeiptr offset;
	long length;

	TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);

	if (!buf->ptr)
		rb_raise(rb_eArgError, "write to unmapped buffer");

	rb_scan_args(argc, argv, "11", &_data, &_offset);

	if (NIL_P(_data))
		rb_raise(rb_eArgError, "cannot write nil to buffer");

	_data = rb_String(_data);

	length = RSTRING_LEN(_data);

	if (NIL_P(_offset)) {
		offset = 0;
	} else {
		offset = NUM2SIZET(_offset);
	}

	if (buf->len != 0 && length + offset > buf->len)
		rb_raise(rb_eArgError, "write to %lu past end of buffer %lu",
				(unsigned long)(length + offset), (unsigned long)buf->len);

	memcpy((char *)buf->ptr + offset, RSTRING_PTR(_data), RSTRING_LEN(_data));

	return self;
}