[FE training-materials-updates] sysdev appdev: new application, a fantastic adventure game
Alexandre Belloni
alexandre.belloni at free-electrons.com
Wed Jul 30 17:52:14 CEST 2014
Repository : git://git.free-electrons.com/training-materials.git
On branch : master
Link : http://git.free-electrons.com/training-materials/commit/?id=6bf97dbcf7e8461d9026672a9d0c1111fd01f6dd
>---------------------------------------------------------------
commit 6bf97dbcf7e8461d9026672a9d0c1111fd01f6dd
Author: Alexandre Belloni <alexandre.belloni at free-electrons.com>
Date: Wed Jul 30 17:50:56 2014 +0200
sysdev appdev: new application, a fantastic adventure game
Signed-off-by: Alexandre Belloni <alexandre.belloni at free-electrons.com>
>---------------------------------------------------------------
6bf97dbcf7e8461d9026672a9d0c1111fd01f6dd
lab-data/sysdev/appdev/app.c | 91 ++++++++++++++++++++
lab-data/sysdev/appdev/data/app.c | 35 --------
lab-data/sysdev/appdev/data/background.png | Bin 7512 -> 0 bytes
lab-data/sysdev/appdev/qemu-ifup | 2 -
lab-data/sysdev/appdev/run_qemu | 2 -
.../sysdev-application-development.tex | 59 ++++++-------
6 files changed, 119 insertions(+), 70 deletions(-)
diff --git a/lab-data/sysdev/appdev/app.c b/lab-data/sysdev/appdev/app.c
new file mode 100644
index 0000000..31e98ee
--- /dev/null
+++ b/lab-data/sysdev/appdev/app.c
@@ -0,0 +1,91 @@
+#include <curses.h>
+#include <stdlib.h>
+#include <time.h>
+
+int main(void)
+{
+ WINDOW * mainwin;
+ int ch, x, y, mx, my, tx, ty;
+ int won = 0;
+
+ srand(time(NULL));
+
+ /* Initialize ncurses */
+ mainwin = initscr();
+ if (mainwin == NULL) {
+ fprintf(stderr, "Error initializing ncurses.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ noecho(); /* Turn off key echoing */
+ keypad(mainwin, TRUE); /* Enable the keypad for non-char keys */
+ curs_set(0); /* Turn off cursor */
+
+ getmaxyx(mainwin, mx, my);
+ mx--;
+ my--;
+
+ x = mx / 2;
+ y = my / 2;
+ tx = rand() % mx; /* Never do that, distribution is not uniform */
+ ty = rand() % my; /* Never do that, distribution is not uniform */
+
+ mvaddstr(0, my / 2 - 6, "Hello World!");
+ mvaddstr(mx, 0, "Move to the target (X), 'q' to quit");
+ mvprintw(x, y, "O");
+ mvprintw(tx, ty, "X");
+ move(mx, my); /* In case the cursor can't be turned off */
+ refresh();
+
+ /* Loop until user presses 'q' */
+ while ((ch = getch()) != 'q') {
+ if (won)
+ continue;
+
+ mvaddch(x, y, ' ');
+ switch(ch) {
+ case KEY_DOWN:
+ ch = 'v';
+ x++;
+ break;
+ case KEY_LEFT:
+ ch = '<';
+ y--;
+ break;
+ case KEY_RIGHT:
+ ch = '>';
+ y++;
+ break;
+ case KEY_UP:
+ ch = '^';
+ x--;
+ break;
+ }
+
+ if (x < 1)
+ x = 1;
+ if (x > mx - 1)
+ x = mx - 1;
+ if (y < 0)
+ y = 0;
+ if (y > my)
+ y = my;
+ mvaddch(x, y, ch);
+
+ if ((x == tx) && (y == ty))
+ {
+ mvaddch(x, y, ' ');
+ mvprintw(mx / 2, my / 2 - 4, "You won!");
+ won = 1;
+ }
+ move(mx, my); /* In case the cursor can't be turned off */
+ refresh();
+ }
+
+ /* Clean */
+ delwin(mainwin);
+ endwin();
+ refresh();
+
+ return EXIT_SUCCESS;
+}
diff --git a/lab-data/sysdev/appdev/data/app.c b/lab-data/sysdev/appdev/data/app.c
deleted file mode 100644
index 937513d..0000000
--- a/lab-data/sysdev/appdev/data/app.c
+++ /dev/null
@@ -1,35 +0,0 @@
-#include <directfb.h>
-#include <unistd.h>
-#include <string.h>
-
-IDirectFB *dfb;
-IDirectFBSurface *primary;
-IDirectFBImageProvider *provider;
-
-DFBSurfaceDescription dsc;
-
-int main(int argc, char *argv[])
-{
- DirectFBInit(&argc, &argv);
- DirectFBCreate(&dfb);
-
- dfb->SetCooperativeLevel(dfb, DFSCL_FULLSCREEN);
-
- /* Primary */
- memset(&dsc, 0, sizeof(DFBSurfaceDescription));
- dsc.flags = DSDESC_CAPS;
- dsc.caps = DSCAPS_PRIMARY;
- dfb->CreateSurface(dfb, &dsc, &primary);
-
- /* Background */
- dfb->CreateImageProvider(dfb, "background.png", &provider);
- provider->RenderTo(provider, primary, NULL);
- provider->Release(provider);
-
- sleep(5);
-
- primary->Release(primary);
- dfb->Release(dfb);
- return 0;
-}
-
diff --git a/lab-data/sysdev/appdev/data/background.png b/lab-data/sysdev/appdev/data/background.png
deleted file mode 100644
index 014a796..0000000
Binary files a/lab-data/sysdev/appdev/data/background.png and /dev/null differ
diff --git a/lab-data/sysdev/appdev/qemu-ifup b/lab-data/sysdev/appdev/qemu-ifup
deleted file mode 100755
index 77b20d8..0000000
--- a/lab-data/sysdev/appdev/qemu-ifup
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-ifconfig $1 172.20.0.1
diff --git a/lab-data/sysdev/appdev/run_qemu b/lab-data/sysdev/appdev/run_qemu
deleted file mode 100755
index aa044b9..0000000
--- a/lab-data/sysdev/appdev/run_qemu
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-sudo qemu-system-arm -m 32 -M versatilepb -kernel ../buildroot/data/zImage -append "root=/dev/nfs ip=172.20.0.2 nfsroot=172.20.0.1:$HOME/felabs-sysdev/appdev/qemu-rootfs/" -net nic -net tap,script=./qemu-ifup
diff --git a/labs/sysdev-application-development/sysdev-application-development.tex b/labs/sysdev-application-development/sysdev-application-development.tex
index 16b4bba..a605047 100644
--- a/labs/sysdev-application-development/sysdev-application-development.tex
+++ b/labs/sysdev-application-development/sysdev-application-development.tex
@@ -1,10 +1,8 @@
\subchapter{Application development}{Objective: Compile and run your
- own DirectFB application on the target.}
+ own ncurses application on the target.}
\section{Setup}
-Select the \code{directfb} package to Buildroot and compile it.
-
Go to the \code{$HOME/felabs-sysdev/appdev} directory.
\section{Compile your own application}
@@ -12,19 +10,9 @@ Go to the \code{$HOME/felabs-sysdev/appdev} directory.
We will re-use the system built during the {\em Buildroot lab} and add
to it our own application.
-First, instead of using an \code{ext2} image, we will mount the root
-filesystem over NFS to make it easier to test our application. So,
-create a \code{qemu-rootfs/} directory, and inside this directory,
-uncompress the tarball generated by Buildroot in the previous lab (in
-the \code{output/images/} directory). Don't forget to extract the
-archive as \code{root} since the archive contains device files.
-
-Then, run the \code{run_qemu} script and check that the system works
-as expected.
-
-Now, our application. In the lab directory the file \code{data/app.c}
-contains a very simple DirectFB application that displays the
-\code{data/background.png} image for five seconds. We will compile and
+In the lab directory the file \code{app.c} contains a very simple
+ncurses application. It is a simple game where you need to reach a
+target using the arrow keys of your keyboard. We will compile and
integrate this simple application to our Linux system.
Buildroot has generated toolchain wrappers in
@@ -47,22 +35,31 @@ Let's try to compile the application:
arm-linux-gcc -o app app.c
\end{verbatim}
-It complains that it cannot find the \code{directfb.h} header. This is
-normal, since we didn't tell the compiler where to find it. So let's
-use \code{pkg-config} to query the {\em pkg-config} database about the
-location of the header files and the list of libraries needed to build
-an application against DirectFB:\footnote{Again,
-\code{output/host/usr/bin} has a special \code{pkg-config} that
-automatically knows where to look, so it already knows the right paths
-to find \code{.pc} files and their sysroot.}
+It complains about undefined references to some symbols. This is
+normal, since we didn't tell the compiler to link with the necessary
+libraries. So let's use \code{pkg-config} to query the {\em
+pkg-config} database about the location of the header files and the
+list of libraries needed to build an application against
+ncurses:\footnote{Again, \code{output/host/usr/bin} has a special
+\code{pkg-config} that automatically knows where to look, so it
+already knows the right paths to find \code{.pc} files and their
+sysroot.q}
-\footnotesize
\begin{verbatim}
-arm-linux-gcc -o app app.c $(pkg-config --libs --cflags directfb)
+arm-linux-gcc -o app app.c $(pkg-config --libs --cflags ncurses)
\end{verbatim}
-\normalsize
-Our application is now compiled! Copy the generated binary and the
-\code{background.png} image to the NFS root filesystem (in the
-\code{root/} directory for example), start your system, and run your
-application!
+You can see that \code{ncurses} doesn't need anything in particular for the
+\code{CFLAGS} but you can have a look at what is needed for
+\code{libvorbis} to get a feel of what it can look like:
+
+\begin{verbatim}
+pkg-config --libs --cflags vorbis
+\end{verbatim}
+
+Our application is now compiled! Copy the generated binary to the NFS
+root filesystem (in the \code{root/} directory for example), start
+your system, and run your application!
+
+You can also try to run it over ssh if you added ssh support to your
+target, Do you notice the difference ?
More information about the training-materials-updates
mailing list