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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
#include "draw.h"
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <string>
using namespace std;
SDL_Surface* Draw::SDL_ScaleSurface(SDL_Surface* Surface, unsigned int Width, unsigned int Height)
{
if(!Surface || !Width || !Height) return 0;
SDL_Surface *_ret = SDL_CreateRGBSurface(Surface->flags, Width, Height, Surface->format->BitsPerPixel,
Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);
double _stretch_factor_x = (static_cast<double>(Width) / static_cast<double>(Surface->w)),
_stretch_factor_y = (static_cast<double>(Height) / static_cast<double>(Surface->h));
for(int y = 0; y < Surface->h; y++) //Run across all Y pixels.
for(int x = 0; x < Surface->w; x++) //Run across all X pixels.
for(int o_y = 0; o_y < _stretch_factor_y; ++o_y) //Draw _stretch_factor_y pixels for each Y pixel.
for(int o_x = 0; o_x < _stretch_factor_x; ++o_x) //Draw _stretch_factor_x pixels for each X pixel.
DrawPixel(_ret, static_cast<int>(_stretch_factor_x * x) + o_x,
static_cast<int>(_stretch_factor_y * y) + o_y, ReadPixel(Surface, x, y));
return _ret;
}
unsigned int Draw::ReadPixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void Draw::DrawPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
//apply_surface came from http://lazyfoo.net/SDL_tutorials/lesson07/index.php
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
/*****All code above this line waS NOT written by me***********************************************************************/
Draw::Draw(void)
{
}
Draw::~Draw(void)
{
}
void Draw::Init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("SDL failed to initialize: %s\n", SDL_GetError());
exit(1);
}
if (TTF_Init() == -1)
{
printf("TTF_Init failed: %s\n", TTF_GetError());
exit(2);
}
screen = SDL_SetVideoMode(800, 600, 16, SDL_HWSURFACE || SDL_DOUBLEBUF);
if (screen == NULL)
{
printf("SetVideo failed: %s\n", SDL_GetError());
exit(1);
}
font = TTF_OpenFont("/usr/share/fonts/corefonts/arial.ttf", 18);
if (font == NULL)
{
printf("font is NULL\n");
exit(1);
}
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
char buff[100];
for (int i = 0; i < 12; i++)
{
sprintf( buff, "%d", i);
enemyimage[i] = TTF_RenderText_Shaded(font, buff, foregroundColor, backgroundColor);
}
SDL_WM_SetCaption("OurRPG", "OurRPG");
red = 0;
green = 0;
blue = 0;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, red, green, blue));
}
int Draw::getState() { return state; }
void Draw::setState(int i) { state = i; }
void Draw::setAllyParty(AllyParty *p)
{
allyparty = p;
Character* temp;
for (int i = 0; i < 4; i++)
{
temp = allyparty->getCharacter(i);
ally[i] = static_cast <Character*> (temp);
}
drawNames();
}
void Draw::setEnemyParty(EnemyParty *p)
{
enemyparty = p;
Character* temp;
for (int i = 0; i < enemyparty->getCount(); i++)
{
temp = enemyparty->getCharacter(i);
enemy[i] = static_cast <Character*> (temp);
}
}
void Draw::draw(int state)
{
SDL_Rect allysource[4], allydest[4], enemysource[12], enemydest[12];
for (int i = 0; i < 12; i++)
{
enemyimage[i] = IMG_Load(enemy[i]->getIMFileName().c_str());
// enemyimage[i] = SDL_ScaleSurface(enemyimage[i], 60, 30);
enemysource[i].h = 50;
enemysource[i].w = 50;
enemysource[i].x = 0;
enemysource[i].y = 0;
enemydest[i].x = enemy[i]->getX();
enemydest[i].y = enemy[i]->getY();
enemydest[i].h = 50;
enemydest[i].w = 50;
}
for (int i = 0; i < 4; i++)
{
allyimage[i] = IMG_Load(ally[i]->getIMFileName().c_str());
allysource[i].h = 50;
allysource[i].w = 50;
allysource[i].x = 0;
allysource[i].y = 0;
allydest[i].x = ally[i]->getX();
allydest[i].y = ally[i]->getY();
allydest[i].w = 50;
allydest[i].h = 50;
SDL_BlitSurface(allyimage[i], &allysource[i], screen, &allydest[i]);
}
for (int i = 0; i < 12; i++)
{
SDL_BlitSurface(enemyimage[i], &enemysource[i], screen, &enemydest[i]);
}
drawLines();
drawNames();
SDL_Flip(screen);
}
void Draw::drawLines(void) //Draw the lines on the screen
{
//Main frame start
lineRGBA(screen,
0, 0,
500, 0,
255, 255, 255, 255);
lineRGBA(screen,
2, 0,
2, 400,
255, 255, 255, 255);
lineRGBA(screen,
0, 400,
500, 400,
255, 255, 255, 255);
lineRGBA(screen,
500, 0,
500, 400,
255, 255, 255, 255);
lineRGBA(screen,
500, 0,
600, 0,
255, 255, 255, 255);
//Main frame end
for (int i = 1; i<5; i++)
{
lineRGBA(screen,
500, (i*100),
600, (i*100),
255, 255, 255, 255);
}
lineRGBA(screen,
600, 0,
600, 400,
255, 255, 255, 255);
}
void Draw::drawNames(void) //Draw ally names and status bars
{
SDL_Color textColor = {255, 255, 255};
for (int i = 0; i<4; i++)
{
//Cycle through each ally and grab their name
const char* c = ally[i]->getName().c_str(); //Convert our std:string to a const char*
allynamearea[i] = TTF_RenderText_Solid(font, c, textColor);
apply_surface( 505, i * 100, allynamearea[i], screen );
}
}
|