1 /*
2 ##########################################
3 # TriMines version 1.3.0 #
4 # #
5 # main.c - deals with mouse #
6 # clicks and mouse movement. #
7 ##########################################
8 */
9
10
11 /* board values:
12 * 0 = clicked button
13 * 1 = 1
14 * 2 = 2
15 * 3 = 3
16 * 4 = 4
17 * 5 = 5
18 * 6 = 6
19 * 7 = 7
20 * 8 = 8
21 * 9 = 9
22 * 10 = 10
23 * 11 = 11
24 * 12 = 12
25 * 13 = red mine
26 * 14 = mine
27 * 15 =
28 * 16 = pointer
29 */
30
31 /* board2 values:
32 * 0 = show unpressed button
33 * 1 = show as it was pressed
34 * 2 = show flag
35 * 3 = show question mark
36 * 4 = false flagging
37 */
38
39
40 /* gamestatus values:
41 * 0 = new game or during game
42 * 1 = win
43 * 2 = game over
44 */
45
46 /* firstclick values:
47 * 0 = not first click
48 * 1 = first click
49 */
50
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <time.h>
55 #include "SDL.h"
56
57 int mouse_x, mouse_y;
58 int boardx = 50; // max 66
59 int boardy = 20; // max 26
60 int mines = 120; //(boardx * boardy) * minesprecents;
61 int flags,gamestatus,firstclick;
62 int board[66][26];
63 int board2[66][26];
64 SDL_Surface *screen;
65 SDL_Surface *Ibutton;
66 SDL_Surface *Ibutton2;
67 SDL_Surface *Iflag;
68 SDL_Surface *Ifalse;
69 SDL_Surface *Ifalse2;
70 SDL_Surface *Iflag2;
71 SDL_Surface *Iclicked;
72 SDL_Surface *Iclicked2;
73 SDL_Surface *Iquestion;
74 SDL_Surface *Iquestion2;
75 SDL_Surface *Imine;
76 SDL_Surface *Iredmine;
77 SDL_Surface *Iredmine2;
78 SDL_Surface *Imine2;
79 SDL_Surface *Ipointer;
80 SDL_Surface *Ipointer2;
81 SDL_Surface *I1;
82 SDL_Surface *I1_2;
83 SDL_Surface *I2;
84 SDL_Surface *I2_2;
85 SDL_Surface *I3;
86 SDL_Surface *I3_2;
87 SDL_Surface *I4;
88 SDL_Surface *I4_2;
89 SDL_Surface *I5;
90 SDL_Surface *I5_2;
91 SDL_Surface *I6;
92 SDL_Surface *I6_2;
93 SDL_Surface *I7;
94 SDL_Surface *I7_2;
95 SDL_Surface *I8;
96 SDL_Surface *I8_2;
97 SDL_Surface *I9;
98 SDL_Surface *I9_2;
99 SDL_Surface *I10;
100 SDL_Surface *I10_2;
101 SDL_Surface *I11;
102 SDL_Surface *I11_2;
103 SDL_Surface *I12;
104 SDL_Surface *I12_2;
105 SDL_Surface *Ibar;
106 SDL_Surface *Ibar2;
107 SDL_Surface *Ismile;
108 SDL_Surface *Ismile2;
109 SDL_Surface *Ismile3;
110 SDL_Surface *Ismile4;
111 SDL_Surface *Icounter0;
112 SDL_Surface *Icounter1;
113 SDL_Surface *Icounter2;
114 SDL_Surface *Icounter3;
115 SDL_Surface *Icounter4;
116 SDL_Surface *Icounter5;
117 SDL_Surface *Icounter6;
118 SDL_Surface *Icounter7;
119 SDL_Surface *Icounter8;
120 SDL_Surface *Icounter9;
121 SDL_Surface *Icounterm;
122 SDL_Surface *Icounterclear;
123 SDL_Surface *Imenu;
124 SDL_Surface *Imenu_select;
125 SDL_Surface *Imenu_select_clear;
126 SDL_Surface *Imenu_newgame,*Imenu_options,*Imenu_quit,*Imenu_width,*Imenu_height,*Imenu_mines;
127 float minesprecents = 0.15; // max precentes of mines
128 int flagcounterx,flagcountery,smileyposx,smileyposy,timercounterx,timercountery;
129 int window_width, window_height;
130 int timer = 0;
131 int out;
132 int loadimgs = 1;
133
134 #include "boardinit.c"
135 #include "counters.c"
136 #include "gfx.c"
137 #include "gamestatus.c"
138 #include "uncover.c"
139 #include "menu.c"
140
main(int argc,char * argv[])141 main(int argc, char *argv[])
142 {
143 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
144 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
145 exit(1);
146 }
147
148 // GAME TITLEBAR TEXT:
149 SDL_WM_SetCaption("TriMines","TriMines");
150
151 // LOAD ALL GAME IMAGES INTO VARS:
152 if (loadimgs == 1) {load_images();}
153
154 // ENABLE KEYPRESS REPEAT:
155 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
156
157 // START THE GAME MENU:
158 menu();
159
160 // SET WINDOW SIZE:
161 window_width = (boardx * 12) + 8;
162 window_height = (boardy*20) + 80;
163
164
165 timercounterx = window_width - 91;
166 timercountery = window_height - 48;
167 flagcounterx = 15;
168 flagcountery = window_height - 48;
169 smileyposy = window_height - 75;
170 smileyposx = (window_width / 2) - 40;
171
172
173 // START GAME:
174 screen = SDL_SetVideoMode(window_width,window_height , 24, SDL_SWSURFACE);
175 if ( screen == NULL ) {
176 fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
177 exit(1);
178 }
179
180
181 // DRAW BAR:
182 ShowBMP(Ibar, screen, 0,(boardy*20));
183 ShowBMP(Ibar2, screen, window_width - 112,(boardy*20));
184
185 // START A NEW GAME:
186 newgame();
187
188
189 SDL_Event event;
190
191
192 int x,y,x2,y2,mx,my,i,j;
193
194 x2 = -1;
195
196 while (out == 1) {
197 SDL_Delay(1);
198
199 // TIMER:
200 if (timer != 0 && gamestatus == 0 && ((SDL_GetTicks() - timer) / 1000) < 1000) {
201 drawcounter((SDL_GetTicks() - timer) / 1000,timercounterx,timercountery);}
202
203 //while (SDL_WaitEvent(&event) != 0) {
204 if (SDL_PollEvent(&event)) {
205
206 switch (event.type) {
207
208 // KEYBOARD:
209 case SDL_KEYDOWN:
210
211 switch (event.key.keysym.sym) {
212 case SDLK_RIGHT:
213 if (firstclick == 1 && mines < ((boardx * boardy) * minesprecents)-1) {
214 mines++;
215 flags = mines;
216 drawcounter(flags,flagcounterx,flagcountery);
217 }
218 break;
219 case SDLK_LEFT:
220 if (firstclick == 1 && mines > 1) {
221 mines--;
222 flags = mines;
223 drawcounter(flags,flagcounterx,flagcountery);
224 }
225 break;
226 case SDLK_ESCAPE:
227 SDL_ShowCursor(SDL_ENABLE); main(0,0);
228 break;
229 case SDLK_F1: if (firstclick == 0 && gamestatus == 0) {solve();}
230 break;
231 }
232
233 break; // end case SDL_KEYDOWN
234
235 // MOUSE MOVEMENT:
236 case SDL_MOUSEMOTION:
237 SDL_GetMouseState(&mouse_x, &mouse_y);
238 x = mouse_x / 12;
239 y = mouse_y / 20;
240
241 if ((mouse_y >= smileyposy && mouse_y <= smileyposy+71) && (mouse_x >= smileyposx && mouse_x <= smileyposx+81)) {ShowBMP(Ismile4, screen, smileyposx,smileyposy);} else {
242
243 switch (gamestatus) {
244 case 0: ShowBMP(Ismile, screen, smileyposx,smileyposy); break;
245 case 1: ShowBMP(Ismile3, screen, smileyposx,smileyposy); break;
246 case 2: ShowBMP(Ismile2, screen, smileyposx,smileyposy); break;
247 }
248
249 }
250
251
252 if ((x < boardx) && (y < boardy) && (gamestatus == 0)) {
253 SDL_ShowCursor(SDL_DISABLE);
254 if (x2 != -1){drawb(board[x2][y2], x2, y2);}
255 drawb(16, x, y);
256 x2 = x;
257 y2 = y;
258 }
259
260 else {
261 if (x2 != -1){drawb(board[x2][y2], x2, y2);}
262 SDL_ShowCursor(SDL_ENABLE);
263 }
264
265
266
267 break; // end case SDL_MOUSEMOTION
268
269 // MOUSE CLICKS:
270
271 case SDL_MOUSEBUTTONDOWN:
272 SDL_GetMouseState(&mouse_x, &mouse_y);
273 mx = mouse_x / 12;
274 my = mouse_y / 20;
275
276
277
278 if ((mx < boardx) && (my < boardy)) { // WHEN YOU CLICK ON THE BOARD:
279
280 // CHECK IF THIS IS THE FIRST CLICK OR NOT FOR GENERATING MINES ONLY AFTER THE FIRST CLICK IS MADE.
281 if ((firstclick == 1) && (event.button.button != SDL_BUTTON_MIDDLE) && (event.button.button != SDL_BUTTON_WHEELUP)
282 && (event.button.button != SDL_BUTTON_WHEELDOWN))
283 {
284 genrandmines(mx,my);
285 // start the timer:
286 timer = SDL_GetTicks();
287 firstclick = 0;
288 }
289
290 switch (event.button.button) { // CHECK WHICH MOUSE KEY WAS PRESSED:
291
292 case SDL_BUTTON_RIGHT: // RIGHT BUTTON:
293
294 if (gamestatus == 0) {
295 if (board2[mx][my] == 0 && flags != -999) {
296 board2[mx][my] = 2;
297 drawb(123, mx, my);
298 flags = flags - 1; drawcounter(flags,flagcounterx,flagcountery);
299 } else {
300 if (board2[mx][my] == 2) {
301 board2[mx][my] = 3;
302 drawb(123, mx, my);
303 flags = flags + 1; drawcounter(flags,flagcounterx,flagcountery);
304 } else {
305 if (board2[mx][my] == 3) {
306 board2[mx][my] = 0;
307 drawb(123, mx, my);}
308 } }
309 }
310 break; // end case SDL_BUTTON_RIGHT
311
312 case SDL_BUTTON_LEFT: // LEFT BUTTON:
313
314 if (((board2[mx][my] == 0) || (board2[mx][my] == 3)) && (gamestatus == 0)){
315
316
317
318 if (board[mx][my] == 14) {
319 // GAME OVER!
320 board2[mx][my] = 1;
321 board[mx][my] = 13;
322 drawb(board[mx][my], mx, my);
323 gameover();
324 } else {
325 if (board[mx][my] == 0) {
326 board2[mx][my] = 1;
327 drawb(board[mx][my], mx, my);
328 showarea(mx,my);
329 } else {
330 board2[mx][my] = 1;
331 drawb(board[mx][my], mx, my);
332 }}
333
334 }
335
336 break; // end case SDL_BUTTON_LEFT
337
338 case SDL_BUTTON_MIDDLE: // MIDDLE BUTTON:
339
340 if ((board[mx][my] == countsomething(mx,my,2,board2)) && (board[mx][my] >= 1 && board[mx][my] <= 12) && (board2[mx][my] == 1))
341 {middleclick(mx,my);}
342
343
344
345 break; // end case SDL_BUTTON_MIDDLE
346
347 } // end switch
348
349
350 if (gamestatus == 0){drawb(16, x, y);}
351
352 } else { // WHEN YOU CLICK OUTSIDE THE BOARD:
353
354 switch (event.button.button) {
355 case SDL_BUTTON_WHEELUP: // WHEELUP BUTTON:
356 if (firstclick == 1 && mines < ((boardx * boardy) * minesprecents)-1) {
357 mines++;
358 flags = mines;
359 drawcounter(flags,flagcounterx,flagcountery);
360 }
361 break; // end case SDL_BUTTON_WHEELUP
362
363 case SDL_BUTTON_WHEELDOWN: // WHEELDOWN BUTTON:
364 if (firstclick == 1 && mines > 1) {
365 mines--;
366 flags = mines;
367 drawcounter(flags,flagcounterx,flagcountery);
368 }
369 break; // end case SDL_BUTTON_WHEELDOWN
370
371 // LEFT BUTTON (OUTSIDE THE BOARD):
372 case SDL_BUTTON_LEFT: if ((mouse_y >= smileyposy && mouse_y <= smileyposy+71) && (mouse_x >= smileyposx && mouse_x <= smileyposx+81)) {newgame();} break;
373 }
374 }
375
376 // CHECK WINING:
377 if (checkwining() == 1){won();}
378
379
380 break; // end case SDL_MOUSEBUTTONDOWN
381
382 case SDL_QUIT: SDL_ShowCursor(SDL_ENABLE); main(0,0);
383
384 } // end switch (event.type)
385
386 } }// end whiles
387
388 atexit(SDL_Quit);
389
390 }
391