import a.fs import a.time import a.math import a.ggez import a.ggez.graphics as gg import a.ggez.audio as ga # print(gg.new_text('some text')) SCALE = 32 ggez.init() lines_above = [] curline = '' lines_below = [] start_time = time.now() impath = fs.join(fs.dirname(__file), 'resources', 'oh.png') mp3path = fs.join(fs.dirname(__file), 'resources', 'sample.mp3') gg.set_window_title('some app') # sound_bytes = fs.read(mp3path, encoding=nil) # sound_data = ga.SoundData.from_bytes(sound_bytes) samples = [] amp = 2 ** 14 for t in range(44100) { t = t / 44100 sample = math.sin(t * 440 * math.tau) samples.push(int(amp * sample)) } sound_data = ga.SoundData.from_samples(samples) sound = ga.Source.from_data(sound_data) sound.set_repeat(true) print('volume = %r' % [sound.volume()]) print('repeat = %r' % [sound.repeat()]) sound.play() width = gg.width() height = gg.height() W34 = width * 3 / 4 H34 = height * 3 / 4 pos = [W34, H34] imgbytes = fs.read(impath, encoding=nil) img = gg.Image.from_bytes(imgbytes) imw = img.width() imh = img.height() sb = gg.SpriteBatch(img) for i in range(0.5, 4.5) { for j in range(0.5, 4.5) { sb.add( x = imw * i, y = imh * j, xoffset = 0.5, yoffset = 0.5, rotation = i + j, ) } } mesh = gg.MeshBuilder().ellipse( mode='fill', point=[width / 4, height * 3 / 4], radius1 = 60, radius2 = 40, color=[0, 1, 0], ).circle( mode='stroke', point=[width / 2, height / 2], radius=40, color=[1, 1, 1], ).line( [[0, 0], [width / 2, height / 2], [width, 0]], color=[1, 0, 0], ).rectangle( [[width * 3 / 4, height * 3 / 4], [width, height]], color=[0, 0, 1], ).triangles( [ [width, height], [width * 7 / 8, height * 7 / 8], [width, height * 7 / 8], ], color=[0, 0.1, 0.1], ).build() pmesh = gg.MeshBuilder().polygon( [ [0, 0], [20, 0], [25, 10], [20, 20], [0, 20], ], ).build() ggez.run( update = nil, draw = def { dt = time.now() - start_time width = gg.width() height = gg.height() gg.clear([1/2, 1/2, 1]) gg.draw(mesh) gg.draw(pmesh, x = pos[0], y = pos[1]) gg.draw(sb) gg.draw( img, x = width * 3 / 4, y = height / 2, rotation = dt, xoffset = 0.5, yoffset = 0.5, ) gg.draw(img, x = width * 3 / 4, y = height / 4) y = 0 for text in lines_above { gg.queue_text(text, y=y) y += text.height() } text = gg.Text([ 'text': '%s@' % [curline], 'scale': SCALE, ]) gg.queue_text(text, y=y) y += text.height() for text in lines_below { gg.queue_text(text, y=y) y += text.height() } gg.draw_queued_text() }, text_input = def(ch) { nonlocal curline print('text-input %r' % [ch]) if ch == '\u{7F}' { if curline { curline = curline[:-1] } elif lines_above { text = lines_above.pop() curline = text.contents() } } elif ch == '\n' { lines_above.push([ 'text': curline, 'scale': SCALE, ]) lines_above.push(curline) } else { curline = curline + ch } }, mouse_down = def(x, y, btn) { nonlocal pos print('mouse-down %r' % [[x, y, btn]]) pos = [x, y] }, mouse_wheel = def(x, y) { print('mouse-wheel %r' % [[x, y]]) }, resize = def(width, height) { print('resize %r' % [[width, height]]) }, )