Click here to Skip to main content
13,596,541 members
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 4 Aug 2009
Licenced CPOL

A Simple Cross-platform Program

, 13 Aug 2009
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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Nagaraj Muthuchamy29-Sep-09 0:05
memberNagaraj Muthuchamy29-Sep-09 0:05 
GeneralMy vote of 2 Pin
Henry Minute29-Aug-09 6:36
memberHenry Minute29-Aug-09 6:36 
GeneralMy vote of 1 Pin
Rick York19-Aug-09 7:20
memberRick York19-Aug-09 7:20 
GeneralRe: My vote of 1 Pin
carl morey21-Aug-09 20:18
membercarl morey21-Aug-09 20:18 
GeneralGlade on Windows Pin
Blair C. Bonnett18-Aug-09 20:44
memberBlair C. Bonnett18-Aug-09 20:44 
GeneralRe: Glade on Windows Pin
carl morey21-Aug-09 20:17
membercarl morey21-Aug-09 20:17 
GeneralPNG Pin
TobiasP11-Aug-09 8:33
memberTobiasP11-Aug-09 8:33 
GeneralRe: PNG Pin
carl morey13-Aug-09 19:22
membercarl morey13-Aug-09 19:22 
GeneralUsing mono Pin
carl morey9-Aug-09 18:55
membercarl morey9-Aug-09 18:55 
QuestionIs there a reason you didn't want to use Mono? Pin
spoodygoon4-Aug-09 9:21
memberspoodygoon4-Aug-09 9:21 
AnswerRe: Is there a reason you didn't want to use Mono? Pin
carl morey4-Aug-09 11:51
membercarl morey4-Aug-09 11:51 
GeneralRe: Is there a reason you didn't want to use Mono? Pin
sam.hill4-Aug-09 14:48
membersam.hill4-Aug-09 14:48 
GeneralRe: Is there a reason you didn't want to use Mono? Pin
carl morey4-Aug-09 17:52
membercarl morey4-Aug-09 17:52 
GeneralThe Starfish Pin
sam.hill4-Aug-09 8:21
membersam.hill4-Aug-09 8:21 
GeneralRe: The Starfish Pin
Jean-Paul Mikkers4-Aug-09 10:03
memberJean-Paul Mikkers4-Aug-09 10:03 
GeneralRe: The Starfish Pin
sam.hill4-Aug-09 14:45
membersam.hill4-Aug-09 14:45 
GeneralRe: The Starfish Pin
Atanas Palavrov10-Aug-09 11:49
memberAtanas Palavrov10-Aug-09 11:49 
GeneralRe: The Starfish Pin
Paul Watt17-Aug-09 10:24
memberPaul Watt17-Aug-09 10:24 
GeneralRe: The Starfish Pin
sameeraperera7-Aug-09 1:01
membersameeraperera7-Aug-09 1:01 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180621.3 | Last Updated 14 Aug 2009
Article Copyright 2009 by carl morey
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid