Thursday, 10 September 2020

Chapter 16 // Exercise 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 16 // Exercise 5

Write a program that draws a shape of your choice and moves it to a new point each time you click "Next". The new point should be determined by a co-ordinate pair read from an input stream.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2016/Exercise%205


Another straightforward one. I added a next button & action function to the window created in the previous exercise and had it draw a randomly sized/shaped/coloured star. If no co-ordinates are given, it chooses a random one. In In_box::get_int(), it always returns -999999 if no number is given so I plugged that in as no number given.


Chapter 16 // Exercise 5 - Principles & Practice Using C++

Wednesday, 9 September 2020

Chapter 16 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 16 // Exercise 4

Make a menu with items that make a circle, a square, an equilateral triangle, and a hexagon, respectively. Make an input box (or two) for giving a coordinate pair, and place the shape made by pressing a menu item at that coordinate. Sorry, no drag and drop.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2016/Exercise%204


This was tricky because the window constantly calls draw() which appears to be causing problems. When you add a new shape to the window it seems to overwrite the previous one with garbage, causing problems when it comes to redraw the scene.

For some strange reason, I had to make everything a pointer in order for it to work properly. I'm not quite sure why but I have a hunch. Even though I was pushing back instances of Shapes into a vector stored in the window; the previous shapes were going out of scope when redraw() was called. This would explain why the vector suddenly becomes filled with garbage. It knows a Shape used to be there but it doesn't know what. To get round this, I new-ed the shapes up and pushed them into a vector of Shape pointers. This ensures that the shapes cannot go out of scope.

Once again I had one function that handled the button press and used an enum to tell the function what shape to draw.


Chapter 16 // Exercise 4 - Principles & Practice Using C++



Tuesday, 8 September 2020

Chapter 16 // Exercise 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 16 // Exercise 3

Place an Image  on top of a Button; move both when the button is pushed. Use this random number generator from std_lib_facilities.h to pick a new location for the "image button":

#include <random>

inline int randint(int min, int max)
{
    static default_random_engine ran;
    return uniform_int_distribution<>{min, max}(ran);
}

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2016/Exercise%203


This one was quite straightforward. I'm a little surprised at just how easy it is to get movable buttons up and running using fltk, it's a bit of a nightmare in DirectX. I modified My_window slightly to have another button and an image and changed the move() function in widget so it actually moves it to a new location instead of adding the new location to the current one.


Chapter 16 // Exercise 3 - Principles & Practice Using C++

Monday, 7 September 2020

Chapter 16 // Exercise 2 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 16 // Exercise 2

Make a window (based on My_window) with a 4-by-4 checkerboard of square buttons. When pressed, a button performs a simple action, such as printing its coordinates in an output box, or turns a slightly different colour (until another button is pressed).

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2016/Exercise%202


The main problem with this exercise is that I learnt that you cannot use a lambda as a callback function if it is capturing local variables. I managed to get rid of all the call back functions and have only 1 button action function but because of this constraint, I still had to have 16 separate push_backs in the constructor for CheckerboardWindow instead of being able to use a for loop.

I added a couple of new values to the Colour_type enum in Graph.h as well which gives us the exact background colour (handily called FL_BACKGROUND_COLOR by fltk). 

There was something oddly satsifying about this exercise though...maybe I can finally make Minesweeper live and reloaded now...


Chapter 16 // Exercise 2 - Principles & Practice Using C++

Wednesday, 2 September 2020

Chapter 16 // Exercise 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 16 // Exercise 1

Make a My_window that's a bit like Simple_window except that it has two buttons, next and quit.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2016/Exercise%201

I could've easily just inherited Simple_window and added another button to it but I wanted to practice making a window so created it in a new file.

My_window doesn't work exactly like Simple_window and needs return gui_main() to work properly (unlike Simple_window) but I like it.

Chapter 16 // Exercise 1 - Principles & Practice Using C++

The green square appears after next is pressed.