How to add vibration to any cocos2dx 3.3 Android app

I had originally avoided using vibration in my game, because I had found several different posts trying to figure out exactly what you needed to do, and I wasn’t able to find any tutorials that really broke down the process, so I chugged some coffee and dug into the code that was linked on the official forums. forum post. I can’t overstate how helpful it was to have a sub 10 second turn around time, between using Visual Studio to write the code, cocos to compile it (cocos compile -p android) and then to push it to my android phone with adb (adb install -r debug.apk), and then monitoring what was happening with logcat (adb logcat *:w), filtering only for warnings, errors and anything higher). Before we jump into it though, here’s a rough summary:

  1. You need to add a header in your Classes/ folder where you just have the vibrate function declared but not defined.
  2. Add a vibration function definition in the proj.android/jni, in a .cpp file. I used proj.android/jni/hellocpp/main.cpp , which I believe is the default for a starter cocos2dxv3 project. In this method you call the method you’ve defined in your java, in this case it was defined as a static method on your Activity. The default was named AppActivity .
  3. make sure you’ve got the vibration permission on your proj.android/AndroidManifest.xml , <uses-permission android:name= android.permission.VIBRATE>”

So, in one of your standard C++ headers in the Classes/ folder, add

    //Classes/any_header.h
    void vibrate(int milliseconds);

and in the associated <tt>.cpp</tt> file, call it: 

    //Classes/any_header.cpp
    void get_hit()
    {
      vibrate(200);
    }

In your
proj.android/AndroidManifest.xml , you’ll want to add that line

    <manifest>
      ...
      <uses-permission android:name="android.permission.VIBRATE"/>
    </manifest>

In a cpp file the jni will deal with, say proj.android/jni/hellocpp/main.cpp , you’ll need to define the connection between the Java and C++ by calling the Java method:

    //proj.android/jni/hellocpp/main.cpp
    /* this function should already exist though */
    void cocos_android_app_init (JNIEnv* env, jobject thiz) {
        LOGD("cocos_android_app_init");
        AppDelegate *pAppDelegate = new AppDelegate();
    }

    /* this is the new one you're adding, where org/cocos2dx/cpp/AppActivity
       is the java file where your Activity with the vibrate static method is defined
    */
    void vibrate(int milliseconds)
    {
        JniMethodInfo minfo;
        CCAssert(JniHelper::getStaticMethodInfo(minfo, "org/cocos2dx/cpp/AppActivity", "vibrate", "(I)V"), "Function doesn't exist");
        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, (jint)milliseconds);
        minfo.env->DeleteLocalRef(minfo.classID);
    }

Finally, here’s the .java file where the android call to vibrate is made. Mine was proj.android/src/org/cocos2dx/cpp/AppActivity.java

    /* default imports */
    package org.cocos2dx.cpp;

    import org.cocos2dx.lib.Cocos2dxActivity;

    /* imports needed for vibration stuff */
    import android.os.Vibrator;
    import android.content.Context;
    import android.app.ActivityManager;
    import android.util.Log;
    import android.os.Bundle;

    public class AppActivity extends Cocos2dxActivity {

        private static volatile Cocos2dxActivity mainActivity;
        public static void setMainActivity(Cocos2dxActivity activity)
        {
            mainActivity = activity;
        }

        public static Cocos2dxActivity getMainActivity()
        {
            if(mainActivity == null)
            {
                Log.w("MY_APP_NAME_GOES_HERE", "Warning : null main Activity");
            }
            return mainActivity;
        }

    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
        	AppActivity.setMainActivity(this);
    	}

        public static void vibrate(int milliseconds)
        {
            Vibrator v = (Vibrator) getMainActivity().getSystemService(Context.VIBRATOR_SERVICE);
            if (getMainActivity().getSystemService(Context.VIBRATOR_SERVICE) != null)
            {
                v.vibrate(milliseconds);
            }
        }
    }

Hope this helped you!

Going from nothing to an employed Python programmer in a few years

This is what I did in about 2 or 3 years of programming, from nothing to employed at a kick ass company.

tl;dr is that I just worked on things over time that I felt to lazy to do myself, or things that interested me, and got going.

When I started out the only library I think I was using was requests and beautifulsoup for making a bunch of web requests. Scraping reddit and downloading wallpapers automatically. I started off wanting to learn how to make a game, so I made a little roguelike in libtcod, which really taught me a bunch of stuff about Python, since I was motivated, and the tutorial on RogueBasin is fantastic.

After a little while of making little scripts like a sleeptimer, I tried making a music player, because I was sick of WMP’s poor shuffling, and Winamp didn’t respect my media keys and since I was on XP at the time, didn’t have that sweet toolbar integration that WMP did. I then moved to working with the Imgur and reddit APIs downloading even more images, making things like detecting if a user has any posts on gonewild and other creepy stuff like that.

I wanted to get back into gamedev, so I learned PyGame to make a topdown shooter but wasn’t happy with the performance (which I now realize is probably due to the way I designed it rather than Python’s fault) but then learned C# because I wanted to make a game on the xbox using XNA.

From there I made a site like abetterqueue.com in C#s ASP.NET MVC4, learning about web development proper, like how to do anything in HTML, CSS and JS. I also made a few Chrome extensions using that JS knowledge, like one that scrapped Steam for the account history to get an accurate total of the money you’ve spent on your games, before that was integrated into Steam, an extension to count down from when you get into work and pop up 8 hours later and most recently an extension that tweaks Project Free TVs site to make it a bit more readable.

