From b8fb1400b12c4e491f94b79a2f0a57f187c8e91d Mon Sep 17 00:00:00 2001 From: tkarrass Date: Wed, 13 Nov 2013 20:48:15 +0100 Subject: [PATCH] did it - it's animated ;) --- src/display.c | 5 ++++- src/image.c | 37 +++++++++++++++++++++++++++++-------- src/image.h | 2 +- src/main.c | 6 +++--- src/msg.c | 10 ++++++---- src/msg.h | 3 +-- src/net.c | 3 ++- src/net.h | 2 +- 8 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/display.c b/src/display.c index faeba70..a5ad5f6 100644 --- a/src/display.c +++ b/src/display.c @@ -65,10 +65,12 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) { static char *img = NULL; static int w; static int h; + static int f; if (!img && msg->image) { img = msg->image; w = msg->width; h = msg->height; + f = msg->frames; } if (!img) { printf("awaiting state ... %d\r", msg->timestamp); @@ -97,13 +99,14 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) { // printf("loop\n"); int rowoffset = (rows-h)/2; //int coloffset = left-frame; + int imgoff = (frame % f) * w * h; for (int y=0; yheight; y++) { for (int x=left-frame; x=w?' ':img[y*w+p]); + mvaddch(y + rowoffset, x, p>=w-1?' ':img[imgoff+(y*w+p)]); } } diff --git a/src/image.c b/src/image.c index 233cc7b..31bc33e 100644 --- a/src/image.c +++ b/src/image.c @@ -7,7 +7,7 @@ #define LINELEN 20000 -char *readImage(const char* filename, int *cols, int *rows) { +char *readImage(const char* filename, int *cols, int *rows, int *imgz) { printf("fopen\n"); FILE *f = fopen(filename, "r"); @@ -15,30 +15,50 @@ char *readImage(const char* filename, int *cols, int *rows) { *cols = 0; *rows = 0; + int maxrows = 0; + *imgz = 1; char *linebuf = (char *)malloc(LINELEN); char *ret = NULL; if (f) { while (fgets(linebuf, LINELEN, f)) { int len = strlen(linebuf); + if ( len > 2 && linebuf[0] == '.' && linebuf[1] == '.') { + (*imgz)++; + (*rows)=0; + } *cols = len>*cols?len:*cols; (*rows)++; + maxrows = (*rows>maxrows)?*rows:maxrows; } + (*rows)=maxrows; (*cols)--; - ret = (char *)malloc(*cols * *rows + 1); - memset(ret, ' ', *cols * *rows); + ret = (char *)malloc(*imgz * *cols * *rows + 1); + memset(ret, ' ', *imgz * *cols * *rows); fseek(f, 0, SEEK_SET); - for (int r=0; r<*rows; r++) { - fgets(&ret[r * *cols], LINELEN, f); + + for (int fp=0; fp<(*imgz); fp++) { + for (int r=0; r<*rows; r++) { + int base = (fp * *rows * *cols) + r * *cols; + fgets(&ret[base], LINELEN, f); + if (ret[base] == '.' && ret[base+1] == '.') { + ret[base] = ' '; + ret[base+1] = ' '; + break; + } + } } - + for (int i=0; i < (*imgz * *cols * *rows); i++) + if (ret[i] == '\n' || ret[i] == '\r') + ret[i] = ' '; + fclose(f); } free(linebuf); - printf("image size: %d x %d\n", *cols, *rows); + printf("image size: %d x %d\n%d frames\n", *cols, *rows, *imgz); - for (int y=0; y < *rows; y++) { + for (int y=0; y < *rows; y++) { for (int x=0; x < *cols; x++) { //mvaddch(y,x,img[y * *cols +x]); printf("%c", ret[y * *cols +x]); @@ -49,3 +69,4 @@ char *readImage(const char* filename, int *cols, int *rows) { return ret; } + diff --git a/src/image.h b/src/image.h index b3b7ba0..75f8e0b 100644 --- a/src/image.h +++ b/src/image.h @@ -1,6 +1,6 @@ #ifndef __IMAGE #define __IMAGE -char *readImage(const char* filename, int *cols, int *rows); +char *readImage(const char* filename, int *cols, int *rows, int *imgz); #endif diff --git a/src/main.c b/src/main.c index 4afa0fc..2724f5b 100644 --- a/src/main.c +++ b/src/main.c @@ -123,14 +123,14 @@ int main(int argc, char **argv) { printf("no image given\n"); return -12; } - int w, h; - char *image = readImage(prog_info.filename, &w, &h); + int w, h, i; + char *image = readImage(prog_info.filename, &w, &h, &i); if (!image) { printf("could not read image!\n"); return -13; } prog_info.width = prog_info.width>(w*2)?prog_info.width:(w*2); - ret = run_server(&prog_info, image, w, h); + ret = run_server(&prog_info, image, w, h, i); } else { printf("running in CLIENT mode, using client offset %d\n", prog_info.client_offset); signal(SIGINT,&die); diff --git a/src/msg.c b/src/msg.c index 7bc8f46..aff3a2f 100644 --- a/src/msg.c +++ b/src/msg.c @@ -6,24 +6,26 @@ #include "msg.h" int getBufferSize(struct message *msg) { - return 3*sizeof(uint32_t) + msg->width * msg->height; + return 4*sizeof(uint32_t) + msg->width * msg->height * msg->frames; } void serialize (char *buf, struct message *msg) { memcpy(&buf[0], &msg->timestamp, 4); memcpy(&buf[4], &msg->width, 4); memcpy(&buf[8], &msg->height, 4); + memcpy(&buf[12], &msg->frames, 4); if (msg->width * msg->height) - memcpy(&buf[12], msg->image, msg->width * msg->height); + memcpy(&buf[16], msg->image, msg->frames * msg->width * msg->height); } void deserialize (struct message *msg, const char *buf) { memcpy(&msg->timestamp, &buf[0], 4); memcpy(&msg->width, &buf[4], 4); memcpy(&msg->height, &buf[8], 4); + memcpy(&msg->frames, &buf[12], 4); if (msg->width * msg->height) { - msg->image = (char*) malloc(msg->width * msg->height); - memcpy(msg->image, &buf[12], msg->width * msg->height); + msg->image = (char*) malloc(msg->width * msg->height * msg->frames); + memcpy(msg->image, &buf[16], msg->width * msg->height * msg->frames); } else { msg->image = NULL; } diff --git a/src/msg.h b/src/msg.h index 4998e8c..b90056c 100644 --- a/src/msg.h +++ b/src/msg.h @@ -12,10 +12,9 @@ struct message { uint32_t timestamp; uint32_t width; // normally 80 uint32_t height; // normally 25, may vary + uint32_t frames; // number of frames //char **image; // dimension is width x height char *image; // dimension is width x height - //TODO: - // Make image anim capable /* * image[row][col] >>> image[row*width+col]; */ diff --git a/src/net.c b/src/net.c index 98b0979..134be9d 100644 --- a/src/net.c +++ b/src/net.c @@ -37,7 +37,7 @@ static void *get_in_addr(struct sockaddr *sa) { // server mode // pumpt im for(;;) den status ins eth // -int run_server(const struct prog_info *pinfo, char *img, int w, int h) { +int run_server(const struct prog_info *pinfo, char *img, int w, int h, int frms) { struct addrinfo hints, *servinfo, *p; int ret; int sockfd; @@ -91,6 +91,7 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) { bool sendimg = (t % 100) == 0; outmsg->width = sendimg ? w : 0; outmsg->height = sendimg ? h : 0; + outmsg->frames = sendimg ? frms: 0; outmsg->image = sendimg ? img : NULL; int buflen = getBufferSize(outmsg); diff --git a/src/net.h b/src/net.h index 7a4e9ae..f2546e9 100644 --- a/src/net.h +++ b/src/net.h @@ -6,7 +6,7 @@ #include "msg.h" // message serialization #include "display.h" // callback -int run_server(const struct prog_info *pinfo, char *img, int w, int h); +int run_server(const struct prog_info *pinfo, char *img, int w, int h, int frms); int run_client(const struct prog_info *pinfo,void(*framecallback)(const struct message *, const struct prog_info *)); #endif