diff options
Diffstat (limited to 'lib/sprite.cc')
-rw-r--r-- | lib/sprite.cc | 380 |
1 files changed, 190 insertions, 190 deletions
diff --git a/lib/sprite.cc b/lib/sprite.cc index ef5d7a8..bd0df94 100644 --- a/lib/sprite.cc +++ b/lib/sprite.cc @@ -1,190 +1,190 @@ -/*
- * mogltk
- * Copyright (C) 1999-2004 Nicolas "Pixel" Noble
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-/* $Id: sprite.cc,v 1.14 2004-11-27 21:44:53 pixel Exp $ */
-
-#include <SDL.h>
-#include "engine.h"
-#include "base.h"
-#include "sprite.h"
-
-#define TEXSIZE 1024
-
-mogltk::Sprite * mogltk::Sprite::Cursor;
-
-mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0;
-
-mogltk::Sprite::Sprite(Handle * h, int asx, int asy) throw (GeneralException) : sx(asx), sy(asy) {
- alloc();
- for (int y = 0; y < sy; y++) {
- h->read(tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4);
- }
-}
-
-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()->GetPixels() + TEXSIZE * y + posx, sx * 4);
- p += sx * 4;
- }
-}
-
-mogltk::Sprite::~Sprite() {
- if (prev)
- prev->next = next;
- else
- tlist->sprheader = next;
-
- if (next)
- next->prev = prev;
-
- if (!tlist->sprheader) {
- delete tlist;
- }
-}
-
-void mogltk::Sprite::alloc() {
- /* FIXMEEEEEEEEEEEEE */
- bool found = false;
- TexList * p;
-
- for (p = tlist; p && !found; p = p->GetNext()) {
- 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;
- }
- }
- }
- }
-
- if (!found) {
- printm(M_INFO, "Allocating a new texture for sprites\n");
- posx = posy = 0;
- p = new TexList(TEXSIZE);
- }
-
- tlist = p;
-}
-
-mogltk::Sprite::TexList::TexList(int size) {
- tex = new texture(size, size, true);
-
- if (header)
- header->prev = this;
- prev = 0;
- next = header;
- header = this;
-}
-
-mogltk::Sprite::TexList::~TexList() {
- delete tex;
-
- if (prev)
- prev->next = next;
- else
- header = next;
-
- if (next)
- next->prev = prev;
-}
-
-const mogltk::texture * mogltk::Sprite::TexList::GetTex() const {
- return tex;
-}
-
-mogltk::texture * mogltk::Sprite::TexList::GetTex() {
- return tex;
-}
-
-const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() {
- return header;
-}
-
-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();
-}
-
-SDL_Surface * mogltk::Sprite::TexList::GetSurface() {
- return tex->GetSurface();
-}
-
-
-bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const {
- 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;
- else
- if (next)
- return next->canfit(x1, y1, x2, y2);
- else
- return false;
-}
-
-void mogltk::Sprite::draw(int dx, int dy, ColorP c, float sx, float sy) {
- bool locked = false;
-
- if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) {
- locked = true;
- SDL_LockSurface(mogltk::engine::base_o->getsurface());
- }
-
- SDL_Rect src, dst;
-
- src.x = posx; src.y = posy; src.w = sx; src.h = sy;
- dst.x = dx; dst.y = dy; dst.w = sx; dst.h = sy;
-
- SDL_BlitSurface(tlist->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst);
-
- if (locked)
- SDL_UnlockSurface(mogltk::engine::base_o->getsurface());
-}
-
-void mogltk::Sprite::Bind() {
- tlist->Bind();
-}
-
-int mogltk::Sprite::GetPX() {
- return posx;
-}
-
-int mogltk::Sprite::GetPY() {
- return posy;
-}
-
-int mogltk::Sprite::GetSX() {
- return sx;
-}
-
-int mogltk::Sprite::GetSY() {
- return sy;
-}
+/* + * mogltk + * Copyright (C) 1999-2004 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: sprite.cc,v 1.15 2004-11-27 21:48:03 pixel Exp $ */ + +#include <SDL.h> +#include "engine.h" +#include "base.h" +#include "sprite.h" + +#define TEXSIZE 1024 + +mogltk::Sprite * mogltk::Sprite::Cursor; + +mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0; + +mogltk::Sprite::Sprite(Handle * h, int asx, int asy) throw (GeneralException) : sx(asx), sy(asy) { + alloc(); + for (int y = 0; y < sy; y++) { + h->read(tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); + } +} + +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()->GetPixels() + TEXSIZE * y + posx, sx * 4); + p += sx * 4; + } +} + +mogltk::Sprite::~Sprite() { + if (prev) + prev->next = next; + else + tlist->sprheader = next; + + if (next) + next->prev = prev; + + if (!tlist->sprheader) { + delete tlist; + } +} + +void mogltk::Sprite::alloc() { + /* FIXMEEEEEEEEEEEEE */ + bool found = false; + TexList * p; + + for (p = tlist; p && !found; p = p->GetNext()) { + 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; + } + } + } + } + + if (!found) { + printm(M_INFO, "Allocating a new texture for sprites\n"); + posx = posy = 0; + p = new TexList(TEXSIZE); + } + + tlist = p; +} + +mogltk::Sprite::TexList::TexList(int size) { + tex = new texture(size, size, true); + + if (header) + header->prev = this; + prev = 0; + next = header; + header = this; +} + +mogltk::Sprite::TexList::~TexList() { + delete tex; + + if (prev) + prev->next = next; + else + header = next; + + if (next) + next->prev = prev; +} + +const mogltk::texture * mogltk::Sprite::TexList::GetTex() const { + return tex; +} + +mogltk::texture * mogltk::Sprite::TexList::GetTex() { + return tex; +} + +const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() { + return header; +} + +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(); +} + +SDL_Surface * mogltk::Sprite::TexList::GetSurface() { + return tex->GetSurface(); +} + + +bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const { + 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; + else + if (next) + return next->canfit(x1, y1, x2, y2); + else + return false; +} + +void mogltk::Sprite::draw(int dx, int dy, ColorP c, float sx, float sy) { + bool locked = false; + + if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) { + locked = true; + SDL_LockSurface(mogltk::engine::base_o->getsurface()); + } + + SDL_Rect src, dst; + + src.x = posx; src.y = posy; src.w = sx; src.h = sy; + dst.x = dx; dst.y = dy; dst.w = sx; dst.h = sy; + + SDL_BlitSurface(tlist->GetSurface(), &src, mogltk::engine::base_o->getsurface(), &dst); + + if (locked) + SDL_UnlockSurface(mogltk::engine::base_o->getsurface()); +} + +void mogltk::Sprite::Bind() { + tlist->Bind(); +} + +int mogltk::Sprite::GetPX() { + return posx; +} + +int mogltk::Sprite::GetPY() { + return posy; +} + +int mogltk::Sprite::GetSX() { + return sx; +} + +int mogltk::Sprite::GetSY() { + return sy; +} |