diff --git a/include/PPL/action.h b/include/PPL/action.h new file mode 100644 index 0000000..191552e --- /dev/null +++ b/include/PPL/action.h @@ -0,0 +1,44 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef ACTION_H +#define ACTION_H + +#include + +namespace PPL { + +class Action { +public: + virtual ~Action() = default; + virtual const std::string name() const = 0; + virtual void doAction() = 0; +}; + +} + +#endif // ACTION_H diff --git a/include/PPL/alcontextchanger.h b/include/PPL/alcontextchanger.h new file mode 100644 index 0000000..01be916 --- /dev/null +++ b/include/PPL/alcontextchanger.h @@ -0,0 +1,78 @@ +// Copyright (c) 2018, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef ALCONTEXTCHANGER_H +#define ALCONTEXTCHANGER_H + +#if APL == 1 +#include +#include +#elif IBM == 1 +#include +#include +#elif LIN == 1 +#include +#include +#else +#error "No platform defined" +#endif + +namespace PPL { + +/** + * @brief RAII class to change the openal context on construction and + * ensure the old context is restored on destruction. + * + * @author (c) 2009-2018 by Philipp Ringler + * @version 1.0 + */ +class ALContextChanger +{ +public: + + /** + * Switch to the openAL context given for the time this object lives. + * + * @param own_context the AL context to switch to + */ + ALContextChanger(ALCcontext* own_context); + + /** + * switch back to whatever context was active at the time the object was created. + */ + ~ALContextChanger(); + + ALContextChanger(const ALContextChanger&) = delete; + ALContextChanger& operator=(const ALContextChanger&) = delete; + +private: + ALCcontext* m_other_context; +}; + +} + +#endif // ALCONTEXTCHANGER_H diff --git a/include/PPL/alcontextmanager.h b/include/PPL/alcontextmanager.h new file mode 100644 index 0000000..6474b5f --- /dev/null +++ b/include/PPL/alcontextmanager.h @@ -0,0 +1,166 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef ALCONTEXTMANAGER_H +#define ALCONTEXTMANAGER_H + +#include +#include +#include + +#if APL == 1 +#include +#include +#elif IBM == 1 +#include +#include +#elif LIN == 1 +#include +#include +#else +#error "No platform defined" +#endif + +#include "alsoundbuffer.h" + +namespace PPL { + +/** + * Encapsulates all openAL and alut related stuff for looking up devices, handling different + * contexts etc. Stores SoundBuffer istances to keep track of the currently loaded sounds + * and provides convenient functions for standard operations like playing. + * + * @version 1.1 + * @author (c) 2009-2018 by Philipp Ringler + */ +class ALContextManager +{ +public: + class SoundNotFoundError : public std::runtime_error + { + public: + SoundNotFoundError(const std::string& msg): + std::runtime_error(msg) + {} + }; + + class SoundLoadError : public std::runtime_error + { + public: + SoundLoadError(const std::string& msg): + std::runtime_error(msg) + {} + }; + + class SoundPlayError : public std::runtime_error + { + public: + SoundPlayError(const std::string& msg): + std::runtime_error(msg) + {} + }; + + /** + * initializes openAL and alut and creates context + */ + ALContextManager(); + + /** + * deletes context and cleans up loaded sounds + */ + ~ALContextManager(); + + ALContextManager(const ALContextManager&) = delete; + ALContextManager& operator=(const ALContextManager&) = delete; + + /** + * tries to load a sound by a file (format depends on what alut distro supports) + * and stores it locally in a map, providing access by an integer key + * @param filename the sound to load as a path relative to X-Planes main directory + * @return unique integer id for adressing the sound buffer + * @exception throws a SoundLoadError if file cannot be found or has unacceptable format + */ + int addSoundFromFile(const std::string& filename); + + /** + * removes the sound from the map and deletes its buffer + * @param id the sound buffers id in the map + */ + void removeSound(int id); + + /** + * starts playback of the sound (playback continues when function returns) + * @param id the sound buffers id in the map + */ + bool playSound(int id, float volume=1.f); + + /** + * stops playback of the sound + * @param id the sound buffers id in the map + */ + void stopSound(int id); + + /** + * marks the sound to be played in a loop (when playback starts by play() ) + * @param id the sound buffers id in the map + */ + void loopSound(int id); + + /** + * removes the looping flag (playback does stop when the sound's end is reached) + * @param id the sound buffers id in the map + */ + void unLoopSound(int id); + + /** + * rewinds the sound to it's starting position + * @param id the sound buffers id in the map + */ + void rewindSound(int id); + + /** + * @return whether the sound is playing right now + * @param id the sound buffers id in the map + */ + bool isPlayingSound(int id); + + +private: + ALSoundBuffer* findSoundById(int id); + void deleteAllSounds(); + +private: + std::map> m_sounds; + int m_internal_counter; + ALCdevice* m_device; + ALCcontext* m_my_context; + ALCcontext* m_current_context; +}; + +} + +#endif // ALCONTEXTMANAGER_H diff --git a/include/PPL/alsoundbuffer.h b/include/PPL/alsoundbuffer.h new file mode 100644 index 0000000..96b3713 --- /dev/null +++ b/include/PPL/alsoundbuffer.h @@ -0,0 +1,139 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef ALSOUNDBUFFER_H +#define ALSOUNDBUFFER_H + +#include +#include + +#if APL == 1 +#include +#include +#elif IBM == 1 +#include +#include +#elif LIN == 1 +#include +#include +#else +#error "No platform defined" +#endif + + +namespace PPL { + +/** + * This class encapsulates openALs buffers and sources, does the resource handling + * and provides the listener setup. + * @version 1.0 + * @author (c) 2009-2018 by Philipp Ringler + */ +class ALSoundBuffer +{ +public: + class SoundPlayingError : public std::runtime_error + { + public: + SoundPlayingError(const std::string& msg): + std::runtime_error(msg) + {} + }; + + class SoundBufferError : public SoundPlayingError + { + public: + SoundBufferError(const std::string& msg): + SoundPlayingError(msg) + {} + }; + + class SoundSourceError : public SoundPlayingError + { + public: + SoundSourceError(const std::string& msg): + SoundPlayingError(msg) + {} + }; + + /** + * create a new soundbuffer by creating a buffer from a file (format depends + * on what alut distro supports). Sets up the openAL source at coordinate origin + * @param filename the sound to load as a path relative to X-Planes main directory + * @exception throws an SoundPlayingError if Buffer could not be created + */ + ALSoundBuffer(const std::string& filename); + + /** + * release the resources, delete buffer and source + */ + ~ALSoundBuffer(); + + ALSoundBuffer(const ALSoundBuffer&) = delete; + ALSoundBuffer& operator=(const ALSoundBuffer&) = delete; + + /** + * set up the listener at coordinate origin and play the sound buffer + * @return play command was successfull (false could mean invalid source or buffer) + */ + bool play(float volume); + + /** + * set source to looping the sound (effective next time play() called) + * @param yes loop true or false + */ + void setLoop(bool yes); + + /** + * stop playback of the sound + */ + void stop(); + + /** + * rewind to the start of the sound + */ + void rewind(); + + /** + * is the sound currently in playback + * @return bool is playing right now ? + */ + bool isPlaying() const; + +private: + std::string m_name; + ALuint m_buffer; + ALuint m_source; + ALboolean m_loop; +}; + +ALuint LoadWav(const std::string& fileName); + +} + + +#endif // ALSOUNDBUFFER_H diff --git a/include/PPL/basics.h b/include/PPL/basics.h new file mode 100644 index 0000000..ad07a0a --- /dev/null +++ b/include/PPL/basics.h @@ -0,0 +1,57 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef BASICS_H +#define BASICS_H + +#include +#include + + +namespace PPL { + +inline long ipow(long base, long exp) +{ + int result = 1; + while (exp) + { + if (exp & 1) + result *= base; + exp >>= 1; + base *= base; + } + return result; +} + +inline double mods(double y, double x) +{ + return y - x*std::floor(y/x); +} + +} + +#endif // BASICS_H diff --git a/include/PPL/dataref.h b/include/PPL/dataref.h new file mode 100644 index 0000000..ace6b4b --- /dev/null +++ b/include/PPL/dataref.h @@ -0,0 +1,591 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef DATAREF_H +#define DATAREF_H + +#include +#include +#include +#include + +#include "XPLMDataAccess.h" +#include "XPLMPlugin.h" + +#include "log.h" + +namespace PPL { + + +constexpr long DRE_MSG_ADD_DATAREF = 0x01000000; +const char* const DRE_PLUGIN_SINATURE = "xplanesdk.examples.DataRefEditor"; + + +/** + * @brief RWType distinguishes the three types of acess to datarefs. + * + * For simdata, it specifies if incoming data may be written to X-Plane + * and if data should be readable. + * For owned data, it specifies if the data is readable or writeable + * to OTHER plugins (or anything that accesses datarefs e.g. panels). + * @todo Should there be two enums for simdata and owned data? + */ +enum RWType { + ReadOnly = 1, + WriteOnly = 2, + ReadWrite = WriteOnly | ReadOnly + }; + +/** + * A generic base exception that is thrown when anything + * with looking up an X-Plane dataref and binding it goes wrong + */ +class LookupException : public std::runtime_error { +public: + LookupException(const std::string& msg): + runtime_error(msg) + {} +}; + +/** + * The dataref identifier could not be found in the plugin system's table + * of datarefs, neither X-Plane nor a plugin publishes it. + */ +class NotFoundException : public LookupException { +public: + NotFoundException(const std::string& msg): + LookupException(msg) + {} +}; + +/** + * The requested data type does not match the type specified in the + * plugin system's table of datarefs + */ +class IncompatibleTypeException : public LookupException { +public: + IncompatibleTypeException(const std::string& msg): + LookupException(msg) + {} +}; + +/** + * Indicates that a write-acess (write-only or read/write) to a + * read-only dataref was requested + */ +class NotWriteableException : public LookupException { +public: + NotWriteableException(const std::string& msg): + LookupException(msg) + {} +}; + +template +struct dataref_trait { + typedef T BasicType; +}; + +template<> +struct dataref_trait > { + typedef float BasicType; + mutable std::vector cache_; +}; + +template<> +struct dataref_trait > { + typedef int BasicType; + mutable std::vector cache_; +}; + +template<> +struct dataref_trait { + typedef char BasicType; + mutable std::vector cache_; +}; + +/** + * @brief Wrapper for access to datarefs published by XP itself or by other plugins. + * + * It wraps the lookup of datarefs, type-safe getting and setting of data, and + * obeying to X-Planes writeability restrictions. + * By creating a DataRef instance, it is bound to a specific dataref + * specified in the constructor. + * @author (c) 2009-2015 by Philipp Muenzel + * @version 2.0 + */ +template +class DataRef : private dataref_trait +{ +public: + + /** + * Sets up the dataref connection. + * looks up the datarefs and stores handler locally, also checks for correct + * type of data (sim-type) and correct read-/writeability + * @param identifier The identifier as in datarefs.txt as std::string + * @param writeability the writeability as defined by RWType + * @param share treat the dataref as a shared dataref, i.e. register the user as a shared dataref participant + * @exception LookupException is thrown if one of the following happens + * a) DataRef can not be found + * b) Data type is invalid (trying to access an int DataRef through float functions) + * c) data was requested to be writeable, but X-Plane says it is read-only + */ + DataRef(const std::string& identifier, RWType writeability = ReadOnly, bool share = false, bool publish_in_dre = false); + DataRef(const DataRef&) = delete; + + /** + * unshare dataref if it was created as shared + */ + virtual ~DataRef(); + + /** + * read the current value from X-Plane's plugin system + */ + operator SimType() const; + + /** + * write value to X-Plane + */ + const DataRef& operator=(const SimType& rhs); + + /** + * read value from other dataref and write it to this dataref + */ + const DataRef& operator=(const DataRef& rhs); + + /** + * @return does the actual value differ from the last history value + */ + bool hasChanged() const; + + /** + * save the actual value to the history + */ + void save(); + + /** + * overwrite the actual value with the last history value + */ + void undo(); + + /** + * offset history value from actual value, so the next hasChanged returns true. + * Does not change the actual value of the dataref. + */ + void forceChanged(); + + /** + * read the current value from X-Plane and access element in vector data + * @note is the same as operator SimType() for non-vector data + * @todo is there a more elegant way to do this? + */ + typename dataref_trait::BasicType operator[](std::size_t index) const; + + /** + * callback that invokes the template function specified in a sub-class, + * if the value of a shared dataref has been changed by someone else + */ + void notify() { doNotify(); } + + std::string name() const { return identifier_; } + + /** + * write value of vector element to X-Plane + * @note is the same as operator=() for non-vector data + * @todo is there a more elegant way to do this? + */ + void setVal(std::size_t, typename dataref_trait::BasicType val) + { + operator =(val); + } + + /** + * reserve as many elements in the history value array as X-Plane says the vector can hold at maximum + * @note does nothing for non-vector data + */ + void reserve() {} + + /** + * reserve this many elements in the history value array + * @note does nothing for non-vector data + */ + void reserve(std::size_t) {} + +private: + + static void NotifactionFunc(void* refcon) + { + DataRef* self = static_cast(refcon); + self->notify(); + } + + void shareDataRef(const std::string& identifier, bool publish_in_dre); + + void publishInDRE(); + + void share(int success, bool publish_in_dre); + + void unshareData(); + + void lookUp(const std::string& identifier); + + void checkWriteabilityIsValid(); + + void checkDataType(); + + void logErrorWithDataRef(const std::string& error_msg, const std::string& dataref_identifier); + + virtual void doNotify() {} //!< override this in a subclass if you want callback functionality + +private: + XPLMDataRef m_data_ref; //!< opaque handle to X-Plane's data + RWType m_read_write; //!< is the data required to be write- or readable + SimType m_history; //!< stores the last value to detect changes + bool shared_; + std::string identifier_; +}; + + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + + +template +DataRef::DataRef(const std::string& identifier, + RWType writeability, + bool share, + bool publish_in_dre): + m_data_ref(nullptr), + m_read_write(writeability), + shared_(false), + identifier_(identifier) +{ + try + { + if (share && XPLMFindDataRef(identifier.c_str()) == nullptr) + shareDataRef(identifier, publish_in_dre); + lookUp(identifier); + checkDataType(); + checkWriteabilityIsValid(); + } catch (LookupException& ex) + { + logErrorWithDataRef(ex.what(), identifier); + // We log only an error and re-throw the exception, so the + // caller may decide if this is a fatal error that requires an X-Plane exit. + throw; + } +} + + +template +DataRef::~DataRef() +{ + if (shared_) + unshareData(); +} + +template +const DataRef& DataRef::operator=(const DataRef& rhs) +{ + return operator=(SimType(rhs)); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +bool DataRef::hasChanged() const +{ + return true; +} + +template <> +bool DataRef::hasChanged() const; + +template <> +bool DataRef::hasChanged() const; + +template <> +bool DataRef::hasChanged() const; + +template <> +bool DataRef >::hasChanged() const; + +template <> +bool DataRef >::hasChanged() const; + +template <> +bool DataRef::hasChanged() const; + + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +void DataRef::forceChanged() +{ + m_history = std::numeric_limits::max(); +} + +template <> +void DataRef >::forceChanged(); + +template <> +void DataRef >::forceChanged(); + +template <> +void DataRef::forceChanged(); + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + + +template +void DataRef::save() +{ + m_history = operator SimType(); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +void DataRef::undo() +{ + operator=(m_history); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +void DataRef::shareDataRef(const std::string&, bool) +{ + throw IncompatibleTypeException("No type defined for sharing data"); +} + +template<> +void DataRef::shareDataRef(const std::string&, bool publish_in_dre); +template<> +void DataRef::shareDataRef(const std::string&, bool publish_in_dre); +template<> +void DataRef::shareDataRef(const std::string&, bool publish_in_dre); +template<> +void DataRef >::shareDataRef(const std::string&, bool publish_in_dre); +template<> +void DataRef >::shareDataRef(const std::string&, bool publish_in_dre); +template<> +void DataRef::shareDataRef(const std::string&, bool publish_in_dre); + +template +void DataRef::unshareData() +{ + throw IncompatibleTypeException("No type defined for sharing data"); +} + +template +void DataRef::share(int success, bool publish_in_dre) +{ + if (!success) + throw IncompatibleTypeException("Could not share data "+identifier_+" type mismatch with already existing data."); + shared_ = true; + if (publish_in_dre) + publishInDRE(); +} + +template<> +void DataRef::unshareData(); +template<> +void DataRef::unshareData(); +template<> +void DataRef::unshareData(); +template<> +void DataRef >::unshareData(); +template<> +void DataRef >::unshareData(); +template<> +void DataRef::unshareData(); + +template +void DataRef::publishInDRE() +{ + XPLMPluginID PluginID = XPLMFindPluginBySignature(DRE_PLUGIN_SINATURE); + if (PluginID != XPLM_NO_PLUGIN_ID) + XPLMSendMessageToPlugin(PluginID, DRE_MSG_ADD_DATAREF, static_cast(const_cast(identifier_.c_str()))); +} + + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + + +template +void DataRef::lookUp(const std::string& identifier) +{ + m_data_ref = XPLMFindDataRef(identifier.c_str()); + if(!m_data_ref) + throw NotFoundException(identifier+" not found in X-Plane's available Datarefs."); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +void DataRef::checkWriteabilityIsValid() +{ + if(m_read_write == WriteOnly || m_read_write == ReadWrite) + if (!XPLMCanWriteDataRef(m_data_ref)) + throw NotWriteableException(identifier_+ " declared to be writeable, but X-Plane says it is read-only."); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +void DataRef::checkDataType() +{ + throw IncompatibleTypeException(identifier_+" no type defined."); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +void DataRef::logErrorWithDataRef(const std::string& error_msg, const std::string& dataref_identifier) +{ + Log() << Log::Error << "When setting up dataref " << dataref_identifier << + " the following error occured: " << error_msg << Log::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +void DataRef::checkDataType(); + +template <> +void DataRef::checkDataType(); + +template <> +void DataRef::checkDataType(); + +template <> +void DataRef >::checkDataType(); + +template <> +void DataRef >::checkDataType(); + +template <> +void DataRef::checkDataType(); + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +DataRef::operator int() const; + +template <> +DataRef::operator float() const; + +template <> +DataRef::operator double() const; + +template <> +DataRef >::operator std::vector() const; + +template <> +DataRef >::operator std::vector() const; + +template <> +DataRef::operator std::string() const; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +const DataRef& DataRef::operator=(const int&); + +template <> +const DataRef& DataRef::operator=(const float&); + +template <> +const DataRef& DataRef::operator=(const double&); + +template <> +const DataRef >& DataRef >::operator=(const std::vector&); + +template <> +const DataRef >& DataRef >::operator=(const std::vector&); + +template <> +const DataRef& DataRef::operator=(const std::string&); + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +typename dataref_trait::BasicType DataRef::operator[](std::size_t) const +{ + typedef typename dataref_trait::BasicType T; + return T(*this); +} + +template<> +dataref_trait >::BasicType DataRef >::operator[](std::size_t index) const; + +template<> +dataref_trait >::BasicType DataRef >::operator[](std::size_t index) const; + +template<> +dataref_trait::BasicType DataRef::operator[](std::size_t index) const; + +template<> +void DataRef >::setVal(std::size_t pos, int val); +template<> +void DataRef >::setVal(std::size_t pos, float val); +template<> +void DataRef::setVal(std::size_t pos, char val); + +template<> +void DataRef >::reserve(std::size_t i); +template<> +void DataRef >::reserve(std::size_t i); +template<> +void DataRef::reserve(std::size_t i); + + +template<> +void DataRef >::reserve(); +template<> +void DataRef >::reserve(); +template<> +void DataRef::reserve(); + + +} + +#endif // DATAREF_H diff --git a/include/PPL/fontmgr.h b/include/PPL/fontmgr.h new file mode 100644 index 0000000..c29b8e3 --- /dev/null +++ b/include/PPL/fontmgr.h @@ -0,0 +1,330 @@ +/* + * Copyright (c) 2006, Ben Supnik and Chris Serio. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +#ifndef _Fonts_h_ +#define _Fonts_h_ + +#include +#include FT_FREETYPE_H + +#include +#include + +namespace PPL { + +using std::multimap; +using std::string; + +/* + + A BRIEF DIGRESSION ON FONT METRICS BY BEN.... + + We have to get our terms consistent (if not straight) if we are to have any hope of drawing fonts in the right + location. Some terms: + + - A "point" is a real-world physical unit...about 1/72th of an inch, more or less...see WikiPedia for more info + than you'd ever want to know. Since the old CRTs ran at about 72 DPI, a point and a pixel have become + somewhat interchangeable to old-school Mac font programmers. + + - A "pixel" is a single phosphor on your screen...one of the "dots" you can see. The DPI of the monitor tells + how big they are. Most monitors are around 90 DPI, more or less, I think...they used to be more like 72 DPI. + + - A "texel" is a pixel in a texture. Because OpenGL will draw our fonts almost anywhere, in any shape, including + in 3-d, it's not really proper to talk about how m any pixels tall a letter is unless we've very carefully + controlled a number of aspects of the OpenGL drawing environment. However the texture is built by the font + manager, so we can speak with total certainty about how many "texels" tall a letter is. + + - The "baseline" of a font is a line of reference that letters "sit on"...if you are in 1st grade and are learning + to right, you try to write on the baseline. + + - Letters that go below the baseline (like 'g' and 'p') are "descenders". The font "descent" is the distance from + the baseline to the lowest of the descenders. + + - The "ascent" is the distance from the baseline to the tallest letter. So the height of an A is the ascent + because it's real tall and sits on the baseline. + + - The line height is the distance from one baseline to the next when spacing multiple lines. + + - The point size of the font is the height from the lowest descender to tallest ascender (descent + ascent) in + points. + + Now that was a lot of fun but who cares? Well here's the KEY to understanding the fontmgr's layout: + + The font manager always renders fonts into their textures at a scale of 1 point = 1 texel! + + So...the "point size" you request should be based on roughly how many screen pixels you plan to cover. Want + your 'A' to be about 13 screen pixels? Well you have to pick the rectangle, but ask for a 13 point font so + that your 13 texel A looks good. + + Please note that this doesn't take into account the monitor resolution at all! If you draw at 1 texel = 1 pixel + and draw a 13 point font on a modern monitor, it will look SMALLER than a real-world 13 point font because + the font manager never takes the monitor DPI into account. Heck - the font manager doesn't KNOW the DPI of + your monitor! + + Point sizes are passed into the font manager as unsigned integers - no fractional point sizes for now. + + Line heights and other measurements are given in float point fractional texels. While a texel is either there + or not, we preserve the floating point metrics to provide the most accurate layout of the font. + + OPEN ISSUE: Hrm...the glyphs are rendered with rounding...can we capture that so the metrics match the real + texels? The FreeType manual isn't terribly clear on this. + + A WARNING ABOUT MULTIPLE OPENGL CONTEXTS: + + FontMgr attempts to only build up each font once. But it cannot tell if you have multiple OpenGL windows. Therefore + if you hvae multiple OpenGL windows you should share your texture contexts between them using the appropriate + windowing-system specific stuff. + + */ + +/* + * Opaque Font pointer... + * Declaraction is in the CPP + */ +typedef struct FontInfo_t* FontHandle; + +/* + * Enums to allow the user to specify the alignment of the + * text string that we're going to draw. + */ +enum { + ALIGN_LEFT = 0, + ALIGN_CENTER, + ALIGN_RIGHT +}; +typedef int FontAlign_t; + +/* + * Class FontMgr + * + * This class is essentially a wrapper around the FreeType 2.x font library. + * It uses FreeType to generate a pixmap texture of all possible characters in + * a given true-type font. Once the texture is generated for a particular font, + * FreeType resources are released and we then use the texture to draw text ourselves. + * + * The class enforces a 10px font interval to save on resources. That is, any texture + * generated must be on a 10px interval. If the client asks for a 13px texture and we + * already have a 10px one, he'll share that with the other user. If there is no 10px + * texture, we generate one for the client. + */ +class FontMgr { +public: + + FontMgr(); + ~FontMgr(); + + /* + * Load font takes the path to a TTF as well as a font size. This size specifies + * how big the font on the TEXTURE will be, NOT how big the font will be when + * it's drawn! If a size is specified that's not on an interval of 10px, it will + * be rounded to a 10px size, unless inRequireExactSize is true, in which case + * you get the precise requested size. + * This flag also affects filtering: nearest neighbor when exact sizes are requested, + * for a sharp look, otherwise linear filtering for a scalable look. + * This function returns a FontHandle to be used as a reference for draw and unload + * requests. + * NOTE: Texture sharing is implemented for multiple requests for the same font. + * Each call to LoadFont increments a reference count for the texture whether + * it is created or shared. + */ + FontHandle loadFont( + const char* inFontPath, + const char * inStartMem, // null if we open file + const char * inEndMem, // null if we open file + unsigned int inSizePx, + bool inRequireExactSize); + + /* + * Unload font takes a font handle and will decrement the reference count of a + * particular font. When no one is using the font anymore, it will be unloaded + * from memory and the texture will be deleted. + */ + void unloadFont( + FontHandle inFont); + + /* + * This is DEBUGGING function that will display the pixmap texture on the screen + * so that you can see what the font looks like. + */ + void displayTexture( + FontHandle inFont); + + /* + * Simple Drawing: draws a left-justified string starting at a point. The first + * letter will sit on a line starting at X, and Y (e.g. Y is the basline). Font is + * drawn such that one texel = one modelview unit. So this is only appropriate if + * you are using OpenGL with "pixels" as a drawing unit. + * + */ + void drawString( + FontHandle inFont, + float color[4], // 4-part color, featuring alpha. + float inX, + float inY, + const char * inString); + + /* + * Simple Drawing: draws a left- or right justified string starting at a point. The first + * letter will sit on a line starting at X, and Y (e.g. Y is the basline). Font is + * drawn such that one texel = one modelview unit. So this is only appropriate if + * you are using OpenGL with "pixels" as a drawing unit. + * + */ + void drawString( + FontHandle inFont, + float color[4], // 4-part color, featuring alpha. + float inX, + float inY, + FontAlign_t align, + const char * inString); + + /* + * Advanced drawing: draws a string justified within a box. The bottom of the box is + * the lowest descender, the top is the highest ascender plus any built-in leading. + * Justification: inRight is ignored for left-justified strings. The whole string is + * drawn - even parts that exceed the passed in box. + * + */ + void drawRange( + FontHandle inFont, + float color[4], // 4-part color, featuring alpha. + float inLeft, + float inBottom, + float inRight, + float inTop, + const char * inStart, + const char * inEnd, + FontAlign_t fontAlign); + + /***************** FONT MEASUREMENT ROUTINES ****************** + * + * These routines measure in "pixels" - that is, screen units. So you have to provide + * how tall the line will end up and they return how wide it will be. + * + */ + + /* + * Given a font and a height in px, this function returns the length necessary + * to draw the string in px. + * + * OPEN ISSUE: change to start, end + 1 syntax? + * + */ + float measureString( + FontHandle inFont, + float inHeightPx, + const char * inString); + + float measureRange( + FontHandle inFont, + float inHeightPx, + const char * inStart, + const char * inEnd); + + /* + * Given a string, return how many characters will fit in a given space, either + * forward or backward. Strings are specified as a ptr to the start and a ptr to + * the end plus 1. + * + */ + int fitForward( + FontHandle inFont, + float inHeightPx, + float inWidthPx, + const char * inStringStart, + const char * inStringEnd); + + int fitReverse( + FontHandle inFont, + float inHeightPx, + float inWidthPx, + const char * inStringStart, + const char * inStringEnd); + + /******************** FONT METRICS ************************* + * + * These routines return the measurements of the font in TEXELS. + * You pass in the point size of the font. Why? Well, if you are + * using a scalable font, the point size you asked for may not be + * the one in the font handle. These routines take this into + * consideration. + * + * OPEN ISSUE perhaps the font handle should know the actual font size + * and the requested font size? Passing the size in pixels is weird. + * + */ + + /* + * Returns the line height - that is the number of texels from + * one baseline to another. + * + */ + float getLineHeight( + FontHandle inFont, + unsigned int inFontSizePx); + + /* + * Returns the number of texels from the baseline to the lowest + * descender (this is like the bottom of o to the bottom of g). + * + */ + float getLineDescent( + FontHandle inFont, + unsigned int inFontSizePx); + + /* + * Returns the number of texels from the baseline to the top of + * the tallest ascender. This is like the height of the letter A. + * + */ + float getLineAscent( + FontHandle inFont, + unsigned int inFontSizePx); + +private: + + void CalcTexSize( + FT_Face* inFace, + int* outHeight, + int* outWidth); + + void CopyBitmapSection( + unsigned int inSrcHeight, + unsigned int inSrcWidth, + unsigned int inSrcLeft, + unsigned int inSrcTop, + unsigned char* inSrcData, + unsigned int inDstHeight, + unsigned int inDstWidth, + unsigned int inDstLeft, + unsigned int inDstTop, + unsigned char* inDstData); + + typedef multimap TextureMap_t; + + FT_Library library_; + TextureMap_t tex_map_; +}; + +} + +#endif diff --git a/include/PPL/log.h b/include/PPL/log.h new file mode 100644 index 0000000..17e4bca --- /dev/null +++ b/include/PPL/log.h @@ -0,0 +1,143 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include + +namespace PPL { + +class LogWriter; + +enum Level { + L_UNSPEC=-1, //!< unspecified, occurs when no severity function pointer was called + L_FAIL = 0, //!< Failure, forces quit of application + L_ERROR = 1, //!< Error, permits keeping the application running + L_WARN = 2, //!< Warning + L_INFO = 3 //!< Information +}; + +/** + * @brief Every log message that arises from libcanaero is in this format. + */ +struct LogEntry { + Level lvl; //!< Severity of the occured event, see enum + time_t time; //!< Timestamp + std::string txt; //!< Human-readable message +}; + + +/** + * @brief A stream-based logger using any LogWriter. + * A Log instance corresponds to ONE log entry. + * + * Creating an instance provides the stream to which a log message is written. + * Specifying severity level and Log::endl are mandatory. + * @code Log() << Log::Error << "This is an error" << Log::endl; + * Log() << Log::Info << "This is an information about " << some_integer << Log::endl @endcode + * @author (c) 2009-2011 by Philipp Münzel + * @version 1.4 + */ +class Log { +public: + /** + * create a new Logstream for ONE new message + */ + Log(); + + /** + * Indicate the following message has severity Information + */ + static Log& Info(Log& log); + + /** + * Indicate the following message has severity Warnining + */ + static Log& Warn(Log& log); + + /** + * Indicate the following message has severity Error + */ + static Log& Error(Log& log); + + /** + * Indicate the following message has severity Failure + */ + static Log& Fail(Log& log); + + /** + * Terminate the log stream. This is mandatory! + */ + static Log& endl(Log& log); + + /** + * Log an integer. + * @param i + */ + Log& operator<<(int32_t i); + + /** + * Log an integer. + * @param i + */ + Log& operator<<(int16_t i); + + /** + * Log a double. + * @param d + */ + Log& operator<<(double d); + + /** + * Log a char + * @param c + */ + Log& operator<<(char c); + + /** + * Log a string. + * @param s + */ + Log& operator<<(const std::string& s); + + /** + * Invoke a function pointer on the logstream (set severity or terminate) + */ + Log& operator<<(Log& (*f)(Log&)); + +private: + Level m_severity; + LogWriter& m_writer; + std::ostringstream m_stream; +}; +} + +#endif // LOG_H diff --git a/include/PPL/logichandler.h b/include/PPL/logichandler.h new file mode 100644 index 0000000..ff9cfd7 --- /dev/null +++ b/include/PPL/logichandler.h @@ -0,0 +1,132 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef logichandler_h +#define logichandler_h + +#include +#include + +namespace PPL { + +/** + * abstract class for all kinds of processing that takes values from XP, + * do calculations in the flightloop and publish the result in a custom Dataref + * @author (c) 2009-2011 by Philipp Muenzel + * @version 0.4 + */ +class LogicHandler { + + public: + + + virtual ~LogicHandler() = default; + + /** + * reimplement to do the setups that have to be done once when data is acessible + */ + virtual bool initializeAtStart() = 0; + + + /** + * reimplement this function to re-initialize when a new user aircraft is loaded + */ + virtual bool initializeOnAircraftChange() = 0; + + + /** + * reimplement to do the calculations that are done periodically (via the flightloop) + */ + virtual bool processInTheLoop() = 0; + + + /** + * reimplement to process a signal from outside (e.g. interplugin message) + */ + virtual bool processInput(long input, void* param) = 0; + + + /** + * reimplement to return the value for the next call of your processing- positive for seconds, negative for ticks + */ + virtual float loopFrequency() = 0; + + + /** + * reimplement to suspend all processing by this handler if asked + */ + virtual void suspend(bool yes) = 0; + + + /** + * reimplement to tell if this handler is currenty suspended + */ + virtual bool isSuspended() const= 0; + + + /** + * name of this handler + */ + virtual std::string name() const= 0; + + /** + * enable processing for this handler and register in X-Planes flight-loop + */ + void hookToSim(); + + /** + * stop processing and unregister from X-Planes flight-loop + */ + void unhookFromSim(); + +private: + + /** + * reimplement to register for the data needed for calculation + */ + virtual bool aquireSimData() = 0; + + + /** + * reimplement to register the acessors to the owned data to form a custom dataref(s) + */ + virtual bool publishData() = 0; + + + /** + * reimplement to unregister the custom dataref(s) + */ + virtual bool withdrawPublishedData() = 0; + +}; + +float HandlerCallbackInit(float, float, int, void* inRefCon); +float HandlerCallbackProcess(float, float, int, void* inRefCon); + +} + +#endif diff --git a/include/PPL/logwriter.h b/include/PPL/logwriter.h new file mode 100644 index 0000000..f8a5e09 --- /dev/null +++ b/include/PPL/logwriter.h @@ -0,0 +1,74 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef LOGWRITER_H +#define LOGWRITER_H + +#include +#include "log.h" + +namespace PPL { + +/** + * @brief Logger implementation that logs to file. + * + * By default, this is implemented by ConsoleLogger. If you want to log + * to a file or GUI instead, override the LogWriter::writeString function. + * @author (c) 2009-2011 by Philipp Münzel + * @version 1.3 + */ +class LogWriter +{ +public: + LogWriter(); + + ~LogWriter(); + + LogWriter(const LogWriter&) = delete; + LogWriter& operator=(const LogWriter&) = delete; + + /** + * Set a file to log all outputs to. If a log happens before + * this funtion was called (e.g. static initializations), output is logged to stdout + */ + void setLogFile(const std::string& filename); + + /** + * Post a log entry to the log queue. + * Can be called from any thread. + */ + void postEntry(const LogEntry& entry); + + static LogWriter& getLogger(); + +private: + std::ofstream m_logfile; +}; + +} + +#endif // LOGWRITER_H diff --git a/include/PPL/menuitem.h b/include/PPL/menuitem.h new file mode 100644 index 0000000..c9f3ded --- /dev/null +++ b/include/PPL/menuitem.h @@ -0,0 +1,62 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef MENUITEM_H +#define MENUITEM_H + +#include +#include +#include + +#include "XPLMDisplay.h" +#include "XPLMMenus.h" +#include "XPWidgets.h" +#include "XPStandardWidgets.h" + +namespace PPL { + +class Action; + +class MenuItem +{ +public: + MenuItem(const std::string& title); + ~MenuItem(); + void addSubItem(std::unique_ptr&& action); + void menuHandler(void* iRef); +private: + static void menuHandler(void* mRef, void* iRef); + int m_item_id; + std::vector> m_actions; + XPLMMenuID m_menu_id; +}; + + +} + + +#endif // MENUITEM_H diff --git a/include/PPL/messagewindow.h b/include/PPL/messagewindow.h new file mode 100644 index 0000000..8fd1df0 --- /dev/null +++ b/include/PPL/messagewindow.h @@ -0,0 +1,185 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef MESSAGEWINDOW_H +#define MESSAGEWINDOW_H + +#include +#include +#include + +#include "XPWidgets.h" +#include "XPStandardWidgets.h" + +#include "dataref.h" + +namespace PPL { + +/** + * A convenient way to display a message box with a title and a longer text message + * that quits X-Plane when closed. This is a very good way to handle improper setups, + * blame the user, and force quit afterwards + * @version 1.0 + * @author (c) 2009-2011 by Philipp Muenzel + */ +class MessageWindow +{ +public: + /** + * This exception is thrown during setup of a Message box, if the requested + * size of the box is too large to fit in the simulator window. + */ + class BoxOutOfBoundsException: public std::runtime_error { + public: + BoxOutOfBoundsException(const std::string& msg): + std::runtime_error(msg) + {} + }; + + /** + * construct message box centered in the X-Plane window, with title and auto-breaking message + * @param width in pixels + * @param height in pixels + * @param title text in the titlebar + * @param message text gets automatically br'd according to window width + * @param quit whether the simulator should be closed after the message was confirmed + */ + MessageWindow(int width, int height, const std::string& title, const std::string& message, bool quit); + + /** + * destroy window and all child windows + */ + ~MessageWindow(); + + MessageWindow(const MessageWindow&) = delete; + MessageWindow& operator=(const MessageWindow&) = delete; + + /** + * @param width in pixels + * @exception BoxOutOfBoundsException if width is inappropriate + */ + void setWidth(int width); + + /** + * @param height in pixels + * @exception BoxOutOfBoundsException if height is inappropriate + */ + void setHeight(int height); + + /** + * @param title to show in the title bar of the message box + */ + void setTitle(const std::string& title); + + /** + * @param message to show in the message box + */ + void setMessage(const std::string& message); + + /** + * @param quit quit the sime after the user confirms the dialog? yes or no + */ + void quitSimOnConfirm(bool quit); + + /** + * display the window in the sim, forces user to acknownledge the dialog + */ + void display(); + + /** + * @return is the window currently drawn by X-Plane + */ + bool isDisplayed(); + + /** + * internal processing of message from X-Plane's widget system + * it was already figured out that we are adressed + * @param message + * @param param1 + * @param param2 + */ + int processMessages(XPWidgetMessage message, intptr_t param1, intptr_t param2); + + /** + * static widget callback to register in X-Plane's widget logic + * retrieves the instance via a pointer-to-object stored in the widget struct + */ + static int widgetCallback(XPWidgetMessage, XPWidgetID, intptr_t, intptr_t); + +private: + + /** + * split a string in words and store them in the list + * @param L list of strings where words are stored + * @param seq original string to split up + * @param _1cdelim string that declares a splitting point + * @param _removews remove white spaces + */ + int splitStr(std::list& L, const std::string& seq, const std::string& _1cdelim, bool _removews ); + + /** + * create the box widget with title bar and close buttons + */ + void createSurroundingBox(); + + /** + * create the inner panel on which strings are drawn + */ + void createInnerScreen(); + +private: + + int m_left; //!< upper left corner left offset in pixels from X-Plane main window + + int m_right; //!< upper right corner right offset in pixels from X-Plane main window + + int m_top; //!< upper left corner upper offset in pixels from X-Plane main window + + int m_bottom; //!< lower left corner bottom offset in pixels from X-Plane main window + + std::string m_title; //!< title bar caption + + std::string m_message; //!< message to display + + bool m_quit_on_confirm; //!< quit X-Plane when user closes message box + + bool m_is_displayed; + + XPWidgetID m_box_widget; + + XPWidgetID m_screen_widget; + + std::vector m_caption_widgets_list; + + DataRef m_xp_width; //!< X-Plane main window width + + DataRef m_xp_height; //!< X-Plane main window height +}; + +} + +#endif // MESSAGEWINDOW_H diff --git a/include/PPL/onscreendisplay.h b/include/PPL/onscreendisplay.h new file mode 100644 index 0000000..ad28783 --- /dev/null +++ b/include/PPL/onscreendisplay.h @@ -0,0 +1,61 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef ONSCREENDISPLAY_H +#define ONSCREENDISPLAY_H + +#include "XPWidgetDefs.h" +#include "XPLMDisplay.h" +#include "dataref.h" + +namespace PPL { + +class OnScreenDisplay { +public: + OnScreenDisplay(int width, int height, const std::string& title); + + ~OnScreenDisplay(); + + OnScreenDisplay(const OnScreenDisplay&) = delete; + OnScreenDisplay& operator=(const OnScreenDisplay&) = delete; + + int processMessages(XPWidgetMessage inMessage, intptr_t, intptr_t); + + static int widgetCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t param1, intptr_t param2); + +private: + DataRef screen_w_, screen_h_; + DataRef vr_enabled_; + XPWidgetID widget_id_; + XPLMWindowID window_id_; + int top_, left_, bottom_, right_; + std::string title_; +}; + +} + +#endif // ONSCREENDISPLAY_H diff --git a/include/PPL/overlaygauge.h b/include/PPL/overlaygauge.h new file mode 100644 index 0000000..ab4b4f6 --- /dev/null +++ b/include/PPL/overlaygauge.h @@ -0,0 +1,156 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef OVERLAYWINDOW_H +#define OVERLAYWINDOW_H + +#include "XPLMDisplay.h" +#include "dataref.h" + +#if APL == 1 +#include +#elif IBM == 1 +#include +#include "GL/glew.h" +#include +#elif LIN == 1 +#include +#include +#endif + +namespace PPL { + +class OverlayGauge +{ +public: + OverlayGauge(int left2d, int top2d, int width2d, int height2d, int left3d, int top3d, int width3d, int height3d, + int frameOffX, int frameOffY, int textureId3d, bool allow_keyboard = true, bool is_visible3d = true, + bool is_visible2d = false, bool always_draw_3d = false, bool allow_3d_click = false, float scale_3d = 1.0f, + bool double_size = false, int panel_render_pass = 2); + virtual ~OverlayGauge(); + OverlayGauge(const OverlayGauge&) = delete; + OverlayGauge& operator=(const OverlayGauge&) = delete; + + void set3d(int left3d, int top3d, int width3d, int height3d, int texture_id, bool always_draw_3d); + void setCopy3d(int left3dcopy, int top3dcopy); + void disable3d(); + bool isVisible() const; + void setVisible(bool b); + void frame(); + int draw3dCallback(XPLMDrawingPhase phase, int is_before); + void toggleKeyboardFocus(); + + void draw2dWindowCallback(XPLMWindowID window_id); + void handle2dKeyCallback(XPLMWindowID window_id, char key, XPLMKeyFlags flags, unsigned char virtual_key, int losing_focus); + int handle2dClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse); + int handle2dRightClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse); + XPLMCursorStatus handle2dCursorCallback(XPLMWindowID window_id, int x, int y); + int handle2dWheelCallback(XPLMWindowID inWindowID, int x, int y, int wheel, int clicks); + + + virtual bool wantRedraw(); + virtual void draw(int left, int top, int right, int bottom) = 0; + virtual int handleNonDragClick(int x_rel, int y_rel, bool right) = 0; + virtual void handleNonDragClickRelease(int x_rel, int y_rel, bool right); + virtual void handleKeyPress(char key, XPLMKeyFlags flags, unsigned char virtual_key) = 0; + virtual int frameTextureId() const = 0; + virtual int frameTextureLitId() const { return 0; } + virtual void drawFrameTexture(int, int, int, int); + virtual void drawFrameTextureLit(int, int, int, int); + virtual bool wantClearTexture() const; + virtual bool wantVFlip() const; + virtual int handleMouseWheel(int x, int y, int wheel, int clicks); + virtual float instrumentBrightness() const; + virtual bool wantVRifAvailable() const; + virtual void update(); + + static int draw3dCallback(XPLMDrawingPhase phase, int is_before, void* refcon); + + static void draw2dWindowCallback(XPLMWindowID window_id, void* refcon); + static void handle2dKeyCallback(XPLMWindowID window_id, char key, XPLMKeyFlags flags, char virtual_key, void* refcon, int losing_focus); + static int handle2dClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse, void* refcon); + static int handle2dRightClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse, void* refcon); + static XPLMCursorStatus handle2dCursorCallback(XPLMWindowID window_id, int x, int y, void* refcon); + static int handle2dWheelCallback(XPLMWindowID inWindowID, int x, int y, int wheel, int clicks, void* refcon); + + static float frameCallback(float, float, int, void* refcon); + + void setDrawState(int inEnableFog, + int inNumberTexUnits, + int inEnableLighting, + int inEnableAlphaTesting, + int inEnableAlphaBlending, + int inEnableDepthTesting, + int inEnableDepthWriting); + void bindTex(int tex_id, int texture_unit); + void generateTex(int* tex_id, int number_of_textures); + + static bool coordInRect(float x, float y, float l, float t, float r, float b); + void drawTexture(int tex_id, int left, int top, int right, int bottom, bool vflip=false); + + int width3d() const { return width_3d_; } + int height3d() const { return height_3d_; } + GLuint gaugeTexture() const { return gauge_texture_; } + +private: + void updateFBO(); + XPLMWindowID window2d_id_; + int left_3d_; + int top_3d_; + int left_2d_; + int top_2d_; + int width_3d_; + int height_3d_; + const int width_2d_; + const int height_2d_; + int frame_off_x_; + int frame_off_y_; + bool visible_2d_; + bool visible_3d_; + bool always_draw_3d_; + float scale_3d_; + int width_view_3d_; + int height_view_3d_; + int panel_render_pass_; + DataRef screen_width_, screen_height_; + DataRef view_type_, panel_render_type_, vr_enabled_; + DataRef > instrument_brightness_; + DataRef lit_level_r_, lit_level_g_, lit_level_b_; + bool window_is_dragging_; + bool window_has_keyboard_focus_; + GLuint gauge_texture_; + GLuint rbo_; + GLuint fbo_; + int copy_left_3d_; + int copy_top_3d_; + int dX = 0; + int dY = 0; +}; + +} + +#endif // OVERLAYWINDOW_H diff --git a/include/PPL/owneddata.h b/include/PPL/owneddata.h new file mode 100644 index 0000000..5483ffa --- /dev/null +++ b/include/PPL/owneddata.h @@ -0,0 +1,235 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef OWNEDDATA_H +#define OWNEDDATA_H + +#include +#include +#include + +#include "XPLMDataAccess.h" + +#include "dataref.h" + +namespace PPL { + +template +T readFunc(void*); +template +void writeFunc(void*, T); + +int readFuncStr(void*, void*, int, int); +void writeFuncStr(void*, void*, int, int); + +int readFuncVF(void*, float*, int, int); +void writeFuncVF(void*, float*, int, int); + +class DataRefNotPublishedException : public std::runtime_error { +public: + DataRefNotPublishedException(const std::string& msg): + std::runtime_error(msg) + {} +}; + +/** + * Data owned by the plugin and published as a XPLM DataRef. + * create an instance and call a register function to make data accessible to other plugins + * @author (c) 2009-2011 by Philipp Muenzel + * @version 0.6 + */ +template +class OwnedData{ +public: + + typedef void (*DataCallback_f)(const T&); + + /** + * create owned date for sharing. + * the identifier provided is the string identifier later used for the dataref. + * the dataref is then registered in X-Plane's plugin system + * Please consult the XP SDK naming conventions on how to name things right. + * @param std::string& identifier + * @param RWType set the dataref writeable to OTHER plugins + * @param publish_in_dre by default, a message is sent to the DataRefEditor plugin that exposes the dataref + * @param callback optional callback function pointer to be called when other plugins write to the dataref + * @exception throws DataRefNotPublishedException if publishing in X-Plane fails + */ + OwnedData(const std::string& identifier, + RWType read_write = ReadOnly, + bool publish_in_dre = false, + DataCallback_f callback = 0): + m_data_ref_identifier(identifier), + m_data_ref(nullptr), + m_value(T()), + m_callback(callback) + { + switch(read_write) + { + case ReadOnly : + registerRead(); + break; + case WriteOnly : + registerWrite(); + break; + case ReadWrite: + registerReadWrite(); + break; + } + if (publish_in_dre) + { + XPLMPluginID PluginID = XPLMFindPluginBySignature("xplanesdk.examples.DataRefEditor"); + if (PluginID != XPLM_NO_PLUGIN_ID) + XPLMSendMessageToPlugin(PluginID, DRE_MSG_ADD_DATAREF, (void*)m_data_ref_identifier.c_str()); + } + } + + /** + * upon destruction, the dataref is de-registered from X-Plane + */ + ~OwnedData() { unregister(); } + + OwnedData(const OwnedData&) = delete; + OwnedData& operator=(const OwnedData&) = delete; + + /** + * acces the currently stored value + * @return value + */ + T value() const { return m_value; } + + /** + * acces the currently stored value + * @return value + */ + operator T() const { return m_value; } + + /** + * set the value so all other monitors of the dataref get it + * @param val + */ + const OwnedData& operator=(const T& val) { m_value = val; return *this; } + + /** + * set the value so all other monitors of the dataref get it + * @param val + */ + void setValue(const T& val) + { + m_value = val; + if (m_callback) + m_callback(val); + } + + +private: + + void registerRead(); + + void registerWrite(); + + void registerReadWrite(); + + void unregister() + { + if (m_data_ref) + { + XPLMUnregisterDataAccessor(m_data_ref); + m_data_ref = 0; + } + } + +private: + + std::string m_data_ref_identifier; + XPLMDataRef m_data_ref; + T m_value; + DataCallback_f m_callback; +}; + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template +T readFunc(void* inRefCon) +{ + OwnedData* p_owned_data = static_cast*>(inRefCon); + return p_owned_data->value(); +} + +template +void writeFunc(void* inRefCon, T data) +{ + OwnedData* p_owned_data = static_cast*>(inRefCon); + p_owned_data->setValue(data); +} + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +void OwnedData::registerRead(); +template <> +void OwnedData::registerRead(); +template <> +void OwnedData::registerRead(); +template <> +void OwnedData::registerRead(); +template <> +void OwnedData >::registerRead(); + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +void OwnedData::registerWrite(); +template <> +void OwnedData::registerWrite(); +template <> +void OwnedData::registerWrite(); +template <> +void OwnedData::registerWrite(); +template <> +void OwnedData >::registerWrite(); + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +template <> +void OwnedData::registerReadWrite(); +template <> +void OwnedData::registerReadWrite(); +template <> +void OwnedData::registerReadWrite(); +template <> +void OwnedData::registerReadWrite(); +template <> +void OwnedData >::registerReadWrite(); + +} + +#endif // OWNEDDATA_H diff --git a/include/PPL/pluginpath.h b/include/PPL/pluginpath.h new file mode 100644 index 0000000..f9ed96f --- /dev/null +++ b/include/PPL/pluginpath.h @@ -0,0 +1,97 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef PLUGINPATH_H +#define PLUGINPATH_H + +#include +#include + +namespace PPL { + +/** + * PluginPath provides a platform-independant absolute path to directory + * the plugin currently runs in. + * @author (c) 2009-2011 by Philipp Muenzel + * @version 0.3 + */ +class PluginPath +{ +public: + + class PathSetupError : public std::runtime_error{ + public: + PathSetupError(const std::string& msg): + std::runtime_error(msg) + {} + }; + + + static std::string prependXPlanePath(const std::string&); + + /** + * prepend the absolute path to the directory of the currently running plugin + * to the path + * @param path to which absolute path should be prepended + * @return the absolute path + * @exception PathSetupError is thrown if path conversion fails + */ + static std::string prependPluginPath(const std::string& path); + + /** + * prepend the absolute path to the Resources subdirectory of the + * directory of the currently running plugin to the path + * @param path to which absolute path should be prepended + * @return the absolute path + * @exception PathSetupError is thrown if path conversion fails + */ + static std::string prependPluginResourcesPath(const std::string& path); + + /** + * prepend the absolute path to the directory of the currently loaded + * user airplane to the path + * @param path to which absolute path should be prepended + * @return the absolute path + * @exception PathSetupError is thrown if path conversion fails + */ + static std::string prependPlanePath(const std::string& path); + + /** + * set the name of the directory where fat plugin resides + * @param name + */ + static void setPluginDirectoryName(const std::string& name); + +private: + + static std::string plugin_directory; + +}; + +} + +#endif // PLUGINPATH_H diff --git a/include/PPL/processor.h b/include/PPL/processor.h new file mode 100644 index 0000000..3d253d7 --- /dev/null +++ b/include/PPL/processor.h @@ -0,0 +1,45 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef PROCESSOR_H +#define PROCESSOR_H + +namespace PPL { + +class Processor +{ +public: + Processor(float time_to_start = -1.f); + virtual ~Processor(); + virtual float callback(float, float, int) = 0; +private: + static float flightloopcallback(float, float, int, void* refcon); +}; + +} + +#endif // PROCESSOR_H diff --git a/include/PPL/settings.h b/include/PPL/settings.h new file mode 100644 index 0000000..c49b669 --- /dev/null +++ b/include/PPL/settings.h @@ -0,0 +1,94 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef SETTINGS_H +#define SETTINGS_H + +#include + +#define SI_CONVERT_GENERIC +#include "SimpleIni.h" + +namespace PPL { + +/** + * Convenient access to ini-Files. + * This is a small wrapper accessing the most frequently used basic functions of SimpleIni + * @version 0.3 + * @author (c) 2009-2011 by Philipp Muenzel + */ +class Settings : private CSimpleIniA { +public: + /** + * init with the path to an ini.-file. If the file does not exist, it is created + * and everything set via setValue is stored + * @param filename path to ini-file + * @param create_if_not_exists whether to create a new .ini-file with default settings if it wasn't there before + * @param write_new_file whether to rewrite the file with updated settings on exit + */ + Settings(const std::string& filename, bool create_if_not_exists = false, bool write_new_file = false); + + /** + * if file should be created + */ + ~Settings(); + + /** + * load settings from the file an overwrite everything that was set before + */ + bool loadFromFile(); + + /** + * get a string value + */ + std::string get(const std::string& section, const std::string& key); + + /** + * get a long value + */ + long getLong(const std::string& section, const std::string& key); + + /** + * set a long value + */ + void set(const std::string& section, const std::string& key, const std::string& value); + + /** + * set a string value + */ + void setLong(const std::string& section, const std::string& key, long value); + + +private: + std::string m_config_file_name; + bool m_file_did_exist_before; + bool m_create_file_if_not_exists; + bool m_write_new_file; +}; + +} +#endif // SETTINGS_H diff --git a/include/PPL/smoothed.h b/include/PPL/smoothed.h new file mode 100644 index 0000000..90d507b --- /dev/null +++ b/include/PPL/smoothed.h @@ -0,0 +1,68 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef SMOOTHED_H +#define SMOOTHED_H + +#include + +namespace PPL { + +template +class Smoothed +{ +public: + Smoothed() + { + memset(m_history, 0, sizeof(T)*n); + } + + Smoothed(T val) + { + for (std::size_t i = 0 ; i < n ; i++) + m_history[i] = val; + } + + const Smoothed& operator=(T val) + { + memmove(&m_history[1],m_history,(n-1)*sizeof(T)); + m_history[0] = val; + return *this; + } + + operator T() const + { + T result(0); + for (std::size_t i = 0 ; i < n ; i++) + result += m_history[i]/n; + return result; + } +private: + T m_history[n]; +}; +} +#endif // SMOOTHED_H diff --git a/include/PPL/texture.h b/include/PPL/texture.h new file mode 100644 index 0000000..b1cbdaa --- /dev/null +++ b/include/PPL/texture.h @@ -0,0 +1,96 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef TEXTURE_H +#define TEXTURE_H + +#include +#include +#include "stdint.h" + +namespace PPL { + +class Texture +{ +public: + +#pragma pack(push, ident, 2) + + struct BMPFILEHEADER + { + int16_t bfType; + int32_t bfSize; + int16_t bfReserved1; + int16_t bfReserved2; + int32_t bfOffBits; + }; + + struct BMPINFOHEADER + { + int32_t biSize; + int32_t biWidth; + int32_t biHeight; + int16_t biPlanes; + int16_t biBitCount; + int32_t biCompression; + int32_t biSizeImage; + int32_t biXPelsPerMeter; + int32_t biYPelsPerMeter; + int32_t biClrUsed; + int32_t biClrImportant; + }; + + struct IMAGEDATA + { + std::vector pData; + int32_t Width; + int32_t Height; + int32_t Padding; + int16_t Channels; + unsigned int bpp; + }; + +#pragma pack(pop, ident) + + Texture(const std::string& file_name, bool build_mipmaps = false); + ~Texture(); + Texture(const Texture&) = delete; + Texture& operator=(const Texture&) = delete; + + int id() const; + int width() const; + int height() const; + +private: + void swapRedBlue(); + IMAGEDATA m_imagedata; + int m_id; +}; + +} + +#endif // TEXTURE_H diff --git a/include/PPL/vertexbuffer.hpp b/include/PPL/vertexbuffer.hpp new file mode 100644 index 0000000..77549f1 --- /dev/null +++ b/include/PPL/vertexbuffer.hpp @@ -0,0 +1,150 @@ +// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those +// of the authors and should not be interpreted as representing official policies, +// either expressed or implied, of the FreeBSD Project. + +#ifndef VERTEXBUFFER_HPP +#define VERTEXBUFFER_HPP + +#include + +#if APL == 1 +#include +#elif IBM == 1 +#include +#include "GL/glew.h" +#include +#elif LIN == 1 +#include +#include +#endif + +#include "XPLMGraphics.h" + +namespace PPL{ + + +/** + * @brief A class to use Vertex Buffer Objects (VBOs) for drawing in X-Plane. + * + * To quote Ben Supnik "VBO beats the pants off of using our own memory." + * Use this class to specify vertices, normals, colors and texture coordinates for drawing + * and store them in GPU driver memory (or even in VRAM itself) and later draw them + * in a drawing callback. + * + * For efficiency, VBOs should be at least 4096 bytes long - that's somewhere + * between 70 and 341 vertices, but it depends on how much data each vertex has. + * Don't make a VBO for 4 vertices!! A few big VBOs is a lot better than many + * small ones. + * + * + */ +class VertexBuffer +{ +public: + + /** + * @brief Give the driver a hint how much the geometry in the VBO will change. + * + * Either the VBO will be set up like once to draw more or less the same + * thing every frame. + * Or you can specify the VBO is going to change every single frame. + */ + enum Type { + STATIC, //!< VBO will not change much, save in VRAM for max draw speed + STREAMING //!< Save in AGP RAM so changing vertices is not so bad. + }; + + /** @param type, whether to use static or streaming buffer + * The following params define the number of components in each vertex! + * @param num_verts 2, 3 or 4 + * @param num_normals number of normal coordinates 0 or 3 + * @param num_tex number of texture coordinates, 0 to 4 + * @param num_tex2 number of secondary texture coordinates, 0 to 4 + * @param num_colors 0 or 4 + */ + VertexBuffer(Type type, + std::size_t num_verts, + std::size_t num_normals, + std::size_t num_tex, + std::size_t num_tex2, + std::size_t num_colors); + + ~VertexBuffer(); + + /** + * @brief Provide non-null pointers for the params you want to set + * @param num_vertices + * @param out_verts + * @param out_normals + * @param out_texes + * @param out_texes2 + * @param out_colors + * @return the stride length in bytes + */ + std::size_t beginSpecifyVerts(std::size_t num_vertices, + volatile float **out_verts, + volatile float **out_normals, + volatile float **out_texes, + volatile float **out_texes2, + volatile float **out_colors); + + /** + * @brief call this when done setting the vertices + */ + void endSpecifyVerts(); + + /** + * @brief call this immediately before drawing the stuff, don't call any + * other openGL op between this and the actual drawing + */ + void setupForDraw(); + + /** + * @brief draw This works like you are used to from glDrawArrays + */ + void draw(GLenum mode, + GLint first, + GLsizei count); + + /** + * @brief endDraw call this to reset the states after drawing + */ + void endDraw(); + +private: + std::size_t num_vrt_; + std::size_t num_nrm_; + std::size_t num_tx_; + std::size_t num_tx2_; + std::size_t num_col_; + std::size_t stride_floats_; + Type mode_; + GLuint vbo_; +}; + +} + +#endif // VERTEXBUFFER_HPP diff --git a/src/action.h b/src/action.h index 191552e..cbe211e 100644 --- a/src/action.h +++ b/src/action.h @@ -1,44 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_ACTION_H +#define PPL_SRC_ACTION_H -#ifndef ACTION_H -#define ACTION_H +#include "../include/PPL/action.h" -#include - -namespace PPL { - -class Action { -public: - virtual ~Action() = default; - virtual const std::string name() const = 0; - virtual void doAction() = 0; -}; - -} - -#endif // ACTION_H +#endif // PPL_SRC_ACTION_H diff --git a/src/alcontextchanger.h b/src/alcontextchanger.h index 01be916..4ff78a7 100644 --- a/src/alcontextchanger.h +++ b/src/alcontextchanger.h @@ -1,78 +1,6 @@ -// Copyright (c) 2018, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_ALCONTEXTCHANGER_H +#define PPL_SRC_ALCONTEXTCHANGER_H -#ifndef ALCONTEXTCHANGER_H -#define ALCONTEXTCHANGER_H +#include "../include/PPL/alcontextchanger.h" -#if APL == 1 -#include -#include -#elif IBM == 1 -#include -#include -#elif LIN == 1 -#include -#include -#else -#error "No platform defined" -#endif - -namespace PPL { - -/** - * @brief RAII class to change the openal context on construction and - * ensure the old context is restored on destruction. - * - * @author (c) 2009-2018 by Philipp Ringler - * @version 1.0 - */ -class ALContextChanger -{ -public: - - /** - * Switch to the openAL context given for the time this object lives. - * - * @param own_context the AL context to switch to - */ - ALContextChanger(ALCcontext* own_context); - - /** - * switch back to whatever context was active at the time the object was created. - */ - ~ALContextChanger(); - - ALContextChanger(const ALContextChanger&) = delete; - ALContextChanger& operator=(const ALContextChanger&) = delete; - -private: - ALCcontext* m_other_context; -}; - -} - -#endif // ALCONTEXTCHANGER_H +#endif // PPL_SRC_ALCONTEXTCHANGER_H diff --git a/src/alcontextmanager.h b/src/alcontextmanager.h index 6474b5f..e90d02b 100644 --- a/src/alcontextmanager.h +++ b/src/alcontextmanager.h @@ -1,166 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_ALCONTEXTMANAGER_H +#define PPL_SRC_ALCONTEXTMANAGER_H -#ifndef ALCONTEXTMANAGER_H -#define ALCONTEXTMANAGER_H +#include "../include/PPL/alcontextmanager.h" -#include -#include -#include - -#if APL == 1 -#include -#include -#elif IBM == 1 -#include -#include -#elif LIN == 1 -#include -#include -#else -#error "No platform defined" -#endif - -#include "alsoundbuffer.h" - -namespace PPL { - -/** - * Encapsulates all openAL and alut related stuff for looking up devices, handling different - * contexts etc. Stores SoundBuffer istances to keep track of the currently loaded sounds - * and provides convenient functions for standard operations like playing. - * - * @version 1.1 - * @author (c) 2009-2018 by Philipp Ringler - */ -class ALContextManager -{ -public: - class SoundNotFoundError : public std::runtime_error - { - public: - SoundNotFoundError(const std::string& msg): - std::runtime_error(msg) - {} - }; - - class SoundLoadError : public std::runtime_error - { - public: - SoundLoadError(const std::string& msg): - std::runtime_error(msg) - {} - }; - - class SoundPlayError : public std::runtime_error - { - public: - SoundPlayError(const std::string& msg): - std::runtime_error(msg) - {} - }; - - /** - * initializes openAL and alut and creates context - */ - ALContextManager(); - - /** - * deletes context and cleans up loaded sounds - */ - ~ALContextManager(); - - ALContextManager(const ALContextManager&) = delete; - ALContextManager& operator=(const ALContextManager&) = delete; - - /** - * tries to load a sound by a file (format depends on what alut distro supports) - * and stores it locally in a map, providing access by an integer key - * @param filename the sound to load as a path relative to X-Planes main directory - * @return unique integer id for adressing the sound buffer - * @exception throws a SoundLoadError if file cannot be found or has unacceptable format - */ - int addSoundFromFile(const std::string& filename); - - /** - * removes the sound from the map and deletes its buffer - * @param id the sound buffers id in the map - */ - void removeSound(int id); - - /** - * starts playback of the sound (playback continues when function returns) - * @param id the sound buffers id in the map - */ - bool playSound(int id, float volume=1.f); - - /** - * stops playback of the sound - * @param id the sound buffers id in the map - */ - void stopSound(int id); - - /** - * marks the sound to be played in a loop (when playback starts by play() ) - * @param id the sound buffers id in the map - */ - void loopSound(int id); - - /** - * removes the looping flag (playback does stop when the sound's end is reached) - * @param id the sound buffers id in the map - */ - void unLoopSound(int id); - - /** - * rewinds the sound to it's starting position - * @param id the sound buffers id in the map - */ - void rewindSound(int id); - - /** - * @return whether the sound is playing right now - * @param id the sound buffers id in the map - */ - bool isPlayingSound(int id); - - -private: - ALSoundBuffer* findSoundById(int id); - void deleteAllSounds(); - -private: - std::map> m_sounds; - int m_internal_counter; - ALCdevice* m_device; - ALCcontext* m_my_context; - ALCcontext* m_current_context; -}; - -} - -#endif // ALCONTEXTMANAGER_H +#endif // PPL_SRC_ALCONTEXTMANAGER_H diff --git a/src/alsoundbuffer.h b/src/alsoundbuffer.h index 96b3713..7baaaa3 100644 --- a/src/alsoundbuffer.h +++ b/src/alsoundbuffer.h @@ -1,139 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_ALSOUNDBUFFER_H +#define PPL_SRC_ALSOUNDBUFFER_H -#ifndef ALSOUNDBUFFER_H -#define ALSOUNDBUFFER_H +#include "../include/PPL/alsoundbuffer.h" -#include -#include - -#if APL == 1 -#include -#include -#elif IBM == 1 -#include -#include -#elif LIN == 1 -#include -#include -#else -#error "No platform defined" -#endif - - -namespace PPL { - -/** - * This class encapsulates openALs buffers and sources, does the resource handling - * and provides the listener setup. - * @version 1.0 - * @author (c) 2009-2018 by Philipp Ringler - */ -class ALSoundBuffer -{ -public: - class SoundPlayingError : public std::runtime_error - { - public: - SoundPlayingError(const std::string& msg): - std::runtime_error(msg) - {} - }; - - class SoundBufferError : public SoundPlayingError - { - public: - SoundBufferError(const std::string& msg): - SoundPlayingError(msg) - {} - }; - - class SoundSourceError : public SoundPlayingError - { - public: - SoundSourceError(const std::string& msg): - SoundPlayingError(msg) - {} - }; - - /** - * create a new soundbuffer by creating a buffer from a file (format depends - * on what alut distro supports). Sets up the openAL source at coordinate origin - * @param filename the sound to load as a path relative to X-Planes main directory - * @exception throws an SoundPlayingError if Buffer could not be created - */ - ALSoundBuffer(const std::string& filename); - - /** - * release the resources, delete buffer and source - */ - ~ALSoundBuffer(); - - ALSoundBuffer(const ALSoundBuffer&) = delete; - ALSoundBuffer& operator=(const ALSoundBuffer&) = delete; - - /** - * set up the listener at coordinate origin and play the sound buffer - * @return play command was successfull (false could mean invalid source or buffer) - */ - bool play(float volume); - - /** - * set source to looping the sound (effective next time play() called) - * @param yes loop true or false - */ - void setLoop(bool yes); - - /** - * stop playback of the sound - */ - void stop(); - - /** - * rewind to the start of the sound - */ - void rewind(); - - /** - * is the sound currently in playback - * @return bool is playing right now ? - */ - bool isPlaying() const; - -private: - std::string m_name; - ALuint m_buffer; - ALuint m_source; - ALboolean m_loop; -}; - -ALuint LoadWav(const std::string& fileName); - -} - - -#endif // ALSOUNDBUFFER_H +#endif // PPL_SRC_ALSOUNDBUFFER_H diff --git a/src/basics.h b/src/basics.h index ad07a0a..8bfc58e 100644 --- a/src/basics.h +++ b/src/basics.h @@ -1,57 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_BASICS_H +#define PPL_SRC_BASICS_H -#ifndef BASICS_H -#define BASICS_H +#include "../include/PPL/basics.h" -#include -#include - - -namespace PPL { - -inline long ipow(long base, long exp) -{ - int result = 1; - while (exp) - { - if (exp & 1) - result *= base; - exp >>= 1; - base *= base; - } - return result; -} - -inline double mods(double y, double x) -{ - return y - x*std::floor(y/x); -} - -} - -#endif // BASICS_H +#endif // PPL_SRC_BASICS_H diff --git a/src/dataref.h b/src/dataref.h index ace6b4b..640dae1 100644 --- a/src/dataref.h +++ b/src/dataref.h @@ -1,591 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_DATAREF_H +#define PPL_SRC_DATAREF_H -#ifndef DATAREF_H -#define DATAREF_H +#include "../include/PPL/dataref.h" -#include -#include -#include -#include - -#include "XPLMDataAccess.h" -#include "XPLMPlugin.h" - -#include "log.h" - -namespace PPL { - - -constexpr long DRE_MSG_ADD_DATAREF = 0x01000000; -const char* const DRE_PLUGIN_SINATURE = "xplanesdk.examples.DataRefEditor"; - - -/** - * @brief RWType distinguishes the three types of acess to datarefs. - * - * For simdata, it specifies if incoming data may be written to X-Plane - * and if data should be readable. - * For owned data, it specifies if the data is readable or writeable - * to OTHER plugins (or anything that accesses datarefs e.g. panels). - * @todo Should there be two enums for simdata and owned data? - */ -enum RWType { - ReadOnly = 1, - WriteOnly = 2, - ReadWrite = WriteOnly | ReadOnly - }; - -/** - * A generic base exception that is thrown when anything - * with looking up an X-Plane dataref and binding it goes wrong - */ -class LookupException : public std::runtime_error { -public: - LookupException(const std::string& msg): - runtime_error(msg) - {} -}; - -/** - * The dataref identifier could not be found in the plugin system's table - * of datarefs, neither X-Plane nor a plugin publishes it. - */ -class NotFoundException : public LookupException { -public: - NotFoundException(const std::string& msg): - LookupException(msg) - {} -}; - -/** - * The requested data type does not match the type specified in the - * plugin system's table of datarefs - */ -class IncompatibleTypeException : public LookupException { -public: - IncompatibleTypeException(const std::string& msg): - LookupException(msg) - {} -}; - -/** - * Indicates that a write-acess (write-only or read/write) to a - * read-only dataref was requested - */ -class NotWriteableException : public LookupException { -public: - NotWriteableException(const std::string& msg): - LookupException(msg) - {} -}; - -template -struct dataref_trait { - typedef T BasicType; -}; - -template<> -struct dataref_trait > { - typedef float BasicType; - mutable std::vector cache_; -}; - -template<> -struct dataref_trait > { - typedef int BasicType; - mutable std::vector cache_; -}; - -template<> -struct dataref_trait { - typedef char BasicType; - mutable std::vector cache_; -}; - -/** - * @brief Wrapper for access to datarefs published by XP itself or by other plugins. - * - * It wraps the lookup of datarefs, type-safe getting and setting of data, and - * obeying to X-Planes writeability restrictions. - * By creating a DataRef instance, it is bound to a specific dataref - * specified in the constructor. - * @author (c) 2009-2015 by Philipp Muenzel - * @version 2.0 - */ -template -class DataRef : private dataref_trait -{ -public: - - /** - * Sets up the dataref connection. - * looks up the datarefs and stores handler locally, also checks for correct - * type of data (sim-type) and correct read-/writeability - * @param identifier The identifier as in datarefs.txt as std::string - * @param writeability the writeability as defined by RWType - * @param share treat the dataref as a shared dataref, i.e. register the user as a shared dataref participant - * @exception LookupException is thrown if one of the following happens - * a) DataRef can not be found - * b) Data type is invalid (trying to access an int DataRef through float functions) - * c) data was requested to be writeable, but X-Plane says it is read-only - */ - DataRef(const std::string& identifier, RWType writeability = ReadOnly, bool share = false, bool publish_in_dre = false); - DataRef(const DataRef&) = delete; - - /** - * unshare dataref if it was created as shared - */ - virtual ~DataRef(); - - /** - * read the current value from X-Plane's plugin system - */ - operator SimType() const; - - /** - * write value to X-Plane - */ - const DataRef& operator=(const SimType& rhs); - - /** - * read value from other dataref and write it to this dataref - */ - const DataRef& operator=(const DataRef& rhs); - - /** - * @return does the actual value differ from the last history value - */ - bool hasChanged() const; - - /** - * save the actual value to the history - */ - void save(); - - /** - * overwrite the actual value with the last history value - */ - void undo(); - - /** - * offset history value from actual value, so the next hasChanged returns true. - * Does not change the actual value of the dataref. - */ - void forceChanged(); - - /** - * read the current value from X-Plane and access element in vector data - * @note is the same as operator SimType() for non-vector data - * @todo is there a more elegant way to do this? - */ - typename dataref_trait::BasicType operator[](std::size_t index) const; - - /** - * callback that invokes the template function specified in a sub-class, - * if the value of a shared dataref has been changed by someone else - */ - void notify() { doNotify(); } - - std::string name() const { return identifier_; } - - /** - * write value of vector element to X-Plane - * @note is the same as operator=() for non-vector data - * @todo is there a more elegant way to do this? - */ - void setVal(std::size_t, typename dataref_trait::BasicType val) - { - operator =(val); - } - - /** - * reserve as many elements in the history value array as X-Plane says the vector can hold at maximum - * @note does nothing for non-vector data - */ - void reserve() {} - - /** - * reserve this many elements in the history value array - * @note does nothing for non-vector data - */ - void reserve(std::size_t) {} - -private: - - static void NotifactionFunc(void* refcon) - { - DataRef* self = static_cast(refcon); - self->notify(); - } - - void shareDataRef(const std::string& identifier, bool publish_in_dre); - - void publishInDRE(); - - void share(int success, bool publish_in_dre); - - void unshareData(); - - void lookUp(const std::string& identifier); - - void checkWriteabilityIsValid(); - - void checkDataType(); - - void logErrorWithDataRef(const std::string& error_msg, const std::string& dataref_identifier); - - virtual void doNotify() {} //!< override this in a subclass if you want callback functionality - -private: - XPLMDataRef m_data_ref; //!< opaque handle to X-Plane's data - RWType m_read_write; //!< is the data required to be write- or readable - SimType m_history; //!< stores the last value to detect changes - bool shared_; - std::string identifier_; -}; - - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - - -template -DataRef::DataRef(const std::string& identifier, - RWType writeability, - bool share, - bool publish_in_dre): - m_data_ref(nullptr), - m_read_write(writeability), - shared_(false), - identifier_(identifier) -{ - try - { - if (share && XPLMFindDataRef(identifier.c_str()) == nullptr) - shareDataRef(identifier, publish_in_dre); - lookUp(identifier); - checkDataType(); - checkWriteabilityIsValid(); - } catch (LookupException& ex) - { - logErrorWithDataRef(ex.what(), identifier); - // We log only an error and re-throw the exception, so the - // caller may decide if this is a fatal error that requires an X-Plane exit. - throw; - } -} - - -template -DataRef::~DataRef() -{ - if (shared_) - unshareData(); -} - -template -const DataRef& DataRef::operator=(const DataRef& rhs) -{ - return operator=(SimType(rhs)); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -bool DataRef::hasChanged() const -{ - return true; -} - -template <> -bool DataRef::hasChanged() const; - -template <> -bool DataRef::hasChanged() const; - -template <> -bool DataRef::hasChanged() const; - -template <> -bool DataRef >::hasChanged() const; - -template <> -bool DataRef >::hasChanged() const; - -template <> -bool DataRef::hasChanged() const; - - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -void DataRef::forceChanged() -{ - m_history = std::numeric_limits::max(); -} - -template <> -void DataRef >::forceChanged(); - -template <> -void DataRef >::forceChanged(); - -template <> -void DataRef::forceChanged(); - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - - -template -void DataRef::save() -{ - m_history = operator SimType(); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -void DataRef::undo() -{ - operator=(m_history); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -void DataRef::shareDataRef(const std::string&, bool) -{ - throw IncompatibleTypeException("No type defined for sharing data"); -} - -template<> -void DataRef::shareDataRef(const std::string&, bool publish_in_dre); -template<> -void DataRef::shareDataRef(const std::string&, bool publish_in_dre); -template<> -void DataRef::shareDataRef(const std::string&, bool publish_in_dre); -template<> -void DataRef >::shareDataRef(const std::string&, bool publish_in_dre); -template<> -void DataRef >::shareDataRef(const std::string&, bool publish_in_dre); -template<> -void DataRef::shareDataRef(const std::string&, bool publish_in_dre); - -template -void DataRef::unshareData() -{ - throw IncompatibleTypeException("No type defined for sharing data"); -} - -template -void DataRef::share(int success, bool publish_in_dre) -{ - if (!success) - throw IncompatibleTypeException("Could not share data "+identifier_+" type mismatch with already existing data."); - shared_ = true; - if (publish_in_dre) - publishInDRE(); -} - -template<> -void DataRef::unshareData(); -template<> -void DataRef::unshareData(); -template<> -void DataRef::unshareData(); -template<> -void DataRef >::unshareData(); -template<> -void DataRef >::unshareData(); -template<> -void DataRef::unshareData(); - -template -void DataRef::publishInDRE() -{ - XPLMPluginID PluginID = XPLMFindPluginBySignature(DRE_PLUGIN_SINATURE); - if (PluginID != XPLM_NO_PLUGIN_ID) - XPLMSendMessageToPlugin(PluginID, DRE_MSG_ADD_DATAREF, static_cast(const_cast(identifier_.c_str()))); -} - - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - - -template -void DataRef::lookUp(const std::string& identifier) -{ - m_data_ref = XPLMFindDataRef(identifier.c_str()); - if(!m_data_ref) - throw NotFoundException(identifier+" not found in X-Plane's available Datarefs."); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -void DataRef::checkWriteabilityIsValid() -{ - if(m_read_write == WriteOnly || m_read_write == ReadWrite) - if (!XPLMCanWriteDataRef(m_data_ref)) - throw NotWriteableException(identifier_+ " declared to be writeable, but X-Plane says it is read-only."); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -void DataRef::checkDataType() -{ - throw IncompatibleTypeException(identifier_+" no type defined."); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -void DataRef::logErrorWithDataRef(const std::string& error_msg, const std::string& dataref_identifier) -{ - Log() << Log::Error << "When setting up dataref " << dataref_identifier << - " the following error occured: " << error_msg << Log::endl; -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template <> -void DataRef::checkDataType(); - -template <> -void DataRef::checkDataType(); - -template <> -void DataRef::checkDataType(); - -template <> -void DataRef >::checkDataType(); - -template <> -void DataRef >::checkDataType(); - -template <> -void DataRef::checkDataType(); - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template <> -DataRef::operator int() const; - -template <> -DataRef::operator float() const; - -template <> -DataRef::operator double() const; - -template <> -DataRef >::operator std::vector() const; - -template <> -DataRef >::operator std::vector() const; - -template <> -DataRef::operator std::string() const; - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template <> -const DataRef& DataRef::operator=(const int&); - -template <> -const DataRef& DataRef::operator=(const float&); - -template <> -const DataRef& DataRef::operator=(const double&); - -template <> -const DataRef >& DataRef >::operator=(const std::vector&); - -template <> -const DataRef >& DataRef >::operator=(const std::vector&); - -template <> -const DataRef& DataRef::operator=(const std::string&); - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -typename dataref_trait::BasicType DataRef::operator[](std::size_t) const -{ - typedef typename dataref_trait::BasicType T; - return T(*this); -} - -template<> -dataref_trait >::BasicType DataRef >::operator[](std::size_t index) const; - -template<> -dataref_trait >::BasicType DataRef >::operator[](std::size_t index) const; - -template<> -dataref_trait::BasicType DataRef::operator[](std::size_t index) const; - -template<> -void DataRef >::setVal(std::size_t pos, int val); -template<> -void DataRef >::setVal(std::size_t pos, float val); -template<> -void DataRef::setVal(std::size_t pos, char val); - -template<> -void DataRef >::reserve(std::size_t i); -template<> -void DataRef >::reserve(std::size_t i); -template<> -void DataRef::reserve(std::size_t i); - - -template<> -void DataRef >::reserve(); -template<> -void DataRef >::reserve(); -template<> -void DataRef::reserve(); - - -} - -#endif // DATAREF_H +#endif // PPL_SRC_DATAREF_H diff --git a/src/fontmgr.h b/src/fontmgr.h index c29b8e3..b8cea40 100644 --- a/src/fontmgr.h +++ b/src/fontmgr.h @@ -1,330 +1,6 @@ -/* - * Copyright (c) 2006, Ben Supnik and Chris Serio. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ -#ifndef _Fonts_h_ -#define _Fonts_h_ +#ifndef PPL_SRC_FONTMGR_H +#define PPL_SRC_FONTMGR_H -#include -#include FT_FREETYPE_H +#include "../include/PPL/fontmgr.h" -#include -#include - -namespace PPL { - -using std::multimap; -using std::string; - -/* - - A BRIEF DIGRESSION ON FONT METRICS BY BEN.... - - We have to get our terms consistent (if not straight) if we are to have any hope of drawing fonts in the right - location. Some terms: - - - A "point" is a real-world physical unit...about 1/72th of an inch, more or less...see WikiPedia for more info - than you'd ever want to know. Since the old CRTs ran at about 72 DPI, a point and a pixel have become - somewhat interchangeable to old-school Mac font programmers. - - - A "pixel" is a single phosphor on your screen...one of the "dots" you can see. The DPI of the monitor tells - how big they are. Most monitors are around 90 DPI, more or less, I think...they used to be more like 72 DPI. - - - A "texel" is a pixel in a texture. Because OpenGL will draw our fonts almost anywhere, in any shape, including - in 3-d, it's not really proper to talk about how m any pixels tall a letter is unless we've very carefully - controlled a number of aspects of the OpenGL drawing environment. However the texture is built by the font - manager, so we can speak with total certainty about how many "texels" tall a letter is. - - - The "baseline" of a font is a line of reference that letters "sit on"...if you are in 1st grade and are learning - to right, you try to write on the baseline. - - - Letters that go below the baseline (like 'g' and 'p') are "descenders". The font "descent" is the distance from - the baseline to the lowest of the descenders. - - - The "ascent" is the distance from the baseline to the tallest letter. So the height of an A is the ascent - because it's real tall and sits on the baseline. - - - The line height is the distance from one baseline to the next when spacing multiple lines. - - - The point size of the font is the height from the lowest descender to tallest ascender (descent + ascent) in - points. - - Now that was a lot of fun but who cares? Well here's the KEY to understanding the fontmgr's layout: - - The font manager always renders fonts into their textures at a scale of 1 point = 1 texel! - - So...the "point size" you request should be based on roughly how many screen pixels you plan to cover. Want - your 'A' to be about 13 screen pixels? Well you have to pick the rectangle, but ask for a 13 point font so - that your 13 texel A looks good. - - Please note that this doesn't take into account the monitor resolution at all! If you draw at 1 texel = 1 pixel - and draw a 13 point font on a modern monitor, it will look SMALLER than a real-world 13 point font because - the font manager never takes the monitor DPI into account. Heck - the font manager doesn't KNOW the DPI of - your monitor! - - Point sizes are passed into the font manager as unsigned integers - no fractional point sizes for now. - - Line heights and other measurements are given in float point fractional texels. While a texel is either there - or not, we preserve the floating point metrics to provide the most accurate layout of the font. - - OPEN ISSUE: Hrm...the glyphs are rendered with rounding...can we capture that so the metrics match the real - texels? The FreeType manual isn't terribly clear on this. - - A WARNING ABOUT MULTIPLE OPENGL CONTEXTS: - - FontMgr attempts to only build up each font once. But it cannot tell if you have multiple OpenGL windows. Therefore - if you hvae multiple OpenGL windows you should share your texture contexts between them using the appropriate - windowing-system specific stuff. - - */ - -/* - * Opaque Font pointer... - * Declaraction is in the CPP - */ -typedef struct FontInfo_t* FontHandle; - -/* - * Enums to allow the user to specify the alignment of the - * text string that we're going to draw. - */ -enum { - ALIGN_LEFT = 0, - ALIGN_CENTER, - ALIGN_RIGHT -}; -typedef int FontAlign_t; - -/* - * Class FontMgr - * - * This class is essentially a wrapper around the FreeType 2.x font library. - * It uses FreeType to generate a pixmap texture of all possible characters in - * a given true-type font. Once the texture is generated for a particular font, - * FreeType resources are released and we then use the texture to draw text ourselves. - * - * The class enforces a 10px font interval to save on resources. That is, any texture - * generated must be on a 10px interval. If the client asks for a 13px texture and we - * already have a 10px one, he'll share that with the other user. If there is no 10px - * texture, we generate one for the client. - */ -class FontMgr { -public: - - FontMgr(); - ~FontMgr(); - - /* - * Load font takes the path to a TTF as well as a font size. This size specifies - * how big the font on the TEXTURE will be, NOT how big the font will be when - * it's drawn! If a size is specified that's not on an interval of 10px, it will - * be rounded to a 10px size, unless inRequireExactSize is true, in which case - * you get the precise requested size. - * This flag also affects filtering: nearest neighbor when exact sizes are requested, - * for a sharp look, otherwise linear filtering for a scalable look. - * This function returns a FontHandle to be used as a reference for draw and unload - * requests. - * NOTE: Texture sharing is implemented for multiple requests for the same font. - * Each call to LoadFont increments a reference count for the texture whether - * it is created or shared. - */ - FontHandle loadFont( - const char* inFontPath, - const char * inStartMem, // null if we open file - const char * inEndMem, // null if we open file - unsigned int inSizePx, - bool inRequireExactSize); - - /* - * Unload font takes a font handle and will decrement the reference count of a - * particular font. When no one is using the font anymore, it will be unloaded - * from memory and the texture will be deleted. - */ - void unloadFont( - FontHandle inFont); - - /* - * This is DEBUGGING function that will display the pixmap texture on the screen - * so that you can see what the font looks like. - */ - void displayTexture( - FontHandle inFont); - - /* - * Simple Drawing: draws a left-justified string starting at a point. The first - * letter will sit on a line starting at X, and Y (e.g. Y is the basline). Font is - * drawn such that one texel = one modelview unit. So this is only appropriate if - * you are using OpenGL with "pixels" as a drawing unit. - * - */ - void drawString( - FontHandle inFont, - float color[4], // 4-part color, featuring alpha. - float inX, - float inY, - const char * inString); - - /* - * Simple Drawing: draws a left- or right justified string starting at a point. The first - * letter will sit on a line starting at X, and Y (e.g. Y is the basline). Font is - * drawn such that one texel = one modelview unit. So this is only appropriate if - * you are using OpenGL with "pixels" as a drawing unit. - * - */ - void drawString( - FontHandle inFont, - float color[4], // 4-part color, featuring alpha. - float inX, - float inY, - FontAlign_t align, - const char * inString); - - /* - * Advanced drawing: draws a string justified within a box. The bottom of the box is - * the lowest descender, the top is the highest ascender plus any built-in leading. - * Justification: inRight is ignored for left-justified strings. The whole string is - * drawn - even parts that exceed the passed in box. - * - */ - void drawRange( - FontHandle inFont, - float color[4], // 4-part color, featuring alpha. - float inLeft, - float inBottom, - float inRight, - float inTop, - const char * inStart, - const char * inEnd, - FontAlign_t fontAlign); - - /***************** FONT MEASUREMENT ROUTINES ****************** - * - * These routines measure in "pixels" - that is, screen units. So you have to provide - * how tall the line will end up and they return how wide it will be. - * - */ - - /* - * Given a font and a height in px, this function returns the length necessary - * to draw the string in px. - * - * OPEN ISSUE: change to start, end + 1 syntax? - * - */ - float measureString( - FontHandle inFont, - float inHeightPx, - const char * inString); - - float measureRange( - FontHandle inFont, - float inHeightPx, - const char * inStart, - const char * inEnd); - - /* - * Given a string, return how many characters will fit in a given space, either - * forward or backward. Strings are specified as a ptr to the start and a ptr to - * the end plus 1. - * - */ - int fitForward( - FontHandle inFont, - float inHeightPx, - float inWidthPx, - const char * inStringStart, - const char * inStringEnd); - - int fitReverse( - FontHandle inFont, - float inHeightPx, - float inWidthPx, - const char * inStringStart, - const char * inStringEnd); - - /******************** FONT METRICS ************************* - * - * These routines return the measurements of the font in TEXELS. - * You pass in the point size of the font. Why? Well, if you are - * using a scalable font, the point size you asked for may not be - * the one in the font handle. These routines take this into - * consideration. - * - * OPEN ISSUE perhaps the font handle should know the actual font size - * and the requested font size? Passing the size in pixels is weird. - * - */ - - /* - * Returns the line height - that is the number of texels from - * one baseline to another. - * - */ - float getLineHeight( - FontHandle inFont, - unsigned int inFontSizePx); - - /* - * Returns the number of texels from the baseline to the lowest - * descender (this is like the bottom of o to the bottom of g). - * - */ - float getLineDescent( - FontHandle inFont, - unsigned int inFontSizePx); - - /* - * Returns the number of texels from the baseline to the top of - * the tallest ascender. This is like the height of the letter A. - * - */ - float getLineAscent( - FontHandle inFont, - unsigned int inFontSizePx); - -private: - - void CalcTexSize( - FT_Face* inFace, - int* outHeight, - int* outWidth); - - void CopyBitmapSection( - unsigned int inSrcHeight, - unsigned int inSrcWidth, - unsigned int inSrcLeft, - unsigned int inSrcTop, - unsigned char* inSrcData, - unsigned int inDstHeight, - unsigned int inDstWidth, - unsigned int inDstLeft, - unsigned int inDstTop, - unsigned char* inDstData); - - typedef multimap TextureMap_t; - - FT_Library library_; - TextureMap_t tex_map_; -}; - -} - -#endif +#endif // PPL_SRC_FONTMGR_H diff --git a/src/log.h b/src/log.h index 17e4bca..52dbc95 100644 --- a/src/log.h +++ b/src/log.h @@ -1,143 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_LOG_H +#define PPL_SRC_LOG_H -#ifndef LOG_H -#define LOG_H +#include "../include/PPL/log.h" -#include -#include -#include -#include - -namespace PPL { - -class LogWriter; - -enum Level { - L_UNSPEC=-1, //!< unspecified, occurs when no severity function pointer was called - L_FAIL = 0, //!< Failure, forces quit of application - L_ERROR = 1, //!< Error, permits keeping the application running - L_WARN = 2, //!< Warning - L_INFO = 3 //!< Information -}; - -/** - * @brief Every log message that arises from libcanaero is in this format. - */ -struct LogEntry { - Level lvl; //!< Severity of the occured event, see enum - time_t time; //!< Timestamp - std::string txt; //!< Human-readable message -}; - - -/** - * @brief A stream-based logger using any LogWriter. - * A Log instance corresponds to ONE log entry. - * - * Creating an instance provides the stream to which a log message is written. - * Specifying severity level and Log::endl are mandatory. - * @code Log() << Log::Error << "This is an error" << Log::endl; - * Log() << Log::Info << "This is an information about " << some_integer << Log::endl @endcode - * @author (c) 2009-2011 by Philipp Münzel - * @version 1.4 - */ -class Log { -public: - /** - * create a new Logstream for ONE new message - */ - Log(); - - /** - * Indicate the following message has severity Information - */ - static Log& Info(Log& log); - - /** - * Indicate the following message has severity Warnining - */ - static Log& Warn(Log& log); - - /** - * Indicate the following message has severity Error - */ - static Log& Error(Log& log); - - /** - * Indicate the following message has severity Failure - */ - static Log& Fail(Log& log); - - /** - * Terminate the log stream. This is mandatory! - */ - static Log& endl(Log& log); - - /** - * Log an integer. - * @param i - */ - Log& operator<<(int32_t i); - - /** - * Log an integer. - * @param i - */ - Log& operator<<(int16_t i); - - /** - * Log a double. - * @param d - */ - Log& operator<<(double d); - - /** - * Log a char - * @param c - */ - Log& operator<<(char c); - - /** - * Log a string. - * @param s - */ - Log& operator<<(const std::string& s); - - /** - * Invoke a function pointer on the logstream (set severity or terminate) - */ - Log& operator<<(Log& (*f)(Log&)); - -private: - Level m_severity; - LogWriter& m_writer; - std::ostringstream m_stream; -}; -} - -#endif // LOG_H +#endif // PPL_SRC_LOG_H diff --git a/src/logichandler.h b/src/logichandler.h index ff9cfd7..e136bf5 100644 --- a/src/logichandler.h +++ b/src/logichandler.h @@ -1,132 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_LOGICHANDLER_H +#define PPL_SRC_LOGICHANDLER_H -#ifndef logichandler_h -#define logichandler_h +#include "../include/PPL/logichandler.h" -#include -#include - -namespace PPL { - -/** - * abstract class for all kinds of processing that takes values from XP, - * do calculations in the flightloop and publish the result in a custom Dataref - * @author (c) 2009-2011 by Philipp Muenzel - * @version 0.4 - */ -class LogicHandler { - - public: - - - virtual ~LogicHandler() = default; - - /** - * reimplement to do the setups that have to be done once when data is acessible - */ - virtual bool initializeAtStart() = 0; - - - /** - * reimplement this function to re-initialize when a new user aircraft is loaded - */ - virtual bool initializeOnAircraftChange() = 0; - - - /** - * reimplement to do the calculations that are done periodically (via the flightloop) - */ - virtual bool processInTheLoop() = 0; - - - /** - * reimplement to process a signal from outside (e.g. interplugin message) - */ - virtual bool processInput(long input, void* param) = 0; - - - /** - * reimplement to return the value for the next call of your processing- positive for seconds, negative for ticks - */ - virtual float loopFrequency() = 0; - - - /** - * reimplement to suspend all processing by this handler if asked - */ - virtual void suspend(bool yes) = 0; - - - /** - * reimplement to tell if this handler is currenty suspended - */ - virtual bool isSuspended() const= 0; - - - /** - * name of this handler - */ - virtual std::string name() const= 0; - - /** - * enable processing for this handler and register in X-Planes flight-loop - */ - void hookToSim(); - - /** - * stop processing and unregister from X-Planes flight-loop - */ - void unhookFromSim(); - -private: - - /** - * reimplement to register for the data needed for calculation - */ - virtual bool aquireSimData() = 0; - - - /** - * reimplement to register the acessors to the owned data to form a custom dataref(s) - */ - virtual bool publishData() = 0; - - - /** - * reimplement to unregister the custom dataref(s) - */ - virtual bool withdrawPublishedData() = 0; - -}; - -float HandlerCallbackInit(float, float, int, void* inRefCon); -float HandlerCallbackProcess(float, float, int, void* inRefCon); - -} - -#endif +#endif // PPL_SRC_LOGICHANDLER_H diff --git a/src/logwriter.h b/src/logwriter.h index f8a5e09..7c3ce3f 100644 --- a/src/logwriter.h +++ b/src/logwriter.h @@ -1,74 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_LOGWRITER_H +#define PPL_SRC_LOGWRITER_H -#ifndef LOGWRITER_H -#define LOGWRITER_H +#include "../include/PPL/logwriter.h" -#include -#include "log.h" - -namespace PPL { - -/** - * @brief Logger implementation that logs to file. - * - * By default, this is implemented by ConsoleLogger. If you want to log - * to a file or GUI instead, override the LogWriter::writeString function. - * @author (c) 2009-2011 by Philipp Münzel - * @version 1.3 - */ -class LogWriter -{ -public: - LogWriter(); - - ~LogWriter(); - - LogWriter(const LogWriter&) = delete; - LogWriter& operator=(const LogWriter&) = delete; - - /** - * Set a file to log all outputs to. If a log happens before - * this funtion was called (e.g. static initializations), output is logged to stdout - */ - void setLogFile(const std::string& filename); - - /** - * Post a log entry to the log queue. - * Can be called from any thread. - */ - void postEntry(const LogEntry& entry); - - static LogWriter& getLogger(); - -private: - std::ofstream m_logfile; -}; - -} - -#endif // LOGWRITER_H +#endif // PPL_SRC_LOGWRITER_H diff --git a/src/menuitem.h b/src/menuitem.h index c9f3ded..95fe1bf 100644 --- a/src/menuitem.h +++ b/src/menuitem.h @@ -1,62 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_MENUITEM_H +#define PPL_SRC_MENUITEM_H -#ifndef MENUITEM_H -#define MENUITEM_H +#include "../include/PPL/menuitem.h" -#include -#include -#include - -#include "XPLMDisplay.h" -#include "XPLMMenus.h" -#include "XPWidgets.h" -#include "XPStandardWidgets.h" - -namespace PPL { - -class Action; - -class MenuItem -{ -public: - MenuItem(const std::string& title); - ~MenuItem(); - void addSubItem(std::unique_ptr&& action); - void menuHandler(void* iRef); -private: - static void menuHandler(void* mRef, void* iRef); - int m_item_id; - std::vector> m_actions; - XPLMMenuID m_menu_id; -}; - - -} - - -#endif // MENUITEM_H +#endif // PPL_SRC_MENUITEM_H diff --git a/src/messagewindow.h b/src/messagewindow.h index 8fd1df0..a588ada 100644 --- a/src/messagewindow.h +++ b/src/messagewindow.h @@ -1,185 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_MESSAGEWINDOW_H +#define PPL_SRC_MESSAGEWINDOW_H -#ifndef MESSAGEWINDOW_H -#define MESSAGEWINDOW_H +#include "../include/PPL/messagewindow.h" -#include -#include -#include - -#include "XPWidgets.h" -#include "XPStandardWidgets.h" - -#include "dataref.h" - -namespace PPL { - -/** - * A convenient way to display a message box with a title and a longer text message - * that quits X-Plane when closed. This is a very good way to handle improper setups, - * blame the user, and force quit afterwards - * @version 1.0 - * @author (c) 2009-2011 by Philipp Muenzel - */ -class MessageWindow -{ -public: - /** - * This exception is thrown during setup of a Message box, if the requested - * size of the box is too large to fit in the simulator window. - */ - class BoxOutOfBoundsException: public std::runtime_error { - public: - BoxOutOfBoundsException(const std::string& msg): - std::runtime_error(msg) - {} - }; - - /** - * construct message box centered in the X-Plane window, with title and auto-breaking message - * @param width in pixels - * @param height in pixels - * @param title text in the titlebar - * @param message text gets automatically br'd according to window width - * @param quit whether the simulator should be closed after the message was confirmed - */ - MessageWindow(int width, int height, const std::string& title, const std::string& message, bool quit); - - /** - * destroy window and all child windows - */ - ~MessageWindow(); - - MessageWindow(const MessageWindow&) = delete; - MessageWindow& operator=(const MessageWindow&) = delete; - - /** - * @param width in pixels - * @exception BoxOutOfBoundsException if width is inappropriate - */ - void setWidth(int width); - - /** - * @param height in pixels - * @exception BoxOutOfBoundsException if height is inappropriate - */ - void setHeight(int height); - - /** - * @param title to show in the title bar of the message box - */ - void setTitle(const std::string& title); - - /** - * @param message to show in the message box - */ - void setMessage(const std::string& message); - - /** - * @param quit quit the sime after the user confirms the dialog? yes or no - */ - void quitSimOnConfirm(bool quit); - - /** - * display the window in the sim, forces user to acknownledge the dialog - */ - void display(); - - /** - * @return is the window currently drawn by X-Plane - */ - bool isDisplayed(); - - /** - * internal processing of message from X-Plane's widget system - * it was already figured out that we are adressed - * @param message - * @param param1 - * @param param2 - */ - int processMessages(XPWidgetMessage message, intptr_t param1, intptr_t param2); - - /** - * static widget callback to register in X-Plane's widget logic - * retrieves the instance via a pointer-to-object stored in the widget struct - */ - static int widgetCallback(XPWidgetMessage, XPWidgetID, intptr_t, intptr_t); - -private: - - /** - * split a string in words and store them in the list - * @param L list of strings where words are stored - * @param seq original string to split up - * @param _1cdelim string that declares a splitting point - * @param _removews remove white spaces - */ - int splitStr(std::list& L, const std::string& seq, const std::string& _1cdelim, bool _removews ); - - /** - * create the box widget with title bar and close buttons - */ - void createSurroundingBox(); - - /** - * create the inner panel on which strings are drawn - */ - void createInnerScreen(); - -private: - - int m_left; //!< upper left corner left offset in pixels from X-Plane main window - - int m_right; //!< upper right corner right offset in pixels from X-Plane main window - - int m_top; //!< upper left corner upper offset in pixels from X-Plane main window - - int m_bottom; //!< lower left corner bottom offset in pixels from X-Plane main window - - std::string m_title; //!< title bar caption - - std::string m_message; //!< message to display - - bool m_quit_on_confirm; //!< quit X-Plane when user closes message box - - bool m_is_displayed; - - XPWidgetID m_box_widget; - - XPWidgetID m_screen_widget; - - std::vector m_caption_widgets_list; - - DataRef m_xp_width; //!< X-Plane main window width - - DataRef m_xp_height; //!< X-Plane main window height -}; - -} - -#endif // MESSAGEWINDOW_H +#endif // PPL_SRC_MESSAGEWINDOW_H diff --git a/src/onscreendisplay.h b/src/onscreendisplay.h index ad28783..1bfc923 100644 --- a/src/onscreendisplay.h +++ b/src/onscreendisplay.h @@ -1,61 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_ONSCREENDISPLAY_H +#define PPL_SRC_ONSCREENDISPLAY_H -#ifndef ONSCREENDISPLAY_H -#define ONSCREENDISPLAY_H +#include "../include/PPL/onscreendisplay.h" -#include "XPWidgetDefs.h" -#include "XPLMDisplay.h" -#include "dataref.h" - -namespace PPL { - -class OnScreenDisplay { -public: - OnScreenDisplay(int width, int height, const std::string& title); - - ~OnScreenDisplay(); - - OnScreenDisplay(const OnScreenDisplay&) = delete; - OnScreenDisplay& operator=(const OnScreenDisplay&) = delete; - - int processMessages(XPWidgetMessage inMessage, intptr_t, intptr_t); - - static int widgetCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t param1, intptr_t param2); - -private: - DataRef screen_w_, screen_h_; - DataRef vr_enabled_; - XPWidgetID widget_id_; - XPLMWindowID window_id_; - int top_, left_, bottom_, right_; - std::string title_; -}; - -} - -#endif // ONSCREENDISPLAY_H +#endif // PPL_SRC_ONSCREENDISPLAY_H diff --git a/src/overlaygauge.h b/src/overlaygauge.h index 55a912a..504c131 100644 --- a/src/overlaygauge.h +++ b/src/overlaygauge.h @@ -1,156 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. - -#ifndef OVERLAYWINDOW_H -#define OVERLAYWINDOW_H - -#include "XPLMDisplay.h" -#include "dataref.h" - -#if APL == 1 -#include -#elif IBM == 1 -#include -#include "GL/glew.h" -#include -#elif LIN == 1 -#include -#include -#endif - -namespace PPL { - -class OverlayGauge -{ -public: - OverlayGauge(int left2d, int top2d, int width2d, int height2d, int left3d, int top3d, int width3d, int height3d, - int frameOffX, int frameOffY, int textureId3d, bool allow_keyboard = true, bool is_visible3d = true, - bool is_visible2d = false, bool always_draw_3d = false, bool allow_3d_click = false, float scale_3d = 1.0f, - bool double_size = false, int panel_render_pass = 2); - virtual ~OverlayGauge(); - OverlayGauge(const OverlayGauge&) = delete; - OverlayGauge& operator=(const OverlayGauge&) = delete; - - void set3d(int left3d, int top3d, int width3d, int height3d, int texture_id, bool always_draw_3d); - void setCopy3d(int left3dcopy, int top3dcopy); - void disable3d(); - bool isVisible() const; - void setVisible(bool b); - void frame(); - int draw3dCallback(XPLMDrawingPhase phase, int is_before); - void toggleKeyboardFocus(); - - void draw2dWindowCallback(XPLMWindowID window_id); - void handle2dKeyCallback(XPLMWindowID window_id, char key, XPLMKeyFlags flags, unsigned char virtual_key, int losing_focus); - int handle2dClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse); - int handle2dRightClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse); - XPLMCursorStatus handle2dCursorCallback(XPLMWindowID window_id, int x, int y); - int handle2dWheelCallback(XPLMWindowID inWindowID, int x, int y, int wheel, int clicks); - - - virtual bool wantRedraw(); - virtual void draw(int left, int top, int right, int bottom) = 0; - virtual int handleNonDragClick(int x_rel, int y_rel, bool right) = 0; - virtual void handleNonDragClickRelease(int x_rel, int y_rel, bool right); - virtual void handleKeyPress(char key, XPLMKeyFlags flags, unsigned char virtual_key) = 0; - virtual int frameTextureId() const = 0; - virtual int frameTextureLitId() const { return 0; } - virtual void drawFrameTexture(int, int, int, int); - virtual void drawFrameTextureLit(int, int, int, int); - virtual bool wantClearTexture() const; - virtual bool wantVFlip() const; - virtual int handleMouseWheel(int x, int y, int wheel, int clicks); - virtual float instrumentBrightness() const; - virtual bool wantVRifAvailable() const; - virtual void update(); - - static int draw3dCallback(XPLMDrawingPhase phase, int is_before, void* refcon); - - static void draw2dWindowCallback(XPLMWindowID window_id, void* refcon); - static void handle2dKeyCallback(XPLMWindowID window_id, char key, XPLMKeyFlags flags, char virtual_key, void* refcon, int losing_focus); - static int handle2dClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse, void* refcon); - static int handle2dRightClickCallback(XPLMWindowID window_id, int x, int y, XPLMMouseStatus mouse, void* refcon); - static XPLMCursorStatus handle2dCursorCallback(XPLMWindowID window_id, int x, int y, void* refcon); - static int handle2dWheelCallback(XPLMWindowID inWindowID, int x, int y, int wheel, int clicks, void* refcon); - - static float frameCallback(float, float, int, void* refcon); - - void setDrawState(int inEnableFog, - int inNumberTexUnits, - int inEnableLighting, - int inEnableAlphaTesting, - int inEnableAlphaBlending, - int inEnableDepthTesting, - int inEnableDepthWriting); - void bindTex(int tex_id, int texture_unit); - void generateTex(int* tex_id, int number_of_textures); - - static bool coordInRect(float x, float y, float l, float t, float r, float b); - void drawTexture(int tex_id, int left, int top, int right, int bottom, bool vflip=false); - - int width3d() const { return width_3d_; } - int height3d() const { return height_3d_; } - GLuint gaugeTexture() const { return gauge_texture_; } - -private: - void updateFBO(); - XPLMWindowID window2d_id_; - int left_3d_; - int top_3d_; - int left_2d_; - int top_2d_; - int width_3d_; - int height_3d_; - const int width_2d_; - const int height_2d_; - int frame_off_x_; - int frame_off_y_; - bool visible_2d_; - bool visible_3d_; - bool always_draw_3d_; - float scale_3d_; - int width_view_3d_; - int height_view_3d_; - int panel_render_pass_; - DataRef screen_width_, screen_height_; - DataRef view_type_, panel_render_type_, vr_enabled_; - DataRef > instrument_brightness_; - DataRef lit_level_r_, lit_level_g_, lit_level_b_; - bool window_is_dragging_; - bool window_has_keyboard_focus_; - GLuint gauge_texture_; - GLuint rbo_; - GLuint fbo_; - int copy_left_3d_; - int copy_top_3d_; - int dX = 0; - int dY = 0; -}; - -} - -#endif // OVERLAYWINDOW_H +#ifndef PPL_SRC_OVERLAYGAUGE_H +#define PPL_SRC_OVERLAYGAUGE_H + +#include "../include/PPL/overlaygauge.h" + +#endif // PPL_SRC_OVERLAYGAUGE_H diff --git a/src/owneddata.h b/src/owneddata.h index 5483ffa..eebd1e8 100644 --- a/src/owneddata.h +++ b/src/owneddata.h @@ -1,235 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_OWNEDDATA_H +#define PPL_SRC_OWNEDDATA_H -#ifndef OWNEDDATA_H -#define OWNEDDATA_H +#include "../include/PPL/owneddata.h" -#include -#include -#include - -#include "XPLMDataAccess.h" - -#include "dataref.h" - -namespace PPL { - -template -T readFunc(void*); -template -void writeFunc(void*, T); - -int readFuncStr(void*, void*, int, int); -void writeFuncStr(void*, void*, int, int); - -int readFuncVF(void*, float*, int, int); -void writeFuncVF(void*, float*, int, int); - -class DataRefNotPublishedException : public std::runtime_error { -public: - DataRefNotPublishedException(const std::string& msg): - std::runtime_error(msg) - {} -}; - -/** - * Data owned by the plugin and published as a XPLM DataRef. - * create an instance and call a register function to make data accessible to other plugins - * @author (c) 2009-2011 by Philipp Muenzel - * @version 0.6 - */ -template -class OwnedData{ -public: - - typedef void (*DataCallback_f)(const T&); - - /** - * create owned date for sharing. - * the identifier provided is the string identifier later used for the dataref. - * the dataref is then registered in X-Plane's plugin system - * Please consult the XP SDK naming conventions on how to name things right. - * @param std::string& identifier - * @param RWType set the dataref writeable to OTHER plugins - * @param publish_in_dre by default, a message is sent to the DataRefEditor plugin that exposes the dataref - * @param callback optional callback function pointer to be called when other plugins write to the dataref - * @exception throws DataRefNotPublishedException if publishing in X-Plane fails - */ - OwnedData(const std::string& identifier, - RWType read_write = ReadOnly, - bool publish_in_dre = false, - DataCallback_f callback = 0): - m_data_ref_identifier(identifier), - m_data_ref(nullptr), - m_value(T()), - m_callback(callback) - { - switch(read_write) - { - case ReadOnly : - registerRead(); - break; - case WriteOnly : - registerWrite(); - break; - case ReadWrite: - registerReadWrite(); - break; - } - if (publish_in_dre) - { - XPLMPluginID PluginID = XPLMFindPluginBySignature("xplanesdk.examples.DataRefEditor"); - if (PluginID != XPLM_NO_PLUGIN_ID) - XPLMSendMessageToPlugin(PluginID, DRE_MSG_ADD_DATAREF, (void*)m_data_ref_identifier.c_str()); - } - } - - /** - * upon destruction, the dataref is de-registered from X-Plane - */ - ~OwnedData() { unregister(); } - - OwnedData(const OwnedData&) = delete; - OwnedData& operator=(const OwnedData&) = delete; - - /** - * acces the currently stored value - * @return value - */ - T value() const { return m_value; } - - /** - * acces the currently stored value - * @return value - */ - operator T() const { return m_value; } - - /** - * set the value so all other monitors of the dataref get it - * @param val - */ - const OwnedData& operator=(const T& val) { m_value = val; return *this; } - - /** - * set the value so all other monitors of the dataref get it - * @param val - */ - void setValue(const T& val) - { - m_value = val; - if (m_callback) - m_callback(val); - } - - -private: - - void registerRead(); - - void registerWrite(); - - void registerReadWrite(); - - void unregister() - { - if (m_data_ref) - { - XPLMUnregisterDataAccessor(m_data_ref); - m_data_ref = 0; - } - } - -private: - - std::string m_data_ref_identifier; - XPLMDataRef m_data_ref; - T m_value; - DataCallback_f m_callback; -}; - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template -T readFunc(void* inRefCon) -{ - OwnedData* p_owned_data = static_cast*>(inRefCon); - return p_owned_data->value(); -} - -template -void writeFunc(void* inRefCon, T data) -{ - OwnedData* p_owned_data = static_cast*>(inRefCon); - p_owned_data->setValue(data); -} - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template <> -void OwnedData::registerRead(); -template <> -void OwnedData::registerRead(); -template <> -void OwnedData::registerRead(); -template <> -void OwnedData::registerRead(); -template <> -void OwnedData >::registerRead(); - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template <> -void OwnedData::registerWrite(); -template <> -void OwnedData::registerWrite(); -template <> -void OwnedData::registerWrite(); -template <> -void OwnedData::registerWrite(); -template <> -void OwnedData >::registerWrite(); - -/////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - -template <> -void OwnedData::registerReadWrite(); -template <> -void OwnedData::registerReadWrite(); -template <> -void OwnedData::registerReadWrite(); -template <> -void OwnedData::registerReadWrite(); -template <> -void OwnedData >::registerReadWrite(); - -} - -#endif // OWNEDDATA_H +#endif // PPL_SRC_OWNEDDATA_H diff --git a/src/pluginpath.h b/src/pluginpath.h index f9ed96f..641c967 100644 --- a/src/pluginpath.h +++ b/src/pluginpath.h @@ -1,97 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_PLUGINPATH_H +#define PPL_SRC_PLUGINPATH_H -#ifndef PLUGINPATH_H -#define PLUGINPATH_H +#include "../include/PPL/pluginpath.h" -#include -#include - -namespace PPL { - -/** - * PluginPath provides a platform-independant absolute path to directory - * the plugin currently runs in. - * @author (c) 2009-2011 by Philipp Muenzel - * @version 0.3 - */ -class PluginPath -{ -public: - - class PathSetupError : public std::runtime_error{ - public: - PathSetupError(const std::string& msg): - std::runtime_error(msg) - {} - }; - - - static std::string prependXPlanePath(const std::string&); - - /** - * prepend the absolute path to the directory of the currently running plugin - * to the path - * @param path to which absolute path should be prepended - * @return the absolute path - * @exception PathSetupError is thrown if path conversion fails - */ - static std::string prependPluginPath(const std::string& path); - - /** - * prepend the absolute path to the Resources subdirectory of the - * directory of the currently running plugin to the path - * @param path to which absolute path should be prepended - * @return the absolute path - * @exception PathSetupError is thrown if path conversion fails - */ - static std::string prependPluginResourcesPath(const std::string& path); - - /** - * prepend the absolute path to the directory of the currently loaded - * user airplane to the path - * @param path to which absolute path should be prepended - * @return the absolute path - * @exception PathSetupError is thrown if path conversion fails - */ - static std::string prependPlanePath(const std::string& path); - - /** - * set the name of the directory where fat plugin resides - * @param name - */ - static void setPluginDirectoryName(const std::string& name); - -private: - - static std::string plugin_directory; - -}; - -} - -#endif // PLUGINPATH_H +#endif // PPL_SRC_PLUGINPATH_H diff --git a/src/processor.h b/src/processor.h index 3d253d7..b38d41e 100644 --- a/src/processor.h +++ b/src/processor.h @@ -1,45 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_PROCESSOR_H +#define PPL_SRC_PROCESSOR_H -#ifndef PROCESSOR_H -#define PROCESSOR_H +#include "../include/PPL/processor.h" -namespace PPL { - -class Processor -{ -public: - Processor(float time_to_start = -1.f); - virtual ~Processor(); - virtual float callback(float, float, int) = 0; -private: - static float flightloopcallback(float, float, int, void* refcon); -}; - -} - -#endif // PROCESSOR_H +#endif // PPL_SRC_PROCESSOR_H diff --git a/src/settings.h b/src/settings.h index c49b669..a02c928 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,94 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_SETTINGS_H +#define PPL_SRC_SETTINGS_H -#ifndef SETTINGS_H -#define SETTINGS_H +#include "../include/PPL/settings.h" -#include - -#define SI_CONVERT_GENERIC -#include "SimpleIni.h" - -namespace PPL { - -/** - * Convenient access to ini-Files. - * This is a small wrapper accessing the most frequently used basic functions of SimpleIni - * @version 0.3 - * @author (c) 2009-2011 by Philipp Muenzel - */ -class Settings : private CSimpleIniA { -public: - /** - * init with the path to an ini.-file. If the file does not exist, it is created - * and everything set via setValue is stored - * @param filename path to ini-file - * @param create_if_not_exists whether to create a new .ini-file with default settings if it wasn't there before - * @param write_new_file whether to rewrite the file with updated settings on exit - */ - Settings(const std::string& filename, bool create_if_not_exists = false, bool write_new_file = false); - - /** - * if file should be created - */ - ~Settings(); - - /** - * load settings from the file an overwrite everything that was set before - */ - bool loadFromFile(); - - /** - * get a string value - */ - std::string get(const std::string& section, const std::string& key); - - /** - * get a long value - */ - long getLong(const std::string& section, const std::string& key); - - /** - * set a long value - */ - void set(const std::string& section, const std::string& key, const std::string& value); - - /** - * set a string value - */ - void setLong(const std::string& section, const std::string& key, long value); - - -private: - std::string m_config_file_name; - bool m_file_did_exist_before; - bool m_create_file_if_not_exists; - bool m_write_new_file; -}; - -} -#endif // SETTINGS_H +#endif // PPL_SRC_SETTINGS_H diff --git a/src/smoothed.h b/src/smoothed.h index 90d507b..d5f3f39 100644 --- a/src/smoothed.h +++ b/src/smoothed.h @@ -1,68 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_SMOOTHED_H +#define PPL_SRC_SMOOTHED_H -#ifndef SMOOTHED_H -#define SMOOTHED_H +#include "../include/PPL/smoothed.h" -#include - -namespace PPL { - -template -class Smoothed -{ -public: - Smoothed() - { - memset(m_history, 0, sizeof(T)*n); - } - - Smoothed(T val) - { - for (std::size_t i = 0 ; i < n ; i++) - m_history[i] = val; - } - - const Smoothed& operator=(T val) - { - memmove(&m_history[1],m_history,(n-1)*sizeof(T)); - m_history[0] = val; - return *this; - } - - operator T() const - { - T result(0); - for (std::size_t i = 0 ; i < n ; i++) - result += m_history[i]/n; - return result; - } -private: - T m_history[n]; -}; -} -#endif // SMOOTHED_H +#endif // PPL_SRC_SMOOTHED_H diff --git a/src/texture.h b/src/texture.h index b1cbdaa..ae1f9a9 100644 --- a/src/texture.h +++ b/src/texture.h @@ -1,96 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_TEXTURE_H +#define PPL_SRC_TEXTURE_H -#ifndef TEXTURE_H -#define TEXTURE_H +#include "../include/PPL/texture.h" -#include -#include -#include "stdint.h" - -namespace PPL { - -class Texture -{ -public: - -#pragma pack(push, ident, 2) - - struct BMPFILEHEADER - { - int16_t bfType; - int32_t bfSize; - int16_t bfReserved1; - int16_t bfReserved2; - int32_t bfOffBits; - }; - - struct BMPINFOHEADER - { - int32_t biSize; - int32_t biWidth; - int32_t biHeight; - int16_t biPlanes; - int16_t biBitCount; - int32_t biCompression; - int32_t biSizeImage; - int32_t biXPelsPerMeter; - int32_t biYPelsPerMeter; - int32_t biClrUsed; - int32_t biClrImportant; - }; - - struct IMAGEDATA - { - std::vector pData; - int32_t Width; - int32_t Height; - int32_t Padding; - int16_t Channels; - unsigned int bpp; - }; - -#pragma pack(pop, ident) - - Texture(const std::string& file_name, bool build_mipmaps = false); - ~Texture(); - Texture(const Texture&) = delete; - Texture& operator=(const Texture&) = delete; - - int id() const; - int width() const; - int height() const; - -private: - void swapRedBlue(); - IMAGEDATA m_imagedata; - int m_id; -}; - -} - -#endif // TEXTURE_H +#endif // PPL_SRC_TEXTURE_H diff --git a/src/vertexbuffer.hpp b/src/vertexbuffer.hpp index 77549f1..1dd7361 100644 --- a/src/vertexbuffer.hpp +++ b/src/vertexbuffer.hpp @@ -1,150 +1,6 @@ -// Copyright (c) 2017, Philipp Ringler philipp@x-plane.com -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The views and conclusions contained in the software and documentation are those -// of the authors and should not be interpreted as representing official policies, -// either expressed or implied, of the FreeBSD Project. +#ifndef PPL_SRC_VERTEXBUFFER_HPP +#define PPL_SRC_VERTEXBUFFER_HPP -#ifndef VERTEXBUFFER_HPP -#define VERTEXBUFFER_HPP +#include "../include/PPL/vertexbuffer.hpp" -#include - -#if APL == 1 -#include -#elif IBM == 1 -#include -#include "GL/glew.h" -#include -#elif LIN == 1 -#include -#include -#endif - -#include "XPLMGraphics.h" - -namespace PPL{ - - -/** - * @brief A class to use Vertex Buffer Objects (VBOs) for drawing in X-Plane. - * - * To quote Ben Supnik "VBO beats the pants off of using our own memory." - * Use this class to specify vertices, normals, colors and texture coordinates for drawing - * and store them in GPU driver memory (or even in VRAM itself) and later draw them - * in a drawing callback. - * - * For efficiency, VBOs should be at least 4096 bytes long - that's somewhere - * between 70 and 341 vertices, but it depends on how much data each vertex has. - * Don't make a VBO for 4 vertices!! A few big VBOs is a lot better than many - * small ones. - * - * - */ -class VertexBuffer -{ -public: - - /** - * @brief Give the driver a hint how much the geometry in the VBO will change. - * - * Either the VBO will be set up like once to draw more or less the same - * thing every frame. - * Or you can specify the VBO is going to change every single frame. - */ - enum Type { - STATIC, //!< VBO will not change much, save in VRAM for max draw speed - STREAMING //!< Save in AGP RAM so changing vertices is not so bad. - }; - - /** @param type, whether to use static or streaming buffer - * The following params define the number of components in each vertex! - * @param num_verts 2, 3 or 4 - * @param num_normals number of normal coordinates 0 or 3 - * @param num_tex number of texture coordinates, 0 to 4 - * @param num_tex2 number of secondary texture coordinates, 0 to 4 - * @param num_colors 0 or 4 - */ - VertexBuffer(Type type, - std::size_t num_verts, - std::size_t num_normals, - std::size_t num_tex, - std::size_t num_tex2, - std::size_t num_colors); - - ~VertexBuffer(); - - /** - * @brief Provide non-null pointers for the params you want to set - * @param num_vertices - * @param out_verts - * @param out_normals - * @param out_texes - * @param out_texes2 - * @param out_colors - * @return the stride length in bytes - */ - std::size_t beginSpecifyVerts(std::size_t num_vertices, - volatile float **out_verts, - volatile float **out_normals, - volatile float **out_texes, - volatile float **out_texes2, - volatile float **out_colors); - - /** - * @brief call this when done setting the vertices - */ - void endSpecifyVerts(); - - /** - * @brief call this immediately before drawing the stuff, don't call any - * other openGL op between this and the actual drawing - */ - void setupForDraw(); - - /** - * @brief draw This works like you are used to from glDrawArrays - */ - void draw(GLenum mode, - GLint first, - GLsizei count); - - /** - * @brief endDraw call this to reset the states after drawing - */ - void endDraw(); - -private: - std::size_t num_vrt_; - std::size_t num_nrm_; - std::size_t num_tx_; - std::size_t num_tx2_; - std::size_t num_col_; - std::size_t stride_floats_; - Type mode_; - GLuint vbo_; -}; - -} - -#endif // VERTEXBUFFER_HPP +#endif // PPL_SRC_VERTEXBUFFER_HPP