view TaskManager/Test/test_render/Application/dynamic_create.cc @ 543:9f9d51b60062

complex lindaapi
author aaa
date Wed, 21 Oct 2009 21:00:48 +0900
parents 7014034b8d69
children 519a1bba9b05
line wrap: on
line source

#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 "SceneGraphRoot.h"
#include "lindaapi/lindaapi.h"

#define PORT 10000

/*
typedef struct {
    caddr_t file_mmap;
    off_t size;
} st_mmap_t;
*/

typedef struct {
    int tid;
    int read_id;
    SceneGraphPtr node;
} callback_arg;

int
fix_byte(int size,int fix_byte_size)
{
    size = (size/fix_byte_size)*fix_byte_size  + ((size%fix_byte_size)!= 0)*fix_byte_size;
    
    return size;
}

st_mmap_t
my_mmap(char *filename)
{
    int fd = -1;
    int map = MAP_PRIVATE;
    st_mmap_t st_mmap;
    struct stat sb;

    if ((fd = open(filename, O_RDONLY, 0666)) == 0 ) {
	fprintf(stderr, "Can't open %s\n", filename);
    }

    if (fstat(fd, &sb)) {
	fprintf(stderr, "Can't fstat %s\n", filename);
    }

    printf("file size %d\n", (int)sb.st_size);
	
    st_mmap.size = fix_byte(sb.st_size, 4096);

    printf("fix 4096byte file size %d\n", (int)st_mmap.size);

    st_mmap.file_mmap = (char *)mmap(NULL, st_mmap.size, PROT_READ, map, fd, (off_t)0);
    if (st_mmap.file_mmap == (caddr_t)-1) {
	fprintf(stderr, "Can't mmap file\n");
	perror(NULL);
	exit(0);
    }

    return st_mmap;	
}

static void
earth_collision(SceneGraphPtr node, int screen_w, int screen_h,
	       SceneGraphPtr tree)
{    
}

static void
moon_collision(SceneGraphPtr node, int screen_w, int screen_h,
	       SceneGraphPtr tree)
{
}

static void
moon_move(SceneGraphPtr node, int screen_w, int screen_h)
{
    node->angle[0] += 3.0f;
    node->xyz[1] += 1.0f;
}


static void
earth_move(SceneGraphPtr node, int screen_w, int screen_h)
{
    node->angle[1] += 1.0f;
    if (node->angle[1] > 360.0f) {
	node->angle[1] = 0.0f;
    }

    node->xyz[0] += node->stack_xyz[0];
    if ((int)node->xyz[0] > screen_w || (int)node->xyz[0] < 0) {
	node->stack_xyz[0] = -node->stack_xyz[0];
    }

    node->xyz[1] += node->stack_xyz[1];
    if ((int)node->xyz[1] > screen_h || (int)node->xyz[1] < 0) {
	node->stack_xyz[1] = -node->stack_xyz[1];
    }
    
    //Pad *pad = sgroot->getController();

    /*
    if (pad->right.isPush()) {
        SceneGraphPtr earth;	
	
	st_mmap_t m = my_mmap("xml_file/universe.xml");

	//sgroot->createFromXMLmemory(sgroot->tmanager, "xml_file/universe.xml");
	sgroot->createFromXMLmemory(sgroot->tmanager, m);
	earth = sgroot->createSceneGraph("Earth");
	earth->set_move_collision(moon_move, moon_collision);
	node->addChild(earth);
    }
    */

    psx_sync_n();
}

void
create_sg(SceneGraphPtr node, unsigned char *data, int len)
{
    SceneGraphPtr earth;    

    //sgroot->createFromXMLmemory(sgroot->tmanager, "xml_file/universe.xml");
    const char *objname = sgroot->createFromXMLmemory(sgroot->tmanager, (char *)data, len);
    earth = sgroot->createSceneGraph(objname);
    earth->set_move_collision(moon_move, moon_collision);
    node->addChild(earth);
    
}

static void
callbacker(unsigned char *tuple, void *arg) {
    int len;
    unsigned char *data;
    callback_arg *carg = (callback_arg *)arg;
    
    len = psx_get_datalength(tuple);
    // 最初の4byteデータは使わない
    data = tuple+LINDA_HEADER_SIZE;
    
    // ここで create
    create_sg(carg->node, data, len);

    /* dataは'\0'で終わっている事を期待     (writerで保証する)  */
    //printf("get data[%d]: `%s'\n", len, data);
    free(tuple);

    psx_callback_wait_rd(carg->tid, carg->read_id, callbacker, arg);
}


void
linda_init(TaskManager *manager, SceneGraphPtr node)
{
    init_linda();
    callback_arg *carg = (callback_arg *)manager->allocate(sizeof(callback_arg));
    carg->tid = open_linda_java("cycas.cr.ie.u-ryukyu.ac.jp", PORT);
    carg->read_id = 10;
    carg->node = node;
    psx_callback_wait_rd(carg->tid, carg->read_id, callbacker, carg);
}

void
dynamic_init(TaskManager *manager)
{
    SceneGraphPtr earth;
    sgroot->tmanager = manager;

#if 0
    // テスト用に mmap したデータを第2引数に渡す予定でした
    sgroot->createFromXMLmemory(manager, "xml_file/universe.xml");

    // sglist に登録されている name から sgid を引き、sg_src[sgid] からコピーして返す
    earth = sgroot->createSceneGraph("Earth");
#else
    SceneGraphPtr parent;
    parent = sgroot->createSceneGraph();
    parent->set_move_collision(earth_move, earth_collision);
#endif    

    linda_init(manager, parent);
    
    // SceneGraphRoot に、使用する SceneGraph を設定する
    // このとき、ユーザーが記述した SceneGraph の root を渡す。
    sgroot->setSceneData(parent);
}