Tuesday 31 March 2020

Chapter 13 // Drill 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 13 // Drill 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.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2013/Drills/Drill%204

.


Monday 30 March 2020

Chapter 13 // Drill 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 13 // Drill 3

Make the eight squares on the diagonal starting from the top left corner red (use Rectangle).

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2013/Drills/Drill%203

I know he intended us using templates to draw the rectangles in this exercise. I now understand why the copy constructors in Shape had been deleted. However, I'm not going to use templates because they are slow and should only be used if absolutely necessary.

Chapter 13 // Drill 3 - Principles & Practice Using C++

Sunday 29 March 2020

Chapter 13 // Drill 1, 2 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 13 // Drill 1 & 2

Drill 1 - Make an 800-by-1000 Simple_window.
Drill 2 - Put an 8-by-8 grid on the leftmost 800, 800 part of that window (so that each square is 100 by 100).

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2013/Drills/Drill%201%2C2

.


Saturday 28 March 2020

Chapter 12 // Exercise 13 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 13

Find a way to add color to the lines from the previous exercise. Make some lines one color and other lines another color or other colors.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2013

A closed polyline is basically just a sequence of lines so in theory we should be able to set each line colour...in theory. The problem is that Shape only has 1 colour member that it uses for the entire object. Therefore we need to change the closed polyline to just lines so we can individually set each lines colour. It ended up a little hacky looking but it works.

Chapter 12 // Exercise 13 - Principles & Practice Using C++

Friday 27 March 2020

Chapter 12 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 12

A superellipse is a two-dimensional shape defined by the equation
Look up superellipse on the web to get a better idea of what such shapes look like. Write a program that draws "starlike" patters by connecting points on a superellipse. Take a, b, m, n, and N as arguments. Select N points on the superellipse defined by a, b, m, and, n. Make the points equally spaced for some definition of 'equal'. Connect each of those points to one or more other points (if you like you can make the number of points to which to connect a point another argument or just use N-1, i.e., all the other points).

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2012/Exercise%2012%20-%20Complete

Eurgh; another mind blower. I was upset as well because there is nothing on super ellipses on Maths is Fun. After searching and failing to understand code examples dotted across the internet; I ended up watching this video which was extremely helpful:
https://youtu.be/z86cx2A4_3E

He does code in javascript but it's easy enough to adapt. Whilst trying to draw points in a circle I realised that we really need a way to add doubles to the mix as values were getting narrowed left right and centre (pun intended). I changed Point to use doubles instead and then had to change a couple other constructors in order for it to build. There will be many, many warnings though. Through following the video I managed to draw a circle using a Polyline:


The + 300 on each line is just to change the default centre from 0,0 to wherever you want it. From there he then showed that the formula we care about to make a super ellipse is this one from wikipedia:
I'm really glad he pointed this out and took the time to explain it as I think I understand now. Using these formulas we can easily write a function to create super ellipses given a, b and n.

The full code for this mini program can be found here:
https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2012/SuperEllipseProgram

Now for what doesn't really seem like the hard part anymore. The closed_polyline contains a vector of points. We can just choose to select every N points or so and add those points to another polyline.


This one took me a good 3+ hours but once I got into it I think it was one of the most enjoyable. As much as I love the command window it's so nice to be drawing things and doing cool shit with maths. I'm really sad.

Thursday 26 March 2020

Chapter 12 // Exercise 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 11

Draw a series of regular polygons, one inside the other. The innermost should be an equliateral triangle, enclosed by a square, enclosed by a pentagon, etc. For the mathematically adept only: let all the points of each N-polygon touch sides of the (N+1)-polygon.Hint: The trigonometric functions are found in <cmath> (Section 24.8, Appendix B.9.2)

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2011/Exercise%2011%20-%20Complete

This exercise reminded me of when I first read some of the exercises in Chapter 4 way back in 2016; I was scared and convinced I couldn't do it. I'm also mathematically inept so the second portion of the exercise really threw me off. This was one of those exercises that really threw me out of my comfort zone and I had to go off and do some research to figure out what he was talking about.

I once again turned to Maths Is Fun to figure out what the hell I had to do.
https://www.mathsisfun.com/geometry/regular-polygons.html

After about an hour of reading and re-reading I managed to grasp that a regular polygon means everything is equal (I know, it's big brain time). The sides will always be the same length as all the others. With this in mind I started off by writing a little program that given the end points of a line, an angle and a length; it will draw the next line:
Chapter 12 // Exercise 11 - Principles & Practice Using C++

I was very happy when this worked. I'm terrible at this kind of stuff. You can find the code for this here:
https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2011/11.1

This is tedious though and error prone. Maths is Fun state how each point on a regular polygon can be found on the circumference of a circle. Therefore, given a centre point, radius and the number of sides, we can effectively work out each point in the polygon. These points can then be added to the Polygon type to draw one for us instead of using lines.

Chapter 12 // Exercise 11 - Principles & Practice Using C++

The code for this example can be found here:
https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2011/11.2

The important part is dividing the current side number by the overall number of sides each time to determine the next point. I am terrible at maths; I'm still not 100% sure how this is working. I eventually ended up adapting code from here to get this to work:
https://stackoverflow.com/questions/7198144/how-to-draw-a-n-sided-regular-polygon-in-cartesian-coordinates

The last part was then putting this in a loop and drawing as many polygons as we can! What's interesting to note though is that at a certain point, you will have a pretty solid circle and will become difficult to pick out all the individual polygons:

10 Polygons:
Chapter 12 // Exercise 11 - Principles & Practice Using C++

20 Polygons:
Chapter 12 // Exercise 11 - Principles & Practice Using C++

After all that, this exercise took me a good 5 hours in total but I'm glad I finished it as this stuff is actually extremely useful for game development.

Another tip. You can change the rotation of the polygons by adding an angle to the new x an y co-ordinates in makePolygon(). Remember, the C++ standard library uses radians not degrees.
Chapter 12 // Exercise 11 - Principles & Practice Using C++

Chapter 12 // Exercise 11 - Principles & Practice Using C++


EDIT 29/03/2020: After letting my brain decompress, I realised that I still haven't actually done what he asked for in either scenarios. I hadn't placed polygons inside one another nor does each point touch the sides of the n+1 polygon. 

So, to draw polygons inside each other I modified the code slightly to increase the radius each time:


And to make it so all the points touch the sides of the outer polygon, then the radius of the inside polygon must match the radius of the apothem of the outer polygon...damn this makes me sound smart.

We know for sure the radius of the polygon so we can use this formula to find the apothem:
All the points can't actually touch the sides of the outer polygon otherwise they would no longer be regular polygons but now each polygon is perfectly sized to fit within the incircle circumference of the outer polygon:
Chapter 12 // Exercise 11 - Principles & Practice Using C++



Wednesday 25 March 2020

Chapter 12 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 10

Draw the file diagram from section 12.8.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%2010

I really hate this man...why? Just why? This could be used as a form of torture. This took me an entire hour, not because it was hard but it was just painful. I haven't created little triangles at the end of each arrow because I just couldn't take it anymore.


Chapter 12 // Exercise 10 - Principles & Practice Using C++



Tuesday 24 March 2020

Chapter 12 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 9

Display an image in the screen, e.g., a photo of a friend. Label the image both with a title on the window and with a caption in the window.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%209

.



Monday 23 March 2020

Chapter 12 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 8

Draw the Olympic five rings. If you can't remember the colours, look them up.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%208

Oh man, since I got PNG images working in the last exercise, the temptation to just download a png of them and display it was so strong but it's not the point of the exercises so I did it the "right way". This exercise is pretty easy; the only thing that annoys me is that you can't get the rings to only overlap once to give it that "interlocked effect".



Sunday 22 March 2020

Chapter 12 // Exercise 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 7

Draw a two-dimensional house seen from the front, the way a child would: with a door, two windows, and a roof with a chimney. Feel free to add details; maybe have "smoke come out of the chimney.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%207

I wanted to add some clouds and grass to this and thought it would be easy however I got an error when drawing saying "PNG images are unsupported". FLTK does actually support png however we are using Bjarne's "version" of fltk and if you go to Graph.cpp (line 378) you'll see that he's only supported the use of two file formats; jpg and gif. So, I decided to add png.

First add #include <Fl/Fl_PNG_Image.H>  to fltk.h. Then in Graph.h add another enum type to Suffix::Encoding called png. In init_suffix_map() in Graph.cpp, add two more entries for png.

In Graph.cpp, in the Image constructor, add another case for png (basically copy pasta but change the name).

And then last, go to the project properties->linker->input->additional dependencies. Click edit and add fltkpngd.lib and fltkzlibd.lib

Png images now work with Bjarne's Image class! Once that was working; I got lazy....super lazy. And so I made the house in Photoshop, saved it as png and then loaded it.



Looks legit right? I could've just done the entire thing in photoshop...then loaded it as a normal png but I don't think that was the point of the exercise, or was it?

Saturday 21 March 2020

Chapter 12 // Exercise 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 6

What happens when you draw a Shape that doesn't fit inside its window? What happens when you a Window that doesn't fit on your screen? Write two programs that illustrate these two phenomena.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Exercise%206

The window draws at 0,0 and then continues on. You won't be able to see the window label though. As I have two monitors, the window extended onto the second one.



The shape will be cut off wherever it hits the edges of the window.



Friday 20 March 2020

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

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 5

Draw a red 1/4-inch frame around a rectangle that is three-quarters the height of your screen and two-thirds the width. 

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

Eurgh, I had to get the calculator out for this one. I hate doing percentages.


Thursday 19 March 2020

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

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 4

Draw a 3-by-3 tic-tac-toe board of alternating white and red squares.

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

I got confused on this just because I started over-complicating it. I started thinking of creating it as 8 lines that intersect but pondered about how to colour the parts in-between. Instead I decided to create a function that creates and fills a rectangle with just the top left co-ordinate given. 

I also don't like how set_color() means set the line colour. To me it's quite ambiguous and alludes to the fill colour more.

Another thing, you can't return any object that is a type of Shape as its copy constructor has been explicitly deleted. MSDN says people do this to prevent behaviour that they don't want but in this case I can't see why creating a rectangle and then returning it would be so heinous. Clearly it was done for a reason, however, I decided to be naughty and commented out the line in Shape (in my files it's lines 176 and 177).

