Commits

mitsuhiko  committed e8747cf

Initial checkin

  • Participants

Comments (0)

Files changed (11)

+neglect/vs

File examples/helloworld/hello_world.cpp

+/* simple hello world example application that displays a "Hello World!"
+   message that moves from left to right and when it touches the edge
+   it will reverse the movement. */
+
+#include <neglect.hpp>
+
+namespace {
+
+    // how often do we want to update?
+    const float update_interval = 1.0f / 60;
+
+    // holds a sans serif font
+    neglect::font sans_font;
+
+    // the message we want to display
+    neglect::sprite message;
+
+    // in what direction is the font moving?
+    int direction = 1;
+
+    // called to setup the application
+    void setup()
+    {
+        // get active application and window
+        neglect::application &app = neglect::application::active();
+        neglect::renderable &ren = neglect::render_window::active();
+
+        // for the 2d context we use internally a 1280x720
+        // coordinate system where 0,0 is the lower level corner.
+        neglect::render_context &ctx = ren.context_2d();
+        ctx.set_ortho(0.0f, 0.0f, 1280.0f, 720.0f);
+
+        // load the font from the application's resource folder
+        sans_font = app.load_resource<font>("sans.ttf");
+
+        // create the message we want to display
+        message = neglect::text("Hello World!", 48, 0xffffffff);
+        message.position(vec2(460.0f, 10.0f));
+    }
+
+    // called whenever the renderable (window) needs to be redrawn
+    void draw(neglect::renderable &ren)
+    {
+        // clear the screen with a black color
+        ren.clear(0x000000ff);
+
+        // set the line color to red and make the line 2px wide
+        ren.line_color(0xff0000ff);
+        ren.line_width(2.0f);
+
+        // draw a box
+        ren.draw_rect(rect(10.0f, 10.0, 1850.0f, 700.0f));
+
+        // render the text
+        message.draw(ren);
+    }
+
+    // called every update_interval seconds.  the timedelta shows how
+    // much time was between the last time this function was called
+    // and now.
+    void update(const neglect::timedelta &dt)
+    {
+        vec2 newpos = message.position() += vec2(direction * 40 * dt, 0.0f); 
+        if (newpos.x <= 10.0f || newpos.x >= 1270.0f)
+            direction *= -1;
+        else
+            message.position(newpos);
+
+        // redraw the screen
+        neglect::render_window::active().draw();
+    }
+
+    // tear down everything
+    void quit()
+    {
+        neglect::application::active().quit();
+    }
+
+    void run_app()
+    {
+        // first create an application object.  we can either pass it
+        // explicitly to functions and classes or bind it to the
+        // current active thread which we will do.
+        neglect::application app;
+        NEGLECT_THREADBOUND(app);
+
+        // next we need a window and bind it.
+        neglect::render_window win(640, 480);
+        NEGLECT_THREADBOUND(win);
+
+        // setup the hello world application
+        setup();
+
+        // schedule the update function
+        app.schedule(update, update_interval);
+
+        // handle draw events if the window is the sender
+        NEGLECT_CONNECT(on_draw, draw, win);
+
+        // react to the window's quit button or any quit
+        // event
+        NEGLECT_CONNECT(on_quit, quit, win);
+
+        // run the application
+        app.quit();
+    }
+}
+
+
+// NEGLECT_MAIN() expands to either main() or WinMain() depending on which
+// operating system we are on.  The return value is an implicit integer
+NEGLECT_MAIN()
+{
+    // for non-debug builds we activate an error console.  Writing to
+    // std::cerr and std::clog will write to the debug output.  On
+    // linux and OS X this does nothing, on windows it will ensure a
+    // console opens with the window.
+#ifndef _NDEBUG
+    neglect::platform::activate_debug_output();
+#endif
+
+    // try to run the application
+    try {
+        run_app();
+        return 0;
+    }
+
+    // if it fails with an exception, we get the error details as string
+    // and show it in an error dialog.
+    catch (std::exception &e) {
+        std::string msg = neglect::get_error_details(e);
+        neglect::platform::show_error_dialog("Critical Error", msg);
+        return 1;
+    }
+}

File neglect/CMakeLists.txt

+cmake_minimum_required(VERSION 2.6)
+
+project(Neglect)
+
+set(SOURCES
+	src/exception.cpp
+)
+set(HEADERS
+	include/neglect/boot.hpp
+	include/neglect/exception.hpp
+	include/neglect/vector.hpp
+	include/neglect/_vector2.hpp
+	include/neglect/_vector3.hpp
+	include/neglect/_vector4.hpp
+)
+
+find_package(Boost)
+
+include_directories(
+	${CMAKE_CURRENT_SOURCE_DIR}/include
+	${Boost_INCLUDE_DIRS}
+)
+
+add_library(neglect STATIC ${SOURCES} ${HEADERS})