I’ve used the WordPress XMLRPC API via a library to purge spammy posts from my blog, which makes my own life a lot easier because I’m not about to go through 1000 comments a month by hand.

A few years ago I learned how to use vim because I’m super lazy and want to speed up doing everything I do, and I love the idea of getting better at navigating text as I get better at programming.

Last summer I decided I want to do a proper game, to learn C++, a language I always thought was one of those things that a real could do, sorta like how you can’t call your self a car enthusiast without being able to drive stick. So far it’s been a blast, and I’ve got a kick ass site dedicated to the game. It’s actually in the same library that I used to learn python, which is fun.

This all sounds like a lot, now that I’ve laid it out, but its a pretty standard experience for a lot of programmers to just tinker with things they want to play around with, or to automate some things they do every day. I don’t think there’s a fast track to getting employed other than simply working at it day by day.

How to delete spam comments on your WordPress blog, with Python via the Wordress API

In this tutorial we’ll be covering how to delete all those spam comments you’re getting posted to your
blog using the wordpress api via it’s fantastic Python wrapper, python-wordpress-xmlrpc, and make sure you’ve enabled the XMLRPC API on your blog.

How this script works is that it pulls all the comments that are in the pending
queue and then if it contains any words from the list of words that are likely spam words,
like ‘ambien’ or ‘oakley’, then take care of by deleting or marking them as spam. Here’s the full PasteBin of the script, and as usual, it’s also included in full at the end of this post.
Nothing too tricky, let’s get started!

Continue reading How to delete spam comments on your WordPress blog, with Python via the Wordress API

How to compile Libtcod and C++, mirrored from Ysgard.net

Source: http://www.ysgard.net/2011/06/visual-studio-2010-c-express-and-libtcod/

So I was playing with the Doryen Library, aka libtcod, the other day.  What a fantastic little library.  I had just finished a small demo for displaying how influence maps works (more for my own education, as I’m a pretty inexperienced programmer and doing is how I learn) and was pretty pleased with the result.   I’m using Visual Studio Express because I use Windows and happen to like it a bit more than Code::Blocks.

Okay, so I don’t need that debug window anymore.  Go into the linker properties, specify the subsystem as Windows app, compile…. and BAM!

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\Sigil\VS\InfluenceMap\Debug\InfluenceMap.exe : fatal error LNK1120: 1 unresolved externals

Hmm, okay what’s going on here?  Check the libtcod documentation… no, there’s nothing here about this.  Why is this happening?

The answer to this lies in the way the program is run.  Before, I was compiling the program as a console app.  For this, the standard C++ entry point, main, was sufficient.

int main(int argc, char** argv)

But the moment I specified the program as a Windows application, the rules changed.  To use the Windows api, I need a Windows entry point, which is defined like this:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

But the samples I’ve seen don’t define this entry point.  They define a standard main().  So what am I missing?  As it turns out, I’m missing SDL!

libtcod is an SDL library, and SDL has its own way of mapping main() to WinMain().  But in order to make this happen, you also need to link in SDL and pull in the SDL.h header.

Here’s how to set up a Visual Studio project with both libtcod and SDL.  You can then write and test your application using the console window, and when the time is right, switch to a native windows app without any issues!

Here’s the exact procedure I followed.

  1. Download and extract the libtcod library for Visual Studio to wherever you place your libraries.  In my case, I extracted it to C:\Sigil\VS\libs, which resulted in the folder C:\Sigil\VS\libs\libtcod-1.5.1.
  2. Download and extract the SDL development libraries for Visual Studio.  You want the latest one available, I used SDL-devel-1.2.14-VC8.zip.  I extracted it to C:\Sigil\VS\libs\SDL-1.2.14.
  3. Open Visual Studio Express.  Create an Empty project for your new libtcod application.
  4. Right click on your project in the Solution Explorer and select Properties.
  5. Go to Configuration Properties -> VC++ Directories.
  6. Click on Include Directories, and from the little drop-down box on the right, select <Edit…>
  7. Click on the little folder icon to add a new folder to your include directories, specify the directory for your libtcod library (for me, C:\Sigil\VS\libs\libtcod-1.5.1\include).
  8. Do the same for SDL (mine was C:\Sigil\VS\libs\SDL-1.2.14\include).
  9. Click OK.
  10. Now do the same for the Library Directories, specifying the folders for your libtcod and SDL lib directories.
  11. Now go to Configuration Properties -> Linker -> Input.  Click on Additional Dependencies, click on the drop-down box, select <Edit…>.  Add the following dependencies, one per line: libtcod-VS.lib, SDL.lib, SDLmain.lib.  Click on OK.
  12. There’s one final step left.  You need to pull in the main SDL header file.  At the top of your main file, add #include <SDL.h>

You’re now set!  Go ahead and develop your application as normal.  Whenever you’re ready (or just want to test it) you can set your linker to produce a native Windows application.  To do this, you need to go into the Properties windows again, and select Configuration Properties -> Linker -> System.  In the right-hand pane, click on SubSystem and select Windows (/SUBSYSTEM:WINDOWS) from the drop-down menu.

Build your application again, and you should now be enjoying some console-free Windows goodness!

Imgur API part 3: OAuth2

It’s been a while since I wrote the last one, but a user on reddit asked how to get OAuth working with Imgur, and I couldn’t resist giving it one more shot. Thanks to my recent experience with the Netflix API, even though it was in C# rather than in Python, I was able to wrangle a quick and easy script for getting started.

Here’s the PasteBin with the complete code, thought it’s shown at the bottom of the page, as usual. Continue reading Imgur API part 3: OAuth2

A blog about gamedev and vim