Skip to content

Commit fb0a57f

Browse files
committed
Simon:
-Mouse interaction (for windows) is wwworking -Game logic finish -Started research on linux interactions TO DO: Put sounds, tutorial and thanks
1 parent 56231ba commit fb0a57f

File tree

7 files changed

+186
-69
lines changed

7 files changed

+186
-69
lines changed

Simon/Simon.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<ClCompile Include="ansi_escapes.c" />
23+
<ClCompile Include="linuxMouseInteraction.c" />
2324
<ClCompile Include="simon.c" />
2425
<ClCompile Include="windowsConsoleInteraction.c" />
2526
</ItemGroup>
2627
<ItemGroup>
2728
<ClInclude Include="ansi_escapes.h" />
2829
<ClInclude Include="linuxMouseInteraction.h" />
30+
<ClInclude Include="windowsConsoleInteraction.h" />
2931
</ItemGroup>
3032
<PropertyGroup Label="Globals">
3133
<VCProjectVersion>16.0</VCProjectVersion>

Simon/Simon.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClCompile Include="windowsConsoleInteraction.c">
2525
<Filter>Arquivos de Origem</Filter>
2626
</ClCompile>
27+
<ClCompile Include="linuxMouseInteraction.c">
28+
<Filter>Arquivos de Origem</Filter>
29+
</ClCompile>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ClInclude Include="ansi_escapes.h">
@@ -32,5 +35,8 @@
3235
<ClInclude Include="linuxMouseInteraction.h">
3336
<Filter>Arquivos de Cabeçalho</Filter>
3437
</ClInclude>
38+
<ClInclude Include="windowsConsoleInteraction.h">
39+
<Filter>Arquivos de Cabeçalho</Filter>
40+
</ClInclude>
3541
</ItemGroup>
3642
</Project>

Simon/linuxMouseInteraction.c

Whitespace-only changes.

Simon/linuxMouseInteraction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef LINUX_MOUSE_INTERACTION
22
#define LINUX_MOUSE_INTERACTION
33

4+
#include <curses.h>
45

56

67
#endif // !LINUX_MOUSE_INTERACTION

Simon/simon.c

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <stdbool.h>
44
#include <time.h>
55
#include "ansi_escapes.h"
6-
#include "windowsMouseInteraction.h"
6+
#include "windowsConsoleInteraction.h"
7+
8+
extern EVENT eventMain(VOID);
79

810

911
extern void setupConsole(void);
@@ -21,6 +23,17 @@ typedef enum ColorButtons {
2123
BLUE
2224
}Color;
2325

26+
void delay(int milliseconds)
27+
{
28+
long pause;
29+
clock_t now, then;
30+
31+
pause = milliseconds * (CLOCKS_PER_SEC / 1000);
32+
now = then = clock();
33+
while ((now - then) < pause)
34+
now = clock();
35+
}
36+
2437
void display(int button, int screenSize[2])
2538
{
2639
setupConsole();
@@ -74,18 +87,42 @@ void display(int button, int screenSize[2])
7487
printf("\n");
7588
}
7689

77-
void rng(int* sequence, int level)
90+
int coordinatesToButton(COORD click, int windowSize[2])
91+
{
92+
if (click.Y < windowSize[0] / 2)
93+
{
94+
if (click.X < windowSize[1] / 2)
95+
return GREEN;
96+
if (click.X > windowSize[1] / 2)
97+
return RED;
98+
}
99+
100+
if (click.Y > windowSize[0] / 2)
101+
{
102+
if (click.X < windowSize[1] / 2)
103+
return YELLOW;
104+
if (click.X > windowSize[1] / 2)
105+
return BLUE;
106+
}
107+
return 0;
108+
}
109+
110+
void rng(sshort* sequence, int level)
78111
{
79112
sequence[level - 1] = 1 + rand() % 4;
113+
sequence[level] = 0;
80114
}
81115

