Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 25, 2007, 11:06:13 PM (16 years ago)
Author:
scheusso
Message:

added GameStateManager class:

  • gets snapshots of the universe (gamestate) and returns them
  • processes snapshots and loads them to the universe

modified Synchronisable

  • added a function getData(usigned char *data), which puts the data to

the already given pointer (more efficient memory handling for the
GameState class)

  • added a function getSize(), which tells you the total size of the

current gamestate (so that you can allocate enough memory and give it to
getData

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/Synchronisable.cc

    r245 r247  
    55//
    66//
    7 // Author:  Oliver Scheuss, (C) 2007
     7// Author:  Dumeni, Oliver Scheuss, (C) 2007
    88//
    99// Copyright: See COPYING file that comes with this distribution
     
    1414namespace network {
    1515
     16/**
     17 * Constructor:
     18 * calls registarAllVariables, that has to be implemented by the inheriting classID
     19 */
    1620Synchronisable::Synchronisable()
    1721{
     22  datasize=0;
    1823  registerAllVariables();
    1924}
     
    2530}
    2631
     32/**
     33 * This function is used to register a variable to be synchronized
     34 * also counts the total datasize needed to save the variables
     35 * @param var pointer to the variable
     36 * @param size size of the datatype the variable consists of
     37 */
    2738void Synchronisable::registerVar(const void *var, int size){
     39  // create temporary synch.Var struct
    2840  synchronisableVariable temp={size, var};
     41  // increase datasize
     42  datasize+=sizeof(int)+size;
     43  // push temp to syncList (at the bottom)
    2944  syncList.push_back(temp);
    3045}
     46
     47/**
     48 * note: only use this function for debug use, because it's inefficient (in order to produce a gamestate, you have to copy the whole data again to another memory location after this process)
     49 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
     50 * structure of the bitstream:
     51 * (var1_size,var1,var2_size,var2,...)
     52 * varx_size: size = sizeof(int)
     53 * varx: size = varx_size
     54 * @return data containing all variables and their sizes
     55 */
    3156syncData Synchronisable::getData(){
    3257  std::list<synchronisableVariable>::iterator i;
     
    5681  return retVal;
    5782}
     83/**
     84 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
     85 * Difference to the above function:
     86 * takes a pointer to already allocated memory (must have at least getSize bytes length)
     87 * structure of the bitstream:
     88 * (var1_size,var1,var2_size,var2,...)
     89 * varx_size: size = sizeof(int)
     90 * varx: size = varx_size
     91 * @return data containing all variables and their sizes
     92 */
     93syncData Synchronisable::getData(unsigned char *mem){
     94  std::list<synchronisableVariable>::iterator i;
     95  syncData retVal;
     96  retVal.objectID=this->objectID;
     97  retVal.classID=this->classID;
     98  retVal.length=datasize;
     99  retVal.data=mem;
     100  // copy to location
     101  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
     102  int n=0;
     103  for(i=syncList.begin(); n<datasize && i!=syncList.end(); i++){
     104        //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* 
     105    std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
     106    n+=sizeof(int);
     107    //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i
     108    std::memcpy(retVal.data+n, (const void*)(i->var), i->size);
     109    n+=i->size;
     110  }
     111  return retVal;
     112}
     113
     114/**
     115 * This function takes a syncData struct and takes it to update the variables
     116 * @param vars data of the variables
     117 * @return true/false
     118 */
    58119bool Synchronisable::updateData(syncData vars){
    59120  unsigned char *data=vars.data;
     
    70131}
    71132
     133/**
     134 * This function returns the total amount of bytes needed by getData to save the whole content of the variables
     135 * @return amount of bytes
     136 */
     137int Synchronisable::getSize(){
     138  return datasize;
    72139}
     140
     141}
Note: See TracChangeset for help on using the changeset viewer.