Click here to Skip to main content
Click here to Skip to main content

A Simple Cross-platform Program

, 13 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to write a program that allows a simple game to run on Linux and Windows


I wrote the original code for my Sudoku program using Visual Basic on Microsoft's Visual Studio IDE. This restricts the program to Windows. To allow the code to execute successfully on Windows and Linux, I decided to use the portability of both the C and C++ languages. I abstract the program to a series of functions which can be interfaced to the native GUI toolkits. The GUI interface needs only some buttons, an image box and mouse position. It is used only to call the functions in the core C or C++ program. In Windows, I still use Visual Basic IDE to provide the GUI. The functions are provided by compiling the C++ core to a managed assembly DLL. In Linux, I use Glade to develop the GUI and associated code. The functions are added to the GUI code directly. The image of the Sudoku uses an uncompressed bitmap file in Windows BMP file format. This can be displayed in Windows and Linux. It can also be directly worked on to produce the various images necessary for the program.

Image Generation

The Visual Basic program, sudoku_bmp, is used to generate the start image used by the core code in the program. It was written with Visual Basic 2008 Express Edition. Run this program to produce a window which displays the Sudoku start image. Unfortunately we cannot directly get at a BMP file image by...

Dim soduku As New Bitmap(10, 10)
'generate the graphics as per sudoku_bmp program

...because all bitmaps are stored in PNG file format. This uses a complex lossless compression and we can't use it directly at the color byte level. We can do a screen copy and then cut as necessary using a paint type program to produce start.bmp with 24 bits per pixel.

