Ogre Resource Managers References: 1.http://www.ogre3d.org/tikiwiki/Resources+and+ResourceM anagers&structure=Tutorialshttp://www.ogre3d.org/tikiwiki/Resources+and+ResourceM.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 PHP Storing and Retrieving Data. string fgets(resource handle [, int length]) Reads and returns one line from a file; moves file pointer to next line.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Templates. Class templates – why? Writing programs we often use abstract data types such as stack, queue or tree. Implementations of these types may be.
ASP.NET Programming with C# and SQL Server First Edition
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
C++ fundamentals.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Games Development 2 Resource Management CO3301 Week 3.
Nachos Phase 1 Code -Hints and Comments
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
Pointers OVERVIEW.
Area Detector Drivers Towards A Pattern Jon Thompson.
Object-Oriented Programming in C++
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
J2EE Lecture 7: Spring – Spring MVC
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CMSC 202 Templates.
Constructors and Destructors
Java Programming Language
Chapter 9: Pointers and String
References Revisted (Ch 5)
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2018
Copy Constructors and Overloaded Assignment
Presentation transcript:

Ogre Resource Managers References: 1. anagers&structure=Tutorialshttp:// anagers&structure=Tutorials 2."Pro Ogre Programming", Junker, Springer-Verlag (2006)

Resources and ResourcePtr's Base classes – Resource is the resource itself (only one copy) – ResourcePtr is a (smart) pointer to a resource Might also contain parameters (e.g. what material) Eg. Mesh class – Contains all mesh data (indices, points, etc) Eg. MeshPtr: – Each entity has a MeshPtr attribute internally. – Each entity can change material settings.

ResourceManagers and ResourceGroupManager ResourceManager: a base class – Derived classes include: MeshManager, MaterialManager, etc. – Contains a map (hash table) of all Resources The one and only copy of each resource. ResourceGroup: – A named list of resources – [look in resources.cfg] ResrouceGroupManager: contains all ResourceManagers. – Owned by the root.

Ogre Resource Stages a.Undefined=>Declared i.ResourceGroupManager::declareResource ii.resources discovered during script loading (initResourceLocations). b.Declared=>Unloaded i.ResourceGroupManager::initialiseAllResourceGroups (initResources) ii.ResourceGroupManager::initialiseResourceGroup iii.ResourceManager::create + root::initialise() c.Unloaded=>Loaded i.ResourceGroupManager::loadResourceGroup() ii.Resource::load iii.when resource first used. 1. Undefined (Unknown) 2. Declared 3. Unloaded (Created) 4. Loaded a b d d e c d d.?=>Undefined i.ResourceGroupManager::clearResourceGroup() ii.Resource Manager::remove() e.Loaded=>Unloaded iv.ResourceManager::unload() v.ResourceGroupManager::unloadResourceGroup()

Why fit our resource manager's into Ogre's framework? Memory Budgets – Resource replacement (supposedly) thread-safe All resource scripts loaded during initialization. Ensures we only have one copy of a resource at a time. More practice integrating (in a C++ manner) our code into a different project.

Making a new Resource Manager 1.Derive a new resource class from Ogre::Resource – Re-define several methods 2.Create a new template-specialization for SharedPtr. 3.Derive a new manager class from Ogre::ResourceManager – Re-define several methods 4.Create an instance of your manager (in EngineApplication) 5.Create an EngineObjectComponent which includes a SharedPtr to a particular resource.

Using a 3D-sound resource manager (once it’s written) In a script file named mySounds.sound: [SoundFx] type = sound source = bubbles.wav [SoundFx2] type = sound source = magic.wav [LoadingMusic] type = music source = overture.ogg In initializeScene: // Create a sound for an arbitrary EngineObject (with “POS”) EngineObjectSoundComponent * snd = new EngineObjectSoundComponent(obj, “SoundFx”); // Register the player EngineObject (pobj) as a 3d listener EngineSoundManager::getSingleton().registerListener(pobj); In frameStarted: // Play (then switch) a sound (through EngineObject* obj) obj->handleMessage(“SOUND”, “play”); obj->handleMessage(“SOUND”, “setSource”, “SoundFx2”);

