Changeset 33039 in project


Ignore:
Timestamp:
12/29/15 20:31:14 (4 years ago)
Author:
svnwiki
Message:

Anonymous wiki edit for IP [84.210.11.111]: move docs to github repo. sdl2 now available!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/sdl2-android

    r31715 r33039  
    1 This is a very quick-and-dirty guide to get SDL2 up and running with CHICKEN. However, there is unfortunately currently no SDL2 egg for CHICKEN (there is a SDL1 egg but that doesn't help us here - we want SDL2 since it has official Android support and it has a nice touch API).
    21
    3 To debug you can use {{adb logcat}}, but note that stdout is ignored by Android! This means CHICKEN-errors may get dumped on the floor.
     2CHICKEN runs well under Android, though quite tricky to build and package. Many of the graphs-eggs work on Android too:
    43
    5 Hopefully, the following script-guide may be helpful. Also, if things go wrong, perhaps [[https://gist.github.com/kristianlm/8229999|this]] simpler guide might help.
     4- sdl2
     5- opengl-glew
     6- gl-utils
    67
    7 Let's start off with the official SDL2 android template. We've only tested against SDL 2.0.0.
    8 
    9 <enscript highlight="sh">
    10 # put the official SDL2 tar under ~/opt/SDL2-2.0.0 or something
    11 $ cd ~/projects
    12 $ cp -a ~/opt/SDL2-2.0.0/android-project/ sdl2chick
    13 $ cd sdl2chick/
    14 $ cd jni/
    15 $ ln -s ~/opt/SDL2-2.0.0/ SDL
    16 $ cd ..
    17 $ ndk-build
    18 # it should error with missing YourSourceHere.c. that's good!
    19 $ android update project -p . -t android-14
    20 </enscript>
    21 
    22 This is just following SDL2 android instructions. Let's change the package name which must be done before building our cross-chicken:
    23 
    24 <enscript highlight="diff">
    25         Modified   AndroidManifest.xml
    26 diff --git a/AndroidManifest.xml b/AndroidManifest.xml
    27 index 27db418..20c80a1 100644
    28 --- a/AndroidManifest.xml
    29 +++ b/AndroidManifest.xml
    30 @@ -5,3 +5,3 @@
    31  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    32 -      package="org.libsdl.app"
    33 +      package="com.adellica.sdl2-testapp"
    34        android:versionCode="1"
    35 @@ -23,3 +23,3 @@
    36                   android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    37 -        <activity android:name="SDLActivity"
    38 +        <activity android:name="org.libsdl.app.SDLActivity"
    39                    android:label="@string/app_name">
    40 </enscript>
    41 
    42 Let's allow us to use the internet so we can use {{nrepl}} later:
    43 
    44 <enscript highlight="xml">
    45     <uses-permission android:name="android.permission.INTERNET" />
    46 </enscript>
    47 
    48 Next let's attach a cross-chicken to our android project:
    49 
    50 <enscript highlight="sh">
    51 $ cd jni/
    52 $ git clone https://github.com/chicken-mobile/android-chicken.git -b chicken.mk chicken
    53 $ cd chicken
    54 $ make # wait forever (clones chicken-core, bootstraps and cross-compiles it).
    55 </enscript>
    56 
    57 this should build a cross-chicken for you under {{./jni/chicken/target}} and {{host}}. note that you probably have to {{rm ./jni/chicken/target ./jni/chicken/host}} whenever your app package name is changed.
    58 
    59 This build gives a cross-chicken which we'll be needing:
    60 
    61 <enscript highlight="sh">
    62 $ PATH=$PWD/jni/chicken/host/com.adellica.sdl2-testapp/bin/:$PATH
    63 </enscript>
    64 
    65 Next, let's do a bunch of stuff:
    66 
    67 * Supply a {{System.loadLibrary("chicken");}}
    68 * add a {{chicken}} dependency to {{Android.mk}}
    69 * add a simple Makefile
    70 * add YourSourceHere.scm
    71 
    72 <enscript highlight="diff">
    73         New        Makefile
    74 diff --git a/Makefile b/Makefile
    75 new file mode 100644
    76 index 0000000..e773d7b
    77 --- /dev/null
    78 +++ b/Makefile
    79 @@ -0,0 +1,7 @@
    80 +
    81 +all:
    82 +       android-csc -t jni/src/YourSourceHere.scm # generates YourSourceHere.c as requested by ndk-build
    83 +       ndk-build
    84 +       make -C jni/chicken libs # copies eggs/units with lib prefix (sigh ...), run this before ndk-build
    85 +       ant clean debug
    86 +       adb install -r bin/SDLActivity-debug.apk
    87         Modified   jni/src/Android.mk
    88 diff --git a/jni/src/Android.mk b/jni/src/Android.mk
    89 index 70ca7dc..1e40e31 100644
    90 --- a/jni/src/Android.mk
    91 +++ b/jni/src/Android.mk
    92 @@ -14,3 +14,3 @@ LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
    93  
    94 -LOCAL_SHARED_LIBRARIES := SDL2
    95 +LOCAL_SHARED_LIBRARIES := SDL2 chicken
    96  
    97         New        jni/src/YourSourceHere.scm
    98 diff --git a/jni/src/YourSourceHere.scm b/jni/src/YourSourceHere.scm
    99 new file mode 100644
    100 index 0000000..7e4c845
    101 --- /dev/null
    102 +++ b/jni/src/YourSourceHere.scm
    103 @@ -0,0 +1,8 @@
    104 +(foreign-declare "#include <SDL.h>")
    105 +
    106 +(with-output-to-file "/sdcard/sdl2-log"
    107 +        (lambda () (print "hello world!")))
    108 +
    109 +(return-to-host)
    110         Modified   res/values/strings.xml
    111 diff --git a/res/values/strings.xml b/res/values/strings.xml
    112 index 9bce51c..dd478c7 100644
    113 --- a/res/values/strings.xml
    114 +++ b/res/values/strings.xml
    115 @@ -2,3 +2,3 @@
    116  <resources>
    117 -    <string name="app_name">SDL App</string>
    118 +    <string name="app_name">Sokotar</string>
    119  </resources>
    120         Modified   src/org/libsdl/app/SDLActivity.java
    121 diff --git a/src/org/libsdl/app/SDLActivity.java b/src/org/libsdl/app/SDLActivity.java
    122 index ed6f4ef..42e7ea0 100644
    123 --- a/src/org/libsdl/app/SDLActivity.java
    124 +++ b/src/org/libsdl/app/SDLActivity.java
    125 @@ -55,2 +55,3 @@ public class SDLActivity extends Activity {
    126          System.loadLibrary("SDL2");
    127 +        System.loadLibrary("chicken");
    128          //System.loadLibrary("SDL2_image");
    129 
    130 </enscript>
    131 
    132 
    133 note that we've got a {{chicken-csc}} in there which has access to this apps' own egg-repository which we'll be using soon. all this will hopefully give you a running android app:
    134 
    135 <enscript highlight="sh">
    136 $ make
    137 # wait and start app from phone when done
    138 # it should exit
    139 $ adb shell cat /sdcard/sdl2-log
    140 hello-world!
    141 </enscript>
    142 
    143 it's not all that useful, but it has chicken and SDL2 in it so let's try and expose some of this! and all errors get dumped on the group because adb logcat doesn't show stderr output. to solve this, we add a couple tricks to YourSourceHere.scm:
    144 
    145 <enscript highlight="scheme">
    146 (let ((o (let ((p (open-output-file "/sdcard/log")))
    147            (make-output-port (lambda (str) (display str p)
    148                                 (flush-output p))
    149                              (lambda () (close-output-port p))))))
    150   (current-error-port o)
    151   (current-output-port o))
    152 </enscript>
    153 
    154 this should make any errors visible with {{adb shell cat /sdcard/log}}. the next step is to be able to load egg extensions. building these is surprisingly easy! all we need to to add {{android-chicken-install}} binary to our path and use it as a normal {{chicken-install}}:
    155 
    156 <enscript highlight="sh">
    157 $ android-chicken-install matchable nrepl
    158 </enscript>
    159 
    160 now re-run {{make}} and check that {{libnrepl.so}} is somewhere under {{./libs}}. {{nrepl.so}} has been prefixed with {{lib}} because otherwise the android-build-process won't include it in the final apk file. to make it available at runtime, we need to hack chicken. so we hack chicken to make it include the prefix:
    161 
    162 <enscript highlight="scheme">
    163 (include "./jni/chicken/find-extension.scm")
    164 </enscript>
    165 
    166 now, we should be able to use eggs like normal. your {{YourSourceHere.scm}} could now look like this:
    167 
    168 <enscript highlight="scheme">
    169 
    170 (foreign-declare "#include <SDL.h>") ;; so that main() becomes SDL_main
    171 
    172 (use ports) ;; for make-output-port and friends
    173 (let ((o (let ((p (open-output-file "/sdcard/log")))
    174            (make-output-port (lambda (str) (display str p)
    175                                 (flush-output p))
    176                              (lambda () (close-output-port p))))))
    177   (current-error-port o)
    178   (current-output-port o))
    179 
    180 ;; make (use sdl2) work properly (.so lib prefix path hack)
    181 (include "./jni/chicken/find-extension.scm")
    182 
    183 (use nrepl)
    184 (nrepl 1234)
    185 </enscript>
    186 
    187 now build and run. you should see a black screen that "hangs". if it exits, try {{adb shell cat /sdcard/log}} and see what it says. if we're lucky, your nrepl is listening to connections:
    188 
    189 <enscript highlight="sh">
    190 $ make
    191 $ adb forward tcp:1234 tcp:1234
    192 $ nc localhost 1234
    193 #;> (use posix)
    194 #;> (with-input-from-pipe "ls -ld /data" read-string)
    195 "drwxrwx--x system   system            2014-10-24 16:00 data\n"
    196 </enscript>
    197 
    198 I hope you've had fun so far because there's still a lot of paint to overcome. We now need SDL2 bindings to Chicken. These have been started, but not completed. These are a little tricky to build because they have the SDL2 third-party dependency which is inside the current app. The location of SDL2 can be supplied with {{CSC_OPTIONS}}.
    199 
    200 <enscript highlight="sh">
    201 $ SDLPRJ=$PWD # where your AndroidManifest.xml is
    202 $ cd ~/projects
    203 $ git clone https://github.com/Adellica/chicken-sdl2.git
    204 $ cd chicken-sdl2
    205 $ CSC_OPTIONS="-I$SDLPRJ/jni/SDL/include/ -L$SDLPRJ/libs/armeabi -lSDL2" android-chicken-install
    206 </enscript>
    207 
    208 now you can add this snippet to your {{YourSourceHere.scm}} file just before you start your nrepl:
    209 
    210 <enscript highlight="scheme">
    211 (use sdl2)
    212 (define w (sdl-create-window "hello world" 20 30 400 300 0))
    213 (define r (sdl-create-renderer w -1 (+ SDL_RENDERER_ACCELERATED)))
    214 
    215 (begin
    216   (sdl-set-render-draw-color r 0 0 0 0)
    217   (sdl-render-clear r)
    218   (sdl-set-render-draw-color r 255 0 255 255)
    219   (sdl-render-fill-rect r (make-sdl-rect 100 200 120 400))
    220   (sdl-render-present r))
    221 </enscript>
    222 
    223 if all goes well, you can run this and see a purple rectangle on your screen:
    224 
    225 <enscript highlight="scheme">
    226 $ cd $SDLPRJ
    227 $ make # start app after successful deploy
    228 </enscript>
     8See https://github.com/chicken-mobile/chicken-android-template on how you might go about creating an Android app with CHICKEN.
Note: See TracChangeset for help on using the changeset viewer.