1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <iostream>
#include <windows.h>
#include <sstream>
#include "Item.h"
#define IT_BASE 0
#define IT_HELMET 1
#define IT_CHESTPLATE 2
#define IT_PANTS 3
#define IT_BOOTS 4
#define IT_ACCESSORRY 5
#define IT_WEAPON 6
Item::Item()
{
}
Item::~Item()
{
W=0;
H=0;
Name = "";
}
Item::Item(int x,int y,int w,int h,const char* name,const char* img_addr,int flags)
{
X=x;
Y=y;
W=w;
H=h;
imgAddr=img_addr;
Name=name;
TYPE = flags;
OX=0;
OY=0;
SDL_Surface* loadedImg = IMG_Load(imgAddr);
surface = SDL_DisplayFormatAlpha(loadedImg);
SDL_FreeSurface(loadedImg);
}
ITEM Item::getInstance()
{
ITEM item;
switch(TYPE)
{
case IT_BASE:
item = ITEM(Name,imgAddr,TYPE);
break;
case IT_WEAPON:
item = ITEM_WEAPON(Name,imgAddr,damage,attackSpeed);
break;
}
return item;
}
void Item::drawName(SDL_Surface* window,int x,int y)
{
if(strlen(Name) > 0)
{
SDL_Rect dst;
dst.x=x;
dst.y=y;
dst.h=16;
dst.w=32;
SDL_Color textColor = {255,255,255};
std::stringstream text_to_draw;
text_to_draw<<Name;
switch(TYPE)
{
case IT_WEAPON:
text_to_draw<<" "<<minDamage<<"-"<<damage;
break;
}
std::string Text = text_to_draw.str();
const char* drawFinale = Text.c_str();
TTF_Font* font = TTF_OpenFont("font.ttf",18);
SDL_Surface* text = TTF_RenderText_Solid(font,drawFinale,textColor);
SDL_BlitSurface(text,NULL,window,&dst);
}
}
void Item::setWeapon(int Damage,int AttackSpeed)
{
damage = Damage;
attackSpeed = AttackSpeed;
TYPE = IT_WEAPON;
minDamage = (int)damage/2;
}
bool Item::contains(int x,int y)
{
bool retVal = false;
if(x>=X)
{
if(x<X+W)
{
if(y>=Y)
{
if(y<Y+H)
{
retVal = true;
}
}
}
}
return retVal;
}
void Item::draw(SDL_Surface* window)
{
SDL_Rect src;
src.x=0;
src.y=0;
src.w=W;
src.h=H;
SDL_Rect dst;
dst.x=(int)X;
dst.y=(int)Y;
dst.w=W;
dst.h=H;
SDL_BlitSurface(surface ,&src ,window ,&dst);
}
void Item::flag(int type)
{
TYPE = type;
}
void Item::move_to(int x,int y)
{
X=x;
Y=y;
}
int Item::getItemType()
{
return TYPE;
}
const char* Item::getImgAddr()
{
return imgAddr;
}
const char* Item::getName()
{
return Name;
}
int Item::getDamage()
{
return damage;
}
int Item::getAttackSpeed()
{
return attackSpeed;
}
int Item::getX()
{
return X;
}
int Item::getY()
{
return Y;
}
int Item::getW()
{
return W;
}
int Item::getH()
{
return H;
}
|