From a5d9dde24327755ec4c6068963f2c6ba0f71db94 Mon Sep 17 00:00:00 2001 From: epoch Date: Wed, 1 Nov 2017 02:53:21 -0500 Subject: fixed memory leak in load_stdin() and made it so world changes from load_stdin() will trigger a redraw --- src/hackvr.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'src/hackvr.c') diff --git a/src/hackvr.c b/src/hackvr.c index cdc5084..30fe173 100644 --- a/src/hackvr.c +++ b/src/hackvr.c @@ -53,7 +53,7 @@ char *read_line_hack(FILE *fp,int len) { //warning: clobbers input //skips leading and trailing space. //compresses multiple spaces to one. -//return length of array +//returns pointer to array of strings. second argument is return by reference length of returned array. char **line_splitter(char *line,int *rlen) { char **a; int len,i=0; @@ -81,14 +81,14 @@ char **line_splitter(char *line,int *rlen) { return a; } -int load_stdin() {//what is this returning? +int load_stdin() {//this function returns -1 to quit, 0 to not ask for a redraw, and 1 to ask for redraw struct c3_shape s; // struct c3_line l; c3_group_rot_t *gr; char *command; - char *line; + char *line=0; char *id; - char **a; + char **a=0; int len; int ret=0; int j,k,l; @@ -117,10 +117,11 @@ int load_stdin() {//what is this returning? } if(FD_ISSET(0,&readfs)) {*/ //#endif - while((line=read_line_hack(stdin,0))) {//load as long there's something to load + while((line=line?free(line),read_line_hack(stdin,0):read_line_hack(stdin,0))) {//load as long there's something to load if(*line == '#') return 0; // printf("# read command: %s\n",line); - a=line_splitter(line,&len); + if(a) free(a); + a=line_splitter(line,&len); // for(i=0;iid=strdup(a[3]); } } + ret=1; continue; } + if(!strcmp(command,"status")) { +#ifdef GRAPHICAL + printf("# fps: %d\n",gra_global.fps); + continue; +#endif + } if(!strcmp(command,"dump")) { printf("%s set global.camera.p.x %Lf\n",global.user,global.camera.p.x); printf("%s set global.camera.p.y %Lf\n",global.user,global.camera.p.y); @@ -243,6 +254,7 @@ int load_stdin() {//what is this returning? else if(!strcmp(a[2],"camera.r.z")) global.camera.r.z.d=atoi(a[3]); #endif else printf("# unknown variable: %s\n",a[2]); + ret=1; continue; } #ifdef GRAPHICAL @@ -251,6 +263,7 @@ int load_stdin() {//what is this returning? #endif else { printf("# unknown variable: %s\n",a[2]); continue; } printf("# %s toggled!\n",a[2]); + ret=1; continue; } if(!strcmp(command,"addshape")) {//need to add a grouprot with this. @@ -289,6 +302,7 @@ int load_stdin() {//what is this returning? global.group_rot[i]->r.z=(degrees){0}; } } + ret=1; continue; } if(!strcmp(command,"export")) { @@ -316,6 +330,7 @@ int load_stdin() {//what is this returning? } } } + ret=1; continue; } if(!strcmp(command,"rotate")) { @@ -337,6 +352,7 @@ int load_stdin() {//what is this returning? global.group_rot[i]->r.y=(degrees){atoi(a[3])}; global.group_rot[i]->r.z=(degrees){atoi(a[4])}; } + ret=1; continue; } if(!strcmp(command,"move")) { @@ -361,11 +377,11 @@ int load_stdin() {//what is this returning? else { printf("# ERROR: wrong amount of parts for move. got: %d expected: 4\n",len); } + ret=1; continue; } printf("# I don't know what command you're talking about. %s\n",command); - free(line); - if(a) free(a); + //I used to have free(line) here, but this place is never gotten to if a command is found so it wasn't getting released. } return ret; } @@ -381,6 +397,7 @@ int export_file(FILE *fp) {//not used yet. maybe export in obj optionally? no. t } int main(int argc,char *argv[]) { + char redraw; if(argc < 2) { fprintf(stderr,"usage: hackvr yourname < from_others > to_others\n"); return 1; @@ -396,7 +413,7 @@ int main(int argc,char *argv[]) { #endif printf("# entering main loop\n"); for(;;) { - switch(load_stdin()) { + switch(redraw=load_stdin()) { case -1: return 0; break; @@ -406,8 +423,9 @@ int main(int argc,char *argv[]) { break; } #ifdef GRAPHICAL - graphics_event_handler();//this thing should call draw_screen when it needs to. + graphics_event_handler(redraw);//this thing should call draw_screen when it needs to. #endif + sleep(.01); } return 0; } -- cgit v1.2.3