Code Search for Developers
 
 
  

Scene.cpp from palisma2d at Krugle


Show Scene.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 "Scene.h"
//#include "../shared/vector.h"
#include <istream>
#include "entitymanager.h"
#include "../shared/geom.h"
#include <stdio.h>

#include "../kernel.h"

extern Kernel* g_kernel;

Scene::Scene(void)
{
    m_init = false;
    m_maxX = 0;
    m_maxY = 0;

    TILESIZE = 64;

}



/** Get the tile at this location in array*/
Tile* Scene::GetTile(int layer, int x, int y)
{
    return (layer > 1) ? m_layer2[y][x] : m_layer1[y][x];
}
/** Get the tile from screen coords */
Tile* Scene::GetScreenTile(int layer, int x, int y)
{
    // TODO
    return NULL;
}
  
/** Test wether entities are allowed on the tile */
bool  Scene::Collide(int x, int y, Rect &r)
{
    // TODO - check bounds of character
    if ( CheckBounds(x,y) )
        return true;

    int index_x = 0, index_y = 0;
    WorldToTile( x, y , index_x, index_y );

    return  (m_layer2[index_y][index_x] != NULL);

}


/** Check the map boundries */
bool  Scene::CheckBounds(int x, int y)
{
    // check the map bounds
    return ( x <= 0 || y <= 0     ||
            x >= m_maxX*TILESIZE-64 ||
            y >= m_maxY*TILESIZE-64   );
}


/** Check for a Trigger event */
void  Scene::InvokeTrigger( IEntity* triggerer, int x, int y )
{
    int index_x = 0, index_y = 0;
    WorldToTile( x, y , index_x, index_y );

    m_layer1[index_y][index_x]->Trigger(triggerer, MPoint(index_x,index_y) );
}

/** convert from world coordinates to tile locations */
void Scene::WorldToTile( int x, int y, int &toIndex_x, int &toIndex_y )
{
    // Current Tile offset (to pixels)
    int tileOffset_x =  ( x % TILESIZE );
    // to next index
    toIndex_x    = ( tileOffset_x + x) / TILESIZE;

    // current tile y offset (to pixels)
    int tileOffset_y = ( y % TILESIZE);
    toIndex_y    = (tileOffset_y + y) / TILESIZE;
}

/** Convert Tile coordinates to world coordinates */
void Scene::TileToWorld( int tx, int ty, int &wx, int &wy )
{
    wx = tx * TILESIZE;
    wy = ty * TILESIZE;
}

/** Center the camera around our player */
void Scene::CenterCamera( ICamera* cam, Vector2f &pos )
{
    // screen dimensions
    float h_height = (cam->GetViewPort().height /2);
    float h_width  = (cam->GetViewPort().width  /2);

    // handle being on top edge of map
    if ( pos.y + h_height > m_maxY*TILESIZE )
    {
        h_height = (pos.y-m_maxY*TILESIZE + h_height*2);
    }
    // handle bottom edge
    if ( pos.y - h_height < 0 ) 
    {
        h_height = pos.y;
    }

    // handle left edge
    if ( pos.x - h_width  < 0 )
    {
        h_width = pos.x;
    }

    // handle right edge
    if ( pos.x + h_width  > m_maxX*TILESIZE )
    {
        h_width = pos.x+m_maxX*TILESIZE - h_width*2;
    }
    // adjust the camera
    cam->SetScreenCoord( Vector2f(h_width, h_height ) );
    
    // move the camera
    cam->MoveTo( pos );
    

}


/**
=================================================
Update the current scene
TODO-
update animation tiles, and check triggers
=================================================
*/
void Scene::Update( long dt )
{

}

/**
=====================
Draw the current scene given location of the camera
=====================
*/
void Scene::Draw( IRender *r, ICamera* cam )
{
    Vector2f camPos = cam->GetPosition();

    // the viewport x, and y location
    //int vx = cam->GetViewPort().x;
    //int vy = cam->GetViewPort().y;

    // Get the current map offsets
    m_offset.Set( camPos );

    //screen pixel x,y coordinate to draw the current tile to
    int pixel_x = 0;
    int pixel_y = 0;

	int index_x = 0;
    int index_y = 0;

    // Current Tile offset (to pixels)
	int tileOffset_x =  -( (int)camPos.x % TILESIZE );
    // to next index
    int toIndex_x    = ( tileOffset_x + (int)camPos.x) / TILESIZE;

    // current tile y offset (to pixels)
    int tileOffset_y = -( (int)camPos.y % TILESIZE);
    int toIndex_y    = (tileOffset_y + (int)camPos.y) / TILESIZE;
    
    index_y = toIndex_y;

    for( pixel_y = tileOffset_y/*+vy*/; pixel_y < cam->GetViewPort().height/*+vy*/ && index_y < m_maxY;  pixel_y += TILESIZE )
    {
        for( pixel_x = tileOffset_x/*+vx*/, index_x = toIndex_x; pixel_x < cam->GetViewPort().width/*+vx*/ && index_x < m_maxX;  pixel_x += TILESIZE)
        {
            // draw the first layer
            if ( m_layer1[index_y][index_x] != NULL )
            {
                // set the tile to the correct location
                m_layer1[index_y][index_x]->Set( pixel_x, pixel_y );
                
                // Render the tile
                m_layer1[index_y][index_x]->Render( r );
            }
            // draw second layer
            if ( m_layer2[index_y][index_x] != NULL )
            {
                m_layer2[index_y][index_x]->Set( pixel_x, pixel_y );
                m_layer2[index_y][index_x]->Render( r );
            }

            // update the index
            index_x++;
        }
        index_y++;
    }
}



/**
===============================================================
Free the current scenes resources
===============================================================
*/
void Scene::FreeScene()
{

    // delete all the tiles
    //for (int i = 0; i < m_layer1.size(); i++ ) {
    //    for ( int j = 0; j < m_layer1[i].size(); j++ )
    //    {
    //        if ( m_layer1[i][j] )
    //            delete m_layer1[i][j];
    //        if ( m_layer2[i][j] )
    //            delete m_layer2[i][j];
    //    }
    //}
    m_layer1.clear();
    m_layer2.clear();
}



Scene::~Scene(void)
{
    FreeScene();
}




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

  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
  StateMachine.cpp
  StateMachine.h
  Tile.cpp
  Tile.h
  Weather.cpp
  Weather.h
  stdafx.cpp
  stdafx.h