82116
int main(int argc, char** argv)
83117
{
84118
int level = 1;
85119
int windowSize[2];
86120
sshort sequence[4000];
87-
sshort mouseCoordinates[2];
121+
sshort input[4000];
122+
COORD mouseCoord;
123+
EVENT retEvent;
88124
char trashcan[10];
125+
bool mistake = false;
89126

90127
srand(time(NULL));
91128

@@ -101,18 +138,58 @@ int main(int argc, char** argv)
101138
{
102139
for (bool replay = true; replay == true;)
103140
{
104-
setupConsole();
105-
moveTo(999, 999);
106-
getCursorPosition(&windowSize[0], &windowSize[1]);
107-
clearScreenToTop();
108-
restoreConsoleMode();
109-
display(NO_COLOR, windowSize);
110-
111-
mouseClick(&mouseCoordinates[0], &mouseCoordinates[1]);
112-
printf("%i %i", mouseCoordinates[0], mouseCoordinates[1]);
113-
114-
resetColor();
115-
break;
141+
level = 1;
142+
while (mistake == false)
143+
{
144+
setupConsole();
145+
moveTo(999, 999);
146+
getCursorPosition(&windowSize[0], &windowSize[1]);
147+
clearScreenToTop();
148+
restoreConsoleMode();
149+
display(NO_COLOR, windowSize);
150+
151+
rng(sequence, level);
152+
153+
for (int count = 0; count < level; count++)
154+
{
155+
delay(500);
156+
display(sequence[count], windowSize);
157+
display(NO_COLOR, windowSize);
158+
}
159+
160+
for (int count = 0; count < level; count++)
161+
{
162+
display(NO_COLOR, windowSize);
163+
while (true)
164+
{
165+
retEvent.event.mouseEvent = 0xc00;
166+
retEvent = eventMain();
167+
mouseCoord = retEvent.event.mouseCoord;
168+
if (retEvent.event.mouseEvent == FROM_LEFT_1ST_BUTTON_PRESSED)
169+
break;
170+
}
171+
172+
input[count] = coordinatesToButton(mouseCoord, windowSize);
173+
174+
display(input[count], windowSize);
175+
176+
if (sequence[count] != input[count])
177+
{
178+
mistake = true;
179+
break;
180+
}
181+
}
182+
183+
if (mistake == true)
184+
{
185+
//play error
186+
puts("Not right! Try again? (Y/N)");
187+
fgets(trashcan, 5, stdin);
188+
if (trashcan[0] == 'n' || trashcan[0] == 'N')
189+
replay = false;
190+
}
191+
level++;
192+
}
116193
}
117194
}
118195

Simon/windowsConsoleInteraction.c

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
#include <windows.h>
2-
#include <stdio.h>
3-
4-
//https://stackoverflow.com/questions/35797336/how-to-find-the-mouse-button-click-position-x-and-y*/
1+
#include "windowsConsoleInteraction.h"
52

63
HANDLE hStdin;
74
DWORD fdwSaveOldMode;
5+
EVENT retEvent;
86

97
VOID ErrorExit(LPSTR);
108
VOID KeyEventProc(KEY_EVENT_RECORD);
119
VOID MouseEventProc(MOUSE_EVENT_RECORD);
1210
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
1311

14-
int main(VOID)
12+
EVENT eventMain(VOID)
1513
{
16-
DWORD cNumRead, fdwMode, i;
14+
DWORD cNumRead, fdwMode, i;
1715
INPUT_RECORD irInBuf[128];
1816
int counter = 0;
1917

@@ -31,58 +29,54 @@ int main(VOID)
3129
// Enable the window and mouse input events.
3230

3331
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
32+
fdwMode &= ~ENABLE_QUICK_EDIT_MODE;
33+
SetConsoleMode(hStdin, fdwMode | ENABLE_EXTENDED_FLAGS);
3434
if (!SetConsoleMode(hStdin, fdwMode))
3535
ErrorExit("SetConsoleMode");
3636

37-
// Loop to read and handle the next 100 input events.
38-
39-
while (counter++ <= 100)
40-
{
41-
// Wait for the events.
37+
// Wait for the events.
4238

43-
if (!ReadConsoleInput(
44-
hStdin, // input buffer handle
45-
irInBuf, // buffer to read into
46-
128, // size of read buffer
47-
&cNumRead)) // number of records read
48-
ErrorExit("ReadConsoleInput");
39+
if (!ReadConsoleInput(
40+
hStdin, // input buffer handle
41+
irInBuf, // buffer to read into
42+
128, // size of read buffer
43+
&cNumRead)) // number of records read
44+
ErrorExit("ReadConsoleInput");
4945

50-
// Dispatch the events to the appropriate handler.
46+
// Dispatch the events to the appropriate handler.
5147

52-
for (i = 0; i < cNumRead; i++)
53-
{
54-
switch (irInBuf[i].EventType)
55-
{
56-
case KEY_EVENT: // keyboard input
57-
KeyEventProc(irInBuf[i].Event.KeyEvent);
58-
break;
48+
i = 0;
49+
switch (irInBuf[i].EventType)
50+
{
51+
case KEY_EVENT: // keyboard input
52+
KeyEventProc(irInBuf[i].Event.KeyEvent);
53+
break;
5954

60-
case MOUSE_EVENT: // mouse input
61-
MouseEventProc(irInBuf[i].Event.MouseEvent);
62-
break;
55+
case MOUSE_EVENT: // mouse input
56+
MouseEventProc(irInBuf[i].Event.MouseEvent);
57+
break;
6358

64-
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
65-
ResizeEventProc(irInBuf[i].Event.WindowBufferSizeEvent);
66-
break;
59+
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
60+
ResizeEventProc(irInBuf[i].Event.WindowBufferSizeEvent);
61+
break;
6762

68-
case FOCUS_EVENT: // disregard focus events
63+
case FOCUS_EVENT: // disregard focus events
6964

70-
case MENU_EVENT: // disregard menu events
71-
break;
65+
case MENU_EVENT: // disregard menu events
66+
break;
7267

73-
default:
74-
ErrorExit("Unknown event type");
75-
break;
76-
}
77-
}
68+
default:
69+
ErrorExit("Unknown event type");
70+
break;
7871
}
7972

8073
// Restore input mode on exit.
8174

8275
SetConsoleMode(hStdin, fdwSaveOldMode);
8376

84-
return 0;
77+
return retEvent;
8578
}
79+
8680
VOID ErrorExit(LPSTR lpszMessage)
8781
{
8882
fprintf(stderr, "%s\n", lpszMessage);
@@ -96,54 +90,66 @@ VOID ErrorExit(LPSTR lpszMessage)
9690

9791
VOID KeyEventProc(KEY_EVENT_RECORD ker)
9892
{
99-
printf("Key event: ");
93+
/*printf("Key event: ");
10094
if (ker.bKeyDown)
10195
printf("key pressed\n");
102-
else printf("key released\n");
96+
else printf("key released\n");*/
97+
98+
retEvent.regEvent = _KEY_EVENT;
99+
retEvent.event.keyPress = ker.uChar.AsciiChar;
103100
}
104101
VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
105102
{
106103
#ifndef MOUSE_HWHEELED
107104
#define MOUSE_HWHEELED 0x0008
108105
#endif
109-
printf("Mouse event: ");
106+
//printf("Mouse event: ");
110107

111108
switch (mer.dwEventFlags)
112109
{
113110
case 0:
114-
111+
retEvent.regEvent = _MOUSE_EVENT;
115112
if (mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
116113
{
117-
printf("left button press \n");
114+
//printf("left button press \n");
115+
retEvent.event.mouseEvent = mer.dwButtonState;
116+
retEvent.event.mouseCoord = mer.dwMousePosition;
118117
}
119118
else if (mer.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
120119
{
121-
printf("right button press \n");
120+
//printf("right button press \n");
121+
retEvent.event.mouseEvent = mer.dwButtonState;
122122
}
123123
else
124124
{
125-
printf("button press\n");
125+
//printf("button press\n");
126+
retEvent.event.mouseEvent = mer.dwButtonState;
126127
}
127128
break;
128129
case DOUBLE_CLICK:
129-
printf("double click\n");
130+
//printf("double click\n");
131+
retEvent.event.mouseEvent = mer.dwButtonState;
130132
break;
131133
case MOUSE_HWHEELED:
132-
printf("horizontal mouse wheel\n");
134+
//printf("horizontal mouse wheel\n");
135+
retEvent.event.mouseEvent = mer.dwButtonState;
133136
break;
134137
case MOUSE_MOVED:
135-
printf("mouse moved\n");
138+
//printf("mouse moved\n");
139+
retEvent.event.mouseEvent = mer.dwButtonState;
136140
break;
137141
case MOUSE_WHEELED:
138-
printf("vertical mouse wheel\n");
142+
//printf("vertical mouse wheel\n");
143+
retEvent.event.mouseEvent = mer.dwButtonState;
139144
break;
140145
default:
141-
printf("unknown\n");
146+
//printf("unknown\n");
147+
retEvent.event.mouseEvent = mer.dwButtonState;
142148
break;
143149
}
144150
}
145151
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
146152
{
147-
printf("Resize event\n");
148-
printf("Console screen buffer is %d columns by %d rows.\n", wbsr.dwSize.X, wbsr.dwSize.Y);
153+
//printf("Resize event\n");
154+
//printf("Console screen buffer is %d columns by %d rows.\n", wbsr.dwSize.X, wbsr.dwSize.Y);
149155
}

0 commit comments

Comments
 (0)