|
| | Personal → Porting a simple program from Windows to Linux | Rating: 0 | 01 Sep 10 at 18:28 |
I want to substantiate information about Eclipse with some practice. The story will focus on a cross-platform programming. Do you like cross-platform software? I just love it - it's so convenient to use the same tools in any operating system!
We will use a simple chiming clock as a program for porting. I wrote this program ages ago, even when I was a student, but still use it.
The specification is as follows: every hour it must strike the number of hours, every half of an hour it must strike once.
The code for Windows looks like this:
Let's run through the code:
<windows.h> - the standard library for win32-programs.
<mmsystem.h> - we use a function called "sndPlaySound" from this library
int main () - the usual main function
int h, m, s; - declare integer variables to store hours, minutes and seconds
SYSTEMTIME lpSystemTime; - the structure for storing the system time
while (1) (- beginning of an infinite loop, so the program will work permanently
GetLocalTime (& lpSystemTime); - here we fill the structure "lpSystemTime" with current system time
h = lpSystemTime.wHour; - choose from "lpSystemTime" structure the field with the number of hours and put it in the variable "h"
m = lpSystemTime.wMinute; - the same for minutes
s = lpSystemTime.wSecond; - the same for seconds
if (h>12) {h=h-12;} - as the number of hours is represented with a number from 0 to 23, and I don't want to listen dozens of bell strikes, it was decided to limit it by 12. Accordingly, the number of hours representation was converted from military to regular one. For example, if it is 17 o'clock, then the chiming clock wil strike 17 - 12 = 5 times.
//if (h==0) h=12; - the commented line that handles a situation when the number of hours is equal to zero (midnight). Why it is commented? Because when the clock strikes 11 times in 23:00 it's no one sleeps yet and it's tolerated, but when in night silence a bell strikes 12 times... As a result, it was decided not to strike at midnight.
Sleep (1000) - since we are interested in time only with pricision up to seconds, then it is not necessary to utilize the CPU always - let it sleeps for 1000 milliseconds.
if ((m == 30) & (s == 0)) {sndPlaySound ("bell.wav", SND_ASYNC);}; - if the number of minutes is 30, and the number of seconds is 0 (i.e. exactly an half of an hour), then play asynchronously the file called "bell.wav", i.e. strike once. The function called "sndPlaySound" accepts only "wav"-files, so the piece of the intro of "Metallica - For whom the bell tolls.mp3" had to be converted. Note that if you specify such a filename, it will be searched only in the working directory, so, if you want to put it apart you need to specify a full path.
if ((m == 0) & (s == 0)) - this condition is satisfied when the number of minutes equal to zero and the number of seconds is zero too. That is, when it is exactly an hour sharp.
for (int i=0; i<h; i++) - a loop from zero to the current number of hours. That is, for example, if it's five o'clock, then this part will be repeated five times.
sndPlaySound ("bell.wav", SND_ASYNC); - this function asynchronously plays the already known sound of a bell toll.
Sleep (2000) - waiting for two seconds. This is important because playing is asynchronous: the second bell is heard before the end of the first, if the bell sample is longer than two seconds, or with a slight delay if the sound is shorter. In general, this value must be chosen depending on the selected signal.
return 0; - return zero. In fact, considering that there is the infinite loop before, we will never reach this line.
Now we turn to porting. The source for Linux looks like this:
There are only three functions in the program - to get the system time, to play a sound file, and to sleep. Let's start with including of libraries that contain these functions:
time.h - a library with functions for working with time
unistd.h - a library with "sleep" function
stdlib.h - and this library will allow us to make a system call to play the sound file directly from the program.
The "main" function and declaration of variables for hours, minutes and seconds left without changes.
But the structure containing a system time now looks somewhat different:
The difference is because Windows function "GetLocalTime" immediately fills the structure of time expanding it by years, months, days, etc., but Linux function "time" returns unix-time, i.e. time represented in seconds, and for its transformation into what we need we have to use another function - "localtime".
We see these differences after the identical while (1):
Accordingly, it is a different procedure of filling variables of the structure - a Linux-variant even a bit C++-style:
Converting time from the military standard to the regular one remains the same. But "sleep" now in seconds rather than milliseconds:
Conditions under which the sounds are played remained unchanged, but now it's different ways to play: in Linux it can be done easily through the system call "play file_name.wav":
In such case playing will be synchronous (next toll will not start until the previous ends), so the duration between beats can be adjusted by file length. So the second "sleep" is not needed.
That's all the differences. But the program is very tiny. Doing something like this you understand that you shouldn't criticize severely Blizzard or Valve, because they don't want to port their games to Linux. Especially if the cross-platform feature initially wasn't expected.
PS I can't share the source in the text form instead of images, because of paranoid "antihacker". So if you want it - PM me. | | Tag: programming, Linux, Windows, cross-platform, c++ |
Rating: 0
Entry comments: No comments on this entry. Back.
Post a comment
Log in or register to post a comment. |
| |
 | |  |
|
|
| Total number of registered users: |
4810 |
| Online: |
10 |
| Newbe: abwphbkyon |
Who's online: |
|
|
| Guests online: |
8 |
| Maximum online (10 May 2011) |
80 |
|
|
|