libsoy
view src/widgets/Canvas.gs @ 756:d0b74f2ac128
added self to AUTHORS
| author | Alexander Light <scialexlight@gmail.com> |
|---|---|
| date | Wed, 25 Apr 2012 17:48:34 -0400 |
| parents | 7d81def3eaed |
| children |
line source
1 /*
2 * libsoy - soy.widgets.Canvas
3 * Copyright (C) 2006,2007,2008,2009,2010,2011,2012 Copyleft Games Group
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program; if not, see http://www.gnu.org/licenses
17 *
18 */
20 [indent=4]
21 uses
22 GLib
23 GL
24 soy.atoms
25 soy.textures
27 class soy.widgets.Canvas : soy.widgets.Widget
28 alignWidth : int
29 alignHeight : int
30 aspect : float
31 marginTop : int
32 marginLeft : int
33 marginRight : int
34 marginBottom : int
36 // These verts form a 1x1 square with 2 tris (A and B) :
37 // 3----2
38 // | B/ |
39 // | /A |
40 // 0----1
41 //
42 // px py tx ty
43 _verts : static array of GLfloat = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // 0,0
44 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // 1,0
45 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 1,1
46 0.0f, 1.0f, 0.0f, 0.0f, 1.0f} // 0,1
48 // face A face B
49 _faces : static array of GLushort = { 0, 1, 2, 2, 3, 0 }
51 vbuffer : GLint // Vertex Buffer
52 ibuffer : GLint // Index Buffer
54 construct (parent : soy.widgets.Container?,
55 texture : soy.textures.Texture?)
56 super(parent)
57 self._texture = texture
60 ////////////////////////////////////////////////////////////////////////
61 // Methods
63 def override render (x: int, y : int, width : int, height : int)
64 #if !WINDOWS
65 // don't bother rendering w/o a texture
66 if _texture == null
67 return
69 // Setup and Use VBOs
70 if vbuffer == 0 or ibuffer == 0
71 //
72 ///////////////////////////////////////
73 // The Vertex and Index buffers are not initialized
74 //
75 // Generate, Bind, and upload to the vertex buffer
76 glGenBuffers(1, &vbuffer);
77 glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
78 glBufferData(GL_ARRAY_BUFFER, (GL.GLsizei) sizeof(GLfloat) * 20, _verts,
79 GL_STATIC_DRAW);
81 // Generate, Bind, and upload to the index buffer
82 glGenBuffers(1, &ibuffer);
83 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuffer);
84 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
85 (GL.GLsizei) sizeof(GLushort) * 6, _faces, GL_STATIC_DRAW);
87 else
88 //
89 // Just use the buffers that have been previously created
90 glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
91 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuffer);
93 //
94 // Set the viewport to the current Widget position and size
95 //TODO: This needs to calculate the actual dimensions instead of using the
96 // size fractions passed to this function
97 glViewport((GLint) x, (GLint) y, (GLint) width, (GLint) height)
98 //
99 // Reset the projection and modelview matrix, set ortho rendering
100 glMatrixMode(GL_PROJECTION)
101 glLoadIdentity()
102 glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f)
103 glMatrixMode(GL_MODELVIEW)
104 glLoadIdentity()
106 glDisableClientState(GL_NORMAL_ARRAY) // We're not going to use Normals
108 self._texture.enable()
110 // Verticies and Texcoord are packed (20 stride)
111 glVertexPointer(3, GL_FLOAT, 20, (void *)0)
112 // Texcoords are found after the verticies (3 floats, 4 bytes each = 12 bytes offset)
113 glTexCoordPointer(2, GL_FLOAT, 20,(void *) 12)
114 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,(void *) 0)
116 self._texture.disable()
118 glEnableClientState(GL_NORMAL_ARRAY) // But remember to enable them for later...
120 glBindBuffer(GL_ARRAY_BUFFER, 0);
121 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
122 #else
123 print "Canvas: glext functions not currently supported on Windows"
124 #endif
127 ////////////////////////////////////////////////////////////////////////
128 // Properties
130 prop texture : soy.textures.Texture
