Posts Tagged ‘linux’

20100219 Breakpoint demolog, day 17: building for windows (from linux)

Windows and Linux executables, rotating a triangle and generating a sine wave audio stream

Today I decided to do something different. And I thought: Okay, I’ll try and sort out this compiling for windows issue, so that I can focus on the rest of more important things without having to worry about it any more.

And guess what…? I think I managed to do it :-)

Roughly, these are the steps I followed, just in case someone else wants to compile for windows with opengl and sdl support, using [ubuntu] linux:

  • install mingw (I did it with Synaptic Package Manager so that I didn’t have to go through the ./configure and ./make hell)
  • get the dev libraries from sdl for windows/mingw –this file– or check their downloads page
  • uncompress the file and locate the bin, include and lib folders within, and…
    • copy bin/SDL.dll to the folder where your win32 .exe will go — so that the exe can pick and load the dll when executing
    • copy the include and lib folders to a folder in your project’s src folder — or anywhere you’ve got access to! For example: project/src/libs/sdl/
  • now in your main.cpp, include SDL’s header files like this:
    #include <SDL/SDL.h>
    #include <SDL/SDL_opengl.h>
  • And the makefile could be something like this too:
    WINDOWS_SDL = ./libs/sdl/windows

    windows:
            i586-mingw32msvc-g++ -I$(WINDOWS_SDL)/include -L$(WINDOWS_SDL) \
    main.cpp $(WINDOWS_SDL)/libSDL.dll.a -o ../test.exe\
     -lmingw32 -lSDLmain -lSDL -mwindows -lopengl32 -Wl,-R.

(ugly hard line returns added by me so that you can see all parameters in one go).

This assumes you are invoking make from the src folder. The output will go to the parent folder. But well, the most important things that need to be highlighted are the bunch of switches you need to include in order to get the program to link:

-I$(WINDOWS_SDL)/include — this tells the compiler where to look for additional header files. This way it can find <SDL/SDL.h> and you do not need to modify the file or add #define’s when building for linux.

-L$(WINDOWS_SDL) — pretty much the same but tells the compiler to look for static libraries in the WINDOWS_SDL folder too

$(WINDOWS_SDL)/libSDL.dll.a — links the library into our binary!

-lmingw32 -lSDLmain -lSDL -mwindows -lopengl32 -Wl,-R. — these are magic – remove any of them and the linker will complain about missing symbols ;)

I still have to test this in a real windows machine; so far I have just tried with Wine. The output is a little jerky and a bit slower than the native (64 bit) counterpart, but I guess this will do much better than compiling in Virtual Box and testing with Wine :D

I’ll try to upload a test .exe when I test it on a real windows box myself, so that you can test it and see if it works in your computer (if you want, of course!). It’s ages since I compiled anything for win, and I truly wonder whether it will work with Vista/7… my last demo for Windows was done in 2005 and XP was in full glory back then :P

20091110 Too many open files

I am doing something that requires manipulating a lot of files, and I fell in the classical too many open files error trap.

A way of finding out which files are being used by a process is to type

ps -ax

in a terminal, then identify the guilty process and its PID. Let’s imagine its PID is 9090. Now to list every one of its open files you just run this (again, in the terminal):

lsof -p 9090

Or you can get a raw estimate by piping that through wc and getting the number of lines in the return value of lsof:

lsof -p 9090 | wc -l

That will return a number, like for example “33″.

It’s interesting to know that the output from lsof does not only show files in the windows way of referring to a file as a folder or data written in the disk, but returns files in the UNIX way, i.e., everything is a file, including pipes, sockets, files-files, etc.

I have changed my code meanwhile to be a bit more austere in regards to the number of open files, but this is an interesting tool nevertheless. If you run it for example with Firefox, you can even see which font files Firefox is using:

...
firefox 3847 sole  mem    REG                8,1   224692   272248 /usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf
firefox 3847 sole  mem    REG                8,1   622020     6209 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf
...

