Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/util/resource_manager.cc @ 3655

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

orxonox/trunk: moved protoclass to folder proto
added protosingleton
added resourceManager
modiefied some stuff to work better

File size: 3.8 KB
Line 
1/*
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: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
17
18#include "resource_manager.h"
19
20// different resource Types
21#include "objModel.h"
22#include "texture.h"
23
24// File Handling Includes
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29
30using namespace std;
31
32
33/**
34   \brief standard constructor
35*/
36ResourceManager::ResourceManager () 
37{
38   this->setClassName ("ResourceManager");
39   this->dataDir = NULL;
40}
41
42ResourceManager* ResourceManager::getInstance(void)
43{
44  if (!ResourceManager::singletonRef)
45    ResourceManager::singletonRef = new ResourceManager();
46  return ResourceManager::singletonRef;
47}
48
49ResourceManager* ResourceManager::singletonRef = NULL;
50
51/**
52   \brief standard deconstructor
53*/
54ResourceManager::~ResourceManager (void) 
55{
56  ResourceManager::singletonRef = NULL;
57}
58
59
60
61/**
62   \brief sets the data main directory
63   \param dataDir the DataDirectory.
64*/
65bool ResourceManager::setDataDir(char* dataDir)
66{
67  if (isDir(dataDir))
68    {
69      this->dataDir = new char[strlen(dataDir)+1];
70      strcpy(this->dataDir, dataDir);
71    }
72  else
73    {
74      PRINTF(1)("%s is not a Directory, and can not be the Data Directory\n", dataDir);
75    }
76}
77
78/**
79   \brief loads resources
80   \param fileName The fileName of the resource to load
81   \returns a pointer to a desired Resource.
82*/
83void* ResourceManager::load(const char* fileName)
84{
85  ResourceType tmpType;
86  if (!strncmp(fileName+(strlen(fileName)-4), ".obj", 4))
87    tmpType = OBJ;
88
89  return ResourceManager::load(fileName, tmpType);
90}
91
92/**
93   \brief loads resources
94   \param fileName The fileName of the resource to load
95   \param type The Type of Resource to load (\see ResourceType)
96   \returns a pointer to a desired Resource.
97*/
98void* ResourceManager::load(const char* fileName, ResourceType type)
99{
100  void* tmpResource = NULL;
101  char* tmpName = new char[strlen(fileName)+1];
102  strcpy(tmpName, fileName);
103  // Checking for the type of resource \see ResourceType
104  switch(type)
105    {
106    case OBJ:
107      if(isFile(tmpName))
108        tmpResource = new OBJModel(tmpName);
109      else
110        {
111          PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", tmpName);
112          tmpResource = new Model(CUBE);
113        }
114      break;
115    case IMAGE:
116      if(isFile(tmpName))
117        {
118          tmpResource = new Texture(tmpName);
119        }
120      else
121        PRINTF(1)("!!Texture %s not Found!!\n", tmpName);
122      break;
123    default:
124      tmpResource = NULL;
125      PRINTF(2)("No type found for %s.\n   !!This should not happen unless the Type is not supported yet.!!\n", tmpName);
126      break;
127    }
128
129  // checking if the File really exists.
130  if(!isFile(tmpName))
131    {
132      PRINTF(2)("Sorry, %s is not a regular file.\n", tmpName);
133      tmpResource = NULL;
134    }
135  return tmpResource;
136}
137
138/**
139   \brief Checks if it is a Directory
140   \param directoryName the Directory to check for
141   \returns true if it is a directory/symlink false otherwise
142*/
143bool ResourceManager::isDir(const char* directoryName)
144{
145  struct stat status;
146  stat(directoryName, &status);
147  if (status.st_mode & (S_IFDIR | S_IFLNK))
148    return true;
149  else 
150    return false;
151}
152
153/**
154   \brief Checks if the file is either a Regular file or a Symlink
155   \param fileName the File to check for
156   \returns true if it is a regular file/symlink, false otherwise
157*/
158bool ResourceManager::isFile(const char* fileName)
159{
160  struct stat status;
161  stat(fileName, &status);
162  if (status.st_mode & (S_IFREG | S_IFLNK))
163    return true;
164  else
165    return false;
166}
Note: See TracBrowser for help on using the repository browser.