• Content Count

  • Joined

  • Last visited

  • Days Won


Everything posted by grelf

  1. grelf

    2D Map Representation as 1D array

    Matt: you prompted me to reexamine my scene drawing code with a view to possibly improving its performance. It is even more surprising that I get the performance that I do, because the following is the gist of what happens with an array called "about" which is created every time a scene is drawn. Its purpose is to hold information gathered about the ground immediately around the observer (object called "me") out to a distance which is a user-alterable range (up to 400 metres, 1 ground unit per metre). var mex = Math.round (me.x), mey = Math.round (me.y); // Fractional array indices would not work var xyLen = 2 * this.range_m + 1; // max 801 this.around = new Array (xyLen); // "this" is the scene object, we are in its draw() method for (var xScan = mex - this.range_m; xScan <= mex + this.range_m; xScan++) { this.around [xScan] = new Array (xyLen); // ... start filling this.around by scanning both x & y around me My point is that this.around [xScan][yScan] is being indexed by ground coordinates which can be huge. I recently discovered that the width and height of my map are each about 1.8E15 metres (it could not be a planet in our universe). The code fails beyond that range but performance does not fall off before that! So I get away with it because of JavaScript's very flexible way of indexing arrays (and evidently very efficient in current browser implementations). If I were instead to allocate (the largest possible version of) the array just once when the scene object is constructed I would have to calculate indices in my own code so that they range from 0 upwards. I expect performance could easily be worse then.
  2. My approach is to use photos to avoid any complicated rendering, and use the basic "2d" canvas context without any third party libraries or frameworks. I do not use WebGL because I believe it is not available on all devices, whereas plain HTML5/JavaScript is. I want my efforts to work on everything from desktop to smartphone. I have been amazed by how fast images can be scaled and drawn in the 2d context. My prime example is at where the scenes are drawn in less than a second on current machines and yet each comprises many thousands of scaled copies of a few photos (sample scene below). The scenes are 3D in the sense that true perspective is calculated for distance from the observer but each drawn image is of course 2D. I use my own photos to avoid copyright issues. I edit them in Photoshop to cut out the objects I want against a transparent background and the PNG file format preserves the transparency so objects can be drawn sensibly in front of each other. I have the impression that the frameworks are each written for very specific types of games. I want to encourage people to break out from those moulds and be more creative. I know that my forest is miles away from mainstream video games but I hope it demonstrates that the basic HTML5/JavaScript platform can be used for very varied effects. In an attempt to help others I am documenting my forest, including how it is programmed, starting here:
  3. grelf

    2D Map Representation as 1D array

    I think there are two main issues: - Maintainability: using multiple indices, as in map [x][y] will be much easier to comprehend if you come back to the program years later to correct or enhance it. - Performance. There is no doubt that allocating a single large array is faster than allocating hundreds or perhaps thousands of smaller ones, which is what happens in the 2-index case*. Likewise the garbage collector has much more work to do in the latter case but if your arrays remain permanently through your program (do not go out of scope) that is not a consideration. In drawing the scenes in my forest I do allocate and release a 2D array every time (up to 400 x 400 so it can comprise up to 401 1D arrays). The time taken to redraw the same scene in my program is very variable and think this is probably due to garbage collection. I have been aware from the outset that performance might be improved by redesigning this but it is not my highest priority. * According to the JavaScript standard because there is no way to say new Array (m, n) to indicate 2D (instead you would get the 1D array [m, n] in this case).
  4. One example I used in my JavaScript/HTML5 programming course ( ) was a rather crude drawing program, grDraw. An exercise at the end of the course suggests some improvements to make. My own improved version can be found at I mention it because it includes some things that may be useful to other developers such as - a colour palette object which may help beginners to understand CSS colours - browsing to display a user's images and get spot colours from them - saving images in local storage and retrieving them (not as difficult as I first imagined) An example display can be seen below - echoes of Kandinsky?
  5. I have completed writing my free JavaScript course (begun in 2011). It is in two main parts. The first is aimed at complete beginners to introduce the whole idea of programming. It shows how to get some simple examples working and introduces some HTML5 as needed to make a platform for running the examples. It covers some general programming concepts such as writing requirements and testing against them. Part 2 is a more systematic exposition of the language, up to some of the more recent capabilities such as local storage and image processing. It is available at That link is to a contents page so that those who already know some of it can jump in to any particular topic of interest.
  6. As some have already pointed out in this forum there is a problem with scenes stretching ahead with a sudden cut-off at a horizon. Objects just beyond the horizon suddenly pop completely into view on the slightest forward movement. I have addressed this in my program The Forest ( ) by making the scene hazy (or foggy) towards the horizon, if the user switches this effect on via a check box on the page. A couple of example scenes are attached here. I have written a detailed description of how I programmed it ( ). My code is plain HTML5/JavaScript using no framework and only the standard 2D graphics context because I want my program to run on as many platforms as possible. So my documentation explains how to process images in that environment and indeed how to copy an object of type Image complete with its pixel data, which is not entirely obvious or straightforward. I hope this may be useful to others.
  7. grelf

    New free game: The Forest

    New free game: The Forest See and documentation at This is a redevelopment in HTML5/JavaScript combining two much earlier programs, "The Forest" and "Explorer", which I wrote and had published in the 1980s for the TRS-80, Sinclair Spectrum and BBC Micro. It is partly a simulation of the sport of orienteering and an aid to interpreting contour maps but there are several diversions for non-orienteers, including a challenging treasure hunt. It is set in a vast forest, effectively infinite. This rewrite demonstrates how powerful the HTML5/JavaScript platform can be. Detailed maps and complicated scenes are displayed in fractions of a second. The program should run on any device that has a browser, from desktop PC to smartphone; it works on my cheap Android phone even in battery-saver mode. A friend tells me it works on her Kindle tablet. Older devices may not be fast enough though. Despite the huge extent of the terrain, the game downloads and runs in seconds because the program is only about 120 kilobytes (yes, kilo!). And there is nothing to install. I do not use any third party libraries. I deliberately use only the basic 2D graphics context because others, such as WebGL, are not available on all devices. Please feed back to me (gr at grelf dot net) details of any device that you find it doesn't work on. Tell me make and model, and particularly the operating system and browser (with version), and what doesn't work. The program and its source are freely available, uncompressed, because I want to encourage others to program creatively on the HTML5/JavaScript platform. I am writing on my personal web site about how it works. Lots of background information can be found at and other pages linked from there, including my free JavaScript course. I think HTML5/JavaScript is a good combination for rewriting retro games and making them available to all, rather than emulating the cumbersome old machines in software or physically reproducing them, as some are doing. Reprogramming is more work of course but it can be very satisfying (and JavaScript is much easier to write than assembler). The technology now makes it possible to write what I really had in mind back in the 80s.