After a frustrating 20 minutes of nothing appearing I was ready to just give up and do it the long way, however, when stepping through the code, the rectangles were being created and attached to the window in the for loop, but, on the last rectangle all the values got cleared in the window and therefore nothing was drawn. For some reason a range-based for loop 'resets' the window's shapes. I think it has something to do with Shape's default destructor being called as r will only be in scope during the loop. Anyway, a standard for loop works fine and we don't have to repeat rectangle code constantly.

(16/08/2021 - Note from future me...it didn't work because you allowed it to use the copy constructor which had been deleted...creating a copy that goes out of scope.....)


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

(This is very umbrella looking.....)

Wednesday 18 March 2020

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

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 3

Draw your initials 150 pixels high. Use a thick line. Draw each initial in a different colour.

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

I found how to change the width of the line by looking up the type in Graph.h. One of the constructors for Line_style takes a Line_style_type and an integer for the width.



Tuesday 17 March 2020

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

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 2

Draw a 100-by-30 Rectangle and place the text "Howdy!" inside it.

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

This uses just a bit of trickery; as looking at the code; there doesn't appear to be a way to "attach" the text to the Rectangle..yet. I'm sure that is something he'll have us implementing.



Monday 16 March 2020

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

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Exercise 1

Draw a rectangle as a Rectangle and as a Polygon. Make the lines of the Polygon red and the lines of the Rectangle blue.

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

It's weird to think that tediously defining each line of a polgon was the standard in games programming 25 years ago.



Sunday 15 March 2020

Chapter 12 // Drill 2, 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Drill 2, 3

Now add the examples from section 12.7 one by one, testing between each added subsection example.

Go through and make one minor change (e.g, in color, in location, or in number of points) to each of the subsection examples.

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

Ah, so I already followed along with the book and did these (it was how I realised that the files don't work). The link above is the full code from the sections with some images that I added. As I was completing the examples I wrote some notes on them here:
https://lptcp.blogspot.com/2020/03/notes-on-examples-in-chapter-12.html
(Extras just in case:)
Setting up FLTK:

Fixed graphics files (this one took me a few hours):


Saturday 14 March 2020

Chapter 12 // Drill 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 12 // Drill 1

Get an empty Simple_window with the size 600 by 400 and a label My Window compiled, linked, and run. Note that you have to link the FLTK library as described in Appendix D; #include Graph.h and Simple_window.h in your code; and include Graph.cpp and Window.cpp in your project.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2012/Drill%201

This is quite possibly, the hardest exercise in the entire book if you have no experience with linking libraries. Fortunately, we had to use DirectX all throughout my uni course and I had to learn how to compile library binaries when starting SNES development so I wasn't completely stumped. The other annoying set back is the files you need are out of date and versions of FLTK numbered 1.1.x (as recommended in the book) no longer compiles on modern pc's. 

So, I've already written a few posts on how to get set up for this chapter so I'll link the appropriate ones here.

Setting up FLTK:

Fixed graphics files (this one took me a few hours):

EDIT 01/01/2021
I've now added a full project zip to github for this exercise:

Please make sure to use Simple_window2.h and not the original if you have reached this stage. I recommend downloading Bjarne's new files from here:

Errors you may encounter:
1>c:\users\lj\documents\fltk\fltk-1.3.5\fl\x.h(37): fatal error C1083: Cannot open include file: 'X11/Xlib.h': No such file or directory

I never encountered this on my first run and then I just kept using the same project. However when going over this again to create a new project I got this new error. Basically, if you double click on the error it takes you to x.H and it's complaining that it does't know what <x11/xlib.h> and friends are.

If you look a little further up on line 29, you'll notice that it hits these includes if we are not in WIN32 mode. If you are doing these exercises on Windows, it should know what WIN32 is, if it doesn't it means it cannot find the definition for the macro WIN32. This is defined in Windows.h which is included in the std_lib_facilities.h file. To fix these errors, simply put std_lib_facilities.h/windows.h before <Fl/fl...> includes (or "fltk.h"). 

I don't know why this isn't causing issues on my original project as the settings are all the same. Computers....

Friday 13 March 2020

Notes on the examples in Chapter 12 Programming:Principles and Practice Using C++

So I've started reading through Chapter 12 and just trying to get the first example shown a page in took me a while. Therefore, I'll be leaving comments and fixes in this post for anyone else who may have struggled.

In order to get the program running in the first place you'll need some files made by Bjarne himself. I spoke about these in a previous post and fixed them up and posted them to my Git. There were a lot of errors as, due to time they have become wildly out of date.

These cleaned up files can be found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

And I wrote about this process in detail here:
https://lptcp.blogspot.com/2020/03/programming-principles-practice-fixing.html

I wrote a guide for installing FLTK here as well:
https://lptcp.blogspot.com/2020/03/how-to-install-fltk-for-use-with.html

12.3 - A First Example pg 415
Due to namespace ambiguity, it's probably best if you avoid using namespace Graph_lib. In the first example in 12.3, Polygon is considered ambiguous and requires Graph_lib even though the using keyword has been set.  That was the only issue I had for the first example.

12.7.3
Initially, the axis drew as light grey on my screen. I tried looking for the definition but I couldn't seem to find where it is initially set to use grey, so I just left it. I then remembered that we're supposed to be hitting "next" after every example to show it building up. The key is to make sure you have win.wait_for_button() after every example. The axis shows as black after that.

I also noticed that the canvas label disappears after the first screen. Odd. (edit 30/09/2020 - I ended up fixing this here: https://lptcp.blogspot.com/2020/08/notes-on-chapter-15-principles-practice.html)

12.7.5 - 12.7.6
Both Rectangle and Polygon will need their scope (Graph_Lib) when using them. Even if you have using namespace Graph_lib;

12.7.7
The green shade is much brighter than the one printed.

12.7.10
Ellipse also needs Graph_lib::

You can find the full code and images I used here:
https://github.com/l-paz91/principles-practice/tree/master/Chapter%2012/Examples

Thursday 12 March 2020

Programming: Principles & Practice - Fixing Files for Chapters 12

Chapter 12 had quite a bit of setup to get it working (and to be able to complete the exercises). Bjarne never actually mentions that you need to download extra files in order to get his examples working. He says at the start of the exercises "you need these" but doesn't give any other information. You can find the custom classes he's created here:

Not all of these are necessary for the graphics chapters. I have a few notes on getting them to compile though as they are hilariously outdated and the internet only turned up bits and pieces for working versions, so I begrudgingly spent an entire evening going through every bloody error. You can find the cleaned up versions on my github however, in the interest of knowledge, try going through and fixing the files.

1 - There are 2 Gui.h (one is all capitals). Use the uppercase GUI.h.

2 - Simple_window.cpp relies on a custom constructor for the next_button(Point()) member. This has been commented out in the code. In Point.h un-comment the two commented out constructors.

3 - Graph.cpp is a red mess.

  • Include Window.h at the top. 
  • Around line 316, change bool can_open to ifstream can_open.
  • In Graph.h add #include <FL/fl_draw.H> and <FL.Fl_Image.H> for reasons. And yes, the f and the is lowercase for some absurd reason and one contains a slash, the other a dot....
4. Simple_Window has two versions of wait_for_button(). In Simple_window.h change void wait_for_button() to void wait_for_button_modified(). Then add  bool wait_for_button();  above it. (This is because, at the time of writing this, I'm not sure if the modified version will be used at some point).

5. Simple_Window has two definitions for it's constructor. Delete the one in the header file and replace it with:
  • Simple_window(Point xy, int w, in h, const string& title);
6. Window is the Windows handle for a hwnd, files are not happy about this. Put all the code in namespace Graph_lib {code here} for the following files:
  • Simple_Window.h
  • Simple_window.cpp
7. Simple_window...again. 
  • next() already has a body. Delete the body from the .h file so it just reads void next();
  • cb_next() also already has a body. Delete the one in the cpp file as, with a quick glance, the one in the header looks safer; pointer usage wise that is.
8. They're not errors but all the mismatch warnings are annoying me. Regardless of how annoying they are, the phrase "if at first you don't succeed, #pragma disable warning" is not the approach to take...(as much as I want to). Where it says '<' signed/unsigned mismatch; changed the int to unsigned int (or remove the unsigned) till the warnings go away. 

There are two more warnings on Graph.cpp warning about conversion from double to int...seeing as how Point only uses ints I'm guessing floating point maths is not allowed so I wrapped the second part in a static_cast<int>(u.first*(p2.x - p1.x)); etc. 

9. Only two errors left. Graph_lib::Menu::Menu(...) already has a body (of course it does). Delete the one in the cpp file (they're the same...practically). And fixing that fixed the last error and it builds...YEAHHH.


Alright, I think that will do for the evening. I've been sat here for 2 hours just trying to do the bloody example a page into the chapter. 

Full code files:

EDIT 14/12/2020
I've just realised, I never posted the original cleaned up version of SimpleWindow.cpp...Whoops. I never saved the original copies but over Christmas break I'll fix up a new copy.

EDIT 01/01/2021
The link to Bjarne's original files above no longer works...It took me a good 10 minutes to navigate his website and realise that the old files are now provided as a zip file here:

He notes that they were recovered from the loss of his previous website which must've gone down sometime last year. These new files are slightly different to ones I downloaded; they appear to be newer. This batch does not contain a Simple_window.cpp as everything is defined in the header:

Wednesday 11 March 2020

How To Install FLTK For Use With Programming: Principles & Practice Using C++

There are instructions at the back of the book (page 1204) on how to install FLTK however, these are for use with 1.1.x.

FLTK advises users to no longer use the 1.1.x versions of FLTK anymore as they won't compile on most modern computers. This is true. 1.1.10 would not compile on my pc (some of the files were from 2003...)

Instead of copying the lib files to the VS folders I have documented how I would usually include a library into a project. You can use steps 6 onwards, to include any library. I normally use DirectX9.

1. Download the latest stable release from here:
https://www.fltk.org/software.php

(I usually download the tar.gz version).

I'm using 1.3.5 in these exercises.

EDIT 07/10/2021 - I followed these steps using the latest version of 1.3.7 and it worked fine.
EDIT 12/02/2023 - Followed these step using the latest version of 1.3.8 and works fine.
EDIT 30/04/2024 - Followed these steps using the latest version of 1.3.9 with Visual Studio 2022 and it works fine. I did have to change my build mode to x86 though with VS2022.

2. Unzip the file (you may need to do this twice depending on what zip programs you have installed).

3. Within the FLTK folder, navigate to ide -> VisualC2010 and open the fltk Visual Studio Solution. For me this opened automatically using VS2017.

4. VS will ask you to re-target the solution. Just click ok.


5. Build the solution. This will take a few minutes. When you include FLTK in your solution now, you won't have to recompile the library every time you build. (Building in Win32 Debug mode is fine for debugging. If you want release, build in release). You can then close the solution.

6. Open up the VS solution you wish to work in. Right click on the solution name in the explorer and select properties.


7. Click on VC++ Directories. Click on Include Directories and the arrow that appears on the right and then edit.


8. Click on the new folder icon at the top, then the three dots that appear. Navigate to the folder on your pc where fltk is. Click Select Folder and the press OK.

9. Repeat steps 7 & 8 but with the the Library Directories.


Press Apply and then OK. Normally you would choose the separate folders for each. For example, the include directories would be the include folder and the libraries would be the library folder. However, FLTK itself uses the FL/... structure so if you do this you will find that your program will fail to build with an error like so:

EDIT: I didn't realise I had done this on my own PC until these steps failed to work on my work pc; I had added additional dependencies in Linker. In the Library Directories section, please also include the lib folder of FLTK. Or you can leave it the way it is and add the folder to the additional dependencies like so:


10. Go to the properties again but this time navigate to Linker -> Input -> Additional Dependencies. Click the arrow, then edit and add these on new lines  in the top box (ignore the top one):

Click OK. The on the end of each library is for the debug builds. If you want to build a release version you will need to use different libraries.

Edit 07/10/2021 - If you'd like to add png images to your builds, add fltkpngd.lib

11. Write down the sample code, build and run. You should get a pop up window:



Other errors you may encounter:
- "Fatal error LNK1104: cannot open file 'fltkd.lib'
I got this error when doing the above steps on my work pc.  Depending on your computer you may need to change step 9 to directly include the file path of the libraries. For example it would be FLTK->fltk 1.3.5->lib. (or you can just add the folder to the additional library dependencies in Linker).

EDIT 01/01/2021
I've now added a zip file to github containing a full program for this exercise:

Tuesday 10 March 2020

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

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 16

Write a program to read a file of whitespace-separated numbers and output
them in order (lowest value first), one value per line. Write a value only
once, and if it occurs more than once write the count of it's occurrences on
it's line. For example, 7 5 5 7 3 117 5 should give
3
5 3
7 2
117
This one took me a stupid amount of time. It could've been done quite quickly with some brute force and a lot of if statements but I knew there had to be a better way (that didn't involve using maps). I also didn't want to rely on output trickery to get the data and eventually settled on a custom struct that holds a number and how many times it appears. There is a function that sorts the vector, then removes an extra values whilst increasing the count of the number.

Monday 9 March 2020

Chapter 11 // Exercise 15 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 15

 Write a program that reads a file of whitespace-separated numbers and outputs
 a file of numbers using scientific format and precision 8 in four fields of 
 20 characters per line.
.

Sunday 8 March 2020

Chapter 11 // Exercise 14 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 14

 Write a program that reads a text file and writes out how many characters of each character classification are in the file.
.

Saturday 7 March 2020

Chapter 11 // Exercise 13 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 13

Reverse the order of words (defined as whitespace-separated strings) in a file. For example, Norwegian Blue Parrot becomes parrot Blue Norwegian. You are allowed to assume that all the strings from the file will fit into memory at once.
.

Friday 6 March 2020

Chapter 11 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 12

Reverse the order of characters in a text file. For example, asdfghjkl becomes
 lkjhgfdsa. Warning: There is no really good, portable, and efficient way of
 reading a file backward.
This one was pretty simple. I did originally do it with just an fstream as we're reading and writing the same file however, it appears an fstream, by default, appends new text rather than overwrite. I tried using ios::trunc to make it overwrite but it didn't work so I just switched to if/ostream.