Skip to content

Commit

Permalink
Only one place where the size is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Nov 29, 2017
1 parent 14cd59b commit 4146138
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions VoxelSpace.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@

var map =
{
height: new Uint8Array(1024*1024), // 1024*1024 byte array with height information
color: new Uint32Array(1024*1024) // 1024*1024 int array with RGB colors
width: 1024,
height: 1024,
shift: 10, // power of two: 2^10 = 1024
altitude: new Uint8Array(1024*1024), // 1024 * 1024 byte array with height information
color: new Uint32Array(1024*1024) // 1024 * 1024 int array with RGB colors
};

// ---------------------------------------------
Expand Down Expand Up @@ -188,8 +191,8 @@
}

// Collision detection. Don't fly below the surface.
var mapoffset = ((Math.floor(camera.y) & 1023) << 10) + (Math.floor(camera.x) & 1023)|0;
if ((map.height[mapoffset]+10) > camera.height) camera.height = map.height[mapoffset] + 10;
var mapoffset = ((Math.floor(camera.y) & (map.width-1)) << map.shift) + (Math.floor(camera.x) & (map.height-1))|0;
if ((map.altitude[mapoffset]+10) > camera.height) camera.height = map.altitude[mapoffset] + 10;

time = current;

Expand Down Expand Up @@ -374,6 +377,9 @@

function Render()
{
var mapwidthperiod = map.width - 1;
var mapheightperiod = map.height - 1;

var screenwidth = screen.canvas.width|0;
var sinang = Math.sin(camera.angle);
var cosang = Math.cos(camera.angle);
Expand All @@ -400,8 +406,8 @@
var invz = 1. / z * 240.;
for(var i=0; i<screenwidth|0; i=i+1|0)
{
var mapoffset = ((Math.floor(ply) & 1023) << 10) + (Math.floor(plx) & 1023)|0;
var heightonscreen = (camera.height - map.height[mapoffset]) * invz + camera.horizon|0;
var mapoffset = ((Math.floor(ply) & mapwidthperiod) << map.shift) + (Math.floor(plx) & mapheightperiod)|0;
var heightonscreen = (camera.height - map.altitude[mapoffset]) * invz + camera.horizon|0;
DrawVerticalLine(i, heightonscreen|0, hiddeny[i], map.color[mapoffset]);
if (heightonscreen < hiddeny[i]) hiddeny[i] = heightonscreen;
plx += dx;
Expand Down Expand Up @@ -450,11 +456,10 @@
image.onload = function() {
var tempcanvas = document.createElement("canvas");
var tempcontext = tempcanvas.getContext("2d");
tempcanvas.width = 1024;
tempcanvas.height = 1024;
tempcontext.drawImage(image, 0, 0, 1024, 1024 );

result[i] = tempcontext.getImageData(0, 0, 1024, 1024).data;
tempcanvas.width = map.width;
tempcanvas.height = map.height;
tempcontext.drawImage(image, 0, 0, map.width, map.height);
result[i] = tempcontext.getImageData(0, 0, map.width, map.height).data;
pending--;
if (pending === 0) {
OnSuccess(result);
Expand All @@ -474,10 +479,10 @@
{
var datac = result[0];
var datah = result[1];
for(var i=0; i<1024*1024; i++)
for(var i=0; i<map.width*map.height; i++)
{
map.color[i] = 0xFF000000 | (datac[(i<<2) + 2] << 16) | (datac[(i<<2) + 1] << 8) | datac[(i<<2) + 0];
map.height[i] = datah[i<<2];
map.altitude[i] = datah[i<<2];
}
Draw();
}
Expand Down Expand Up @@ -505,10 +510,10 @@

function Init()
{
for(var i=0; i<1024*1024; i++)
for(var i=0; i<map.width*map.height; i++)
{
map.color[i] = 0xFF007050;
map.height[i] = 0;
map.altitude[i] = 0;
}

LoadMap("C1W;D1");
Expand Down

0 comments on commit 4146138

Please sign in to comment.