def test_nested(self): r = random.Random() r.seed(1356317227) root = buffer.Buffer(width=0, height=0) current = root all = [] i = 0 for sub in range(0, min(self.height, self.width), 4): h = self.height - sub w = self.width - sub i += 1 b = buffer.Box(width=w, height=h, border_fg=(i % 16), border_bg=(i + 1) % 8) current.children.append(b) all.append(b) current = b for i in range(30): root.draw() b = r.choice(all) for j in range(20): b.set_at( x=r.randint(0, b.width - 1), y=r.randint(0, b.height - 1), char=chr(r.randint(0, 255)), fg=int(r.triangular(0, 15)), bg=int(r.triangular(0, 7)), ) term.flip()
def draw_box(self, box): """ Draw a box, and check that it has drawn correctly """ box.draw() term.flip() #check the border is blitting if box.draw_left and box.draw_top: self.check(box.x, box.y, box.boxtype.tl, fg=box.border_fg, bg=box.border_bg) if box.draw_right and box.draw_top: self.check(box.x + box.width - 1, box.y, box.boxtype.tr, fg=box.border_fg, bg=box.border_bg) if box.draw_right and box.draw_bottom: self.check(box.x + box.width - 1, box.y + box.height - 1, box.boxtype.br, fg=box.border_fg, bg=box.border_bg) if box.draw_left and box.draw_bottom: self.check(box.x, box.y + box.height - 1, box.boxtype.bl, fg=box.border_fg, bg=box.border_bg) if box.draw_left and box.inner_height: self.check(box.x, box.y+1, box.boxtype.vert, fg=box.border_fg, bg=box.border_bg) if box.draw_top and box.inner_width: self.check(box.x+1, box.y, box.boxtype.horiz, fg=box.border_fg, bg=box.border_bg) if box.inner_width and box.inner_height: self.check(box.x+1, box.y+1, SPACE, fg=box.interior_fg, bg=box.interior_bg) self.check(box.x+box.width-1-1, box.y+box.height-1-1, SPACE, fg=box.interior_fg, bg=box.interior_bg)
def test_allcharacters(self): r = random.Random() r.seed(1356317227) def make_box(start_color): box = buffer.Buffer(width=self.width, height=self.height) i = 0 bg = start_color fg = 0 for x in range(self.width): for y in range(self.height): fg += 1 if fg == 16: fg = 0 bg += 1 if bg % 8 == 0: bg = 0 box.set_at(x=x, y=y, char=chr(i % 256), bg=bg, fg=fg) i += 1 return box boxes = [make_box(i) for i in range(16)] for i in range(30): r.choice(boxes).draw() term.flip()
def test_large_random_blit(self): #let's make this test random yet deterministic r = random.Random() r.seed(1356317227) def make_box(): return buffer.Buffer(width=self.width, height=self.height, data=[ [ [int(r.triangular(0, 15)), int(r.triangular(0, 7)), chr(r.randint(0, 255))] for x in range(self.width) ] for y in range(self.height) ]) box = make_box() for i in range(10): for i in range(80): box.set_at( x=r.randint(0, self.width-1), y=r.randint(0, self.height-1), char=chr(r.randint(0, 255)), fg=int(r.triangular(0, 15)), bg=int(r.triangular(0, 7)), ) box.draw() term.flip() for i in range(15): box = make_box() box.draw() term.flip()
def test_large_random_blit(self): #let's make this test random yet deterministic r = random.Random() r.seed(1356317227) def make_box(): return buffer.Buffer(width=self.width, height=self.height, data=[[[ int(r.triangular(0, 15)), int(r.triangular(0, 7)), chr(r.randint(0, 255)) ] for x in range(self.width)] for y in range(self.height)]) box = make_box() for i in range(10): for i in range(80): box.set_at( x=r.randint(0, self.width - 1), y=r.randint(0, self.height - 1), char=chr(r.randint(0, 255)), fg=int(r.triangular(0, 15)), bg=int(r.triangular(0, 7)), ) box.draw() term.flip() for i in range(15): box = make_box() box.draw() term.flip()
def test_oob_blit(self): nullbuff = buffer.Buffer(x=10, y=10, width=0, height=0) nullbuff.draw() whitebox = buffer.Box(x=10, y=10, width=5, height=5, interior_bg=colors.WHITE) redbox = buffer.Box(x=10, y=10, width=5, height=5, interior_bg=colors.RED) bluebox = buffer.Box(x=10, y=10, width=5, height=5, interior_bg=colors.BLUE) self.draw_box(whitebox) redbox.x = -1 redbox.y = -1 redbox.draw() term.flip() self.check(0, 0, bg=colors.RED) self.check(self.width - 1, self.height - 1, bg=colors.BLACK) bluebox.x = self.width - 2 bluebox.y = self.height - 2 bluebox.draw() term.flip() self.check(self.width - 2, self.height - 2, ch=boxtypes.BoxDouble.tl) self.check(self.width - 1, self.height - 1, bg=colors.BLUE)
def test_allcharacters(self): r = random.Random() r.seed(1356317227) def make_box(start_color): box = buffer.Buffer(width=self.width, height=self.height) i = 0 bg = start_color fg = 0 for x in range(self.width): for y in range(self.height): fg += 1 if fg == 16: fg = 0 bg += 1 if bg % 8 == 0: bg = 0 box.set_at( x=x, y=y, char=chr(i % 256), bg=bg, fg=fg ) i += 1 return box boxes= [make_box(i) for i in range(16)] for i in range(30): r.choice(boxes).draw() term.flip()
def test_nested(self): r = random.Random() r.seed(1356317227) root = buffer.Buffer(width=0, height=0) current = root all = [] i = 0 for sub in range(0, min(self.height, self.width), 4): h = self.height - sub w = self.width - sub i += 1 b = buffer.Box(width=w, height=h, border_fg=(i % 16), border_bg=(i + 1) % 8) current.children.append(b) all.append(b) current = b for i in range(30): root.draw() b = r.choice(all) for j in range(20): b.set_at( x=r.randint(0, b.width-1), y=r.randint(0, b.height-1), char=chr(r.randint(0, 255)), fg=int(r.triangular(0, 15)), bg=int(r.triangular(0, 7)), ) term.flip()
def add(self, msg, **kwargs): if isinstance(msg, list): for m in msg: self.box.add(m, **kwargs) else: self.box.add(msg, **kwargs) self.box.draw() term.flip()
def draw_box(self, box): """ Draw a box, and check that it has drawn correctly """ box.draw() term.flip() #check the border is blitting if box.draw_left and box.draw_top: self.check(box.x, box.y, box.boxtype.tl, fg=box.border_fg, bg=box.border_bg) if box.draw_right and box.draw_top: self.check(box.x + box.width - 1, box.y, box.boxtype.tr, fg=box.border_fg, bg=box.border_bg) if box.draw_right and box.draw_bottom: self.check(box.x + box.width - 1, box.y + box.height - 1, box.boxtype.br, fg=box.border_fg, bg=box.border_bg) if box.draw_left and box.draw_bottom: self.check(box.x, box.y + box.height - 1, box.boxtype.bl, fg=box.border_fg, bg=box.border_bg) if box.draw_left and box.inner_height: self.check(box.x, box.y + 1, box.boxtype.vert, fg=box.border_fg, bg=box.border_bg) if box.draw_top and box.inner_width: self.check(box.x + 1, box.y, box.boxtype.horiz, fg=box.border_fg, bg=box.border_bg) if box.inner_width and box.inner_height: self.check(box.x + 1, box.y + 1, SPACE, fg=box.interior_fg, bg=box.interior_bg) self.check(box.x + box.width - 1 - 1, box.y + box.height - 1 - 1, SPACE, fg=box.interior_fg, bg=box.interior_bg)
def test_set_at(self): box = buffer.Box(x=10, y=10, width=6, height=6) self.draw_box(box) log.debug(pprint.pformat(box._data)) box.set_at(2, 2, '0', colors.LIGHTGREY) log.debug(pprint.pformat(box._data)) box.draw() term.flip() self.check(12, 12, '0') self.check(13, 13, ' ') self.check(12, 13, ' ') self.check(13, 12, ' ')
def test_draw_text(self): msg = "abcdef" txt = buffer.PlainText(msg) for i in range(10): txt.x = i txt.y = i txt.draw() term.flip() self.check(0, 0, 'a') self.check(1, 0, 'b') self.check(4, 2, 'c')
def skip_test_blink_timing(self): import time term.set_cursor_type(1) term.move_cursor(1, 1) t = buffer.PlainText("Time!", x=0, y=0) t.draw() start = time.time() while time.time() - start < 2: term.flip() t.set("Keys!") t.draw() term.flip() for i in range(10): term.getkey()
def test_getch(self): x = 0 y = 0 for bgcolor in (colors.BLACK, colors.GREEN, colors.RED): for fgcolor in (colors.BLACK, colors.DARKGREY, colors.LIGHTGREY, colors.WHITE, colors.RED, colors.LIGHTRED): x += 1 y += 1 p = buffer.Buffer(width=1, height=1, x=x, y=y, data=[[[fgcolor, bgcolor, 'Q']]]) p.draw() term.flip() self.check(x=x, y=y, fg=fgcolor, bg=bgcolor, ch='Q')
def test_waterfall(self): #make a silly little waterfall r = random.Random() r.seed(1356317227) center = 0 def make_fall(): color = random.choice([ colors.WHITE, colors.LIGHTGREY, colors.LIGHTBLUE, colors.BLUE ]) x = int(random.triangular(0, self.width, mode=center)) return buffer.Buffer(width=1, height=5, x=x, y=-4, data=[ [[color, colors.BLACK, ' ']], [[color, colors.BLACK, '\xb0']], [[color, colors.BLACK, '\xb1']], [[color, colors.BLACK, '\xb2']], [[color, colors.BLACK, '\xdb']], ]) items = [make_fall()] frames = 150 start = time.time() for i in range(frames): if i < frames / 2: center += 1 else: center -= 1 items.extend([make_fall() for _ in range(4)]) for item in items: item.y += 1 if item.y > self.height: items.remove(item) else: item.draw() term.flip() end = time.time() fps = frames / (end - start) log.debug("waterfall FPS: %.2f over %r frames", fps, frames) self.assertGreater(fps, 40)
def test_make_box(self): box = self.box = buffer.MessageBox(x=5, y=5, width=20, height=20, padding_x=1, padding_y=1) self.draw_box(box) self.check(5, 5, boxtypes.BoxDouble.tl) self.check(6, 6, SPACE) self.add("a<RED>A") self.add("bb") self.add("ccc") self.add("dddd") self.check(6, 6, 'a') self.check(6, 7, 'b') self.add("twenty\n" * 20, scroll=False) self.check(6, 7, 'b') self.add("eeeee") self.check(6, 6, 't') self.check(6, 23, 'e') box.scroll(home=True) box.draw() term.flip() self.check(6, 6, 'a') self.check(6, 23, 't') self.check(6, 24, boxtypes.BoxDouble.horiz) self.add('\n'.join([str(i) + "!" for i in range(30)])) self.check(6, 5, boxtypes.BoxDouble.horiz) self.check(6, 6, '1') self.check(7, 6, '2') self.check(6, 23, '2') self.check(7, 23, '9') self.check(6, 24, boxtypes.BoxDouble.horiz) box.scroll(-3) box.draw() term.flip() self.check(6, 6, '9')
def test_pattern(self): #inspired by the space-filler algorithm at https://dl.dropbox.com/u/1094010/Projs/spacefill.html #designed to use a lot of little buffers as an alternate performance test #it flips a new frame on every 25 calculations, to be a little fair to poor slow-flip winconsole r = random.Random() r.seed(1356317227) seen = set() candidates = [(r.randint(0, self.width), r.randint(0, self.height), 0)] step = 0 buffers = [] start = time.time() # just count the last one now draws = 1 while candidates: next_index = step % len(candidates) x, y, depth = candidates.pop(next_index) step += 1 for xoff, yoff in [(-1, 0), (0, -1), (1, 0), (0, 1)]: nx = x + xoff ny = y + yoff if ( nx, ny ) in seen or nx < 0 or ny < 0 or nx > self.width or ny > self.height: continue seen.add((nx, ny)) candidates.append((nx, ny, depth + 1)) b = buffer.Buffer(x=x, y=y, width=1, height=1, data=[[[step % 15 + 1, colors.BLACK, '\xb0']]]) buffers.append(b) b.draw() if step % 25 == 0: term.flip() draws += 1 term.flip() end = time.time() fps = draws / (end - start) log.debug("pattern FPS: %.2f over %r frames", fps, draws)
def test_pattern(self): #inspired by the space-filler algorithm at https://dl.dropbox.com/u/1094010/Projs/spacefill.html #designed to use a lot of little buffers as an alternate performance test #it flips a new frame on every 25 calculations, to be a little fair to poor slow-flip winconsole r = random.Random() r.seed(1356317227) seen = set() candidates = [(r.randint(0, self.width), r.randint(0, self.height), 0)] step = 0 buffers = [] start = time.time() # just count the last one now draws = 1 while candidates: next_index = step % len(candidates) x, y, depth = candidates.pop(next_index) step += 1 for xoff, yoff in [(-1, 0), (0, -1), (1, 0), (0, 1)]: nx = x + xoff ny = y + yoff if (nx, ny) in seen or nx < 0 or ny < 0 or nx > self.width or ny > self.height: continue seen.add((nx, ny)) candidates.append((nx, ny, depth+1)) b = buffer.Buffer(x=x, y=y, width=1, height=1, data=[ [ [step % 15 + 1, colors.BLACK, '\xb0'] ] ]) buffers.append(b) b.draw() if step % 25 == 0: term.flip() draws += 1 term.flip() end = time.time() fps = draws / (end - start) log.debug("pattern FPS: %.2f over %r frames", fps, draws)
def test_make_box(self): box = self.box = buffer.MessageBox(x=5, y=5, width=20, height=20, padding_x=1, padding_y=1) self.draw_box(box) self.check(5, 5, boxtypes.BoxDouble.tl) self.check(6, 6, SPACE) self.add("a<RED>A") self.add("bb") self.add("ccc") self.add("dddd") self.check(6, 6, 'a') self.check(6, 7, 'b') self.add("twenty\n"*20, scroll=False) self.check(6, 7, 'b') self.add("eeeee") self.check(6, 6, 't') self.check(6, 23, 'e') box.scroll(home=True) box.draw() term.flip() self.check(6, 6, 'a') self.check(6, 23, 't') self.check(6, 24, boxtypes.BoxDouble.horiz) self.add('\n'.join([str(i) + "!" for i in range(30)])) self.check(6, 5, boxtypes.BoxDouble.horiz) self.check(6, 6, '1') self.check(7, 6, '2') self.check(6, 23, '2') self.check(7, 23, '9') self.check(6, 24, boxtypes.BoxDouble.horiz) box.scroll(-3) box.draw() term.flip() self.check(6, 6, '9')
def test_waterfall(self): #make a silly little waterfall r = random.Random() r.seed(1356317227) center = 0 def make_fall(): color = random.choice([colors.WHITE, colors.LIGHTGREY, colors.LIGHTBLUE, colors.BLUE]) x = int(random.triangular(0, self.width, mode=center)) return buffer.Buffer(width=1, height=5, x=x, y=-4, data=[ [[color, colors.BLACK, ' ']], [[color, colors.BLACK, '\xb0']], [[color, colors.BLACK, '\xb1']], [[color, colors.BLACK, '\xb2']], [[color, colors.BLACK, '\xdb']], ]) items = [make_fall()] frames = 150 start = time.time() for i in range(frames): if i < frames/2: center += 1 else: center -= 1 items.extend([make_fall() for _ in range(4)]) for item in items: item.y += 1 if item.y > self.height: items.remove(item) else: item.draw() term.flip() end = time.time() fps = frames / (end - start) log.debug("waterfall FPS: %.2f over %r frames", fps, frames) self.assertGreater(fps, 40)
def test_oob_blit(self): nullbuff = buffer.Buffer(x=10, y=10, width=0, height=0) nullbuff.draw() whitebox = buffer.Box(x=10, y=10, width=5, height=5,interior_bg=colors.WHITE) redbox = buffer.Box(x=10, y=10, width=5, height=5,interior_bg=colors.RED) bluebox = buffer.Box(x=10, y=10, width=5, height=5,interior_bg=colors.BLUE) self.draw_box(whitebox) redbox.x = -1 redbox.y = -1 redbox.draw() term.flip() self.check(0, 0, bg=colors.RED) self.check(self.width-1, self.height-1, bg=colors.BLACK) bluebox.x = self.width - 2 bluebox.y = self.height - 2 bluebox.draw() term.flip() self.check(self.width-2, self.height-2, ch=boxtypes.BoxDouble.tl) self.check(self.width-1, self.height-1, bg=colors.BLUE)
def scroll(self, *args, **kwargs): self.box.scroll(*args, **kwargs) self.box.draw() term.flip()
def test_getch(self): x = 0; y = 0 for bgcolor in (colors.BLACK, colors.GREEN, colors.RED): for fgcolor in (colors.BLACK, colors.DARKGREY, colors.LIGHTGREY, colors.WHITE, colors.RED, colors.LIGHTRED): x += 1; y += 1 p = buffer.Buffer(width=1, height=1, x=x, y=y, data=[[[fgcolor, bgcolor, 'Q']]]); p.draw(); term.flip(); self.check(x=x, y=y, fg=fgcolor, bg=bgcolor, ch='Q')
return buf if __name__ == "__main__": import WConio as W x = 0 def show(filename): global x f = open(filename) buf = read_to_buffer(f) buf.x = x x += buf.width buf.draw() try: import sys if not len(sys.argv) > 1: print "nom nom need argument" else: term.init() show(sys.argv[1]) if len(sys.argv) > 2: show(sys.argv[2]) term.flip() W.settitle("Mutants Of Melimnor") term.getkey() except: raise finally: W.textmode() W.textcolor(W.LIGHTGREY)