2006-08-25 10:58:33 +04:00
|
|
|
//
|
|
|
|
// "$Id$"
|
|
|
|
//
|
|
|
|
// "Block Attack!" scrolling blocks game using the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2017-07-25 20:25:51 +03:00
|
|
|
// Copyright 2006-2017 by Michael Sweet.
|
2006-08-25 10:58:33 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// 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
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/COPYING.php
|
2006-08-25 10:58:33 +04:00
|
|
|
//
|
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Double_Window.H>
|
2006-08-27 00:01:24 +04:00
|
|
|
#include <FL/Fl_Button.H>
|
2006-08-25 10:58:33 +04:00
|
|
|
#include <FL/Fl_Preferences.H>
|
|
|
|
#include <FL/Fl_XPM_Image.H>
|
2006-08-27 00:01:24 +04:00
|
|
|
#include <FL/Fl_XBM_Image.H>
|
|
|
|
#include <FL/Fl_Tiled_Image.H>
|
2006-08-25 10:58:33 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/x.H>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
// Audio headers...
|
|
|
|
#include <config.h>
|
|
|
|
|
2016-01-04 01:54:29 +03:00
|
|
|
#if defined(WIN32) || defined(__APPLE__)
|
|
|
|
#elif defined(FL_PORTING)
|
2016-01-04 16:40:57 +03:00
|
|
|
# pragma message "FL_PORTING: FLTK does not provide cross platform sound support"
|
2016-01-04 01:54:29 +03:00
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
#ifndef WIN32
|
|
|
|
# include <unistd.h>
|
2017-07-25 20:25:51 +03:00
|
|
|
# include <sys/time.h> // gettimeofday()
|
2006-08-25 10:58:33 +04:00
|
|
|
#endif // !WIN32
|
|
|
|
|
|
|
|
#ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
# define ALSA_PCM_NEW_HW_PARAMS_API
|
|
|
|
# include <alsa/asoundlib.h>
|
|
|
|
#endif // HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
#ifdef __APPLE__
|
|
|
|
# include <CoreAudio/AudioHardware.h>
|
|
|
|
#endif // __APPLE__
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <mmsystem.h>
|
|
|
|
#endif // WIN32
|
|
|
|
|
|
|
|
#define BLOCK_COLS 20
|
|
|
|
#define BLOCK_ROWS 10
|
|
|
|
#define BLOCK_SIZE 32
|
|
|
|
#define BLOCK_BLAST 100
|
|
|
|
|
2017-07-25 21:15:52 +03:00
|
|
|
// These factors are used to fine-tune the game when these events
|
|
|
|
// occur by multiplying the timer interval with the given factor:
|
|
|
|
//
|
|
|
|
// (a) enter the next game level
|
|
|
|
// (b) click on a "normal block", destroy this one and adjacent blocks
|
|
|
|
// (c) click on a "bomb", destroy all blocks of the same color
|
|
|
|
|
|
|
|
#define LEVEL_FACTOR 0.90 // was: 0.95
|
|
|
|
#define NORMAL_FACTOR 0.999
|
|
|
|
#define BOMB_FACTOR 0.995
|
|
|
|
|
|
|
|
// Set this to 1 to debug the timer callback (should be 0)
|
|
|
|
#define DEBUG_TIMER 0
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
#include "pixmaps/blast.xpm"
|
|
|
|
Fl_Pixmap blast_pixmap(blast_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/red.xpm"
|
|
|
|
Fl_Pixmap red_pixmap(red_xpm);
|
|
|
|
#include "pixmaps/red_bomb.xpm"
|
|
|
|
Fl_Pixmap red_bomb_pixmap(red_bomb_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/green.xpm"
|
|
|
|
Fl_Pixmap green_pixmap(green_xpm);
|
|
|
|
#include "pixmaps/green_bomb.xpm"
|
|
|
|
Fl_Pixmap green_bomb_pixmap(green_bomb_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/blue.xpm"
|
|
|
|
Fl_Pixmap blue_pixmap(blue_xpm);
|
|
|
|
#include "pixmaps/blue_bomb.xpm"
|
|
|
|
Fl_Pixmap blue_bomb_pixmap(blue_bomb_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/yellow.xpm"
|
|
|
|
Fl_Pixmap yellow_pixmap(yellow_xpm);
|
|
|
|
#include "pixmaps/yellow_bomb.xpm"
|
|
|
|
Fl_Pixmap yellow_bomb_pixmap(yellow_bomb_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/cyan.xpm"
|
|
|
|
Fl_Pixmap cyan_pixmap(cyan_xpm);
|
|
|
|
#include "pixmaps/cyan_bomb.xpm"
|
|
|
|
Fl_Pixmap cyan_bomb_pixmap(cyan_bomb_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/magenta.xpm"
|
|
|
|
Fl_Pixmap magenta_pixmap(magenta_xpm);
|
|
|
|
#include "pixmaps/magenta_bomb.xpm"
|
|
|
|
Fl_Pixmap magenta_bomb_pixmap(magenta_bomb_xpm);
|
|
|
|
|
|
|
|
#include "pixmaps/gray.xpm"
|
|
|
|
Fl_Pixmap gray_pixmap(gray_xpm);
|
|
|
|
#include "pixmaps/gray_bomb.xpm"
|
|
|
|
Fl_Pixmap gray_bomb_pixmap(gray_bomb_xpm);
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
Fl_Pixmap *normal_pixmaps[] = {
|
2006-08-25 10:58:33 +04:00
|
|
|
&red_pixmap,
|
|
|
|
&green_pixmap,
|
|
|
|
&blue_pixmap,
|
|
|
|
&yellow_pixmap,
|
|
|
|
&cyan_pixmap,
|
|
|
|
&magenta_pixmap,
|
|
|
|
&gray_pixmap
|
|
|
|
};
|
2006-08-27 00:01:24 +04:00
|
|
|
Fl_Pixmap *bomb_pixmaps[] = {
|
2006-08-25 10:58:33 +04:00
|
|
|
&red_bomb_pixmap,
|
|
|
|
&green_bomb_pixmap,
|
|
|
|
&blue_bomb_pixmap,
|
|
|
|
&yellow_bomb_pixmap,
|
|
|
|
&cyan_bomb_pixmap,
|
|
|
|
&magenta_bomb_pixmap,
|
|
|
|
&gray_bomb_pixmap
|
|
|
|
};
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
const unsigned char screen_bits[] = {
|
|
|
|
0xff, 0x55, 0xff, 0xaa, 0xff, 0x55, 0xff, 0xaa
|
|
|
|
};
|
|
|
|
Fl_Bitmap screen_bitmap(screen_bits, 8, 8);
|
|
|
|
Fl_Tiled_Image screen_tile(&screen_bitmap);
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
// Sound class...
|
|
|
|
//
|
|
|
|
// There are MANY ways to implement sound in a FLTK application.
|
|
|
|
// The approach we are using here is to conditionally compile OS-
|
|
|
|
// specific code into the application - CoreAudio for MacOS X, the
|
|
|
|
// standard Win32 API stuff for Windows, ALSA or X11 for Linux, and
|
|
|
|
// X11 for all others. We have to support ALSA on Linux because the
|
|
|
|
// current Xorg releases no longer support XBell() or the PC speaker.
|
|
|
|
//
|
|
|
|
// There are several good cross-platform audio libraries we could also
|
|
|
|
// use, such as OpenAL, PortAudio, and SDL, however they were not chosen
|
|
|
|
// for this application because of our limited use of sound.
|
|
|
|
//
|
|
|
|
// Many thanks to Ian MacArthur who provided sample code that led to
|
|
|
|
// the CoreAudio implementation you see here!
|
|
|
|
class BlockSound {
|
|
|
|
// Private, OS-specific data...
|
|
|
|
#ifdef __APPLE__
|
|
|
|
AudioDeviceID device;
|
2014-04-27 17:57:09 +04:00
|
|
|
#ifndef MAC_OS_X_VERSION_10_5
|
|
|
|
#define MAC_OS_X_VERSION_10_5 1050
|
|
|
|
#endif
|
2010-03-11 01:56:56 +03:00
|
|
|
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|
|
|
AudioDeviceIOProcID audio_proc_id;
|
|
|
|
# endif
|
2006-08-25 10:58:33 +04:00
|
|
|
AudioStreamBasicDescription format;
|
|
|
|
short *data;
|
|
|
|
int remaining;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
static OSStatus audio_cb(AudioDeviceID device,
|
|
|
|
const AudioTimeStamp *current_time,
|
|
|
|
const AudioBufferList *data_in,
|
|
|
|
const AudioTimeStamp *time_in,
|
|
|
|
AudioBufferList *data_out,
|
|
|
|
const AudioTimeStamp *time_out,
|
|
|
|
void *client_data);
|
|
|
|
#elif defined(WIN32)
|
|
|
|
HWAVEOUT device;
|
|
|
|
HGLOBAL header_handle;
|
|
|
|
LPWAVEHDR header_ptr;
|
|
|
|
HGLOBAL data_handle;
|
|
|
|
LPSTR data_ptr;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
snd_pcm_t *handle;
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
#endif // __APPLE__
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
public:
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
// Common data...
|
|
|
|
static short *sample_data;
|
|
|
|
static int sample_size;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
BlockSound();
|
|
|
|
~BlockSound();
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
void play_explosion(float duration);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sound class globals...
|
|
|
|
short *BlockSound::sample_data = NULL;
|
|
|
|
int BlockSound::sample_size = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the BlockSound class
|
|
|
|
BlockSound::BlockSound() {
|
|
|
|
sample_size = 0;
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
remaining = 0;
|
|
|
|
|
|
|
|
UInt32 size = sizeof(device);
|
|
|
|
|
|
|
|
if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
&size, (void *)&device) != noErr) return;
|
|
|
|
|
|
|
|
size = sizeof(format);
|
|
|
|
if (AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyStreamFormat,
|
|
|
|
&size, &format) != noErr) return;
|
|
|
|
|
|
|
|
// Set up a format we like...
|
|
|
|
format.mSampleRate = 44100.0; // 44.1kHz
|
|
|
|
format.mChannelsPerFrame = 2; // stereo
|
|
|
|
|
|
|
|
if (AudioDeviceSetProperty(device, NULL, 0, false,
|
|
|
|
kAudioDevicePropertyStreamFormat,
|
|
|
|
sizeof(format), &format) != noErr) return;
|
|
|
|
|
|
|
|
// Check we got linear pcm - what to do if we did not ???
|
|
|
|
if (format.mFormatID != kAudioFormatLinearPCM) return;
|
|
|
|
|
2010-03-11 01:56:56 +03:00
|
|
|
// Attach the callback and start the device
|
|
|
|
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|
|
|
if (AudioDeviceCreateIOProcID(device, audio_cb, (void *)this, &audio_proc_id) != noErr) return;
|
|
|
|
AudioDeviceStart(device, audio_proc_id);
|
|
|
|
# else
|
2006-08-25 10:58:33 +04:00
|
|
|
if (AudioDeviceAddIOProc(device, audio_cb, (void *)this) != noErr) return;
|
|
|
|
AudioDeviceStart(device, audio_cb);
|
2010-03-11 01:56:56 +03:00
|
|
|
# endif
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
sample_size = (int)format.mSampleRate;
|
|
|
|
|
|
|
|
#elif defined(WIN32)
|
|
|
|
WAVEFORMATEX format;
|
|
|
|
|
|
|
|
memset(&format, 0, sizeof(format));
|
|
|
|
format.cbSize = sizeof(format);
|
|
|
|
format.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
format.nChannels = 2;
|
|
|
|
format.nSamplesPerSec = 44100;
|
|
|
|
format.nAvgBytesPerSec = 44100 * 4;
|
|
|
|
format.nBlockAlign = 4;
|
|
|
|
format.wBitsPerSample = 16;
|
|
|
|
|
2006-08-28 18:45:20 +04:00
|
|
|
data_handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, format.nSamplesPerSec * 4);
|
2006-08-25 10:58:33 +04:00
|
|
|
if (!data_handle) return;
|
|
|
|
|
|
|
|
data_ptr = (LPSTR)GlobalLock(data_handle);
|
|
|
|
|
|
|
|
header_handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(WAVEHDR));
|
|
|
|
if (!header_handle) return;
|
|
|
|
|
|
|
|
header_ptr = (WAVEHDR *)GlobalLock(header_handle);
|
|
|
|
|
|
|
|
header_ptr->lpData = data_ptr;
|
|
|
|
header_ptr->dwFlags = 0;
|
|
|
|
header_ptr->dwLoops = 0;
|
|
|
|
|
|
|
|
if (waveOutOpen(&device, WAVE_MAPPER, &format, 0, 0, WAVE_ALLOWSYNC)
|
|
|
|
!= MMSYSERR_NOERROR) return;
|
|
|
|
|
|
|
|
sample_size = format.nSamplesPerSec;
|
|
|
|
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
handle = NULL;
|
|
|
|
|
|
|
|
if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
|
|
|
|
// Initialize PCM sound stuff...
|
|
|
|
snd_pcm_hw_params_t *params;
|
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(¶ms);
|
|
|
|
snd_pcm_hw_params_any(handle, params);
|
|
|
|
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16);
|
|
|
|
snd_pcm_hw_params_set_channels(handle, params, 2);
|
|
|
|
unsigned rate = 44100;
|
|
|
|
int dir;
|
|
|
|
snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir);
|
|
|
|
snd_pcm_uframes_t period = (int)rate;
|
|
|
|
snd_pcm_hw_params_set_period_size_near(handle, params, &period, &dir);
|
|
|
|
|
|
|
|
sample_size = rate;
|
|
|
|
|
|
|
|
if (snd_pcm_hw_params(handle, params) < 0) {
|
|
|
|
sample_size = 0;
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
|
|
|
if (sample_size) {
|
|
|
|
// Make an explosion sound by passing white noise through a low pass
|
|
|
|
// filter with a decreasing frequency...
|
|
|
|
sample_data = new short[2 * sample_size];
|
|
|
|
|
|
|
|
short *sample_ptr = sample_data;
|
|
|
|
int max_sample = 2 * sample_size - 2;
|
|
|
|
|
|
|
|
*sample_ptr++ = 0;
|
|
|
|
*sample_ptr++ = 0;
|
|
|
|
|
|
|
|
for (int j = max_sample; j > 0; j --, sample_ptr ++) {
|
|
|
|
float freq = (float)j / (float)max_sample;
|
|
|
|
float volume = 32767.0 * (0.5 * sqrt(freq) + 0.5);
|
|
|
|
float sample = 0.0001 * ((rand() % 20001) - 10000);
|
|
|
|
|
|
|
|
*sample_ptr = (int)(volume * freq * sample +
|
|
|
|
(1.0 - freq) * sample_ptr[-2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cleanup the BlockSound class
|
|
|
|
BlockSound::~BlockSound() {
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (sample_size) {
|
2010-03-11 01:56:56 +03:00
|
|
|
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|
|
|
AudioDeviceStop(device, audio_proc_id);
|
|
|
|
AudioDeviceDestroyIOProcID(device, audio_proc_id);
|
|
|
|
# else
|
2006-08-25 10:58:33 +04:00
|
|
|
AudioDeviceStop(device, audio_cb);
|
|
|
|
AudioDeviceRemoveIOProc(device, audio_cb);
|
2010-03-11 01:56:56 +03:00
|
|
|
# endif
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(WIN32)
|
|
|
|
if (sample_size) {
|
|
|
|
waveOutClose(device);
|
|
|
|
|
|
|
|
GlobalUnlock(header_handle);
|
|
|
|
GlobalFree(header_handle);
|
|
|
|
|
|
|
|
GlobalUnlock(data_handle);
|
|
|
|
GlobalFree(data_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
if (handle) {
|
|
|
|
snd_pcm_drain(handle);
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
}
|
|
|
|
# endif // HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
|
|
|
if (sample_size) {
|
|
|
|
delete[] sample_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
// Callback function for writing audio data...
|
2017-07-25 20:25:51 +03:00
|
|
|
OSStatus BlockSound::audio_cb(AudioDeviceID device,
|
|
|
|
const AudioTimeStamp *current_time,
|
|
|
|
const AudioBufferList *data_in,
|
|
|
|
const AudioTimeStamp *time_in,
|
|
|
|
AudioBufferList *data_out,
|
|
|
|
const AudioTimeStamp *time_out,
|
|
|
|
void *client_data) {
|
2006-08-25 10:58:33 +04:00
|
|
|
BlockSound *ss = (BlockSound *)client_data;
|
|
|
|
int count;
|
|
|
|
float *buffer;
|
|
|
|
|
|
|
|
if (!ss->remaining) return noErr;
|
|
|
|
|
|
|
|
for (count = data_out->mBuffers[0].mDataByteSize / sizeof(float),
|
|
|
|
buffer = (float*) data_out->mBuffers[0].mData;
|
|
|
|
ss->remaining > 0 && count > 0;
|
|
|
|
count --, ss->data ++, ss->remaining --) {
|
|
|
|
*buffer++ = *(ss->data) / 32767.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
*buffer++ = 0.0;
|
|
|
|
count --;
|
|
|
|
}
|
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
|
|
|
|
|
|
|
// Play a note for the given amount of time...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockSound::play_explosion(float duration) {
|
2006-08-25 10:58:33 +04:00
|
|
|
Fl::check();
|
|
|
|
|
|
|
|
if (duration <= 0.0)
|
|
|
|
return;
|
|
|
|
|
2007-05-20 19:26:09 +04:00
|
|
|
#if defined(__APPLE__) || defined(WIN32) || defined(HAVE_ALSA_ASOUNDLIB_H)
|
2006-08-25 10:58:33 +04:00
|
|
|
if (duration > 1.0)
|
|
|
|
duration = 1.0;
|
|
|
|
|
|
|
|
int samples = (int)(duration * sample_size);
|
|
|
|
short *sample_ptr = sample_data + 2 * (sample_size - samples);
|
2007-05-20 04:01:06 +04:00
|
|
|
#endif // __APPLE__ || WIN32 || HAVE_ALSA_ASOUNDLIB_H
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
// Point to the next note...
|
|
|
|
data = sample_ptr;
|
|
|
|
remaining = samples * 2;
|
|
|
|
|
|
|
|
#elif defined(WIN32)
|
|
|
|
if (sample_size) {
|
|
|
|
memcpy(data_ptr, sample_ptr, samples * 4);
|
|
|
|
|
|
|
|
header_ptr->dwBufferLength = samples * 4;
|
|
|
|
waveOutPrepareHeader(device, header_ptr, sizeof(WAVEHDR));
|
|
|
|
|
|
|
|
waveOutWrite(device, header_ptr, sizeof(WAVEHDR));
|
2017-07-25 20:25:51 +03:00
|
|
|
} else
|
|
|
|
Beep(440, (int)(1000.0 * duration));
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
#elif defined(HAVE_ALSA_ASOUNDLIB_H)
|
|
|
|
if (handle) {
|
|
|
|
// Use ALSA to play the sound...
|
|
|
|
if (snd_pcm_writei(handle, sample_ptr, samples) < 0) {
|
|
|
|
snd_pcm_prepare(handle);
|
|
|
|
snd_pcm_writei(handle, sample_ptr, samples);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif // __APPLE__
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
class BlockWindow : public Fl_Double_Window {
|
2006-08-26 17:14:24 +04:00
|
|
|
public:
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
struct Block {
|
2006-08-25 10:58:33 +04:00
|
|
|
int color;
|
|
|
|
bool bomb;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
struct Column {
|
2006-08-25 10:58:33 +04:00
|
|
|
int num_blocks;
|
|
|
|
Block blocks[BLOCK_ROWS];
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
2006-08-26 17:14:24 +04:00
|
|
|
private:
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
Fl_Button *help_button_,
|
|
|
|
*play_button_;
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
int num_columns_;
|
|
|
|
Column columns_[BLOCK_COLS];
|
|
|
|
int count_;
|
2006-08-27 00:01:24 +04:00
|
|
|
bool help_;
|
2006-08-25 10:58:33 +04:00
|
|
|
int high_score_;
|
|
|
|
float interval_;
|
|
|
|
int level_;
|
|
|
|
int num_colors_;
|
|
|
|
int opened_columns_;
|
|
|
|
bool paused_;
|
|
|
|
static Fl_Preferences prefs_;
|
|
|
|
int score_;
|
|
|
|
BlockSound *sound_;
|
|
|
|
char title_[255];
|
|
|
|
int title_y_;
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
void _BlockWindow();
|
2006-08-25 10:58:33 +04:00
|
|
|
int bomb(int color);
|
|
|
|
int click(int col, int row);
|
2006-08-27 00:01:24 +04:00
|
|
|
static void help_cb(Fl_Widget *wi, BlockWindow *bw);
|
2006-08-25 10:58:33 +04:00
|
|
|
void init();
|
2006-08-27 00:01:24 +04:00
|
|
|
static void play_cb(Fl_Widget *wi, BlockWindow *bw);
|
2006-08-25 10:58:33 +04:00
|
|
|
static void timeout_cb(BlockWindow *bw);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
BlockWindow(int X, int Y, int W, int H, const char *L = 0);
|
|
|
|
BlockWindow(int W, int H, const char *L = 0);
|
|
|
|
~BlockWindow();
|
|
|
|
|
|
|
|
void draw();
|
|
|
|
int handle(int event);
|
|
|
|
void new_game();
|
|
|
|
int score() { return (score_); }
|
2007-11-19 17:14:13 +03:00
|
|
|
void up_level();
|
2006-08-25 10:58:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Fl_Preferences BlockWindow::prefs_(Fl_Preferences::USER, "fltk.org", "blocks");
|
|
|
|
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
int main(int argc, char *argv[]) {
|
2006-08-27 00:01:24 +04:00
|
|
|
Fl::scheme("plastic");
|
|
|
|
Fl::visible_focus(0);
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
BlockWindow *bw = new BlockWindow(BLOCK_COLS * BLOCK_SIZE,
|
|
|
|
BLOCK_ROWS * BLOCK_SIZE + 20,
|
|
|
|
"Block Attack!");
|
|
|
|
|
|
|
|
bw->show(argc, argv);
|
|
|
|
|
|
|
|
return (Fl::run());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Create a block window at the specified position
|
2006-08-25 10:58:33 +04:00
|
|
|
BlockWindow::BlockWindow(int X, int Y, int W, int H, const char *L)
|
2006-08-27 00:01:24 +04:00
|
|
|
: Fl_Double_Window(X, Y, W, H, L) {
|
|
|
|
_BlockWindow();
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Create a block window
|
2006-08-25 10:58:33 +04:00
|
|
|
BlockWindow::BlockWindow(int W, int H, const char *L)
|
2006-08-27 00:01:24 +04:00
|
|
|
: Fl_Double_Window(W, H, L) {
|
|
|
|
_BlockWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Delete a block window
|
|
|
|
BlockWindow::~BlockWindow() {
|
|
|
|
Fl::remove_timeout((Fl_Timeout_Handler)timeout_cb, (void *)this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize a block window...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::_BlockWindow() {
|
2006-08-25 10:58:33 +04:00
|
|
|
init();
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
help_button_ = new Fl_Button(0, 0, 20, 20, "?");
|
|
|
|
help_button_->callback((Fl_Callback *)help_cb, this);
|
|
|
|
help_button_->shortcut('?');
|
|
|
|
|
|
|
|
play_button_ = new Fl_Button(80, (h() - 80) / 2, 80, 80, "@>");
|
|
|
|
play_button_->callback((Fl_Callback *)play_cb, this);
|
|
|
|
play_button_->labelsize(44);
|
|
|
|
play_button_->shortcut(' ');
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
sound_ = new BlockSound();
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
prefs_.get("high_score", high_score_, 0);
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
Fl::add_timeout(0.1, (Fl_Timeout_Handler)timeout_cb, (void *)this);
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Bomb all blocks of a given color and return the number of affected blocks
|
2017-07-25 20:25:51 +03:00
|
|
|
int BlockWindow::bomb(int color) {
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k;
|
2006-08-25 10:58:33 +04:00
|
|
|
int count;
|
|
|
|
Block *b;
|
|
|
|
Column *c;
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (color >= BLOCK_BLAST) return (0);
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = num_columns_, c = columns_, count = 1; j > 0; j --, c ++)
|
|
|
|
for (k = c->num_blocks, b = c->blocks; k > 0; k --, b ++)
|
2006-08-27 00:01:24 +04:00
|
|
|
if (b->color == color) {
|
2006-08-25 10:58:33 +04:00
|
|
|
b->color = -color;
|
|
|
|
count ++;
|
|
|
|
}
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Tag all blocks connected to the clicked block and return the number
|
|
|
|
// of affected blocks
|
2017-07-25 20:25:51 +03:00
|
|
|
int BlockWindow::click(int col, int row) {
|
2006-08-25 10:58:33 +04:00
|
|
|
Block *b;
|
|
|
|
Column *c;
|
|
|
|
int count, color;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
c = columns_ + col;
|
|
|
|
b = c->blocks + row;
|
|
|
|
color = b->color;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
if (color < 0 || color >= BLOCK_BLAST) return (0);
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
// Find the bottom block...
|
2006-08-27 00:01:24 +04:00
|
|
|
while (row > 0 && b[-1].color == color) {
|
2006-08-25 10:58:33 +04:00
|
|
|
row --;
|
|
|
|
b --;
|
|
|
|
}
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
count = 0;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
while (row < c->num_blocks && b->color == color) {
|
2006-08-25 10:58:33 +04:00
|
|
|
b->color = -color;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
if (col > 0 && row < c[-1].num_blocks &&
|
2006-08-27 00:01:24 +04:00
|
|
|
c[-1].blocks[row].color == color) {
|
2006-08-25 10:58:33 +04:00
|
|
|
count += click(col - 1, row);
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
if (col < (num_columns_ - 1) && row < c[1].num_blocks &&
|
2006-08-27 00:01:24 +04:00
|
|
|
c[1].blocks[row].color == color) {
|
2006-08-25 10:58:33 +04:00
|
|
|
count += click(col + 1, row);
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
count ++;
|
|
|
|
row ++;
|
|
|
|
b ++;
|
|
|
|
}
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Draw the block window...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::draw() {
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k, xx, yy;
|
2006-08-25 10:58:33 +04:00
|
|
|
Block *b;
|
|
|
|
Column *c;
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Draw the blocks...
|
2006-08-25 10:58:33 +04:00
|
|
|
fl_color(FL_BLACK);
|
|
|
|
fl_rectf(0, 0, w(), h());
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Draw the blocks...
|
2007-05-20 04:01:06 +04:00
|
|
|
for (j = num_columns_, c = columns_; j > 0; j --, c ++)
|
|
|
|
for (k = c->num_blocks, b = c->blocks; k > 0; k --, b ++) {
|
2006-08-25 10:58:33 +04:00
|
|
|
xx = w() - c->x;
|
|
|
|
yy = h() - BLOCK_SIZE - b->y;
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (b->color >= BLOCK_BLAST) {
|
2006-08-25 10:58:33 +04:00
|
|
|
b->color ++;
|
|
|
|
blast_pixmap.draw(xx, yy);
|
2006-08-27 00:01:24 +04:00
|
|
|
} else if (b->color < 0) {
|
|
|
|
if (b->bomb) bomb_pixmaps[-b->color - 1]->draw(xx, yy);
|
|
|
|
else normal_pixmaps[-b->color - 1]->draw(xx, yy);
|
|
|
|
} else {
|
|
|
|
if (b->bomb) bomb_pixmaps[b->color - 1]->draw(xx, yy);
|
|
|
|
else normal_pixmaps[b->color - 1]->draw(xx, yy);
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (interval_ < 0.0 || paused_) {
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
screen_tile.draw(0, 0, w(), h(), 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redraw the widgets...
|
|
|
|
play_button_->redraw();
|
|
|
|
help_button_->redraw();
|
|
|
|
draw_children();
|
|
|
|
|
|
|
|
// Draw any paused/game over/new game message...
|
|
|
|
if ((paused_ || interval_ < 0.0) && play_button_->w() == 80) {
|
2006-08-25 10:58:33 +04:00
|
|
|
const char *s;
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (help_) {
|
|
|
|
s = "Click on adjacent blocks of the same color. Clear all blocks "
|
|
|
|
"before they reach the left side.";
|
|
|
|
|
|
|
|
fl_font(FL_HELVETICA_BOLD, 24);
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
fl_draw(s, 171, 3, w() - 250, h() - 6,
|
|
|
|
(Fl_Align)(FL_ALIGN_WRAP | FL_ALIGN_LEFT));
|
|
|
|
|
|
|
|
fl_color(FL_YELLOW);
|
|
|
|
fl_draw(s, 168, 0, w() - 250, h(),
|
|
|
|
(Fl_Align)(FL_ALIGN_WRAP | FL_ALIGN_LEFT));
|
|
|
|
} else {
|
|
|
|
if (interval_ < 0.0) {
|
2006-08-25 10:58:33 +04:00
|
|
|
#ifdef DEBUG
|
2006-08-27 00:01:24 +04:00
|
|
|
// Show sample waveform...
|
|
|
|
short *sample_ptr;
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
for (i = 0; i < 2; i++) {
|
2006-08-27 00:01:24 +04:00
|
|
|
fl_color(FL_RED + i);
|
|
|
|
fl_begin_line();
|
|
|
|
for (j = 0, sample_ptr = sound_->sample_data + i;
|
|
|
|
j < sound_->sample_size;
|
|
|
|
j ++, sample_ptr += 2)
|
|
|
|
fl_vertex(j * w() / sound_->sample_size,
|
|
|
|
*sample_ptr * h() / 4 / 65534 + h() / 2);
|
|
|
|
fl_end_line();
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
#endif // DEBUG
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
if (num_columns_ && (time(NULL) & 7) < 4)
|
|
|
|
s = "Game Over";
|
|
|
|
else
|
|
|
|
s = "Block Attack!\nby Michael R Sweet";
|
|
|
|
} else
|
|
|
|
s = "Paused";
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
fl_font(FL_HELVETICA_BOLD, 32);
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
fl_draw(s, 6, 6, w() - 6, h() - 6, FL_ALIGN_CENTER);
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
fl_color(FL_YELLOW);
|
|
|
|
fl_draw(s, 0, 0, w(), h(), FL_ALIGN_CENTER);
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Draw the scores and level...
|
2006-08-25 10:58:33 +04:00
|
|
|
char s[255];
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
sprintf(s, " Score: %d", score_);
|
|
|
|
fl_color(FL_WHITE);
|
|
|
|
fl_font(FL_HELVETICA, 14);
|
2006-08-27 00:01:24 +04:00
|
|
|
fl_draw(s, 40, 0, w() - 40, 20, FL_ALIGN_LEFT);
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
sprintf(s, "High Score: %d ", high_score_);
|
|
|
|
fl_draw(s, 0, 0, w(), 20, FL_ALIGN_RIGHT);
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
if (level_ > 1 || title_y_ <= 0) {
|
2006-08-25 10:58:33 +04:00
|
|
|
sprintf(s, "Level: %d ", level_);
|
|
|
|
fl_draw(s, 0, 0, w(), 20, FL_ALIGN_CENTER);
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
if (title_y_ > 0 && interval_ > 0.0) {
|
2006-08-25 10:58:33 +04:00
|
|
|
int sz = 14 + title_y_ * 86 / h();
|
|
|
|
|
|
|
|
fl_font(FL_HELVETICA_BOLD, sz);
|
|
|
|
fl_color(FL_YELLOW);
|
|
|
|
fl_draw(title_, 0, title_y_, w(), sz, FL_ALIGN_CENTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Handle mouse clicks, etc.
|
2017-07-25 20:25:51 +03:00
|
|
|
int BlockWindow::handle(int event) {
|
2007-05-20 04:01:06 +04:00
|
|
|
int j, k, mx, my, count;
|
2006-08-25 10:58:33 +04:00
|
|
|
Block *b;
|
|
|
|
Column *c;
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (Fl_Double_Window::handle(event)) return (1);
|
2006-08-27 00:25:37 +04:00
|
|
|
else if (interval_ < 0.0 || paused_) return (0);
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
switch (event) {
|
2017-07-25 20:25:51 +03:00
|
|
|
|
2007-11-19 17:14:13 +03:00
|
|
|
case FL_KEYBOARD:
|
2017-07-25 20:25:51 +03:00
|
|
|
if (Fl::event_text()) {
|
|
|
|
if (strcmp(Fl::event_text(), "+") == 0)
|
|
|
|
up_level();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FL_PUSH:
|
|
|
|
mx = w() - Fl::event_x() + BLOCK_SIZE;
|
|
|
|
my = h() - Fl::event_y();
|
|
|
|
count = 0;
|
|
|
|
b = 0;
|
|
|
|
|
|
|
|
for (j = 0, c = columns_; !count && j < num_columns_; j ++, c ++)
|
|
|
|
for (k = 0, b = c->blocks; !count && k < c->num_blocks; k ++, b ++)
|
|
|
|
if (mx >= c->x && mx < (c->x + BLOCK_SIZE) &&
|
|
|
|
my >= b->y && my < (b->y + BLOCK_SIZE)) {
|
|
|
|
if (b->bomb) count = bomb(b->color);
|
|
|
|
else count = click(j, k);
|
|
|
|
break;
|
|
|
|
}
|
2006-08-27 00:01:24 +04:00
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
if (count < 2) {
|
|
|
|
for (j = 0, c = columns_; j < num_columns_; j ++, c ++)
|
|
|
|
for (k = 0, b = c->blocks; k < c->num_blocks; k ++, b ++)
|
|
|
|
if (b->color < 0) b->color = -b->color;
|
|
|
|
} else {
|
|
|
|
count --;
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
if (b->bomb) {
|
|
|
|
sound_->play_explosion(0.19 + 0.005 * count);
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2017-07-25 21:15:52 +03:00
|
|
|
interval_ *= BOMB_FACTOR;
|
2017-07-25 20:25:51 +03:00
|
|
|
score_ += count;
|
|
|
|
} else {
|
|
|
|
sound_->play_explosion(0.09 + 0.005 * count);
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2017-07-25 21:15:52 +03:00
|
|
|
interval_ *= NORMAL_FACTOR;
|
2017-07-25 20:25:51 +03:00
|
|
|
score_ += count * count;
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
if (score_ > high_score_) {
|
|
|
|
high_score_ = score_;
|
|
|
|
prefs_.set("high_score", high_score_);
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
2017-07-25 20:25:51 +03:00
|
|
|
|
|
|
|
for (j = 0, c = columns_; j < num_columns_; j ++, c ++)
|
|
|
|
for (k = 0, b = c->blocks; k < c->num_blocks; k ++, b ++)
|
|
|
|
if (b->color < 0) b->color = BLOCK_BLAST;
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
return (0);
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Toggle the on-line help...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::help_cb(Fl_Widget *, BlockWindow *bw) {
|
2006-08-27 00:01:24 +04:00
|
|
|
bw->paused_ = bw->help_ = !bw->help_;
|
|
|
|
bw->play_button_->label("@>");
|
|
|
|
bw->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the block window...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::init() {
|
2006-08-25 10:58:33 +04:00
|
|
|
count_ = 0;
|
2006-08-28 18:45:20 +04:00
|
|
|
help_ = false;
|
2006-08-25 10:58:33 +04:00
|
|
|
interval_ = -1.0;
|
|
|
|
level_ = 1;
|
|
|
|
num_colors_ = 3;
|
|
|
|
num_columns_ = 0;
|
|
|
|
paused_ = false;
|
|
|
|
score_ = 0;
|
|
|
|
title_[0] = '\0';
|
|
|
|
title_y_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Start a new game...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::new_game() {
|
2006-08-27 00:01:24 +04:00
|
|
|
// Seed the random number generator...
|
2006-08-25 10:58:33 +04:00
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
interval_ = 0.1;
|
2006-08-25 10:58:33 +04:00
|
|
|
opened_columns_ = 0;
|
|
|
|
|
|
|
|
strcpy(title_, "Level: 1");
|
|
|
|
title_y_ = h();
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Play/pause...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::play_cb(Fl_Widget *wi, BlockWindow *bw) {
|
2006-08-27 00:01:24 +04:00
|
|
|
if (bw->interval_ < 0) bw->new_game();
|
|
|
|
else bw->paused_ = !bw->paused_;
|
|
|
|
|
|
|
|
if (bw->paused_) wi->label("@>");
|
|
|
|
else {
|
|
|
|
wi->label("@-2||");
|
|
|
|
bw->help_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 17:14:13 +03:00
|
|
|
void BlockWindow::up_level() {
|
2017-07-25 21:15:52 +03:00
|
|
|
interval_ *= LEVEL_FACTOR;
|
2007-11-19 17:14:13 +03:00
|
|
|
opened_columns_ = 0;
|
|
|
|
if (num_colors_ < 7) num_colors_ ++;
|
|
|
|
level_ ++;
|
|
|
|
sprintf(title_, "Level: %d", level_);
|
|
|
|
title_y_ = h();
|
|
|
|
}
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
// Animate the game...
|
2017-07-25 20:25:51 +03:00
|
|
|
void BlockWindow::timeout_cb(BlockWindow *bw) {
|
2006-08-25 10:58:33 +04:00
|
|
|
int i, j;
|
|
|
|
Block *b;
|
|
|
|
Column *c;
|
|
|
|
int lastx, lasty;
|
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
#if DEBUG_TIMER
|
|
|
|
static double lasttime;
|
|
|
|
static double delta_sum;
|
|
|
|
static double interval;
|
|
|
|
double curtime;
|
|
|
|
static int ntime = 0;
|
|
|
|
static int level = 0;
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
#if !defined(WIN32)
|
|
|
|
{
|
|
|
|
struct timeval atime;
|
|
|
|
gettimeofday(&atime, NULL);
|
|
|
|
curtime = atime.tv_sec % 60 + 0.000001 * atime.tv_usec;
|
|
|
|
}
|
|
|
|
#else // (WIN32)
|
|
|
|
{
|
|
|
|
SYSTEMTIME atime;
|
|
|
|
GetLocalTime(&atime);
|
|
|
|
curtime = atime.wSecond + 0.001 * atime.wMilliseconds;
|
|
|
|
}
|
|
|
|
#endif // (WIN32)
|
|
|
|
|
|
|
|
// platform independent part of timer debugging code
|
|
|
|
if (bw->interval_ > 0) { // game is active
|
|
|
|
if (bw->level_ != level) {
|
|
|
|
if (ntime > 0) {
|
|
|
|
printf("*** average delta time = %9.6f, n =%4d, level %d, interval %f\n",
|
|
|
|
delta_sum / ntime, ntime, level, interval);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
delta_sum = 0; // reset average
|
|
|
|
ntime = 0;
|
|
|
|
interval = bw->interval_;
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2017-07-25 20:25:51 +03:00
|
|
|
double delta = curtime - lasttime;
|
|
|
|
if (delta < 0)
|
|
|
|
delta += 60;
|
|
|
|
|
|
|
|
printf("%9.6f (%+f - %f = %9.6f), level: %d\n",
|
|
|
|
curtime, delta, interval, delta - interval, level);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
interval = bw->interval_;
|
|
|
|
level = bw->level_;
|
|
|
|
delta = delta - interval;
|
|
|
|
delta_sum += delta > 0 ? delta : -delta; // abs(delta)
|
|
|
|
ntime++;
|
|
|
|
} else { // waiting ...
|
|
|
|
// printf("[OFF] %6.2f\n", curtime);
|
|
|
|
// fflush(stdout);
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
lasttime = curtime;
|
2017-07-25 20:25:51 +03:00
|
|
|
#endif // DEBUG_TIMER
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Update blocks that have been destroyed...
|
2006-08-25 10:58:33 +04:00
|
|
|
for (i = 0, c = bw->columns_; i < bw->num_columns_; i ++, c ++)
|
|
|
|
for (j = 0, b = c->blocks; j < c->num_blocks; j ++, b ++)
|
2006-08-27 00:01:24 +04:00
|
|
|
if (b->color > (BLOCK_BLAST + 1)) {
|
|
|
|
bw->redraw();
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
c->num_blocks --;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
if (j < c->num_blocks) {
|
2006-08-25 10:58:33 +04:00
|
|
|
memmove(b, b + 1, (c->num_blocks - j) * sizeof(Block));
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
|
|
|
|
2006-08-25 10:58:33 +04:00
|
|
|
j --;
|
|
|
|
b --;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
if (!c->num_blocks) {
|
2006-08-25 10:58:33 +04:00
|
|
|
bw->num_columns_ --;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
if (i < bw->num_columns_) {
|
2006-08-25 10:58:33 +04:00
|
|
|
memmove(c, c + 1, (bw->num_columns_ - i) * sizeof(Column));
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
i --;
|
|
|
|
c --;
|
|
|
|
j = c->num_blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Let the rest of the blocks fall and/or move...
|
2017-07-25 20:25:51 +03:00
|
|
|
for (i = bw->num_columns_, c = bw->columns_, lastx = c->x; i > 0; i--, c++) {
|
2006-08-27 00:01:24 +04:00
|
|
|
if (c->x > lastx) {
|
2006-08-25 10:58:33 +04:00
|
|
|
c->x -= 8;
|
2006-08-27 00:01:24 +04:00
|
|
|
bw->redraw();
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
lastx = c->x + BLOCK_SIZE;
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (!bw->paused_ && bw->interval_ > 0.0) {
|
|
|
|
bw->redraw();
|
|
|
|
c->x ++;
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
for (j = c->num_blocks, b = c->blocks, lasty = 0; j > 0; j --, b ++) {
|
|
|
|
if (b->y > lasty) {
|
|
|
|
bw->redraw();
|
2006-08-25 10:58:33 +04:00
|
|
|
b->y -= 8;
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
|
|
|
lasty = b->y + BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Slide the title text as needed...
|
|
|
|
if (bw->title_y_ > 0) {
|
|
|
|
bw->redraw();
|
|
|
|
bw->title_y_ -= 5;
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Play the game...
|
|
|
|
if (!bw->paused_ && bw->interval_ > 0.0) {
|
|
|
|
bw->count_ --;
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (bw->count_ <= 0) {
|
2006-08-25 10:58:33 +04:00
|
|
|
bw->redraw();
|
2006-08-27 00:01:24 +04:00
|
|
|
bw->count_ = BLOCK_SIZE;
|
|
|
|
|
|
|
|
if (bw->num_columns_ == BLOCK_COLS) {
|
|
|
|
bw->interval_ = -1.0;
|
|
|
|
bw->sound_->play_explosion(0.8);
|
|
|
|
bw->play_button_->label("@>");
|
|
|
|
} else {
|
|
|
|
bw->opened_columns_ ++;
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (bw->opened_columns_ > (2 * BLOCK_COLS)) {
|
2007-11-19 17:14:13 +03:00
|
|
|
bw->up_level();
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
c = bw->columns_;
|
|
|
|
|
|
|
|
if (bw->num_columns_) {
|
|
|
|
memmove(c + 1, c, bw->num_columns_ * sizeof(Column));
|
|
|
|
}
|
|
|
|
|
|
|
|
bw->num_columns_ ++;
|
|
|
|
c->x = 0;
|
|
|
|
c->num_blocks = BLOCK_ROWS;
|
|
|
|
|
|
|
|
for (j = 0, b = c->blocks; j < BLOCK_ROWS; j ++, b ++) {
|
|
|
|
b->bomb = bw->num_colors_ > 3 && (rand() & 127) < bw->num_colors_;
|
|
|
|
b->color = 1 + (rand() % bw->num_colors_);
|
|
|
|
b->y = j * (BLOCK_SIZE + 8) + 24;
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 20:25:51 +03:00
|
|
|
} else {
|
2006-08-27 00:01:24 +04:00
|
|
|
bw->count_ --;
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
if (bw->count_ <= 0) {
|
|
|
|
bw->count_ = 40;
|
|
|
|
bw->redraw();
|
|
|
|
}
|
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
|
2006-08-27 00:01:24 +04:00
|
|
|
// Update the play/pause button as needed...
|
|
|
|
if ((bw->paused_ || bw->interval_< 0.0) &&
|
|
|
|
bw->play_button_->w() < 80) {
|
2006-08-27 00:25:37 +04:00
|
|
|
int s = bw->play_button_->w() + 10;
|
2006-08-27 00:01:24 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
bw->play_button_->resize(s, (s - 20) * (bw->h() - s) / 120, s, s);
|
|
|
|
bw->play_button_->labelsize(s / 2 + 4);
|
|
|
|
bw->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bw->interval_ > 0.0) {
|
2017-07-25 20:25:51 +03:00
|
|
|
Fl::repeat_timeout(bw->interval_, (Fl_Timeout_Handler)timeout_cb, (void *)bw);
|
2006-08-27 00:01:24 +04:00
|
|
|
} else {
|
2017-07-25 20:25:51 +03:00
|
|
|
Fl::repeat_timeout(0.1, (Fl_Timeout_Handler)timeout_cb, (void *)bw);
|
2006-08-27 00:01:24 +04:00
|
|
|
}
|
2006-08-25 10:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// End of "$Id$".
|
|
|
|
//
|