Game development

(Add wmcaption note)
(SDL specific notes: tidy)
 
(7 intermediate revisions not shown)
Line 1: Line 1:
-
{{Midgard article}}
 
-
 
This page contains tips on game development and SDL library usage for internet tablets.
This page contains tips on game development and SDL library usage for internet tablets.
-
=Useful Tips=
+
==Useful Tips==
-
==Hardware notes==
+
===Hardware notes===
* Internet tablets use a touchscreen, which enables new ways to implement game-features, such as on-screen buttons, in-game drawing, moving * character by tapping screen. etc.
* Internet tablets use a touchscreen, which enables new ways to implement game-features, such as on-screen buttons, in-game drawing, moving * character by tapping screen. etc.
Line 12: Line 10:
* When using screen's vertical orientation (as in tetris) think other use for esc button than exiting game. It's so close to the rocker that accidental presses will happen.
* When using screen's vertical orientation (as in tetris) think other use for esc button than exiting game. It's so close to the rocker that accidental presses will happen.
-
==SDL specific notes==
+
===SDL specific notes===
Things to remember:
Things to remember:
-
* The screen is natively 800x480. No resolution changes are supported. Maybe in the future we might have support for 400x240 res for faster graphics but this is quite unlikely. If trying to set fullscreen window smaller than the native screen size, you will just have black borders.
+
* The screen is natively 800x480 pixels. No resolution changes are supported. Maybe in the future we might have support for 400x240 res for faster graphics but this is quite unlikely. If trying to set a fullscreen window that is smaller than the native screen size, you will just have black borders.
-
* In windowed mode matchbox will force the window to certain size (you should look at the UI specifications to see the actual window size). If your requested window is smaller than this, your window will be aligned to the upper-left and have black borders on the lower-right.
+
* In windowed mode, matchbox will force the window to certain size (you should look at the UI specifications to see the actual window size). If your requested window is smaller than this, your window will be aligned to the upper-left and have black borders on the lower-right. Generally, SDL windows (not fullscreen) look quite bad with the theme, if nothing for the theme is done inside the SDL window, but it's up to you to decide.
-
Generally SDL windows (not fullscreen) look quite bad with the theme if nothing for the theme is done inside the SDL window, but it's up to you to decide.
+
* Avoid full 800x480 screen updates. Memory bandwidth is limited. If you still have to do these for some reason, you should make the area smaller (for example, having some statusbar or any GUI elements which doesn't need to be updated each time). Also do not use <code>SDL_Flip()</code>, since it will update the whole screen. Use rect updating functions instead. Thank you.
-
* Avoid full 800x480 screen updates. Memory bandwidth is limited. If you still have to do these for some reason, you should make the area smaller (eg having some statusbar or any GUI elements which doesn't need to be updated each time). Also do not use SDL_Flip(), since it will update whole screen. Use rect updating functions instead. Thank you.
+
* 32 bit mode doesn't work. The reason was that there was a bug that caused SDL to segfault when you had 32 bit window and you recreated the window. Since the display is only 16 bit, there is no reason to support 32 bit mode, which would just give an extra round of pixel conversion and therefore slow things down. Convert your images on loading to native 16-bit with SDL.
-
* 32 bit mode doesn't work. The reason was that there was a bug that caused SDL to segfault when you had 32 bit window and you recreated the window. Since the display is only 16 bit, there is no reason to support 32 bit mode, which would just give extra round of pixel conversion and therefore slow things down. Convert your images on loading to native 16-bit with SDL.
+
* Mouse events. There is touchscreen, and you cannot assume that an average end user would hack the device to give USB host mode and use the USB mouse. So, use absolute coordinates as much as possible. One example where relative coordinates are used is scummvm. If you use the keypad to move the cursor, and then use the touchscreen, the actuall cursor will be at offset from original touchpadpress. Think absolutely, not relatively ;)
-
* Mouse events. There is touchscreen, and you can't assume average end user Billy-Bob would hack the device to USB host mode and use the USB mouse. So use absolute coordinates as much as possible. One example where relative coordinates are used is scummvm. If you use keypad to move the cursor, and then use the touchscreen, the actuall cursor will be at offset from original touchpadpress. Think absolutely, not relatively ;)
+
 +
===Adding support for the Task Navigator===
-
==Adding support for the Task Navigator==
+
====Create an entry in the task list====
-
 
+
-
===Create an entry in the task list===
+
The Maemo Task Navigator identifies non-Maemo programs (like pure SDL applications) by matching the <code>StartupWMClass</code>-variable in the <code>.desktop</code>-file and the application "window manager class" name. By default all SDL applications have the name <code>SDL_App</code>. This can be changed with the environment variable <code>SDL_VIDEO_X11_WMCLASS</code>.
The Maemo Task Navigator identifies non-Maemo programs (like pure SDL applications) by matching the <code>StartupWMClass</code>-variable in the <code>.desktop</code>-file and the application "window manager class" name. By default all SDL applications have the name <code>SDL_App</code>. This can be changed with the environment variable <code>SDL_VIDEO_X11_WMCLASS</code>.
Line 47: Line 43:
* Alternative 1: Here <code>app.bin</code> is the binary executable and <code>app</code> is a wrapper script:
* Alternative 1: Here <code>app.bin</code> is the binary executable and <code>app</code> is a wrapper script:
-
<pre>
+
<source lang="bash">
#!/bin/sh
#!/bin/sh
BASENAME=`basename $0`
BASENAME=`basename $0`
export SDL_VIDEO_X11_WMCLASS=${BASENAME}
export SDL_VIDEO_X11_WMCLASS=${BASENAME}
exec ${0}.bin "$@"  
exec ${0}.bin "$@"  
-
</pre>
+
</source>
Notice that <code>StartupWMClass == SDL_VIDEO_X11_WMCLASS</code>, and that <code>Exec</code> points to the wrapper script, not the main program.
Notice that <code>StartupWMClass == SDL_VIDEO_X11_WMCLASS</code>, and that <code>Exec</code> points to the wrapper script, not the main program.
Line 58: Line 54:
* Alternative 2: An other possibility that doesn't need a script is the following. Put the line
* Alternative 2: An other possibility that doesn't need a script is the following. Put the line
-
<pre>
+
<source lang="c">
putenv("SDL_VIDEO_X11_WMCLASS=app");
putenv("SDL_VIDEO_X11_WMCLASS=app");
-
</pre>
+
</source>
in your code. A good position is directly at the beginning of the <code>main</code> method. This way there is no need for a helper script.
in your code. A good position is directly at the beginning of the <code>main</code> method. This way there is no need for a helper script.
-
===Set the window title===
+
====Set the window title====
-
You can set the title shown in the task list by using:
+
If you see "unknown" title in task list (OS2008 bug/feature), you can set it by using:
-
<pre>
+
<source lang="c">
#include <SDL/SDL_syswm.h>
#include <SDL/SDL_syswm.h>
#include <X11/Xutil.h>
#include <X11/Xutil.h>
Line 84: Line 80:
   if (dpy && win) XStoreName(dpy, win, "Title");
   if (dpy && win) XStoreName(dpy, win, "Title");
}
}
-
</pre>
+
</source>
-
If SDL_WM_SetCaption is present in the code, add the code above after that call. Or, just remove SDL_WM_SetCaption as the code above will set the window title and the icon for the window is handled by hildon-desktop using the Icon field in your desktop file.
+
Alternatively you can try to set title for both windows no matter if fullscreen or not. This seems to work and saves you setting the title again after fullscreen switch.
-
==Utilizing pixel doubling inside SDL==
+
<source lang="c">
 +
  Display *dpy = info.info.x11.display;
 +
  Window win;
 +
  if (dpy){
 +
    win = info.info.x11.fswindow;
 +
    if (win) XStoreName(dpy, win, "Title");
 +
    win = info.info.x11.wmwindow;
 +
    if (win) XStoreName(dpy, win, "Title");
 +
  }
 +
