Made the iffexport utility (design based on libfar), added in an HTML5 z-buffer renderer, and made tags for libfar 1.0.0 and libvitaboy 1.0.0.

This commit is contained in:
Fatbag 2012-03-04 13:28:34 -06:00
parent f23bcd7b4d
commit 432d12397c
15 changed files with 671 additions and 0 deletions

View file

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 2.6)
project(FileHandler)
add_subdirectory(iff)
add_subdirectory(libexpat)
add_subdirectory(libfar)
add_subdirectory(libjpeg-turbo)

View file

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.6)
project(iff)
set(IFF_SOURCES
chunks.c
iff.c
iffexport.c
)
add_executable(iffexport ${IFF_SOURCES})

View file

@ -0,0 +1,24 @@
/*
chunks.c - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "iff.h"
int iff_read_rsmp(IFFChunk * ChunkInfo, const uint8_t * Buffer, unsigned MaxChunkSize, unsigned IFFSize){
return 1;
}

View file

@ -0,0 +1,150 @@
/*
iff.c - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "iff.h"
#ifndef __inline
#define __inline
#endif
#ifndef __restrict
#define __restrict
#endif
IFFFile * iff_create()
{
IFFFile *ptr = malloc(sizeof(IFFFile));
if(ptr == NULL) return NULL;
memset(ptr, 0, sizeof(IFFFile));
return ptr;
}
int iff_read_header(IFFFile * IFFFileInfo, const uint8_t * Buffer, unsigned FileSize)
{
unsigned offset;
if(!FileSize) FileSize = ~0;
else if(FileSize < 64)
return 0;
if(memcmp(Buffer, Header_IFF, 60))
return 0;
memcpy(IFFFileInfo->Header, Buffer, 60);
offset = read_uint32be(Buffer+60);
if(offset > FileSize - 28)
return 0;
return 1;
}
IFFChunkNode * iff_add_chunk(IFFFile * IFFFileInfo, int Position)
{
IFFChunkNode *ptr = malloc(sizeof(IFFChunkNode)), *node;
if(ptr == NULL) return NULL;
memset(ptr, 0, sizeof(IFFChunkNode));
if(IFFFileInfo == NULL) return ptr;
if(Position >= 0){
node = IFFFileInfo->FirstChunk;
if(node == NULL){
IFFFileInfo->FirstChunk = ptr;
IFFFileInfo->LastChunk = ptr;
}else{
/* Find the node we will take the place of */
while(Position-- && node->NextChunk != NULL)
node = node->NextChunk;
if(node->PrevChunk == NULL)
IFFFileInfo->FirstChunk = ptr;
/* Shift this node and all nodes after it above us */
ptr->PrevChunk = node->PrevChunk;
ptr->NextChunk = node;
node->PrevChunk = ptr;
}
}else{
node = IFFFileInfo->LastChunk;
if(node == NULL){
IFFFileInfo->FirstChunk = ptr;
IFFFileInfo->LastChunk = ptr;
}else{
/* Find the node we will take the place of */
while(++Position && node->PrevChunk != NULL)
node = node->PrevChunk;
if(node->NextChunk == NULL)
IFFFileInfo->LastChunk = ptr;
/* Shift this node and all nodes before it below us */
ptr->PrevChunk = node;
ptr->NextChunk = node->NextChunk;
node->NextChunk = ptr;
}
}
IFFFileInfo->ChunkCount++;
return ptr;
}
int iff_read_chunk(IFFChunk * ChunkInfo, const uint8_t * Buffer, unsigned MaxChunkSize)
{
if(MaxChunkSize == 0) MaxChunkSize = ~0;
if(MaxChunkSize < 76)
return 0;
memcpy(ChunkInfo->Type, Buffer+0, 4);
ChunkInfo->Size = read_uint32be(Buffer+4);
ChunkInfo->ChunkID = read_uint16be(Buffer+8);
ChunkInfo->Flags = read_uint16be(Buffer+10);
memcpy(ChunkInfo->Label, Buffer+12, 64);
if(ChunkInfo->Size < 76 || ChunkInfo->Size > MaxChunkSize)
return 0;
if(ChunkInfo->Size > 76){
ChunkInfo->Data = malloc(ChunkInfo->Size - 76);
if(ChunkInfo->Data == NULL)
return 0;
memcpy(ChunkInfo->Data, Buffer+76, ChunkInfo->Size - 76);
}
return 1;
}
int iff_enumerate_chunks(IFFFile * IFFFileInfo, const uint8_t * Buffer, unsigned BufferSize)
{
while(BufferSize){
IFFChunkNode * chunk = iff_add_chunk(IFFFileInfo, -1);
if(chunk == NULL)
return 0;
if(!iff_read_chunk(&chunk->Chunk, Buffer, BufferSize)){
free(chunk);
return 0;
}
Buffer += chunk->Chunk.Size;
BufferSize -= chunk->Chunk.Size;
}
return 1;
}

