view TaskManager/Test/test_render/Application/dynamic_create.cc @ 537:7014034b8d69

work dynamic_create
author aaa
date Wed, 21 Oct 2009 19:03:07 +0900
parents 997d2a73a758
children c5a411c19e7e 9f9d51b60062
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"

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

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);
    }
}

/*
void
linda_init()
{
    init_linda();
    tspace = open_linda_java("localhost", PORT);
    printf("open");
}
*/

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