Skip to content

Commit

Permalink
* Fix paint for tiles without ground
Browse files Browse the repository at this point in the history
* Optimized Item Blit
* Fix protocol bug when adding items to tiles
  • Loading branch information
mips committed Nov 10, 2007
1 parent 3420403 commit 5fb16f2
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 79 deletions.
2 changes: 1 addition & 1 deletion creatureui.h
Expand Up @@ -24,7 +24,7 @@
#include "thingui.h"
class CreatureUI : virtual public ThingUI {
public:
void Blit(int x,int y) const {}
virtual void Blit(int x,int y) const {}
};

#endif
7 changes: 6 additions & 1 deletion gamecontent/item.cpp
Expand Up @@ -20,13 +20,18 @@

#include "item.h"

Item::Item(uint16_t id, uint8_t count) : ItemUI(id, count)
Item::Item(uint16_t id, uint8_t count) : ItemUI(id)
{
m_id = id;
m_count = count;
m_it = Objects::getInstance()->getItemType(m_id);
}

void Item::Blit(int x, int y) const
{
BlitItem(x, y, m_count, m_it);
}

Item* Item::CreateItem(const uint16_t type, const uint8_t count /*= 0*/)
{
return new Item(type, count);
Expand Down
2 changes: 2 additions & 0 deletions gamecontent/item.h
Expand Up @@ -50,6 +50,8 @@ class Item : public Thing, public ItemUI
virtual Item* getItem() {return this;}
virtual const Item* getItem() const {return this;}

virtual void Blit(int x, int y) const;

protected:

Item(uint16_t id, uint8_t count);
Expand Down
13 changes: 10 additions & 3 deletions gamecontent/map.cpp
Expand Up @@ -194,7 +194,7 @@ bool Tile::removeThing(Thing *thing)
return false;
}