File neglect/generate-vs-solution.bat

+@echo off
+echo -- Generating Visual Studio Solution
+echo.
+
+if not exist vs\null mkdir vs
+cd vs
+cmake ..
+
+echo.
+pause

File neglect/include/neglect/_vector2.hpp

+#ifndef _INC_NEGLECT__VECTOR2_HPP_
+#define _INC_NEGLECT__VECTOR2_HPP_
+
+#include <neglect/vector.hpp>
+
+namespace neglect {
+
+    /* two dimensional vector */
+    template <typename T>
+    class vector<T, 2> {
+    public:
+        union {
+            struct { T x, y };
+            struct { T s, t };
+        };
+
+        // -- constructors
+
+        template <class Sequence>
+        explicit vector(Sequence seq) : x(seq[0]), y(seq[1])
+        {
+        }
+
+        vector(T x, T y) : x(x), y(y)
+        {
+        }
+
+        // -- subscript operators
+
+        T &operator[](const size_t idx)
+        {
+            assert(x < 2);
+            return *(&x + idx);
+        }
+
+        T operator[](const size_t idx) const
+        {
+            assert(x < 2);
+            return *(&x + idx);
+        }
+
+        // -- comparison operators
+
+        bool operator==(const vector<T, 2> &other) const
+        {
+            return x == other.x && y == other.y;
+        }
+
+        bool operator!=(const vector<T, 2> &other) const
+        {
+            return x != other.x || y != other.y;
+        }
+
+        // -- arithmetic operators
+
+        vector<T, 2> operator+(const vector<T, 2> &other) const
+        {
+            return vector<T, 2>(x + other.x, y + other.y);
+        }
+
+        vector<T, 2> operator-(const vector<T, 2> &other) const
+        {
+            return vector<T, 2>(x - other.x, y - other.y);
+        }
+
+        vector<T, 2> operator*(const vector<T, 2> &other) const
+        {
+            return vector<T, 2>(x * other.x, y * other.y);
+        }
+
+        template <class ScalarType>
+        vector<T, 2> operator*(ScalarType scalar) const
+        {
+            return vector<T, 2>(x * scalar, y * scalar);
+        }
+
+        vector<T, 2> operator/(const vector<T, 2> &other) const
+        {
+            assert(other.x && other.y);
+            return vector<T, 2>(x / other.x, y / other.y);
+        }
+
+        template <class ScalarType>
+        vector<T, 2> operator/(ScalarType scalar) const
+        {
+            assert(scalar);
+            ScalarType inv = static_cast<ScalarType>(1) / scalar;
+            return vector<T, 2>(x * inv, y * inv);
+        }
+
+        const vector<T, 2> &operator+() const
+        {
+            return *this;
+        }
+
+        vector<T, 2> operator-() const
+        {
+            return vector<T, 2>(-x, -y);
+        }
+
+        template <class ScalarType>
+        friend vector<T, 2> operator*(ScalarType scalar, const vector<T, 2> &vec)
+        {
+            return vector<T, 2>(vec.x * scalar, vec.y * scalar);
+        }
+
+        template <class ScalarType>
+        friend vector<T, 2> operator/(ScalarType scalar, const vector<T, 2> &vec)
+        {
+            assert(scalar);
+            ScalarType inv = static_cast<ScalarType>(1) / scalar;
+            return vector<T, 2>(vec.x * inv, vec.y * inv);
+        }
+
+        // -- arithmetic assignment operators
+        vector<T, 2> &operator+=(const vector<T, 2> &other)
+        {
+            x += other.x;
+            y += other.y;
+        }
+
+        vector<T, 2> &operator-=(const vector<T, 2> &other)
+        {
+            x -= other.x;
+            y -= other.y;
+        }
+
+        vector<T, 2> &operator*=(const vector<T, 2> &other)
+        {
+            x *= other.x;
+            y *= other.y;
+        }
+
+        template <class ScalarType>
+        vector<T, 2> &operator*=(ScalarType scalar)
+        {
+            x *= other;
+            y *= other;
+        }
+
+        vector<T, 2> &operator/=(const vector<T, 2> &other)
+        {
+            x /= other.x;
+            y /= other.y;
+        }
+
+        template <class ScalarType>
+        vector<T, 2> &operator/=(ScalarType scalar)
+        {
+            assert(scalar);
+            ScalarType inv = static_cast<ScalarType>(1) / scalar;
+            x *= inv;
+            y *= inv;
+        }
+
+        // -- helper methods
+
+        T length() const
+        {
+            return math::sqrt(x * x + y * y);
+        }
+
+        T norm() const
+        {
+            return x * x + y * y;
+        }
+
+        size_t size() const { return 2; }
+    };
+}
+
+#endif

