Code Search for Developers
 
 
  

Kernel.cpp from palisma2d at Krugle


Show Kernel.cpp syntax highlighted

/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Kernel.h"
#include <iostream>
#include <stdio.h>
#include <time.h>
 

#define CAP 30

Kernel::Kernel(void)
{
    m_delta = 0;
    m_lastFrame = 0;
    timer.Refresh();
}

int Kernel::Init()
{
    // Init SDL
    if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0){ 
        std::cerr << "Couldn't init SDL"; 
        return 1;
    }
    //Now that we're enabled, make sure we cleanup
    atexit(SDL_Quit); 
    return 0;
}
 
void Kernel::Shutdown()
{

    // close sdl
    SDL_Quit();
}

void Kernel::Execute()
{   
    m_lastFrame = GetTime();
    const int MAX_FRAMESKIP = 5;
    float interp = 0.0f;
    int loops;
    long  nextTick = GetTime();
    // Execute all processes in order
    // of priority
    while(proList.size())
    {
        timer.Refresh();
        const int SKIP_TICKS = 1000 / m_cvars->GetFloatValue("v_maxfps");


        // calculate how long it was since we last came round this loop
        // and hold on to it so we can let the updating/rendering routine
        // know how much time to update by
        m_delta = (long)(GetTime() - m_lastFrame);
        m_lastFrame = GetTime();

		std::list< CMMPointer<IProcess> >::iterator it;
		for(it=proList.begin();it!=proList.end();)
		{
			IProcess *t=(*it);
			it++;
            if(!t->CanKill() && t->IsActive() ) {
                if ( dynamic_cast< GameManager* >(t) ) {
                    loops = 0;
                    
                    while( GetTime() > nextTick && loops < MAX_FRAMESKIP ) {
                        t->Update( 20 );
                        nextTick += SKIP_TICKS;
                        loops++;
                        
                    }
                } else if ( dynamic_cast<IRenderManager* >(t) ) {
                    interp = float( GetTime() + SKIP_TICKS - nextTick ) / float( SKIP_TICKS );
                    t->Update( interp );
                }
                else {
                    t->Update( GetFrameTime() );
                }
            }
		}
		//loop again to remove dead tasks
		for(it=proList.begin();it!=proList.end();)
		{
			IProcess *t=(*it);
			it++;
			if(t->CanKill())
			{
                t->Shutdown();
				proList.remove(t);
				t=0;
			}
		}
        IMMObject::CollectGarbage();

        //// cap off FPS
        //float waitTime = (  m_cvars->GetFloatValue("v_maxfps") - GetFrameTime());//m_delta;
        //float currentTime = GetTime();
        //while( waitTime + currentTime > GetTime() );
    }
}

/**
=======================
Get the current time 
=======================
*/
long Kernel::GetTime() 
{
    return (long)SDL_GetTicks();
}

/**
========================
Get the FrameTime - frametime is one game loop
========================
*/
double Kernel::GetFrameTime()
{
    return timer.GetFrameTime()*1000;//m_delta;
}

void Kernel::AddProcess(CMMPointer<IProcess> &p)
{
	// keep the order of priorities straight
	std::list< CMMPointer<IProcess> >::iterator it;

	for(it=proList.begin();it!=proList.end();it++)
	{
		CMMPointer<IProcess> &comp=(*it);
		// if this priority is greater,
        // we want to insert it now
        if(comp->GetPriority() > p->GetPriority() )
            break;
	}
    // insert the process
	proList.insert(it,p);
}


void Kernel::SuspendProcess(CMMPointer<IProcess> &p)
{
	//check that this task is in our list - we don't want to suspend a task that isn't running
	if(std::find( proList.begin(), proList.end(), p) != proList.end())
	{
		proList.remove(p);
		pausedList.push_back(p);
	}
}