</source>
 +
 
 +
If <code>SDL_WM_SetCaption</code> is present in the code, add the code above after that call. Or, just remove <code>SDL_WM_SetCaption</code> as the code above will set the window title and the icon for the window is handled by hildon-desktop using the Icon field in your desktop file.
 +
 
 +
If you wish for text to appear in the task navigator on the second line corresponding to your game; make "Title" be: "'text to display on first line' - 'text to display on second line'". The " - " (with spaces) are important.
 +
 
 +
===Utilizing pixel doubling inside SDL===
As there is support for pixel-doubled drawing to the screen inside the HW, there is implementations to utilize that up to X. So if you want to use it you have to do some xlib code, therefore it might be a good idea to put this small piece of code to switch pixel doubling mode to a separate file.
As there is support for pixel-doubled drawing to the screen inside the HW, there is implementations to utilize that up to X. So if you want to use it you have to do some xlib code, therefore it might be a good idea to put this small piece of code to switch pixel doubling mode to a separate file.
 +
 +
'''NOTE:''' There is no Xsp scaling on [[Nokia N900|N900]] anymore. You might want to use Hildon Animation Actor to scale the pixmap. Either with [http://git.maemo.org/git/sdlhildon/?p=sdlhildon;a=tree;f=sdlhaa SDL_haa] or [[Documentation/Maemo_5_Developer_Guide/Porting_Software/Scaling_Fixed_Size_Windows|directly]].
====What is pixel doubling?====
====What is pixel doubling?====
Line 109: Line 122:
There exists one X extension (Xsp), which can be used for that. Sample piece of code with SDL:
There exists one X extension (Xsp), which can be used for that. Sample piece of code with SDL:
-
<pre>
+
<source lang="c">
#include <X11/Xlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xsp.h>
#include <X11/extensions/Xsp.h>
Line 124: Line 137:
   XSPSetPixelDoubling(wminfo.info.x11.display, 0, enable);
   XSPSetPixelDoubling(wminfo.info.x11.display, 0, enable);
}
}
-
</pre>
+
</source>
Note that when you compile it you have to include X include path and link this with libXsp (-lXsp).
Note that when you compile it you have to include X include path and link this with libXsp (-lXsp).
 +
