From 02ee82a20cc5462ab43fdfa3b0237cd8618bdd97 Mon Sep 17 00:00:00 2001 From: pixel Date: Sat, 8 Mar 2003 02:49:42 +0000 Subject: Sprites working(?) --- include/sprite.h | 7 ++++--- lib/sprite.cc | 48 +++++++++++++++++++++++++++++++----------------- src/test.cc | 27 +++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/include/sprite.h b/include/sprite.h index 803bd4a..fbc50ff 100644 --- a/include/sprite.h +++ b/include/sprite.h @@ -8,7 +8,6 @@ #include "gltexture.h" namespace mogltk { - class Sprite : public Base { public: Sprite(Handle *, int, int); @@ -22,8 +21,10 @@ namespace mogltk { virtual ~TexList(); Sprite * sprheader; const texture * GetTex() const; - static const TexList * GetHead() const; + texture * GetTex(); + static const TexList * GetHead(); const TexList * GetNext() const; + TexList * GetNext(); void Bind() const; private: texture * tex; @@ -33,7 +34,7 @@ namespace mogltk { Sprite * next, * prev; TexList * tlist; int sx, sy, posx, posy; - void alloc() const; + void alloc(); bool canfit(int, int, int, int) const; bool intersect(int, int, int, int) const; }; diff --git a/lib/sprite.cc b/lib/sprite.cc index be2fd3b..26b57dd 100644 --- a/lib/sprite.cc +++ b/lib/sprite.cc @@ -1,4 +1,5 @@ #include +#include "glbase.h" #include "sprite.h" #define TEXSIZE 256 @@ -8,23 +9,26 @@ mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0; mogltk::Sprite::Sprite(Handle * h, int asx, int asy) : sx(asx), sy(asy) { alloc(); for (int y = 0; y < sy; y++) { - h->read(tlist->GetTex()->pixels + TEXSIZE * y + posx, sx); + h->read(tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); } + + SDL_SaveBMP(tlist->GetTex()->GetSurface(), "bleh.bmp"); } mogltk::Sprite::Sprite(Uint8 * p, int asx, int asy) : sx(asx), sy(asy) { alloc(); for (int y = 0; y < sy; y++) { - bcopy(p, tlist->GetTex()->pixels + TEXSIZE * y + posx, sx); - p += sx; + bcopy(p, tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); + p += sx * 4; } } mogltk::Sprite::~Sprite() { if (prev) - prev->next = next + prev->next = next; else tlist->sprheader = next; + if (next) next->prev = prev; @@ -39,9 +43,9 @@ void mogltk::Sprite::alloc() { TexList * p; for (p = tlist; p && !found; p = p->GetNext()) { - for (y = 0; (py < (TEXSIZE - sy)) && !found; py++) { - for (x = 0; (px < (TEXSIZE - sx)) && !found; px++) { - if (p->strheader->canfit(x, y, x + sx, y + sy)) { + for (posy = 0; (posy < (TEXSIZE - sy)) && !found; posy++) { + for (posx = 0; (posx < (TEXSIZE - sx)) && !found; posx++) { + if (p->sprheader->canfit(posx, posy, posx + sx, posy + sy)) { found = true; } } @@ -52,7 +56,7 @@ void mogltk::Sprite::alloc() { #ifdef DEBUG printm(M_INFO, "Allocating a new texture for sprites\n"); #endif - x = y = 0; + posx = posy = 0; p = new TexList(TEXSIZE); } @@ -71,10 +75,12 @@ mogltk::Sprite::TexList::TexList(int size) { mogltk::Sprite::TexList::~TexList() { delete tex; + if (prev) - prev->next = next + prev->next = next; else header = next; + if (next) next->prev = prev; } @@ -83,7 +89,11 @@ const mogltk::texture * mogltk::Sprite::TexList::GetTex() const { return tex; } -const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() const { +mogltk::texture * mogltk::Sprite::TexList::GetTex() { + return tex; +} + +const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() { return header; } @@ -91,22 +101,26 @@ const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() const { return next; } +mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() { + return next; +} + void mogltk::Sprite::TexList::Bind() const { tex->Bind(); } bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const { - int sx1 = x, sy1 = y, sx2 = x + sx, sy2 = y + sy; + int sx1 = posx, sy1 = posy, sx2 = posx + sx, sy2 = posy + sy; return !((sx1 >= x2) || (sx2 <= x1) || (sy1 >= y1) || (sy2 <= y2)); } bool mogltk::Sprite::canfit(int x1, int y1, int x2, int y2) const { if (!intersect(x1, y1, x2, y2)) - return true + return true; else if (next) - return next->canfit() + return next->canfit(x1, y1, x2, y2); else return false; } @@ -121,10 +135,10 @@ void mogltk::Sprite::draw(int dx, int dy, Color c) { tlist->Bind(); glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(x , y ); glVertex2i(dx , dy ); - glTexCoord2i(x + sx - 1, y ); glVertex2i(dx + sx - 1, dy ); - glTexCoord2i(x , y + sy - 1); glVertex2i(dx , dy + sy - 1); - glTexCoord2i(x + sx - 1, y + sy - 1); glVertex2i(dx + sx - 1, dy + sy - 1); + glTexCoord2i(posx , posy ); glVertex2i(dx , dy ); + glTexCoord2i(posx + sx - 1, posy ); glVertex2i(dx + sx - 1, dy ); + glTexCoord2i(posx , posy + sy - 1); glVertex2i(dx , dy + sy - 1); + glTexCoord2i(posx + sx - 1, posy + sy - 1); glVertex2i(dx + sx - 1, dy + sy - 1); glEnd(); if (!was2D) diff --git a/src/test.cc b/src/test.cc index 129127a..b09c00d 100644 --- a/src/test.cc +++ b/src/test.cc @@ -7,15 +7,20 @@ #include "gltexture.h" #include "glfont.h" #include "engine.h" +#include "sprite.h" CODE_BEGINS virtual int startup() throw (GeneralException) { verbosity = M_INFO; mogltk::glbase::setup(); - new Archive("datas.paq"); +// new Archive("datas.paq"); Input * fonte = new Input("font-2.bin"); mogltk::font font(fonte); delete fonte; + Input * cursor = new Input("cursor.rgba"); + printm(M_INFO, "Sprite file size: %i\n", cursor->GetSize()); + mogltk::Sprite * s = new mogltk::Sprite(cursor, 25, 25); + delete cursor; verbosity = M_INFO; @@ -27,7 +32,7 @@ virtual int startup() throw (GeneralException) { while (!mogltk::engine::quitrequested()) { mogltk::glbase::Enter2DMode(); - + mytex->Bind(); glBegin(GL_TRIANGLE_STRIP); glColor3d(0, 0, 0); @@ -75,6 +80,24 @@ virtual int startup() throw (GeneralException) { "It works!!\n" "I can't believe it!\n" ); + + s->draw( 50, 50); + s->draw(100, 50); + s->draw(150, 50); + s->draw(200, 50); + s->draw( 50, 100); + s->draw(100, 100); + s->draw(150, 100); + s->draw(200, 100); + s->draw( 50, 150); + s->draw(100, 150); + s->draw(150, 150); + s->draw(200, 150); + s->draw( 50, 200); + s->draw(100, 200); + s->draw(150, 200); + s->draw(200, 200); + mogltk::glbase::Leave2DMode(); mogltk::glbase::Flip(); } -- cgit v1.2.3