Buttons not showing up!

Pages: 12
I'm making a 4D minesweeper game, which requires lots and lots of buttons. Unfortunately, I cannot make even a single button appear!
I don't know what else to say except give me my code and let you figure out what I've done wrong, so here you go...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <windows.h>
#include <iostream>
#include <time.h>

#define BUTTON_SIZE 25
#define SPACING 25


LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

TCHAR	szAppName[]	= TEXT("Minesweeper");
TCHAR	szWinClass[]	= TEXT("WIN_CLASS");



DWORD WINAPI cLine( LPVOID param )
{
	int *stay = (int*)param;
	while (*stay==2)
	{
		std::cout << "Type 1 to reset, 0 to quit.\n";
		std::cin >> *stay;
		if (*stay > 2 || *stay < 0)
		    *stay = 2;
	}
}

int WINAPI WinMain(HINSTANCE hInstance,
		HINSTANCE hPrevInstance,
		PSTR lpCmdLine,
		int nCmdShow)
{
    MSG			msg;
	WNDCLASS	wc, bc;
	HWND		hwnd;
	g_hInst = hInstance;
	HANDLE prompt;
	
	wc.style			= 0;
	wc.lpfnWndProc		= WinProc;
    wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= szWinClass;
	
	bc.style			= 0;
	bc.lpfnWndProc		= WinProc;
    bc.cbClsExtra		= 0;
	bc.cbWndExtra		= 0;
	bc.hInstance		= hInstance;
	bc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	bc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	bc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	bc.lpszMenuName		= NULL;
	bc.lpszClassName	= "Button";

 	RegisterClass(&wc);
 	RegisterClass(&bc);

	int stay=1;
	while(stay)
	{
        srand(time(NULL));
		bool ** grid;
		HWND ** buttons;
		int sizes[4];
		stay = 2;
		{
            long long int numBombs;
			bool invalid = 1;
			while (invalid)
			{
				invalid = 0;
				std::cout << "Please list the 4 dimension lengths, with spaces.\n";
				for (int k=0;k<4;k++)
				{
					std::cin >> sizes[k];
				}
				if (sizes[0]*sizes[1]*sizes[2]*sizes[3] < 2)
				{
					std::cout << "Dimensions need to be bigger!\n";
					invalid = 1;
					continue;
				}
				if ((sizes[0]*BUTTON_SIZE+SPACING)*sizes[2]-SPACING > 800)
				{
					invalid = 1;
					std::cout << "With these dimensions, the screen would be wider than 800 pixels!\n";
				}
				if ((sizes[1]*BUTTON_SIZE+SPACING)*sizes[3]-SPACING > 600)
				{
					invalid = 1;
					std::cout << "With these dimensions, the screen would be taller than 600 pixels!\n";
				}
				if (invalid)
				{
					bool cont;
					std::cout << "Do you want to re-input the dimensions? (1/0)\n";
					std::cin >> cont;
					invalid = cont;
					if (invalid)
						std::cout << "\n\n";
				}
			}
			grid = new bool*[sizes[0]*sizes[1]];
			buttons = new HWND*[sizes[0]*sizes[1]];
			for (int i=0;i<sizes[0];i++)
			    for (int j=0;j<sizes[1];j++)
			    {
			    	grid[i*sizes[1]+j] = new bool[sizes[2]*sizes[3]];
			    	buttons[i*sizes[1]+j] = new HWND[sizes[2]*sizes[3]];
				}
			invalid = 1;
			
			while (invalid)
			{
				invalid = 0;
				std::cout << "How many bombs would you like?\n";
				std::cin >> numBombs;
				if (numBombs > sizes[0]*sizes[1]*sizes[2]*sizes[3]-1 || numBombs < 0)
				{
					std::cout << "No, really... ";
					invalid = 1;
				}
			}

			bool initBombs;
			if (numBombs < (sizes[0]*sizes[1]*sizes[2]*sizes[3])/2)
				initBombs = 0;
			else
                initBombs = 1;
			for (int x=0;x<sizes[0];x++)
				for (int y=0;y<sizes[1];y++)
				    for (int z=0;z<sizes[2];z++)
				        for (int t=0;t<sizes[3];t++)
							(grid[x*sizes[1]+y])[z*sizes[3]+t] = initBombs;
			while(numBombs)
		        if ((grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
					[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] == initBombs)
					{
						(grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
						[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] = !initBombs;
						numBombs--;
					}
		}
		hwnd = CreateWindow(szWinClass,
							szAppName,
							WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPCHILDREN,
							300,
							300,
							(sizes[0]*BUTTON_SIZE+SPACING)*sizes[2]-SPACING,
							(sizes[1]*BUTTON_SIZE+SPACING)*sizes[3]-SPACING,
							NULL,
							NULL,
							hInstance,
							NULL);
		ShowWindow(hwnd, nCmdShow);
		UpdateWindow(hwnd);
		
		/*for (int x=0;x<sizes[0];x++)
			for (int y=0;y<sizes[1];y++)
			    for (int z=0;z<sizes[2];z++)
			        for (int t=0;t<sizes[3];t++)
			            (buttons[x*sizes[1]+y])[z*sizes[3]+t] =
							CreateWindow(szWinClass,
								"BUTTON",
								BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
								z*(SPACING+BUTTON_SIZE*sizes[0])+x*BUTTON_SIZE,
								t*(SPACING+BUTTON_SIZE*sizes[1])+y*BUTTON_SIZE,
								BUTTON_SIZE,
								BUTTON_SIZE,
								hwnd,
								(HMENU)(sizes[2]*(sizes[1]*(sizes[0]*x+y)+z)+t),
								hInstance,
								NULL);*/
								
	HWND lalz = CreateWindow(

		"BUTTON","BUTTON",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,

		50,50,100,25,hwnd,(HMENU)156,NULL,NULL

	);

						
		ShowWindow(lalz, nCmdShow);
		UpdateWindow(lalz);

		
		
		prompt = CreateThread( NULL, 0, cLine, &stay, 0, NULL);
		while (stay==2)
		{
			if (PeekMessage (&msg, hwnd, 0, 0, PM_REMOVE))
			{
                if (msg.message == WM_COMMAND)
                {
					for (int x=0;x<sizes[0];x++)
						for (int y=0;y<sizes[1];y++)
						    for (int z=0;z<sizes[2];z++)
						        for (int t=0;t<sizes[3];t++)
									if ((HWND)msg.wParam == (buttons[x*sizes[1]+y])[z*sizes[3]+t])
									{
										if ((grid[x*sizes[1]+y])[z*sizes[3]+t])
										{
											stay = 2;
											std::cout << "You lose, sorry! :(\n";
										}
										int adjBombs;
										for (int i=-1;i<2;i++)
											for (int j=-1;j<2;j++)
												for (int k=-1;k<2;k++)
													for (int u=-1;u<2;u++)
													    if (x+i>=0&&x+i<sizes[0]&&y+j>=0&&y+j<sizes[1]&&z+k>=0&&z+k<sizes[2]&&t+u>=0&&t+u<sizes[3])
															if ((grid[(x+i)*sizes[1]+(y+j)])[(z+k)*sizes[3]+(t+u)])
																adjBombs++;
                                        SetWindowText((buttons[x*sizes[1]+y])[z*sizes[3]+t],"HARR");
									}
    }else
    {
					TranslateMessage (&msg);
					DispatchMessage (&msg);
    }
			}
			else
			{
			}
		}
		for (int i=0;i<sizes[0];i++)
		    for (int j=0;j<sizes[1];j++)
		    {
		    	delete grid[i*sizes[1]+j];
		    	delete buttons[i*sizes[1]+j];
			}
		delete grid;
		delete buttons;
		DestroyWindow(hwnd);
	}
	return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		break;
		case WM_PAINT:
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break; // pass to DefWindowProc(...) as well
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
}

I know, I have terrible style, and no comments at all, but with luck you'll see my problem right away. The commented code is the code that SHOULD work but doesn't (no errors), and the window creation code right after that (with HWND lalz) is a test button.

This is my first Windows API program that actually uses windows (I've multithreaded successfully before, and that part I KNOW is fine), so please do point out stuff I'm doing wrong!

And finally, I am using Dev-C++ 4.9.9.2
Last edited on
I love the way my code goes all the way off the right of the screen :O
This program does not work at all.
You are mixing a CONSOLE app with a GUI app - so all that stuff with cout
and cin does not function. In fact you have an endless loop at starting at line 83 with this code:
1
2
3
4
5
6
if (sizes[0]*sizes[1]*sizes[2]*sizes[3] < 2)
{
    std::cout << "Dimensions need to be bigger!\n";
	invalid = 1;
	continue;
}


becasue cin isn't working, line 81 std::cin >> sizes[k]; does nothing - lines 86 and 87 (at least on MS Visial Studio) always gets done - which means that you keep going around the loop.

I think you should create the main window, then use a dialogbox (probably in the WM_CREATE message) to get the details.

There are also some other issues.
Try running it before you say such horrible things about me :(
In Dev-C++ 4.9.9.2 with my settings, at least, the console appears and runs just fine. I tested all of that already... The window shows up, and the console runs and receives input fine concurrently in the other thread. But the window is a blank white window, even though I'm doing what I think draws a button in it.

There might be an infinite loop, and thanks for noticing that, but that doesn't have anything to do with my problem.

EDIT: No... there isn't an infinite loop, because cin works. I'll try writing a console-less version later tonight, when I get home, so you can compile it in Visual Studio.
Last edited on
When Dev C++ runs a program, it opens a console window as well I believe, this is maybe why your cin works. (I have dev C++ and will confirm that also)
When run as a windows program it won't work.

YES I did run it - I have it open in Microsoft Visual Studio>

PS - what horrible things about you?
Last edited on
Or about my program. How it doesn't work at all and stuff.
Any luck with making it run?
Any ideas about the button problem?
Why are you creating a thread? There shouldn't be any reason for it.
So I can accept input at any point in the program. cin pauses the program, so if I want to use that, I have to make it in its own thread.
=\

Mixing Win32 Windows and Console isn't a good idea. Especially trying to thread an application when you are so new to development.

Multi-threaded application development is exponentially harder (not just a little harder) than standard code crunching. There are soooo many more things you have to consider. Even change a variable value becomes tricky.

If your using Win32 GUI then you should handle all input through either that, or configuration files that are loaded automatically.
That has nothing whatsoever to do with my problem, though... my threads work 100% perfectly.
Here, I took the thread out, since that seems to distract y'all. Same problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <windows.h>
#include <iostream>
#include <time.h>

#define BUTTON_SIZE 25
#define SPACING 25

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

TCHAR	szAppName[]	= TEXT("Minesweeper");
TCHAR	szWinClass[]	= TEXT("WIN_CLASS");

int WINAPI WinMain(HINSTANCE hInstance,
		HINSTANCE hPrevInstance,
		PSTR lpCmdLine,
		int nCmdShow)
{
    MSG			msg;
	WNDCLASS	wc, bc;
	HWND		hwnd;
	HANDLE prompt;

	wc.style			= 0;
	wc.lpfnWndProc		= WinProc;
    wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= szWinClass;

	bc.style			= 0;
	bc.lpfnWndProc		= WinProc;
    bc.cbClsExtra		= 0;
	bc.cbWndExtra		= 0;
	bc.hInstance		= hInstance;
	bc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	bc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	bc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	bc.lpszMenuName		= NULL;
	bc.lpszClassName	= "Button";

 	RegisterClass(&wc);
 	RegisterClass(&bc);

	int stay=1;
	while(stay)
	{
        srand(time(NULL));
		bool ** grid;
		HWND ** buttons;
		int sizes[4];
		stay = 2;
		{
            long long int numBombs = 4;
				for (int k=0;k<4;k++)
				{
					sizes[k] = 2;
				}
			grid = new bool*[sizes[0]*sizes[1]];
			buttons = new HWND*[sizes[0]*sizes[1]];
			for (int i=0;i<sizes[0];i++)
			    for (int j=0;j<sizes[1];j++)
			    {
			    	grid[i*sizes[1]+j] = new bool[sizes[2]*sizes[3]];
			    	buttons[i*sizes[1]+j] = new HWND[sizes[2]*sizes[3]];
				}
			bool initBombs;
			if (numBombs < (sizes[0]*sizes[1]*sizes[2]*sizes[3])/2)
				initBombs = 0;
			else
                initBombs = 1;
			for (int x=0;x<sizes[0];x++)
				for (int y=0;y<sizes[1];y++)
				    for (int z=0;z<sizes[2];z++)
				        for (int t=0;t<sizes[3];t++)
							(grid[x*sizes[1]+y])[z*sizes[3]+t] = initBombs;
			while(numBombs)
		        if ((grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
					[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] == initBombs)
					{
						(grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
						[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] = !initBombs;
						numBombs--;
					}
		}
		hwnd = CreateWindow(szWinClass,
							szAppName,
							WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPCHILDREN,
							300,
							300,
							(sizes[0]*BUTTON_SIZE+SPACING)*sizes[2]-SPACING,
							(sizes[1]*BUTTON_SIZE+SPACING)*sizes[3]-SPACING,
							NULL,
							NULL,
							hInstance,
							NULL);
		ShowWindow(hwnd, nCmdShow);
		UpdateWindow(hwnd);

		/*for (int x=0;x<sizes[0];x++)
			for (int y=0;y<sizes[1];y++)
			    for (int z=0;z<sizes[2];z++)
			        for (int t=0;t<sizes[3];t++)
			            (buttons[x*sizes[1]+y])[z*sizes[3]+t] =
							CreateWindow(szWinClass,
								"BUTTON",
								BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
								z*(SPACING+BUTTON_SIZE*sizes[0])+x*BUTTON_SIZE,
								t*(SPACING+BUTTON_SIZE*sizes[1])+y*BUTTON_SIZE,
								BUTTON_SIZE,
								BUTTON_SIZE,
								hwnd,
								(HMENU)(sizes[2]*(sizes[1]*(sizes[0]*x+y)+z)+t),
								hInstance,
								NULL);*/

	HWND lalz = CreateWindow(

		"BUTTON","BUTTON",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,

		50,50,100,25,hwnd,(HMENU)156,NULL,NULL

	);


		ShowWindow(lalz, nCmdShow);
		UpdateWindow(lalz);

		while (stay==2)
		{
			if (PeekMessage (&msg, hwnd, 0, 0, PM_REMOVE))
			{
                if (msg.message == WM_COMMAND)
                {
					for (int x=0;x<sizes[0];x++)
						for (int y=0;y<sizes[1];y++)
						    for (int z=0;z<sizes[2];z++)
						        for (int t=0;t<sizes[3];t++)
									if ((HWND)msg.wParam == (buttons[x*sizes[1]+y])[z*sizes[3]+t])
									{
										if ((grid[x*sizes[1]+y])[z*sizes[3]+t])
										{
											stay = 1;
										}
										int adjBombs;
										for (int i=-1;i<2;i++)
											for (int j=-1;j<2;j++)
												for (int k=-1;k<2;k++)
													for (int u=-1;u<2;u++)
													    if (x+i>=0&&x+i<sizes[0]&&y+j>=0&&y+j<sizes[1]&&z+k>=0&&z+k<sizes[2]&&t+u>=0&&t+u<sizes[3])
															if ((grid[(x+i)*sizes[1]+(y+j)])[(z+k)*sizes[3]+(t+u)])
																adjBombs++;
                                        SetWindowText((buttons[x*sizes[1]+y])[z*sizes[3]+t],"HARR");
									}
    }else
    {
					TranslateMessage (&msg);
					DispatchMessage (&msg);
    }
			}
			else
			{
			}
		}
		for (int i=0;i<sizes[0];i++)
		    for (int j=0;j<sizes[1];j++)
		    {
		    	delete grid[i*sizes[1]+j];
		    	delete buttons[i*sizes[1]+j];
			}
		delete grid;
		delete buttons;
		DestroyWindow(hwnd);
	}
	return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		break;
		case WM_PAINT:
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break; // pass to DefWindowProc(...) as well
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
}

EDIT: Removed all the console interface, too. Now you KNOW it's not the threading or command line!
Last edited on
Actually I've been working on it - and found some mistake.
I have also removed the console stuff and use some fixed values for
the grid sizes and the number of bombs.
Now this works - and displays a window with loads of pushbuttons on it.
I have made some comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <windows.h>
#include <iostream>
#include <time.h>

#define BUTTON_SIZE 25
#define SPACING 25

HINSTANCE g_hInst; 


LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

TCHAR	szAppName[]	= TEXT("Minesweeper");
TCHAR	szWinClass[]	= TEXT("WIN_CLASS");



DWORD WINAPI cLine( LPVOID param )
{
	int *stay = (int*)param;
	while (*stay==2)
	{
		std::cout << "Type 1 to reset, 0 to quit.\n";
		std::cin >> *stay;
		if (*stay > 2 || *stay < 0)
		    *stay = 2;
	}

    /////////////
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,
		HINSTANCE hPrevInstance,
		PSTR lpCmdLine,
		int nCmdShow)
{
    MSG			msg;
	WNDCLASS	wc, bc;
	HWND		hwnd;
	g_hInst = hInstance;
	HANDLE prompt;
	
	wc.style			= 0;
	wc.lpfnWndProc		= WinProc;
    wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= szWinClass;
	
	//Get rid of this Button class class - it clashes with
    // the default windows button style.
    /*bc.style			= 0;
	bc.lpfnWndProc		= WinProc;
    bc.cbClsExtra		= 0;
	bc.cbWndExtra		= 0;
	bc.hInstance		= hInstance;
	bc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	bc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	bc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	bc.lpszMenuName		= NULL;
	bc.lpszClassName	= _T("Button");*/

 	RegisterClass(&wc);
 	//RegisterClass(&bc);

	int stay=1;
	while(stay)
	{
        srand(time(NULL));
		bool ** grid;
		HWND ** buttons;
        
        //Got rid of the sonsole cout and cin stuff and made it fixed size for
        //testing purposes
        int sizes[4]={4,4,4,4}; 
		stay = 2;
         //Got rid of cout and cin stuff and fixed the number of bombs   
        long long int numBombs = 10;

			bool invalid = 1;
			grid = new bool*[sizes[0]*sizes[1]];
			buttons = new HWND*[sizes[0]*sizes[1]];
			for (int i=0;i<sizes[0];i++)
			    for (int j=0;j<sizes[1];j++)
			    {
			    	grid[i*sizes[1]+j] = new bool[sizes[2]*sizes[3]];
			    	buttons[i*sizes[1]+j] = new HWND[sizes[2]*sizes[3]];
				}
			
                invalid = 1;
			

			bool initBombs;
			if (numBombs < (sizes[0]*sizes[1]*sizes[2]*sizes[3])/2)
				initBombs = 0;
			else
                initBombs = 1;
			for (int x=0;x<sizes[0];x++)
				for (int y=0;y<sizes[1];y++)
				    for (int z=0;z<sizes[2];z++)
				        for (int t=0;t<sizes[3];t++)
							(grid[x*sizes[1]+y])[z*sizes[3]+t] = initBombs;
			while(numBombs)
		        if ((grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
					[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] == initBombs)
					{
						(grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
						[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] = !initBombs;
						numBombs--;
					}
		hwnd = CreateWindow(szWinClass,
							szAppName,
							WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPCHILDREN,
							300,
							300,
							(sizes[0]*BUTTON_SIZE+SPACING)*sizes[2]-SPACING,
							(sizes[1]*BUTTON_SIZE+SPACING)*sizes[3]-SPACING,
							NULL,
							NULL,
							hInstance,
							NULL);
		ShowWindow(hwnd, nCmdShow);
		UpdateWindow(hwnd);
		
		for (int x=0;x<sizes[0];x++)
			for (int y=0;y<sizes[1];y++)
			    for (int z=0;z<sizes[2];z++)
			        for (int t=0;t<sizes[3];t++)
                    {
			            //made some temp variables for easier debugging
                        int z1, z2,  z3;
                        z1 = z*(SPACING+BUTTON_SIZE*sizes[0])+x*BUTTON_SIZE;
                        z2 = t*(SPACING+BUTTON_SIZE*sizes[1])+y*BUTTON_SIZE;
                        z3 = (sizes[2]*(sizes[1]*(sizes[0]*x+y)+z)+t);

                        (buttons[x*sizes[1]+y])[z*sizes[3]+t] =
							CreateWindow(_T("BUTTON"), //szWinClass <- you had the wrong window class here!!!!
								_T(""),
								BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
								z1, //z*(SPACING+BUTTON_SIZE*sizes[0])+x*BUTTON_SIZE,
								z2,  //t*(SPACING+BUTTON_SIZE*sizes[1])+y*BUTTON_SIZE,
								BUTTON_SIZE,
								BUTTON_SIZE,
								hwnd,
								(HMENU)z3, //(HMENU)(sizes[2]*(sizes[1]*(sizes[0]*x+y)+z)+t),
								hInstance,
								NULL);
                    }
								
	//Don't need this anymore
    /*HWND lalz = CreateWindow(

		_T("BUTTON"),_T("BUTTON"),BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,

		50,50,100,25,hwnd,(HMENU)156,NULL,NULL

	);

	ShowWindow(lalz, nCmdShow);
    UpdateWindow(lalz);*/

		
		//Temporarily removed thread for testing purposes
		//prompt = CreateThread( NULL, 0, cLine, &stay, 0, NULL);
		while (stay==2)
		{
			if (PeekMessage (&msg, hwnd, 0, 0, PM_REMOVE))
			{
                if (msg.message == WM_COMMAND)
                {
					for (int x=0;x<sizes[0];x++)
						for (int y=0;y<sizes[1];y++)
						    for (int z=0;z<sizes[2];z++)
						        for (int t=0;t<sizes[3];t++)
									if ((HWND)msg.wParam == (buttons[x*sizes[1]+y])[z*sizes[3]+t])
									{
										if ((grid[x*sizes[1]+y])[z*sizes[3]+t])
										{
											stay = 2;
											std::cout << "You lose, sorry! :(\n";
										}
										int adjBombs=0;//////////////////////
										for (int i=-1;i<2;i++)
											for (int j=-1;j<2;j++)
												for (int k=-1;k<2;k++)
													for (int u=-1;u<2;u++)
													    if (x+i>=0&&x+i<sizes[0]&&y+j>=0&&y+j<sizes[1]&&z+k>=0&&z+k<sizes[2]&&t+u>=0&&t+u<sizes[3])
															if ((grid[(x+i)*sizes[1]+(y+j)])[(z+k)*sizes[3]+(t+u)])
																adjBombs++;
                                        SetWindowText((buttons[x*sizes[1]+y])[z*sizes[3]+t],_T("HARR"));
									}
                }
                else
                {
					TranslateMessage (&msg);
					DispatchMessage (&msg);
                }
            }
			else
			{
                //do nothing??
			}
		}
		for (int i=0;i<sizes[0];i++)
		    for (int j=0;j<sizes[1];j++)
		    {
		    	delete grid[i*sizes[1]+j];
		    	delete buttons[i*sizes[1]+j];
			}
		delete grid;
		delete buttons;
		DestroyWindow(hwnd);
	}
	return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		break;
		case WM_PAINT:
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break; // pass to DefWindowProc(...) as well
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		default:
            break;
	}
   return DefWindowProc(hwnd, msg, wParam, lParam);

}
The other major issue (apart from the console stuff) was the way the buttons were being created (which was why they were't showing up).

1. You were registering a BUTTON class of your own, which was over-ruling
the natural built in windows button type.

2. Also when you were creating the pushbuttons you were using the szWinClass type. This is the same as the main window class! which was causing problems.
PS - In case you are wondering about the _T () macro surrounding the string quotes - it's because I'm using Microsoft Visual Studio.
DEV C++ might complain about them - if so you might need to remplace them with TEXT
Last edited on
You're fantastic :)
Thanks a bajillion!
Set it up with the console and the thread working again. Thanks so much!
IMO. Once the game gets more adv the threading will cause you an issue unless you get a good threading library and implement some locks, barriers etc.
The threading isn't going to get any more complicated. I've written everything I want to write for it.
Wait until 2 threads access the same variable simultaneously :)
Last edited on
Why would they spontaneously do that if I don't change the code?
You have 2 threads running concurrently.

Both threads read and modify the variable 'stay'. Yet you have no locks around this to ensure you don't end up with a race condition and invalid memory or undefined behaviour.
Pages: 12