Or which plug-ins has it loaded:

...
firefox 3847 sole  mem    REG                8,1   101536    13774 /usr/lib/mozilla/plugins/libtotem-cone-plugin.so
firefox 3847 sole  mem    REG                8,1   117960      741 /var/lib/flashplugin-installer/npwrapper.libflashplayer.so
...

etc etc :)

20091022 dig

I normally go to webs like dnsstuff to find the IP address of a domain. But since I found this command, I think I’ll stop visiting them so often!

An example is worth one thousand words:

sole@courgette:~$ dig google.com

; <<>> DiG 9.5.1-P2 <<>> google.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15808
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             239     IN      A       74.125.53.100
google.com.             239     IN      A       74.125.45.100
google.com.             239     IN      A       74.125.67.100

;; Query time: 12 msec
;; SERVER: 208.67.222.222#53(208.67.222.222)
;; WHEN: Thu Oct 22 09:50:01 2009
;; MSG SIZE  rcvd: 76

By default it returns type A DNS records, which are the ones for translating domain names to IP addresses. But you can also ask it to retrieve other record types. For example, for retrieving every record that it can find:

sole@courgette:~$ dig -t ANY google.com

; <<>> DiG 9.5.1-P2 <<>> -t ANY google.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15253
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com.                    IN      ANY

;; ANSWER SECTION:
google.com.             175     IN      A       74.125.45.100
google.com.             175     IN      A       74.125.67.100
google.com.             175     IN      A       74.125.53.100
google.com.             807     IN      MX      10 google.com.s9a1.psmtp.com.
google.com.             807     IN      MX      10 google.com.s9a2.psmtp.com.
google.com.             807     IN      MX      100 smtp1.google.com.
google.com.             807     IN      MX      10 google.com.s9b1.psmtp.com.
google.com.             807     IN      MX      10 google.com.s9b2.psmtp.com.
google.com.             807     IN      MX      100 smtp2.google.com.
google.com.             171315  IN      NS      ns1.google.com.
google.com.             171315  IN      NS      ns2.google.com.
google.com.             171315  IN      NS      ns3.google.com.
google.com.             171315  IN      NS      ns4.google.com.

;; Query time: 14 msec
;; SERVER: 208.67.222.222#53(208.67.222.222)
;; WHEN: Thu Oct 22 09:51:05 2009
;; MSG SIZE  rcvd: 326

There it shows now the name servers (NS column) and the mail servers (MX column) too.

Go play around with its options and see what you can find. Of course this is very raw and could do with a bit of makeover, but it’s a command line option and there are lots of pages/scripts out there who use dig as its base for a more attractive result. But for what I usually need this is more than enough.

Something to note is that these results are taken from your current DNS cache. I.e. let’s say you’ve changed the DNS values for a domain and want to make sure that the changes have been applied… then do not expect this tool to show instant changes, because the results would still be in your DNS cache. You either reset your router or wait until the cache expires and your query shows the updated results. And of course you also have to wait until the change propagates :)

I didn’t have to install any new package for running this program, I guess it comes by default with ubuntu. But I haven’t investigated it either :D

Happy digging! ;)

20090715 “re:fritos” sources released

ppg06: refritos

Since the code for re:fritos is pretty much the same than the one from tube, porting it to Linux has been pretty much easy. So here’s another batch of demoscene sources! This demo is totally different from tube. While tube is highly aggressive and reflects perfectly the daily fight to get to your final destination on time, re:fritos is a cheeky, for-fun only, demo.

While porting this demo to Linux, I found something strange. Once I finally got everything compiled again, and executed the demo, I got a super segfault when initializing one of the resources in the demo. The SEGFAULT more exactly happened when calling std_vector::push_back. The fun begins when I tell you that this very source had also been compiled in mac and in windows, always using gcc, and never got any problem when executing it, and the machine was way less powerful than this one (just compare a Quad Core vs a humble Powerbook G4 or Mac Mini).

