SDL Hook for Usarsim on Linux

Introduction

We could not find any way of accessing image data from Usarsim on Linux.  So we did it around a rate of 10 fps on a Celeron-M 1.5GHz, with on board graphics chip and few megabayts of graphics memory.

You can try it if you wish or implement it yourself. Any comments/questions are welcome at eposta@cankavaklioglu.name.tr / Can Kavaklıoğlu

Problem

On m$, usarsim uses a hook to the DirectX buffer. UT2004 runs with OpenGL on Linux, thus the provided hook does not work on Linux.

Background

First approach, which failed to succeed was to do the LD_PRELOAD trick as described in [1]. [2] uses the same trick. But for some unknown reason this did not work with UT2004 and similar failure reports were found on the web. The guess for the reason is the use of SDL in UT2004.

The guess was investigated further, forming the second (working!) approach. Employing another adorable marvel of the GNU General Public License , SDL [3] source code was obtained.

Here is the proof: raw output, converted jpg.

Instructions

First make sure you take a copy of any file you replace. You may wish to revert to your previous state. This code is highly experimental and provided "as-is" without any warranty, use it at your own risk.

Currently there are three pieces of code.

First is the SDL_video-image_client.c. The original file located in src/video folder of the SDL-1.2-12 source code. It is modified to transmit the Usarsim image buffer to ip 127.0.0.1 port 6664.

Second is the SDL_video-write_to_file.c. The same original file is modified to save the Usarsim image buffer to individual files located in the folder called frames, you should create this folder within the System folder of the ut2004, which is the location of the libSDL-1.2.so.0.

Third is a sample image server, which listens for the modified libSDL to contact it. Upon connection the server starts to save the images to a file. Similar to above, this server saves the image files in the folder called frames just beside its binary, wherever you create the binary. You should also create this folder as well.

To apply these modifications you can either copy the shared library file presented below to replace the libSDL-1.2.so.0 in the System folder of the ut2004. Or you can use the modified SDL source code files to compile your own libSDL.

Usage

The main contribution of this technical work is to present a hook mechanism to obtain Usarsim image data in the Linux environment.

Other parts of the code are quite custom and may not suit everyone. However all the material is quite straight forward and easy to customize according to your specific needs.

Opengl buffer is held upside down, so we need to flip it to have the right orientation, you can find the code in above files, due to graps code[1].

Stats

Resolution (pix)
Usarsim Side (fps)
Server Side (fps)
Flip Code
800 x 600
8
7
On
800 x 600
11
8
Off
640 x 480
17
13
Off
640 x 480 14
12
On
512 x 384
22
17
On
512 x 384 28
20
Off
320 x 240
70
50
Off
320 x 240 58
40
On

Test bed: Celeron-M 1.5GHz, with on board graphics chip and few megabayts of graphics memory.

Note: File saving was turned off in all tests.

Result: Lowest resolution seems to give the best performance.

Legend
Resolution: resolution set within the ut2004.
Usarsim side: the frames processed per second by the modified SDL library.
Server side: the frames processed per second by the custom image server used to receive the images send by Usarsim.

Download

Again due to GPL, we can provide the code:

binary
source code
Explanation


*Requires FreeImage, used version 3.10,  (http://freeimage.sourceforge.net)*
libSDL-1.2.so.0-image_client
SDL_video-image_client.c
Writes the raw image data on ip 127.0.0.1 port 6664. First sends image_width, then image_height, then image_data_size one by one. Then sends each frame grabbed from the OpenGL buffer on the same ip/port.
libSDL-1.2.so.0-write_to_file
SDL_video_write_to_file.c
Writes the raw image data on files into the folder named frames.
sampleImageServer
sampleImageServer.c Listens for incoming connections from the Usarsim patched with the SDL library presented above and saves the files into the folder named frames.


The presented code uses pieces from [1] and [2] (GPL) to modify the SDL source code (LGPL) [3], original forms are available at their respective locations.

Todo

References

Many thanks to the main sources of this work:

[1] http://blog.smr.co.in/graphics/graps-capture-opengl-frames/
[2] http://wiki.vislab.usyd.edu.au/moinwiki/VideoCreationGuide
[3] http://www.libsdl.org
[4] http://sourceforge.net/projects/usarsim

Server/client socket tutorial:
http://sage.mc.yu.edu/kbeen/teaching/networking/resources/sockets.html
BOUNRobotics package - thanks to Ergin Özkucur for cruical network code help