Below is a byte listing of a 1x1 black pixel using BMP file format: 00000000 42 4D 3A 00 00 00 00 00-00 00 36 00 00 00 28 00 BM:.......6...(. 00000010 00 00 01 00 00 00 01 00-00 00 01 00 18 00 00 00 ................ 00000020 00 00 04 00 00 00 13 0B-00 00 13 0B 00 00 00 00 ................ 00000030 00 00 00 00 00 00 next is data 00 00-00 00 .......... First two bytes BM next two bytes size of file (little endian) H003A = 58 bytes. Next 6 bytes zeros H36 little end of 54 bytes = size of header next is size 01 by 01 ect including H18 = 24 bits per pixel. The data on each row is rounded up to make an even number. 00 00 00 with a filler byte of 00 data is backwards, i.e. BGR.

The start.bmp is 573 pixels wide and 598 pixels high. The file has a 54 byte header and the pixel data is stored in a raster arrangement. Each horizontal line has 573 x 3 bytes for the data and one filler byte = 1720 bytes. There are 598 lines giving a total of 1028614 bytes. When the image is displayed on the screen, the top line corresponds to the last line stored in the file.


The start.bmp contains all the individual images needed to run the Sudoku program.

Core Program

The core program is written in C++ for Windows. It is written with Visual C++ 2008 Express Edition. The core is functionally the same but down sized to C for Linux. It is written by modifying the C++ .cpp and .h files with the text editor in Linux. The functions which are used by the GUI interface are:

sudoku(); 			//constructor to get the main image
				//and extract the images for the Sudoku program
void userselect(int x,int y, int number);	//user selects number
void stepback(); 			//go back one step in the solution
void highlight(int x, int y); 	//change the pencil number to yellow
void randomgame(int level); 	//start hard, medium or easy game
void solveexisting(); 		//solve existing Sudoku
void startenter(); 		//start Sudoku at from step 0

The core has these global variables:

char *mainimage = new char[1028614];	//allocate memory as needed -dynamic
// won't be pushed on stack which is set at about 1M
char selectnumber[10][10800]; 	//error(0) and selected(1-9) numbers
char pencilnumber[2][10][1200]; 	//blank(0) and pencil(1-9) numbers
//dim [0] is pencil...dim[1] is select pencil
char sudokuarray[82][3][3][3][3][3][3]; //0 to 81 steps in solution
//x large..y large..x small..y small..x number..y number
int sudokustep =0; 		//step counter used in all solutions as current step
int yellow[2] = {27,27}; 		//x-y record of pencilnumbers highlighted

When the program starts, we load the main image:

fstream inclientfile ("start.bmp",ios::binary); 	//1,028,614 bytes in file,1028614 ); 		//read raw bytes to the array

Then we extract the images of pencil numbers and select numbers from the mainimage array. These images are then used in the core program to generate the image which shows the progress of the game. At each step, the new image is then stored:

ofstream outClientFile( "copy.bmp", ios::binary ); 	// copy the file
outClientFile.write(mainimage,1028614 ); 		//write raw bytes to the array

The GUI then displays the new image in an image box on its window.


The Visual C++ program sudokudll is used to produce a managed assembly DLL. Open the project file with VC++2008 Express and build solution to compile sudokudll.dll. The Visual Basic program MySudoku references this DLL. This program is used to produce the GUI window for the program. As each step in the program occurs, we update the image on the window:

Dim st As Stream = File.Open("copy.bmp", FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(st)

Open the project file with VB2008 Express and build MySudoku to compile the .exe file.


The program Glade3 is used to produce the GUI window for the Sudoku core. As each step in the program occurs, we update the image on the window:

gtk_image_set_from_file(image1, "copy.bmp");

Open the file, then select build to produce the code for the GUI. Open a terminal window and in the directory which has the .glade file and type: ./ This gives us all the code for the user GUI window. Copy all the files in src_add_replace and paste them using "replace all" into the src folder. Change the terminal to the src directory and type "make" to compile the Linux executable file.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

carl morey

Australia Australia
I identify with the starfish.
I may be really stupid and have to use visual basic but at least I'm happy.

Comments and Discussions

GeneralMy vote of 1 PinmemberNagaraj Muthuchamy29-Sep-09 1:05 
GeneralMy vote of 2 PinmemberHenry Minute29-Aug-09 7:36 
GeneralMy vote of 1 PinmemberRick York19-Aug-09 8:20 
I think the article is bordering on useless. You talk about a cross-platform program but have not described anything about that is actually "cross-platform". To me the word "cross-platform" means that the same piece of code will compile and work on different platforms. That is not the case here. You say the core functionality is the same but it is in C for Linux and C for Windows. To me this does not qualify as "cross-platform."
Then there is the code which, in my opinion, is awful. One example : you describe how you calculated the size of your bitmap image and then used that value literally throughout the code samples. When I have to "break in" a new programmer the very first thing we discuss is that is a VERY bad practice and any code seen like will not survive a review. One can determine that size programmatically and that is what should have been done then that value should be declared as a "const size_t" or something similar.
GeneralRe: My vote of 1 Pinmembercarl morey21-Aug-09 21:18 
GeneralGlade on Windows PinmemberBlair C. Bonnett18-Aug-09 21:44 
GeneralRe: Glade on Windows Pinmembercarl morey21-Aug-09 21:17 
GeneralPNG PinmemberTobiasP11-Aug-09 9:33 
GeneralRe: PNG Pinmembercarl morey13-Aug-09 20:22 
GeneralUsing mono Pinmembercarl morey9-Aug-09 19:55 
QuestionIs there a reason you didn't want to use Mono? Pinmemberspoodygoon4-Aug-09 10:21 
AnswerRe: Is there a reason you didn't want to use Mono? Pinmembercarl morey4-Aug-09 12:51 
GeneralRe: Is there a reason you didn't want to use Mono? Pinmembersam.hill4-Aug-09 15:48 
GeneralRe: Is there a reason you didn't want to use Mono? Pinmembercarl morey4-Aug-09 18:52 
GeneralThe Starfish Pinmembersam.hill4-Aug-09 9:21 
GeneralRe: The Starfish PinmemberJean-Paul Mikkers4-Aug-09 11:03 
GeneralRe: The Starfish Pinmembersam.hill4-Aug-09 15:45 
GeneralRe: The Starfish PinmemberAtanas Palavrov10-Aug-09 12:49 
GeneralRe: The Starfish PinmemberPaul Watt17-Aug-09 11:24 
GeneralRe: The Starfish Pinmembersameeraperera7-Aug-09 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 14 Aug 2009
Article Copyright 2009 by carl morey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid