Skip to content

Commit

Permalink
Add a new module known as Sample onto Love2D
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimateprogramer committed Oct 3, 2020
1 parent f359764 commit fd007c5
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/common/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class Module : public Object
M_TOUCH,
M_VIDEO,
M_WINDOW,
M_MAX_ENUM
M_SAMPLE,
M_MAX_ENUM,
};

Module();
Expand Down
7 changes: 6 additions & 1 deletion src/modules/love/love.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// C++
#include <string>
#include <sstream>
#include <iostream>

#ifdef LOVE_WINDOWS
#include <windows.h>
Expand Down Expand Up @@ -83,6 +84,7 @@ extern "C"
// of addressing implementations directly.
extern "C"
{
extern int luaopen_love_sample(lua_State *);
#if defined(LOVE_ENABLE_AUDIO)
extern int luaopen_love_audio(lua_State*);
#endif
Expand Down Expand Up @@ -145,6 +147,7 @@ extern "C"
}

static const luaL_Reg modules[] = {
{ "love.sample", luaopen_love_sample },
#if defined(LOVE_ENABLE_AUDIO)
{ "love.audio", luaopen_love_audio },
#endif
Expand Down Expand Up @@ -462,8 +465,10 @@ int luaopen_love(lua_State *L)
}

// Preload module loaders.
for (int i = 0; modules[i].name != nullptr; i++)
for (int i = 0; modules[i].name != nullptr; i++) {
std::cout << "Loading module: " << modules[i].name << " with: " << modules[i].func << "\n";
love::luax_preload(L, modules[i].func, modules[i].name);
}

// Necessary for Data-creating methods to work properly in Data subclasses.
love::luax_require(L, "love.data");
Expand Down
46 changes: 46 additions & 0 deletions src/modules/sample/Sample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2006-2019 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/

#ifndef LOVE_SAMPLE_SAMPLE_H
#define LOVE_SAMPLE_SAMPLE_H

#include "common/Module.h"

namespace love
{
namespace sample
{

class Sample : public Module
{
public:
Sample() {}
virtual ~Sample() {}

// Implements Module.
virtual ModuleType getModuleType() const { return M_SAMPLE; }
const char *getName() const override { return "love.sample"; }

}; // Sample

} // sample
} // love

#endif // LOVE_SAMPLE_SAMPLE_H
79 changes: 79 additions & 0 deletions src/modules/sample/wrap_Sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2006-2019 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/

// LOVE
#include "Sample.h"
#include "wrap_Sample.h"
#include "common/config.h"

#include <iostream>

namespace love
{
namespace sample
{

#define instance() (Module::getInstance<Sample>(Module::M_SAMPLE))

int w_doSum(lua_State *L) {
int num1 = (int) luaL_optinteger(L, 1, 0);
int num2 = (int) luaL_optinteger(L, 2, 0);
lua_pushnumber(L, num1 + num2);
return 1;
}

// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "doSum", w_doSum },
{ 0, 0 }
};

extern "C" int luaopen_love_sample(lua_State *L)
{
Sample *instance = instance();

if (instance == nullptr)
{
std::cout << "Sample Module was null\n";
luax_catchexcept(L, [&](){ instance = new love::sample::Sample(); });
}
else
{
std::cout << "Sample Module was not null\n";
instance->retain();
}

std::cout << "Wrapping sample module\n";

WrappedModule w;
w.module = instance;
w.name = "sample";
w.type = &Module::type;
w.functions = functions;
w.types = 0;

std::cout << "Registering 'sample' onto Lua\n";

return luax_register_module(L, w);
}

} // sample
} // love
38 changes: 38 additions & 0 deletions src/modules/sample/wrap_Sample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2006-2019 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/

#ifndef LOVE_MOUSE_WRAP_SAMPLE_H
#define LOVE_MOUSE_WRAP_SAMPLE_H

// LOVE
#include "common/runtime.h"
#include "common/config.h"

namespace love
{
namespace sample
{

extern "C" LOVE_EXPORT int luaopen_love_sample(lua_State *L);

} // sample
} // love

#endif // LOVE_MOUSE_WRAP_SAMPLE_H
3 changes: 3 additions & 0 deletions src/scripts/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ function love.init()
thread = true,
window = true,
video = true,
sample = true,
},
audio = {
mixwithsystem = true, -- Only relevant for Android / iOS.
Expand Down Expand Up @@ -500,8 +501,10 @@ function love.init()
"graphics",
"math",
"physics",
"sample",
} do
if c.modules[v] then
print("loading: " .. v)
require("love." .. v)
end
end
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/boot.lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ const unsigned char boot_lua[] =
0x09, 0x09, 0x09, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a,
0x09, 0x09, 0x7d, 0x2c, 0x0a,
0x09, 0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x3d, 0x20, 0x7b, 0x0a,
0x09, 0x09, 0x09, 0x6d, 0x69, 0x78, 0x77, 0x69, 0x74, 0x68, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x3d,
Expand Down Expand Up @@ -988,9 +989,12 @@ const unsigned char boot_lua[] =
0x09, 0x09, 0x22, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x22, 0x2c, 0x0a,
0x09, 0x09, 0x22, 0x6d, 0x61, 0x74, 0x68, 0x22, 0x2c, 0x0a,
0x09, 0x09, 0x22, 0x70, 0x68, 0x79, 0x73, 0x69, 0x63, 0x73, 0x22, 0x2c, 0x0a,
0x09, 0x09, 0x22, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x22, 0x2c, 0x0a,
0x09, 0x7d, 0x20, 0x64, 0x6f, 0x0a,
0x09, 0x09, 0x69, 0x66, 0x20, 0x63, 0x2e, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x5b, 0x76, 0x5d, 0x20,
0x74, 0x68, 0x65, 0x6e, 0x0a,
0x09, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x28, 0x22, 0x6c, 0x6f, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x3a,
0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x76, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x28, 0x22, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x22,
0x20, 0x2e, 0x2e, 0x20, 0x76, 0x29, 0x0a,
0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
Expand Down

0 comments on commit fd007c5

Please sign in to comment.