==External Links==
-
=External Links=
+
* [http://www.libsdl.org/ Simple DirectMedia Layer Homepage]
-
 
+
* [http://www.gamedev.net/ Game Development Resources]
-
[http://www.libsdl.org/ Simple DirectMedia Layer Homepage]
+
-
 
+
-
[http://www.gamedev.net/ Game Development Resources]
+
[[Category:Development]]
[[Category:Development]]
-
[[Category:Midgard wiki]]
+
[[Category:Games]]

Latest revision as of 10:42, 11 May 2010

This page contains tips on game development and SDL library usage for internet tablets.

Contents

[edit] Useful Tips

[edit] Hardware notes

  • Internet tablets use a touchscreen, which enables new ways to implement game-features, such as on-screen buttons, in-game drawing, moving * character by tapping screen. etc.
  • Since the device does not have any buttons on right side, implement those in software!
  • Avoid using the 4 way rocker for fast and complex movements. It's not well suited for gaming.
  • When using screen's vertical orientation (as in tetris) think other use for esc button than exiting game. It's so close to the rocker that accidental presses will happen.

[edit] SDL specific notes

Things to remember:

  • The screen is natively 800x480 pixels. No resolution changes are supported. Maybe in the future we might have support for 400x240 res for faster graphics but this is quite unlikely. If trying to set a fullscreen window that is smaller than the native screen size, you will just have black borders.
  • In windowed mode, matchbox will force the window to certain size (you should look at the UI specifications to see the actual window size). If your requested window is smaller than this, your window will be aligned to the upper-left and have black borders on the lower-right. Generally, SDL windows (not fullscreen) look quite bad with the theme, if nothing for the theme is done inside the SDL window, but it's up to you to decide.
  • Avoid full 800x480 screen updates. Memory bandwidth is limited. If you still have to do these for some reason, you should make the area smaller (for example, having some statusbar or any GUI elements which doesn't need to be updated each time). Also do not use SDL_Flip(), since it will update the whole screen. Use rect updating functions instead. Thank you.
  • 32 bit mode doesn't work. The reason was that there was a bug that caused SDL to segfault when you had 32 bit window and you recreated the window. Since the display is only 16 bit, there is no reason to support 32 bit mode, which would just give an extra round of pixel conversion and therefore slow things down. Convert your images on loading to native 16-bit with SDL.
  • Mouse events. There is touchscreen, and you cannot assume that an average end user would hack the device to give USB host mode and use the USB mouse. So, use absolute coordinates as much as possible. One example where relative coordinates are used is scummvm. If you use the keypad to move the cursor, and then use the touchscreen, the actuall cursor will be at offset from original touchpadpress. Think absolutely, not relatively ;)

[edit] Adding support for the Task Navigator

[edit] Create an entry in the task list

The Maemo Task Navigator identifies non-Maemo programs (like pure SDL applications) by matching the StartupWMClass-variable in the .desktop-file and the application "window manager class" name. By default all SDL applications have the name SDL_App. This can be changed with the environment variable SDL_VIDEO_X11_WMCLASS.

Here app.desktop is the desktop file, with:

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=The Application
Exec=/var/lib/install/usr/bin/app
Icon=app
X-Window-Icon=tn-bookmarks-link
X-Osso-Type=application/x-executable
StartupWMClass=app
Terminal=false
  • Alternative 1: Here app.bin is the binary executable and app is a wrapper script:
#!/bin/sh
BASENAME=`basename $0`
export SDL_VIDEO_X11_WMCLASS=${BASENAME}
exec ${0}.bin "$@"

Notice that StartupWMClass == SDL_VIDEO_X11_WMCLASS, and that Exec points to the wrapper script, not the main program.

  • Alternative 2: An other possibility that doesn't need a script is the following. Put the line
putenv("SDL_VIDEO_X11_WMCLASS=app");

in your code. A good position is directly at the beginning of the main method. This way there is no need for a helper script.

[edit] Set the window title

If you see "unknown" title in task list (OS2008 bug/feature), you can set it by using:

#include <SDL/SDL_syswm.h>
#include <X11/Xutil.h>
 
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if ( SDL_GetWMInfo(&info) ) {
  Display *dpy = info.info.x11.display;
  Window win;
 
  if (Video.FullScreen)
    win = info.info.x11.fswindow;
  else
    win = info.info.x11.wmwindow;
  if (dpy && win) XStoreName(dpy, win, "Title");
}

Alternatively you can try to set title for both windows no matter if fullscreen or not. This seems to work and saves you setting the title again after fullscreen switch.

  Display *dpy = info.info.x11.display;
  Window win;
  if (dpy){
    win = info.info.x11.fswindow;
    if (win) XStoreName(dpy, win, "Title");
    win = info.info.x11.wmwindow;
    if (win) XStoreName(dpy, win, "Title");
  }

If SDL_WM_SetCaption is present in the code, add the code above after that call. Or, just remove SDL_WM_SetCaption as the code above will set the window title and the icon for the window is handled by hildon-desktop using the Icon field in your desktop file.

If you wish for text to appear in the task navigator on the second line corresponding to your game; make "Title" be: "'text to display on first line' - 'text to display on second line'". The " - " (with spaces) are important.

[edit] Utilizing pixel doubling inside SDL

As there is support for pixel-doubled drawing to the screen inside the HW, there is implementations to utilize that up to X. So if you want to use it you have to do some xlib code, therefore it might be a good idea to put this small piece of code to switch pixel doubling mode to a separate file.

NOTE: There is no Xsp scaling on N900 anymore. You might want to use Hildon Animation Actor to scale the pixmap. Either with SDL_haa or directly.

[edit] What is pixel doubling?

Pixel doubling is a feature of the Xsp extension of the tablet's X server. When enabled, all draws to the display are doubled in size with a smoothing filter. This reduces required bandwidth for screen updates by a factor of 4, making full-motion fullscreen animation possible. This also allows lower-quality graphics to be drawn to portions of the screen (for example a low-res game area and a high-res status bar).

[edit] Pixel doubling problems and workarounds

  • Be aware that Xsp pixel doubling feature was designed for scaling video-player videos (drawn by dsp, not X) and is not officially supported.
  • Don't update areas which would exceed doubled coordinates 400x240.
  • If possible, disable doubling when not drawing, because if another process draws to screen (like infoprints) while the Xsp doubling is active, results can be quite "artistic".
  • If mouse pointer is enabled, it will break pixel doubling. Disable it before turning on doubling with SDL_ShowCursor(0);.
  • Many SDL games that draw several small updaterects will break Xsp pixel doubling, causing flicker. This has been discussed at length on the maemo-developer list without a resolution. If pixel doubling is causing areas to flicker, change the code from individual updated rectangles for sprites to one updaterect for your entire game screen, for example region (0,0,320,240).
  • If your game crashes or exits before disabling pixel doubling, it will be left on until some application switches it off. This means that you will have 'artistic desktop' or a white screen or reboot. This can be avoided by reacting to SDL active events, if somebody else gets active status, immediately switch doubling off.

[edit] Howto use it

There exists one X extension (Xsp), which can be used for that. Sample piece of code with SDL:

#include <X11/Xlib.h>
#include <X11/extensions/Xsp.h>
 
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
 
 
void set_doubling(unsigned char enable)
{
  SDL_SysWMinfo wminfo;
  SDL_VERSION(&wminfo.version);
  SDL_GetWMInfo(&wminfo);
  XSPSetPixelDoubling(wminfo.info.x11.display, 0, enable);
}

Note that when you compile it you have to include X include path and link this with libXsp (-lXsp).

[edit] External Links