Gamepad / Controller Support - OSX / PC / Mobile

Gamepad / Controller Support - OSX / PC / Mobile
0.0 0


My pleasure :slight_smile:


@siddharthshekar Glad you got the controller support working =]
I plan on adding more improvements to it in the future (such as plug/unplug support and more controllers support).

@zhangxm @siddharthshekar Also, I recently submitted a pull request to v3, but it was really messy and contained a lot of other fixes in it. I was trying to get the pull request out there as quickly as possible. I’ll go back when I have time, clean it up, and resubmit. If anybody is in a hurry for it, you can find the latest in my v3-sheado and v3-sheado-merge branches.


@Sheado So the other thing I wanted to ask was that apart from an async class and desktop-controller was anyother class added or modified to make it work? BUt I really cant thank enough for this dude. I will give credit to you for this in the game I am developing, if it ever sees that light of day that is :smiley:. Designers and Artists are such a rare breed. Programmers like me a plenty. :smiley:

@zhangxm I know I am being persistant but if it is not too much of trouble and if only a couple of classes were modified could it be included in the next release of Cocos2d-x and @Sheado could take the onus to update the files in future releases.

I am am a bit of noob on github so I really dont know how pull and merge and the kind of trouble @zhangxm and cocos2d-x if something gets messed up in the process. BUt this certainly would be a sweet feature to have in any version as it allow the developer to monetize from the steam portal. Moreover it is like everyone uses gamepad these days to play pc/mac games. The windows of mac version of cocos2d-x is really not complete without it as hardly anyone plays with kb and mouse. :smiley:


If you guys will implement it and it will not break other things. Then i think i will be glad to merge it.


@zhangxm I have tested it on the mac for now. But it works absolutely fine. Except that since I use a wireless 360 controller I was just checking if the battery runs out and if I replace the battery in the controller will the game detect that. BUt rest works like a dream.

The reason I asked @Sheado the classes that got changed to implement this feature as I am sure as I couldnt download his repo from github to work (due to my lack of experience with github) so I took an existing project and modifies the cocos2d folder to get it working. This we can see what else might get affected because of the changes made to the cocos2d folder. But I changed 4 files that I thought we key to getting the controller working so I was just R&Ding so in reality it could be that only 2 class got changed. So will for seado’s revert.

@zhangxm but thanks that would really be awesome if it gets imeplemented so that it would encourage more people to think beyon mobile and other platforms for monetization. :smiley:


@siddharthshekar I was following the architecture already in place by cocos2d-x - which required me to add an extra class.

When I have time I’ll submit a cleaner pull request. The last one I submitted had too many other fixes in it.


I’m working on a game for PC/Mac (and possibly consoles). Because there wasn’t any gamepad support, I added SDL2 to my project. I couldn’t be happier! SDL’s controller subsystems are the best I’ve seen. (They have support for haptic feedback too!)


I thought about going with SDL2 since it does seem better, but I figured since GLFW is already being used that I should stick to that instead rather than introduce another dependency. So far their API is pretty nice and hopefully I’ll get a proper pull request for them eventually.


Is there a place I read up about implementing controller support. this looks quite interesting. How easy/ hard would you think the implimentation is would you say?

@Sheado Also I guess the 2 changes were the addition of the anyc class and Desktop controller. Did you have to modify any other classes?? Just for future reference.


@siddharthshekar Controller support is relatively straightforward to add. You just need to make sure your code is efficient since it gets called a lot. Search for “controller” here to see all my controller related commits:
Here’s a link to API docs you’d need:


@Sheado Thanks will look into it. :smiley:


I tried building in ipad and I get this error; glfw3.h file not found!!! :frowning:

SOLVED:: Added CCGLViewImpl-desktop.h and cpp to the ios target by mistake causing errors in ios. Removed it to wrk fine. :smiley:


I tried adding SDL2 to my project, and although it was very easy and straightforward it doesn’t worked for me. I keep getting SDL_NumJoysticks() == 0 and receive no events (including joystick connecting and disconnecting). Could you explain how you managed to make it work?


@Sheado @siddharthshekar

any update on these guys, any advancements ?
it would be really great to support controllers.

As most of the smartphones today supports OTG and game controller support.
please keep this thread updated. :smile:

thanks for support.


Why not just using Gainput?

It already supports Android NDK, iOS, Linux, Mac OS X, and Windows.

@pabitrapadhy Have you considered trying it?


@GGalemb - I went with a GLFW solution since cocos2d-x is already using that. You can see the code for that in my branch here:

@pabitrapadhy - I’ve been told that cocos2d-x is prioritizing mobile operating systems. The feature request for Windows/Mac/Linux is unassigned:
If you need a (mostly) working implementation you can use or branch my v3.4 branch at: (note: there are bugs and custom changes that I’ve made)

@iQD - main reason I went with GLFW is because cocos2d-x is already using it for its desktop implementation. It works well so far, but lacks a few features (such as rumble)


I see, and you have a point in not introducing another dependency.
The advantage of Gainput is, that it does not have any dependencies. It’s not even using stl or C++11.
Additionally to that, it comes with great features like gestures, remapping, key replaying, remote syncing…
Oh, and it rumbles :scream:

Nevertheless SDL2 has the best gamepad implementation in terms of supported controllers, but as both libraries are supporting RAW input, every controller should work fine.


@iQD - cool. Thanks for bringing it to my attention… I’ll definitely consider Gainput in a future project.


@Sheado Thanks for your solution for desktop controllers! I hope it gets integrated in a future release, it’s a shame that Cocos2d-x doesn’t support controllers natively.

I managed to make my controller work with SDL2. It’s a bit tricky, however, and it seems a lot of people have trouble with it. The catch is: SDL redefines main(int, char*[]) as SDL_main, and apparently it expects the SDL initialization to happen there. You just have to edit your main.cpp and create a new main() function that will initialize SDL and run your Cocos app:

int main(int argc, char *argv[])
    if (SDL_Init(SDL_INIT_JOYSTICK) != 0)
        printf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());

    AppDelegate app;
    return Application::getInstance()->run();

And call it from _tWinMain:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    int status;


    status = SDL_main(NULL, NULL);


    return 0;

I’m using the win32 solution in Visual Studio 2013. In VS2015 SDL gives lots of warnings and some linker errors, but it seems you only have to recompile the SDL lib. Using the winrt solution is a wholly different thing.


@GGalemb No problem =] Thanks for sharing your solution too!