View file

@ -0,0 +1,85 @@
/*
iff.h - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef read_uint32be
#define read_uint32be(x) (unsigned)(((x)[0]<<(8*3)) | ((x)[1]<<(8*2)) | ((x)[2]<<(8*1)) | ((x)[3]<<(8*0)))
#define read_uint24be(x) (unsigned)(((x)[0]<<(8*2)) | ((x)[1]<<(8*1)) | ((x)[2]<<(8*0)))
#define read_uint16be(x) (unsigned)(((x)[0]<<(8*1)) | ((x)[1]<<(8*0)))
#define read_uint8be(x) (unsigned)(((x)[0]<<(8*0)))
#define read_uint32le(x) (unsigned)(((x)[0]<<(8*0)) | ((x)[1]<<(8*1)) | ((x)[2]<<(8*2)) | ((x)[3]<<(8*3)))
#define read_uint24le(x) (unsigned)(((x)[0]<<(8*0)) | ((x)[1]<<(8*1)) | ((x)[2]<<(8*2)))
#define read_uint16le(x) (unsigned)(((x)[0]<<(8*0)) | ((x)[1]<<(8*1)))
#define read_uint8le(x) (unsigned)(((x)[0]<<(8*0)))
#endif
typedef struct IFFChunk_struct
{
char Type[5];
uint32_t Size;
uint16_t ChunkID;
uint16_t Flags;
char Label[65];
uint8_t * Data;
void * FormattedData;
} IFFChunk;
typedef struct IFFChunkNode_struct
{
IFFChunk Chunk;
struct IFFChunkNode_struct * PrevChunk;
struct IFFChunkNode_struct * NextChunk;
} IFFChunkNode;
typedef struct IFFFile_struct
{
uint8_t Header[64];
uint32_t ChunkCount;
IFFChunkNode * FirstChunk;
IFFChunkNode * LastChunk;
IFFChunkNode * ResourceMap;
} IFFFile;
static const uint8_t Header_IFF[] = "IFF FILE 2.5:TYPE FOLLOWED BY SIZE\0 JAMIE DOORNBOS & MAXIS 1";
#ifdef __cplusplus
extern "C" {
#endif
/*
** IFF file functions
*/
IFFFile * iff_create();
int iff_read_header(IFFFile * IFFFileInfo, const uint8_t * Buffer, unsigned FileSize);
IFFChunkNode * iff_add_chunk(IFFFile * IFFFileInfo, int Position);
int iff_read_chunk(IFFChunk * ChunkInfo, const uint8_t * Buffer, unsigned MaxChunkSize);
int iff_enumerate_chunks(IFFFile * IFFFileInfo, const uint8_t * Buffer, unsigned BufferSize);
void iff_delete_chunk(IFFFile * IFFFileInfo, int Position);
void iff_delete(IFFFile * IFFFileInfo);
/*
** IFF chunk functions
*/
int iff_read_rsmp(IFFChunk * ChunkInfo, const uint8_t * Buffer, unsigned MaxChunkSize, unsigned IFFSize);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,142 @@
/*
iffexport.c - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include "iff.h"
int main(int argc, char *argv[]){
HANDLE hFile;
int overwrite = 0;
char *InFile, *OutDirectory;
HANDLE ProcessHeap = GetProcessHeap();
DWORD FileSize;
DWORD bytestransferred = 0;
uint8_t * IFFData;
unsigned chunkcount, chunk = 0;
IFFFile * IFFFileInfo;
IFFChunkNode * ChunkNode;
if(argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
printf("Usage: iffexport [-f] infile OutDirectory\n"
"Export the resources of an EA IFF file.\n"
"Use -f to force overwriting without confirmation.\n"
"\n"
"Report bugs to <X-Fi6@phppoll.org>.\n"
"iffexport is maintained by the Niotso project.\n"
"Home page: <http://www.niotso.org/>\n");
return 0;
}
if(argc >= 4 && !strcmp(argv[1], "-f")){
overwrite++;
InFile = argv[2];
OutDirectory = argv[3];
}else{
InFile = argv[1];
OutDirectory = argv[2];
}
/****
** Open the file and read in entire contents to memory
*/
hFile = CreateFile(InFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hFile == INVALID_HANDLE_VALUE){
if(GetLastError() == ERROR_FILE_NOT_FOUND){
printf("%sThe specified input file does not exist.", "iffexport: error: ");
return -1;
}
printf("%sThe input file could not be opened for reading.", "iffexport: error: ");
return -1;
}
FileSize = GetFileSize(hFile, NULL);
if(FileSize < 64){
printf("%sNot a valid IFF file.", "iffexport: error: ");
return -1;
}
IFFData = HeapAlloc(ProcessHeap, HEAP_NO_SERIALIZE, FileSize);
if(IFFData == NULL){
printf("%sMemory for this file could not be allocated.", "iffexport: error: ");
return -1;
}
if(!ReadFile(hFile, IFFData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
printf("%sThe input file could not be read.", "iffexport: error: ");
return -1;
}
CloseHandle(hFile);
/****
** Load header information
*/
IFFFileInfo = iff_create();
if(IFFFileInfo == NULL){
printf("%sMemory for this file could not be allocated.", "iffexport: error: ");
return -1;
}
if(!iff_read_header(IFFFileInfo, IFFData, FileSize)){
printf("%sNot a valid IFF file.", "iffexport: error: ");
return -1;
}
/****
** Load entry information
*/
if(!iff_enumerate_chunks(IFFFileInfo, IFFData+64, FileSize-64)){
printf("%sChunk data is corrupt.", "iffexport: error: ");
return -1;
}
chunkcount = IFFFileInfo->ChunkCount;
printf("This IFF file contains %u chunks.\n\nExporting\n", chunkcount);
/****
** Extract each entry
*/
for(ChunkNode = IFFFileInfo->FirstChunk; ChunkNode; ChunkNode = ChunkNode->NextChunk){
char name[256], destination[256];
char filter[] = "\\/:*?\"<>|";
int i;
chunk++;
sprintf(name, "%03u-%s-%04X-%s", chunk, ChunkNode->Chunk.Type, ChunkNode->Chunk.ChunkID, ChunkNode->Chunk.Label);
for(i=0; i<9; i++){
char * c = name;
while((c = strchr(c, filter[i])) != NULL)
*c = '.';
}
sprintf(destination, "%s/%s.%s", OutDirectory, name, (!memcmp(ChunkNode->Chunk.Type, "BMP_", 4)) ? "bmp" : "dat");
hFile = CreateFile(destination, GENERIC_WRITE, 0, NULL, CREATE_NEW+overwrite,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hFile == INVALID_HANDLE_VALUE){
printf(" (%u/%u) Skipped (%s): %s\n", chunk, chunkcount,
(!overwrite && GetLastError() == ERROR_FILE_EXISTS) ? "file exists" : "could not open",
name);
continue;
}
printf(" (%u/%u) %s (%u bytes)\n", chunk, chunkcount, name, ChunkNode->Chunk.Size-76);
WriteFile(hFile, ChunkNode->Chunk.Data, ChunkNode->Chunk.Size-76, &bytestransferred, NULL);
CloseHandle(hFile);
}
return 0;
}