File neglect/include/neglect/_vector3.hpp

+#ifndef _INC_NEGLECT_VECTOR3_HPP_
+#define _INC_NEGLECT_VECTOR3_HPP_
+
+#include <neglect/vector.hpp>
+
+namespace neglect {
+
+    /* three dimensional vector */
+    template <typename T>
+    class vector<T, 3> {
+    public:
+        union {
+            struct { T x, y, z };
+            struct { T r, g, b };
+            struct { T s, t, p };
+        };
+
+        // -- constructors
+
+        template <class Sequence>
+        explicit vector(Sequence seq) : x(seq[0]), y(seq[1]), z(seq[2])
+        {
+        }
+
+        vector(T x, T y, T z) : x(x), y(y), z(z)
+        {
+        }
+
+        // -- subscript operators
+
+        T &operator[](const size_t idx)
+        {
+            assert(x < 3);
+            return *(&x + idx);
+        }
+
+        T operator[](const size_t idx) const
+        {
+            assert(x < 3);
+            return *(&x + idx);
+        }
+
+        // -- comparison operators
+
+        bool operator==(const vector<T, 3> &other) const
+        {
+            return x == other.x && y == other.y && z == other.z;
+        }
+
+        bool operator!=(const vector<T, 3> &other) const
+        {
+            return x != other.x || y != other.y || z != other.z;
+        }
+
+        // -- helper methods
+
+        size_t size() const { return 3; }
+    };
+}
+
+#endif

File neglect/include/neglect/_vector4.hpp

+#ifndef _INC_NEGLECT_VECTOR4_HPP_
+#define _INC_NEGLECT_VECTOR4_HPP_
+
+#include <neglect/vector.hpp>
+
+namespace neglect {
+
+    /* four dimensional vector */
+    template <typename T>
+    class vector<T, 4> {
+    public:
+        union {
+            struct { T x, y, z, w };
+            struct { T r, g, b, a };
+            struct { T s, t, p, q };
+        };
+
+        // -- constructors
+
+        template <class Sequence>
+        explicit vector(Sequence seq) : x(seq[0]), y(seq[1]), z(seq[2]), w(seq[3])
+        {
+        }
+
+        vector(T x, T y, T z, T w) : x(x), y(y), z(z), w(w)
+        {
+        }
+
+        // -- subscript operators
+
+        T &operator[](const size_t idx)
+        {
+            assert(x < 4);
+            return *(&x + idx);
+        }
+
+        T operator[](const size_t idx) const
+        {
+            assert(x < 4);
+            return *(&x + idx);
+        }
+
+        // -- comparison operators
+
+        bool operator==(const vector<T, 4> &other) const
+        {
+            return x == other.x && y == other.y && z == other.z && w == other.w;
+        }
+
+        bool operator!=(const vector<T, 4> &other) const
+        {
+            return x != other.x || y != other.y || z != other.z || w != other.w;
+        }
+
+        // -- helper methods
+
+        size_t size() const { return 4; }
+    };
+}
+
+#endif

File neglect/include/neglect/boot.hpp

