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
|
// Osman Zakir
// 5 / 3 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Drill
// Drill Specifications:
/**
* 1. Make an 800-by-1000 Simple_window.
* 2. Put an 8-by-8 grid on the leftmost 800-by-800 part of that window (so that each square is 100 by
* 100).
* 3. Make the eight squares on the diagonal starting from the top left corner red (use Rectangle).
* 4. Find a 200-by-200-pixel image (JPEG or GIF) and place three copies of it on the grid (each
* image covering four squares). If you can’t find an image that is exactly 200 by 200, use
* set_mask() to pick a 200-by-200 section of a larger image. Don’t obscure the red squares.
* 5. Add a 100-by-100 image. Have it move around from square to square when you click the “Next”
* button. Just put wait_for_button() in a loop with some code that picks a new square for your
* image.
*/
#include "../../cust_std_lib_facilities.h"
#include "../../Simple_window.h"
#include "../../Graph.h"
int main()
{
using namespace Graph_lib;
constexpr int win_x = 100, win_y = 100;
const Point top_left{ win_x, win_y };
constexpr int win_width = 800, win_height = 1000;
Simple_window win{ top_left, win_width, win_height, "grid-image drill" };
Lines grid;
constexpr int col_count = 8;
constexpr int row_count = 8;
constexpr int row_height = 100;
constexpr int col_width = 100;
constexpr int grid_width = col_count * col_width;
constexpr int grid_height = row_height * row_count;
for (int x = 0; x < grid_width; x += col_width)
{
for (int y = 0; y < grid_height; y += row_height)
{
grid.add(Point{ x, 0 }, Point{ x, grid_height });
grid.add(Point{ 0, y }, Point{ grid_width, y });
}
}
grid.set_color(Color::black);
win.attach(grid);
constexpr int rect_width = 100;
constexpr int rect_height = 100;
constexpr int num_rects = 8;
Vector_ref<Graph_lib::Rectangle> rect_vr;
for (int i = 0, x = 0, y = 0; i < num_rects; ++i, x += rect_width, y += rect_height)
{
rect_vr.push_back(new Graph_lib::Rectangle{ Point{ x, y }, rect_width, rect_height });
rect_vr[i].set_color(Color::black);
rect_vr[i].set_fill_color(Color::red);
win.attach(rect_vr[i]);
}
Image rock1{ Point{500, 0}, "20051121_Peace_rock_th200.jpg" };
win.attach(rock1);
Image rock2{ Point{200, 0}, "20051121_Peace_rock_th200.jpg" };
win.attach(rock2);
Image rock3{ Point{200, 500}, "20051121_Peace_rock_th200.jpg" };
win.attach(rock3);
Image ichigo_flying{ Point{0, 0}, "ichigo-flying.gif" };
win.attach(ichigo_flying);
int current_row = 0, current_col = 0;
while (current_row != win.y_max() && current_col != win.x_max())
{
if (win.wait_for_button())
{
if (current_row != win.y_max() - 1)
{
current_row++;
ichigo_flying.move(current_row, current_col);
if (current_row == win.x_max())
{
current_row = 0;
ichigo_flying.move(current_row, current_col);
}
}
else if (current_col != win.x_max() - 1)
{
current_col++;
ichigo_flying.move(current_row, current_col);
if (current_col == win.x_max())
{
current_col = 0;
ichigo_flying.move(current_row, current_col);
}
}
ichigo_flying.move(current_row, current_col);
}
}
win.wait_for_button();
}
|