Skip to content

Commit

Permalink
add wiimote pos/angle functions for joysticks
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Nov 2, 2024
1 parent 86c3bd1 commit 443af2d
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/modules/graphics/Graphics.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ namespace love

void setBlendMode(BlendMode mode, BlendAlpha alphaMode);

Quad* newQuad(Quad::Viewport viewport, double sourceWidth, double sourceHeight) const;

virtual TextureBase* newTexture(const TextureBase::Settings& settings,
const TextureBase::Slices* data = nullptr) = 0;

Expand Down
2 changes: 2 additions & 0 deletions include/modules/graphics/wrap_Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ namespace Wrap_Graphics

int newTexture(lua_State* L);

int newQuad(lua_State* L);

int newImage(lua_State* L);

int newCanvas(lua_State* L);
Expand Down
4 changes: 4 additions & 0 deletions include/modules/joystick/wrap_Joystick.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ namespace Wrap_Joystick
int setSensorEnabled(lua_State* L);

int getSensorData(lua_State* L);

int getPosition(lua_State* L);

int getAngle(lua_State* L);
} // namespace Wrap_Joystick
2 changes: 2 additions & 0 deletions platform/cafe/include/driver/display/Framebuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ namespace love
GX2ScanTarget id;

Uniform* uniform = nullptr;

glm::mat4 tmpModel;
glm::highp_mat4 ortho;

void* scanBuffer;
uint32_t scanBufferSize;
Expand Down
6 changes: 4 additions & 2 deletions platform/cafe/include/modules/joystick/kpad/Joystick.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "common/Vector.hpp"

#include "modules/joystick/Joystick.tcc"

#include <padscore/kpad.h>
Expand Down Expand Up @@ -59,9 +61,9 @@ namespace love

virtual std::vector<float> getSensorData(Sensor::SensorType type) const override;

std::array<float, 2> getPosition() const;
Vector2 getPosition() const;

std::array<float, 2> getAngle() const;
Vector2 getAngle() const;

using JoystickBase::getConstant;

Expand Down
6 changes: 2 additions & 4 deletions platform/cafe/source/driver/display/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace love
this->viewport = { 0, 0, info.width, info.height };
this->scissor = { 0, 0, info.width, info.height };

auto ortho = glm::ortho(0.0f, (float)info.width, (float)info.height, 0.0f, Z_NEAR, Z_FAR);
this->ortho = glm::ortho(0.0f, (float)info.width, (float)info.height, 0.0f, Z_NEAR, Z_FAR);

/* glm::value_ptr lets us access the data linearly rather than an XxY matrix */
uint32_t* dstModel = (uint32_t*)glm::value_ptr(this->uniform->modelView);
Expand All @@ -132,11 +132,9 @@ namespace love
for (size_t index = 0; index < count; index++)
dstModel[index] = __builtin_bswap32(model[index]);

uint32_t* projection = (uint32_t*)glm::value_ptr(ortho);
uint32_t* projection = (uint32_t*)glm::value_ptr(this->ortho);
for (size_t index = 0; index < count; index++)
dstProj[index] = __builtin_bswap32(projection[index]);

love::debugUniform(this->uniform);
}

void Framebuffer::setScissor(const Rect& scissor)
Expand Down
33 changes: 24 additions & 9 deletions platform/cafe/source/modules/joystick/kpad/Joystick.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "common/screen.hpp"

#include "modules/joystick/kpad/Joystick.hpp"

namespace love
Expand Down Expand Up @@ -435,17 +437,27 @@ namespace love
return data;
}

std::array<float, 2> Joystick::getPosition() const
static Vector2 ndcToScreen(const KPADVec2D& input)
{
std::array<float, 2> result {};
Vector2 result {};
const auto& info = love::getScreenInfo((Screen)0);

result.x = ((input.x) / 2) * info.width;
result.y = ((input.y + 1.0f) / 2) * info.height;

return result;
}

Vector2 Joystick::getPosition() const
{
switch (this->gamepadType)
{
case GAMEPAD_TYPE_NINTENDO_WII_REMOTE:
case GAMEPAD_TYPE_NINTENDO_WII_REMOTE_NUNCHUK:
{
result[0] = this->status.pos.x;
result[1] = this->status.pos.y;
if (this->status.posValid)
return ndcToScreen(this->status.pos);

break;
}
case GAMEPAD_TYPE_NINTENDO_WII_CLASSIC:
Expand All @@ -454,20 +466,23 @@ namespace love
break;
}

return result;
return Vector2 {};
}

std::array<float, 2> Joystick::getAngle() const
Vector2 Joystick::getAngle() const
{
std::array<float, 2> result {};
Vector2 result {};

switch (this->gamepadType)
{
case GAMEPAD_TYPE_NINTENDO_WII_REMOTE:
case GAMEPAD_TYPE_NINTENDO_WII_REMOTE_NUNCHUK:
{
result[0] = this->status.angle.x;
result[1] = this->status.angle.y;
if (!this->status.posValid)
break;

result.x = this->status.angle.x;
result.y = this->status.angle.y;
break;
}
case GAMEPAD_TYPE_NINTENDO_WII_CLASSIC:
Expand Down
5 changes: 5 additions & 0 deletions source/modules/graphics/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ namespace love
return data;
}

Quad* GraphicsBase::newQuad(Quad::Viewport viewport, double sourceWidth, double sourceHeight) const
{
return new Quad(viewport, sourceWidth, sourceHeight);
}

TextBatch* GraphicsBase::newTextBatch(FontBase* font, const std::vector<ColoredString>& text)
{
return new TextBatch(font, text);
Expand Down
50 changes: 50 additions & 0 deletions source/modules/graphics/wrap_Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,55 @@ int Wrap_Graphics::newTexture(lua_State* L)
return pushNewTexture(L, slicesRef, settings);
}

int Wrap_Graphics::newQuad(lua_State* L)
{
luax_checkgraphicscreated(L);

Quad::Viewport viewport {};
viewport.x = luaL_checknumber(L, 1);
viewport.y = luaL_checknumber(L, 2);
viewport.w = luaL_checknumber(L, 3);
viewport.h = luaL_checknumber(L, 4);

double sourceWidth = 0.0f;
double sourceHeight = 0.0f;

int layer = 0;

if (luax_istype(L, 5, TextureBase::type))
{
TextureBase* texture = luax_checktexture(L, 5);
sourceWidth = texture->getWidth();
sourceHeight = texture->getHeight();
}
else if (luax_istype(L, 6, TextureBase::type))
{
layer = (int)luaL_checkinteger(L, 5) - 1;
TextureBase* texture = luax_checktexture(L, 6);
sourceWidth = texture->getWidth();
sourceHeight = texture->getHeight();
}
else if (!lua_isnoneornil(L, 7))
{
layer = (int)luaL_checkinteger(L, 5) - 1;
sourceWidth = luaL_checknumber(L, 6);
sourceHeight = luaL_checknumber(L, 7);
}
else
{
sourceWidth = luaL_checknumber(L, 5);
sourceHeight = luaL_checknumber(L, 6);
}

Quad* quad = instance()->newQuad(viewport, sourceWidth, sourceHeight);
quad->setLayer(layer);

luax_pushtype(L, quad);
quad->release();

return 1;
}

int Wrap_Graphics::newImage(lua_State* L)
{
return newTexture(L);
Expand Down Expand Up @@ -1823,6 +1872,7 @@ static constexpr luaL_Reg functions[] =
{ "line", Wrap_Graphics::line },

{ "newTexture", Wrap_Graphics::newTexture },
{ "newQuad", Wrap_Graphics::newQuad },
{ "newImage", Wrap_Graphics::newImage },

// { "newMesh", Wrap_Graphics::newMesh },
Expand Down
45 changes: 44 additions & 1 deletion source/modules/joystick/wrap_Joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,49 @@ static constexpr luaL_Reg functions[] = {
{ "getSensorData", Wrap_Joystick::getSensorData },
{ "getConnectedIndex", Wrap_JoystickModule::getIndex }
};

#if !defined(__WIIU__)
static constexpr luaL_Reg extFunctions[] = {};
#else
#include "modules/joystick/kpad/Joystick.hpp"

int Wrap_Joystick::getPosition(lua_State*L)
{
auto* base = luax_checkjoystick(L, 1);
const auto type = base->getGamepadType();

if (type != GAMEPAD_TYPE_NINTENDO_WII_REMOTE && type != GAMEPAD_TYPE_NINTENDO_WII_REMOTE_NUNCHUK)
return luaL_error(L, "Invalid controller! Must be of type Wii Remote");

auto position = ((kpad::Joystick*)base)->getPosition();

lua_pushnumber(L, position.x);
lua_pushnumber(L, position.y);

return 2;
}

int Wrap_Joystick::getAngle(lua_State*L)
{
auto* base = luax_checkjoystick(L, 1);
const auto type = base->getGamepadType();

if (type != GAMEPAD_TYPE_NINTENDO_WII_REMOTE && type != GAMEPAD_TYPE_NINTENDO_WII_REMOTE_NUNCHUK)
return luaL_error(L, "Invalid controller! Must be of type Wii Remote");

auto angle = ((kpad::Joystick*)base)->getAngle();

lua_pushnumber(L, angle.x);
lua_pushnumber(L, angle.y);

return 2;
}

static constexpr luaL_Reg extFunctions[] = {
{ "getPosition", Wrap_Joystick::getPosition },
{ "getAngle", Wrap_Joystick::getAngle }
};
#endif
// clang-format on

namespace love
Expand All @@ -441,6 +484,6 @@ namespace love

int open_joystick(lua_State* L)
{
return luax_register_type(L, &JoystickBase::type, functions);
return luax_register_type(L, &JoystickBase::type, functions, extFunctions);
}
} // namespace love

0 comments on commit 443af2d

Please sign in to comment.