-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxoox.c
executable file
·209 lines (187 loc) · 5.58 KB
/
xoox.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
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
/********************************************************
* @author Airead Fan <[email protected]> *
* @date 2011 9月 19 21:26:48 CST *
********************************************************
* after studying C 63 days *
* after studying APUE 28 days *
********************************************************/
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <stdbool.h>
#include "xoox.h"
#include "button.h"
#include "timer.h"
#include "judgement.h"
#define TEST 1
int main(int argc, int *argv[])
{
bool quit = false; /* quit flag */
int i, j;
int play_stat = 0;
Timer fps; /* cap frame rate */
SDL_Event event;
SDL_Surface *mouseother;
SDL_Surface *wintitle[3];
SDL_Surface *screen; /* main screen */
SDL_Surface *chessboard; /* chess board */
Button pieces[CHESSBOARD_ROW][CHESSBOARD_COLUMN] = {{0}};
SDL_Surface *piece_stat_img[PIECE_STAT + 1] = {NULL}; /* pieces image */
int mouse_map[CHESSBOARD_ROW][CHESSBOARD_COLUMN] = {{0}}; /* click or not */
int pieces_map[CHESSBOARD_ROW][CHESSBOARD_COLUMN] = {{0}}; /* pieces state */
/* Init SDL lib*/
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
return 1;
}
/* Init screen */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(screen == NULL){
return 1;
}
SDL_WM_SetCaption("XO_OX", NULL);
/* Load resource */
piece_stat_img[1] = load_image("xooxres/chess1.png");
piece_stat_img[2] = load_image("xooxres/chess2.png");
piece_stat_img[3] = piece_stat_img[1]; /* for special show, but no time to do */
piece_stat_img[4] = piece_stat_img[2]; /* for special show, but no time to do */
chessboard = load_image("xooxres/chessboard.png");
mouseother = load_image("xooxres/mouseother.png");
wintitle[0] = load_image("xooxres/wintitle1.png");
wintitle[1] = load_image("xooxres/wintitle2.png");
wintitle[2] = load_image("xooxres/wintitle3.png");
/* Init timer */
timer_init(&fps);
/* Init button */
for(i = 0; i < CHESSBOARD_ROW; i++){
for(j = 0; j < CHESSBOARD_COLUMN; j++){
button_init(&pieces[i][j], SUBBOARD_WIDTH * i, SUBBOARD_HEIGHT * j, SUBBOARD_WIDTH , SUBBOARD_HEIGHT );
button_set_stat_img(&pieces[i][j], BUTTON_MOUSEOVER, NULL);
button_set_stat_img(&pieces[i][j], BUTTON_MOUSEOUT, mouseother);
button_set_stat_img(&pieces[i][j], BUTTON_MOUSEUP, mouseother);
button_set_stat_img(&pieces[i][j], BUTTON_MOUSEDOWN, mouseother);
}
}
/* Main loop */
while(quit == false){
/* timer start */
timer_start(&fps);
while(SDL_PollEvent(&event)){
/* Pieces input handle */
pieces_handle_event(pieces, mouse_map, &event);
/* User quit */
if(event.type == SDL_QUIT){
quit = true;
}
}
/* logic function */
play_stat = mouse_to_piece_map(mouse_map, pieces_map);
/* show chessboard */
subchessboard_show(piece_stat_img[get_leader()], mouseother, mouse_map, screen);
/* show pieces */
pieces_show(piece_stat_img, pieces_map, screen);
/* */
if(play_stat != 0){
printf("play_stat = %d\n", play_stat);
apply_surface((SCREEN_WIDTH - wintitle[play_stat - 1]->w) / 2, (SCREEN_HEIGHT - wintitle[play_stat - 1]->h) / 2, wintitle[play_stat - 1], screen);
}
/* Update screen */
if(SDL_Flip(screen) == -1){
return 1;
}
if(play_stat != 0){
SDL_Delay(3000);
quit = true;
}
/* cap frame rate */
if(timer_get_ticks(&fps) < 1000 / FRAME_PER_SECOND){
SDL_Delay((1000 / FRAME_PER_SECOND) - timer_get_ticks(&fps));
}
}
/* Free resource */
SDL_FreeSurface(chessboard);
SDL_FreeSurface(mouseother);
for(i = 0; i < 2; i++){
SDL_FreeSurface(piece_stat_img[i]);
}
for(i = 0; i < 3; i++){
SDL_FreeSurface(wintitle[i]);
}
SDL_Quit();
return 0;
}
/*
* Load a image to a temporary image, optimized the image
* and free the temporary image.
*
*/
SDL_Surface *load_image(char *filename)
{
SDL_Surface *img_tmp = NULL;
SDL_Surface *img_opt = NULL;
Uint32 colork = 0;
img_tmp = IMG_Load(filename);
if(img_tmp != NULL){
img_opt = SDL_DisplayFormat(img_tmp);
SDL_FreeSurface(img_tmp);
if(img_opt != NULL){
colork = SDL_MapRGB(img_opt->format, 0xFF, 0xFF, 0xFF);
SDL_SetColorKey(img_opt, SDL_SRCCOLORKEY, colork);
}
}
return img_opt;
}
/*
* Simple apply source to destination at (x, y)
*
*/
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
/*
* every pieces handle event
* get mouse_map
*/
void pieces_handle_event(Button bt[][CHESSBOARD_COLUMN], int mouse_map[][CHESSBOARD_COLUMN], SDL_Event *event)
{
int i, j;
for(i = 0; i < CHESSBOARD_ROW; i++){
for(j = 0; j < CHESSBOARD_COLUMN; j++){
button_handle_event(&bt[i][j], event);
mouse_map[i][j] = bt[i][j].cur_stat;
}
}
}
/*
* show all pieces according to pieces map
*/
void pieces_show(SDL_Surface *piece_stat_img[], int pieces_map[][CHESSBOARD_COLUMN], SDL_Surface *screen)
{
int i, j;
for(i = 0; i < CHESSBOARD_ROW; i++){
for(j = 0; j < CHESSBOARD_COLUMN; j++){
if(pieces_map[i][j] != 0){
apply_surface(i * SUBBOARD_WIDTH + 4, j * SUBBOARD_HEIGHT + 4, piece_stat_img[pieces_map[i][j]], screen);
}
}
}
}
/*
* show chessboard
*/
void subchessboard_show(SDL_Surface *mouseover, SDL_Surface *mouseother, int mouse_map[][CHESSBOARD_COLUMN], SDL_Surface *screen)
{
int i, j;
for(i = 0; i < CHESSBOARD_ROW; i++){
for(j = 0; j < CHESSBOARD_COLUMN; j++){
if(mouse_map[i][j] & MOUSEOVER ){
apply_surface(i * SUBBOARD_WIDTH + 4, j * SUBBOARD_HEIGHT + 4, mouseover, screen);
}else{
apply_surface(i * SUBBOARD_WIDTH , j * SUBBOARD_HEIGHT , mouseother, screen);
}
}
}
}