1. Anders Ruud
  2. love
  3. Issues


Issue #778 closed

Using Love2d as a C++ Library

created an issue

I've recently been toying around with love2d, and it has been an outstanding way to learn lua. However, I've recently started to take up C++, and I figure that love2d may be a great way to do that.

Looking through the source code, It appears to be a complete C++ library, with only the wrap_xxx.cpp files being the lua stuff. So perhaps compiling love2d for use with C++ can just be a set of #ifdefs around the #include "wrap_xxx.h" lines.

Comments (9)

  1. Boolsheet

    No. In its current state, it can not be used as a pure C++ library. Some of the functions still tie into Lua.

    It should not be too hard to modify these functions and separate the LÖVE C++ functions from Lua, but it's just so convenient to push directly onto the Lua stack instead of going through 5 buffers.

  2. Akira Sugita

    I'm also curious about this. More specifically, I'm interested in audio and graphics with physfs integrated. One thing I'm curious is, DevIL and OpenAL whatever, are they modified to work with physfs, or simply wrapped?

  3. Alex Szpakowski

    OpenAL doesn't load files by itself in the first place, its buffers just take raw PCM data. There is the ALUT library, but it's old and LÖVE doesn't use it.

    For audio, it goes something like this: filename -> read by love.filesystem (with the physfs backend) into FileData -> decoded by love.sound into a Decoder and/or SoundData -> played by love.audio as a Source.

    For images, it's similar: filename -> read by love.filesystem into FileData -> decoded by love.image into an ImageData -> put into VRAM by love.graphics as an Image.

    In both cases, the file reading step (via love.filesystem) is done separately from the decoding step - the latter is only done for data which is already entirely in RAM; DevIL and the various love.sound backends all have functions to do it.

  4. Boolsheet

    Mostly stuff in love.physics. A really quick search shows these function using the lua_State struct. There may be more functions, perhaps doing other things with Lua.

    You can try to just use it as a C++ library, but you may run into issues where you need to look at the wrapper or the embedded Lua scripts to understand how to set it up. An example is the initialization order of the modules.

    src/modules/physics/box2d/Body.h (5 hits)
        Line 138:   int getMassData(lua_State *L);
        Line 291:   int getWorldPoints(lua_State *L);
        Line 382:   int getFixtureList(lua_State *L) const;
        Line 394:   b2Vec2 getVector(lua_State *L);
        Line 400:   int pushVector(lua_State *L, const b2Vec2 &v);
    src/modules/physics/box2d/Contact.h (2 hits)
        Line 79:    int getPositions(lua_State *L);
        Line 86:    int getNormal(lua_State *L);
    src/modules/physics/box2d/EdgeShape.h (1 hit)
        Line 56:    int getPoints(lua_State *L);
    src/modules/event/Event.h (2 hits)
        Line 51:    int toLua(lua_State *L);
        Line 52:    static Message *fromLua(lua_State *L, int n);
    src/modules/filesystem/physfs/Filesystem.h (2 hits)
        Line 269:   int enumerate(lua_State *L);
        Line 289:   static int lines_i(lua_State *L);
    src/modules/physics/box2d/Fixture.h (11 hits)
        Line 130:   int setUserData(lua_State *L);
        Line 136:   int getUserData(lua_State *L);
        Line 184:   int rayCast(lua_State *L) const;
        Line 189:   int setCategory(lua_State *L);
        Line 190:   int setMask(lua_State *L);
        Line 191:   int getCategory(lua_State *L);
        Line 192:   int getMask(lua_State *L);
        Line 193:   uint16 getBits(lua_State *L);
        Line 194:   int pushBits(lua_State *L, uint16 bits);
        Line 201:   int getBoundingBox(lua_State *L) const;
        Line 207:   int getMassData(lua_State *L) const;
    src/modules/graphics/opengl/Graphics.h (1 hit)
        Line 170:   int getScissor(lua_State *L) const;
    src/modules/physics/box2d/Joint.h (2 hits)
        Line 82:    int getAnchors(lua_State *L);
        Line 87:    int getReactionForce(lua_State *L);
    src/modules/physics/box2d/MouseJoint.h (1 hit)
        Line 62:    int getTarget(lua_State *L);
    src/modules/physics/box2d/Physics.h (3 hits)
        Line 147:   int newPolygonShape(lua_State *L);
        Line 153:   int newChainShape(lua_State *L);
        Line 271:   int getDistance(lua_State *L);
    src/modules/physics/box2d/PolygonShape.h (1 hit)
        Line 61:    int getPoints(lua_State *L);
    src/modules/physics/box2d/PrismaticJoint.h (1 hit)
        Line 135:   int getLimits(lua_State *L);
    src/modules/physics/box2d/PulleyJoint.h (1 hit)
        Line 55:    int getGroundAnchors(lua_State *L);
    src/modules/physics/box2d/RevoluteJoint.h (1 hit)
        Line 135:   int getLimits(lua_State *L);
    src/modules/physics/box2d/Shape.h (3 hits)
        Line 67:    int rayCast(lua_State *L) const;
        Line 68:    int computeAABB(lua_State *L) const;
        Line 69:    int computeMass(lua_State *L) const;
    src/modules/physics/box2d/World.h (10 hits)
        Line 153:   int setCallbacks(lua_State *L);
        Line 158:   int getCallbacks(lua_State *L);
        Line 163:   int setContactFilter(lua_State *L);
        Line 168:   int getContactFilter(lua_State *L);
        Line 182:   int getGravity(lua_State *L);
        Line 225:   int getBodyList(lua_State *L) const;
        Line 231:   int getJointList(lua_State *L) const;
        Line 237:   int getContactList(lua_State *L) const;
        Line 248:   int queryBoundingBox(lua_State *L);
        Line 253:   int rayCast(lua_State *L);

    Edit: Search was performed on an older state of this repository. Line numbers may differ.

  5. Alex Szpakowski

    It's possible to use most love modules from C++ as-is, although some functions still operate on a Lua state directly.

    In general though, LÖVE's code is written specifically for use in Lua rather than as a general C++ API. There are many idioms in the codebase that are written with the expectation that the APIs will be used in Lua – modules are reference-counted singleton objects, objects in general have special reference-counting methods, methods have parameters and return values that reflect Lua's multiple return value functionality, etc.

    If you're looking to write C++ code that uses similar functionality to what LÖVE provides, I'd recommend an actual C or C++ library like SDL or SFML.

  6. Log in to comment