diff Renderer/Test/send_linda.cc @ 566:006c4e9e6acb

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 22 Oct 2009 23:49:34 +0900
parents
children e3f3cfa8794f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Renderer/Test/send_linda.cc	Thu Oct 22 23:49:34 2009 +0900
@@ -0,0 +1,213 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include "SceneGraphRoot.h"
+#include "lindaapi.h"
+#include "send_linda.h"
+
+#define HOSTNAME "localhost"
+#define PORT_NUM 10000
+#define LISTEN_PORT 1
+#define MULTI_NUM 10
+
+void
+root_move(SceneGraphPtr node, int w, int h)
+{
+    Pad *pad = sgroot->getController();
+
+    if (pad->right.isHold() || pad->left.isHold()) {
+	if (pad->right.isHold()) {
+	    node->xyz[0] += 5.0f;
+	} else if (pad->left.isHold()) {
+	    node->xyz[0] -= 5.0f;
+	}
+    }
+    
+    if (pad->down.isHold() || pad->up.isHold() ) {
+	if (pad->down.isHold()) {
+	    node->xyz[1] += 5.0f;
+	} else if (pad->up.isHold()) {
+	    node->xyz[1] -= 5.0f;
+	}
+    }
+
+    /*
+      ここでキー入力を向こうに送る
+     */
+    
+}
+
+void
+root_collision(SceneGraphPtr node, int w, int h, SceneGraphPtr tree)
+{
+}
+
+void
+move(SceneGraphPtr node, int w, int h)
+{
+}
+
+void
+collision(SceneGraphPtr node, int w, int h, SceneGraphPtr tree)
+{
+}
+
+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
+linda_init(char *addr, int *size)
+{
+    
+}
+
+int get_serial_id(int fd) {
+    char *data;
+    int serial;
+    int seq;
+    
+    seq = psx_in(fd, 65535);
+    psx_sync_n();
+    data = (char *)psx_reply(seq);
+    serial = atoi(data + LINDA_HEADER_SIZE);
+    psx_free(data);
+    
+    return serial;
+}
+
+
+void
+send_xml(int tspace, int xml_id, void *addr, int size) {
+    psx_out(tspace, xml_id, (unsigned char *)addr, size);
+    psx_sync_n();
+}
+
+/*
+void
+mainLoop(int tid, int write_id, int fd)
+{
+    void *addr;
+    struct stat sb;
+    
+    if (fstat(fd, &sb) == -1) {
+	perror("fstat");
+	exit(1);
+    }
+    addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+    if (addr==NULL) {
+	perror("mmap");
+	exit(1);
+    }
+
+    printf("file size=%d\n", sb.st_size);
+
+    psx_out(tid, write_id, addr, sb.st_size);
+    psx_sync_n();
+
+    return;
+}
+*/
+
+static char *xml;
+
+MainLoopPtr 
+send_linda::init(Viewer *sgroot, int screen_w, int screen_h)
+{
+    void *addr;
+    int size;
+    int tspace;
+    int serial;
+    int xml_id;
+
+    SceneGraphPtr sgp;
+    SceneGraphPtr root;
+    root = sgroot->createSceneGraph();
+    root->set_move_collision(root_move, root_collision);
+
+    // createFromXMLfile で object name のリストを生成して返したい
+    sgroot->createFromXMLfile( xml);
+    // 今だけは決め打ち、本当はリストを回して object 数 create したい
+    sgp = sgroot->createSceneGraph("Ball");
+    sgp->set_move_collision(move, collision);
+    
+    root->addChild(sgp);
+    
+    init_linda();
+    addr = file_map(xml, &size);
+    tspace = open_linda_java(HOSTNAME, PORT_NUM);    
+    serial = get_serial_id(tspace);
+    xml_id = serial * 10;
+    send_xml(tspace, xml_id, addr, size);   	    
+    
+    int client_id = htonl(serial);
+    send_xml(tspace, LISTEN_PORT, (void *)client_id, sizeof(int));
+
+    sgroot->setSceneData(root);
+    return sgroot;
+}
+
+extern Application *
+application() {
+    return new send_linda();
+}
+
+
+const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n";
+
+extern int init(TaskManager *manager, int argc, char *argv[]);
+extern void task_initialize();
+static void TMend(TaskManager *manager);
+
+int
+TMmain(TaskManager *manager, int argc, char *argv[])
+{
+    task_initialize();
+    manager->set_TMend(TMend);
+
+    for(int i=0;i<argc;i++) {
+	if (strcmp(argv[i],"-xml") == 0 && i+1<=argc) {
+	    xml = argv[i+1];
+	}
+    }
+
+    return init(manager, argc, argv);
+
+}
+
+void
+TMend(TaskManager *manager)
+{
+    printf("test_nogl end\n");
+}
+
+/* end */
+