Addons updated to new doc format
[io.git] / addons / OpenGL / source / IoGLScissor.c
blobe03ef11e12b100b7035e8c2a742416ab7fde7e63
2 //metadoc GLScissor copyright Steve Dekorte 2002
3 //metadoc GLScissor license BSD revised
4 //metadoc GLScissor category Graphics
6 #include "IoGLScissor.h"
7 #include "IoState.h"
8 #include "IoBox_gl.h"
10 #define DATA(self) ((IoGLScissorData *)IoObject_dataPointer(self))
12 IoTag *IoGLScissor_newTag(void *state)
14 IoTag *tag = IoTag_newWithName_("GLScissor");
15 IoTag_state_(tag, state);
16 IoTag_markFunc_(tag, (IoTagMarkFunc *)IoGLScissor_mark);
17 return tag;
20 IoGLScissor *IoGLScissor_proto(void *state)
22 IoObject *self = IoObject_new(state);
23 IoObject_tag_(self, IoGLScissor_newTag(state));
25 IoObject_setDataPointer_(self, calloc(1, sizeof(IoGLScissorData)));
27 DATA(self)->rect = IoBox_new(state);
28 DATA(self)->tmpRect = IoBox_new(state);
30 IoState_registerProtoWithFunc_(state, self, IoGLScissor_proto);
33 IoMethodTable methodTable[] = {
34 {"sync", IoGLScissor_sync},
35 {"set", IoGLScissor_set},
36 {"on", IoGLScissor_on},
37 {"off", IoGLScissor_off},
38 {"isOn", IoGLScissor_isOn},
39 {"push", IoGLScissor_push},
40 {"pop", IoGLScissor_pop},
41 {"isVisible", IoGLScissor_isVisible},
43 {"rect", IoGLScissor_rect},
44 {"setRect", IoGLScissor_setScreenRect},
45 {"setViewRect", IoGLScissor_setViewRect},
46 {"unionWithViewRect", IoGLScissor_unionWithViewRect},
47 {"unionWithScreenRect", IoGLScissor_unionWithScreenRect},
48 {NULL, NULL},
50 IoObject_addMethodTable_(self, methodTable);
52 return self;
55 /* ----------------------------------------------------------- */
57 IoGLScissor *IoGLScissor_rawClone(IoGLScissor *proto)
59 IoObject *self = IoObject_rawClonePrimitive(proto);
60 IoObject_setDataPointer_(self, cpalloc(IoObject_dataPointer(proto), sizeof(IoGLScissorData)));
62 DATA(self)->rect = IOCLONE(DATA(proto)->rect);
63 DATA(self)->tmpRect = IOCLONE(DATA(proto)->tmpRect);
64 return self;
67 IoGLScissor *IoGLScissor_new(void *state)
69 IoGLScissor *proto = IoState_protoWithInitFunction_(state, IoGLScissor_proto);
70 return IOCLONE(proto);
73 void IoGLScissor_mark(IoGLScissor *self)
75 IoObject_shouldMark(DATA(self)->rect);
76 IoObject_shouldMark(DATA(self)->tmpRect);
79 /* ----------------------------------------------------------- */
81 IoObject *IoGLScissor_rect(IoGLScissor *self, IoObject *locals, IoMessage *m)
82 { return DATA(self)->rect; }
84 IoObject *IoGLScissor_setScreenRect(IoGLScissor *self, IoObject *locals, IoMessage *m)
86 IoBox *box = IoMessage_locals_boxArgAt_(m, locals, 0);
87 IoBox_rawCopy(DATA(self)->rect, box);
88 IoGLScissor_set(self, locals, m);
89 return self;
92 IoObject *IoGLScissor_setViewRect(IoGLScissor *self, IoObject *locals, IoMessage *m)
94 IoBox *box = IoMessage_locals_boxArgAt_(m, locals, 0);
95 IoBox_rawCopy(DATA(self)->rect, box);
96 IoBox_glProject(DATA(self)->rect, locals, m);
97 IoGLScissor_set(self, locals, m);
98 return self;
101 void IoGLScissor_rawSync(IoGLScissor *self)
103 double v[4];
104 glGetDoublev(GL_SCISSOR_BOX, v);
105 IoBox_rawSet(DATA(self)->rect, v[0], v[1], 0, v[2], v[3], 0);
108 IoObject *IoGLScissor_sync(IoGLScissor *self, IoObject *locals, IoMessage *m)
110 IoGLScissor_rawSync(self);
111 return self;
114 IoObject *IoGLScissor_set(IoGLScissor *self, IoObject *locals, IoMessage *m)
116 vec2f o = IoSeq_vec2f(IoBox_rawOrigin(DATA(self)->rect));
117 vec2f s = IoSeq_vec2f(IoBox_rawSize(DATA(self)->rect));
118 glScissor((GLint)o.x, (GLint)o.y, (GLsizei)s.x, (GLsizei)s.y);
119 return self;
122 IoObject *IoGLScissor_on(IoGLScissor *self, IoObject *locals, IoMessage *m)
124 glEnable(GL_SCISSOR_TEST);
125 return self;
128 IoObject *IoGLScissor_off(IoGLScissor *self, IoObject *locals, IoMessage *m)
130 glDisable(GL_SCISSOR_TEST);
131 return self;
134 IoObject *IoGLScissor_isOn(IoGLScissor *self, IoObject *locals, IoMessage *m)
136 GLboolean b;
137 glGetBooleanv(GL_SCISSOR_TEST, &b);
138 return IOBOOL(self, b == GL_TRUE);
141 IoObject *IoGLScissor_push(IoGLScissor *self, IoObject *locals, IoMessage *m)
143 glPushAttrib(GL_SCISSOR_BIT);
144 return self;
147 IoObject *IoGLScissor_pop(IoGLScissor *self, IoObject *locals, IoMessage *m)
149 glPopAttrib();
150 IoGLScissor_sync(self, locals, m);
151 return self;
154 IoObject *IoGLScissor_unionWithViewRect(IoGLScissor *self, IoObject *locals, IoMessage *m)
156 IoBox *box = IoMessage_locals_boxArgAt_(m, locals, 0);
157 IoBox_rawCopy(DATA(self)->tmpRect, box);
158 IoBox_glProject(DATA(self)->tmpRect, locals, m);
159 IoBox_rawUnion(DATA(self)->rect, DATA(self)->tmpRect);
160 IoGLScissor_set(self, locals, m);
161 return self;
164 IoObject *IoGLScissor_unionWithScreenRect(IoGLScissor *self, IoObject *locals, IoMessage *m)
166 IoBox *box = IoMessage_locals_boxArgAt_(m, locals, 0);
167 IoBox_rawUnion(DATA(self)->rect, box);
168 IoGLScissor_set(self, locals, m);
169 return self;
172 IoObject *IoGLScissor_isVisible(IoGLScissor *self, IoObject *locals, IoMessage *m)
174 return IOBOOL(self, UArray_isZero(IoSeq_rawUArray(IoBox_rawSize(DATA(self)->rect))));