Skip to content

Commit

Permalink
add optional mode to newFile, fix love.math.random, and patch SDL2's …
Browse files Browse the repository at this point in the history
…joystick init to allow libnx to handle dual mode
  • Loading branch information
= committed May 14, 2018
1 parent a551e2d commit f69e657
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/objects/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class File
{
public:
File(const char * path);
File(const char * path, const char * mode);

File() {};
~File();

Expand Down
2 changes: 2 additions & 0 deletions source/modules/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

void Joystick::Initialize(lua_State * L)
{
SDL_InitSubSystem(SDL_INIT_JOYSTICK);

gamepadNew(L);
}

Expand Down
8 changes: 6 additions & 2 deletions source/modules/mod_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ int Math::Random(lua_State * L)
lua_getfield(L, -1, "random");
lua_remove(L, -2);

if (args == 0)
{
lua_call(L, 0, 1);
}
if (args == 1)
{
lua_pushnumber(L, upper);

lua_call(L, 2, 1);
lua_call(L, 1, 1);
}
else
else if (args == 2)
{
lua_pushnumber(L, lower);
lua_pushnumber(L, upper);
Expand Down
10 changes: 9 additions & 1 deletion source/objects/file/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ File::File(const char * path)
this->open = false;
}

File::File(const char * path, const char * mode)
{
this->path = strdup(path);
this->open = false;

this->Open(mode);
}

File::~File()
{
if (this->open)
Expand Down Expand Up @@ -48,7 +56,7 @@ bool File::Open(const char * mode)
return false;
}

this->mode = mode;
this->mode = strdup(mode);
this->open = true;

return true;
Expand Down
12 changes: 10 additions & 2 deletions source/objects/file/wrap_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
int fileNew(lua_State * L)
{
const char * path = luaL_checkstring(L, 1);
string mode = "";

string abspath = Filesystem::GetSaveDirectory() + string(path);

if (!lua_isnoneornil(L, 2))
mode = (string)luaL_checkstring(L, 2);

void * raw_self = luaobj_newudata(L, sizeof(File));

luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);

if (mode != "")
File * self = new (raw_self) File(abspath.c_str(), mode.c_str());
else
File * self = new (raw_self) File(abspath.c_str());

File * self = new (raw_self) File(abspath.c_str());

return 1;
}

Expand Down
2 changes: 2 additions & 0 deletions source/objects/gamepad/wrap_gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ int gamepadNew(lua_State * L)

luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);

SDL_JoystickOpen(0);

Gamepad * self = new (raw_self) Gamepad(controllers.size());

lua_getglobal(L, "__controllers"); //get global table
Expand Down

0 comments on commit f69e657

Please sign in to comment.