From 648143b7a0c929dc532ca9cecb63ca15a5081248 Mon Sep 17 00:00:00 2001 From: synth-ruiner Date: Sun, 24 Dec 2017 18:14:16 +0000 Subject: [PATCH] "fix" output canvas and data export still not sure why pixels are inverse at some point also tidied up a few things report palette switch bug --- includes/script.js | 44 ++++++++++++++++++++++---------------------- readme.md | 1 + 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/includes/script.js b/includes/script.js index b301238..251e429 100644 --- a/includes/script.js +++ b/includes/script.js @@ -43,7 +43,7 @@ $(document).ready(function() { // sum rgb differences return _.reduce(difference, function(sum, n) { - return sum + n; + return sum + n; }, 0); } @@ -62,7 +62,7 @@ $(document).ready(function() { } function hexToColour(hex) { - var rgb = hex.match(/[\da-fA-F]{2}/g); + var rgb = hex.match(/[\da-f]{2}/gi); return { red: parseInt(rgb[0], 16), @@ -121,6 +121,7 @@ $(document).ready(function() { }); // get tiles + bitsyData.tiles = {}; // tile 0 (background colour only) is implicit in bitsy rather than being stored in the game data @@ -130,22 +131,21 @@ $(document).ready(function() { bitmap: _.chunk(_.split(_.repeat(0, 64), ''), 8) }; + // todo: handle animated tiles properly instead of discarding the second animation frame var tiles = input.match(/TIL (.*)\n([01]{8}\n){8}/g); - if (tiles.length > 0) { - for (var i = 0; i < tiles.length; i++) { - var name = tiles[i].match(/TIL .*/)[0].replace('TIL ', ''); - - tiles[i] = tiles[i].replace(/TIL .*\n/, ''); + _.each(tiles, function(tile, i) { + var name = tile.match(/TIL .*/)[0].replace('TIL ', ''); + + tile = tile.replace(/TIL .*\n/, ''); - var bitmap = tiles[i].match(/[01]/g); + var bitmap = tile.match(/[01]/g); - bitsyData.tiles[name] = { - name: name, - bitmap: _.chunk(bitmap, 8) - }; - } - } + bitsyData.tiles[name] = { + name: name, + bitmap: _.chunk(bitmap, 8) + }; + }); // set palette to first imported palette and redraw palette = _.first(_.sortBy(bitsyData.palettes, 'id')); @@ -197,7 +197,7 @@ $(document).ready(function() { var rawData = imageData.data; var monochrome = []; - brightnessAdjustment = parseFloat($('#brightness').val()); + var brightnessAdjustment = parseFloat($('#brightness').val()); // for each pixel for (var i = 0; i < rawData.length; i += 4) { @@ -210,10 +210,10 @@ $(document).ready(function() { var targetColour = getClosestColour(pixel, palette); - if (targetColour.name === "background") { - monochrome.push(0); + if (targetColour.name === "background") { // ?! why is this reversed? + monochrome.push(1); } else { // tile - monochrome.push(1) + monochrome.push(0) } rawData[i ] = targetColour.red; @@ -280,10 +280,10 @@ $(document).ready(function() { position *= 4; // 4 values (rgba) per pixel - if (parseInt(pixel) === 1) { // ?! wtf - pixelColour = palette.background; + if (parseInt(pixel) === 0) { + var pixelColour = palette.background; } else { - pixelColour = palette.tile; + var pixelColour = palette.tile; } rawData[position ] = pixelColour.red; @@ -366,4 +366,4 @@ $(document).ready(function() { // write $('textarea').val(newGameData.replace(/(ROOM .*\n(.*\n)*PAL .*)/g, '$1\n\n' + newRoom)); }); -}); \ No newline at end of file +}); diff --git a/readme.md b/readme.md index ab88e8a..6edfe06 100644 --- a/readme.md +++ b/readme.md @@ -40,6 +40,7 @@ if (src.match(/^(file|https)?:\/\/|^\/\//)) { ## known bugs +* keyup on game data input sets palette to pal 0 even if game data has not changed ## to do