Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/loading/factory.cc @ 5157

Last change on this file since 5157 was 5156, checked in by bensch, 19 years ago

orxonox/trunk: no more segfaults wenn creating entities

File size: 2.1 KB
RevLine 
[4597]1/*
[3940]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
[4250]13   co-programmer: Benjamin Grauer
[3940]14*/
15
16
17#include "factory.h"
[5155]18
19#include "shell_command.h"
[3940]20#include "game_loader.h"
21using namespace std;
22
23/*  --------------------------------------------------
[4597]24 *               Factory
[4003]25 *   --------------------------------------------------
26 */
[3940]27
28/**
[4836]29 *  constructor
[4597]30
[4020]31   set everything to zero and define factoryName
[3940]32*/
[4487]33Factory::Factory (const char* factoryName)
[3940]34{
[4597]35  this->setClassID(CL_FACTORY, "Factory");
36  this->setName(factoryName);
37
[4739]38  this->next = NULL;
[4597]39
[4739]40  Factory::registerFactory(this);
[3940]41}
42
[4739]43/** a reference to the First Factory */
[4730]44Factory* Factory::first = NULL;
45
[3940]46/**
[4836]47 *  destructor
[4597]48
[3940]49   clear the Q
50*/
51Factory::~Factory ()
52{
[4020]53  //  printf("%s\n", this->factoryName);
[4004]54  //  Factory* tmpDel = this->next;
55  //  this->next = NULL;
56  if (this->next)
57    delete this->next;
[3940]58}
59
[4487]60/**
[4836]61 *  add a Factory to the Factory Queue
62 * @param factory a Factory to be registered
[3940]63*/
[4739]64void Factory::registerFactory( Factory* factory)
[3940]65{
[4739]66  assert( factory != NULL);
[3940]67
[4739]68  PRINTF(4)("Registered factory for '%s'\n", factory->getName());
69
70  if( Factory::first == NULL)
[5155]71  {
[4739]72    Factory::first = factory;
[5155]73    ShellCommand<Factory>::registerCommand("create", CL_FACTORY, &Factory::fabricate);
74  }
[4739]75  else
76  {
77    Factory* tmpFac = Factory::first;
78    while( tmpFac->next != NULL)
79    {
80      tmpFac = tmpFac->next;
81    }
82    tmpFac->setNext(factory);
83  }
[3940]84}
[5155]85
86void Factory::fabricate(const char* className, const char* entityName)
87{
88  if (className == NULL)
89    return;
90  Factory* fac = Factory::first;
91
92  while (fac != NULL)
93  {
94    if (!strcmp(className, fac->getName()))
95    {
96      PRINTF(3)("Create a new Object of type %s\n", fac->getName());
[5156]97      BaseObject* object = fac->fabricateDirect();
[5155]98      if (object != NULL)
99      {
100        object->setName(entityName);
101      }
102      break;
103    }
104    fac = fac->next;
105  }
106}
Note: See TracBrowser for help on using the repository browser.