Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
42 KB
Referenced Files
None
Subscribers
None
diff --git a/util/bitmap.h b/util/bitmap.h
index ee54bd9c..d58ee95c 100644
--- a/util/bitmap.h
+++ b/util/bitmap.h
@@ -1,292 +1,296 @@
/* if you include this file then it must be the first line in file (after any
* #ifdef's of course. windows will complain loudly if <windows.h> ends up being
* included before allegro.h and this file fixes things so the order is correct.
*/
#ifndef _paintown_bitmap_h_
#define _paintown_bitmap_h_
#include <string>
#include <vector>
#include <iostream>
#ifdef USE_ALLEGRO
#include "allegro/bitmap.h"
#endif
#ifdef USE_SDL
#include "sdl/bitmap.h"
#endif
class Bitmap{
private:
static Bitmap * Screen;
public:
static int SCALE_X;
static int SCALE_Y;
/* default constructor makes 10x10 bitmap */
Bitmap();
Bitmap( int x, int y );
Bitmap( const char * load_file );
Bitmap( const std::string & load_file );
Bitmap( const char * load_file, int sx, int sy );
Bitmap( const char * load_file, int sx, int sy, double accuracy );
+
+ /* 4/24/2010: remove this at some point */
+#ifdef USE_ALLEGRO
explicit Bitmap( BITMAP * who, bool deep_copy = false );
+#endif
Bitmap( const Bitmap & copy, bool deep_copy = false );
Bitmap( const Bitmap & copy, int sx, int sy );
Bitmap( const Bitmap & copy, int sx, int sy, double accuracy );
Bitmap( const Bitmap & copy, int x, int y, int width, int height );
virtual ~Bitmap();
virtual void save( const std::string & str );
Bitmap & operator=( const Bitmap & );
virtual void load( const std::string & str );
int getWidth() const;
int getHeight() const;
/*
inline const int getWidth() const{
return getBitmap()->w;
}
inline const int getHeight() const{
return getBitmap()->h;
}
*/
/*
inline const int getWidth() const{
return my_bitmap->w;
}
inline const int getHeight() const{
return my_bitmap->h;
}
*/
static Bitmap memoryPCX(unsigned char * const data, const int length, const bool mask = true);
void detach();
static void transBlender( int r, int g, int b, int a );
static void multiplyBlender( int r, int g, int b, int a );
static void dissolveBlender( int r, int g, int b, int a );
static void addBlender( int r, int g, int b, int a );
static void burnBlender( int r, int g, int b, int a );
static void colorBlender( int r, int g, int b, int a );
static void differenceBlender( int r, int g, int b, int a );
static void dodgeBlender( int r, int g, int b, int a );
static void hueBlender( int r, int g, int b, int a );
static void luminanceBlender( int r, int g, int b, int a );
static void invertBlender( int r, int g, int b, int a );
static void screenBlender( int r, int g, int b, int a );
static int setGraphicsMode( int mode, int width, int height );
static void drawingMode( int type );
void acquire();
void release();
void resize( const int width, const int height );
void debugSelf() const;
/* convert to a grey scale version */
virtual Bitmap greyScale();
virtual void triangle( int x1, int y1, int x2, int y2, int x3, int y3, int color ) const;
virtual void ellipse( int x, int y, int rx, int ry, int color ) const;
virtual void ellipseFill( int x, int y, int rx, int ry, int color ) const;
virtual void light(int x, int y, int width, int height, int start_y, int focus_alpha, int edge_alpha, int focus_color, int edge_color) const;
virtual void applyTrans(const int color);
virtual void border( int min, int max, int color ) const;
virtual void rectangle( int x1, int y1, int x2, int y2, int color ) const;
virtual void rectangleFill( int x1, int y1, int x2, int y2, int color ) const;
virtual void circleFill( int x, int y, int radius, int color ) const;
virtual void circle( int x, int y, int radius, int color ) const;
virtual void line( const int x1, const int y1, const int x2, const int y2, const int color ) const;
virtual void floodfill( const int x, const int y, const int color ) const;
virtual void horizontalLine( const int x1, const int y, const int x2, const int color ) const;
virtual void hLine( const int x1, const int y, const int x2, const int color ) const;
virtual void vLine( const int y1, const int x, const int y2, const int color ) const;
virtual void polygon( const int * verts, const int nverts, const int color ) const;
virtual void arc(const int x, const int y, const double ang1, const double ang2, const int radius, const int color ) const;
virtual void draw(const int x, const int y, const Bitmap & where) const;
virtual void draw(const int x, const int y, const int startWidth, const int startHeight, const int width, const int height, const Bitmap & where) const;
virtual void drawCharacter( const int x, const int y, const int color, const int background, const Bitmap & where ) const;
/* to draw lit use LitBitmap */
// virtual void drawLit( const int x, const int y, const int level, const Bitmap & where ) const;
virtual void drawHFlip(const int x, const int y, const Bitmap & where) const;
virtual void drawHFlip(const int x, const int y, const int startWidth, const int startHeight, const int width, const int height, const Bitmap & where) const;
virtual void drawVFlip( const int x, const int y, const Bitmap & where ) const;
virtual void drawHVFlip( const int x, const int y, const Bitmap & where ) const;
virtual void drawTrans( const int x, const int y, const Bitmap & where ) const;
virtual void drawTransHFlip( const int x, const int y, const Bitmap & where ) const;
virtual void drawTransVFlip( const int x, const int y, const Bitmap & where ) const;
virtual void drawTransHVFlip( const int x, const int y, const Bitmap & where ) const;
virtual void drawMask( const int x, const int y, const Bitmap & where );
virtual void drawStretched( const int x, const int y, const int new_width, const int new_height, const Bitmap & who );
virtual void drawRotate( const int x, const int y, const int angle, const Bitmap & where );
virtual void drawPivot( const int centerX, const int centerY, const int x, const int y, const int angle, const Bitmap & where );
virtual void drawPivot( const int centerX, const int centerY, const int x, const int y, const int angle, const double scale, const Bitmap & where );
virtual void Stretch( const Bitmap & where ) const;
virtual void Stretch( const Bitmap & where, const int sourceX, const int sourceY, const int sourceWidth, const int sourceHeight, const int destX, const int destY, const int destWidth, const int destHeight ) const;
virtual void StretchBy2( const Bitmap & where );
virtual void StretchBy4( const Bitmap & where );
virtual void Blit( const std::string & xpath ) const;
virtual void Blit( const Bitmap & where ) const;
virtual void Blit( const int x, const int y, const Bitmap & where ) const;
virtual void Blit( const int mx, const int my, const int wx, const int wy, const Bitmap & where ) const;
virtual void Blit( const int mx, const int my, const int width, const int height, const int wx, const int wy, const Bitmap & where ) const;
virtual void BlitMasked( const int mx, const int my, const int width, const int height, const int wx, const int wy, const Bitmap & where ) const;
virtual void BlitToScreen() const;
virtual void BlitAreaToScreen(const int upper_left_x, const int upper_left_y) const;
virtual void BlitToScreen(const int upper_left_x, const int upper_left_y) const;
virtual void BlitFromScreen(const int x, const int y) const;
/* try to call Global::getScreenWidth/Height() instead of these directly */
static int getScreenWidth();
static int getScreenHeight();
virtual void fill( int color ) const;
inline void clear() const{
this->fill( 0 );
}
inline void clearToMask() const{
this->fill(MaskColor);
}
bool getError();
inline const BitmapData & getData() const {
return data;
}
inline BitmapData & getData(){
return data;
}
virtual void readLine( std::vector< int > & vec, int y );
int getPixel( const int x, const int y ) const;
/* uses _putpixel16 underneath which ignores translucent behavior */
void putPixel( int x, int y, int col ) const;
/* respects the current trans mode */
void putPixelNormal(int x, int y, int col) const;
/*
inline int getPixel( int x, int y ) const{
if ( x >= 0 && x < my_bitmap->w && y >= 0 && y <= my_bitmap->h )
return _getpixel16( my_bitmap, x, y );
return -1;
}
inline void putPixel( int x, int y, int col ) const{
if ( x >= 0 && x < my_bitmap->w && y >= 0 && y <= my_bitmap->h )
_putpixel16( my_bitmap, x, y, col );
}
*/
void setClipRect( int x1, int y1, int x2, int y2 ) const;
inline const std::string & getPath() const{
return path;
}
/* produce a temporary bitmap that is not guaranteed to be preserved
* after your function returns. do *not* hold references to this bitmap
* and if you make a temporary bitmap, do *not* call functions that
* might also make temporary bitmaps.
*/
static Bitmap temporaryBitmap(int w, int h);
/* call this method to delete all temporary bitmaps.
* don't call this unless you know what you are doing
*/
static void cleanupTemporaryBitmaps();
static double getScale();
static int setGfxModeText();
static int setGfxModeFullscreen( int x, int y );
static int setGfxModeWindowed( int x, int y );
static int makeColor( int r, int g, int b );
static int darken( int color, double factor );
static void hsvToRGB( float h, float s, float v, int * r, int * g, int * b );
/*
* Convert color values between the HSV and RGB color spaces. The RGB values
* range from 0 to 255, hue is from 0 to 360, and saturation and value are
* from 0 to 1.
*/
static void rgbToHSV(int r, int g, int b, float * h, float * s, float * v);
/* convert cymk to rgb. values should be in the range 0-255 */
static void cymkToRGB(int c, int y, int m, int k, int * r, int * g, int * b);
static int getRed( int x );
static int getBlue( int x );
static int getGreen( int x );
/* Add two RGB16 colors together
* r = c1.r + c2.r
* g = c1.g + c2.g
* b = c1.b + c2.b
*/
static int addColor( int color1, int color2 );
/*
inline static int makeColor( int r, int g, int b ){
return makecol16( r, g, b );
}
*/
// static const int MaskColor = MASK_COLOR_16;
static const int MaskColor;
static const int MODE_TRANS;
static const int MODE_SOLID;
static const int SPRITE_NO_FLIP;
static const int SPRITE_V_FLIP;
static const int SPRITE_H_FLIP;
static const int SPRITE_NORMAL;
static const int SPRITE_LIT;
static const int SPRITE_TRANS;
protected:
void releaseInternalBitmap();
/*
inline void setBitmap( BITMAP * bitmap ){
if ( bitmap == NULL ){
std::cout << "*FATAL* Setting null bitmap" << std::endl;
}
_my_bitmap = bitmap;
}
*/
void internalLoadFile( const char * load_file );
/* implementation specific data */
BitmapData data;
int * own;
// bool own;
bool error;
std::string path;
static Bitmap * temporary_bitmap;
};
#endif
diff --git a/util/ebox.cpp b/util/ebox.cpp
index a8f4f962..6b0fbb0e 100644
--- a/util/ebox.cpp
+++ b/util/ebox.cpp
@@ -1,715 +1,717 @@
/* ebox version 3:
* by Jon Rafkind
*/
#include "ebox.h"
#include <stdio.h>
#include "funcs.h"
#include "bitmap.h"
#include <iostream>
using namespace std;
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;
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 ){
++total;
}
if ( total * 100 / denom > 50 ){
/* we're done already,
* stop counting pixels
*/
goto short_circuit;
}
}
}
/* 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 * 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 * 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 );
}
}
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;
*/
}
+#if 0
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;
*/
}
+#endif
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 );
}
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;
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, int color, bool 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 bc4644df..385c6f6b 100644
--- a/util/ebox.h
+++ b/util/ebox.h
@@ -1,176 +1,176 @@
/* ebox version 3:
* by Jon Rafkind
*/
#ifndef _ebox_3_h
#define _ebox_3_h
#include "bitmap.h"
#include <vector>
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 * 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, std::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 * 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( 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, 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, std::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/font.cpp b/util/font.cpp
index 2b63b1a9..160b0181 100644
--- a/util/font.cpp
+++ b/util/font.cpp
@@ -1,255 +1,257 @@
#include <allegro.h>
#include "bitmap.h"
#include "font.h"
#include "init.h"
#include "factory/font_factory.h"
#include <string.h>
using namespace std;
Font::Font(){
}
/* copy/pasted from network/message.cpp */
static vector< string > wrapStrings( const string & left, const string & right, const Font & font, int max, vector< string > accum ){
if ( left == "" ){
return accum;
}
int length = font.textLength(left.c_str());
if (length >= max){
return wrapStrings( left.substr( 0, left.length() / 2 ), left.substr( left.length() / 2 ) + right, font, max, accum );
} else if ( length >= max - font.textLength( "E" ) || right == "" ){
accum.push_back( left );
return wrapStrings( right, "", font, max, accum );
} else {
return wrapStrings( left + right.substr( 0, 1 ), right.substr( 1 ), font, max, accum );
}
}
void Font::printfWrapLine(int x, int & y, int color, const Bitmap & work, int maxWidth, const char * line) const {
vector< string > all;
all = wrapStrings(string(line), "", *this, maxWidth, all );
for ( vector< string >::iterator str = all.begin(); str != all.end(); str++ ){
printf(x, y, color, work, *str, 0);
y += getHeight();
}
y += getHeight() / 2;
}
#if 0
void Font::printfWrapLine2(int x, int & y, int color, const Bitmap & work, int maxWidth, const char * line) const {
int height = getHeight();
while (*line != '\0'){
char tmp2[1024];
int left = strlen(line);
int min = 0;
int max = left;
int current = (min + max) / 2;
strncpy(tmp2, line, current);
tmp2[current] = '\0';
bool done = false;
while (!done){
int length = textLength(tmp2);
if (length >= maxWidth){
max = current;
current = (min + max) / 2;
strncpy(tmp2, line, current);
tmp2[current] = '\0';
} else if (length < maxWidth && current < max){
min = current;
current = (min + max) / 2;
if (current == max - 1){
current = max;
done = true;
}
strncpy(tmp2, line, current);
tmp2[current] = '\0';
} else {
done = true;
}
}
printf(x, y, color, work, string(tmp2), 0);
y += height;
line += current;
}
y += height / 2;
}
#endif
void Font::printfWrap(int x, int y, int color, const Bitmap & work, int maxWidth, const std::string & str, int marker, ... ) const {
char buf[4096];
va_list ap;
va_start(ap, marker);
uvszprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
char * start = buf;
char * end = strchr(start, '\n');
while (end != NULL){
char tmp[1024];
unsigned int ender = end - start;
if (ender >= sizeof(tmp) - 1){
ender = sizeof(tmp) - 1;
}
strncpy(tmp, start, ender);
tmp[ender] = '\0';
printfWrapLine(x, y, color, work, maxWidth, tmp);
start = end + 1;
end = strchr(start, '\n');
}
printfWrapLine(x, y, color, work, maxWidth, start);
}
Font::~Font(){
}
+#ifdef USE_ALLEGRO
AllegroFont::AllegroFont( const FONT * const font ):
font(font){
}
AllegroFont::AllegroFont( const AllegroFont & copy ):
font( copy.getInternalFont() ){
}
AllegroFont::~AllegroFont(){
}
int AllegroFont::textLength( const char * text ) const{
return text_length( getInternalFont(), text );
}
int AllegroFont::getHeight( const string & str ) const {
return getHeight();
}
int AllegroFont::getHeight() const {
return text_height( getInternalFont() );
}
void AllegroFont::setSize( const int x, const int y ){
}
int AllegroFont::getSizeX() const {
return 0;
}
int AllegroFont::getSizeY() const {
return 0;
}
void AllegroFont::printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
uvszprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
textout_ex(work.getData().getBitmap(), getInternalFont(), buf, x, y, color, -1);
}
void AllegroFont::printf( int x, int y, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
uvszprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
textout_ex(work.getData().getBitmap(), getInternalFont(), buf, x, y, color, -1);
}
+#endif
const Font & Font::getDefaultFont(){
// return getFont( "tmp/comic.ttf" );
return *FontFactory::getFont(Filesystem::RelativePath("bios"), 16, 16);
}
/* name should be the path of a .ttf file in the fonts/ directory.
* something like 'arial.ttf'
*/
const Font & Font::getFont(const Filesystem::RelativePath & name, const int x, const int y){
Font & font = *FontFactory::getFont(name, x, y);
/* sanity check */
if (font.getHeight("A") == 0){
return getDefaultFont();
}
return font;
}
FreeTypeFont::FreeTypeFont(const Filesystem::AbsolutePath & str ):
sizeX( 16 ),
sizeY( 16 ),
own(true){
this->font = new ftalleg::freetype(str, getSizeX(), getSizeY() );
}
int FreeTypeFont::getHeight( const string & str ) const {
return this->font->getHeight( str );
}
int FreeTypeFont::getHeight() const {
return getHeight( "A" );
}
int FreeTypeFont::textLength( const char * text ) const {
return this->font->getLength(string(text));
}
void FreeTypeFont::printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
vsnprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
int old_x = 0;
int old_y = 0;
this->font->getSize(&old_x, &old_y);
this->font->setSize(xSize, ySize);
this->font->render( x, y, color, work, ftalleg::freetype::ftLeft, string( buf ), 0 );
this->font->setSize(old_x, old_y);
}
void FreeTypeFont::printf( int x, int y, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
vsnprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
this->font->render(x, y, color, work, ftalleg::freetype::ftLeft, string(buf), 0);
}
void FreeTypeFont::setSize( const int x, const int y ){
this->sizeX = x;
this->sizeY = y;
this->font->setSize( this->sizeX, this->sizeY );
}
int FreeTypeFont::getSizeX() const {
return this->sizeX;
}
int FreeTypeFont::getSizeY() const {
return this->sizeY;
}
FreeTypeFont::~FreeTypeFont(){
// cout << "Delete font " << this->font << endl;
if (own){
delete this->font;
}
}
diff --git a/util/font.h b/util/font.h
index 29e5cbf5..60df3737 100644
--- a/util/font.h
+++ b/util/font.h
@@ -1,89 +1,91 @@
#ifndef _paintown_font_h
#define _paintown_font_h
#include <string>
#include <vector>
#include "ftalleg.h"
class Bitmap;
struct FONT;
/* handle allegro fonts and true type fonts */
class Font{
public:
Font();
virtual ~Font();
virtual void setSize( const int x, const int y ) = 0;
virtual int getSizeX() const = 0;
virtual int getSizeY() const = 0;
virtual int textLength( const char * text ) const = 0;
virtual int getHeight( const std::string & str ) const = 0;
virtual int getHeight() const = 0;
virtual void printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const std::string & str, int marker, ... ) const = 0;
virtual void printf( int x, int y, int color, const Bitmap & work, const std::string & str, int marker, ... ) const = 0;
virtual void printfWrap( int x, int y, int color, const Bitmap & work, int maxWidth, const std::string & str, int marker, ... ) const;
static const Font & getDefaultFont();
static const Font & getFont( const Filesystem::RelativePath & name, const int x = 32, const int y = 32 );
/* store all the freetype fonts forever */
static std::vector< ftalleg::freetype * > cacheFreeType;
protected:
void printfWrapLine(int x, int & y, int color, const Bitmap & work, int maxWidth, const char * line) const;
};
+#ifdef USE_ALLEGRO
class AllegroFont: public Font {
public:
AllegroFont( const FONT * const font );
AllegroFont( const AllegroFont & copy );
virtual ~AllegroFont();
virtual int getHeight() const;
virtual int getHeight( const std::string & str ) const;
virtual int textLength( const char * text ) const;
virtual void printf( int x, int y, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void setSize( const int x, const int y );
virtual int getSizeX() const;
virtual int getSizeY() const;
private:
inline const FONT * getInternalFont() const {
return font;
}
const FONT * const font;
};
+#endif
class FreeTypeFont: public Font {
public:
FreeTypeFont(const Filesystem::AbsolutePath & filename);
FreeTypeFont(const FreeTypeFont & copy);
virtual ~FreeTypeFont();
virtual int getHeight() const;
virtual int getHeight( const std::string & str ) const;
virtual int textLength( const char * text ) const;
virtual void printf( int x, int y, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void setSize( const int x, const int y );
virtual int getSizeX() const;
virtual int getSizeY() const;
private:
ftalleg::freetype * font;
int sizeX;
int sizeY;
bool own;
};
#endif
diff --git a/util/sdl/bitmap.h b/util/sdl/bitmap.h
index e69de29b..a3592730 100644
--- a/util/sdl/bitmap.h
+++ b/util/sdl/bitmap.h
@@ -0,0 +1,2 @@
+struct BitmapData{
+};

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 1:16 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69201
Default Alt Text
(42 KB)

Event Timeline