bool Tile::addThing(Thing *thing)
bool Tile::addThing(Thing *thing, bool pushThing/* = false*/)
{
if(thing == NULL){
return false;
Expand Down Expand Up @@ -224,8 +224,15 @@ bool Tile::addThing(Thing *thing)

for(it = m_objects.begin(); it != m_objects.end(); ++it){
int itThingOrder = (*it)->getOrder();
if(itThingOrder > thingOrder){
break;
if(pushThing){
if(itThingOrder >= thingOrder){
break;
}
}
else{
if(itThingOrder > thingOrder){
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion gamecontent/map.h
Expand Up @@ -101,7 +101,7 @@ class Tile{
int32_t getThingCount() const;
bool insertThing(Thing* thing, int32_t stackpos);
bool removeThing(Thing* thing);
bool addThing(Thing* thing);
bool addThing(Thing* thing, bool pushThing = false);
Thing* getThingByStackPos(int32_t pos);
const Thing* getThingByStackPos(int32_t pos) const;
const Item* getGround() const;
Expand Down
5 changes: 2 additions & 3 deletions gm_debug.cpp
Expand Up @@ -57,10 +57,9 @@ void GM_Debug::UpdateOnClick(glictPos* relmousepos, glictContainer* callerclass)
{
GM_Debug *gd = (GM_Debug*)g_game;
gd->spr = g_engine->createSprite("Tibia.spr", atoi(gd->txtSprite.GetCaption().c_str()));
gd->thing = new ItemUI(atoi(gd->txtItem.GetCaption().c_str()), 1);
//gd->thing = new ItemUI(atoi(gd->txtItem.GetCaption().c_str()), 1);
}


GM_Debug::GM_Debug()
{

Expand Down Expand Up @@ -108,7 +107,7 @@ GM_Debug::GM_Debug()
if(g_engine){
background = g_engine->createSprite("Tibia.pic", 0);
spr = g_engine->createSprite("Tibia.spr", 200);
thing = new ItemUI(6401, 1);
//thing = new ItemUI(6401, 1);
}
else{ // i think that if g_engine does not exist, we might as well crash. what do you think, guys? ivucica
NativeGUIError("Somehow, engine managed to not initialize.", "YATC Fatal Error");
Expand Down
34 changes: 20 additions & 14 deletions gm_gameworld.cpp
Expand Up @@ -45,30 +45,36 @@ void GM_Gameworld::updateScene()
}

// TODO (ivucica#2#) test on edge of map
printf("Painting...\n");
//printf("Painting...\n");
Position pos = GlobalVariables::getPlayerPosition();

for ( int i = 0; i < 18; i++) {
for ( int j = 0; j < 14; j++) {
for(uint32_t i = 0; i < 18; ++i){
for(uint32_t j = 0; j < 14; ++j){

Tile *t = Map::getInstance().getTile(pos.x+i - 9, pos.y+j - 7, 7);
if (!t) {
printf("No tile?\n");
const Tile* tile = Map::getInstance().getTile(pos.x + i - 9, pos.y + j - 7, 7);
if(!tile){
//printf("No tile?\n");
continue;
}
// FIXME (ivucica#1#) error: passing 'const Item' as 'this' argument of 'virtual const void ItemUI::Blit(int, int)' discards qualifiers
// I cast it into Item* temporarily

t->getGround()->Blit(i*32,j*32);
const Item* ground = tile->getGround();
if(ground){
ground->Blit(i*32,j*32);
}

for (int k=0; k<t->getThingCount(); k++) {
Thing *th = t->getThingByStackPos(k);
if (th)
th->Blit(i*32,j*32);
else
printf("Thing invalid\n");
// FIXME (mips#1#) error: find the right draw order
for(int32_t k = tile->getThingCount() - 1; k >= 1 ; --k){
const Thing* thing = tile->getThingByStackPos(k);
if(thing){
thing->Blit(i*32, j*32);
}
else{
printf("Thing invalid %d\n", k);
}
}
printf("Painting %d %d\n", i, j);
//printf("Painting %d %d\n", i, j);
}
}

Expand Down
109 changes: 61 additions & 48 deletions itemui.cpp
Expand Up @@ -20,73 +20,86 @@

#include "itemui.h"
#include "engine.h"
#include "fassert.h"

ItemUI::ItemUI(uint16_t id, uint8_t count) : ThingUI() {
m_id = id;

if (id < 100) {
ItemUI::ItemUI(uint16_t id) : ThingUI()
{
if(id < 100){
printf("Error [ItemUI::ItemUI] Invalid item %d\n", id);
m_obj = NULL;
return;
}

m_obj = Objects::getInstance()->getItemType(id);
const ObjectType* obj = Objects::getInstance()->getItemType(id);
printf("== Item %d ==\n" , id);
for (int i = 0; i < m_obj->numsprites; i++) {
printf("Loading sprite %d...\n", m_obj->imageData[i]);
m_gfx.insert(m_gfx.end(), g_engine->createSprite("Tibia.spr", m_obj->imageData[i]));
for(uint32_t i = 0; i < obj->numsprites; i++){
printf("Loading sprite %d...\n", obj->imageData[i]);
m_gfx.insert(m_gfx.end(), g_engine->createSprite("Tibia.spr", obj->imageData[i]));
}

printf("== End of item %d == \n", id);
}
ItemUI::~ItemUI() {
if (!m_obj)
return;
for (std::vector<Sprite*>::iterator it = m_gfx.begin(); it != m_gfx.end();) {

ItemUI::~ItemUI()
{
std::vector<Sprite*>::iterator it;
for(it = m_gfx.begin(); it != m_gfx.end(); ++it){
delete *it;
m_gfx.erase(it);
}
m_gfx.clear();
}

void ItemUI::Blit(int x, int y) const {
int g_frame = 0;
void ItemUI::BlitItem(int x, int y, uint8_t count, const ObjectType* obj) const
{
uint32_t g_frame = 0;
struct { int x, y; } m_pos = {0, 0};

if (!m_obj)
if(!obj)
return;
//m_gfx[0]->Blit(x, y);

if (m_obj->stackable) {
if (m_count < 1)
m_gfx[0]->Blit(x,y);
else if (m_count <= 4)
m_gfx[m_count-1]->Blit(x,y);
else if (m_count <= 9)
m_gfx[4]->Blit(x,y);
else if (m_count <= 24)
m_gfx[5]->Blit(x,y);
else if (m_count <= 49)
m_gfx[6]->Blit(x,y);
else if (m_count <= 100)
m_gfx[7]->Blit(x,y);
else
m_gfx[0]->Blit(x,y);
} else {
unsigned int activeframe;
for (int i = 0; i < m_obj->height; i++) {
for (int j = 0; j < m_obj->width; j++) {
for (int k = 0; k < m_obj->blendframes; k++) { // note: if it's anything except item, there won't be blendframes...
if(obj->stackable){

if(obj->numsprites < 8){
m_gfx[0]->Blit(x, y);
printf("Item stackable - m_obj->numsprites < 8");
return;
}

if(count < 1){
m_gfx[0]->Blit(x, y);
}
else if(count < 5){
m_gfx[count - 1]->Blit(x,y);
}
else if(count < 10){
m_gfx[4]->Blit(x, y);
}
else if(count < 25){
m_gfx[5]->Blit(x, y);
}
else if(count < 50){
m_gfx[6]->Blit(x, y);
}
else if(count <= 100){
m_gfx[7]->Blit(x, y);
}
else{
m_gfx[0]->Blit(x, y);
}
}
else{
uint32_t activeframe = g_frame *
(obj->ydiv + m_pos.y % obj->ydiv) *
(obj->xdiv + m_pos.x % obj->xdiv);

for(uint32_t k = 0; k < obj->blendframes; ++k){ // note: if it's anything except item, there won't be blendframes...
for(uint32_t i = 0; i < obj->height; ++i){
for(uint32_t j = 0; j < obj->width; ++j){

activeframe = (((((( // same amount of ('s as of *'s
g_frame)
* m_obj->ydiv + m_pos.y % m_obj->ydiv)
* m_obj->xdiv + m_pos.x % m_obj->xdiv)
* m_obj->blendframes + k) // k == subblendframes (stacked sprite)
* m_obj->height + i) // i == subheight (y coordinate)
* m_obj->width + j) // j == subwidth (x coordinate)
ASSERT(activeframe < obj->numsprites);

;
m_gfx[activeframe]->Blit(x-j*32,y-i*32);
printf("Blitting to %d %d\n", x-j*32,y-i*32);
m_gfx[activeframe]->Blit(x - j*32, y - i*32);
activeframe++;
// printf("Blitting to %d %d\n", x-j*32,y-i*32);
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions itemui.h
Expand Up @@ -24,17 +24,18 @@

#include "thingui.h"
#include "objects.h"
class ItemUI : virtual public ThingUI {

class ItemUI : virtual public ThingUI
{
public:
ItemUI(uint16_t id, uint8_t count);
ItemUI(uint16_t id);
virtual ~ItemUI();

void Blit(int x, int y) const;
virtual void Blit(int x, int y) const = 0;

protected:

private:
ObjectType *m_obj;
int m_id;
int m_count;
void BlitItem(int x, int y, uint8_t count, const ObjectType* obj) const;
};

#endif
2 changes: 1 addition & 1 deletion net/protocolgame80.cpp
Expand Up @@ -190,7 +190,7 @@ bool ProtocolGame80::onRecv(NetworkMessage& msg)
RAISE_PROTOCOL_ERROR("Tile Add - !tile");
}

if(!tile->addThing(thing)){
if(!tile->addThing(thing, true)){
RAISE_PROTOCOL_ERROR("Tile Add - addThing");
}

Expand Down
19 changes: 19 additions & 0 deletions thingui.cpp
@@ -0,0 +1,19 @@
//////////////////////////////////////////////////////////////////////
// Yet Another Tibia Client
//////////////////////////////////////////////////////////////////////
// Base item/creature/... class
//////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////
1 change: 1 addition & 0 deletions thingui.h
Expand Up @@ -30,6 +30,7 @@ class ThingUI {

virtual void Blit(int x, int y) const = 0;
protected:

std::vector<Sprite*> m_gfx;
};

Expand Down

0 comments on commit 5fb16f2

Please sign in to comment.