# HG changeset patch # User kazz # Date 1278377495 -32400 # Node ID 157f2ffeb526d26f1779fc3afc085612964b224e # Parent 58b4fb8ad77cc6d1673d650c202651c946476fc5 [Renderer/Test/aquarium] sendable xml object by psx_out() diff -r 58b4fb8ad77c -r 157f2ffeb526 Renderer/Test/aquarium.cc --- a/Renderer/Test/aquarium.cc Tue Jun 22 16:10:28 2010 +0900 +++ b/Renderer/Test/aquarium.cc Tue Jul 06 09:51:35 2010 +0900 @@ -1,5 +1,10 @@ #include #include +#include +#include +#include +#include +#include #include "SceneGraphRoot.h" #include "lindaapi.h" #include "aquarium.h" @@ -7,11 +12,16 @@ #define GET_SERIAL_ID 65535 +int aquarium::last_player_id = 1; + +Viewer *aquarium::sgroot; +SceneGraphPtr aquarium::parent; linda_t aquarium::linda_addr = { "localhost", 10000 }; int aquarium::linda; int aquarium::serial_id; int aquarium::width; int aquarium::start_x; +char *aquarium::xml_file_name; const char *usr_help_str = "Usage: ./aquarium -linda LINDA_SERVER_NAME\n"; void TMend(TaskManager *manager); @@ -23,12 +33,149 @@ return new aquarium(); } +static void +null_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) +{ +} +static void +null_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree) +{ +} + +static void +set_position(SceneGraphPtr node, unsigned char *reply) { +} + +static void +update_position_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) +{ + + SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; + // LindaServerから座標データを取得してオブジェクトに反映させる。 + + if (!node->resend_flag || node->seq_rd != node->seq) { + unsigned char *reply_rd = psx_reply(node->seq_rd); + if (reply_rd != NULL) { + set_position(node, reply_rd); + free(reply_rd); + return; + } + } + unsigned char *reply = psx_reply(node->seq); + if (reply != NULL) { + set_position(node, reply); + free(reply); + node->seq = psx_wait_rd(sgroot->tid, node->id * 10 + 1); + node->resend_flag = true; + } else if (node->resend_flag) { + node->seq_rd = psx_rd(sgroot->tid, node->id * 10 + 1); + node->resend_flag = false; + } +} + +SceneGraphPtr +create_sg(Viewer *viewer, SceneGraphPtr parent, unsigned char *data, int len, int serial_id) +{ + SceneGraphPtr child = viewer->sgroot->createSceneGraph(); + parent->addChild(child); + viewer->sgroot->createFromXMLmemory(viewer->sgroot->tmanager, child, (char *)data, len); + child->set_move_collision(update_position_move, null_collision); // TODO: + child->id = serial_id; + child->seq = psx_wait_rd(aquarium::linda, serial_id * 10 + 1); + child->seq_rd = psx_rd(aquarium::linda, serial_id * 10 + 1); + child->resend_flag = false; + return child; +} + + +static void +check_new_player_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) +{ + unsigned char *reply_rd = psx_reply(node->seq_rd); + if (reply_rd != NULL) { + unsigned char *xml_data = reply_rd + LINDA_HEADER_SIZE; + //int xml_len = *(int *)(reply_rd + LINDA_DATA_LENGTH_OFFSET); + int xml_len = psx_get_datalength(reply_rd); + create_sg(aquarium::sgroot, node, xml_data, xml_len, aquarium::last_player_id); + psx_free(reply_rd); + aquarium::last_player_id++; + int tuple_id = aquarium::last_player_id * 10; + aquarium::parent->seq_rd = psx_rd(aquarium::linda, tuple_id); + } + // printf("rd id: %d\n", aquarium::last_player_id); +} + +void * +file_map(const char *filename, int *size) { + int fd; + void *addr; + struct stat sb; + + if ((fd = open(filename, O_RDONLY)) == -1) { + fprintf(stderr, "Can't open %s\n", filename); + perror(NULL); + } + if (fstat(fd, &sb) == -1) { + fprintf(stderr, "Can't fstat %s\n", filename); + perror(NULL); + } + *size = sb.st_size; + addr = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); + if (addr == MAP_FAILED) { + perror("mmap error\n"); + exit(EXIT_FAILURE); + } + close(fd); + + return addr; +} + +void +aquarium::create_my_sg(Viewer *sgroot, int screen_w, int screen_h) +{ + SceneGraphPtr root = sgroot->createSceneGraph(); + root->set_move_collision(null_move, null_collision); // TODO: + + int size; + void *addr = file_map(xml_file_name, &size); + SceneGraphPtr sgp = sgroot->createSceneGraph(); + sgroot->createFromXMLmemory(sgp, (char *)addr, size); + sgp->set_move_collision(null_move, null_collision); + + root->addChild(sgp); + + int xml_id = serial_id * 10; + psx_out(linda, xml_id, (unsigned char *)addr, size); + printf("%d", size); + int pos_id = serial_id * 10 + 1; + + aqua::Position *pos = new aqua::Position(); + pos->set_x(0.0f); + pos->set_y(0.0f); + unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(unsigned char *) * size); + pos->SerializeToArray(msg, size); + psx_out(linda, pos_id, (unsigned char *)msg, pos->ByteSize()); + delete pos; + root->seq = 0; + root->resend_flag = 0; +} + MainLoopPtr aquarium::init(Viewer *sgroot, int screen_w, int screen_h) { + this->sgroot = sgroot; width = screen_w; - linda_connect(); + linda_connect(); // 接続に合わせて serial_id も取得 update_screen_scope(); + parent = sgroot->createSceneGraph(); + parent->set_move_collision(check_new_player_move, null_collision); + + create_my_sg(sgroot, screen_w, screen_h); + if (serial_id == last_player_id) + last_player_id++; + int tuple_id = aquarium::last_player_id * 10; + parent->seq_rd = psx_rd(linda, tuple_id); + sgroot->setSceneData(parent); return sgroot; } @@ -61,12 +208,12 @@ start_x = w->width(); // 現在の全長を取得 w->set_width(start_x + width); // 自分の横幅を足す int size = w->ByteSize(); - unsigned char *msg = (unsigned char *) malloc(sizeof(char) * size); + unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(char) * size); w->SerializeToArray(msg, size); // 更新したデータを再度シリアライズ + delete w; psx_out(linda, 1, msg, size); psx_sync_n(); printf("start_x = %d, width = %d\n", start_x, width); - delete w; } int @@ -80,6 +227,8 @@ aquarium::linda_addr.hostname = argv[i+1]; } else if (strcmp(argv[i],"-port") == 0 && i + 1 <= argc) { aquarium::linda_addr.port = atoi(argv[i+1]); + } else if (strcmp(argv[i],"-xml") == 0 && i + 1 <= argc) { + aquarium::xml_file_name = argv[i+1]; } } return init(manager, argc, argv); diff -r 58b4fb8ad77c -r 157f2ffeb526 Renderer/Test/aquarium.h --- a/Renderer/Test/aquarium.h Tue Jun 22 16:10:28 2010 +0900 +++ b/Renderer/Test/aquarium.h Tue Jul 06 09:51:35 2010 +0900 @@ -10,12 +10,18 @@ class aquarium : public Application { public: + static int last_player_id; + + static Viewer *sgroot; + static SceneGraphPtr parent; static linda_t linda_addr; static int linda; static int serial_id; static int start_x; static int width; + static char *xml_file_name; static void linda_connect(); static void update_screen_scope(); + void create_my_sg(Viewer *sgroot, int screen_w, int screen_h); MainLoopPtr init(Viewer *viewer, int screen_w, int screen_h); };