/
draw.cpp
226 lines (198 loc) · 10 KB
/
draw.cpp
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
// draw.cpp
// graphics for mancala
// Copyright Matthew Chandler 2014
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <gtkmm/drawingarea.h>
#include <gdkmm/general.h>
#include <glibmm/fileutils.h>
#include <glibmm/main.h>
#include "config.h"
#include "draw.h"
// pixel data to be used as fallback image
const int fallback_h = 16;
const int fallback_w = 16;
unsigned char fallback_data[fallback_h * fallback_w * 4];
Glib::RefPtr<Gdk::Pixbuf> pixbuf_create_from_file_fallback(const char * filename, const Glib::RefPtr<Gdk::Pixbuf> & fallback)
{
Glib::RefPtr<Gdk::Pixbuf> ret;
try
{
ret = Gdk::Pixbuf::create_from_file(filename);
}
catch(const Glib::FileError& ex)
{
std::cerr<<"File Error: "<<ex.what()<<std::endl;
ret = fallback;
}
catch(const Gdk::PixbufError& ex)
{
std::cerr<<"Pixbuf Error: "<< ex.what()<<std::endl;
ret = fallback;
}
return ret;
}
namespace Mancala
{
Draw::Draw(const int Num_bowls, const int Num_beads, const int Ai_depth,
const bool Extra_rule, const bool Capture_rule, const bool Collect_rule):
hint_player(Mancala::PLAYER_1),
show_hint(false),
hint_i(0),
b(Num_bowls, Num_beads, Ai_depth, Extra_rule, Capture_rule, Collect_rule)
{
// signal on mouse click
add_events(Gdk::BUTTON_PRESS_MASK);
// create a fallback image to be used in place of missing files
// it is a black and magenta checkerboard
for(int row = 0; row < fallback_h; ++row)
{
for(int col = 0; col < fallback_w; ++col)
{
int i = (row * fallback_w + col) * 4;
fallback_data[i] = fallback_data[i + 2] = ((row & 1) == (col & 1))? 0 : 255;
fallback_data[i + 1] = 0;
fallback_data[i + 3] = 192;
}
}
Glib::RefPtr<Gdk::Pixbuf> fallback_img = Gdk::Pixbuf::create_from_data(fallback_data, Gdk::COLORSPACE_RGB,
true, 8, fallback_w, fallback_h, fallback_w * 4);
// load images from disk
bg_store = pixbuf_create_from_file_fallback(check_in_pwd("img/bg_store.png").c_str(), fallback_img);
bg_bowl = pixbuf_create_from_file_fallback(check_in_pwd("img/bg_bowl.png").c_str(), fallback_img);
bg_board = pixbuf_create_from_file_fallback(check_in_pwd("img/bg_board.png").c_str(), fallback_img);
hint_img = pixbuf_create_from_file_fallback(check_in_pwd("img/hint.png").c_str(), fallback_img);
bead_s_img = pixbuf_create_from_file_fallback(check_in_pwd("img/bead_s.png").c_str(), fallback_img);
bead_imgs.push_back(pixbuf_create_from_file_fallback(check_in_pwd("img/bead_red.png").c_str(), fallback_img));
bead_imgs.push_back(pixbuf_create_from_file_fallback(check_in_pwd("img/bead_green.png").c_str(), fallback_img));
bead_imgs.push_back(pixbuf_create_from_file_fallback(check_in_pwd("img/bead_blue.png").c_str(), fallback_img));
bead_imgs.push_back(pixbuf_create_from_file_fallback(check_in_pwd("img/bead_yellow.png").c_str(), fallback_img));
bead_imgs.push_back(pixbuf_create_from_file_fallback(check_in_pwd("img/bead_magenta.png").c_str(), fallback_img));
bead_imgs.push_back(pixbuf_create_from_file_fallback(check_in_pwd("img/bead_cyan.png").c_str(), fallback_img));
}
// helper function to draw an image
void Draw::draw_img(const Cairo::RefPtr<Cairo::Context>& cr, const Glib::RefPtr<Gdk::Pixbuf>& img,
const double translate_x, const double translate_y, const double scale_x, const double scale_y)
{
cr->save();
cr->translate(translate_x, translate_y);
cr->scale(scale_x, scale_y);
Gdk::Cairo::set_source_pixbuf(cr, img);
cr->paint();
cr->restore();
}
// helper function to draw a number
void Draw::draw_num(const Cairo::RefPtr<Cairo::Context>& cr, const Pango::FontDescription & font,
const int num, const double x, const double y)
{
std::ostringstream str;
int txt_w, txt_h;
str<<num;
Glib::RefPtr<Pango::Layout> txt = create_pango_layout(str.str());
txt->set_font_description(font);
txt->get_pixel_size(txt_w, txt_h);
cr->move_to(x - .5 * txt_w, y - .5 * txt_h);
txt->show_in_cairo_context(cr);
}
// main drawing routine
bool Draw::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
int num_cells = b.num_bowls + 2;
double inv_num_cells = 1.0 / num_cells;
Gtk::Allocation alloc = get_allocation();
Pango::FontDescription font("Monospace");
font.set_size(std::min(alloc.get_width(), alloc.get_height()) * .1 * Pango::SCALE);
cr->set_source_rgb(0.0, 0.0, 0.0);
// draw bg
draw_img(cr, bg_board, 0.0, 0.0, alloc.get_width() / (bg_board->get_width() - .5),
alloc.get_height() / (bg_board->get_height() - .5));
// draw bg for left store
draw_img(cr, bg_store, 0.0, 0.0, alloc.get_width() / (bg_store->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bg_store->get_height() - .5));
// l store beads
for(auto & j: b.l_store.beads)
{
draw_img(cr, bead_imgs[j.color_i], alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_imgs[j.color_i]->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_imgs[j.color_i]->get_height() - .5) * .5);
draw_img(cr, bead_s_img, alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_s_img->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_s_img->get_height() - .5) * .5);
}
// draw # for left store
draw_num(cr, font, b.l_store.beads.size(), alloc.get_width() * .5 * inv_num_cells,
alloc.get_height() * .5);
// draw bg for right store
draw_img(cr, bg_store, alloc.get_width() * (1.0 - inv_num_cells), 0,
alloc.get_width() / (bg_store->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bg_store->get_height() - .5));
// r store beads
for(auto & j: b.r_store.beads)
{
draw_img(cr, bead_imgs[j.color_i], alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_imgs[j.color_i]->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_imgs[j.color_i]->get_height() - .5) * .5);
draw_img(cr, bead_s_img, alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_s_img->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_s_img->get_height() - .5) * .5);
}
// draw # for right store
draw_num(cr, font, b.r_store.beads.size(), alloc.get_width() * (1.0 - .5 * inv_num_cells),
alloc.get_height() * .5);
// draw bowls
for(size_t i = 0; i < b.top_row.size(); ++i)
{
// draw upper row hint
if(hint_player == PLAYER_2 && show_hint && hint_i == (int)i)
{
draw_img(cr, hint_img, alloc.get_width() * (i + 1) * inv_num_cells, 0.0,
alloc.get_width() / (hint_img->get_width() - .5) * inv_num_cells,
alloc.get_height() / (hint_img->get_height() - .5) * .5);
}
// upper row bgs
draw_img(cr, bg_bowl, alloc.get_width() * (i + 1) * inv_num_cells, 0,
alloc.get_width() / (bg_bowl->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bg_bowl->get_height() - .5) * .5);
// upper row beads
for(auto & j: b.top_row[i].beads)
{
draw_img(cr, bead_imgs[j.color_i], alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_imgs[j.color_i]->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_imgs[j.color_i]->get_height() - .5) * .5);
draw_img(cr, bead_s_img, alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_s_img->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_s_img->get_height() - .5) * .5);
}
// upper row txt
draw_num(cr, font, b.top_row[i].beads.size(), alloc.get_width() * (2 * i + 3) * .5 * inv_num_cells,
alloc.get_height() * .25);
// draw lower row hint
if(hint_player == PLAYER_1 && show_hint && hint_i == (int)i)
{
draw_img(cr, hint_img, alloc.get_width() * (i + 1) * inv_num_cells, .5 * alloc.get_height(),
alloc.get_width() / (hint_img->get_width() - .5) * inv_num_cells,
alloc.get_height() / (hint_img->get_height() - .5) * .5);
}
// lower row bgs
draw_img(cr, bg_bowl, alloc.get_width() * (i + 1) * inv_num_cells, .5 * alloc.get_height(),
alloc.get_width() / (bg_bowl->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bg_bowl->get_height() - .5) * .5);
// lower row beads
for(auto & j: b.bottom_row[i].beads)
{
draw_img(cr, bead_imgs[j.color_i], alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_imgs[j.color_i]->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_imgs[j.color_i]->get_height() - .5) * .5);
draw_img(cr, bead_s_img, alloc.get_width() * j.pos[0], alloc.get_height() * j.pos[1],
alloc.get_width() / (bead_s_img->get_width() - .5) * inv_num_cells,
alloc.get_height() / (bead_s_img->get_height() - .5) * .5);
}
// lower row txt
draw_num(cr, font, b.bottom_row[i].beads.size(), alloc.get_width() * (2 * i + 3) * .5 * inv_num_cells,
alloc.get_height() * .75);
}
return true;
}
}