Skip to content

Commit

Permalink
i made controller support but forgot what even for
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Sep 3, 2024
1 parent b7a22e3 commit eb6114f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
48 changes: 48 additions & 0 deletions freesprite/Gamepad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "Gamepad.h"
#include "Notification.h"

void Gamepad::TryCaptureGamepad()
{
int n = SDL_NumJoysticks();
if (n > 0) {
gamepad = SDL_GameControllerOpen(0);
}

if (gamepad != NULL) {
g_addNotification(Notification("Gamepad connected", SDL_GameControllerName(gamepad)));
}
gamepadConnected = gamepad != NULL;
}

void Gamepad::TakeEvent(SDL_Event evt)
{
switch (evt.type) {
case SDL_CONTROLLERDEVICEADDED:
if (!gamepadConnected) {
TryCaptureGamepad();
}
break;
case SDL_CONTROLLERDEVICEREMOVED:
if (gamepad == NULL || !SDL_GameControllerGetAttached(gamepad)) {
gamepad = NULL;
gamepadConnected = false;
TryCaptureGamepad();
}
break;
case SDL_CONTROLLERAXISMOTION:
if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) {
gamepadLSX = evt.caxis.value / 32768.0f;
}
if (evt.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) {
gamepadLSY = evt.caxis.value / 32768.0f;
}
break;
}
}

void Gamepad::SetLightbar(uint8_t r, uint8_t g, uint8_t b)
{
if (gamepad != NULL) {
SDL_GameControllerSetLED(gamepad, r, g, b);
}
}
15 changes: 15 additions & 0 deletions freesprite/Gamepad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include "globals.h"

class Gamepad
{
public:
float gamepadLSX = 0, gamepadLSY = 0;

bool gamepadConnected = false;
SDL_GameController* gamepad = NULL;
void TryCaptureGamepad();
void TakeEvent(SDL_Event evt);
void SetLightbar(uint8_t r, uint8_t g, uint8_t b);
};

2 changes: 2 additions & 0 deletions freesprite/freesprite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<ClCompile Include="BrushFill.cpp" />
<ClCompile Include="BrushRect.cpp" />
<ClCompile Include="BrushRectFill.cpp" />
<ClCompile Include="Gamepad.cpp" />
<ClCompile Include="PopupTextTool.cpp" />
<ClCompile Include="PopupTileGeneric.cpp" />
<ClCompile Include="ToolRectSwap.cpp" />
Expand Down Expand Up @@ -258,6 +259,7 @@
<ClInclude Include="BrushFill.h" />
<ClInclude Include="BrushRect.h" />
<ClInclude Include="BrushRectFill.h" />
<ClInclude Include="Gamepad.h" />
<ClInclude Include="PopupTextTool.h" />
<ClInclude Include="PopupTileGeneric.h" />
<ClInclude Include="ToolRectSwap.h" />
Expand Down
6 changes: 6 additions & 0 deletions freesprite/freesprite.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@
<ClCompile Include="PopupTileGeneric.cpp">
<Filter>Source files\popup</Filter>
</ClCompile>
<ClCompile Include="Gamepad.cpp">
<Filter>Source files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="globals.h">
Expand Down Expand Up @@ -632,6 +635,9 @@
<ClInclude Include="PopupTileGeneric.h">
<Filter>Header files\popup</Filter>
</ClInclude>
<ClInclude Include="Gamepad.h">
<Filter>Header files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="freesprite.rc">
Expand Down
2 changes: 2 additions & 0 deletions freesprite/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Notification;
class UILabel;
class UITextField;
class ScrollingView;
class Gamepad;

template <typename T>
class ScreenWideNavBar;
Expand All @@ -82,6 +83,7 @@ extern std::string g_programDirectory;
extern SDL_Window* g_wd;
extern SDL_Renderer* g_rd;
extern TextRenderer* g_fnt;
extern Gamepad* g_gamepad;
extern int g_mouseX, g_mouseY;
extern std::vector<BaseBrush*> g_brushes;
extern std::vector<Pattern*> g_patterns;
Expand Down
7 changes: 6 additions & 1 deletion freesprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "BrushReplaceColor.h"
#include "ToolRectSwap.h"
#include "ToolText.h"
#include "Gamepad.h"

#include "ee_creature.h"

Expand All @@ -30,6 +31,7 @@ SDL_Window* g_wd;
SDL_Renderer* g_rd;
int g_mouseX = 0, g_mouseY = 0;
TextRenderer* g_fnt;
Gamepad* g_gamepad = NULL;
std::vector<std::string> g_cmdlineArgs;
bool fullscreen = false;
bool g_ctrlModifier = false;
Expand Down Expand Up @@ -207,6 +209,9 @@ int main(int argc, char** argv)
SDL_FreeSurface(srf);
//SDL_Texture* the_creature = IMGLoadToTexture("assets/kaosekai.png");

g_gamepad = new Gamepad();
g_gamepad->TryCaptureGamepad();

//load brushes
g_brushes.push_back(new Brush1x1());
g_brushes.push_back(new Brush1x1ArcX());
Expand Down Expand Up @@ -339,7 +344,7 @@ int main(int argc, char** argv)
//g_addNotification(Notification("WARNING", "<- the creature", 5000, the_creature));
break;
}

g_gamepad->TakeEvent(evt);
if (!popupStack.empty() && popupStack[popupStack.size() - 1]->takesInput()) {
popupStack[popupStack.size() - 1]->takeInput(evt);
}
Expand Down
13 changes: 12 additions & 1 deletion freesprite/maineditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "TilemapPreviewScreen.h"
#include "MinecraftSkinPreviewScreen.h"
#include "PopupTileGeneric.h"
#include "Gamepad.h"

MainEditor::MainEditor(XY dimensions) {

Expand Down Expand Up @@ -222,14 +223,24 @@ void MainEditor::render() {
}

void MainEditor::tick() {

if (abs(g_gamepad->gamepadLSX) > 0.05f || abs(g_gamepad->gamepadLSY) > 0.05f) {
canvasCenterPoint.x += g_gamepad->gamepadLSX * 10;
canvasCenterPoint.y += g_gamepad->gamepadLSY * 10;
RecalcMousePixelTargetPoint(g_windowW / 2, g_windowH / 2);
currentBrush->mouseMotion(this, currentBrush->wantDoublePosPrecision() ? mousePixelTargetPoint2xP : mousePixelTargetPoint);
}

canvasCenterPoint = XY{
iclamp(-texW * scale + 4, canvasCenterPoint.x, g_windowW - 4),
iclamp(-texH * scale +4, canvasCenterPoint.y, g_windowH - 4)
iclamp(-texH * scale + 4, canvasCenterPoint.y, g_windowH - 4)
};

//fuck it we ball
layerPicker->position.x = g_windowW - 260;

g_gamepad->SetLightbar((pickedColor >> 16) & 0xff, (pickedColor >> 8) & 0xff, pickedColor & 0xff);

if (closeNextTick) {
g_closeScreen(this);
}
Expand Down

0 comments on commit eb6114f

Please sign in to comment.