Skip to content

Commit

Permalink
Switch layouts to a simple JSON format.
Browse files Browse the repository at this point in the history
  • Loading branch information
zestyping committed Jun 11, 2013
1 parent 042c758 commit 0dfd64f
Show file tree
Hide file tree
Showing 5 changed files with 1,308 additions and 1,261 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ ifeq ($(platform),Darwin)
ALL=bin/dummy_client bin/dummy_server bin/gl_server
GL_OPTS=-framework OpenGL -framework GLUT
else ifeq ($(platform),Linux)
ALL=bin/dummy_client bin/dummy_server bin/tcl_server
ALL=bin/dummy_client bin/dummy_server bin/gl_server bin/tcl_server
GL_OPTS=-lGL -lGLU -lglut
endif

all: $(ALL)
Expand All @@ -24,6 +25,6 @@ bin/tcl_server: tcl_server.c opc_server.c
mkdir -p bin
gcc -o $@ $^

bin/gl_server: gl_server.c opc_server.c
bin/gl_server: gl_server.c opc_server.c cJSON.c
mkdir -p bin
gcc $(GL_OPTS) -o $@ $^
52 changes: 43 additions & 9 deletions gl_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ specific language governing permissions and limitations under the License. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef __APPLE__
#include <OpenGL/CGLCurrent.h>
#include <OpenGL/CGLTypes.h>
Expand All @@ -21,6 +23,7 @@ specific language governing permissions and limitations under the License. */
#include <GL/glut.h>
#endif

#include "cJSON.h"
#include "opc.h"

opc_source source = -1;
Expand Down Expand Up @@ -223,19 +226,50 @@ void idle() {
opc_receive(source, handler, 20);
}

void init(char* filename) {
char* read_file(char* filename) {
FILE* fp;
int i;
struct stat st;
char* buffer;

if (stat(filename, &st) != 0) {
return strdup("");
}
buffer = malloc(st.st_size + 1);
fp = fopen(filename, "r");
for (i = 0; ; i++) {
if (fscanf(fp, "%lf %lf %lf\n",
&(vectors[i].x), &(vectors[i].y), &(vectors[i].z)) < 3) {
num_pixels = i;
break;
fread(buffer, st.st_size, 1, fp);
fclose(fp);
buffer[st.st_size] = 0;
return buffer;
}

void init(char* filename) {
char* buffer;
cJSON* json;
cJSON* item;
cJSON* index;
cJSON* point;
cJSON* x;
int i = 0;

buffer = read_file(filename);
json = cJSON_Parse(buffer);
free(buffer);

for (item = json->child, i = 0; item; item = item->next, i++) {
index = cJSON_GetObjectItem(item, "index");
if (index) {
i = index->valueint;
}
point = cJSON_GetObjectItem(item, "point");
x = point ? point->child : NULL;
if (x && x->next && x->next->next) {
vectors[i].x = x->valuedouble;
vectors[i].y = x->next->valuedouble;
vectors[i].z = x->next->next->valuedouble;
printf("%.2f %.2f %.2f\n", vectors[i].x, vectors[i].y, vectors[i].z);
}
}
fclose(fp);
num_pixels = i;
for (i = 0; i < num_pixels; i++) {
pixels[i].r = pixels[i].g = pixels[i].b = 1;
}
Expand All @@ -249,7 +283,7 @@ int main(int argc, char** argv) {

glutInit(&argc, argv);
if (argc < 2) {
fprintf(stderr, "Usage: %s <options> <filename.l> [<port>]\n", argv[0]);
fprintf(stderr, "Usage: %s <options> <filename.json> [<port>]\n", argv[0]);
exit(1);
}
init(argv[1]);
Expand Down
Loading

0 comments on commit 0dfd64f

Please sign in to comment.