Skip to content

Commit babe636

Browse files
committed
tests, performance manual page
1 parent 700329c commit babe636

File tree

9 files changed

+95
-27
lines changed

9 files changed

+95
-27
lines changed

license.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2012, Ondrej Zara
1+
Copyright (c) 2012-now(), Ondrej Zara
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification,

manual/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ <h1>rot.js interactive manual</h1>
3737
<li><a href="#hex/indexing">Indexing</a></li>
3838
</ul>
3939
</li>
40-
<li><a class="last" href="#stringgenerator">String generator</a></li>
40+
<li><a href="#stringgenerator">String generator</a></li>
41+
<li><a class="last" href="#performance">Performance</a></li>
4142
</ul>
4243

4344
<div id="content"></div>

manual/manual.css

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import url(http://fonts.googleapis.com/css?family=Droid+Serif:400,400italic,700,700italic|Inconsolata|Droid+Sans+Mono);
1+
@import url(http://fonts.googleapis.com/css?family=Droid+Serif:400,400italic,700,700italic|Inconsolata);
22

33
body {
44
background-color: #606060;
@@ -35,7 +35,7 @@ li {
3535
#menu {
3636
position: fixed;
3737
left: 0px;
38-
top: 40px;
38+
top: 10px;
3939
width: 180px;
4040
}
4141

@@ -204,3 +204,7 @@ li {
204204
content: "✘ ";
205205
color: #f00;
206206
}
207+
208+
.performance li {
209+
margin-bottom: 1em;
210+
}

manual/pages/performance.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h2>Performance tips</h2>
2+
3+
<ul class="performance">
4+
<li><strong>Slow display output?</strong> If you use a rectangular layout, you can enable glyph caching by calling <code>ROT.Display.Rect.cache = true</code>. This enables rendering into tiny cacheable canvases, but will improve performance only if you do not use too many glyph/color/bgcolor combinations. Your mileage may vary.</li>
5+
6+
<li>
7+
<strong>Too much time spent in lighting computations?</strong> Decrease the maximum light range or switch to 1-pass lighting.</li>
8+
</li>
9+
</ul>

rot.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This is rot.js, the ROguelike Toolkit in JavaScript.
3-
Version 0.4~dev, generated on Sun Jan 27 21:08:49 CET 2013.
3+
Version 0.4~dev, generated on Mon Jan 28 14:18:21 CET 2013.
44
*/
55

66
/**
@@ -714,10 +714,10 @@ ROT.Display.Backend.prototype.compute = function(options) {
714714
ROT.Display.Backend.prototype.draw = function(data, clearBefore) {
715715
}
716716

717-
ROT.Display.Backend.prototype.computeSize = function(availWidth, availHeight, options) {
717+
ROT.Display.Backend.prototype.computeSize = function(availWidth, availHeight) {
718718
}
719719

720-
ROT.Display.Backend.prototype.computeFontSize = function(availWidth, availHeight, options) {
720+
ROT.Display.Backend.prototype.computeFontSize = function(availWidth, availHeight) {
721721
}
722722
/**
723723
* @class Rectangular backend
@@ -729,13 +729,15 @@ ROT.Display.Rect = function(context) {
729729
this._spacingX = 0;
730730
this._spacingY = 0;
731731
this._canvasCache = {};
732+
this._options = {};
732733
}
733734
ROT.Display.Rect.extend(ROT.Display.Backend);
734735

735736
ROT.Display.Rect.cache = false;
736737

737738
ROT.Display.Rect.prototype.compute = function(options) {
738739
this._canvasCache = {};
740+
this._options = options;
739741

740742
var charWidth = Math.ceil(this._context.measureText("W").width);
741743
this._spacingX = Math.ceil(options.spacing * charWidth);
@@ -801,19 +803,19 @@ ROT.Display.Rect.prototype._drawNoCache = function(data, clearBefore) {
801803
this._context.fillText(ch, (x+0.5) * this._spacingX, (y+0.5) * this._spacingY);
802804
}
803805

804-
ROT.Display.Rect.prototype.computeSize = function(availWidth, availHeight, options) {
806+
ROT.Display.Rect.prototype.computeSize = function(availWidth, availHeight) {
805807
var width = Math.floor(availWidth / this._spacingX);
806808
var height = Math.floor(availHeight / this._spacingY);
807-
return [width, height]
809+
return [width, height];
808810
}
809811

810-
ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight, options) {
811-
var boxWidth = Math.floor(availWidth / options.width);
812-
var boxHeight = Math.floor(availHeight / options.height);
812+
ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight) {
813+
var boxWidth = Math.floor(availWidth / this._options.width);
814+
var boxHeight = Math.floor(availHeight / this._options.height);
813815

814816
/* compute char ratio */
815817
var oldFont = this._context.font;
816-
this._context.font = "100px " + options.fontFamily;
818+
this._context.font = "100px " + this._options.fontFamily;
817819
var width = Math.ceil(this._context.measureText("W").width);
818820
this._context.font = oldFont;
819821
var ratio = width / 100;
@@ -822,7 +824,7 @@ ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight, o
822824
if (widthFraction > 1) { /* too wide with current aspect ratio */
823825
boxHeight = Math.floor(boxHeight / widthFraction);
824826
}
825-
return Math.floor(boxHeight / options.spacing);
827+
return Math.floor(boxHeight / this._options.spacing);
826828
}
827829
/**
828830
* @class Hexagonal backend
@@ -868,11 +870,11 @@ ROT.Display.Hex.prototype.draw = function(data, clearBefore) {
868870
}
869871

870872

871-
ROT.Display.Hex.prototype.computeSize = function(availWidth, availHeight, options) {
873+
ROT.Display.Hex.prototype.computeSize = function(availWidth, availHeight) {
872874
/* FIXME */
873875
}
874876

875-
ROT.Display.Hex.prototype.computeFontSize = function(availWidth, availHeight, options) {
877+
ROT.Display.Hex.prototype.computeFontSize = function(availWidth, availHeight) {
876878
/* FIXME */
877879
}
878880

src/display/backend.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ ROT.Display.Backend.prototype.compute = function(options) {
1212
ROT.Display.Backend.prototype.draw = function(data, clearBefore) {
1313
}
1414

15-
ROT.Display.Backend.prototype.computeSize = function(availWidth, availHeight, options) {
15+
ROT.Display.Backend.prototype.computeSize = function(availWidth, availHeight) {
1616
}
1717

18-
ROT.Display.Backend.prototype.computeFontSize = function(availWidth, availHeight, options) {
18+
ROT.Display.Backend.prototype.computeFontSize = function(availWidth, availHeight) {
1919
}

src/display/hex.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ ROT.Display.Hex.prototype.draw = function(data, clearBefore) {
4242
}
4343

4444

45-
ROT.Display.Hex.prototype.computeSize = function(availWidth, availHeight, options) {
45+
ROT.Display.Hex.prototype.computeSize = function(availWidth, availHeight) {
4646
/* FIXME */
4747
}
4848

49-
ROT.Display.Hex.prototype.computeFontSize = function(availWidth, availHeight, options) {
49+
ROT.Display.Hex.prototype.computeFontSize = function(availWidth, availHeight) {
5050
/* FIXME */
5151
}
5252

src/display/rect.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ ROT.Display.Rect = function(context) {
88
this._spacingX = 0;
99
this._spacingY = 0;
1010
this._canvasCache = {};
11+
this._options = {};
1112
}
1213
ROT.Display.Rect.extend(ROT.Display.Backend);
1314

1415
ROT.Display.Rect.cache = false;
1516

1617
ROT.Display.Rect.prototype.compute = function(options) {
1718
this._canvasCache = {};
19+
this._options = options;
1820

1921
var charWidth = Math.ceil(this._context.measureText("W").width);
2022
this._spacingX = Math.ceil(options.spacing * charWidth);
@@ -80,19 +82,19 @@ ROT.Display.Rect.prototype._drawNoCache = function(data, clearBefore) {
8082
this._context.fillText(ch, (x+0.5) * this._spacingX, (y+0.5) * this._spacingY);
8183
}
8284

83-
ROT.Display.Rect.prototype.computeSize = function(availWidth, availHeight, options) {
85+
ROT.Display.Rect.prototype.computeSize = function(availWidth, availHeight) {
8486
var width = Math.floor(availWidth / this._spacingX);
8587
var height = Math.floor(availHeight / this._spacingY);
86-
return [width, height]
88+
return [width, height];
8789
}
8890

89-
ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight, options) {
90-
var boxWidth = Math.floor(availWidth / options.width);
91-
var boxHeight = Math.floor(availHeight / options.height);
91+
ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight) {
92+
var boxWidth = Math.floor(availWidth / this._options.width);
93+
var boxHeight = Math.floor(availHeight / this._options.height);
9294

9395
/* compute char ratio */
9496
var oldFont = this._context.font;
95-
this._context.font = "100px " + options.fontFamily;
97+
this._context.font = "100px " + this._options.fontFamily;
9698
var width = Math.ceil(this._context.measureText("W").width);
9799
this._context.font = oldFont;
98100
var ratio = width / 100;
@@ -101,5 +103,5 @@ ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight, o
101103
if (widthFraction > 1) { /* too wide with current aspect ratio */
102104
boxHeight = Math.floor(boxHeight / widthFraction);
103105
}
104-
return Math.floor(boxHeight / options.spacing);
106+
return Math.floor(boxHeight / this._options.spacing);
105107
}

tests/spec/display.js

+50
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,54 @@ describe("Display", function() {
66
expect(lines).toBe(2);
77
});
88
});
9+
10+
describe("computeSize", function() {
11+
var d1 = new ROT.Display({fontSize:18, spacing:1});
12+
var d2 = new ROT.Display({fontSize:18, spacing:1.2});
13+
14+
it("should compute integer size for spacing 1", function() {
15+
var size = d1.computeSize(1/0, 180);
16+
expect(size[1]).toBe(10);
17+
});
18+
19+
it("should compute fractional size for spacing 1", function() {
20+
var size = d1.computeSize(1/0, 170);
21+
expect(size[1]).toBe(9);
22+
});
23+
24+
it("should compute integer size for spacing >1", function() {
25+
var size = d2.computeSize(1/0, 220);
26+
expect(size[1]).toBe(10);
27+
});
28+
29+
it("should compute fractional size for spacing >1", function() {
30+
var size = d2.computeSize(1/0, 210);
31+
expect(size[1]).toBe(9);
32+
});
33+
});
34+
35+
describe("computeFontSize", function() {
36+
var d1 = new ROT.Display({width:100, height:20, spacing:1});
37+
var d2 = new ROT.Display({width:100, height:20, spacing:1.2});
38+
39+
it("should compute integer size for spacing 1", function() {
40+
var size = d1.computeFontSize(1/0, 180);
41+
expect(size).toBe(9);
42+
});
43+
44+
it("should compute fractional size for spacing 1", function() {
45+
var size = d1.computeFontSize(1/0, 170);
46+
expect(size).toBe(8);
47+
});
48+
49+
it("should compute integer size for spacing >1", function() {
50+
var size = d2.computeFontSize(1/0, 180);
51+
expect(size).toBe(7);
52+
});
53+
54+
it("should compute fractional size for spacing >1", function() {
55+
var size = d2.computeFontSize(1/0, 170);
56+
expect(size).toBe(6);
57+
});
58+
});
959
});

0 commit comments

Comments
 (0)