void Kernel::ResumeProcess(CMMPointer<IProcess> &p)
{
    if(std::find( pausedList.begin(), pausedList.end(), p ) != pausedList.end() )
    {
        pausedList.remove(p);
        // insert in the proper order
        std::list< CMMPointer<IProcess> >::iterator it;
        for ( it = proList.begin(); it != proList.end(); it++ )
        {
            if ( (*it)->GetPriority() > p->GetPriority() )
                break;
        }
        // insert in properly
        proList.insert( it, p );
    }

}
void Kernel::RemoveProcess(CMMPointer<IProcess> &p)
{
    // Kill the process
    if( std::find( proList.begin(), proList.end(), p) != proList.end() )
    {
        p->SetKill( true );
    }
}
void Kernel::KillAll()
{
    std::list< CMMPointer<IProcess> >::iterator it;
    for ( it = proList.begin(); it != proList.end(); it++ )
    {
        (*it)->SetKill( true );
    }
}

Kernel::~Kernel(void)
{
}




See more files for this project here

palisma2d

The University of Wisconsin-Parkside Developers Union first product. More info to come. Code name Palisma.

Project homepage: http://code.google.com/p/palisma2d/
Programming language(s): C,C++
License: gpl2

  Data/
    entities/
      link/
        finallink.tga
      objects/
        pot.tga
        torchpit.bmp
      soldier/
        soldier_01.png
        soldier_01.tga
        soldier_011.tga
      sryion.tga
    maps/
      dialogs/
        test.dialog
      tiles/
        layer2/
          fence.bmp
          fence.tga
          stump.tga
        grass_01.bmp
        street_01.bmp
        street_02.bmp
        street_03.bmp
        tile_a.bmp
        tile_b.bmp
        tile_c.bmp
        tile_d.bmp
        tile_e.bmp
        tile_f.bmp
        tile_g.bmp
        tile_h.bmp
        tile_i.bmp
      map01.dfn
      map01.ents
      map01.evt
      map01.ly1
      map01.ly2
      test.dfn
      test.ly1
      test.ly1.big
      test.ly2
    misc/
      rain1.tga
      rain2.tga
      rain3.tga
      rain4.tga
    scripts/
      hitman_mission/
        hitman.dialog
        hitman.lua
        hitman.msn
        hitman_inprogress.dialog
        hitman_reward.dialog
        hitman_reward.lua
        hitman_setup.lua
      test_mission/
        HitPotSetup.lua
        m_hitPot.dialog
        m_hitPot.lua
        m_hitPot.msn
        m_hitPot_inprogress.dialog
        m_hitPot_reward.lua
        reward.dialog
      mission_check.lua
    sounds/
      weather/
      hurt.wav
      lift.wav
      stroke.wav
      theme.wav
      throw.wav
    weapons/
      shotgun/
  Lib/
    LUA/
    OpenAL/
    SDL/
    Zlib/
  game/
    DialogModel.cpp
    DialogModel.h
    DialogState.cpp
    DialogState.h
    Enemy.cpp
    Enemy.h
    EntityController.cpp
    EntityController.h
    EntityEvents.cpp
    EntityEvents.h
    EntityFactory.cpp
    EntityFactory.h
    EntityManager.cpp
    EntityManager.h
    EntityStates.cpp
    EntityStates.h
    Event.h
    HUD.cpp
    HUD.h
    IEntity.cpp
    IEntity.h
    IWeapon.cpp
    IWeapon.h
    InGameState.cpp
    InGameState.h
    Inventory.cpp
    Inventory.h
    MissionHolder.cpp
    MissionHolder.h
    Player.cpp
    Player.h
    PlayerConfig.cpp
    PlayerConfig.h
    PlayerController.cpp
    PlayerController.h
    QuestImporter.cpp
    QuestImporter.h
    ReadMe.txt
    Scene.cpp
    Scene.h
    Shotgun.cpp
    Shotgun.h
    State.h
    StateFactory.cpp
    StateFactory.h
  gui/
  input/
  render/
  script/
  shared/
  sound/
  Console.cpp
  Console.h
  ConsoleFunctions.h
  Cvars.cpp
  Cvars.h
  EventManager.cpp
  EventManager.h
  Exec_f.cpp
  GameManager.cpp
  GameManager.h
  IConsole.h
  IGameState.h
  IProcess.h
  Kernel.cpp
  Kernel.h
  Log.cpp
  Log.h
  Myriad.cpp
  Myriad.h
  ReadMe.txt
  Recorder.cpp
  Recorder.h
  Resource.cpp
  Resource.h
  Timer.cpp
  Timer.h
  client.log
  game01.zip
  mmanager.cpp
  mmanager.h
  stdafx.cpp
  stdafx.h