Trying to find out why was that happening, I suspected it could be something to do with the heap size or the stack size, although I didn’t quite know why should I have that problem. I did a test in which the effect used less interpolation points (and therefore used less memory), and the SEGFAULT didn’t happen, so that confirmed my suspicions in some way. But why? Some forums suggested to have a look at ulimits and see if that would shed any light but it didn’t really help me (or I don’t quite know how to read those results). Looking somewhere else, they suggested to add the #include <new> line. I tried it, quite skeptically to be honest, and to my surprise, the demo worked! I’m still puzzled by this. I guess that maybe Xcode included that header or linked to it when building, but I still don’t now what’s the purpose of that header. Must investigate.

Anyway, enjoy the demo, have a peek at the code, or if you know anything about this mysterious new and segfault issue, do let me know!

PS: And the count down goes on! 2…

20090710 “Blue Tuesday” sources released

Blue Tuesday by xplsv

Blue Tuesday” is a direct evolution from the codecolors code base. Since it was all done in a hurry, there were lots of things which didn’t work as expected. I somehow got rid of some of them when I ported the demo to mac, then I got rid of some extra things these days when I made the demo work in Linux, prior to releasing these sources. I am not happy with this code; with so many modifications it has grown way too much to be still readable. There are effects whose behaviour is not predictable, things like the ribbons are very inefficient and so on.

I’m still not completely happy with the synchronization. In the first version trace did a simple flash app in which he tapped a key each time an event happened (e.g. a snare hit) and then generated a list from there. But due to the way the demo is structured, I had to split the list in three parts so that I could check whether an event happened in each effect. Also with so many changes in the code, the synch had gone slightly awry and when I managed to compile the code in Linux, it clearly was asking for a revision.

So I thought about using Audacity for re-recording the synchronization. I would store each event as a label (Audacity has something called Label Tracks) and then exporting those labels as txt files and processing them to convert them into a .h file with all the events in an array. I had the idea of opening Audacity’s project with a text editor and found out that it was a simple XML file — which would make even easier the task of importing the labels’ information, without having to go through the step of exporting to a text file and parsing it.

I then tried to process the XML file with Python, but it was a horrible experience. Since it has a namespace on it and I wanted to do a simple XPath search, it seems I was doomed to fail without installing a couple of Python libraries — which I didn’t want to install. My main premise is to make my demos simple to compile, and having to install an extension just for parsing XML is not what I consider “simple”. So I resorted to good old PHP’s XML functions and in 20 minutes the import script was done. That was the easy part.

What was really painful was recording the synch points! Audacity took hanging itself up as a hobby. Thankfully, I have the CTRL+S tic –in which whenever I’m working with any program I tend to press CTRL+S regularly, pretty much each time I have typed in something, just in case– and that prevented me from losing my changes more than once. But it was just annoying to have to kill the program, open it again, say yes to “yes please recover the project I was working on when you decided to crash”. Even worse: at some point, the project got corrupted, and when I reopened it again, Audacity just got stuck switching between an sleep/idle status. I had to create a new project with the same mp3, save it and then edit the new project in a text editor, and copy and paste the old label tracks from the initial source project. Luckily it was all XML. I can’t imagine what would have I done if it had been binary data! (Probably scream a good lot!)

So lesson learnt: for making good synchronization you need a dedicated, and probably integrated, piece of software. If I need to do something like this again, I will probably spend a good amount of time in preparing something like the editor blackpawn created. Because it wasn’t only a problem of crashing, it was also the problem of not having a comfortable interface which lets you go back and forth the song, reduce its speed, etc, without having to use the mouse.

This is probably one of the reasons why I prefer to make demos with my own music: it’s easier to access the original song source files and build a list of events from there if I wanted to :-)

Enough chattering, go watch the demo or get a nice headache just by looking at the code :P