forked from sqrt-negativeone/KALAH-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKALAH.c
135 lines (127 loc) · 5.26 KB
/
KALAH.c
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
#include <string.h>
#include <stdio.h>
#include "utils.h"
int main(int argc, char** argv){
if (!init()){
printf("could not initialize!\n");
}
else {
//events handler
SDL_Event e;
//main loop flag
bool quit=false;
while (!quit){
//render the menu to display
//SDL_RenderClear(renderer);
if (render){
SDL_RenderClear(renderer);
renderMenu(q->top);
} render=false;
//handel events based on the type of the menu
switch(q->top->type){
// the menus with only buttons
case MAIN_GAME:{
if (api->game->s->is_all_empty){
Menu* menu=createMenu(WINNER);
insert(menu);
render=true;
break;
}
}
case FIRST_FRAME:
case CHOOSE_NUMBER_OF_PIECES:
case HOME_MENU:
case WINNER:{
while (SDL_PollEvent(&e)){
//if quit request
if (e.type==SDL_QUIT) {
quit=true;
}
if (e.type == SDL_MOUSEBUTTONDOWN){
//loop over all the buttons in the menu
for (int i=0 ; i< q->top->numberOfButtons ; i++){
//if the mouse was inside the countainer of the button then activate it
if (isMouseInsideContainer(q->top->buttons[i].container)){
render=true;
handleClick(&(q->top->buttons[i]));
break;
}
}
}
}
break;
}
case LOADING:{
empty();
saveAPI();
Menu* menu=createMenu(MAIN_GAME);
insert(menu);
render=true;
break;
}
default :{ //Menus with input field
SDL_StartTextInput();
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Special key input
else if( e.type == SDL_KEYDOWN )
{
//Handle backspace
if( e.key.keysym.sym == SDLK_BACKSPACE )
{
//lop off character
if (strlen(textInput)==0) continue;
render=true;
textInput=deleteChar(textInput);
}
//Handle copy
else if( e.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
{
SDL_SetClipboardText( textInput );
}
//Handle paste
else if( e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
{
render=true;
textInput = SDL_GetClipboardText();
}
}
//Special text input event
else if( e.type == SDL_TEXTINPUT )
{
//Not copy or pasting
if( !( SDL_GetModState() & KMOD_CTRL && ( e.text.text[ 0 ] == 'c' || e.text.text[ 0 ] == 'C' || e.text.text[ 0 ] == 'v' || e.text.text[ 0 ] == 'V' ) ) )
{
render=true;
//Append character
textInput=addChar(textInput,e.text.text[0]);
}
}
else if (e.type== SDL_MOUSEBUTTONDOWN){
//loop over all the buttons in the menu
for (int i=0 ; i< q->top->numberOfButtons ; i++){
//if the mouse was inside the countainer of the button then activate it
if (isMouseInsideContainer(q->top->buttons[i].container)){
render=true;
handleClick(&(q->top->buttons[i]));
break;
}
}
}
}
SDL_StopTextInput();
}
}
//clear screen
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderPresent( renderer );
}
}
quit();
return 0;
}