View file

@ -0,0 +1 @@
Fatbag <X-Fi6@phppoll.org>

View file

@ -0,0 +1,8 @@
libvitaboy 1.0.0 (2012-03-04) Fatbag
* first stable release
* read support for ANIM, APR, BND, COL, HAG, MESH, OFT, PO, SKEL
* working rendering and animation of characters in OpenGL
* basic keyboard controls
* support for environments: MinGW
* support for platforms: i686, x86-64

View file

@ -0,0 +1,13 @@
libfar - Copyright (c) 2011-2012 Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View file

@ -0,0 +1,67 @@
***************************************************************************
* ** libvitaboy - Open source OpenGL TSO character animation library ** *
* ** Copyright (c) 2012 Drew D'Addesio ("Fatbag", <X-Fi6@phppoll.org>) ** *
* *
* Use and distribution of all files in the "include" and "lib" folders *
* are governed under the ISC License, included with this source code in *
* "COPYING". *
* *
* All other files included but not aforementioned are not copyrighted, *
* and are distributed into the public domain. *
***************************************************************************
libvitaboy is a cross-platform C++ library for parsing the various character rendering formats used in The Sims Online and using
that information to render and animate the 3D characters of the game using OpenGL.
Formats include:
(*) ANIM (Animation)
(*) APR (Appearance)
(*) BND (Binding)
(*) COL (Collection)
(*) HAG (Hand Group)
(*) MESH (Mesh)
(*) OFT (Outfit)
(*) PO (Purchasable Outfit)
(*) SKEL (Skeleton)
1. What this library will do for you
You can use this library to render a character in a fixed location.
2. Compiling
Compiling is straightforward. The Makefiles and project files should already be set up for you, and are easy to tweak.
Before compiling the library, make your changes to config.h to specify what's compiled in.
a. Compiling from a Makefile
Makefiles are listed in the top directory in the format Makefile.$(PLATFORM). Just copy the one applicable to your platform
to the destination file "Makefile" and run GNU make.
b. Compiling from a project file
Project files are held within their own directories. They can be opened up with the target IDE and be compiled by
invoking the compile action of that IDE.
c. Compiling using your own Makefile or project file
It's possible no such file exists for your platform. In this case, look at whichever Makefile is most relevant to your
platform, copy all source files down (.cpp, .hpp, ...), and add it your own Makefile or project. In theory,
it should work. :)
libvitaboy is intended to work with any C++ compiler. If not, and the compiler is not only modern and popular but also free
software (gratis, libre, open source software), please report it as a bug.
3. Useful links
libvitaboy is hosted by and maintained by the Niotso project.
Home page: <http://www.niotso.org/>
Don Hopkins documentation:
(*) http://www.donhopkins.com/drupal/node/20 [Sims Character Animation File Format]
(*) http://www.donhopkins.com/drupal/node/21 [Details on The Sims Character Animation File Format and Rendering]
(*) http://www.donhopkins.com/drupal/node/19 [Sims VitaBoy Character Animation Library Documentation]
(*) http://www.donhopkins.com/home/movies/ [Index of Archived Streaming QuickTime Movies]

