Merge branch 'master' into pgrd-rendering
Conflicts: apps/openmw/mwrender/renderingmanager.cppactorid
commit
65fc141b62
@ -1,6 +0,0 @@
|
||||
[submodule "libs/mangle"]
|
||||
path = libs/mangle
|
||||
url = git://github.com/zinnschlag/mangle.git
|
||||
[submodule "libs/openengine"]
|
||||
path = libs/openengine
|
||||
url = git://github.com/zinnschlag/OpenEngine
|
@ -0,0 +1,86 @@
|
||||
|
||||
#include "inventorystore.hpp"
|
||||
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
#include "class.hpp"
|
||||
|
||||
void MWWorld::InventoryStore::copySlots (const InventoryStore& store)
|
||||
{
|
||||
// some const-trickery, required because of a flaw in the handling of MW-references and the
|
||||
// resulting workarounds
|
||||
for (std::vector<ContainerStoreIterator>::const_iterator iter (
|
||||
const_cast<InventoryStore&> (store).mSlots.begin());
|
||||
iter!=const_cast<InventoryStore&> (store).mSlots.end(); ++iter)
|
||||
{
|
||||
std::size_t distance = std::distance (const_cast<InventoryStore&> (store).begin(), *iter);
|
||||
|
||||
ContainerStoreIterator slot = begin();
|
||||
|
||||
std::advance (slot, distance);
|
||||
|
||||
mSlots.push_back (slot);
|
||||
}
|
||||
}
|
||||
|
||||
MWWorld::InventoryStore::InventoryStore()
|
||||
{
|
||||
for (int i=0; i<Slots; ++i)
|
||||
mSlots.push_back (end());
|
||||
}
|
||||
|
||||
MWWorld::InventoryStore::InventoryStore (const InventoryStore& store)
|
||||
: ContainerStore (store)
|
||||
{
|
||||
copySlots (store);
|
||||
}
|
||||
|
||||
MWWorld::InventoryStore& MWWorld::InventoryStore::operator= (const InventoryStore& store)
|
||||
{
|
||||
ContainerStore::operator= (store);
|
||||
mSlots.clear();
|
||||
copySlots (store);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void MWWorld::InventoryStore::equip (int slot, const ContainerStoreIterator& iterator)
|
||||
{
|
||||
if (slot<0 || slot>=static_cast<int> (mSlots.size()))
|
||||
throw std::runtime_error ("slot number out of range");
|
||||
|
||||
if (iterator.getContainerStore()!=this)
|
||||
throw std::runtime_error ("attempt to equip an item that is not in the inventory");
|
||||
|
||||
if (iterator!=end())
|
||||
{
|
||||
std::pair<std::vector<int>, bool> slots = Class::get (*iterator).getEquipmentSlots (*iterator);
|
||||
|
||||
if (std::find (slots.first.begin(), slots.first.end(), slot)==slots.first.end())
|
||||
throw std::runtime_error ("invalid slot");
|
||||
}
|
||||
|
||||
/// \todo restack item previously in this slot (if required)
|
||||
|
||||
/// \todo unstack item pointed to by iterator if required)
|
||||
|
||||
mSlots[slot] = iterator;
|
||||
}
|
||||
|
||||
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot)
|
||||
{
|
||||
if (slot<0 || slot>=static_cast<int> (mSlots.size()))
|
||||
throw std::runtime_error ("slot number out of range");
|
||||
|
||||
if (mSlots[slot]==end())
|
||||
return end();
|
||||
|
||||
if (mSlots[slot]->getRefData().getCount()<1)
|
||||
{
|
||||
// object has been deleted
|
||||
mSlots[slot] = end();
|
||||
return end();
|
||||
}
|
||||
|
||||
return mSlots[slot];
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
#ifndef GAME_MWWORLD_INVENTORYSTORE_H
|
||||
#define GAME_MWWORLD_INVENTORYSTORE_H
|
||||
|
||||
#include "containerstore.hpp"
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
///< \brief Variant of the ContainerStore for NPCs
|
||||
class InventoryStore : public ContainerStore
|
||||
{
|
||||
public:
|
||||
|
||||
static const int Slot_Helmet = 0;
|
||||
static const int Slot_Cuirass = 1;
|
||||
static const int Slot_Greaves = 2;
|
||||
static const int Slot_LeftPauldron = 3;
|
||||
static const int Slot_RightPauldron = 4;
|
||||
static const int Slot_LeftGauntlet = 5;
|
||||
static const int Slot_RightGauntlet = 6;
|
||||
static const int Slot_Boots = 7;
|
||||
static const int Slot_Shirt = 8;
|
||||
static const int Slot_Pants = 9;
|
||||
static const int Slot_Skirt = 10;
|
||||
static const int Slot_Robe = 11;
|
||||
static const int Slot_LeftRing = 12;
|
||||
static const int Slot_RightRing = 13;
|
||||
static const int Slot_Amulet = 14;
|
||||
static const int Slot_Belt = 15;
|
||||
static const int Slot_CarriedRight = 16;
|
||||
static const int Slot_CarriedLeft = 17;
|
||||
static const int Slot_Ammunition = 18;
|
||||
|
||||
static const int Slots = 19;
|
||||
|
||||
static const int Slot_NoSlot = -1;
|
||||
|
||||
private:
|
||||
|
||||
mutable std::vector<ContainerStoreIterator> mSlots;
|
||||
|
||||
void copySlots (const InventoryStore& store);
|
||||
|
||||
public:
|
||||
|
||||
InventoryStore();
|
||||
|
||||
InventoryStore (const InventoryStore& store);
|
||||
|
||||
InventoryStore& operator= (const InventoryStore& store);
|
||||
|
||||
void equip (int slot, const ContainerStoreIterator& iterator);
|
||||
///< \note \a iteartor can be an end-iterator
|
||||
|
||||
ContainerStoreIterator getSlot (int slot);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,120 @@
|
||||
#include "filelibrary.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
namespace Files
|
||||
{
|
||||
// Looks for a string in a vector of strings
|
||||
bool containsVectorString(const StringVector& list, const std::string& str)
|
||||
{
|
||||
for (StringVector::const_iterator iter = list.begin();
|
||||
iter != list.end(); iter++)
|
||||
{
|
||||
if (*iter == str)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Searches a path and adds the results to the library
|
||||
void FileLibrary::add(const boost::filesystem::path &root, bool recursive, bool strict,
|
||||
const StringVector &acceptableExtensions)
|
||||
{
|
||||
if (!boost::filesystem::exists(root))
|
||||
{
|
||||
std::cout << "Warning " << root.string() << " does not exist.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string fileExtension;
|
||||
std::string type;
|
||||
|
||||
// remember the last location of the priority list when listing new items
|
||||
int length = mPriorityList.size();
|
||||
|
||||
// First makes a list of all candidate files
|
||||
FileLister(root, mPriorityList, recursive);
|
||||
|
||||
// Then sort these files into sections according to the folder they belong to
|
||||
for (PathContainer::iterator listIter = mPriorityList.begin() + length;
|
||||
listIter != mPriorityList.end(); ++listIter)
|
||||
{
|
||||
if( !acceptableExtensions.empty() )
|
||||
{
|
||||
fileExtension = boost::filesystem::path (listIter->extension()).string();
|
||||
boost::algorithm::to_lower(fileExtension);
|
||||
if(!containsVectorString(acceptableExtensions, fileExtension))
|
||||
continue;
|
||||
}
|
||||
|
||||
type = boost::filesystem::path (listIter->parent_path().leaf()).string();
|
||||
if (!strict)
|
||||
boost::algorithm::to_lower(type);
|
||||
|
||||
mMap[type].push_back(*listIter);
|
||||
// std::cout << "Added path: " << listIter->string() << " in section "<< type <<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if the named section exists
|
||||
bool FileLibrary::containsSection(std::string sectionName, bool strict)
|
||||
{
|
||||
if (!strict)
|
||||
boost::algorithm::to_lower(sectionName);
|
||||
StringPathContMap::const_iterator mapIter = mMap.find(sectionName);
|
||||
if (mapIter == mMap.end())
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns a pointer to const for a section of the library
|
||||
const PathContainer* FileLibrary::section(std::string sectionName, bool strict)
|
||||
{
|
||||
if (!strict)
|
||||
boost::algorithm::to_lower(sectionName);
|
||||
StringPathContMap::const_iterator mapIter = mMap.find(sectionName);
|
||||
if (mapIter == mMap.end())
|
||||
{
|
||||
//std::cout << "Empty\n";
|
||||
return &mEmptyPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
return &(mapIter->second);
|
||||
}
|
||||
}
|
||||
|
||||
// Searches the library for an item and returns a boost path to it
|
||||
boost::filesystem::path FileLibrary::locate(std::string item, bool strict, std::string sectionName)
|
||||
{
|
||||
boost::filesystem::path result("");
|
||||
if (sectionName == "")
|
||||
{
|
||||
return FileListLocator(mPriorityList, boost::filesystem::path(item), strict);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!containsSection(sectionName, strict))
|
||||
{
|
||||
std::cout << "Warning: There is no section named " << sectionName << "\n";
|
||||
return result;
|
||||
}
|
||||
result = FileListLocator(mMap[sectionName], boost::filesystem::path(item), strict);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Prints all the available sections, used for debugging
|
||||
void FileLibrary::printSections()
|
||||
{
|
||||
for(StringPathContMap::const_iterator mapIter = mMap.begin();
|
||||
mapIter != mMap.end(); mapIter++)
|
||||
{
|
||||
std::cout << mapIter->first <<std::endl;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
#ifndef COMPONENTS_FILES_FILELIBRARY_HPP
|
||||
#define COMPONENTS_FILES_FILELIBRARY_HPP
|
||||
|
||||
#include <components/files/fileops.hpp>
|
||||
|
||||
namespace Files
|
||||
{
|
||||
typedef std::map<std::string, PathContainer> StringPathContMap;
|
||||
typedef std::vector<std::string> StringVector;
|
||||
|
||||
/// Looks for a string in a vector of strings
|
||||
bool containsVectorString(const StringVector& list, const std::string& str);
|
||||
|
||||
/// \brief Searches directories and makes lists of files according to folder name
|
||||
class FileLibrary
|
||||
{
|
||||
private:
|
||||
StringPathContMap mMap;
|
||||
PathContainer mEmptyPath;
|
||||
PathContainer mPriorityList;
|
||||
|
||||
public:
|
||||
/// Searches a path and adds the results to the library
|
||||
/// Recursive search and fs strict options are available
|
||||
/// Takes a vector of acceptable files extensions, if none is given it lists everything.
|
||||
void add(const boost::filesystem::path &root, bool recursive, bool strict,
|
||||
const StringVector &acceptableExtensions);
|
||||
|
||||
/// Returns true if the named section exists
|
||||
/// You can run this check before running section()
|
||||
bool containsSection(std::string sectionName, bool strict);
|
||||
|
||||
/// Returns a pointer to const for a section of the library
|
||||
/// which is essentially a PathContainer.
|
||||
/// If the section does not exists it returns a pointer to an empty path.
|
||||
const PathContainer* section(std::string sectionName, bool strict);
|
||||
|
||||
/// Searches the library for an item and returns a boost path to it
|
||||
/// Optionally you can provide a specific section
|
||||
/// The result is the first that comes up according to alphabetical
|
||||
/// section naming
|
||||
boost::filesystem::path locate(std::string item, bool strict, std::string sectionName="");
|
||||
|
||||
/// Prints all the available sections, used for debugging
|
||||
void printSections();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -1 +0,0 @@
|
||||
Subproject commit 14b2851e72f610ae81dd296598867e6fb0babd2a
|
@ -0,0 +1,3 @@
|
||||
upload_docs.sh
|
||||
docs
|
||||
*~
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,26 @@
|
||||
Minimal Abstraction Game Layer (Mangle) is licensed under the
|
||||
'zlib/libpng' license:
|
||||
|
||||
----
|
||||
|
||||
Copyright (c) 2009 Nicolay Korslund
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
|
@ -0,0 +1,129 @@
|
||||
Welcome to Mangle v0.1
|
||||
----------------------
|
||||
|
||||
Written by: Nicolay Korslund (korslund@gmail.com)
|
||||
License: zlib/png (see LICENSE.txt)
|
||||
WWW: http://asm-soft.com/mangle/
|
||||
Documentation: http://asm-soft.com/mangle/docs
|
||||
|
||||
|
||||
|
||||
Mangle is the project name for a small set of generic interfaces for
|
||||
various game middleware libraries, such as sound, input, graphics, and
|
||||
so on. You can imagine that it stands for "Minimal Abstraction Game
|
||||
Layer", if you like. It will consist of several more or less
|
||||
independent modules, one for each of these areas. These may be used
|
||||
together to build an entire game engine, or they can be used
|
||||
individually as separate libraries.
|
||||
|
||||
However, Mangle does NOT actually implement a game engine, or any new
|
||||
fundamental functionality. More on that below.
|
||||
|
||||
Currently there's modules for sound and streams / archives (virtual
|
||||
file systems.) More will come in the future (including input, 2D/3D
|
||||
graphics, GUI, physics, and more.)
|
||||
|
||||
|
||||
Main idea
|
||||
---------
|
||||
|
||||
The idea behind Mangle is to provide a uniform, consistent interface
|
||||
to other game libraries. The library does not provide ANY
|
||||
functionality on its own. Instead it connects to a backend
|
||||
implementation of your choice (or of your making.)
|
||||
|
||||
The Sound module, for example, currently has backends for OpenAL
|
||||
(output only), FFmpeg (input only) and for Audiere. Hopefully we'll
|
||||
add IrrKlang, FMod, DirectSound, Miles and more in the future. It can
|
||||
combine libraries to get more complete functionality (like using
|
||||
OpenAL for output and FFmpeg to decode sound files), and it's also
|
||||
easy to write your own backend if you're using a different (or
|
||||
home-brewed) sound system.
|
||||
|
||||
Regardless of what backend you use, the front-end interfaces (found
|
||||
eg. in sound/output.h) is identical, and as a library user you
|
||||
shouldn't notice much difference at all if you swap one backend for
|
||||
another at a later point. It should Just Work.
|
||||
|
||||
The interfaces themselves are also quite simple. Setting up a sound
|
||||
stream from FFmpeg or other decoder into OpenAL can be quite hairy -
|
||||
but with Mangle the hairy parts have already been written for you. You
|
||||
just plug the parts together.
|
||||
|
||||
The goal in the long run is to support a wide variety of game-related
|
||||
libraries, and as many backend libraries (free and commercial) as
|
||||
possible, so that you the user will have to write as little code as
|
||||
possible.
|
||||
|
||||
|
||||
|
||||
What is it good for
|
||||
-------------------
|
||||
|
||||
The main point of Mangle, as we said above, is that it connects to any
|
||||
library of your choice "behind the scenes" but provides the same,
|
||||
super-simple interface front-end for all of them. There can benefit
|
||||
you in many ways:
|
||||
|
||||
- If you want to use a new library that Mangle support. You don't have
|
||||
to scour the net for tutorials and usage examples, since much of the
|
||||
common usage code is already included in the implementation classes.
|
||||
|
||||
- If you don't want to pollute your code with library-specific code.
|
||||
The Mangle interfaces can help you keep your code clean, and its
|
||||
user interface is often simpler than the exteral library one.
|
||||
|
||||
- If you want to quickly connect different libraries together, it
|
||||
really helps if they speak a common language. The Mangle interfaces
|
||||
are exactly that - a common language between libraries. Do you need
|
||||
Audiere to load sounds from a weird archive format only implemented
|
||||
for PhysFS, all channeled through the OGRE resource system? No
|
||||
problem!
|
||||
|
||||
- If you are creating a library that depends on a specific feature
|
||||
(such as sound), but you don't want to lock your users into any
|
||||
specific sound library. Mangle works as an abstraction that lets
|
||||
your users select their own implementation.
|
||||
|
||||
- If you want to support multiple backends for your game/app, or want
|
||||
to make it possible to easily switch backends later. You can select
|
||||
backends at compile time or even at runtime. For example you might
|
||||
want to switch to to a commercial sound library at a later stage in
|
||||
development, or you may want to use a different input library on
|
||||
console platforms than on PC.
|
||||
|
||||
The Mangle implementations are extremely light-weight - often just one
|
||||
or two cpp/h pairs per module. You can plug them directly into your
|
||||
program, there's no separate library building step required.
|
||||
|
||||
Since the library aims to be very modularly put together, you can
|
||||
also, in many cases, just copy-and-paste the parts you need and ignore
|
||||
the rest. Or modify stuff without fearing that the whole 'system' will
|
||||
come crashing down, because there is no big 'system' to speak of.
|
||||
|
||||
|
||||
Past and future
|
||||
---------------
|
||||
|
||||
Mangle started out as (and still is) a spin-off from OpenMW, another
|
||||
project I am personally working on ( http://openmw.com/ ). OpenMW is
|
||||
an attempt to recreate the engine behind the commercial game
|
||||
Morrowind, using only open source software.
|
||||
|
||||
The projects are still tightly interlinked, and they will continue to
|
||||
be until OpenMW is finished. Most near-future work on Mangle will be
|
||||
focused chiefly on OpenMW at the moment. However I will gladly include
|
||||
external contributions and suggestions that are not OpenMW-related if
|
||||
someone sends them to me.
|
||||
|
||||
|
||||
Conclusion
|
||||
----------
|
||||
|
||||
As you might have guessed, Mangle is more a concept in development
|
||||
than a finished library right now.
|
||||
|
||||
All feedback, ideas, concepts, questions and code are very
|
||||
welcome. Send them to: korslund@gmail.com
|
||||
|
||||
I will put up a forum later as well if there's enough interest.
|
@ -0,0 +1,29 @@
|
||||
#ifndef MANGLE_INPUT_OGREINPUTFRAME_H
|
||||
#define MANGLE_INPUT_OGREINPUTFRAME_H
|
||||
|
||||
/*
|
||||
This Ogre FrameListener calls capture() on an input driver every frame.
|
||||
*/
|
||||
|
||||
#include <OgreFrameListener.h>
|
||||
#include "../driver.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Input {
|
||||
|
||||
struct OgreInputCapture : Ogre::FrameListener
|
||||
{
|
||||
Mangle::Input::Driver &driver;
|
||||
|
||||
OgreInputCapture(Mangle::Input::Driver &drv)
|
||||
: driver(drv) {}
|
||||
|
||||
bool frameStarted(const Ogre::FrameEvent &evt)
|
||||
{
|
||||
driver.capture();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
@ -0,0 +1,69 @@
|
||||
#ifndef MANGLE_INPUT_DRIVER_H
|
||||
#define MANGLE_INPUT_DRIVER_H
|
||||
|
||||
#include "event.hpp"
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
/** Input::Driver is the main interface to any input system that
|
||||
handles keyboard and/or mouse input, along with any other
|
||||
input source like joysticks.
|
||||
|
||||
It is really a generalized event system, and could also be
|
||||
used for non-input related events. The definition of the event
|
||||
codes and structures are entirely dependent on the
|
||||
implementation.
|
||||
|
||||
A system-independent key code list will be found in keys.hpp,
|
||||
and input drivers should privide optional translations to/from
|
||||
this list for full compatibility.
|
||||
*/
|
||||
struct Driver
|
||||
{
|
||||
Driver() {}
|
||||
virtual ~Driver() {}
|
||||
|
||||
/** Captures input and produces the relevant events from it. An
|
||||
event callback must be set with setEvent(), or all events
|
||||
will be ignored.
|
||||
*/
|
||||
virtual void capture() = 0;
|
||||
|
||||
/** Check the state of a given key or button. The key/button
|
||||
definitions depends on the driver.
|
||||
*/
|
||||
virtual bool isDown(int index) = 0;
|
||||
|
||||
/** Show or hide system mouse cursor
|
||||
*/
|
||||
virtual void showMouse(bool show) = 0;
|
||||
|
||||
/** Set the event handler for input events. The evt->event()
|
||||
function is called for each event. The meaning of the index
|
||||
and *p parameters will be specific to each driver and to
|
||||
each input system.
|
||||
*/
|
||||
void setEvent(EventPtr evt)
|
||||
{ event = evt; }
|
||||
|
||||
/** Instigate an event. Is used internally for all events, but
|
||||
can also be called from the outside to "fake" events from
|
||||
this driver.
|
||||
*/
|
||||
void makeEvent(Event::Type type, int index, const void *p=NULL)
|
||||
{
|
||||
if(event)
|
||||
event->event(type,index,p);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Holds the event callback set byt setEvent()
|
||||
EventPtr event;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<Driver> DriverPtr;
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,46 @@
|
||||
#ifndef MANGLE_INPUT_EVENT_H
|
||||
#define MANGLE_INPUT_EVENT_H
|
||||
|
||||
#include "../tools/shared_ptr.hpp"
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
/** Generic callback for input events. The meaning of the
|
||||
parameters depend on the system producing the events.
|
||||
*/
|
||||
struct Event
|
||||
{
|
||||
/// Event types
|
||||
enum Type
|
||||
{
|
||||
EV_Unknown = 1, // Unknown event type
|
||||
EV_KeyDown = 2, // Keyboard button was pressed
|
||||
EV_KeyUp = 4, // Keyboard button was released
|
||||
EV_Keyboard = 6, // All keyboard events
|
||||
|
||||
EV_MouseMove = 8, // Mouse movement
|
||||
EV_MouseDown = 16, // Mouse button pressed
|
||||
EV_MouseUp = 32, // Mouse button released
|
||||
EV_Mouse = 56, // All mouse events
|
||||
|
||||
EV_ALL = 63 // All events
|
||||
};
|
||||
|
||||
/**
|
||||
Called upon all events. The first parameter give the event
|
||||
type, the second gives additional data (usually the local
|
||||
keysym or button index as defined by the driver), and the
|
||||
pointer points to the full custom event structure provided by
|
||||
the driver (the type may vary depending on the EventType,
|
||||
this is defined in the Driver documentation.)
|
||||
*/
|
||||
virtual void event(Type type, int index, const void *p) = 0;
|
||||
virtual ~Event() {}
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<Event> EventPtr;
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,47 @@
|
||||
#ifndef MANGLE_INPUT_EVENTLIST_H
|
||||
#define MANGLE_INPUT_EVENTLIST_H
|
||||
|
||||
#include "../event.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
/** And Event handler that distributes each event to a list of
|
||||
other handlers. Supports filtering events by their Type
|
||||
parameter.
|
||||
*/
|
||||
struct EventList : Event
|
||||
{
|
||||
struct Filter
|
||||
{
|
||||
EventPtr evt;
|
||||
int flags;
|
||||
};
|
||||
std::vector<Filter> list;
|
||||
|
||||
void add(EventPtr e, int flags = EV_ALL)
|
||||
{
|
||||
Filter f;
|
||||
f.evt = e;
|
||||
f.flags = flags;
|
||||
list.push_back(f);
|
||||
}
|
||||
|
||||
virtual void event(Type type, int index, const void *p)
|
||||
{
|
||||
std::vector<Filter>::iterator it;
|
||||
|
||||
for(it=list.begin(); it!=list.end(); it++)
|
||||
{
|
||||
if(type & it->flags)
|
||||
it->evt->event(type,index,p);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<EventList> EventListPtr;
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,148 @@
|
||||
#include "ois_driver.hpp"
|
||||
|
||||
#include <assert.h>
|
||||
#include <sstream>
|
||||
#include <OgreRenderWindow.h>
|
||||
#include <OIS/OIS.h>
|
||||
|
||||
#ifdef __APPLE_CC__
|
||||
#include <Carbon/Carbon.h>
|
||||
#endif
|
||||
|
||||
using namespace Mangle::Input;
|
||||
using namespace OIS;
|
||||
|
||||
struct Mangle::Input::OISListener : OIS::KeyListener, OIS::MouseListener
|
||||
{
|
||||
OISDriver &drv;
|
||||
|
||||
OISListener(OISDriver &driver)
|
||||
: drv(driver) {}
|
||||
|
||||
bool keyPressed( const OIS::KeyEvent &arg )
|
||||
{
|
||||
drv.makeEvent(Event::EV_KeyDown, arg.key, &arg);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool keyReleased( const OIS::KeyEvent &arg )
|
||||
{
|
||||
drv.makeEvent(Event::EV_KeyUp, arg.key, &arg);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
|
||||
{
|
||||
// Mouse button events are handled as key events
|
||||
// TODO: Translate mouse buttons into pseudo-keysyms
|
||||
drv.makeEvent(Event::EV_MouseDown, id, &arg);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
|
||||
{
|
||||
// TODO: ditto
|
||||
drv.makeEvent(Event::EV_MouseUp, id, &arg);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mouseMoved( const OIS::MouseEvent &arg )
|
||||
{
|
||||
drv.makeEvent(Event::EV_MouseMove, -1, &arg);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
OISDriver::OISDriver(Ogre::RenderWindow *window, bool exclusive)
|
||||
{
|
||||
assert(window);
|
||||
|
||||
size_t windowHnd;
|
||||
|
||||
window->getCustomAttribute("WINDOW", &windowHnd);
|
||||
|
||||
std::ostringstream windowHndStr;
|
||||
ParamList pl;
|
||||
|
||||
windowHndStr << windowHnd;
|
||||
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
|
||||
|
||||
// Set non-exclusive mouse and keyboard input if the user requested
|
||||
// it.
|
||||
if(!exclusive)
|
||||
{
|
||||
#if defined OIS_WIN32_PLATFORM
|
||||
pl.insert(std::make_pair(std::string("w32_mouse"),
|
||||
std::string("DISCL_FOREGROUND" )));
|
||||
pl.insert(std::make_pair(std::string("w32_mouse"),
|
||||
std::string("DISCL_NONEXCLUSIVE")));
|
||||
pl.insert(std::make_pair(std::string("w32_keyboard"),
|
||||
std::string("DISCL_FOREGROUND")));
|
||||
pl.insert(std::make_pair(std::string("w32_keyboard"),
|
||||
std::string("DISCL_NONEXCLUSIVE")));
|
||||
#elif defined OIS_LINUX_PLATFORM
|
||||
pl.insert(std::make_pair(std::string("x11_mouse_grab"),
|
||||
std::string("false")));
|
||||
pl.insert(std::make_pair(std::string("x11_mouse_hide"),
|
||||
std::string("false")));
|
||||
pl.insert(std::make_pair(std::string("x11_keyboard_grab"),
|
||||
std::string("false")));
|
||||
pl.insert(std::make_pair(std::string("XAutoRepeatOn"),
|
||||
std::string("true")));
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __APPLE_CC__
|
||||
// Give the application window focus to receive input events
|
||||
ProcessSerialNumber psn = { 0, kCurrentProcess };
|
||||
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
|
||||
SetFrontProcess(&psn);
|
||||
#endif
|
||||
|
||||
inputMgr = InputManager::createInputSystem( pl );
|
||||
|
||||
// Create all devices
|
||||
keyboard = static_cast<Keyboard*>(inputMgr->createInputObject
|
||||
( OISKeyboard, true ));
|
||||
mouse = static_cast<Mouse*>(inputMgr->createInputObject
|
||||
( OISMouse, true ));
|
||||
|
||||
// Set mouse region
|
||||
const MouseState &ms = mouse->getMouseState();
|
||||
ms.width = window->getWidth();
|
||||
ms.height = window->getHeight();
|
||||
|
||||
// Set up the input listener
|
||||
listener = new OISListener(*this);
|
||||
keyboard-> setEventCallback(listener);
|
||||
mouse-> setEventCallback(listener);
|
||||
}
|
||||
|
||||
OISDriver::~OISDriver()
|
||||
{
|
||||
// Delete the listener object
|
||||
if(listener)
|
||||
delete listener;
|
||||
|
||||
if(inputMgr == NULL) return;
|
||||
|
||||
// Kill the input systems. This will reset input options such as key
|
||||
// repeat rate.
|
||||
inputMgr->destroyInputObject(keyboard);
|
||||
inputMgr->destroyInputObject(mouse);
|
||||
InputManager::destroyInputSystem(inputMgr);
|
||||
inputMgr = NULL;
|
||||
}
|
||||
|
||||
void OISDriver::capture()
|
||||
{
|
||||
// Capture keyboard and mouse events
|
||||
keyboard->capture();
|
||||
mouse->capture();
|
||||
}
|
||||
|
||||
bool OISDriver::isDown(int index)
|
||||
{
|
||||
// TODO: Extend to mouse buttons as well
|
||||
return keyboard->isKeyDown((OIS::KeyCode)index);
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
#ifndef MANGLE_INPUT_OIS_DRIVER_H
|
||||
#define MANGLE_INPUT_OIS_DRIVER_H
|
||||
|
||||
#include "../driver.hpp"
|
||||
|
||||
namespace OIS
|
||||
{
|
||||
class InputManager;
|
||||
class Mouse;
|
||||
class Keyboard;
|
||||
}
|
||||
|
||||
namespace Ogre
|
||||
{
|
||||
class RenderWindow;
|
||||
}
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
struct OISListener;
|
||||
|
||||
/** Input driver for OIS, the input manager typically used with
|
||||
Ogre.
|
||||
*/
|
||||
struct OISDriver : Driver
|
||||
{
|
||||
/// If exclusive=true, then we capture mouse and keyboard from
|
||||
/// the OS.
|
||||
OISDriver(Ogre::RenderWindow *window, bool exclusive=true);
|
||||
~OISDriver();
|
||||
|
||||
void capture();
|
||||
bool isDown(int index);
|
||||
/// Not currently supported.
|
||||
void showMouse(bool) {}
|
||||
|
||||
private:
|
||||
OIS::InputManager *inputMgr;
|
||||
OIS::Mouse *mouse;
|
||||
OIS::Keyboard *keyboard;
|
||||
|
||||
OISListener *listener;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,54 @@
|
||||
#include "sdl_driver.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
using namespace Mangle::Input;
|
||||
|
||||
void SDLDriver::capture()
|
||||
{
|
||||
// Poll for events
|
||||
SDL_Event evt;
|
||||
while(SDL_PollEvent(&evt))
|
||||
{
|
||||
Event::Type type = Event::EV_Unknown;
|
||||
int index = -1;
|
||||
|
||||
switch(evt.type)
|
||||
{
|
||||
// For key events, send the keysym as the index.
|
||||
case SDL_KEYDOWN:
|
||||
type = Event::EV_KeyDown;
|
||||
index = evt.key.keysym.sym;
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
type = Event::EV_KeyUp;
|
||||
index = evt.key.keysym.sym;
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
type = Event::EV_MouseMove;
|
||||
break;
|
||||
// Add more event types later
|
||||
}
|
||||
|
||||
// Pass the event along, using -1 as index for unidentified
|
||||
// event types.
|
||||
makeEvent(type, index, &evt);
|
||||
}
|
||||
}
|
||||
|
||||
bool SDLDriver::isDown(int index)
|
||||
{
|
||||
int num;
|
||||
Uint8 *keys = SDL_GetKeyState(&num);
|
||||
assert(index >= 0 && index < num);
|
||||
|
||||
// The returned array from GetKeyState is indexed by the
|
||||
// SDLK_KEYNAME enums and is just a list of bools. If the indexed
|
||||
// value is true, the button is down.
|
||||
return keys[index];
|
||||
}
|
||||
|
||||
void SDLDriver::showMouse(bool show)
|
||||
{
|
||||
SDL_ShowCursor(show?SDL_ENABLE:SDL_DISABLE);
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#ifndef MANGLE_INPUT_SDL_DRIVER_H
|
||||
#define MANGLE_INPUT_SDL_DRIVER_H
|
||||
|
||||
#include "../driver.hpp"
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
/** Input driver for SDL. As the input system of SDL is seldomly
|
||||
used alone (most often along with the video system), it is
|
||||
assumed that you do your own initialization and cleanup of SDL
|
||||
before and after using this driver.
|
||||
|
||||
The Event.event() calls will be given the proper EV_ type, the
|
||||
key index (for key up/down events), and a pointer to the full
|
||||
SDL_Event structure.
|
||||
*/
|
||||
struct SDLDriver : Driver
|
||||
{
|
||||
void capture();
|
||||
bool isDown(int index);
|
||||
void showMouse(bool);
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,2 @@
|
||||
*_test
|
||||
ogre.cfg
|
@ -0,0 +1,15 @@
|
||||
GCC=g++ -Wall
|
||||
|
||||
all: sdl_driver_test ois_driver_test evtlist_test
|
||||
|
||||
sdl_driver_test: sdl_driver_test.cpp
|
||||
$(GCC) $< ../servers/sdl_driver.cpp -o $@ -I/usr/include/SDL/ -lSDL
|
||||
|
||||
ois_driver_test: ois_driver_test.cpp
|
||||
$(GCC) $< ../servers/ois_driver.cpp -o $@ -I/usr/local/include/OGRE/ -lOgreMain -lOIS -lboost_filesystem
|
||||
|
||||
evtlist_test: evtlist_test.cpp ../filters/eventlist.hpp ../event.hpp
|
||||
$(GCC) $< -o $@
|
||||
|
||||
clean:
|
||||
rm *_test
|
@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include "../driver.hpp"
|
||||
#include <unistd.h>
|
||||
using namespace std;
|
||||
using namespace Mangle::Input;
|
||||
|
||||
Driver *input;
|
||||
|
||||
struct MyCB : Event
|
||||
{
|
||||
void event(Event::Type type, int i, const void *p)
|
||||
{
|
||||
cout << "got event: type=" << type << " index=" << i << endl;
|
||||
}
|
||||
};
|
||||
|
||||
void mainLoop(int argc, int quitKey)
|
||||
{
|
||||
cout << "Hold the Q key to quit:\n";
|
||||
input->setEvent(EventPtr(new MyCB));
|
||||
while(!input->isDown(quitKey))
|
||||
{
|
||||
input->capture();
|
||||
usleep(20000);
|
||||
|
||||
if(argc == 1)
|
||||
{
|
||||
cout << "You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
delete input;
|
||||
cout << "\nBye bye!\n";
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include "../filters/eventlist.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace Mangle::Input;
|
||||
|
||||
struct MyEvent : Event
|
||||
{
|
||||
int ii;
|
||||
MyEvent(int i) : ii(i) {}
|
||||
|
||||
void event(Event::Type type, int i, const void *p)
|
||||
{
|
||||
cout << " #" << ii << " got event: type=" << type << " index=" << i << endl;
|
||||
}
|
||||
};
|
||||
|
||||
EventList lst;
|
||||
|
||||
int iii=1;
|
||||
void make(int flags)
|
||||
{
|
||||
lst.add(EventPtr(new MyEvent(iii++)), flags);
|
||||
}
|
||||
|
||||
void send(Event::Type type)
|
||||
{
|
||||
cout << "Sending type " << type << endl;
|
||||
lst.event(type,0,NULL);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
make(Event::EV_ALL);
|
||||
make(Event::EV_KeyDown);
|
||||
make(Event::EV_KeyUp | Event::EV_MouseDown);
|
||||
|
||||
send(Event::EV_Unknown);
|
||||
send(Event::EV_KeyDown);
|
||||
send(Event::EV_KeyUp);
|
||||
send(Event::EV_MouseDown);
|
||||
|
||||
cout << "Enough of that\n";
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
#include "common.cpp"
|
||||
|
||||
#include "../servers/ois_driver.hpp"
|
||||
#include <Ogre.h>
|
||||
#include <OIS/OIS.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
bool isFile(const char *name)
|
||||
{
|
||||
boost::filesystem::path cfg_file_path(name);
|
||||
return boost::filesystem::exists(cfg_file_path);
|
||||
}
|
||||
|
||||
using namespace Ogre;
|
||||
using namespace OIS;
|
||||
|
||||
Root *root;
|
||||
RenderWindow *window;
|
||||
|
||||
void setupOgre()
|
||||
{
|
||||
// Disable logging
|
||||
new LogManager;
|
||||
Log *log = LogManager::getSingleton().createLog("");
|
||||
log->setDebugOutputEnabled(false);
|
||||
|
||||
bool useConfig = isFile("ogre.cfg");
|
||||
|
||||
// Set up Root
|
||||
root = new Root("plugins.cfg", "ogre.cfg", "");
|
||||
|
||||
// Configure
|
||||
if(!useConfig)
|
||||
root->showConfigDialog();
|
||||
else
|
||||
root->restoreConfig();
|
||||
|
||||
// Initialize OGRE window
|
||||
window = root->initialise(true, "test", "");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
setupOgre();
|
||||
input = new OISDriver(window);
|
||||
|
||||
mainLoop(argc, KC_Q);
|
||||
|
||||
delete root;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
Sending type 1
|
||||
#1 got event: type=1 index=0
|
||||
Sending type 2
|
||||
#1 got event: type=2 index=0
|
||||
#2 got event: type=2 index=0
|
||||
Sending type 4
|
||||
#1 got event: type=4 index=0
|
||||
#3 got event: type=4 index=0
|
||||
Sending type 16
|
||||
#1 got event: type=16 index=0
|
||||
#3 got event: type=16 index=0
|
||||
Enough of that
|
@ -0,0 +1,5 @@
|
||||
Hold the Q key to quit:
|
||||
got event: type=8 index=-1
|
||||
You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly
|
||||
|
||||
Bye bye!
|
@ -0,0 +1,5 @@
|
||||
Hold the Q key to quit:
|
||||
got event: type=1 index=-1
|
||||
You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly
|
||||
|
||||
Bye bye!
|
@ -0,0 +1,12 @@
|
||||
# Defines plugins to load
|
||||
|
||||
# Define plugin folder
|
||||
PluginFolder=/usr/local/lib/OGRE/
|
||||
|
||||
# Define plugins
|
||||
Plugin=RenderSystem_GL
|
||||
Plugin=Plugin_ParticleFX
|
||||
Plugin=Plugin_OctreeSceneManager
|
||||
# Plugin=Plugin_CgProgramManager
|
||||
|
||||
|
@ -0,0 +1,16 @@
|
||||
#include "common.cpp"
|
||||
|
||||
#include "../servers/sdl_driver.hpp"
|
||||
#include <SDL.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
|
||||
input = new SDLDriver();
|
||||
|
||||
mainLoop(argc, SDLK_q);
|
||||
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
make || exit
|
||||
|
||||
mkdir -p output
|
||||
|
||||
PROGS=*_test
|
||||
|
||||
for a in $PROGS; do
|
||||
if [ -f "output/$a.out" ]; then
|
||||
echo "Running $a:"
|
||||
./$a | diff output/$a.out -
|
||||
else
|
||||
echo "Creating $a.out"
|
||||
./$a > "output/$a.out"
|
||||
git add "output/$a.out"
|
||||
fi
|
||||
done
|
@ -0,0 +1,63 @@
|
||||
#ifndef MANGLE_REND2D_DRIVER_H
|
||||
#define MANGLE_REND2D_DRIVER_H
|
||||
|
||||
#include <string>
|
||||
#include "sprite.hpp"
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Rend2D
|
||||
{
|
||||
/**
|
||||
The driver is the connection to the backend system that powers
|
||||
2D sprite rendering. For example the backend could be SDL or
|
||||
any other 2D-capable graphics library.
|
||||
*/
|
||||
struct Driver
|
||||
{
|
||||
/// Get the screen sprite
|
||||
virtual Sprite *getScreen() = 0;
|
||||
|
||||
/// Sets the video mode.
|
||||
virtual void setVideoMode(int width, int height, int bpp=32, bool fullscreen=false) = 0;
|
||||
|
||||
/** Update the screen. Until this function is called, none of
|
||||
the changes written to the screen sprite will be visible.
|
||||
*/
|
||||
virtual void update() = 0;
|
||||
|
||||
/// Set the window title, as well as the title of the window
|
||||
/// when "iconified"
|
||||
virtual void setWindowTitle(const std::string &title,
|
||||
const std::string &icon) = 0;
|
||||
|
||||
/// Set the window title
|
||||
void setWindowTitle(const std::string &title) { setWindowTitle(title,title); }
|
||||
|
||||
/// Load sprite from an image file. Thows an exception on
|
||||
/// failure.
|
||||
virtual Sprite* loadImage(const std::string &file) = 0;
|
||||
|
||||
/// Load a sprite from an image file stored in memory. Throws
|
||||
/// exception on failure.
|
||||
virtual Sprite* loadImage(const void* data, size_t size) = 0;
|
||||
|
||||
/** @brief Set gamma value for all colors.
|
||||
|
||||
Note: Setting this in windowed mode will affect the ENTIRE
|
||||
SCREEN!
|
||||
*/
|
||||
virtual void setGamma(float gamma) = 0;
|
||||
|
||||
/// Set gamma individually for red, green, blue
|
||||
virtual void setGamma(float red, float green, float blue) = 0;
|
||||
|
||||
/// Get screen width
|
||||
virtual int width() = 0;
|
||||
|
||||
/// Get screen height
|
||||
virtual int height() = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,259 @@
|
||||
#include "sdl_driver.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <stdexcept>
|
||||
#include <assert.h>
|
||||
|
||||
using namespace Mangle::Rend2D;
|
||||
|
||||
const SpriteData *SDL_Sprite::lock()
|
||||
{
|
||||
// Make sure we aren't already locked
|
||||
assert(!data.pixels);
|
||||
|
||||
// Lock the surface and set up the data structure
|
||||
SDL_LockSurface(surface);
|
||||
|
||||
data.pixels = surface->pixels;
|
||||
data.w = surface->w;
|
||||
data.h = surface->h;
|
||||
data.pitch = surface->pitch;
|
||||
data.bypp = surface->format->BytesPerPixel;
|
||||
|
||||
return &data;
|
||||
}
|
||||
|
||||
void SDL_Sprite::unlock()
|
||||
{
|
||||
if(data.pixels)
|
||||
{
|
||||
SDL_UnlockSurface(surface);
|
||||
data.pixels = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// This is a really crappy and slow implementation, only intended for
|
||||
// testing purposes. Use lock/unlock for faster pixel drawing.
|
||||
void SDL_Sprite::pixel(int x, int y, int color)
|
||||
{
|
||||
SDL_LockSurface(surface);
|
||||
|
||||
int bpp = surface->format->BytesPerPixel;
|
||||
char *p = (char*)surface->pixels + y*surface->pitch + x*bpp;
|
||||
|
||||
switch(bpp)
|
||||
{
|
||||
case 1: *p = color; break;
|
||||
case 3:
|
||||
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
||||
{
|
||||
p[0] = (color >> 16) & 0xff;
|
||||
p[1] = (color >> 8) & 0xff;
|
||||
p[2] = color & 0xff;
|
||||
}
|
||||
else
|
||||
{
|
||||
p[0] = color & 0xff;
|
||||
p[1] = (color >> 8) & 0xff;
|
||||
p[2] = (color >> 16) & 0xff;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
*(int*)p = color;
|
||||
break;
|
||||
}
|
||||
SDL_UnlockSurface(surface);
|
||||
}
|
||||
|
||||
void SDL_Sprite::draw(Sprite *s, // Must be SDL_Sprite
|
||||
int x, int y, // Destination position
|
||||
int sx, int sy, // Source position
|
||||
int w, int h // Amount to draw. -1 means remainder.
|
||||
)
|
||||
{
|
||||
// Get source surface
|
||||
SDL_Sprite *other = dynamic_cast<SDL_Sprite*>(s);
|
||||
assert(other != NULL);
|
||||
SDL_Surface *img = other->getSurface();
|
||||
|
||||
// Check coordinate validity
|
||||
assert(sx <= img->w && sy <= img->h);
|
||||
assert(x <= surface->w && y <= surface->h);
|
||||
assert(sx >= 0 && sy >= 0);
|
||||
|
||||
// Compute width and height if necessary
|
||||
if(w == -1) w = img->w - sx;
|
||||
if(h == -1) h = img->h - sy;
|
||||
|
||||
// Check them if they're valid
|
||||
assert(w >= 0 && w <= img->w);
|
||||
assert(h >= 0 && h <= img->h);
|
||||
|
||||
SDL_Rect dest;
|
||||
dest.x = x;
|
||||
dest.y = y;
|
||||
dest.w = w;
|
||||
dest.h = h;
|
||||
|
||||
SDL_Rect src;
|
||||
src.x = sx;
|
||||
src.y = sy;
|
||||
src.w = w;
|
||||
src.h = h;
|
||||
|
||||
// Do the Blitman
|
||||
SDL_BlitSurface(img, &src, surface, &dest);
|
||||
}
|
||||
|
||||
SDL_Sprite::SDL_Sprite(SDL_Surface *s, bool autoDelete)
|
||||
: surface(s), autoDel(autoDelete)
|
||||
{
|
||||
assert(surface != NULL);
|
||||
data.pixels = NULL;
|
||||
}
|
||||
|
||||
SDL_Sprite::~SDL_Sprite()
|
||||
{
|
||||
if(autoDel)
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
||||
void SDL_Sprite::fill(int value)
|
||||
{
|
||||
SDL_FillRect(surface, NULL, value);
|
||||
}
|
||||
|
||||
int SDL_Sprite::width() { return surface->w; }
|
||||
int SDL_Sprite::height() { return surface->h; }
|
||||
|
||||
SDLDriver::SDLDriver() : display(NULL), realDisp(NULL), softDouble(false)
|
||||
{
|
||||
if (SDL_InitSubSystem( SDL_INIT_VIDEO ) == -1)
|
||||
throw std::runtime_error("Error initializing SDL video");
|
||||
}
|
||||
SDLDriver::~SDLDriver()
|
||||
{
|
||||
if(display) delete display;
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void SDLDriver::setVideoMode(int width, int height, int bpp, bool fullscreen)
|
||||
{
|
||||
unsigned int flags;
|
||||
|
||||
if(display) delete display;
|
||||
|
||||
if (fullscreen)
|
||||
// Assume fullscreen mode allows a double-bufferd hardware
|
||||
// mode. We need more test code for this to be safe though.
|
||||
flags = SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF;
|
||||
else
|
||||
flags = SDL_SWSURFACE;
|
||||
|
||||
// Create the surface and check it
|
||||
realDisp = SDL_SetVideoMode(width, height, bpp, flags);
|
||||
if(realDisp == NULL)
|
||||
throw std::runtime_error("Failed setting SDL video mode");
|
||||
|
||||
// Code for software double buffering. I haven't found this to be
|
||||
// any speed advantage at all in windowed mode (it's slower, as one
|
||||
// would expect.) Not properly tested in fullscreen mode with
|
||||
// hardware buffers, but it will probably only be an improvement if
|
||||
// we do excessive writing (ie. write each pixel on average more
|
||||
// than once) or try to read from the display buffer.
|
||||
if(softDouble)
|
||||
{
|
||||
// Make a new surface with the same attributes as the real
|
||||
// display surface.
|
||||
SDL_Surface *back = SDL_DisplayFormat(realDisp);
|
||||
assert(back != NULL);
|
||||
|
||||
// Create a sprite representing the double buffer
|
||||
display = new SDL_Sprite(back);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create a sprite directly representing the display surface.
|
||||
// The 'false' parameter means do not autodelete the screen
|
||||
// surface upon exit (since SDL manages it)
|
||||
display = new SDL_Sprite(realDisp, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the screen
|
||||
void SDLDriver::update()
|
||||
{
|
||||
// Blit the soft double buffer onto the real display buffer
|
||||
if(softDouble)
|
||||
SDL_BlitSurface(display->getSurface(), NULL, realDisp, NULL );
|
||||
|
||||
if(realDisp)
|
||||
SDL_Flip(realDisp);
|
||||
}
|
||||
|
||||
/// Set the window title, as well as the title of the window when
|
||||
/// "iconified"
|
||||
void SDLDriver::setWindowTitle(const std::string &title,
|
||||
const std::string &icon)
|
||||
{
|
||||
SDL_WM_SetCaption( title.c_str(), icon.c_str() );
|
||||
}
|
||||
|
||||
// Convert the given surface to display format.
|
||||
static SDL_Surface* convertImage(SDL_Surface* surf)
|
||||
{
|
||||
if(surf != NULL)
|
||||
{
|
||||
// Convert the image to the display buffer format, for faster
|
||||
// blitting
|
||||
SDL_Surface *surf2 = SDL_DisplayFormat(surf);
|
||||
SDL_FreeSurface(surf);
|
||||
surf = surf2;
|
||||
}
|
||||
return surf;
|
||||
}
|
||||
|
||||
/// Load sprite from an image file, using SDL_image.
|
||||
Sprite* SDLDriver::loadImage(const std::string &file)
|
||||
{
|
||||
SDL_Surface *surf = IMG_Load(file.c_str());
|
||||
surf = convertImage(surf);
|
||||
if(surf == NULL)
|
||||
throw std::runtime_error("SDL failed to load image file '" + file + "'");
|
||||
return spriteFromSDL(surf);
|
||||
}
|
||||
|
||||
/// Load sprite from an SDL_RWops structure. autoFree determines
|
||||
/// whether the RWops struct should be closed/freed after use.
|
||||
Sprite* SDLDriver::loadImage(SDL_RWops *src, bool autoFree)
|
||||
{
|
||||
SDL_Surface *surf = IMG_Load_RW(src, autoFree);
|
||||
surf = convertImage(surf);
|
||||
if(surf == NULL)
|
||||
throw std::runtime_error("SDL failed to load image");
|
||||
return spriteFromSDL(surf);
|
||||
}
|
||||
|
||||
/// Load a sprite from an image file stored in memory. Uses
|
||||
/// SDL_image.
|
||||
Sprite* SDLDriver::loadImage(const void* data, size_t size)
|
||||
{
|
||||
SDL_RWops *rw = SDL_RWFromConstMem(data, size);
|
||||
return loadImage(rw, true);
|
||||
}
|
||||
|
||||
void SDLDriver::setGamma(float red, float green, float blue)
|
||||
{
|
||||
SDL_SetGamma(red,green,blue);
|
||||
}
|
||||
|
||||
/// Convert an existing SDL surface into a sprite
|
||||
Sprite* SDLDriver::spriteFromSDL(SDL_Surface *surf, bool autoFree)
|
||||
{
|
||||
assert(surf);
|
||||
return new SDL_Sprite(surf, autoFree);
|
||||
}
|
||||
|
||||
void SDLDriver::sleep(int ms) { SDL_Delay(ms); }
|
||||
unsigned int SDLDriver::ticks() { return SDL_GetTicks(); }
|
@ -0,0 +1,125 @@
|
||||
#ifndef MANGLE_DRAW2D_SDL_H
|
||||
#define MANGLE_DRAW2D_SDL_H
|
||||
|
||||
#include "../driver.hpp"
|
||||
|
||||
// Predeclarations keep the streets safe at night
|
||||
struct SDL_Surface;
|
||||
struct SDL_RWops;
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Rend2D
|
||||
{
|
||||
/// SDL-implementation of Sprite
|
||||
struct SDL_Sprite : Sprite
|
||||
{
|
||||
/** Draw a sprite in the given position. Can only draw other SDL
|
||||
sprites.
|
||||
*/
|
||||
void draw(Sprite *s, // Must be SDL_Sprite
|
||||
int x, int y, // Destination position
|
||||
int sx=0, int sy=0, // Source position
|
||||
int w=-1, int h=-1 // Amount to draw. -1 means remainder.
|
||||
);
|
||||
|
||||
SDL_Sprite(SDL_Surface *s, bool autoDelete=true);
|
||||
~SDL_Sprite();
|
||||
|
||||
// Information retrieval
|
||||
int width();
|
||||
int height();
|
||||
SDL_Surface *getSurface() { return surface; }
|
||||
|
||||
// Fill with a given pixel value
|
||||
void fill(int value);
|
||||
|
||||
// Set one pixel
|
||||
void pixel(int x, int y, int value);
|
||||
|
||||
const SpriteData *lock();
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
// The SDL surface
|
||||
SDL_Surface* surface;
|
||||
|
||||
// Used for locking
|
||||
SpriteData data;
|
||||
|
||||
// If true, delete this surface when the canvas is destructed
|
||||
bool autoDel;
|
||||
};
|
||||
|
||||
class SDLDriver : public Driver
|
||||
{
|
||||
// The main display surface
|
||||
SDL_Sprite *display;
|
||||
|
||||
// The actual display surface. May or may not be the same
|
||||
// surface pointed to by 'display' above, depending on the
|
||||
// softDouble flag.
|
||||
SDL_Surface *realDisp;
|
||||
|
||||
// If true, we do software double buffering.
|
||||
bool softDouble;
|
||||
|
||||
public:
|
||||
SDLDriver();
|
||||
~SDLDriver();
|
||||
|
||||
/// Sets the video mode. Will create the window if it is not
|
||||
/// already set up. Note that for SDL, bpp=0 means use current
|
||||
/// bpp.
|
||||
void setVideoMode(int width, int height, int bpp=0, bool fullscreen=false);
|
||||
|
||||
/// Update the screen
|
||||
void update();
|
||||
|
||||
/// Set the window title, as well as the title of the window
|
||||
/// when "iconified"
|
||||
void setWindowTitle(const std::string &title,
|
||||
const std::string &icon);
|
||||
|
||||
// Include overloads from our Glorious parent
|
||||
using Driver::setWindowTitle;
|
||||
|
||||
/// Load sprite from an image file, using SDL_image.
|
||||
Sprite* loadImage(const std::string &file);
|
||||
|
||||
/// Load sprite from an SDL_RWops structure. autoFree determines
|
||||
/// whether the RWops struct should be closed/freed after use.
|
||||
Sprite* loadImage(SDL_RWops *src, bool autoFree=false);
|
||||
|
||||
/// Load a sprite from an image file stored in memory. Uses
|
||||
/// SDL_image.
|
||||
Sprite* loadImage(const void* data, size_t size);
|
||||
|
||||
/// Set gamma value
|
||||
void setGamma(float gamma) { setGamma(gamma,gamma,gamma); }
|
||||
|
||||
/// Set gamma individually for red, green, blue
|
||||
void setGamma(float red, float green, float blue);
|
||||
|
||||
/// Convert an existing SDL surface into a sprite
|
||||
Sprite* spriteFromSDL(SDL_Surface *surf, bool autoFree = true);
|
||||
|
||||
// Get width and height
|
||||
int width() { return display ? display->width() : 0; }
|
||||
int height() { return display ? display->height() : 0; }
|
||||
|
||||
/// Get the screen sprite
|
||||
Sprite *getScreen() { return display; }
|
||||
|
||||
/// Not really a graphic-related function, but very
|
||||
/// handly. Sleeps the given number of milliseconds using
|
||||
/// SDL_Delay().
|
||||
void sleep(int ms);
|
||||
|
||||
/// Get the number of ticks since SDL initialization, using
|
||||
/// SDL_GetTicks().
|
||||
unsigned int ticks();
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,311 @@
|
||||
#include "sdl_gl_driver.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_opengl.h>
|
||||
#include <stdexcept>
|
||||
#include <assert.h>
|
||||
|
||||
using namespace Mangle::Rend2D;
|
||||
|
||||
void SDLGL_Sprite::draw(Sprite *s, // Must be SDLGL_Sprite
|
||||
int x, int y, // Destination position
|
||||
int sx, int sy, // Source position
|
||||
int w, int h // Amount to draw. -1 means remainder.
|
||||
)
|
||||
{
|
||||
// Get source surface
|
||||
SDLGL_Sprite *other = dynamic_cast<SDLGL_Sprite*>(s);
|
||||
assert(other != NULL);
|
||||
SDL_Surface *img = other->getSurface();
|
||||
|
||||
// Check coordinate validity
|
||||
assert(sx <= img->w && sy <= img->h);
|
||||
assert(x <= surface->w && y <= surface->h);
|
||||
assert(sx >= 0 && sy >= 0);
|
||||
|
||||
// Compute width and height if necessary
|
||||
if(w == -1) w = img->w - sx;
|
||||
if(h == -1) h = img->h - sy;
|
||||
|
||||
// Check them if they're valid
|
||||
assert(w >= 0 && w <= img->w);
|
||||
assert(h >= 0 && h <= img->h);
|
||||
|
||||
SDL_Rect dest;
|
||||
dest.x = x;
|
||||
dest.y = y;
|
||||
dest.w = w;
|
||||
dest.h = h;
|
||||
|
||||
SDL_Rect src;
|
||||
src.x = sx;
|
||||
src.y = sy;
|
||||
src.w = w;
|
||||
src.h = h;
|
||||
|
||||
// Do the Blitman
|
||||
SDL_BlitSurface(img, &src, surface, &dest);
|
||||
}
|
||||
|
||||
SDLGL_Sprite::SDLGL_Sprite(SDL_Surface *s, bool autoDelete)
|
||||
: surface(s), autoDel(autoDelete)
|
||||
{
|
||||
assert(surface != NULL);
|
||||
}
|
||||
|
||||
SDLGL_Sprite::~SDLGL_Sprite()
|
||||
{
|
||||
if(autoDel)
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
||||
void SDLGL_Sprite::fill(int value)
|
||||
{
|
||||
SDL_FillRect(surface, NULL, value);
|
||||
}
|
||||
|
||||
int SDLGL_Sprite::width() { return surface->w; }
|
||||
int SDLGL_Sprite::height() { return surface->h; }
|
||||
|
||||
SDLGLDriver::SDLGLDriver() : display(NULL), realDisp(NULL)
|
||||
{
|
||||
if (SDL_InitSubSystem( SDL_INIT_VIDEO ) == -1)
|
||||
throw std::runtime_error("Error initializing SDL video");
|
||||
}
|
||||
SDLGLDriver::~SDLGLDriver()
|
||||
{
|
||||
if(display) delete display;
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Surface used for the screen. Since OpenGL surfaces must have sizes
|
||||
// that are powers of 2, we have to "fake" the returned display size
|
||||
// to match the screen, not the surface itself. If we don't use this,
|
||||
// the client program will get confused about the actual size of our
|
||||
// screen, thinking it is bigger than it is.
|
||||
struct FakeSizeSprite : SDLGL_Sprite
|
||||
{
|
||||
int fakeW, fakeH;
|
||||
|
||||
FakeSizeSprite(SDL_Surface *s, int fw, int fh)
|
||||
: SDLGL_Sprite(s), fakeW(fw), fakeH(fh)
|
||||
{}
|
||||
|
||||
int width() { return fakeW; }
|
||||
int height() { return fakeH; }
|
||||
};
|
||||
|
||||
static int makePow2(int num)
|
||||
{
|
||||
assert(num);
|
||||
if((num & (num-1)) != 0)
|
||||
{
|
||||
int cnt = 0;
|
||||
while(num)
|
||||
{
|
||||
num >>= 1;
|
||||
cnt++;
|
||||
}
|
||||
num = 1 << cnt;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
void SDLGLDriver::setVideoMode(int width, int height, int bpp, bool fullscreen)
|
||||
{
|
||||
unsigned int flags;
|
||||
|
||||
if(display) delete display;
|
||||
|
||||
flags = SDL_OPENGL;
|
||||
|
||||
if (fullscreen)
|
||||
flags |= SDL_FULLSCREEN;
|
||||
|
||||
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
||||
SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 1 );
|
||||
|
||||
// Create the surface and check it
|
||||
screen = SDL_SetVideoMode(width, height, bpp, flags);
|
||||
if(screen == NULL)
|
||||
throw std::runtime_error("Failed setting SDL video mode");
|
||||
|
||||
// Expand width and height to be powers of 2
|
||||
int width2 = makePow2(width);
|
||||
int height2 = makePow2(height);
|
||||
|
||||
// Create a new SDL surface of this size
|
||||
const SDL_PixelFormat& fmt = *(screen->format);
|
||||
realDisp = SDL_CreateRGBSurface(SDL_SWSURFACE,width2,height2,
|
||||
fmt.BitsPerPixel,
|
||||
fmt.Rmask,fmt.Gmask,fmt.Bmask,fmt.Amask);
|
||||
|
||||
// Create a sprite directly representing the display surface. This
|
||||
// allows the user to blit to it directly.
|
||||
display = new FakeSizeSprite(realDisp, width, height);
|
||||
|
||||
// Set up the OpenGL format
|
||||
nOfColors = fmt.BytesPerPixel;
|
||||
|
||||
if(nOfColors == 4)
|
||||
{
|
||||
if (fmt.Rmask == 0x000000ff)
|
||||
texture_format = GL_RGBA;
|
||||
else
|
||||
texture_format = GL_BGRA;
|
||||
}
|
||||
else if(nOfColors == 3)
|
||||
{
|
||||
if (fmt.Rmask == 0x000000ff)
|
||||
texture_format = GL_RGB;
|
||||
else
|
||||
texture_format = GL_BGR;
|
||||
}
|
||||
else
|
||||
assert(0 && "unsupported screen format");
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
// Have OpenGL generate a texture object handle for us
|
||||
glGenTextures( 1, &texture );
|
||||
|
||||
// Bind the texture object
|
||||
glBindTexture( GL_TEXTURE_2D, texture );
|
||||
|
||||
// Set the texture's stretching properties
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
||||
}
|
||||
|
||||
void SDLGLDriver::updateNoSwap()
|
||||
{
|
||||
if(!realDisp) return;
|
||||
|
||||
// Fist, set up the screen texture:
|
||||
|
||||
// Bind the texture object
|
||||
glBindTexture( GL_TEXTURE_2D, texture );
|
||||
|
||||
// Edit the texture object's image data
|
||||
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, realDisp->w, realDisp->h, 0,
|
||||
texture_format, GL_UNSIGNED_BYTE, realDisp->pixels );
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
|
||||
glLoadIdentity();
|
||||
|
||||
// OpenGL barf. Set up the projection to match our screen
|
||||
int vPort[4];
|
||||
glGetIntegerv(GL_VIEWPORT, vPort);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
glBegin( GL_QUADS );
|
||||
|
||||
// Needed to move the screen into the right place
|
||||
int diff = screen->h - realDisp->h;
|
||||
|
||||
// Bottom-left vertex (corner)
|
||||
glTexCoord2i( 0, 1 );
|
||||
glVertex3f(0,diff,0);
|
||||
|
||||
// Bottom-right vertex (corner)
|
||||
glTexCoord2i( 1, 1 );
|
||||
glVertex3f( realDisp->w, diff, 0.f );
|
||||
|
||||
// Top-right vertex (corner)
|
||||
glTexCoord2i( 1, 0 );
|
||||
glVertex3f( realDisp->w, screen->h, 0.f );
|
||||
|
||||
// Top-left vertex (corner)
|
||||
glTexCoord2i( 0, 0 );
|
||||
glVertex3f( 0, screen->h, 0.f );
|
||||
glEnd();
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPopMatrix();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void SDLGLDriver::swap()
|
||||
{
|
||||
SDL_GL_SwapBuffers();
|
||||
}
|
||||
|
||||
void SDLGLDriver::update()
|
||||
{
|
||||
updateNoSwap();
|
||||
swap();
|
||||
}
|
||||
|
||||
/// Set the window title, as well as the title of the window when
|
||||
/// "iconified"
|
||||
void SDLGLDriver::setWindowTitle(const std::string &title,
|
||||
const std::string &icon)
|
||||
{
|
||||
SDL_WM_SetCaption( title.c_str(), icon.c_str() );
|
||||
}
|
||||
|
||||
// Convert the given surface to display format.
|
||||
static SDL_Surface* convertImage(SDL_Surface* surf)
|
||||
{
|
||||
if(surf != NULL)
|
||||
{
|
||||
// Convert the image to the display buffer format, for faster
|
||||
// blitting
|
||||
SDL_Surface *surf2 = SDL_DisplayFormat(surf);
|
||||
SDL_FreeSurface(surf);
|
||||
surf = surf2;
|
||||
}
|
||||
return surf;
|
||||
}
|
||||
|
||||
/// Load sprite from an image file, using SDL_image.
|
||||
Sprite* SDLGLDriver::loadImage(const std::string &file)
|
||||
{
|
||||
SDL_Surface *surf = IMG_Load(file.c_str());
|
||||
surf = convertImage(surf);
|
||||
if(surf == NULL)
|
||||
throw std::runtime_error("SDL failed to load image file '" + file + "'");
|
||||
return spriteFromSDL(surf);
|
||||
}
|
||||
|
||||
/// Load sprite from an SDL_RWops structure. autoFree determines
|
||||
/// whether the RWops struct should be closed/freed after use.
|
||||
Sprite* SDLGLDriver::loadImage(SDL_RWops *src, bool autoFree)
|
||||
{
|
||||
SDL_Surface *surf = IMG_Load_RW(src, autoFree);
|
||||
surf = convertImage(surf);
|
||||
if(surf == NULL)
|
||||
throw std::runtime_error("SDL failed to load image");
|
||||
return spriteFromSDL(surf);
|
||||
}
|
||||
|
||||
/// Load a sprite from an image file stored in memory. Uses
|
||||
/// SDL_image.
|
||||
Sprite* SDLGLDriver::loadImage(const void* data, size_t size)
|
||||
{
|
||||
SDL_RWops *rw = SDL_RWFromConstMem(data, size);
|
||||
return loadImage(rw, true);
|
||||
}
|
||||
|
||||
void SDLGLDriver::setGamma(float red, float green, float blue)
|
||||
{
|
||||
SDL_SetGamma(red,green,blue);
|
||||
}
|
||||
|
||||
/// Convert an existing SDL surface into a sprite
|
||||
Sprite* SDLGLDriver::spriteFromSDL(SDL_Surface *surf, bool autoFree)
|
||||
{
|
||||
assert(surf);
|
||||
return new SDLGL_Sprite(surf, autoFree);
|
||||
}
|
||||
|
||||
void SDLGLDriver::sleep(int ms) { SDL_Delay(ms); }
|
||||
unsigned int SDLGLDriver::ticks() { return SDL_GetTicks(); }
|
@ -0,0 +1,132 @@
|
||||
#ifndef MANGLE_DRAW2D_SDLGL_H
|
||||
#define MANGLE_DRAW2D_SDLGL_H
|
||||
|
||||
/** This driver is similar to SDLDriver, except that it uses SDL on
|
||||
top of OpenGL.
|
||||
|
||||
I've decided to make it a separate file instead of just adding
|
||||
optional OpenGL support to the original, so that pure SDL users
|
||||
don't have to add OpenGL as a dependency.
|
||||
*/
|
||||
|
||||
#include "../driver.hpp"
|
||||
|
||||
// Predeclarations keep the streets safe at night
|
||||
struct SDL_Surface;
|
||||
struct SDL_RWops;
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Rend2D
|
||||
{
|
||||
/// SDL-implementation of Sprite
|
||||
struct SDLGL_Sprite : Sprite
|
||||
{
|
||||
/** Draw a sprite in the given position. Can only draw other SDL
|
||||
sprites.
|
||||
*/
|
||||
void draw(Sprite *s, // Must be SDLGL_Sprite
|
||||
int x, int y, // Destination position
|
||||
int sx=0, int sy=0, // Source position
|
||||
int w=-1, int h=-1 // Amount to draw. -1 means remainder.
|
||||
);
|
||||
|
||||
SDLGL_Sprite(SDL_Surface *s, bool autoDelete=true);
|
||||
~SDLGL_Sprite();
|
||||
|
||||
// Information retrieval
|
||||
virtual int width();
|
||||
virtual int height();
|
||||
SDL_Surface *getSurface() { return surface; }
|
||||
|
||||
// Fill with a given pixel value
|
||||
void fill(int value);
|
||||
|
||||
private:
|
||||
// The SDL surface
|
||||
SDL_Surface* surface;
|
||||
|
||||
// If true, delete this surface when the canvas is destructed
|
||||
bool autoDel;
|
||||
};
|
||||
|
||||
class SDLGLDriver : public Driver
|
||||
{
|
||||
// The main display surface
|
||||
SDLGL_Sprite *display;
|
||||
|
||||
// The screen surface. This is completely unused.
|
||||
SDL_Surface *screen;
|
||||
|
||||
// The display surface and main GL texture. These are used when
|
||||
// drawing the entire screen as one surface, as a drop-in
|
||||
// replacement for SDLDriver.
|
||||
SDL_Surface *realDisp;
|
||||
unsigned int texture;
|
||||
int nOfColors, texture_format;
|
||||
|
||||
public:
|
||||
SDLGLDriver();
|
||||
~SDLGLDriver();
|
||||
|
||||
/// Sets the video mode. Will create the window if it is not
|
||||
/// already set up. Note that for SDL, bpp=0 means use current
|
||||
/// bpp.
|
||||
void setVideoMode(int width, int height, int bpp=0, bool fullscreen=false);
|
||||
|
||||
/// Update the screen
|
||||
void update();
|
||||
|
||||
/// Calls SDL_GL_SwapBuffers
|
||||
void swap();
|
||||
|
||||
/// Draw surface to screen but do not call SDL_GL_SwapBuffers()
|
||||
void updateNoSwap();
|
||||
|
||||
/// Set the window title, as well as the title of the window
|
||||
/// when "iconified"
|
||||
void setWindowTitle(const std::string &title,
|
||||
const std::string &icon);
|
||||
|
||||
// Include overloads from our Glorious parent
|
||||
using Driver::setWindowTitle;
|
||||
|
||||
/// Load sprite from an image file, using SDL_image.
|
||||
Sprite* loadImage(const std::string &file);
|
||||
|
||||
/// Load sprite from an SDL_RWops structure. autoFree determines
|
||||
/// whether the RWops struct should be closed/freed after use.
|
||||
Sprite* loadImage(SDL_RWops *src, bool autoFree=false);
|
||||
|
||||
/// Load a sprite from an image file stored in memory. Uses
|
||||
/// SDL_image.
|
||||
Sprite* loadImage(const void* data, size_t size);
|
||||
|
||||
/// Set gamma value
|
||||
void setGamma(float gamma) { setGamma(gamma,gamma,gamma); }
|
||||
|
||||
/// Set gamma individually for red, green, blue
|
||||
void setGamma(float red, float green, float blue);
|
||||
|
||||
/// Convert an existing SDL surface into a sprite
|
||||
Sprite* spriteFromSDL(SDL_Surface *surf, bool autoFree = true);
|
||||
|
||||
// Get width and height
|
||||
int width() { return display ? display->width() : 0; }
|
||||
int height() { return display ? display->height() : 0; }
|
||||
|
||||
/// Get the screen sprite
|
||||
Sprite *getScreen() { return display; }
|
||||
|
||||
/// Not really a graphic-related function, but very
|
||||
/// handly. Sleeps the given number of milliseconds using
|
||||
/// SDL_Delay().
|
||||
void sleep(int ms);
|
||||
|
||||
/// Get the number of ticks since SDL initialization, using
|
||||
/// SDL_GetTicks().
|
||||
unsigned int ticks();
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,57 @@
|
||||
#ifndef MANGLE_REND2D_SPRITE_H
|
||||
#define MANGLE_REND2D_SPRITE_H
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Rend2D
|
||||
{
|
||||
/**
|
||||
A pointer to sprite data for direct drawing. Only to be used
|
||||
while the corresponding sprite is locked.
|
||||
*/
|
||||
struct SpriteData
|
||||
{
|
||||
void *pixels; // Pixel data
|
||||
int w, h; // Width and height
|
||||
int pitch, bypp; // Pitch (bytes) and bytes per pixel
|
||||
};
|
||||
|
||||
/**
|
||||
A Sprite is either a bitmap to be drawn or an output of area
|
||||
for blitting other bitmaps, or both. They are created by the
|
||||
Driver.
|
||||
*/
|
||||
struct Sprite
|
||||
{
|
||||
/// Draw a sprite in the given position
|
||||
virtual void draw(Sprite *s, // The sprite to draw
|
||||
int x, int y, // Destination position
|
||||
int sx=0, int sy=0, // Source position
|
||||
int w=-1, int h=-1 // Amount to draw. -1 means remainder.
|
||||
) = 0;
|
||||
|
||||
virtual ~Sprite() {}
|
||||
|
||||
// Information retrieval
|
||||
virtual int width() = 0;
|
||||
virtual int height() = 0;
|
||||
|
||||
/// Fill the sprite with the given pixel value. The pixel format
|
||||
/// depends on the format of the sprite.
|
||||
virtual void fill(int value) = 0;
|
||||
|
||||
/// Set one pixel value. The pixel format depends on the sprite
|
||||
/// format. This is not expected to be fast, and in some
|
||||
/// implementations may not work at all.
|
||||
virtual void pixel(int x, int y, int value) {}
|
||||
|
||||
/// Lock sprite for direct drawing, and return a struct
|
||||
/// containing the necessary pointer. When finished, unlock the
|
||||
/// sprite with unlock(). May return NULL, if so then direct
|
||||
/// drawing is not possible.
|
||||
virtual const SpriteData *lock() { return NULL; }
|
||||
virtual void unlock() {}
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1 @@
|
||||
*_test
|
@ -0,0 +1,15 @@
|
||||
GCC=g++ -Wall -Werror
|
||||
|
||||
all: sdl_test sdl_move_test sdlgl_move_test
|
||||
|
||||
sdl_test: sdl_test.cpp
|
||||
$(GCC) $< ../servers/sdl_driver.cpp -o $@ -I/usr/include/SDL/ -lSDL -lSDL_image
|
||||
|
||||
sdl_move_test: sdl_move_test.cpp ../servers/sdl_driver.cpp
|
||||
$(GCC) $^ -o $@ -I/usr/include/SDL/ -lSDL -lSDL_image
|
||||
|
||||
sdlgl_move_test: sdlgl_move_test.cpp ../servers/sdl_gl_driver.cpp
|
||||
$(GCC) $^ -o $@ -I/usr/include/SDL/ -lSDL -lSDL_image -lGL
|
||||
|
||||
clean:
|
||||
rm *_test
|
@ -0,0 +1,11 @@
|
||||
Loading SDL driver.
|
||||
Creating window.
|
||||
Current mode: 640x480
|
||||
Setting fancy title, cause we like fancy titles.
|
||||
Loading tile1-blue.png from file.
|
||||
Loading tile1-yellow.png from memory.
|
||||
Going bananas.
|
||||
Taking a breather.
|
||||
WOW DID YOU SEE THAT!?
|
||||
Mucking about with the gamma settings
|
||||
Done.
|
@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "../servers/sdl_driver.hpp"
|
||||
|
||||
using namespace Mangle::Rend2D;
|
||||
|
||||
int main()
|
||||
{
|
||||
SDLDriver sdl;
|
||||
|
||||
sdl.setVideoMode(640,480,0,false);
|
||||
sdl.setWindowTitle("Testing 123");
|
||||
Sprite *screen = sdl.getScreen();
|
||||
const char* imgName = "tile1-blue.png";
|
||||
Sprite *image = sdl.loadImage(imgName);
|
||||
|
||||
for(int frames=0; frames<170; frames++)
|
||||
{
|
||||
screen->fill(0);
|
||||
for(int j=0; j<10; j++)
|
||||
for(int i=0; i<25; i++)
|
||||
screen->draw(image, 2*frames+30*j, 20*i);
|
||||
sdl.update();
|
||||
sdl.sleep(10);
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "../servers/sdl_driver.hpp"
|
||||
|
||||
using namespace Mangle::Rend2D;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Loading SDL driver.\n";
|
||||
SDLDriver sdl;
|
||||
|
||||
cout << "Creating window.\n";
|
||||
sdl.setVideoMode(640,480);
|
||||
cout << "Current mode: " << sdl.width() << "x" << sdl.height() << endl;
|
||||
|
||||
cout << "Setting fancy title, cause we like fancy titles.\n";
|
||||
sdl.setWindowTitle("Chief executive window");
|
||||
|
||||
// Display surface
|
||||
Sprite *screen = sdl.getScreen();
|
||||
|
||||
const char* imgName = "tile1-blue.png";
|
||||
cout << "Loading " << imgName << " from file.\n";
|
||||
Sprite *image = sdl.loadImage(imgName);
|
||||
|
||||
const char* imgName2 = "tile1-yellow.png";
|
||||
cout << "Loading " << imgName2 << " from memory.\n";
|
||||
Sprite *image2;
|
||||
{
|
||||
// This is hard-coded for file sizes below 500 bytes, so obviously
|
||||
// you shouldn't mess with the image files.
|
||||
ifstream file(imgName2, ios::binary);
|
||||
char buf[500];
|
||||
file.read(buf, 500);
|
||||
int size = file.gcount();
|
||||
image2 = sdl.loadImage(buf, size);
|
||||
}
|
||||
|
||||
cout << "Going bananas.\n";
|
||||
for(int i=1; i<20; i++)
|
||||
screen->draw(image, 30*i, 20*i);
|
||||
|
||||
cout << "Taking a breather.\n";
|
||||
sdl.update();
|
||||
for(int i=1; i<20; i++)
|
||||
screen->draw(image2, 30*(20-i), 20*i);
|
||||
sdl.sleep(800);
|
||||
sdl.update();
|
||||
cout << "WOW DID YOU SEE THAT!?\n";
|
||||
sdl.sleep(800);
|
||||
|
||||
cout << "Mucking about with the gamma settings\n";
|
||||
sdl.setGamma(2.0, 0.1, 0.8);
|
||||
sdl.sleep(100);
|
||||
sdl.setGamma(0.6, 2.1, 2.1);
|
||||
sdl.sleep(100);
|
||||
sdl.setGamma(1.6);
|
||||
sdl.sleep(100);
|
||||
|
||||
cout << "Done.\n";
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "../servers/sdl_gl_driver.hpp"
|
||||
|
||||
using namespace Mangle::Rend2D;
|
||||
|
||||
int main()
|
||||
{
|
||||
SDLGLDriver sdl;
|
||||
|
||||
sdl.setVideoMode(640,480,0,false);
|
||||
sdl.setWindowTitle("Testing 123");
|
||||
Sprite *screen = sdl.getScreen();
|
||||
const char* imgName = "tile1-blue.png";
|
||||
Sprite *image = sdl.loadImage(imgName);
|
||||
|
||||
for(int frames=0; frames<170; frames++)
|
||||
{
|
||||
screen->fill(0);
|
||||
for(int j=0; j<10; j++)
|
||||
for(int i=0; i<25; i++)
|
||||
screen->draw(image, 2*frames+30*j, 20*i);
|
||||
sdl.update();
|
||||
sdl.sleep(5);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
make || exit
|
||||
|
||||
mkdir -p output
|
||||
|
||||
PROGS=*_test
|
||||
|
||||
for a in $PROGS; do
|
||||
if [ -f "output/$a.out" ]; then
|
||||
echo "Running $a:"
|
||||
./$a | diff output/$a.out -
|
||||
else
|
||||
echo "Creating $a.out"
|
||||
./$a > "output/$a.out"
|
||||
git add "output/$a.out"
|
||||
fi
|
||||
done
|
Binary file not shown.
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 273 B |
Binary file not shown.
Before Width: | Height: | Size: 257 B After Width: | Height: | Size: 257 B |
@ -0,0 +1 @@
|
||||
|
@ -0,0 +1,79 @@
|
||||
#ifndef MANGLE_SOUND_OGRELISTENERMOVER_H
|
||||
#define MANGLE_SOUND_OGRELISTENERMOVER_H
|
||||
|
||||
#include <OgreCamera.h>
|
||||
#include <assert.h>
|
||||
#include "../output.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/** This class lets a sound listener (ie. the SoundFactory) track a
|
||||
given camera in Ogre3D. The position and orientation of the
|
||||
listener will be updated to match the camera whenever the camera
|
||||
is moved.
|
||||
*/
|
||||
struct OgreListenerMover : Ogre::Camera::Listener
|
||||
{
|
||||
OgreListenerMover(Mangle::Sound::SoundFactoryPtr snd)
|
||||
: soundFact(snd), camera(NULL)
|
||||
{}
|
||||
|
||||
/// Follow a camera. WARNING: This will OVERRIDE any other
|
||||
/// MovableObject::Listener you may have attached to the camera.
|
||||
void followCamera(Ogre::Camera *cam)
|
||||
{
|
||||
camera = cam;
|
||||
camera->addListener(this);
|
||||
}
|
||||
|
||||
void unfollowCamera()
|
||||
{
|
||||
// If the camera is null, this object wasn't following a camera.
|
||||
// It doesn't make sense to call unfollow
|
||||
assert(camera != NULL);
|
||||
|
||||
camera->removeListener(this);
|
||||
camera = NULL;
|
||||
}
|
||||
|
||||
private:
|
||||
Mangle::Sound::SoundFactoryPtr soundFact;
|
||||
Ogre::Camera *camera;
|
||||
Ogre::Vector3 pos, dir, up;
|
||||
|
||||
/// From Camera::Listener. This is called once per
|
||||
/// frame. Unfortunately, Ogre doesn't allow us to be notified
|
||||
/// only when the camera itself has moved, so we must poll every
|
||||
/// frame.
|
||||
void cameraPreRenderScene(Ogre::Camera *cam)
|
||||
{
|
||||
assert(cam == camera);
|
||||
|
||||
Ogre::Vector3 nPos, nDir, nUp;
|
||||
|
||||
nPos = camera->getPosition();
|
||||
nDir = camera->getDirection();
|
||||
nUp = camera->getUp();
|
||||
|
||||
// Don't bother the sound system needlessly
|
||||
if(nDir != dir || nPos != pos || nUp != up)
|
||||
{
|
||||
pos = nPos;
|
||||
dir = nDir;
|
||||
up = nUp;
|
||||
|
||||
soundFact->setListenerPos(pos.x, pos.y, pos.z,
|
||||
dir.x, dir.y, dir.z,
|
||||
up.x, up.y, up.z);
|
||||
}
|
||||
}
|
||||
|
||||
void cameraDestroyed(Ogre::Camera *cam)
|
||||
{
|
||||
assert(cam == camera);
|
||||
camera = NULL;
|
||||
}
|
||||
};
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,31 @@
|
||||
#ifndef MANGLE_SOUND_OGREUPDATER_H
|
||||
#define MANGLE_SOUND_OGREUPDATER_H
|
||||
|
||||
/*
|
||||
This Ogre FrameListener calls update on a SoundFactory
|
||||
*/
|
||||
|
||||
#include <OgreFrameListener.h>
|
||||
#include "../output.hpp"
|
||||
#include <assert.h>
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
struct OgreOutputUpdater : Ogre::FrameListener
|
||||
{
|
||||
Mangle::Sound::SoundFactoryPtr driver;
|
||||
|
||||
OgreOutputUpdater(Mangle::Sound::SoundFactoryPtr drv)
|
||||
: driver(drv)
|
||||
{ assert(drv->needsUpdate); }
|
||||
|
||||
bool frameStarted(const Ogre::FrameEvent &evt)
|
||||
{
|
||||
driver->update();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
@ -0,0 +1,68 @@
|
||||
#ifndef MANGLE_INPUT_FILTER_H
|
||||
#define MANGLE_INPUT_FILTER_H
|
||||
|
||||
#include "../output.hpp"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/**
|
||||
@brief This filter class adds file loading capabilities to a
|
||||
Sound::SoundFactory class, by associating a SampleSourceLoader with
|
||||
it.
|
||||
|
||||
The class takes an existing SoundFactory able to load streams, and
|
||||
associates a SampleSourceLoader with it. The combined class is able
|
||||
to load files directly. */
|
||||
class InputFilter : public SoundFactory
|
||||
{
|
||||
protected:
|
||||
SoundFactoryPtr snd;
|
||||
SampleSourceLoaderPtr inp;
|
||||
|
||||
public:
|
||||
/// Empty constructor
|
||||
InputFilter() {}
|
||||
|
||||
/// Assign an input manager and a sound manager to this object
|
||||
InputFilter(SoundFactoryPtr _snd, SampleSourceLoaderPtr _inp)
|
||||
{ set(_snd, _inp); }
|
||||
|
||||
/// Assign an input manager and a sound manager to this object
|
||||
void set(SoundFactoryPtr _snd, SampleSourceLoaderPtr _inp)
|
||||
{
|
||||
inp = _inp;
|
||||
snd = _snd;
|
||||
|
||||
// Set capabilities
|
||||
needsUpdate = snd->needsUpdate;
|
||||
has3D = snd->has3D;
|
||||
canLoadStream = inp->canLoadStream;
|
||||
|
||||
// Both these should be true, or the use of this class is pretty
|
||||
// pointless
|
||||
canLoadSource = snd->canLoadSource;
|
||||
canLoadFile = inp->canLoadFile;
|
||||
assert(canLoadSource && canLoadFile);
|
||||
}
|
||||
|
||||
virtual SoundPtr load(const std::string &file)
|
||||
{ return loadRaw(inp->load(file)); }
|
||||
|
||||
virtual SoundPtr load(Stream::StreamPtr input)
|
||||
{ return loadRaw(inp->load(input)); }
|
||||
|
||||
virtual SoundPtr loadRaw(SampleSourcePtr input)
|
||||
{ return snd->loadRaw(input); }
|
||||
|
||||
virtual void update() { snd->update(); }
|
||||
virtual void setListenerPos(float x, float y, float z,
|
||||
float fx, float fy, float fz,
|
||||
float ux, float uy, float uz)
|
||||
{ snd->setListenerPos(x,y,z,fx,fy,fz,ux,uy,uz); }
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
#ifndef MANGLE_AUDIERE_OPENAL_H
|
||||
#define MANGLE_AUDIERE_OPENAL_H
|
||||
|
||||
#include "input_filter.hpp"
|
||||
#include "../sources/audiere_source.hpp"
|
||||
#include "../outputs/openal_out.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/// A InputFilter that adds audiere decoding to OpenAL. Audiere has
|
||||
/// it's own output, but OpenAL sports 3D and other advanced features.
|
||||
class OpenAL_Audiere_Factory : public InputFilter
|
||||
{
|
||||
public:
|
||||
OpenAL_Audiere_Factory()
|
||||
{
|
||||
set(SoundFactoryPtr(new OpenAL_Factory),
|
||||
SampleSourceLoaderPtr(new AudiereLoader));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,23 @@
|
||||
#ifndef MANGLE_FFMPEG_OPENAL_H
|
||||
#define MANGLE_FFMPEG_OPENAL_H
|
||||
|
||||
#include "input_filter.hpp"
|
||||
#include "../sources/ffmpeg_source.hpp"
|
||||
#include "../outputs/openal_out.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/// A InputFilter that adds ffmpeg decoding to OpenAL.
|
||||
class OpenAL_FFMpeg_Factory : public InputFilter
|
||||
{
|
||||
public:
|
||||
OpenAL_FFMpeg_Factory()
|
||||
{
|
||||
set(SoundFactoryPtr(new OpenAL_Factory),
|
||||
SampleSourceLoaderPtr(new FFMpegLoader));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
#ifndef MANGLE_MPG123_OPENAL_H
|
||||
#define MANGLE_MPG123_OPENAL_H
|
||||
|
||||
#include "input_filter.hpp"
|
||||
#include "../sources/mpg123_source.hpp"
|
||||
#include "../outputs/openal_out.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/// A InputFilter that adds mpg123 decoding to OpenAL. Only supports
|
||||
/// MP3 files.
|
||||
class OpenAL_Mpg123_Factory : public InputFilter
|
||||
{
|
||||
public:
|
||||
OpenAL_Mpg123_Factory()
|
||||
{
|
||||
set(SoundFactoryPtr(new OpenAL_Factory),
|
||||
SampleSourceLoaderPtr(new Mpg123Loader));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
#ifndef MANGLE_SNDFILE_OPENAL_H
|
||||
#define MANGLE_SNDFILE_OPENAL_H
|
||||
|
||||
#include "input_filter.hpp"
|
||||
#include "../sources/libsndfile.hpp"
|
||||
#include "../outputs/openal_out.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/// A InputFilter that adds libsnd decoding to OpenAL. libsndfile
|
||||
/// supports most formats except MP3.
|
||||
class OpenAL_SndFile_Factory : public InputFilter
|
||||
{
|
||||
public:
|
||||
OpenAL_SndFile_Factory()
|
||||
{
|
||||
set(SoundFactoryPtr(new OpenAL_Factory),
|
||||
SampleSourceLoaderPtr(new SndFileLoader));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,33 @@
|
||||
#ifndef MANGLE_SNDFILE_MPG123_OPENAL_H
|
||||
#define MANGLE_SNDFILE_MPG123_OPENAL_H
|
||||
|
||||
#include "input_filter.hpp"
|
||||
#include "source_splicer.hpp"
|
||||
#include "../sources/mpg123_source.hpp"
|
||||
#include "../sources/libsndfile.hpp"
|
||||
#include "../outputs/openal_out.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/// A InputFilter that uses OpenAL for output, and mpg123 (for MP3) +
|
||||
/// libsndfile (for everything else) to decode files. Can only load
|
||||
/// from the file system, and uses the file name to differentiate
|
||||
/// between mp3 and non-mp3 types.
|
||||
class OpenAL_SndFile_Mpg123_Factory : public InputFilter
|
||||
{
|
||||
public:
|
||||
OpenAL_SndFile_Mpg123_Factory()
|
||||
{
|
||||
SourceSplicer *splice = new SourceSplicer;
|
||||
|
||||
splice->add("mp3", SampleSourceLoaderPtr(new Mpg123Loader));
|
||||
splice->setDefault(SampleSourceLoaderPtr(new SndFileLoader));
|
||||
|
||||
set(SoundFactoryPtr(new OpenAL_Factory),
|
||||
SampleSourceLoaderPtr(splice));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,39 @@
|
||||
#ifndef MANGLE_VARIOUS_OPENAL_H
|
||||
#define MANGLE_VARIOUS_OPENAL_H
|
||||
|
||||
#include "input_filter.hpp"
|
||||
#include "source_splicer.hpp"
|
||||
#include "../sources/mpg123_source.hpp"
|
||||
#include "../sources/wav_source.hpp"
|
||||
#include "../outputs/openal_out.hpp"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/** A InputFilter that uses OpenAL for output, and load input from
|
||||
various individual sources, depending on file extension. Currently
|
||||
supports:
|
||||
|
||||
MP3: mpg123
|
||||
WAV: custom wav loader (PCM only)
|
||||
|
||||
This could be an alternative to using eg. 3rd party decoder
|
||||
libraries like libsndfile.
|
||||
*/
|
||||
class OpenAL_Various_Factory : public InputFilter
|
||||
{
|
||||
public:
|
||||
OpenAL_Various_Factory()
|
||||
{
|
||||
SourceSplicer *splice = new SourceSplicer;
|
||||
|
||||
splice->add("mp3", SampleSourceLoaderPtr(new Mpg123Loader));
|
||||
splice->add("wav", SampleSourceLoaderPtr(new WavLoader));
|
||||
|
||||
set(SoundFactoryPtr(new OpenAL_Factory),
|
||||
SampleSourceLoaderPtr(splice));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,72 @@
|
||||
#ifndef MANGLE_SOUND_OUTPUT_PUREFILTER_H
|
||||
#define MANGLE_SOUND_OUTPUT_PUREFILTER_H
|
||||
|
||||
#include "../output.hpp"
|
||||
|
||||
namespace Mangle
|
||||
{
|
||||
namespace Sound
|
||||
{
|
||||
// For use in writing other filters
|
||||
class SoundFilter : public Sound
|
||||
{
|
||||
protected:
|
||||
SoundPtr client;
|
||||
|
||||
public:
|
||||
SoundFilter(SoundPtr c) : client(c) {}
|
||||
void play() { client->play(); }
|
||||
void stop() { client->stop(); }
|
||||
void pause() { client->pause(); }
|
||||
bool isPlaying() const { return client->isPlaying(); }
|
||||
void setVolume(float f) { client->setVolume(f); }
|
||||
void setPan(float f) { client->setPan(f); }
|
||||
void setPos(float x, float y, float z)
|
||||
{ client->setPos(x,y,z); }
|
||||
void setPitch(float p) { client->setPitch(p); }
|
||||
void setRepeat(bool b) { client->setRepeat(b); }
|
||||
void setRange(float a, float b=0, float c=0)
|
||||
{ client->setRange(a,b,c); }
|
||||
void setStreaming(bool b) { client->setStreaming(b); }
|
||||
|
||||
// The clone() function is not implemented here, as you will
|
||||
// almost certainly want to override it yourself
|
||||
};
|
||||
|
||||
class FactoryFilter : public SoundFactory
|
||||
{
|
||||
protected:
|
||||
SoundFactoryPtr client;
|
||||
|
||||
public:
|
||||
FactoryFilter(SoundFactoryPtr c) : client(c)
|
||||
{
|
||||
needsUpdate = client->needsUpdate;
|
||||
has3D = client->has3D;
|
||||
canLoadFile = client->canLoadFile;
|
||||
canLoadStream = client->canLoadStream;
|
||||
canLoadSource = client->canLoadSource;
|
||||
}
|
||||
|
||||
SoundPtr loadRaw(SampleSourcePtr input)
|
||||
{ return client->loadRaw(input); }
|
||||
|
||||
SoundPtr load(Stream::StreamPtr input)
|
||||
{ return client->load(input); }
|
||||
|
||||
SoundPtr load(const std::string &file)
|
||||
{ return client->load(file); }
|
||||
|
||||
void update()
|
||||
{ client->update(); }
|
||||
|
||||
void setListenerPos(float x, float y, float z,
|
||||
float fx, float fy, float fz,
|
||||
float ux, float uy, float uz)
|
||||
{
|
||||
client->setListenerPos(x,y,z,fx,fy,fz,ux,uy,uz);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue