patterncppMinor
Drawing a checkerboard with FLTK
Viewed 0 times
withcheckerboarddrawingfltk
Problem
This is my first attempt to draw using FLTK libraries *:
Questions:
*as part of B. Stroustrup's "Principles and practice of C++".
All the needed files for compilation are here.
The FLTK could be found here
// Objective: Draw an 8-by-8, red and white checkers board
#include "std_lib_facilities.h"
#include
#include "Simple_window.h"
int main(){
try{
// create a window in the center of the screen wiht size: 600x600
int wWidth = 660;
int wHeight = 660;
Point centerScreen(x_max()/2 - wWidth/2, y_max()/2 - wHeight/2);
Simple_window sw(centerScreen, 660, 660, "Chapter 12 Exercise 3");
// starting upper left coordinates of the window + 10 pixels frame
int tlx = sw.x_max() - 650;
int tly = sw.y_max() - 650;
// Point topLeftCheckers(tlx, tly);
// instantiate all the squares as rectangle objects with: heigth = width
int numOfColumns = 8;
int numOfRows = 8;
// sqrSize == step of drawing
int sqrSize = 80;
// vector holding all (pointes to) rectangles
vector rects;
int numOfSquares = numOfColumns * numOfRows;
rects.reserve(numOfSquares);
for(size_t i=0; i set_fill_color(Color::red);
else r->set_color(Color::white);
// save object in vector
rects.push_back(r);
}
}
// attach all the rectangle objects to the window object
for(size_t k=0; k < rects.size(); ++k) sw.attach(*rects[k]);
sw.wait_for_button();
}catch(exception& e){
cerr << e.what() << endl;
getchar();
}catch(...){
cerr <<"Default exception!"<< endl;
getchar();
}
return 0;
}Questions:
- Are there any optimizations (or anything better) that can be done on the above code?
- Any useful tips and practices that one should know in the early stages, when using GUI's packages like FLTK?
*as part of B. Stroustrup's "Principles and practice of C++".
All the needed files for compilation are here.
The FLTK could be found here
Solution
int wWidth = 660;
int wHeigth = 660;If you never change these, you may as well make them constants.
const int wWidth = 660;
const int wHeight = 660;Then it's clear that they aren't intended to change.
I also fixed the spelling of "height".
Simple_window sw(centerScreen, 660, 660, "Chapter 12 Exercise 3");You just created variables for this. Why not use them?
Simple_window sw(centerScreen, wWidth, wHeight, "Chapter 12 Exercise 3");Then you can change one value and have it propagate through.
int tlx = sw.x_max() - 650;
int tly = sw.y_max() - 650;Same thing here.
const int FRAME_SIZE = 10;
const int TOP_LEFT_X = sw.x_max() - wWidth + FRAME_SIZE;
const int TOP_LEFT_Y = sw.y_max() - wHeight + FRAME_SIZE;This would also save you a comment, as it is much more obvious what this does.
for(size_t j=0; j < numOfColumns; ++j){It won't make a functional difference if both are 8, but this should be
for (size_t j = 0; j < numOfRows; ++j) {Otherwise if you change one value without changing the other (e.g. if you wanted a 12x8 checkerboard), it won't draw the board as you designate it.
if ((i+j) % 2 == 0) r->set_fill_color(Color::red);
else r->set_color(Color::white);It's more robust in the face of future edits to write this as
if ((i + j) % 2 == 0) {
r->set_fill_color(Color::red);
} else {
r->set_color(Color::white);
}Also consider
r->set_fill_color(((i + j) % 2 == 0) ? Color::red : Color::white);Which is more direct.
for(size_t k=0; k < rects.size(); ++k) sw.attach(*rects[k]);Why do this separately? Instead, put it in the
j loop, at the end. sw.attach(*r);Note that you can do this even if you put it in
rects as well. For this code, you don't need
rects at all. Perhaps you're planning on using it later, but you don't really need it now. In C++, when you use the
new operator, the memory remains allocated until you explicitly delete it. You may want to use smart pointers. Those would work more like you think, with garbage collection when there are no references left. Also, if you hadn't used
new but instead created a local variable that would have been automatically destroyed, putting it in rects wouldn't have worked. The rects vector would have just contained a bunch of references to memory that no longer represented what it had when it was created.Code Snippets
int wWidth = 660;
int wHeigth = 660;const int wWidth = 660;
const int wHeight = 660;Simple_window sw(centerScreen, 660, 660, "Chapter 12 Exercise 3");Simple_window sw(centerScreen, wWidth, wHeight, "Chapter 12 Exercise 3");int tlx = sw.x_max() - 650;
int tly = sw.y_max() - 650;Context
StackExchange Code Review Q#102299, answer score: 3
Revisions (0)
No revisions yet.