View file

@ -0,0 +1,10 @@
--------------------------------|----------------------------------------------------------------
| Thanks to | for |
|--------------------------------|----------------------------------------------------------------|
| Don Hopkins | Kickstarting The Sims 1 customization, encouraging fine |
| | individuals to document the game, and most importantly, |
| | sharing with us his documentation of his VitaBoy skeletal |
| | character animation library used in The Sims 1, in the form |
| | of articles and videos. Dozens of thanks from each one of us. |
| (*) <http://www.donhopkins.com/> |
-------------------------------------------------------------------------------------------------

View file

View file

@ -0,0 +1,8 @@
Add these files:
* Chair-Liv-Deco_large_front_a.png
* Chair-Liv-Deco_large_front_p.png
* Chair-Liv-Deco_large_front_z.png
* tower_large_ne_a.png
* tower_large_ne_p.png
* tower_large_ne_z.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

152
Tools/zbuffer/index.html Normal file
View file

@ -0,0 +1,152 @@
<script>
window.onload = init;
function LoadSpriteset(names) {
images = new Array(names.length);
imagesloaded = 0;
imagecount = 1 + names.length * 3;
ext = ['a', 'p', 'z'];
for (i = 0; i < names.length; i++) {
images[i] = new Array(3);
for (j = 0; j < 3; j++) {
img = new Image();
img.idi = i, img.idj = j;
img.src = names[i] + '_' + ext[j] + ".png";
img.onload = function() {
context.drawImage(this, 0, 0);
images[this.idi][this.idj] = context.getImageData(0, 0, 136, 384);
delete this;
if (++imagesloaded == imagecount) {
redraw = true;
setInterval(update, 16);
}
};
}
}
}
function init() {
// capture keys
left = 0;
right = 0;
up = 0;
down = 0;
window.onkeydown = function(evt) {
if (evt.keyCode == 37) left = 1;
if (evt.keyCode == 38) up = 1;
if (evt.keyCode == 39) right = 1;
if (evt.keyCode == 40) down = 1;
};
window.onkeyup = function(evt) {
if (evt.keyCode == 37) left = 0;
if (evt.keyCode == 38) up = 0;
if (evt.keyCode == 39) right = 0;
if (evt.keyCode == 40) down = 0;
};
// end capture keys
// The game does not use true isometric projection because each tile is 128x64 (close), 64x32 (medium), or 32x16 (far)
// which, when tilex or tiley is the hypotenuse, does not form a 30-60-90 triangle.
// It's actually arctan(1/2)-arctan(2/1)-90. The image is compressed vertically a bit to allow for power-of-2 texture sizes.
tilex = 0;
tiley = 0;
sin60 = 2.0/Math.sqrt(5); // sin(arctan(2)) or cos(arctan(1/2))
sin30 = 1.0/Math.sqrt(5); // sin(arctan(1/2)) or cos(arctan(2))
sqrt5120 = 32*Math.sqrt(5);
debug = document.getElementById('debug');
canvas = document.getElementById('draw');
context = canvas.getContext('2d');
background = new Image();
background.src = "background.png";
background.onload = function() {
context.drawImage(this, 0, 0);
delete this;
background = context.getImageData(0, 0, 136, 384);
if (++imagesloaded == imagecount) {
redraw = true;
setInterval(update, 16);
}
}
LoadSpriteset(["Chair-Liv-Deco_large_front", "tower_large_ne"]);
render = context.createImageData(136, 384);
}
function Blend(sourceid, soffset)
{
sa = images[sourceid][0].data[soffset+0] / 255.0;
if (sa == 0) return;
sr = images[sourceid][1].data[soffset+0];
sg = images[sourceid][1].data[soffset+1];
sb = images[sourceid][1].data[soffset+2];
if (sa == 255) {
render.data[dest+0] = sr;
render.data[dest+1] = sr;
render.data[dest+2] = sr;
return;
}
dr = render.data[dest+0];
dg = render.data[dest+1];
db = render.data[dest+2];
da = 1.0 - sa;
render.data[dest+0] = da * dr + sa * sr;
render.data[dest+1] = da * dg + sa * sg;
render.data[dest+2] = da * db + sa * sb;
}
function update() {
if (!left && !right && !up && !down && !redraw) return;
redraw = false;
if (left == 1) tilex -= 1;
if (right == 1) tilex += 1;
if (up == 1) tiley += 1;
if (down == 1) tiley -= 1;
screenx = Math.round((tilex + tiley)*sin60);
screeny = Math.round((tilex - tiley)*sin30);
z = screeny*1.5;
for (y = 0; y < 384; y++) {
for (x = 0; x < 136; x++) {
dest = (y*136+x)*4;
render.data[dest+0] = background.data[dest+0];
render.data[dest+1] = background.data[dest+1];
render.data[dest+2] = background.data[dest+2];
render.data[dest+3] = 255;
// Shift: read from the data so-many bytes ago. Subtract rather than add.
if (y-screeny >= 0 && y-screeny < 384 && x-screenx >= 0 && x-screenx < 136) {
source = ((y-screeny)*136 + x-screenx)*4;
if (images[1][2].data[source] != 255 && images[1][2].data[source] - z < images[0][2].data[dest]) {
// Object 2 on top
Blend(0, dest);
Blend(1, source);
} else {
// Object 1 on top
Blend(1, source);
Blend(0, dest);
}
} else {
// Object 1 only
Blend(0, dest);
}
}
}
context.putImageData(render, 0, 0);
debug.innerHTML = "Tile x: " + tilex/sqrt5120 + "<br />Tile y: " + tiley/sqrt5120 + "<br />Screen x: " + screenx + "<br />Screen y: " + screeny + "<br />z: " + z;
}
</script>
<div id="debug"></div>
<canvas width="136" height="384" id="draw" style="border: 1px solid #888"></canvas>