+/* defines some common things like platform macros and includes
+   other important builtins like the shared pointers. */
+#ifndef _INC_NEGLECT_BOOT_HPP_
+#define _INC_NEGLECT_BOOT_HPP_
+
+// platform identifiers
+#define NEGLECT_PLATFORM_WINDOWS 1
+#define NEGLECT_PLATFORM_OSX     2
+
+// figure out the current platform
+#if defined(WIN32) || defined(_WINDOWS)
+#  define _CRT_SECURE_NO_WARNINGS
+#  define WIN32_LEAN_AND_MEAN
+#  define NOMINMAX
+#  include <windows.h>
+#  include <fcntl.h>
+#  include <io.h>
+#  include <conio.h>
+#  define NEGLECT_PLATFORM NEGLECT_PLATFORM_WINDOWS
+#elif defined(__APPLE__) && defined(__MACH__)
+#  define NEGLECT_PLATFORM NEGLECT_PLATFORM_OSX
+#else
+#  error "unsupported platform"
+#endif
+
+// core includes
+#include <string>
+#include <vector>
+#include <map>
+#include <iostream>
+#include <exception>
+#include <memory>
+#include <ctime>
+#include <cmath>
+
+// boost tr1 includes we need.  This is pretty much a noop for systems
+// where a tr1 implementation exists.
+#include <boost/tr1/memory.hpp>
+
+// use the boost shared_array
+#include <boost/shared_array.hpp>
+
+namespace neglect {
+    // the types we use.  This is typedefed in case anyone wants to
+    // later add support for alternative string types etc.
+    using std::string;
+    using std::vector;
+    using std::map;
+
+    // if tr1 is unavailable, boost will add that for us.
+    using std::tr1::shared_ptr;
+    using boost::shared_array;
+
+    // currently these are just aliases to the C/C++ math functions
+    // but this gives the possibility to replace them later with more
+    // optimized functions
+    namespace math {
+        using std::sqrt;
+    }
+}
+
+// standard exception class
+#include <neglect/exception.hpp>
+
+#endif

File neglect/include/neglect/exception.hpp

+#ifndef _INC_NEGLECT_EXCEPTION_HPP_
+#define _INC_NEGLECT_EXCEPTION_HPP_
+
+#include <neglect/boot.hpp>
+
+namespace neglect {
+
+    /* the error baseclass used for all exceptions in neglect.  ensures
+       that it's safe to pass a string to the constructor and throw
+       the object from the stack.  The copy constructor will not fail. */
+    class exception : public std::exception {
+    public:
+        exception();
+
+        exception(const string &msg);
+
+        const char *what() throw()
+        {
+            return m_message.get();
+        }
+
+        string message() const
+        {
+            return string(m_message.get());
+        }
+
+    private:
+        shared_array<char> m_message;
+    };
+}
+
+#endif

File neglect/include/neglect/vector.hpp

+#ifndef _INC_NEGLECT_VECTOR_HPP_
+#define _INC_NEGLECT_VECTOR_HPP_
+
+#include <neglect/boot.hpp>
+
+namespace neglect {
+
+    /* a vector itself is a fake class.  only the specializations for
+       2, 3 and 4 items currently expand to a usable class. */
+    template <typename T, size_t Size>
+    class vector {
+        bool invalid_vector_size[0];
+    };
+
+    // common typedefs
+    typedef vector<float, 2> vec2;
+    typedef vector<float, 3> vec3;
+    typedef vector<float, 4> vec4;
+    typedef vector<int, 2> veci2;
+    typedef vector<int, 3> veci3;
+    typedef vector<int, 4> veci4;
+
+    /* normalizes a vector.  If the vector is close to zero or zero, nothing
+       is done instead.  This should avoid common errors where a code tries
+       to operate on an unknown vector.  If the vector is close to 0, nothing
+       is done instead. */
+    template <typename T, size_t Size>
+    normalize(const vector<T, Size> &vec)
+    {
+        T length = vec.length();
+        return (length > 1e-08) ? vec /= length : vec;
+    }
+
+    /* calculates the cross product of two two-dimensional vectors */
+    template <typename T>
+    T cross(const vector<T, 2> &lhs, const vector<T, 2> &rhs)
+    {
+        return lhs.x * rhs.y - y * rhs.x.
+    }
+
+    /* calculates the dot product of two two-dimensional vectors */
+    template <typename T>
+    T dot(const vector<T, 2> &lhs, const vector<T, 2> &rhs)
+    {
+        return lhs.x * rhs.x + lhs.y * rhs.y;
+    }
+
+    /* returns a reflection vector to the plane with the given normal vector. */
+    template <typename T, size_t Size>
+    vector<T, Size> reflect(const vector<T, Size> &vec,
+                            const vector<T, Size> &normal)
+    {
+        return vec - (2 * dot(vec, normal) * normal);
+    }
+}
+
+// the actual vector implementations
+#include <neglect/_vector2.hpp>
+#include <neglect/_vector3.hpp>
+#include <neglect/_vector4.hpp>
+
+#endif

File neglect/src/exception.cpp

+#include <neglect/exception.hpp>
+
+using namespace neglect;
+
+exception::exception()
+{
+    m_message.reset(new char[1]);
+    m_message.get()[0] = 0;
+}
+
+exception::exception(const string &msg)
+{
+    size_t len = msg.size();
+    m_message.reset(new char[len]);
+    ::memcpy(m_message.get(), msg.c_str(), len + 1);
+}