diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2007-07-24 22:53:43 +0000 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2007-07-24 22:53:43 +0000 |
| commit | 5bd2c434f63ff4a039e854d647f02ef660c544d1 (patch) | |
| tree | 3fe99b2e4a7cf5e80243bde4583e10c5650ed913 /src/tools | |
| parent | 4b098f7711e7cb96eef6797787e3f2f2b4cbb867 (diff) | |
| download | zcatch-5bd2c434f63ff4a039e854d647f02ef660c544d1.tar.gz zcatch-5bd2c434f63ff4a039e854d647f02ef660c544d1.zip | |
epic commit. removed tga support, removed BGR support. fixed one config for editor, server and client, optimized tilemap rendering (this needs some cleanup), added tools to fix alpha outline quirk and glitches in the tilemap reindering
Diffstat (limited to 'src/tools')
| -rw-r--r-- | src/tools/crapnet.cpp | 126 | ||||
| -rw-r--r-- | src/tools/dilate.c | 98 | ||||
| -rw-r--r-- | src/tools/tileset_borderfix.c | 83 |
3 files changed, 307 insertions, 0 deletions
diff --git a/src/tools/crapnet.cpp b/src/tools/crapnet.cpp new file mode 100644 index 00000000..f87803b5 --- /dev/null +++ b/src/tools/crapnet.cpp @@ -0,0 +1,126 @@ +#include <baselib/system.h> +#include <baselib/network.h> + +#include <cstdlib> + +using namespace baselib; + +struct packet +{ + packet *prev; + packet *next; + + netaddr4 send_to; + int64 timestamp; + int id; + int data_size; + char data[1]; +}; + +static packet *first = (packet *)0; +static packet *last = (packet *)0; +static int current_latency = 0; +static int debug = 0; + +int run(int port, netaddr4 dest) +{ + netaddr4 src(0,0,0,0,0); + socket_udp4 socket; + socket.open(port); + char buffer[1024*2]; + int id = 0; + + while(1) + { + // handle incomming packets + while(1) + { + // fetch data + netaddr4 from; + int bytes = socket.recv(&from, buffer, 1024*2); + if(bytes <= 0) + break; + + // create new packet + packet *p = (packet *)mem_alloc(sizeof(packet)+bytes, 1); + + if(from == dest) + p->send_to = src; + else + { + src = from; + p->send_to = dest; + } + + // queue packet + p->prev = last; + p->next = 0; + if(last) + last->next = p; + else + { + first = p; + last = p; + } + last = p; + + // set data in packet + p->timestamp = time_get(); + p->data_size = bytes; + p->id = id++; + mem_copy(p->data, buffer, bytes); + + if(debug) + dbg_msg("crapnet", "<< %08d %d.%d.%d.%d:%5d (%d)", p->id, from.ip[0], from.ip[1], from.ip[2], from.ip[3], from.port, p->data_size); + } + + // + while(1) + { + //dbg_msg("crapnet", "%p", first); + if(first && (time_get()-first->timestamp) > current_latency) + { + packet *p = first; + first = first->next; + if(first) + first->prev = 0; + else + last = 0; + + if(debug) + { + dbg_msg("crapnet", ">> %08d %d.%d.%d.%d:%5d (%d)", p->id, + p->send_to.ip[0], p->send_to.ip[1], + p->send_to.ip[2], p->send_to.ip[3], + p->send_to.port, p->data_size); + } + + // send and remove packet + //if((rand()%10) != 0) // heavy packetloss + socket.send(&p->send_to, p->data, p->data_size); + + // update lag + double flux = rand()/(double)RAND_MAX; + int ms_spike = 0; + int ms_flux = 100; + int ms_ping = 50; + current_latency = ((time_freq()*ms_ping)/1000) + (int64)(((time_freq()*ms_flux)/1000)*flux); // 50ms + + if((p->id%100) == 0) + current_latency += (time_freq()*ms_spike)/1000; + + mem_free(p); + } + else + break; + } + + thread_sleep(1); + } +} + +int main(int argc, char **argv) +{ + run(8302, netaddr4(127,0,0,1,8303)); + return 0; +} diff --git a/src/tools/dilate.c b/src/tools/dilate.c new file mode 100644 index 00000000..f53e7656 --- /dev/null +++ b/src/tools/dilate.c @@ -0,0 +1,98 @@ + +#include "../engine/client/pnglite/pnglite.c" + +typedef struct pixel_t +{ + unsigned char r, g, b, a; +} pixel; + +static int clamp(int v, int min, int max) +{ + if(v > max) + return max; + if(v < min) + return min; + return v; +} + +static void dilate(int w, int h, pixel *src, pixel *dst) +{ + int x, y, k, m, c; + int ix, iy; + + m = 0; + for(y = 0; y < h; y++) + { + for(x = 0; x < w; x++, m++) + { + dst[m] = src[m]; + if(src[m].a) + continue; + + const int xo[] = {0, -1, 1, 0}; + const int yo[] = {-1, 0, 0, 1}; + for(c = 0; c < 4; c++) + { + ix = clamp(x + xo[c], 0, w-1); + iy = clamp(y + yo[c], 0, h-1); + k = iy*w+ix; + if(src[k].a) + { + dst[m] = src[k]; + dst[m].a = 255; + break; + } + } + } + } +} + +static void copy_alpha(int w, int h, pixel *src, pixel *dst) +{ + int x, y, m; + + for(y = 0; y < h; y++) + for(x = 0; x < w; x++, m++) + dst[m].a = src[m].a; +} + +int main(int argc, char **argv) +{ + png_t png; + pixel *buffer[3] = {0,0,0}; + int i, w, h; + + png_init(0,0); + png_open_file(&png, argv[1]); + + if(png.color_type != PNG_TRUECOLOR_ALPHA) + { + printf("not an RGBA image\n"); + return -1; + } + + buffer[0] = (pixel*)malloc(png.width*png.height*sizeof(pixel)); + buffer[1] = (pixel*)malloc(png.width*png.height*sizeof(pixel)); + buffer[2] = (pixel*)malloc(png.width*png.height*sizeof(pixel)); + png_get_data(&png, (unsigned char *)buffer[0]); + png_close_file(&png); + + w = png.width; + h = png.height; + + dilate(w, h, buffer[0], buffer[1]); + for(i = 0; i < 5; i++) + { + dilate(w, h, buffer[1], buffer[2]); + dilate(w, h, buffer[2], buffer[1]); + } + + copy_alpha(w, h, buffer[0], buffer[1]); + + // save here + png_open_file_write(&png, argv[1]); + png_set_data(&png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)buffer[1]); + png_close_file(&png); + + return 0; +} diff --git a/src/tools/tileset_borderfix.c b/src/tools/tileset_borderfix.c new file mode 100644 index 00000000..ba7a40f0 --- /dev/null +++ b/src/tools/tileset_borderfix.c @@ -0,0 +1,83 @@ + +#include "../engine/client/pnglite/pnglite.c" + +typedef struct pixel_t +{ + unsigned char r, g, b, a; +} pixel; + +static pixel sample(int x, int y, int w, int h, pixel *data, int pitch, float u, float v) +{ + x = x + (int)(w*u); + y = y + (int)(h*v); + return data[y*pitch+x]; +} + +static void tilemap_borderfix(int w, int h, pixel *src, pixel *dst) +{ + int tilew = w/16; + int tileh = h/16; + int tx, ty; + int x, y; + int k; + float u, v; + + memset(dst, 0, sizeof(pixel)*w*h); + + for(ty = 0; ty < 16; ty++) + for(tx = 0; tx < 16; tx++) + { + for(y = 0; y < tileh-2; y++) + for(x = 0; x < tilew-2; x++) + { + u = 0.5f/tilew + x/(float)(tilew-2); + v = 0.5f/tileh + y/(float)(tileh-2); + k = (ty*tileh+1+y)*w + tx*tilew+x+1; + dst[k] = sample(tx*tilew, ty*tileh, tilew, tileh, src, w, u, v); + + if(x == 0) dst[k-1] = dst[k]; + if(x == tilew-2-1) dst[k+1] = dst[k]; + if(y == 0) dst[k-w] = dst[k]; + if(y == tileh-2-1) dst[k+w] = dst[k]; + + if(x == 0 && y == 0) dst[k-w-1] = dst[k]; + if(x == tilew-2-1 && y == 0) dst[k-w+1] = dst[k]; + if(x == 0 && y == tileh-2-1) dst[k+w-1] = dst[k]; + if(x == tilew-2-1 && y == tileh-2-1) dst[k+w+1] = dst[k]; + } + } +} + + +int main(int argc, char **argv) +{ + png_t png; + pixel *buffer[2] = {0,0}; + int w, h; + + png_init(0,0); + png_open_file(&png, argv[1]); + + if(png.color_type != PNG_TRUECOLOR_ALPHA) + { + printf("not an RGBA image\n"); + return -1; + } + + w = png.width; + h = png.height; + + buffer[0] = (pixel*)malloc(w*h*sizeof(pixel)); + buffer[1] = (pixel*)malloc(w*h*sizeof(pixel)); + png_get_data(&png, (unsigned char *)buffer[0]); + png_close_file(&png); + + tilemap_borderfix(w, h, buffer[0], buffer[1]); + + // save here + png_open_file_write(&png, "output.png"); + png_set_data(&png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)buffer[1]); + png_close_file(&png); + + return 0; +} |