Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F127061
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
42 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/bitmap.cpp b/util/bitmap.cpp
index c7de7feb..69d8f4ae 100644
--- a/util/bitmap.cpp
+++ b/util/bitmap.cpp
@@ -1,739 +1,738 @@
#include <allegro.h>
#include "bitmap.h"
-#include "font.h"
#include <stdarg.h>
#include <vector>
#include <string>
#include <iostream>
// #include <fblend.h>
#ifdef WINDOWS
#include <winalleg.h>
#endif
using namespace std;
#ifndef debug
#define debug cout<<"File: "<<__FILE__<<" Line: "<<__LINE__<<endl;
#endif
const int Bitmap::MaskColor = MASK_COLOR_16;
const int Bitmap::MODE_TRANS = 0;
const int Bitmap::MODE_SOLID = 1;
Bitmap * Bitmap::Screen;
Bitmap::Bitmap():
error( false ){
setBitmap( create_bitmap( 10, 10 ) );
if ( ! getBitmap() ){
error = true;
cerr << "Could not create bitmap!" << endl;
} else {
clear();
own = new int;
*own = 1;
}
}
Bitmap::Bitmap( int x, int y ):
error( false ){
if ( x < 1 ){
x = 1;
}
if ( y < 1 ){
y = 1;
}
setBitmap( create_bitmap( x, y ) );
if ( ! getBitmap() ){
error = true;
cerr << "Could not create bitmap!" << endl;
} else {
clear();
own = new int;
*own = 1;
}
}
/* If a BITMAP is given to us, we didnt make it so we dont own it */
Bitmap::Bitmap( BITMAP * who, bool deep_copy ):
error( false ){
if ( deep_copy ){
BITMAP * his = who;
setBitmap( create_bitmap( his->w, his->h ) );
::blit( his, getBitmap(), 0, 0, 0, 0, his->w, his->h );
own = new int;
*own = 1;
} else {
setBitmap( who );
own = NULL;
}
}
Bitmap::Bitmap( const char * load_file ):
error( false ){
internalLoadFile( load_file );
/*
my_bitmap = load_bitmap( load_file, NULL );
if ( !my_bitmap ){
my_bitmap = create_bitmap( 100, 100 );
clear( my_bitmap );
cout<<"Could not load "<<load_file<<endl;
error = true;
}
own = true;
*/
}
Bitmap::Bitmap( const string & load_file ):
error( false ){
internalLoadFile( load_file.c_str() );
}
Bitmap::Bitmap( const char * load_file, int sx, int sy ):
error( false ){
path = load_file;
BITMAP * temp = load_bitmap( load_file, NULL );
// my_bitmap = load_bitmap( load_file, NULL );
setBitmap( create_bitmap( sx, sy ) );
// clear( my_bitmap );
clear();
if ( !temp ){
cout<<"Could not load "<<load_file<<endl;
error = true;
} else {
stretch_blit( temp, getBitmap(), 0, 0, temp->w, temp->h, 0, 0, getBitmap()->w, getBitmap()->h );
destroy_bitmap( temp );
}
// own = true;
own = new int;
*own = 1;
}
Bitmap::Bitmap( const char * load_file, int sx, int sy, double accuracy ):
error( false ){
path = load_file;
BITMAP * temp = load_bitmap( load_file, NULL );
own = new int;
*own = 1;
if ( !temp ){
cout<<"Could not load "<<load_file<<endl;
setBitmap( create_bitmap( sx, sy ) );
// clear( my_bitmap );
clear();
error = true;
} else {
if ( temp->w > sx || temp->h > sy ){
double bx = temp->w / sx;
double by = temp->h / sy;
double use;
use = bx > by ? bx : by;
int fx = (int)(sx / use);
int fy = (int)(sy / use);
setBitmap( create_bitmap( fx, fy ) );
stretch_blit( temp, getBitmap(), 0, 0, temp->w, temp->h, 0, 0, getBitmap()->w, getBitmap()->h );
destroy_bitmap( temp );
} else setBitmap( temp );
}
// own = true;
}
Bitmap::Bitmap( const Bitmap & copy, int sx, int sy ):
error( false ){
path = copy.getPath();
BITMAP * temp = copy.getBitmap();
setBitmap( create_bitmap( sx, sy ) );
// clear( my_bitmap );
clear();
stretch_blit( temp, getBitmap(), 0, 0, temp->w, temp->h, 0, 0, getBitmap()->w, getBitmap()->h );
// own = true;
// own = copy.own;
own = new int;
*own = 1;
}
Bitmap::Bitmap( const Bitmap & copy, int sx, int sy, double accuracy ):
error( false ){
path = copy.getPath();
BITMAP * temp = copy.getBitmap();
own = new int;
*own = 1;
if ( temp->w > sx || temp->h > sy ){
double bx = (double)temp->w / (double)sx;
double by = (double)temp->h / (double)sy;
double use;
use = bx > by ? bx : by;
int fx = (int)(temp->w / use);
int fy = (int)(temp->h / use);
setBitmap( create_bitmap( fx, fy ) );
if ( ! getBitmap() ){
allegro_message("Could not create bitmap\n");
// own = false;
// own = copy.own;
// *own++;
error = true;
return;
}
stretch_blit( temp, getBitmap(), 0, 0, temp->w, temp->h, 0, 0, getBitmap()->w, getBitmap()->h );
// destroy_bitmap( temp );
} else {
setBitmap( create_bitmap( temp->w, temp->h ) );
blit( temp, getBitmap(), 0, 0, 0, 0, temp->w, temp->h );
// own = new int
// own = true;
}
}
Bitmap::Bitmap( const Bitmap & copy, bool deep_copy ):
error( false ){
path = copy.getPath();
if ( deep_copy ){
BITMAP * his = copy.getBitmap();
setBitmap( create_bitmap( his->w, his->h ) );
::blit( his, getBitmap(), 0, 0, 0, 0, his->w, his->h );
own = new int;
*own = 1;
} else {
setBitmap( copy.getBitmap() );
// own = false;
own = copy.own;
if ( own ){
*own = *own + 1;
}
}
/*
BITMAP * his = copy.getBitmap();
my_bitmap = create_bitmap( his->w, his->h );
::blit( his, my_bitmap, 0, 0, 0, 0, his->w, his->h );
own = true;
*/
}
Bitmap::Bitmap( const Bitmap & copy, int x, int y, int width, int height ):
error( false ){
path = copy.getPath();
BITMAP * his = copy.getBitmap();
if ( x < 0 )
x = 0;
if ( y < 0 )
y = 0;
if ( width > his->w )
width = his->w;
if ( height > his->h )
height = his->h;
setBitmap( create_sub_bitmap( his, x, y, width, height ) );
if ( ! getBitmap() ){
cout<<"Could not create sub-bitmap"<<endl;
setBitmap( create_bitmap( 10, 10 ) );
// clear( my_bitmap );
clear();
}
// own = true;
own = new int;
*own = 1;
}
void Bitmap::load( const string & str ){
releaseInternalBitmap();
internalLoadFile( str.c_str() );
}
void Bitmap::internalLoadFile( const char * load_file ){
path = load_file;
setBitmap( load_bitmap( load_file, NULL ) );
if ( ! getBitmap() ){
cout<<"Could not load "<<load_file<<". Using default"<<endl;
setBitmap( create_bitmap( 10, 10 ) );
if ( ! getBitmap() ){
cout<<"Out of memory or Allegro not initialized"<<endl;
error = true;
return;
}
// clear( my_bitmap );
clear();
// cout<<"Could not load "<<load_file<<endl;
error = true;
}
// own = true;
own = new int;
*own = 1;
}
void Bitmap::save( const string & str ){
save_bitmap( str.c_str(), getBitmap(), NULL );
}
/* decrement bitmap reference counter and free memory if counter hits 0 */
void Bitmap::releaseInternalBitmap(){
if ( own ){
(*own)--;
if ( *own == 0 ){
delete own;
destroy_bitmap( getBitmap() );
own = NULL;
}
}
}
const int Bitmap::getWidth() const{
return getBitmap()->w;
}
const int Bitmap::getHeight() const{
return getBitmap()->h;
}
int Bitmap::getRed( int x ){
return ::getr( x );
}
int Bitmap::getBlue( int x ){
return ::getg( x );
}
int Bitmap::getGreen( int x ){
return ::getb( x );
}
/* resize the internal bitmap. not guaranteed to destroy the internal bitmap */
void Bitmap::resize( const int width, const int height ){
/* if internal bitmap is already the proper size, do nothing */
if ( getWidth() == width && getHeight() == height ){
return;
}
BITMAP * b = create_bitmap( width, height );
::stretch_blit( getBitmap(), b, 0, 0, getBitmap()->w, getBitmap()->h, 0, 0, b->w, b->h );
releaseInternalBitmap();
own = new int;
setBitmap( b );
*own = 1;
}
/*
const string & Bitmap::getPath() const{
return path;
}
*/
void Bitmap::debugSelf() const{
cout<<"Bitmap: "<<endl;
cout<<"Self = "<< getBitmap() <<endl;
cout<<"Own = "<< own <<" : "<< *own <<endl;
cout<<"Path = "<<path<<endl;
}
Bitmap & Bitmap::operator=( const Bitmap & copy ){
/*
if ( own ){
(*own)--;
if ( *own == 0 ){
destroy_bitmap( getBitmap() );
delete own;
}
}
*/
releaseInternalBitmap();
path = copy.getPath();
setBitmap( copy.getBitmap() );
// own = false;
own = copy.own;
if ( own )
*own++;
return *this;
}
Bitmap::~Bitmap(){
/*
if ( own ){
(*own)--;
if ( *own == 0 ){
destroy_bitmap( getBitmap() );
delete own;
} else {
// cout<<"Not destroying bitmap "<<this<<endl;
}
}
*/
releaseInternalBitmap();
}
void Bitmap::detach(){
/*
if ( own ){
(*own)--;
if ( *own == 0 ){
destroy_bitmap( getBitmap() );
delete own;
}
}
*/
releaseInternalBitmap();
}
/*
BITMAP * Bitmap::getBitmap() const{
return my_bitmap;
}
*/
bool Bitmap::getError(){
return error;
}
/*
int Bitmap::getWidth(){
return my_bitmap->w;
}
int Bitmap::getHeight(){
return my_bitmap->h;
}
*/
void Bitmap::acquire(){
acquire_bitmap( getBitmap() );
}
void Bitmap::release(){
release_bitmap( getBitmap() );
}
void Bitmap::circleFill( int x, int y, int radius, int color ) const{
::circlefill( getBitmap(), x, y, radius, color );
}
void Bitmap::circle( int x, int y, int radius, int color ) const{
::circle( getBitmap(), x, y, radius, color );
}
void Bitmap::line( const int x1, const int y1, const int x2, const int y2, const int color ) const{
::line( getBitmap(), x1, y1, x2, y2, color );
}
void Bitmap::transBlender( int r, int g, int b, int a ){
set_trans_blender( r, g, b, a );
}
void Bitmap::multiplyBlender( int r, int g, int b, int a ){
set_multiply_blender( r, g, b, a );
}
void Bitmap::drawingMode( int mode ){
// drawing_mode( DRAW_MODE_TRANS, NULL, 0, 0 );
switch( mode ){
case MODE_TRANS : {
drawing_mode( DRAW_MODE_TRANS, NULL, 0, 0 );
break;
}
case MODE_SOLID : {
drawing_mode( DRAW_MODE_SOLID, NULL, 0, 0 );
break;
}
}
}
/*
const int Bitmap::getWidth() const{
return getBitmap()->w;
}
const int Bitmap::getHeight() const{
return getBitmap()->h;
}
*/
const int Bitmap::getPixel( const int x, const int y ) const{
if ( x >= 0 && x < getBitmap()->w && y >= 0 && y < getBitmap()->h )
return _getpixel16( getBitmap(), x, y );
return -1;
}
void Bitmap::readLine( vector< int > & vec, int y ){
if ( y >= 0 && y < getBitmap()->h ){
for ( int q = 0; q < getBitmap()->w; q++ ){
// int col = my_bitmap->line[ y ][ q ];
int col = _getpixel16( getBitmap(), q, y );
vec.push_back( col );
}
}
}
int Bitmap::setGfxModeText(){
return ::set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
}
int Bitmap::setGfxModeFullscreen( int x, int y ){
return ::set_gfx_mode( GFX_AUTODETECT_FULLSCREEN, x, y, 0, 0 );
}
int Bitmap::setGfxModeWindowed( int x, int y ){
return ::set_gfx_mode( GFX_AUTODETECT_WINDOWED, x, y, 0, 0 );
}
int Bitmap::makeColor( int r, int g, int b ){
return ::makecol16( r, g, b );
}
void Bitmap::hsvToRGB( float h, float s, float v, int * r, int * g, int * b ){
::hsv_to_rgb( h, s, v, r, g, b );
}
int Bitmap::addColor( int color1, int color2 ){
return makeColor( getr( color1 ) + getr( color2 ),
getg( color1 ) + getg( color2 ),
getb( color1 ) + getb( color2 ) );
}
void Bitmap::putPixel( int x, int y, int col ) const{
if ( x >= 0 && x < getBitmap()->w && y >= 0 && y < getBitmap()->h )
_putpixel16( getBitmap(), x, y, col );
}
/*
void Bitmap::printf( int x, int y, int color, FONT * f, const char * str, ... ) const{
char buf[512];
va_list ap;
va_start(ap, str);
uvszprintf(buf, sizeof(buf), str, ap);
va_end(ap);
textout_ex( getBitmap(), f, buf, x, y, color, -1);
}
void Bitmap::printf( int x, int y, int color, const Font * const f, const char * str, ... ) const{
char buf[512];
va_list ap;
va_start(ap, str);
uvszprintf(buf, sizeof(buf), str, ap);
va_end(ap);
textout_ex( getBitmap(), f->getInternalFont(), buf, x, y, color, -1);
}
void Bitmap::printf( int x, int y, int color, const Font * const f, const string & str ) const{
printf( x, y, color, f, str.c_str() );
}
void Bitmap::printf( int x, int y, int color, const Font & f, const string & str ) const{
printf( x, y, color, &f, str );
}
void Bitmap::printfNormal( int x, int y, int color, const char * str, ... ) const{
char buf[512];
va_list ap;
va_start(ap, str);
uvszprintf(buf, sizeof(buf), str, ap);
va_end(ap);
textout_ex( getBitmap(), font, buf, x, y, color, -1);
}
void Bitmap::printfNormal( int x, int y, int color, const string & str ) const{
printfNormal( x, y, color, "%s", str.c_str() );
}
*/
void Bitmap::triangle( int x1, int y1, int x2, int y2, int x3, int y3, int color ) const{
::triangle( getBitmap(), x1, y1, x2, y2, x3, y3, color );
}
void Bitmap::ellipse( int x, int y, int rx, int ry, int color ) const {
::ellipse( getBitmap(), x, y, rx, ry, color );
}
void Bitmap::ellipseFill( int x, int y, int rx, int ry, int color ) const {
::ellipsefill( getBitmap(), x, y, rx, ry, color );
}
void Bitmap::rectangle( int x1, int y1, int x2, int y2, int color ) const{
::rect( getBitmap(), x1, y1, x2, y2, color );
}
void Bitmap::rectangleFill( int x1, int y1, int x2, int y2, int color ) const{
::rectfill( getBitmap(), x1, y1, x2, y2, color );
}
/*
int Bitmap::getPixel( int x, int y ){
if ( x >= 0 && x < my_bitmap->w && y >= 0 && y <= my_bitmap->h )
return _getpixel16( my_bitmap, x, y );
return -1;
}
*/
/*
int Bitmap::makeColor( int r, int g, int b ){
return makecol16( r, g, b );
}
*/
void Bitmap::hLine( const int x1, const int y, const int x2, const int color ) const{
::hline( getBitmap(), x1, y, x2, color );
}
void Bitmap::horizontalLine( const int x1, const int y, const int x2, const int color ) const{
this->hLine( x1, y, x2, color );
}
void Bitmap::vLine( const int y1, const int x, const int y2, const int color ) const{
::vline( getBitmap(), x, y1, y2, color );
}
void Bitmap::polygon( const int * verts, const int nverts, const int color ) const{
::polygon( getBitmap(), nverts, verts, color );
}
/*
void Bitmap::clear(){
this->fill( 0 );
}
*/
void Bitmap::fill( int color ) const{
::clear_to_color( getBitmap(), color );
}
void Bitmap::draw( const int x, const int y, const Bitmap & where ) const {
::draw_sprite( where.getBitmap(), getBitmap(), x, y );
}
void Bitmap::drawHFlip( const int x, const int y, const Bitmap & where ) const {
::draw_sprite_h_flip( where.getBitmap(), getBitmap(), x, y );
}
void Bitmap::drawLit( const int x, const int y, const int level, const Bitmap & where ) const{
::draw_lit_sprite( where.getBitmap(), getBitmap(), x, y, level );
}
void Bitmap::drawTrans( const int x, const int y, const Bitmap & where ) const{
::draw_trans_sprite( where.getBitmap(), getBitmap(), x, y );
}
void Bitmap::drawRotate( const int x, const int y, const int angle, const Bitmap & where ){
::fixed fang = itofix( (360 - angle) % 360 * 256 / 360 );
::rotate_sprite( where.getBitmap(), getBitmap(), x, y, fang );
}
void Bitmap::drawStretched( const int x, const int y, const int new_width, const int new_height, const Bitmap & who ){
BITMAP * bmp = who.getBitmap();
::masked_stretch_blit( getBitmap(), bmp, 0, 0, getBitmap()->w, getBitmap()->h, x,y, new_height, new_width );
}
void Bitmap::drawMask( const int _x, const int _y, const Bitmap & where ){
int mask = Bitmap::MaskColor;
for ( int x = 0; x < getWidth(); x++ ){
for ( int y = 0; y < getHeight(); y++ ){
if ( getPixel( x,y ) == mask ){
where.putPixel( x+_x, y+_y, mask );
}
}
}
}
void Bitmap::StretchBy2( const Bitmap & where ){
BITMAP * bmp = where.getBitmap();
if ( where.getWidth() == getWidth() && where.getHeight() == getHeight() ){
::blit( getBitmap(), bmp, 0, 0, 0, 0, getBitmap()->w, getBitmap()->h );
return;
}
if ( where.getWidth() != getWidth()*2 ||
where.getHeight() != getHeight()*2 ){
cout<<"Wrong dimensions"<<endl;
cout<<"My: "<< getWidth() << " " << getHeight() << endl;
cout<<"Him: "<<where.getWidth()<< " " << where.getHeight()<<endl;
return;
}
// debug
::stretch_blit( getBitmap(), bmp, 0, 0, getBitmap()->w, getBitmap()->h, 0, 0, bmp->w, bmp->h );
// fblend_2x_stretch( my_bitmap, bmp, 0, 0, 0, 0, my_bitmap->w, my_bitmap->h);
// scale2x_allegro( my_bitmap, bmp, 2 );
// debug
}
void Bitmap::StretchBy4( const Bitmap & where ){
BITMAP * bmp = where.getBitmap();
if ( where.getWidth() != getWidth()*4 ||
where.getHeight() != getHeight()*4 ){
cout<<"Wrong dimensions"<<endl;
cout<<"My: "<< getWidth() << " " << getHeight() << endl;
cout<<"Him: "<<where.getWidth()<< " " << where.getHeight()<<endl;
cout<<"Scaled: "<<getWidth()*4<<" "<< getHeight()*4<<endl;
return;
}
// fblend_2x_stretch( my_bitmap, bmp, 0, 0, 0, 0, my_bitmap->w, my_bitmap->h);
// scale4x_allegro( my_bitmap, bmp, 2 );
::stretch_blit( getBitmap(), bmp, 0, 0, getBitmap()->w, getBitmap()->h, 0, 0, bmp->w, bmp->h );
}
void Bitmap::Stretch( const Bitmap & where ){
BITMAP * bmp = where.getBitmap();
::stretch_blit( getBitmap(), bmp, 0, 0, getBitmap()->w, getBitmap()->h, 0, 0, bmp->w, bmp->h );
}
void Bitmap::Blit( const string & xpath ){
Bitmap duh( xpath );
duh.Blit( *this );
}
void Bitmap::Blit( const int x, const int y, const Bitmap & where ){
BITMAP * bmp = where.getBitmap();
/*
acquire_bitmap( bmp );
acquire_bitmap( my_bitmap );
*/
::blit( getBitmap(), bmp, 0, 0, x, y, getBitmap()->w, getBitmap()->h );
/*
release_bitmap( my_bitmap );
release_bitmap( bmp );
*/
}
void Bitmap::Blit( const int mx, const int my, const int wx, const int wy, const Bitmap & where ){
BITMAP * bmp = where.getBitmap();
::blit( getBitmap(), bmp, mx, my, wx, wy, getBitmap()->w, getBitmap()->h );
}
void Bitmap::Blit( const int mx, const int my, const int width, const int height, const int wx, const int wy, Bitmap & where ){
BITMAP * bmp = where.getBitmap();
::blit( getBitmap(), bmp, mx, my, wx, wy, width, height );
}
void Bitmap::Blit( const Bitmap & where ){
this->Blit( 0, 0, where );
}
void Bitmap::BlitToScreen(){
this->Blit( *Bitmap::Screen );
}
diff --git a/util/ebox.cpp b/util/ebox.cpp
index a03f292f..d338f8e9 100644
--- a/util/ebox.cpp
+++ b/util/ebox.cpp
@@ -1,579 +1,713 @@
-/* ebox version 2:
+/* ebox version 3:
* by Jon Rafkind
*/
#include "ebox.h"
#include <stdio.h>
+#include "funcs.h"
#include "bitmap.h"
#include <iostream>
-#include "timedifference.h"
+
+extern "C" void rest( int i );
// using namespace Ebox2;
#ifndef debug
// #define debug printf("File: %s Line: %d\n", __FILE__, __LINE__ );
#define debug std::cout<<"File: "<<__FILE__<<" Line: "<<__LINE__<<std::endl;
#endif
// #define MIN_SIZE 8
const int MIN_SIZE = 8;
+/* copy constructor but with a pointer */
EQuad::EQuad( EQuad * const head ):
width( head->width ),
height( head->height ),
full( head->full ),
min_x( head->min_x ),
min_y( head->min_y ),
parent( NULL ),
num_quads( head->num_quads ){
for ( int q = 0; q < 4; q++ )
quads[q] = NULL;
for ( int i = 0; i < head->numQuads(); i++ ){
// EQuad * tmp = head->getQuad( i );
quads[i] = new EQuad( head->getQuad( i ) );
quads[i]->parent = this;
}
}
EQuad::EQuad( int w, int h, EQuad * _parent ):
width( w ),
height( h ),
min_x( 0 ),
min_y( 0 ),
parent( _parent ),
num_quads( 0 ){
full = true;
for ( int i = 0; i < 4; i++ )
quads[i] = NULL;
}
EQuad::EQuad( const Bitmap * who, int min_size, int mask_pixel, int _min_x, int _min_y, EQuad * _parent ):
width( who->getWidth() ),
height( who->getHeight() ),
full( false ),
min_x( _min_x ),
min_y( _min_y ),
parent( _parent ){
Bitmap * b1, * b2, * b3, * b4;
EQuad * quad1, * quad2, * quad3, * quad4;
quad1 = quad2 = quad3 = quad4 = NULL;
if ( width > min_size && height > min_size ){
int w = who->getWidth() >> 1;
int h = who->getHeight() >> 1;
// b1 = create_sub_Bitmap( who, 0, 0, w, h );
b1 = new Bitmap( *who, 0, 0, w, h );
quad1 = new EQuad( b1, min_size, mask_pixel, 0, 0, this );
// destroy_Bitmap( b1 );
delete b1;
// b2 = create_sub_Bitmap( who, w, 0, w, h );
b2 = new Bitmap( *who, w, 0, w, h );
quad2 = new EQuad( b2, min_size, mask_pixel, w, 0, this );
delete b2;
// destroy_Bitmap( b2 );
// b3 = create_sub_Bitmap( who, 0, h, w, h );
b3 = new Bitmap( *who, 0, h, w, h );
quad3 = new EQuad( b3, min_size, mask_pixel, 0, h, this );
delete b3;
// destroy_Bitmap( b3 );
// b4 = create_sub_Bitmap( who, w, h, w, h );
b4 = new Bitmap( *who, w, h, w, h );
quad4 = new EQuad( b4, min_size, mask_pixel, w, h, this );
delete b4;
// destroy_Bitmap( b4 );
if ( quad1->empty() ){
delete quad1;
quad1 = NULL;
}
if ( quad2->empty() ){
delete quad2;
quad2 = NULL;
}
if ( quad3->empty() ){
delete quad3;
quad3 = NULL;
}
if ( quad4->empty() ){
delete quad4;
quad4 = NULL;
}
if ( quad1 && quad2 && quad3 && quad4 )
if ( quad1->full && quad2->full && quad3->full && quad4->full ){
delete quad1;
delete quad2;
delete quad3;
delete quad4;
quad1 = NULL;
quad2 = NULL;
quad3 = NULL;
quad4 = NULL;
full = true;
}
checkQuad( quad1 );
checkQuad( quad2 );
checkQuad( quad3 );
checkQuad( quad4 );
} else {
// printf("Size X:%d Y:%d\n", width, height );
int total = 0;
- for ( int x = 0; x < who->getWidth(); x++ )
+ int denom = who->getWidth() * who->getHeight();
+ for ( int x = 0; x < who->getWidth(); x++ ){
for ( int y = 0; y < who->getHeight(); y++ ){
// int pixel = _getpixel16( who, x, y );
int pixel = who->getPixel( x, y );
- if ( pixel != mask_pixel )
+ if ( pixel != mask_pixel ){
++total;
+ }
+ if ( total * 100 / denom > 50 ){
+ /* we're done already,
+ * stop counting pixels
+ */
+ goto short_circuit;
+ }
}
+ }
- if ( total*100/(who->getWidth()*who->getHeight()) > 50 ){
+ /* johnny 5 alive! */
+ short_circuit:
+
+ if ( total * 100 / denom > 50 ){
full = true;
}
-
}
for ( int i = 0; i < 4; i++ )
quads[i] = NULL;
int i = 0;
if ( quad1 ) quads[i++] = quad1;
if ( quad2 ) quads[i++] = quad2;
if ( quad3 ) quads[i++] = quad3;
if ( quad4 ) quads[i++] = quad4;
num_quads = numQuads();
-
}
bool EQuad::addQuad( EQuad * who ){
int n = numQuads();
if ( n < 4 ){
quads[n] = who;
full = false;
return true;
} else return false;
}
void EQuad::checkQuad( EQuad *& q ){
if ( q != NULL ){
while ( q->numQuads() == 1 ){
EQuad * const newquad = q->getQuad();
q->detach( newquad );
int x = q->getMinX();
int y = q->getMinY();
delete q;
q = newquad;
newquad->setMinX( newquad->getMinX() + x );
newquad->setMinY( newquad->getMinY() + y );
q->parent = this;
}
}
}
int EQuad::totalQuads(){
if ( full ) return 1;
int total = 0;
for ( int i = 0; i < numQuads(); i++ )
if ( quads[i] )
total += quads[i]->totalQuads();
/*
if ( quad1 ) total += quad1->totalQuads();
if ( quad2 ) total += quad2->totalQuads();
if ( quad3 ) total += quad3->totalQuads();
if ( quad4 ) total += quad4->totalQuads();
*/
return total;
}
void EQuad::setMinX( int x ){
min_x = x;
}
void EQuad::setMinY( int y ){
min_y = y;
}
/*
int EQuad::numQuads() const{
int total = 0;
/ *
if ( quad1 ) ++total;
if ( quad2 ) ++total;
if ( quad3 ) ++total;
if ( quad4 ) ++total;
* /
for ( total = 0; total < 4 && quads[total] != NULL; total++ );
return total;
}
*/
EQuad * const EQuad::getQuad() const{
return quads[0];
/*
if ( quad1 ) return quad1;
if ( quad2 ) return quad2;
if ( quad3 ) return quad3;
if ( quad4 ) return quad4;
return NULL;
*/
}
EQuad * const EQuad::getQuad( int x ) const{
return quads[x];
}
void EQuad::detach( EQuad * const who ){
// printf("Detach %p\n", who );
/*
if ( who == quad1 ) quad1 = NULL;
if ( who == quad2 ) quad2 = NULL;
if ( who == quad3 ) quad3 = NULL;
if ( who == quad4 ) quad4 = NULL;
*/
for ( int i = 0; i < numQuads(); i++ )
if ( who == quads[i] )
quads[i] = NULL;
EQuad * tmp[ 4 ];
int begin = 0;
for ( int i = 0; i < 4; i++ )
if ( quads[i] ){
tmp[begin++] = quads[i];
quads[i] = NULL;
}
for ( int q = 0; q < begin; q++ )
quads[q] = tmp[q];
}
int EQuad::calcSize(){
int total = sizeof(*this);
for ( int q = 0; q < numQuads(); q++ ){
total += quads[q]->calcSize();
}
return total;
}
bool EQuad::empty(){
if ( numQuads() == 0 ) return !full;
// if ( !quad1 && !quad2 && !quad3 && !quad4 ) return !full;
return false;
}
void EQuad::draw( const Bitmap & work, int x, int y, int color, bool flipped ){
int mx = x + getMinX();
int my = y + getMinY();
// std::cout<<"getMinX: "<<getMinX()<<std::endl;
if ( flipped ){
if ( parent ){
mx = x + parent->getWidth() - (getMinX()+getWidth());
// mx = getMinX() - parent->getWidth()/2 + x;
// my = y + parent->getHeight()/2 - getMinY();
// my = y + getMinY();
// mx2 = mx - getWidth();
// mx2 = mx + getWidth();
/*
int i;
i = mx;
mx = mx2;
mx2 = i;
*/
}
}
int mx2 = mx + getWidth();
int my2 = my + getHeight();
for ( int i = 0; i < numQuads(); i++ )
quads[i]->draw( work, mx, my, color, flipped );
if ( full )
work.rectangle( mx, my, mx2, my2, color );
/*
if ( full )
work->rectangle( x+getMinX(), y+getMinY(), x+getMinX()+getWidth(), y+getMinY()+getHeight(), Bitmap::makeColor(255,64,32) );
else
work->rectangle( x+getMinX(), y+getMinY(), x+getMinX()+getWidth(), y+getMinY()+getHeight(), color );
*/
}
void EQuad::draw( const Bitmap & work, int x, int y, int color, EQuad * who ){
bool cy = false;
for ( int i = 0; i < numQuads(); i++ )
if ( who == quads[i] )
cy = true;
int mx = x + getMinX();
int my = y + getMinY();
if ( cy )
who->draw( work, mx, my, color );
else {
for ( int i = 0; i < numQuads(); i++ )
quads[i]->draw( work, mx, my, color, who );
}
}
-bool boxCollide( int zx1, int zy1, int zx2, int zy2, int zx3, int zy3, int zx4, int zy4 ){
+static bool boxCollide( int zx1, int zy1, int zx2, int zy2, int zx3, int zy3, int zx4, int zy4 ){
if ( zx1 < zx3 && zx1 < zx4 &&
zx2 < zx3 && zx2 < zx4 ) return false;
if ( zx1 > zx3 && zx1 > zx4 &&
zx2 > zx3 && zx2 > zx4 ) return false;
if ( zy1 < zy3 && zy1 < zy4 &&
zy2 < zy3 && zy2 < zy4 ) return false;
if ( zy1 > zy3 && zy1 > zy4 &&
zy2 > zy3 && zy2 > zy4 ) return false;
return true;
}
+/* sure this could be simplified, but thats no fun :p */
+int EQuad::getX1( bool xflipped ){
+ if ( parent ){
+ if ( xflipped ){
+ return parent->getWidth() - (getMinX() + getWidth());
+ } else {
+ return getMinX();
+ }
+ } else {
+ return getMinX();
+ }
+}
+
+int EQuad::getFullX1( bool xflipped ){
+ if ( parent ){
+ if ( xflipped ){
+ return parent->getFullX1( xflipped ) + parent->getWidth() - (getMinX() + getWidth());
+ } else {
+ return parent->getFullX1( xflipped ) + getMinX();
+ }
+ } else {
+ return getMinX();
+ }
+}
+
+int EQuad::getY1( bool yflipped ){
+ if ( parent ){
+ if ( yflipped ){
+ return parent->getHeight() - (getMinY() + getHeight());
+ } else {
+ return getMinY();
+ }
+ } else {
+ return getMinY();
+ }
+}
+
+int EQuad::getFullY1( bool yflipped ){
+ if ( parent ){
+ if ( yflipped ){
+ return parent->getFullY1( yflipped ) - (parent->getHeight() + getMinY() + getHeight());
+ } else {
+ return parent->getFullY1( yflipped ) + getMinY();
+ }
+ } else {
+ return getMinY();
+ }
+}
+
+void EQuad::gather( int mx, int my, int x1, int y1, int x2, int y2, vector< EQuad * > & collides, bool xflipped, bool yflipped ){
+ /*
+ int rx = mx + getX1();
+ int ry = my + getY1();
+ */
+ int rx = mx;
+ int ry = my;
+
+ int rx2 = rx + getWidth();
+ int ry2 = ry + getHeight();
+
+ if ( ! boxCollide( rx, ry, rx2, ry2, x1, y1, x2, y2 ) ){
+ return;
+ }
+
+ for ( int i = 0; i < numQuads(); i++ ){
+ quads[i]->gather( rx + quads[i]->getX1( xflipped ), ry + quads[i]->getY1( yflipped ), x1, y1, x2, y2, collides, xflipped, yflipped );
+ }
+
+ if ( full ){
+ collides.push_back( this );
+ }
+}
+
bool EQuad::collide( int mx, int my, int x1, int y1, int x2, int y2, EQuad ** last, bool xflipped, bool yflipped ){
+ /*
int rx = mx + getMinX();
int ry = my + getMinY();
if ( parent ){
if ( xflipped ){
rx = mx + parent->getWidth() - (getMinX()+getWidth());
}
if ( yflipped ){
ry = my + parent->getHeight() - (getMinY()+getHeight());
}
}
+ */
+
+ int rx = mx + getX1( xflipped );
+ int ry = my + getY1( yflipped );
+
int rx2 = rx + getWidth();
int ry2 = ry + getHeight();
// rect( screen, rx, ry, rx2, ry2, makecol(255,255,0) );
bool cy = boxCollide( rx, ry, rx2, ry2, x1, y1, x2, y2 );
if ( !cy ) return false;
for ( int i = 0; i < numQuads(); i++ )
if ( quads[i]->collide( rx, ry, x1, y1, x2, y2, last ) )
return true;
if ( !full ) return false;
if ( cy ) *last = this;
return cy;
return false;
}
EQuad::~EQuad(){
/*
if ( quad1 ) delete quad1;
if ( quad2 ) delete quad2;
if ( quad3 ) delete quad3;
if ( quad4 ) delete quad4;
*/
for ( int q = 0; q < 4; q++ )
if ( quads[q] != NULL )
delete quads[q];
}
long long ECollide::totalTime = 0;
void ECollide::initECollide( const Bitmap * who, int mask_pixel ){
/*
TimeDifference dif;
dif.startTime();
*/
head_quad = new EQuad( who, MIN_SIZE, mask_pixel, 0, 0, NULL );
last_collide = NULL;
/*
dif.endTime();
totalTime += dif.getTime();
cout<<"Total time taken: "<< totalTime / 1000 <<" ms" <<endl;
*/
}
ECollide::ECollide( const Bitmap * who, int mask_pixel ){
initECollide( who, mask_pixel );
/*
head_quad = new EQuad( who, MIN_SIZE, mask_pixel, 0, 0, NULL );
last_collide = NULL;
*/
}
ECollide::ECollide( const Bitmap & who, int mask_pixel ){
initECollide( &who, mask_pixel );
/*
head_quad = new EQuad( &who, MIN_SIZE, mask_pixel, 0, 0, NULL );
last_collide = NULL;
*/
}
ECollide::ECollide( BITMAP * who, int mask_pixel ){
Bitmap tmp( who );
initECollide( &tmp, mask_pixel );
/*
head_quad = new EQuad( &tmp, MIN_SIZE, mask_pixel, 0, 0, NULL );
last_collide = NULL;
*/
}
ECollide::ECollide( int width, int height ){
last_collide = NULL;
head_quad = new EQuad( width, height, NULL );
}
ECollide::ECollide( const ECollide * e ){
initQuad( e->head_quad );
/*
last_collide = NULL;
head_quad = new EQuad( e->head_quad );
*/
}
ECollide::ECollide( const ECollide & e ){
initQuad( e.head_quad );
/*
last_collide = NULL;
head_quad = new EQuad( e.head_quad );
*/
}
ECollide::ECollide( EQuad * const head ){
initQuad( head );
/*
last_collide = NULL;
head_quad = new EQuad( head );
*/
}
void ECollide::initQuad( EQuad * const head ){
last_collide = NULL;
head_quad = new EQuad( head );
}
ECollide * ECollide::copy(){
return new ECollide( head_quad );
}
int ECollide::getMinHeight(){
return head_quad->getMinY();
}
int ECollide::getMaxHeight(){
return head_quad->getMinY() + head_quad->getHeight();
}
int ECollide::getMinWidth(){
return head_quad->getMinX();
}
int ECollide::getMaxWidth(){
return head_quad->getMinX() + head_quad->getWidth();
}
int ECollide::calcSize(){
int total = sizeof(*this);
if ( head_quad )
total += head_quad->calcSize();
return total;
}
bool ECollide::collide( int mx, int my, int x1, int y1, int x2, int y2, bool xflipped, bool yflipped ){
-
last_collide = NULL;
+ return head_quad->collide(mx,my,x1,y1,x2,y2, &last_collide, xflipped, yflipped );
+}
- return head_quad->collide(mx,my,x1,y1,x2,y2,&last_collide, xflipped, yflipped );
+bool ECollide::collide( int mx, int my, int x1, int y1, int x2, int y2, EQuad ** last, bool xflipped, bool yflipped ){
+ return head_quad->collide(mx,my,x1,y1,x2,y2, last, xflipped, yflipped );
+}
+void ECollide::gather( int mx, int my, int x1, int y1, int x2, int y2, vector< EQuad * > & e, bool xflipped, bool yflipped ){
+ return head_quad->gather( mx, my, x1, y1, x2, y2, e, xflipped, yflipped );
}
bool ECollide::Collision( int mx, int my, int ax, int ay, bool xflipped, bool ylfipped ){
return collide( mx, my, ax, ay, ax, ay );
}
bool ECollide::Collision( int mx, int my, int x1, int y1, int x2, int y2, bool xflipped, bool yflipped ){
return collide( mx, my, x1, y1, x2, y2 );
}
bool ECollide::Collision( ECollide * col, int mx, int my, int ax, int ay, bool my_xflipped, bool my_yflipped, bool him_xflipped, bool him_yflipped ){
if ( !col ) return false;
int x1 = mx > ax ? mx : ax;
int y1 = my > ay ? my : ay;
int x2 = (mx+getWidth()) < (ax+col->getWidth()) ? (mx+getWidth()) : (ax+col->getWidth());
int y2 = (my+getHeight()) < (ay+col->getHeight()) ? (my+getHeight()) : (ay+col->getHeight());
if ( x1 > x2 ) return false;
if ( y1 > y2 ) return false;
- return ( collide(mx,my,x1,y1,x2,y2,my_xflipped,my_yflipped) && col->collide(ax,ay,x1,y1,x2,y2,him_xflipped,him_yflipped) );
+ vector< EQuad * > collides1;
+ gather( mx, my, x1, y1, x2, y2, collides1, my_xflipped, my_yflipped );
+ vector< EQuad * > collides2;
+ col->gather( ax, ay, x1, y1, x2, y2, collides2, him_xflipped, him_yflipped );
+ for ( vector< EQuad * >::iterator it = collides1.begin(); it != collides1.end(); it++ ){
+ EQuad * e = *it;
+ int px1 = mx + e->getFullX1( my_xflipped );
+ int py1 = my + e->getFullY1( my_yflipped );
+ int px2 = px1 + e->getWidth();
+ int py2 = py1 + e->getHeight();
+
+ // Bitmap::Screen->rectangle( px1, py1, px2, py2, Bitmap::makeColor( 128, 128, 255 ) );
+
+ for ( vector< EQuad * >::iterator it2 = collides2.begin(); it2 != collides2.end(); it2++ ){
+ EQuad * e2 = *it2;
+ int zx1 = ax + e2->getFullX1( him_xflipped );
+ int zy1 = ay + e2->getFullY1( him_yflipped );
+
+ // printf( "Test %d,%d,%d,%d against %d,%d,%d,%d\n", px1, py1, px2, py2, zx1, zy1, zx1 + e2->getWidth(), zy1 + e2->getHeight() );
+
+ // Bitmap::Screen->rectangle( zx1, zy1, zx1 + e2->getWidth(), zy1 + e2->getHeight(), Bitmap::makeColor( Util::rnd( 255 ), 255, 255 ) );
+
+ // if ( e2->collide( zx1, zy1, px1, py1, px2, py2, &last_collide, him_xflipped, him_yflipped ) ){
+ if ( boxCollide( px1, py1, px2, py2, zx1, zy1, zx1 + e2->getWidth(), zy1 + e2->getHeight() ) ){
+ Bitmap::Screen->rectangle( zx1, zy1, zx1 + e2->getWidth(), zy1 + e2->getHeight(), Bitmap::makeColor( 255, 255, 0 ) );
+ // cout << "Collided!" << endl;
+ // rest( 1000 );
+ return true;
+ }
+ }
+ }
+
+ // rest( 100 );
+
+ return false;
+ /*
+ return ( collide(mx,my,x1,y1,x2,y2,my_xflipped,my_yflipped) && col->collide(ax,ay,x1,y1,x2,y2,him_xflipped,him_yflipped) );
+ */
}
/*
int ECollide::getWidth() const{
return head_quad->getWidth();
}
int ECollide::getHeight() const{
return head_quad->getHeight();
}
*/
-void ECollide::draw( const Bitmap & work, int x, int y, bool flipped ){
+void ECollide::draw( const Bitmap & work, int x, int y, int color, bool flipped ){
- head_quad->draw( work, x, y, Bitmap::makeColor(255,255,255), flipped );
+ head_quad->draw( work, x, y, color, flipped );
}
int ECollide::numQuads() const{
return head_quad->totalQuads();
}
void ECollide::draw( const Bitmap & work, int x, int y, int color, EQuad * who ){
if ( head_quad == who )
head_quad->draw( work, x, y, color );
else head_quad->draw( work, x, y, color, who );
}
EQuad * ECollide::getLast(){
return last_collide;
}
ECollide::~ECollide(){
delete head_quad;
}
diff --git a/util/ebox.h b/util/ebox.h
index 8ecfec1e..d2baf9f0 100644
--- a/util/ebox.h
+++ b/util/ebox.h
@@ -1,162 +1,178 @@
-/* ebox version 2:
+/* ebox version 3:
* by Jon Rafkind
*/
-#ifndef _ebox_2_h
-#define _ebox_2_h
+#ifndef _ebox_3_h
+#define _ebox_3_h
#include "bitmap.h"
+#include <vector>
+
+using namespace std;
class EQuad{
public:
EQuad( EQuad * const head );
EQuad( int w, int h, EQuad * _parent );
EQuad( const Bitmap * who, int min_size, int mask_pixel, int min_x, int min_y, EQuad * _parent );
void draw( const Bitmap & work, int x, int y, int color, bool flipped = false );
void draw( const Bitmap & work, int x, int y, int color, EQuad * who );
inline int getWidth() const{
return width;
}
inline int getHeight() const{
return height;
}
inline void setFull( bool x ){
full = x;
}
EQuad * const getQuad( int x ) const;
bool addQuad( EQuad * who );
inline int getPosX() const {
if ( parent )
return min_x + parent->getPosX();
return min_x;
}
inline int getPosY() const {
if ( parent )
return min_y + parent->getPosY();
return min_y;
}
+ void gather( int mx, int my, int x1, int y1, int x2, int y2, vector< EQuad * > & collides, bool xflipped, bool yflipped );
+
// bool collide( int mx, int my, int x1, int y1, int x2, int y2, EQuad ** last );
bool collide( int mx, int my, int x1, int y1, int x2, int y2, EQuad ** last, bool xflipped = false, bool yflipped = false );
// int collide( int mx, int my, int x1, int y1, int x2, int y2, EQuad ** last, int depth );
int calcSize();
void setMinX( int x );
void setMinY( int y );
+
+ int getFullX1( bool xflipped = false );
+ int getFullY1( bool yflipped = false );
inline int getMinX() const{
return min_x;
}
inline int getMinY() const{
return min_y;
}
int totalQuads();
~EQuad();
protected:
+ int getX1( bool xflipped = false );
+ int getY1( bool yflipped = false );
+
EQuad * const getQuad() const;
void detach( EQuad * const who );
void checkQuad( EQuad *& q );
inline int numQuads() const{
int total = 0;
if ( quads[0] != NULL ) total++;
if ( quads[1] != NULL ) total++;
if ( quads[2] != NULL ) total++;
if ( quads[3] != NULL ) total++;
// for ( int total = 0; total < 4 && quads[total] != NULL; total++ );
return total;
}
bool empty();
// bool boxCollide( int zx1, int zy1, int zx2, int zy2, int zx3, int zy3, int zx4, int zy4 );
protected:
int width, height;
// EQuad * quad1, * quad2, * quad3, * quad4;
bool full;
int min_x, min_y;
EQuad * quads[ 4 ];
EQuad * parent;
int num_quads;
};
class ECollide{
public:
ECollide( EQuad * const head );
ECollide( BITMAP * who, int mask_pixel = Bitmap::MaskColor );
ECollide( const Bitmap * who, int mask_pixel = Bitmap::MaskColor );
ECollide( const Bitmap & who, int mask_pixel = Bitmap::MaskColor );
ECollide( const ECollide & e );
ECollide( const ECollide * e );
ECollide( int width, int height );
- void draw( const Bitmap & work, int x, int y, bool flipped = false );
+ void draw( const Bitmap & work, int x, int y, int color, bool flipped = false );
void draw( const Bitmap & work, int x, int y, int color, EQuad * who );
bool Collision( ECollide * col, int mx, int my, int ax, int ay, bool my_xflipped = false, bool my_yflipped = false, bool him_xflipped = false, bool him_flipped = false );
bool Collision( int mx, int my, int ax, int ay, bool xflipped = false, bool yflipped = false );
bool Collision( int mx, int my, int x1, int y1, int x2, int y2, bool xflipped = false, bool yflipped = false );
int getMinHeight();
int getMaxHeight();
int getMinWidth();
int getMaxWidth();
+
ECollide * copy();
EQuad * getLast();
EQuad * getHead() const{
return head_quad;
}
+ void gather( int mx, int my, int x1, int y1, int x2, int y2, vector< EQuad * > & e, bool xflipped, bool yflipped );
+
int calcSize();
inline int getWidth() const{
return head_quad->getWidth();
}
inline int getHeight() const{
return head_quad->getHeight();
}
int numQuads() const;
~ECollide();
private:
void initECollide( const Bitmap * who, int mask_pixel );
void initQuad( EQuad * const head );
+
static long long totalTime;
protected:
bool collide( int mx, int my, int x1, int y1, int x2, int y2, bool xflipped = false, bool yflipped = false );
+ bool collide( int mx, int my, int x1, int y1, int x2, int y2, EQuad ** last, bool xflipped, bool yflipped );
// bool collide( int mx, int my, int x1, int y1 );
EQuad * head_quad;
EQuad * last_collide;
};
#endif
diff --git a/util/keyboard.cpp b/util/keyboard.cpp
index f3cb9c58..ddb2bfa4 100644
--- a/util/keyboard.cpp
+++ b/util/keyboard.cpp
@@ -1,72 +1,75 @@
#include "keyboard.h"
#include "allegro.h"
#include <iostream>
#include <vector>
#include <map>
using namespace std;
#ifndef debug
#define debug cout<<"File: "<<__FILE__<<" Line: "<<__LINE__<<endl;
#endif
const int Keyboard::Key_A = KEY_A;
const int Keyboard::Key_S = KEY_S;
const int Keyboard::Key_D = KEY_D;
const int Keyboard::Key_LEFT = KEY_LEFT;
const int Keyboard::Key_RIGHT = KEY_RIGHT;
const int Keyboard::Key_UP = KEY_UP;
const int Keyboard::Key_DOWN = KEY_DOWN;
const int Keyboard::Key_ENTER = KEY_ENTER;
const int Keyboard::Key_SPACE = KEY_SPACE;
const int Keyboard::Key_F12 = KEY_F12;
const int Keyboard::Key_ESC = KEY_ESC;
+
+const int Keyboard::Key_MINUS_PAD = KEY_MINUS_PAD;
+const int Keyboard::Key_PLUS_PAD = KEY_PLUS_PAD;
Keyboard::Keyboard(){
}
/* KEY_MAX is defined in allegro at
* allegro/include/allegro/keyboard.h
*/
void Keyboard::poll(){
for ( int q = 0; q < KEY_MAX; q++ ){
// my_keys[ q ] = key[ q ];
if ( key[ q ] ){
if ( my_keys[ q ] <= 0 ){
my_keys[ q ] -= 1;
} else {
my_keys[ q ] += 1;
}
// printf( "Key %d = %d\n", q, my_keys[ q ] );
} else {
my_keys[ q ] = 0;
}
}
}
void Keyboard::readKeys( vector< int > & all_keys ){
for ( map<int,int>::const_iterator it = my_keys.begin(); it != my_keys.end(); it++ ){
const int & key = it->first;
const int & delay = it->second;
if ( delay < 0 || delay > key_delay[ key ] ){
all_keys.push_back( key );
}
}
}
void Keyboard::setDelay( const int key, const int delay ){
key_delay[ key ] = delay;
}
const bool Keyboard::keypressed(){
for ( map<int,int>::const_iterator it = my_keys.begin(); it != my_keys.end(); it++ ){
const int & n = (*it).second;
if ( n < 0 || n > key_delay[ it->first ] ){
return true;
}
}
return false;
}
diff --git a/util/keyboard.h b/util/keyboard.h
index 8bbdd09e..6dab45c8 100644
--- a/util/keyboard.h
+++ b/util/keyboard.h
@@ -1,72 +1,75 @@
#ifndef _keyboard_h
#define _keyboard_h
#include <map>
#include <vector>
using namespace std;
/* handles allegro key[] array better than keypressed()
* and readkey()
*/
class Keyboard{
public:
Keyboard();
/* poll:
* Put the keys in Allegro's key[] array into our map of int -> bool
*/
void poll();
/* []:
* Extract a boolean value given a key number
*/
inline const bool operator[] ( const int i ){
if ( my_keys[ i ] < 0 ){
my_keys[ i ] = 1;
return true;
}
bool b = my_keys[ i ] > key_delay[ i ];
if ( b ){
my_keys[ i ] = 1;
}
return b;
}
/* keypressed:
* Returns true if a key is pressed
*/
const bool keypressed();
/* readKeys:
* Store all pressed keys in a user supplied vector
*/
void readKeys( vector< int > & all_keys );
void setDelay( const int key, const int delay );
static const int Key_A;
static const int Key_S;
static const int Key_D;
static const int Key_LEFT;
static const int Key_RIGHT;
static const int Key_UP;
static const int Key_DOWN;
static const int Key_SPACE;
static const int Key_ENTER;
static const int Key_F12;
static const int Key_ESC;
+
+ static const int Key_MINUS_PAD;
+ static const int Key_PLUS_PAD;
protected:
map<int,int> my_keys;
map<int,int> key_delay;
};
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 2:15 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69417
Default Alt Text
(42 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline