view Renderer/Engine/SgChange.cc @ 656:d0b8860c17f8

remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
author hiroki@henri.cr.ie.u-ryukyu.ac.jp
date Wed, 25 Nov 2009 21:56:14 +0900
parents
children 9d1bcc07734b b7376415fa5f
line wrap: on
line source

#include <SDL.h>
#include "SgChange.h"
#include "viewer_types.h"
#include "SceneGraph.h"
#include "SceneGraphRoot.h"
#include "scene_graph_pack.h"
#include "sys.h"
#include "Func.h"
#include "error.h"
#include "TaskManager.h"
#include <wchar.h>
#include "Pad.h"
#include "Application.h"
#include "lindaapi.h"

static void post2runLoop(SchedTask *s,void *viewer,void *s1);

/* measure for FPS (Frame Per Second) */
int start_time;
int this_time;
int frames;

/* Data Pack sent to Other CPUs (ex. SPE) */
SceneGraphPack *sgpack;
PolygonPack *ppack;
SpanPackPtr spackList;
SpanPackPtr *spackList_ptr;

int spackList_length;
int spackList_length_align;

/**
 *
 */

SgChange::SgChange(int b, int w, int h, int _num)
{
    bpp = b;
    width = w;
    height = h;
    spe_num = _num;
}

int
SgChange::get_ticks(void)
{
    int time;
    time = SDL_GetTicks();
    return time;
}

bool
SgChange::quit_check(void)
{
    SDL_Event event;

    while(SDL_PollEvent(&event)) {
        if (event.type==SDL_QUIT) {
            return true;
        }
    }

    Uint8 *keys=SDL_GetKeyState(NULL);

    if (keys[SDLK_q] == SDL_PRESSED) {
        return true;
    }

    return false;
}

void
SgChange::quit(void)
{
    SDL_Quit();
}

void
SgChange::swap_buffers(void)
{
    SDL_GL_SwapBuffers();
}


void
SgChange::run_init(TaskManager *manager, Application *app)
{
    this->manager = manager;

    start_time = get_ticks();
    this_time  = 0;
    frames     = 0;

    // ココ!
    sgroot_A = new SceneGraphRoot(this->width, this->height);
    sgroot_A->tmanager = manager;
    sgroot_B = new SceneGraphRoot(this->width, this->height);
    sgroot_B->tmanager = manager;

    MainLoop *mainloop = app->init_only_sg(this, this->width, this->height);

    mainloop->mainLoop();
}

void
SgChange::mainLoop()
{
    HTaskPtr task_next = manager->create_task(Dummy);

    task_next->set_post(&post2runLoop, (void *)this, 0); // set_post(function(this->run_loop()), NULL)
    task_next->spawn();
}

static void
post2runLoop(SchedTask *s, void *viewer_, void *arg)
{
    SgChange *viewer = (SgChange*)viewer_;
    HTaskPtr task_next = viewer->manager->create_task(Dummy);
    viewer->run_loop(task_next);

    psx_sync_n();
}

void
SgChange::exchange_sgroot()
{
    SceneGraphRoot *tmp;
    tmp = sgroot_A;
    sgroot_A = sgroot_B;
    sgroot_B = tmp;
}

void
SgChange::run_loop(HTaskPtr task_next)
{
    bool quit_flg;
    quit_flg = quit_check();
    if (quit_flg == true) {
        this_time = get_ticks();
        run_finish();
        return;
    }

    sgroot_A->allExecute(width, height);
    exchange_sgroot();

    //printf("Sgroot = %x\n", sgroot_A);

    task_next->set_post(&post2runLoop, (void *)this, 0);
    task_next->spawn();
}


void
SgChange::run_finish(void)
{
    if (this_time != start_time) {
        printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0);
    }

    delete sgroot_A;
    delete sgroot_B;
    quit();
}

/* end */