|
commit ea34edc1eadf327f08706f7f32ce4c5fdf3b6729
Author: Michael R Sweet <michael.r.sweet@gmail.com>
AuthorDate: Sat Jan 1 10:01:18 2022 -0500
Commit: Michael R Sweet <michael.r.sweet@gmail.com>
CommitDate: Sat Jan 1 10:01:23 2022 -0500
Tweak blocks game to use higher update rates and slightly larger window.
test/blocks.cxx | 80 ++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 53 insertions(+), 27 deletions(-)
diff --git test/blocks.cxx test/blocks.cxx
index 75a2a1a..792ef8b 100644
--- test/blocks.cxx
+++ test/blocks.cxx
@@ -1,7 +1,7 @@
//
// "Block Attack!" scrolling blocks game using the Fast Light Tool Kit (FLTK).
//
-// Copyright 2006-2018 by Michael Sweet.
+// Copyright © 2006-2021 by Michael Sweet.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@@ -48,8 +48,8 @@
# include <mmsystem.h>
#endif // _WIN32
-#define BLOCK_COLS 20
-#define BLOCK_ROWS 10
+#define BLOCK_COLS 25
+#define BLOCK_ROWS 15
#define BLOCK_SIZE 32
#define BLOCK_BLAST 100
@@ -440,17 +440,22 @@ class BlockWindow : public Fl_Double_Window {
struct Block {
int color;
bool bomb;
- int y;
+ float y;
};
struct Column {
int num_blocks;
Block blocks[BLOCK_ROWS];
- int x;
+ float x;
};
private:
+ int frames_,
+ frames_per_second_;
+ time_t frame_time_;
+ bool show_fps_;
+
Fl_Button *help_button_,
*play_button_;
@@ -546,7 +551,7 @@ void BlockWindow::_BlockWindow() {
prefs_.get("high_score", high_score_, 0);
- Fl::add_timeout(0.1, (Fl_Timeout_Handler)timeout_cb, (void *)this);
+ Fl::add_timeout(0.01666666, (Fl_Timeout_Handler)timeout_cb, (void *)this);
}
@@ -629,8 +634,8 @@ void BlockWindow::draw() {
// Draw the blocks...
for (j = num_columns_, c = columns_; j > 0; j --, c ++)
for (k = c->num_blocks, b = c->blocks; k > 0; k --, b ++) {
- xx = w() - c->x;
- yy = h() - BLOCK_SIZE - b->y;
+ xx = w() - (int)c->x;
+ yy = h() - BLOCK_SIZE - (int)b->y;
if (b->color >= BLOCK_BLAST) {
b->color ++;
@@ -704,6 +709,14 @@ void BlockWindow::draw() {
}
}
+ time_t curtime = time(NULL);
+ frames_ ++;
+ if (curtime > frame_time_) {
+ frames_per_second_ = (frames_per_second_ + 3 * frames_ / (curtime - frame_time_)) / 4;
+ frames_ = 0;
+ frame_time_ = curtime;
+ }
+
// Draw the scores and level...
char s[255];
@@ -720,6 +733,11 @@ void BlockWindow::draw() {
fl_draw(s, 0, 0, w(), 20, FL_ALIGN_CENTER);
}
+ if (show_fps_) {
+ sprintf(s, "FPS: %d ", frames_per_second_);
+ fl_draw(s, 0, h() - 20, w(), 20, FL_ALIGN_LEFT);
+ }
+
if (title_y_ > 0 && interval_ > 0.0) {
int sz = 14 + title_y_ * 86 / h();
@@ -760,6 +778,10 @@ int BlockWindow::handle(int event) {
prefs_.set("high_score", high_score_);
return (1);
}
+ // 'f': toggle showing frames-per-second
+ if (Fl::event_text() && !strcmp(Fl::event_text(), "f")) {
+ show_fps_ = !show_fps_;
+ }
break;
case FL_PUSH:
@@ -825,16 +847,20 @@ void BlockWindow::help_cb(Fl_Widget *, BlockWindow *bw) {
// Initialize the block window...
void BlockWindow::init() {
- count_ = 0;
- help_ = false;
- interval_ = -1.0;
- level_ = 1;
- num_colors_ = 3;
- num_columns_ = 0;
- paused_ = false;
- score_ = 0;
- title_[0] = '\0';
- title_y_ = 0;
+ frames_ = 0;
+ frames_per_second_ = 0;
+ frame_time_ = time(NULL);
+ show_fps_ = false;
+ count_ = 0;
+ help_ = false;
+ interval_ = -1.0;
+ level_ = 1;
+ num_colors_ = 3;
+ num_columns_ = 0;
+ paused_ = false;
+ score_ = 0;
+ title_[0] = '\0';
+ title_y_ = 0;
}
@@ -845,7 +871,7 @@ void BlockWindow::new_game() {
init();
- interval_ = 0.1f;
+ interval_ = 0.01666666666f;
opened_columns_ = 0;
strcpy(title_, "Level: 1");
@@ -881,7 +907,7 @@ void BlockWindow::timeout_cb(BlockWindow *bw) {
int i, j;
Block *b;
Column *c;
- int lastx, lasty;
+ float lastx, lasty;
#if DEBUG_TIMER
static double lasttime;
@@ -941,7 +967,7 @@ void BlockWindow::timeout_cb(BlockWindow *bw) {
// Update blocks that have been destroyed...
for (i = 0, c = bw->columns_; i < bw->num_columns_; i ++, c ++)
for (j = 0, b = c->blocks; j < c->num_blocks; j ++, b ++)
- if (b->color > (BLOCK_BLAST + 1)) {
+ if (b->color > (BLOCK_BLAST + 5)) {
bw->redraw();
c->num_blocks --;
@@ -977,13 +1003,13 @@ void BlockWindow::timeout_cb(BlockWindow *bw) {
if (!bw->paused_ && bw->interval_ > 0.0) {
bw->redraw();
- c->x ++;
+ c->x += 0.25;
}
for (j = c->num_blocks, b = c->blocks, lasty = 0; j > 0; j --, b ++) {
if (b->y > lasty) {
bw->redraw();
- b->y -= 8;
+ b->y -= 4;
}
lasty = b->y + BLOCK_SIZE;
@@ -993,7 +1019,7 @@ void BlockWindow::timeout_cb(BlockWindow *bw) {
// Slide the title text as needed...
if (bw->title_y_ > 0) {
bw->redraw();
- bw->title_y_ -= 5;
+ bw->title_y_ -= 2;
}
// Play the game...
@@ -1002,7 +1028,7 @@ void BlockWindow::timeout_cb(BlockWindow *bw) {
if (bw->count_ <= 0) {
bw->redraw();
- bw->count_ = BLOCK_SIZE;
+ bw->count_ = 4 * BLOCK_SIZE;
if (bw->num_columns_ == BLOCK_COLS) {
bw->interval_ = -1.0;
@@ -1044,14 +1070,14 @@ void BlockWindow::timeout_cb(BlockWindow *bw) {
// Update the play/pause button as needed...
if ((bw->paused_ || bw->interval_< 0.0) &&
bw->play_button_->w() < 80) {
- int s = bw->play_button_->w() + 10;
+ int s = bw->play_button_->w() + 5;
bw->play_button_->resize(s, (s - 20) * (bw->h() - s) / 120, s, s);
bw->play_button_->labelsize(s / 2 + 4);
bw->redraw();
} else if ((!bw->paused_ && bw->interval_ > 0.0) &&
bw->play_button_->w() > 20) {
- int s = bw->play_button_->w() - 5;
+ int s = bw->play_button_->w() - 2;
bw->play_button_->resize(s, (s - 20) * (bw->h() - s) / 120, s, s);
bw->play_button_->labelsize(s / 2 + 4);
[ Direct Link to Message ] | |