コード例 #1
0
ファイル: main.cpp プロジェクト: qpfiffer/Leviathan
int main() {
    srand((int)time(NULL));
    cPlayer *mainPlayer = new cPlayer;

    TCODConsole::initRoot(SCREEN_WIDTH, SCREEN_HEIGHT+5, "LEVIATHAN", false, TCOD_RENDERER_SDL);

    mapData *mpShort = mainPlayer->mapDataHandle;
    if (mpShort->loaded != true) {
        if(mpShort->fileHandler->readMapData(mpShort) == 1) {
            cout<<"Failed to load map data."<<endl;
            mpShort->genNewMap();
            mpShort->fileHandler->writeMapData(mpShort);
            cout<<"Map generated and written."<<endl;
        } else {
            cout<<"Map loaded."<<endl;
        }
    }
    mpShort->genNewMap();

    TCODConsole *messages = new TCODConsole(SCREEN_WIDTH, 5);
    messages->setForegroundColor(TCODColor::red);
    messages->clear();
    int i,j;
    for (i=0;i<5;i++) {
        for (j=0;j<SCREEN_WIDTH;j++) {
            if (i==0) {
                messages->setFore(j, i, TCODColor::white);
                messages->setChar(j, i, '-');
            }
            if (i==4) {
                messages->setFore(j, i, TCODColor::white);
                messages->setChar(j, i, '-');
            }
            if (j==0) {
                messages->setFore(j, i, TCODColor::white);
                messages->setChar(j, i, '|');
            }
            if (j==SCREEN_WIDTH-1) {
                messages->setFore(j, i, TCODColor::white);
                messages->setChar(j, i, '|');
            }
            if (i==0 && (j==0 || j==SCREEN_WIDTH-1)) {
                messages->setFore(j, i, TCODColor::white);
                messages->setChar(j, i, '0');
            }
            if (i==4 && (j==0 || j==SCREEN_WIDTH-1)) {
                messages->setFore(j, i, TCODColor::white);
                messages->setChar(j, i, '0');
            }
        }
    }

    // Render once before everything, because we handle
    // keypresses first
    render(mainPlayer, messages);

    // Main loop
    while(1) {
        // See if we should be closed
        if (TCODConsole::isWindowClosed())
            break;

        if (mainPlayer->health <= 0) {
            // Losing condition:
            TCODConsole::root->setForegroundColor(TCODColor::white);
            TCODConsole::root->clear();
            /*for (int fade=255;fade >= 0;fade--) {
                TCODConsole::setFade(fade,TCODColor::black);
                TCODConsole::flush();
                Sleep(200);
            }*/
            string end = "You are dead.";
            TCODConsole::root->printEx(SCREEN_WIDTH/2-(sizeof("You are dead.")/2),
                SCREEN_HEIGHT/2, TCOD_BKGND_NONE, TCOD_LEFT, end.c_str());
            TCODConsole::flush();

            TCODConsole::waitForKeypress(true);
            break;
        } else if (keyHandler(mainPlayer) == -1) {
            break;
        }

        // Render everything
        render(mainPlayer, messages);
    }

    delete mainPlayer;
    delete messages;
    return 0;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: qpfiffer/Leviathan
void render(cPlayer *mainPlayer, TCODConsole *messages) {
    int i, j;
    // Shorten this shit up a bit:
    mapData *mpShort = mainPlayer->mapDataHandle;

    TCODConsole *offscreenConsole = new TCODConsole(SCREEN_WIDTH,SCREEN_HEIGHT);
    offscreenConsole->setForegroundColor(TCODColor::blue);
    offscreenConsole->clear(); // Flush the new changes to the offscreen console

    // Check if we have loaded the current cell yet:
    if (mpShort->loaded != true) {
        //if(mpShort->fileHandler->readMapData(mpShort) == 1) {
        //    cout<<"Failed to load map data."<<endl;
            mpShort->clearMap();
            mpShort->genNewMap();
        //    mpShort->fileHandler->writeMapData(mpShort);
        //    cout<<"Map generated and written."<<endl;
        //} else {
        //    cout<<"Map loaded."<<endl;
        //}
    }

    mainPlayer->updateFOV();
    // Map renderer:
    for (i=0;i<SCREEN_WIDTH;i++) {
        for (j=0;j<SCREEN_HEIGHT;j++) {
            if (mainPlayer->myFOV->isInFov(i, j)) {
                // If it is in the Field of View, make sure it has been explored now:
                mpShort->mapHandlerMaster[i][j].isExplored = true;

                float distance = (float)((i - mainPlayer->pos[0]) * (i - mainPlayer->pos[0]) + 
                    (j - mainPlayer->pos[1]) * (j - mainPlayer->pos[1]));
                float squaredTorchRadius = (float)(mainPlayer->lightRadius * mainPlayer->lightRadius);

				if(distance < squaredTorchRadius) {
                    float lerpVal = (squaredTorchRadius - distance) / squaredTorchRadius;
                    lerpVal = CLAMP(0.1f, 1.0f, lerpVal);
                    TCODColor foreColor = mpShort->mapHandlerMaster[i][j].foreColor * mainPlayer->lightColor * lerpVal;
					TCODColor backColor = mpShort->mapHandlerMaster[i][j].backColor * mainPlayer->lightColor * lerpVal;
                    offscreenConsole->setFore(i, j, foreColor);
                    offscreenConsole->setBack(i, j, backColor);
                    offscreenConsole->setChar(i, j, mpShort->mapHandlerMaster[i][j].graphic);
                }
            } else if(mpShort->mapHandlerMaster[i][j].isExplored) {	// If the tile is not in fov, but has been seen before...
				offscreenConsole->setFore(i, j, mpShort->mapHandlerMaster[i][j].foreColor * 0.1f);
                offscreenConsole->setBack(i, j, mpShort->mapHandlerMaster[i][j].backColor * 0.1f);
                offscreenConsole->setChar(i, j, mpShort->mapHandlerMaster[i][j].graphic);
			} else {	// And if the tile has never been seen, we just black it out.
				offscreenConsole->putCharEx(i, j, ' ', TCODColor::black, TCODColor::black);
            }
        }
    }

    // Entity renderer:
    entity *loop = mainPlayer->mapDataHandle->masterList->head;
    while (loop != NULL) {
        // We have entities to render:
        if (mainPlayer->myFOV->isInFov(loop->pos[0], loop->pos[1])) {
            float distance = (float)((loop->pos[0] - mainPlayer->pos[0]) * (loop->pos[0] - mainPlayer->pos[0]) + 
                    (loop->pos[1] - mainPlayer->pos[1]) * (loop->pos[1] - mainPlayer->pos[1]));
            float squaredTorchRadius = (float)(mainPlayer->lightRadius * mainPlayer->lightRadius);

			if(distance < squaredTorchRadius) {
                float lerpVal = (squaredTorchRadius - distance) / squaredTorchRadius;
                lerpVal = CLAMP(0.1f, 1.0f, lerpVal);
                TCODColor foreColor = loop->myColor * mainPlayer->lightColor * lerpVal;
                offscreenConsole->setFore(loop->pos[0], loop->pos[1], foreColor);
                offscreenConsole->setChar(loop->pos[0], loop->pos[1], loop->myChar);

                // Compute the path stuff for the entity:
                loop->myPath->compute(loop->pos[0], loop->pos[1], mainPlayer->pos[0], mainPlayer->pos[1]);
                // See if we are one space away from the player, and attack him if we are:
                int qX=0, qY=0;
                loop->myPath->walk(&qX, &qY, true);
                if (qX == mainPlayer->pos[0] && qY == mainPlayer->pos[1]) {
                    loop->attack(mainPlayer);
                } else {
                    loop->pos[0] = qX;
                    loop->pos[1] = qY;
                }
            }
        }
        
        loop = loop->next;
    }

    // Some random event stuff:
    if (mainPlayer->turns == mainPlayer->nextRand) {
        mainPlayer->mapDataHandle->randChanges(mainPlayer);
        mainPlayer->nextRand = mainPlayer->turns+=rand() % 300;
    }

    // Noise stuff:
    if (mainPlayer->isSnowing)
        mpShort->noise(snow, offscreenConsole, mainPlayer);
    
    // Render the player last
    offscreenConsole->setFore(mainPlayer->pos[0], mainPlayer->pos[1], mainPlayer->myColor);
    offscreenConsole->setChar(mainPlayer->pos[0], mainPlayer->pos[1], '@');

    // Blit all that crap on-screen
    TCODConsole::blit(offscreenConsole,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,TCODConsole::root,0,0, 1.0f, 1.0f);

    // Messages:
    // clear and remake the window:
    for (i=1;i<4;i++) {
        for (j=1;j<(SCREEN_WIDTH-1);j++) {
            messages->setChar(j, i, ' ');
        }
    }

    for (i=0;i<3;i++) {
        std::ostringstream temp;
        temp <<"%c"<<mainPlayer->messagesHandle[i]<<"%c";
        TCODConsole::setColorControl(TCOD_COLCTRL_1,mainPlayer->messageColors[i],TCODColor::black);
        messages->printEx(1, (i+1), TCOD_BKGND_NONE, TCOD_LEFT, temp.str().c_str(),TCOD_COLCTRL_1,TCOD_COLCTRL_STOP);
    }
    TCODConsole::blit(messages, 0, 0, SCREEN_WIDTH, 5, TCODConsole::root, 0, SCREEN_HEIGHT);
    
    // Flip it
    TCODConsole::root->flush();

    delete offscreenConsole;
}