Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have implemented the ASCII art images class #19

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e29588d
_digCallback function fix
yanno Feb 17, 2013
9813bdd
Added the first version of ASCII art image converter (from http://blo…
mdtrooper Aug 6, 2013
805aea8
Start to create the Image class from jsascii code. And added into the…
mdtrooper Aug 8, 2013
eeafcdf
Added a directory with some examples (well it is more a working direc…
mdtrooper Aug 8, 2013
e83835c
More job into the image ascii art.
mdtrooper Aug 20, 2013
82c4f88
Delete a ugly files.
mdtrooper Aug 20, 2013
d14790c
Add a small example to testing and check the progress.
mdtrooper Aug 20, 2013
7a7e926
More changes.
mdtrooper Sep 2, 2013
ea857af
First beta version. There is a method to print a ascii art in rot!!
mdtrooper Sep 3, 2013
dc2aac4
It is a early version of image ascii art, there are blit functions.
mdtrooper Sep 27, 2013
64f31fe
Added in the comment where is the original library (jsAscii, author J…
mdtrooper Sep 27, 2013
55e5c96
Added some "awesome" examples of image (ASCII art) class.
Sep 27, 2013
b4afeb7
Merge branch 'master' of https://github.com/ondras/rot.js
mdtrooper Apr 21, 2014
7252f6a
Added a short explain about the ASCIIart support
mdtrooper Apr 21, 2014
e30ea3f
Merge remote-tracking branch 'yanno/patch-1'
mdtrooper Apr 22, 2014
cd085c0
Merge branch 'master' of https://github.com/ondras/rot.js
mdtrooper Sep 8, 2014
d7c2c0f
Merge branch 'master' of https://github.com/ondras/rot.js
mdtrooper Dec 4, 2014
6d07167
Merge branch 'master' of https://github.com/ondras/rot.js
mdtrooper Nov 1, 2016
2b7cafc
Making a new js files.
mdtrooper Nov 1, 2016
b147c84
Upload last generate js files.
mdtrooper Nov 1, 2016
497adce
Added the own image files into the makefile.
mdtrooper Nov 1, 2016
ec4eb44
Fixed typo bug in the Makefile.
mdtrooper Nov 1, 2016
d8bca52
Some fixes in image module and re-generated js libraries.
mdtrooper Nov 1, 2016
876eac6
Cleaned source code of examples for ASCIIart.
mdtrooper Nov 1, 2016
baee4e4
Merge branch 'master' of https://github.com/mdtrooper/rot.js
mdtrooper Nov 1, 2016
88d3d10
I love the f*cking javascript and the lost mutex in this language.
mdtrooper Nov 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ SOURCES = src/rot.js \
src/lighting.js \
src/path/path.js \
src/path/dijkstra.js \
src/path/astar.js
src/path/astar.js \
src/image/image.js

NODE_VERSION = "$(shell head -1 < NODE_VERSION)"
NODE_PRE_SOURCES = node/node-shim.js
Expand Down
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
rot.js
======
rot.js _(with ASCIIart support)_
================================

ROguelike Toolkit in JavaScript. For more info, see http://ondras.github.com/rot.js.
**RO**guelike **T**oolkit in **J**ava**S**cript. For more info, see https://github.com/mdtrooper/rot.js.

To install `rot.js`, pick one:
- clone this repository to gain full source code access;
- download just the [minified rot.min.js file](https://github.com/ondras/rot.js/blob/master/rot.min.js) to include it in your project;
- download just the [minified rot.min.js file](https://github.com/mdtrooper/rot.js/blob/master/rot.min.js) to include it in your project;
- use `bower install rot.js`, if you prefer Bower for package management;

RogueBasin page (with links to some rot.js-based games): http://www.roguebasin.roguelikedevelopment.org/index.php?title=Rot.js

ASCIIart support
----------------
This fork has a support for [ASCIIart (link to wikipedia)](http://en.wikipedia.org/wiki/ASCII_art) for you use the image files in native format (jpg, png…) and the game shows a pretty ASCIIart.

It is very easy to use.

```javascript
var image = new ROT.Image();

image.strResolution = "medium";
image.bBlock = false;
image.load("combat.png");

var display = new ROT.Display({width:w, height:h, fontSize:10});

image.paint(display, 0, 0);
```

The image methods are:
* load(url): get a image from url and convert to ASCIIart.
* blit(display, display_x, display_y, image_x, image_y, rect_w, rect_h): paint a part of ASCIIart into the display.
* paint(display, offset_x, offset_y): paint the entire ASCIIart into a position in the display.

And the config attributes are:
* bBlock: for to paint blocks instead characters.
* bColor: ASCIIart in color or not.
* strResolution: (low, medium, high) the ASCII art resolution.
35 changes: 35 additions & 0 deletions examples/combat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<html>
<head>
<title>test</title>
</head>
<body>
<script type="text/javascript" src="../rot.js"></script>

<!--
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
-->

<script type="text/javascript">
var w = 200;
var h = 60;

var image = new ROT.Image();

image.strResolution = "medium";
image.bBlock = false;
image.load("combat.png", function() {game();} );

var display = new ROT.Display({width:w, height:h, fontSize:10});



document.body.appendChild(display.getContainer());

function game()
{
image.paint(display, 0, 0);
}
</script>
<canvas></canvas>
</body>
</html>
Binary file added examples/combat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/src
34 changes: 34 additions & 0 deletions examples/title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<html>
<head>
<title>test</title>
</head>
<body>
<script type="text/javascript" src="../rot.js"></script>

<!--
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
-->

<script type="text/javascript">
var w = 200;
var h = 60;

var image = new ROT.Image();
image.strResolution = "medium";
image.bBlock = false;
image.load("title.png", function() {game();} );

var display = new ROT.Display({width:w, height:h, fontSize:10});



document.body.appendChild(display.getContainer());

function game()
{
image.paint(display, 0, 0);
}
</script>
<canvas></canvas>
</body>
</html>
Binary file added examples/title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
228 changes: 225 additions & 3 deletions rot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This is rot.js, the ROguelike Toolkit in JavaScript.
Version 0.7~dev, generated on Tue Aug 30 12:08:59 CEST 2016.
Version 0.7~dev, generated on mié nov 2 00:54:29 CET 2016.
*/
/**
* @namespace Top-level ROT namespace
Expand Down Expand Up @@ -1712,6 +1712,17 @@ ROT.EventQueue.prototype.get = function() {
return this._events.splice(0, 1)[0];
}

/**
* Get the time associated with the given event
* @param {?} event
* @returns {number} time
*/
ROT.EventQueue.prototype.getEventTime = function(event) {
var index = this._events.indexOf(event);
if (index == -1) { return undefined }
return this._eventTimes[index];
}

/**
* Remove an event from the queue
* @param {?} event
Expand Down Expand Up @@ -1757,6 +1768,15 @@ ROT.Scheduler.prototype.add = function(item, repeat) {
return this;
}

/**
* Get the time the given item is scheduled for
* @param {?} item
* @returns {number} time
*/
ROT.Scheduler.prototype.getTimeOf = function(item) {
return this._queue.getEventTime(item);
}

/**
* Clear all items
*/
Expand Down Expand Up @@ -1829,10 +1849,11 @@ ROT.Scheduler.Speed.extend(ROT.Scheduler);
/**
* @param {object} item anything with "getSpeed" method
* @param {bool} repeat
* @param {number} [time=1/item.getSpeed()]
* @see ROT.Scheduler#add
*/
ROT.Scheduler.Speed.prototype.add = function(item, repeat) {
this._queue.add(item, 1/item.getSpeed());
ROT.Scheduler.Speed.prototype.add = function(item, repeat, time) {
this._queue.add(item, time !== undefined ? time : 1/item.getSpeed());
return ROT.Scheduler.prototype.add.call(this, item, repeat);
}

Expand Down Expand Up @@ -5305,3 +5326,204 @@ ROT.Path.AStar.prototype._distance = function(x, y) {

throw new Error("Illegal topology");
}
/**
* @class Base image (as ASCII art) converter
*/

//BASED IN THE WORK OF:
/*
* jsAscii 0.1
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* MIT License [http://www.nihilogic.dk/licenses/mit-license.txt]
*/
ROT.Image = function() {
this.aDefaultCharList = (" .,:;i1tfLCG08@").split("");
this.aDefaultColorCharList = (" CGO08@").split("");
this.strFont = "courier new";
this.bBlock = true;
this.bAlpha = false;
this.bColor = true;
this.strResolution = "medium";

this.ascii_art = "";
this.ascii_art_array = null;
this.height = 0;
this.width = 0;
};

ROT.Image.prototype.loadASCII = function() {
var aCharList = this.aDefaultCharList;
var bBlock = this.bBlock;
var bAlpha = this.bAlpha;
var bColor = this.bColor;

var oCanvas = document.createElement("canvas");
if (!oCanvas.getContext) {
return;
}
var oCtx = oCanvas.getContext("2d");
if (!oCtx.getImageData) {
return;
}

var strResolution = this.strResolution;

var fResolution = 0.5;

switch (strResolution) {
case "low" : fResolution = 0.25; break;
case "medium" : fResolution = 0.5; break;
case "high" : fResolution = 1; break;
}

var iWidth = Math.round(parseInt(this.img.width) * fResolution);
var iHeight = Math.round(parseInt(this.img.height) * fResolution);
this.width = iWidth;
this.height = iHeight;

oCanvas.width = iWidth;
oCanvas.height = iHeight;
//oCanvas.style.display = "none";
oCanvas.style.width = iWidth;
oCanvas.style.height = iHeight;

oCtx.drawImage(this.img, 0, 0, iWidth, iHeight);

var oImgData = oCtx.getImageData(0, 0, iWidth, iHeight).data;

var strChars = "";
var array_chars = [];

var iterator_y = 0;
for (var y=0;y<iHeight;y+=2) {
array_chars[iterator_y] = [];
for (var x=0;x<iWidth;x++) {
var iOffset = (y*iWidth + x) * 4;

var iRed = oImgData[iOffset];
var iGreen = oImgData[iOffset + 1];
var iBlue = oImgData[iOffset + 2];
var iAlpha = oImgData[iOffset + 3];

if (iAlpha == 0) {
var iBrightIdx = 0;
} else {
var fBrightness = (0.3*iRed + 0.59*iGreen + 0.11*iBlue) / 255;
var iCharIdx = (aCharList.length-1) - Math.round(fBrightness * (aCharList.length-1));
}

//~ if (bInvert) {
//~ iCharIdx = (aCharList.length-1) - iCharIdx;
//~ }
var strThisChar = aCharList[iCharIdx];
var unscape_char = strThisChar;

if (strThisChar == " ")
strThisChar = "&nbsp;";

if (bColor) {
strChars += "<span style='"
+ "color:rgb("+iRed+","+iGreen+","+iBlue+");"
+ (bBlock ? "background-color:rgb("+iRed+","+iGreen+","+iBlue+");" : "")
+ (bAlpha ? "opacity:" + (iAlpha/255) + ";" : "")
+ "'>" + strThisChar + "</span>";

array_chars[iterator_y][x] = {'r': iRed, 'g' : iGreen, 'b' : iBlue, 'char': unscape_char};
}
else {
strChars += strThisChar;
array_chars[iterator_y][x] = {'char': unscape_char};
}
}
iterator_y++;
strChars += "<br/>";
}

this.height = iterator_y;

this.ascii_art_array = array_chars;

return strChars;
};

ROT.Image.prototype.load = function(image_url, callback_load_complete) {
this.img = new Image();
this.img.setAttribute('crossOrigin', '');
this.img.src = image_url;

var self = this;

if (this.img.complete) {
this.ascii_art = this.loadASCII();

return true;
}
else {
this.img.onload = function() {
self.ascii_art = self.loadASCII();

if (typeof(callback_load_complete) != "undefined")
callback_load_complete();
}

return false;
}
};

ROT.Image.prototype.get = function(xin, yin) {
return this.ascii_art_array[yin][xin];
};

ROT.Image.prototype.blit = function(display, display_x, display_y, image_x, image_y, rect_w, rect_h) {
var i = 0;

for (var y = image_y; y < rect_h; y++) {
var j = 0;
for (var x = image_x; x < rect_w; x++) {
if (this.bColor) {
var color = "#" +
this.ascii_art_array[y][x]['r'].toString(16) +
this.ascii_art_array[y][x]['g'].toString(16) +
this.ascii_art_array[y][x]['b'].toString(16);
if (this.bBlock) {
display.draw(display_x + j, display_y + i, this.ascii_art_array[y][x]['char'], color, color);
}
else {
display.draw(display_x + j, display_y + i, this.ascii_art_array[y][x]['char'], color);
}
}
else {
display.draw(display_x + j, display_y + i, this.ascii_art_array[y][x]['char']);
}
j++;
}
i++;
}
};

ROT.Image.prototype.paint = function(display, offset_x, offset_y) {
var i = 0;

for (var y = 0; y < this.height; y++) {
var j = 0;
for (var x = 0; x < this.width; x++) {
if (this.bColor) {
var color = "#" +
this.ascii_art_array[y][x]['r'].toString(16) +
this.ascii_art_array[y][x]['g'].toString(16) +
this.ascii_art_array[y][x]['b'].toString(16);
if (this.bBlock) {
display.draw(offset_x + j, offset_y + i, this.ascii_art_array[y][x]['char'], color, color);
}
else {
display.draw(offset_x + j, offset_y + i, this.ascii_art_array[y][x]['char'], color);
}
}
else {
display.draw(offset_x + j, offset_y + i, this.ascii_art_array[y][x]['char']);
}
j++;
}
i++;
}
};
Loading