Detour: irrklang Creating a sound engine (only one) ISoundEngine * createIrrKlangDevice() Setting listener position (probably player's pos): ISoundEngine::setListenerPosition(vec3df pos, vec3df up) Adding a sound source ISoundEngine::addSourceFromMemory(unsigned char * data, int dataSizeInBytes, char * name) – data is just a.wav or.ogg file's (binary) contents. Playing a 2d / 3d source: ISound* ISoundEnging::play3D(char * name, vec3df pos) Modifying a 3d position ISound::setPosition(vec3df pos) Stopping all sounds ISoundEngine::stopAllSounds()

Detailed Steps (EngineSound resource) Derive from Ogre::Resource – Add any attributes (filename, type (music,fx), irrklang sound source) Re-define these methods: EngineSound(EngineSoundManager * creator, const String &name, ResourceHandle handle, const String &group, const NameValuePairList *createParams, bool isManual = false, ManualResourceLoader * loader = 0); Pass all arguments except createParams as-is to the Resource constructor. createParams is a map of all script params (we'll construct this in the EngineSoundManager class). We should have "type" and "source" keys. – pick off elements and save them to attributes.

Detailed Steps (EngineSound resource) virtual void loadImpl() Will be (indirectly) called by Ogre when someone creates an instance of a sound for the first time. Actually load the data here. Read the binary contents of the (ogg or wav) file and create an irrklang sound source (probably in / through the SoundManager) set mSize (inherited from Resource) to the file size. virtual void unloadImpl() Will be (indirectly) called by Ogre when: – a) We've gone over memory budget and this res was picked for replacement – b) The EngineSoundManager is shutting down. De-register this sound as a sound source De-allocate the binary data in memory. set mSize to 0. virtual size_t calculateSize() const Just return mSize

Detailed Steps (EngineSoundPtr class) class EngineSoundPtr : public Ogre::SharedPtr { public: /// Basic Constructor. EngineSoundPtr() : Ogre::SharedPtr () {} /// This constructor creates a new pointer to a /// EngineSound object. Remember: we can have multiple /// smart pointers (instances) pointing to one /// EngineSound object. explicit EngineSoundPtr(EngineSound *sound) : Ogre::SharedPtr (sound) {} /// This constructor takes a reference (instead of a /// pointer) to a EngineSound object which it should /// point to. EngineSoundPtr(const EngineSoundPtr &s) : Ogre::SharedPtr (s) {}

Detailed Steps (EngineSoundPtr) /// The constructor takes a reference to a resource (which had /// better be an EngineSound resource). EngineSoundPtr(const Ogre::ResourcePtr &s) : Ogre::SharedPtr () { if(s.isNull()) return; // Lock and copy other mutext pointer. This is necessary // because resource loading CAN be done on its own // thread. This ensures we are acquiring a lock on this // resource. No one else can access it until it's fully // loaded (at which point we release the lock). OGRE_LOCK_MUTEX(*s.OGRE_AUTO_MUTEX_NAME); OGRE_COPY_AUTO_SHARED_MUTEX(s.OGRE_AUTO_MUTEX_NAME); pRep = static_cast (s.getPointer()); pUseCount = s.useCountPointer(); useFreeMethod = s.freeMethod(); if (pUseCount) ++(*pUseCount); }

Detailed Steps (EngineSoundPtr) /// Converts a resource pointer to an EngineSound resource. /// Basically does what EngineSound(const Ogre:ResourcePtr & s) /// does, but also releases the resource we're converting from. EngineSoundPtr& operator = (const Ogre::ResourcePtr& s) { if (pRep == static_cast (s.getPointer())) return *this; release(); if (s.isNull()) return *this; //if the resource ptr is null, the // call to release above has done // what we needed OGRE_LOCK_MUTEX(*s.OGRE_AUTO_MUTEX_NAME); OGRE_COPY_AUTO_SHARED_MUTEX(s.OGRE_AUTO_MUTEX_NAME); pRep = static_cast (s.getPointer()); pUseCount = s.useCountPointer(); useFreeMethod = s.freeMethod(); if (pUseCount) ++(*pUseCount); return *this; } };

Detailed Steps (EngineSoundManager) Derived from Ogre::ResrouceManager – Additional attribues: at least the irrklang::ISoundEngine object. – Should be a singleton (can derive from Ogre::Singleton (a template) class – or do it yourself) Re-define these methods: – Constructor: Add to mScriptPaterns a "*.sound" string Register this with the ResourceGroupManager Register this as a ScriptLoader (with ResourceGroupManager) create the irrklang sound engine. – Destructor: Call removeAll (destroys all resources) Unregister yourself as a resource manager and a script loader. Call ISoundEngine->drop()

Detailed Steps (EngineSoundManager) Ogre::Resource *createImpl(const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, const NameValuePairList *createParams) – Called automatically by ogre any time a new resource is created (probably as part of script parsing) EngineSoundPtr EngineSoundManager::load(const String &name, const String &resourceName, const String &group) – Get an EngineSoundPtr to resourceName. – If it isNull, create it. – Regardless, load it and return it. Ogre::Resource *EngineSoundManager::createImpl(const String &name, ResourceHandle handle, const String &group, bool isManual, ManualResourceLoader *loader, const NameValuePairList *createParams) – Just return a new EngineSound object (pass all arguments to its constructor)

Detailed Steps (EngineSoundManager) void EngineSoundManager::parseScript(DataStreamPtr &stream, const String &groupName) – Ogre is telling you to parse a.sound script. – Use our initializeResourceLocations as a guide. – For each "section" (e.g. [SoundFx1]), declare a new resource of type "sound" with the ResourceGroupManager. – Also create a new NameValuePairList object, adding all key-value for the current "section" (pass this to the declareResource function call above).

Finding a file in our resource path(s) Given: “bubbles.wav” (a ‘local’ file name) Find: The full-fname ( relative to our debug directory ), such as “..\\..\..\\media\\sounds\\bubbles.wav” String fullName; FileInfoListPtr fptr = ResourceGroupManager::getSingleton().findResourceFileInfo(group, “bubbles.wav”); if (fptr->size() > 0) { FileInfo fi = fptr->at(0); String path = fi.archive->getName(); if (path[path.size()-1] != '/' && path[path.size()-1] != '\\') fullName = path + "/" + fi.filename; else fullName = path + fi.filename; } else fullName = "";