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.

Tuesday, 1 September 2020

Chapter 16 // Drills 1, 2, 3, 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 // Drills 1, 2, 3, 4

1. Make a completely new project with linker settings for FLTK (as described in Appendix D).
2. Using the facilities of Graph_lib, type in the line-drawing program from section 16.5 and get it to run.
3. Modify the program to use a pop-up menu as described in section 16.7 and get it to run.
4. Modify the program to have a second menu for choosing line styles and get it to run.

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

I didn't do 1 as I've been using the same project since the start of chapter 12 because I'm lazy.


The second one made me scratch my head a few times. I initially set it up in Graph.h but it didn't like that. So I moved it to Simple_window.h...visual studio also didn't like that. It didn't like it in Window.h either. Eventually, because I'm big brain, I realised I was creating circular dependencies due to certain headers that needed to be included so Lines_window is in GUI.h. Also, because Window is a class and not a struct, make sure you put : public Window when inheriting....

3 is pretty simple as long as you follow the code in the book however you need to add hideMenu() and redraw() to the change() function otherwise the menu won't close itself after you've selected a new colour.

Once 3 was working, 4 was as simple as copypasta and change some code. I decided not to implement callback functions and use lambdas as I hate them and I need to get more comfortable using them..even if I think the callback functions are more readable. Unreal Engine fucking loves lambdas. I made the change line style function take in a line style so I didn't have to create a function for every different line style.


I'm happy to have finally started this chapter though; I'm going to create some sick 1998-looking programs now.