Debian Codegraph @

dokidoki/graphics.lua

raw symbols SCIP
1 require "dokidoki.module"
2 [[ draw_text,
3 sprite_from_image, make_sprite,
4 texture_from_image, texture_from_string,
5 get_texture_count ]]
6
7 local stb_image = require "stb_image"
8
9 import(require "gl")
10 import(require "glu")
11 require 'memarray'
12
13 local will = require "dokidoki.private.will"
14
15 ---- Fonts and Text -----------------------------------------------------------
16
17 function font_map_line_height (font_map)
18 local _, some_glyph = next(font_map)
19 return some_glyph.size[2]
20 end
21
22 function draw_text(font_map, text)
23 local line_height = font_map_line_height (font_map)
24
25 local current_line = 0
26
27 glPushMatrix()
28 for i = 1, #text do
29 local char = text:sub(i, i)
30 if char == "\n" then
31 glPopMatrix()
32 current_line = current_line + 1
33 glPushMatrix()
34 glTranslated(0, current_line * -line_height, 0)
35 else
36 local sprite = font_map[char]
37 if sprite then
38 sprite:draw()
39 glTranslated(sprite.size[1], 0, 0)
40 else
41 error("Tried to render an unavailable character code " .. text:byte(i))
42 end
43 end
44 end
45 glPopMatrix()
46 end
47
48 ---- Sprites ------------------------------------------------------------------
49
50 do
51 local index =
52 {
53 draw = function (self)
54 local t = self.tex_rect
55 local o = self.origin
56 local s = self.size
57
58 self.tex:enable()
59 glBegin(GL_QUADS)
60 glTexCoord2d(t[1], t[2] + t[4])
61 glVertex2d(-o[1], -o[2])
62
63 glTexCoord2d(t[1] + t[3], t[2] + t[4])
64 glVertex2d(-o[1] + s[1], -o[2])
65
66 glTexCoord2d(t[1] + t[3], t[2])
67 glVertex2d(-o[1] + s[1], -o[2] + s[2])
68
69 glTexCoord2d(t[1], t[2])
70 glVertex2d(-o[1], -o[2] + s[2])
71 glEnd()
72 self.tex:disable()
73 end
74 }
75
76 local mt = {__index = index}
77
78 function make_sprite (tex, size, origin, tex_rect)
79 assert(tex)
80 assert(size)
81
82 if origin == "center" then
83 origin = {size[1]/2, size[2]/2}
84 end
85
86 return setmetatable(
87 {
88 tex = tex,
89 size = size,
90 origin = origin or {0, 0},
91 tex_rect = tex_rect or {0, 0, 1, 1}
92 },
93 mt)
94 end
95 end
96
97 function sprite_from_image (filename, size, origin)
98 -- Creates a sprite object from the image file given by filename.
99 --
100 -- See notes about sprite_from_image_string for size/origin info.
101 local image_string, width, height, channels =
102 assert(stb_image.load(filename))
103 return sprite_from_image_string(
104 image_string, width, height, channels, size, origin)
105 end
106
107 function sprite_from_image_string (image_string, width, height, channels, size,
108 origin)
109 -- Creates a sprite object from the given surface.
110 --
111 -- The size is the rendered size of the sprite, given as {x, y}. Its default
112 -- value is the pixel size of the surface. origin specifies the point on the
113 -- sprite which should be rendered at the origin when the sprite is drawn,
114 -- and it's in the same units as the size. origin can either be {x, y} or
115 -- "center", which sets it to half the size. origin's default value is
116 -- {0, 0}.
117
118 local tex, tex_w, tex_h =
119 texture_from_string(image_string, width, height, channels)
120
121 local size = size or {width, height}
122 local origin = origin or {0, 0}
123 local tex_rect = {0, 0, width/tex_w, height/tex_h}
124
125 return make_sprite(tex, size, origin, tex_rect)
126 end
127
128 ---- Textures -----------------------------------------------------------------
129
130 -- Stop it from garbage collecting the currently bound texture
131 bound_texture = false
132
133 -- Texture object
134 do
135 local texture_count = 0
136
137 local index =
138 {
139 enable = function (self)
140 -- Enables 2D texturing and binds the texture.
141 assert(self.name)
142 bound_texture = self
143 glEnable(GL_TEXTURE_2D)
144 glBindTexture(GL_TEXTURE_2D, self.name)
145 end,
146 disable = function (self)
147 -- Disables 2D texturing and unbinds the texture.
148 assert(self.name)
149 glBindTexture(GL_TEXTURE_2D, 0)
150 glDisable(GL_TEXTURE_2D)
151 bound_texture = false
152 end,
153 delete = function (self)
154 -- Deletes the texture from video memory.
155 --
156 -- This invalidates the object, so don't use it anymore.
157 if self.name then
158 if bound_texture == self then self:disable() end
159 delete_texture_name(self.name)
160 texture_count = texture_count - 1
161 self.name = false
162 end
163 end
164 }
165
166 local mt = {__index = index}
167
168 function make_texture (name)
169 -- Makes a new texture object with the given OpenGL texture name.
170 --
171 -- The texture object is deleted automatically by the garbage collector,
172 -- but it is recommended that you delete it manually with :delete() in
173 -- order to free up video memory sooner.
174 local tex = setmetatable({name = name}, mt)
175 will.attach_will(tex, function () tex:delete() end)
176 texture_count = texture_count + 1
177 return tex
178 end
179
180 function get_texture_count()
181 return texture_count
182 end
183 end
184
185 function texture_from_image (filename)
186 -- Creates an OpenGL texture from the image file given by filename.
187 --
188 -- Returns the created texture object, the final width, and the final height.
189 -- See the note on texture_from_surface for width/height issues. Since
190 -- there's no way to determine the original image's size, this function is
191 -- only useful if you know its size beforehand, or at least know that it's a
192 -- power of two.
193 return texture_from_string(assert(stb_image.load(filename)))
194 end
195
196 function image_string_to_powers_of_two(image_string, width, height, channels)
197 assert(#image_string == width * height * channels,
198 "image_string length doesn't match")
199 assert(1 <= channels and channels <= 4, "invalid channels given")
200
201 local new_width = to_power_of_two(width)
202 local new_height = to_power_of_two(height)
203
204 if new_width == width and new_height == height then
205 return image_string, width, height, channels
206 else
207 local new_image = {}
208
209 -- fill right
210 for y = 1, height do
211 local begin = (y-1) * width * channels + 1
212 local ending = y * width * channels
213 local fill = image_string:sub(ending + 1 - channels, ending)
214
215 table.insert(new_image, image_string:sub(begin, ending))
216 table.insert(new_image, string.rep(fill, new_width - width))
217 end
218
219 -- fill bottom
220 local last_row = new_image[#new_image - 1] .. new_image[#new_image]
221 for y = height+1, new_height do
222 table.insert(new_image, last_row)
223 end
224
225 return table.concat(new_image), new_width, new_height, channels
226 end
227 end
228
229 function texture_from_string (image_string, width, height, channels)
230 -- Creates an OpenGL texture from the data in image_string.
231 --
232 -- If width and height are not powers of two, the texture will be padded out
233 -- to the nearest greater power of two. The bytes in image_string are treated
234 -- as unsigned byte values. channels must be 1, 2, 3, or 4 for V, VA, RGB,
235 -- and RGBA respectively. #image_string should be width*height*channels. The
236 -- generated texture, the final width, and the final height are returned.
237
238 assert(#image_string == width * height * channels,
239 "image_string length doesn't match")
240 assert(1 <= channels and channels <= 4, "invalid channels given")
241
242 if not (is_power_of_two(width) and is_power_of_two(height)) then
243 image_string, width, height, channels =
244 image_string_to_powers_of_two(image_string, width, height, channels)
245 end
246
247 local tex = texture_from_pointer(image_string, width, height, channels)
248
249 return tex, width, height
250 end
251
252 function texture_from_pointer(pointer, width, height, channels)
253 -- Creates an OpenGL texture from the data pointed to by pointer.
254 --
255 -- width and height must be powers of two. channels must be 1, 2, 3, or 4 for
256 -- V, VA, RGB, and RGBA respectively. pointer should point to a native array
257 -- of width * height * channels unsigned bytes.
258 assert(is_power_of_two(width) and is_power_of_two(height),
259 "non-power-of-two dimensions given")
260 local format = channels == 1 and GL_LUMINANCE or
261 channels == 2 and GL_LUMINANCE_ALPHA or
262 channels == 3 and GL_RGB or
263 channels == 4 and GL_RGBA
264 assert(format, "invalid channels given")
265
266 local name = new_texture_name()
267 glBindTexture(GL_TEXTURE_2D, name)
268 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
269 glTexParameterf(
270 -- GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
271 GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
272 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
273 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
274 gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format,
275 GL_UNSIGNED_BYTE, pointer)
276 glBindTexture(GL_TEXTURE_2D, 0)
277 return make_texture(name)
278 end
279
280 ---- Utilities ----------------------------------------------------------------
281
282 function new_texture_name ()
283 local namebuffer = memarray('GLint', 1)
284 glGenTextures(1, namebuffer:ptr())
285 return namebuffer[0]
286 end
287
288 function delete_texture_name (name)
289 local namebuffer = memarray("GLint", 1)
290 namebuffer[0] = name
291 glDeleteTextures(1, namebuffer:ptr())
292 end
293
294 function is_power_of_two (x)
295 return x == to_power_of_two(x)
296 end
297
298 function to_power_of_two (x)
299 return 2 ^ math.ceil(math.log(x) / math.log(2))
300 end
301
302 return get_module_exports()