| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name graphic.cpp - The general graphic functions. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2006 by Lutz Sammer, Nehal Mistry, and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
|
| 41 |
#include <string> |
| 42 |
#include <map> |
| 43 |
#include <list> |
| 44 |
|
| 45 |
#include "video.h" |
| 46 |
#include "player.h" |
| 47 |
#include "intern_video.h" |
| 48 |
#include "iocompat.h" |
| 49 |
#include "iolib.h" |
| 50 |
|
| 51 |
/*---------------------------------------------------------------------------- |
| 52 |
-- Variables |
| 53 |
----------------------------------------------------------------------------*/ |
| 54 |
|
| 55 |
static enum { |
| 56 |
ModuleNotInitialized, |
| 57 |
ModuleOK, |
| 58 |
ModuleDeinitialized |
| 59 |
} GraphicsState; |
| 60 |
|
| 61 |
static int HashCount; |
| 62 |
|
| 63 |
static std::map<std::string, CGraphic *> GraphicHash; |
| 64 |
|
| 65 |
static std::list<CGraphic *> Graphics; |
| 66 |
|
| 67 |
struct CGraphicsModule { |
| 68 |
CGraphicsModule() { |
| 69 |
GraphicsState = ModuleOK; |
| 70 |
} |
| 71 |
~CGraphicsModule() { |
| 72 |
GraphicsState = ModuleDeinitialized; |
| 73 |
} |
| 74 |
static void AssertOK() { |
| 75 |
#ifdef DEBUG |
| 76 |
switch (GraphicsState) { |
| 77 |
case ModuleNotInitialized: |
| 78 |
fprintf(stderr, "The graphics module hasn't yet been initialized\n"); |
| 79 |
// Do not call ExitFatal, because that calls exit, |
| 80 |
// which might attempt to call destructors again. |
| 81 |
abort(); |
| 82 |
case ModuleOK: |
| 83 |
break; |
| 84 |
case ModuleDeinitialized: |
| 85 |
fprintf(stderr, "The graphics module has already been deinitialized\n"); |
| 86 |
abort(); |
| 87 |
default: |
| 88 |
abort(); |
| 89 |
} |
| 90 |
#endif // DEBUG |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
static CGraphicsModule GraphicsModule; |
| 95 |
|
| 96 |
/*---------------------------------------------------------------------------- |
| 97 |
-- Functions |
| 98 |
----------------------------------------------------------------------------*/ |
| 99 |
|
| 100 |
/** |
| 101 |
** Video draw the graphic clipped. |
| 102 |
** |
| 103 |
** @param x X screen position |
| 104 |
** @param y Y screen position |
| 105 |
*/ |
| 106 |
void CGraphic::DrawClip(int x, int y) const |
| 107 |
{ |
| 108 |
int oldx = x; |
| 109 |
int oldy = y; |
| 110 |
int w = Width; |
| 111 |
int h = Height; |
| 112 |
CLIP_RECTANGLE(x, y, w, h); |
| 113 |
DrawSub(x - oldx, y - oldy, w, h, x, y); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
** Video draw part of graphic. |
| 118 |
** |
| 119 |
** @param gx X offset into object |
| 120 |
** @param gy Y offset into object |
| 121 |
** @param w width to display |
| 122 |
** @param h height to display |
| 123 |
** @param x X screen position |
| 124 |
** @param y Y screen position |
| 125 |
*/ |
| 126 |
void CGraphic::DrawSub(int gx, int gy, int w, int h, int x, int y) const |
| 127 |
{ |
| 128 |
if (!UseOpenGL) { |
| 129 |
SDL_Rect srect = { |
| 130 |
static_cast<Sint16>(gx), |
| 131 |
static_cast<Sint16>(gy), |
| 132 |
static_cast<Uint16>(w), |
| 133 |
static_cast<Uint16>(h) |
| 134 |
}; |
| 135 |
SDL_Rect drect = { |
| 136 |
static_cast<Sint16>(x), |
| 137 |
static_cast<Sint16>(y), |
| 138 |
0, // SDL_BlitSurface ignores the width and height. |
| 139 |
0 |
| 140 |
}; |
| 141 |
|
| 142 |
SDL_BlitSurface(Surface, &srect, TheScreen, &drect); |
| 143 |
} else { |
| 144 |
DrawTexture(this, Textures, gx, gy, gx + w, gy + h, x, y, 0); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
** Video draw part of graphic clipped. |
| 150 |
** |
| 151 |
** @param gx X offset into object |
| 152 |
** @param gy Y offset into object |
| 153 |
** @param w width to display |
| 154 |
** @param h height to display |
| 155 |
** @param x X screen position |
| 156 |
** @param y Y screen position |
| 157 |
*/ |
| 158 |
void CGraphic::DrawSubClip(int gx, int gy, int w, int h, int x, int y) const |
| 159 |
{ |
| 160 |
int oldx = x; |
| 161 |
int oldy = y; |
| 162 |
CLIP_RECTANGLE(x, y, w, h); |
| 163 |
DrawSub(gx + x - oldx, gy + y - oldy, w, h, x, y); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
** Video draw part of graphic with alpha. |
| 168 |
** |
| 169 |
** @param gx X offset into object |
| 170 |
** @param gy Y offset into object |
| 171 |
** @param w width to display |
| 172 |
** @param h height to display |
| 173 |
** @param x X screen position |
| 174 |
** @param y Y screen position |
| 175 |
** @param alpha Alpha |
| 176 |
*/ |
| 177 |
void CGraphic::DrawSubTrans(int gx, int gy, int w, int h, int x, int y, |
| 178 |
unsigned char alpha) const |
| 179 |
{ |
| 180 |
if (!UseOpenGL) { |
| 181 |
int oldalpha = Surface->format->alpha; |
| 182 |
SDL_SetAlpha(Surface, SDL_SRCALPHA, alpha); |
| 183 |
DrawSub(gx, gy, w, h, x, y); |
| 184 |
SDL_SetAlpha(Surface, SDL_SRCALPHA, oldalpha); |
| 185 |
} else { |
| 186 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 187 |
glColor4ub(255, 255, 255, alpha); |
| 188 |
DrawSub(gx, gy, w, h, x, y); |
| 189 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
** Video draw part of graphic with alpha and clipped. |
| 195 |
** |
| 196 |
** @param gx X offset into object |
| 197 |
** @param gy Y offset into object |
| 198 |
** @param w width to display |
| 199 |
** @param h height to display |
| 200 |
** @param x X screen position |
| 201 |
** @param y Y screen position |
| 202 |
** @param alpha Alpha |
| 203 |
*/ |
| 204 |
void CGraphic::DrawSubClipTrans(int gx, int gy, int w, int h, int x, int y, |
| 205 |
unsigned char alpha) const |
| 206 |
{ |
| 207 |
int oldx = x; |
| 208 |
int oldy = y; |
| 209 |
CLIP_RECTANGLE(x, y, w, h); |
| 210 |
DrawSubTrans(gx + x - oldx, gy + y - oldy, w, h, x, y, alpha); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
** Draw graphic object unclipped. |
| 215 |
** |
| 216 |
** @param frame number of frame (object index) |
| 217 |
** @param x x coordinate on the screen |
| 218 |
** @param y y coordinate on the screen |
| 219 |
*/ |
| 220 |
void CGraphic::DrawFrame(unsigned frame, int x, int y) const |
| 221 |
{ |
| 222 |
if (!UseOpenGL) { |
| 223 |
DrawSub((frame % (Surface->w / Width)) * Width, |
| 224 |
(frame / (Surface->w / Width)) * Height, |
| 225 |
Width, Height, x, y); |
| 226 |
} else { |
| 227 |
int sx, sy, ex, ey, n; |
| 228 |
|
| 229 |
n = GraphicWidth / Width; |
| 230 |
sx = (frame % n) * Width; |
| 231 |
ex = sx + Width; |
| 232 |
sy = (frame / n) * Height; |
| 233 |
ey = sy + Height; |
| 234 |
DrawTexture(this, Textures, sx, sy, ex, ey, x, y, 0); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void CGraphic::DoDrawFrameClip(GLuint *textures, |
| 239 |
unsigned frame, int x, int y) const |
| 240 |
{ |
| 241 |
int ox; |
| 242 |
int oy; |
| 243 |
int skip; |
| 244 |
int w; |
| 245 |
int h; |
| 246 |
int sx, sy, n; |
| 247 |
|
| 248 |
n = GraphicWidth / Width; |
| 249 |
sx = (frame % n) * Width; |
| 250 |
sy = (frame / n) * Height; |
| 251 |
|
| 252 |
w = Width; |
| 253 |
h = Height; |
| 254 |
CLIP_RECTANGLE_OFS(x, y, w, h, ox, oy, skip); |
| 255 |
skip = skip; // avoid a compiler warning |
| 256 |
|
| 257 |
DrawTexture(this, textures, sx + ox, sy + oy, sx + ox + w, sy + oy + h, x, y, 0); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
** Draw graphic object clipped. |
| 262 |
** |
| 263 |
** @param frame number of frame (object index) |
| 264 |
** @param x x coordinate on the screen |
| 265 |
** @param y y coordinate on the screen |
| 266 |
*/ |
| 267 |
void CGraphic::DrawFrameClip(unsigned frame, int x, int y) const |
| 268 |
{ |
| 269 |
if (!UseOpenGL) { |
| 270 |
DrawSubClip((frame % (Surface->w / Width)) * Width, |
| 271 |
(frame / (Surface->w / Width)) * Height, |
| 272 |
Width, Height, x, y); |
| 273 |
} else { |
| 274 |
DoDrawFrameClip(Textures, frame, x, y); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void CGraphic::DrawFrameTrans(unsigned frame, int x, int y, int alpha) const |
| 279 |
{ |
| 280 |
if (!UseOpenGL) { |
| 281 |
DrawSubTrans((frame % (Surface->w / Width)) * Width, |
| 282 |
(frame / (Surface->w / Width)) * Height, |
| 283 |
Width, Height, x, y, alpha); |
| 284 |
} else { |
| 285 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 286 |
glColor4ub(255, 255, 255, alpha); |
| 287 |
DrawFrame(frame, x, y); |
| 288 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void CGraphic::DrawFrameClipTrans(unsigned frame, int x, int y, int alpha) const |
| 293 |
{ |
| 294 |
if (!UseOpenGL) { |
| 295 |
DrawSubClipTrans((frame % (Surface->w / Width)) * Width, |
| 296 |
(frame / (Surface->w / Width)) * Height, |
| 297 |
Width, Height, x, y, alpha); |
| 298 |
} else { |
| 299 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 300 |
glColor4ub(255, 255, 255, alpha); |
| 301 |
DrawFrameClip(frame, x, y); |
| 302 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
** Draw graphic object clipped and with player colors. |
| 308 |
** |
| 309 |
** @param player player number |
| 310 |
** @param frame number of frame (object index) |
| 311 |
** @param x x coordinate on the screen |
| 312 |
** @param y y coordinate on the screen |
| 313 |
*/ |
| 314 |
void CPlayerColorGraphic::DrawPlayerColorFrameClip(int player, unsigned frame, |
| 315 |
int x, int y) |
| 316 |
{ |
| 317 |
if (!UseOpenGL) { |
| 318 |
GraphicPlayerPixels(&Players[player], this); |
| 319 |
DrawFrameClip(frame, x, y); |
| 320 |
} else { |
| 321 |
if (!PlayerColorTextures[player]) { |
| 322 |
MakePlayerColorTexture(this, player); |
| 323 |
} |
| 324 |
DoDrawFrameClip(PlayerColorTextures[player], frame, x, y); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
/** |
| 331 |
** Draw graphic object unclipped and flipped in X direction. |
| 332 |
** |
| 333 |
** @param frame number of frame (object index) |
| 334 |
** @param x x coordinate on the screen |
| 335 |
** @param y y coordinate on the screen |
| 336 |
*/ |
| 337 |
void CGraphic::DrawFrameX(unsigned frame, int x, int y) const |
| 338 |
{ |
| 339 |
if (!UseOpenGL) { |
| 340 |
SDL_Rect srect; |
| 341 |
SDL_Rect drect; |
| 342 |
|
| 343 |
srect.x = (SurfaceFlip->w - (frame % (SurfaceFlip->w / |
| 344 |
Width)) * Width) - Width; |
| 345 |
srect.y = (frame / (SurfaceFlip->w / Width)) * Height; |
| 346 |
srect.w = Width; |
| 347 |
srect.h = Height; |
| 348 |
|
| 349 |
drect.x = x; |
| 350 |
drect.y = y; |
| 351 |
|
| 352 |
SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect); |
| 353 |
} else { |
| 354 |
int sx, sy, ex, ey, n; |
| 355 |
|
| 356 |
n = GraphicWidth / Width; |
| 357 |
sx = (frame % n) * Width; |
| 358 |
ex = sx + Width; |
| 359 |
sy = (frame / n) * Height; |
| 360 |
ey = sy + Height; |
| 361 |
DrawTexture(this, Textures, sx, sy, ex, ey, x, y, 1); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
void CGraphic::DoDrawFrameClipX(GLuint *textures, unsigned frame, |
| 366 |
int x, int y) const |
| 367 |
{ |
| 368 |
int ox; |
| 369 |
int oy; |
| 370 |
int skip; |
| 371 |
int w; |
| 372 |
int h; |
| 373 |
int sx, sy, n; |
| 374 |
|
| 375 |
n = GraphicWidth / Width; |
| 376 |
sx = (frame % n) * Width; |
| 377 |
sy = (frame / n) * Height; |
| 378 |
|
| 379 |
w = Width; |
| 380 |
h = Height; |
| 381 |
CLIP_RECTANGLE_OFS(x, y, w, h, ox, oy, skip); |
| 382 |
skip = skip; // avoid a compiler warning |
| 383 |
|
| 384 |
if (w < Width) { |
| 385 |
if (ox == 0) { |
| 386 |
ox += Width - w; |
| 387 |
} else { |
| 388 |
ox = 0; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
DrawTexture(this, textures, sx + ox, sy + oy, sx + ox + w, sy + oy + h, x, y, 1); |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
** Draw graphic object clipped and flipped in X direction. |
| 397 |
** |
| 398 |
** @param frame number of frame (object index) |
| 399 |
** @param x x coordinate on the screen |
| 400 |
** @param y y coordinate on the screen |
| 401 |
*/ |
| 402 |
void CGraphic::DrawFrameClipX(unsigned frame, int x, int y) const |
| 403 |
{ |
| 404 |
if (!UseOpenGL) { |
| 405 |
SDL_Rect srect; |
| 406 |
SDL_Rect drect; |
| 407 |
int oldx; |
| 408 |
int oldy; |
| 409 |
|
| 410 |
srect.x = (SurfaceFlip->w - (frame % (SurfaceFlip->w / |
| 411 |
Width)) * Width) - Width; |
| 412 |
srect.y = (frame / (SurfaceFlip->w / Width)) * Height; |
| 413 |
srect.w = Width; |
| 414 |
srect.h = Height; |
| 415 |
|
| 416 |
oldx = x; |
| 417 |
oldy = y; |
| 418 |
CLIP_RECTANGLE(x, y, srect.w, srect.h); |
| 419 |
srect.x += x - oldx; |
| 420 |
srect.y += y - oldy; |
| 421 |
|
| 422 |
drect.x = x; |
| 423 |
drect.y = y; |
| 424 |
|
| 425 |
SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect); |
| 426 |
} else { |
| 427 |
DoDrawFrameClipX(Textures, frame, x, y); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
void CGraphic::DrawFrameTransX(unsigned frame, int x, int y, int alpha) const |
| 432 |
{ |
| 433 |
if (!UseOpenGL) { |
| 434 |
SDL_Rect srect; |
| 435 |
SDL_Rect drect; |
| 436 |
int oldalpha; |
| 437 |
|
| 438 |
srect.x = (SurfaceFlip->w - (frame % (SurfaceFlip->w / |
| 439 |
Width)) * Width) - Width; |
| 440 |
srect.y = (frame / (SurfaceFlip->w / Width)) * Height; |
| 441 |
srect.w = Width; |
| 442 |
srect.h = Height; |
| 443 |
|
| 444 |
drect.x = x; |
| 445 |
drect.y = y; |
| 446 |
|
| 447 |
oldalpha = Surface->format->alpha; |
| 448 |
SDL_SetAlpha(Surface, SDL_SRCALPHA, alpha); |
| 449 |
SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect); |
| 450 |
SDL_SetAlpha(Surface, SDL_SRCALPHA, oldalpha); |
| 451 |
} else { |
| 452 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 453 |
glColor4ub(255, 255, 255, alpha); |
| 454 |
DrawFrameX(frame, x, y); |
| 455 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
void CGraphic::DrawFrameClipTransX(unsigned frame, int x, int y, int alpha) const |
| 460 |
{ |
| 461 |
if (!UseOpenGL) { |
| 462 |
SDL_Rect srect; |
| 463 |
SDL_Rect drect; |
| 464 |
int oldx; |
| 465 |
int oldy; |
| 466 |
int oldalpha; |
| 467 |
|
| 468 |
srect.x = (SurfaceFlip->w - (frame % (SurfaceFlip->w / |
| 469 |
Width)) * Width) - Width; |
| 470 |
srect.y = (frame / (SurfaceFlip->w / Width)) * Height; |
| 471 |
srect.w = Width; |
| 472 |
srect.h = Height; |
| 473 |
|
| 474 |
oldx = x; |
| 475 |
oldy = y; |
| 476 |
CLIP_RECTANGLE(x, y, srect.w, srect.h); |
| 477 |
srect.x += x - oldx; |
| 478 |
srect.y += y - oldy; |
| 479 |
|
| 480 |
drect.x = x; |
| 481 |
drect.y = y; |
| 482 |
|
| 483 |
oldalpha = Surface->format->alpha; |
| 484 |
SDL_SetAlpha(Surface, SDL_SRCALPHA, alpha); |
| 485 |
SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect); |
| 486 |
SDL_SetAlpha(Surface, SDL_SRCALPHA, oldalpha); |
| 487 |
} else { |
| 488 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 489 |
glColor4ub(255, 255, 255, alpha); |
| 490 |
DrawFrameClipX(frame, x, y); |
| 491 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
** Draw graphic object clipped, flipped, and with player colors. |
| 497 |
** |
| 498 |
** @param player player number |
| 499 |
** @param frame number of frame (object index) |
| 500 |
** @param x x coordinate on the screen |
| 501 |
** @param y y coordinate on the screen |
| 502 |
*/ |
| 503 |
void CPlayerColorGraphic::DrawPlayerColorFrameClipX(int player, unsigned frame, |
| 504 |
int x, int y) |
| 505 |
{ |
| 506 |
if (!UseOpenGL) { |
| 507 |
GraphicPlayerPixels(&Players[player], this); |
| 508 |
DrawFrameClipX(frame, x, y); |
| 509 |
} else { |
| 510 |
if (!PlayerColorTextures[player]) { |
| 511 |
MakePlayerColorTexture(this, player); |
| 512 |
} |
| 513 |
DoDrawFrameClipX(PlayerColorTextures[player], frame, x, y); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
/*---------------------------------------------------------------------------- |
| 518 |
-- Global functions |
| 519 |
----------------------------------------------------------------------------*/ |
| 520 |
|
| 521 |
/** |
| 522 |
** Make a new graphic object. |
| 523 |
** |
| 524 |
** @param file Filename |
| 525 |
** @param w Width of a frame (optional) |
| 526 |
** @param h Height of a frame (optional) |
| 527 |
** |
| 528 |
** @return New graphic object |
| 529 |
*/ |
| 530 |
CGraphic *CGraphic::New(const std::string &file, int w, int h) |
| 531 |
{ |
| 532 |
if (file.empty()) { |
| 533 |
return new CGraphic; |
| 534 |
} |
| 535 |
|
| 536 |
CGraphicsModule::AssertOK(); |
| 537 |
|
| 538 |
CGraphic *g = GraphicHash[file]; |
| 539 |
if (!g) { |
| 540 |
g = new CGraphic; |
| 541 |
if (!g) { |
| 542 |
fprintf(stderr, "Out of memory\n"); |
| 543 |
ExitFatal(-1); |
| 544 |
} |
| 545 |
// FIXME: use a constructor for this |
| 546 |
g->File = file; |
| 547 |
g->HashFile = g->File; |
| 548 |
g->Width = w; |
| 549 |
g->Height = h; |
| 550 |
GraphicHash[g->HashFile] = g; |
| 551 |
} else { |
| 552 |
++g->Refs; |
| 553 |
Assert((w == 0 || g->Width == w) && (g->Height == h || h == 0)); |
| 554 |
} |
| 555 |
|
| 556 |
return g; |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
** Make a new player color graphic object. |
| 561 |
** |
| 562 |
** @param file Filename |
| 563 |
** @param w Width of a frame (optional) |
| 564 |
** @param h Height of a frame (optional) |
| 565 |
** |
| 566 |
** @return New graphic object |
| 567 |
*/ |
| 568 |
CPlayerColorGraphic *CPlayerColorGraphic::New(const std::string &file, int w, int h) |
| 569 |
{ |
| 570 |
if (file.empty()) { |
| 571 |
return new CPlayerColorGraphic; |
| 572 |
} |
| 573 |
|
| 574 |
CGraphicsModule::AssertOK(); |
| 575 |
|
| 576 |
CPlayerColorGraphic *g = dynamic_cast<CPlayerColorGraphic *>(GraphicHash[file]); |
| 577 |
if (!g) { |
| 578 |
g = new CPlayerColorGraphic; |
| 579 |
if (!g) { |
| 580 |
fprintf(stderr, "Out of memory\n"); |
| 581 |
ExitFatal(-1); |
| 582 |
} |
| 583 |
// FIXME: use a constructor for this |
| 584 |
g->File = file; |
| 585 |
g->HashFile = g->File; |
| 586 |
g->Width = w; |
| 587 |
g->Height = h; |
| 588 |
GraphicHash[g->HashFile] = g; |
| 589 |
} else { |
| 590 |
++g->Refs; |
| 591 |
Assert((w == 0 || g->Width == w) && (g->Height == h || h == 0)); |
| 592 |
} |
| 593 |
|
| 594 |
return g; |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
** Make a new graphic object. Don't reuse a graphic from the hash table. |
| 599 |
** |
| 600 |
** @param file Filename |
| 601 |
** @param w Width of a frame (optional) |
| 602 |
** @param h Height of a frame (optional) |
| 603 |
** |
| 604 |
** @return New graphic object |
| 605 |
*/ |
| 606 |
CGraphic *CGraphic::ForceNew(const std::string &file, int w, int h) |
| 607 |
{ |
| 608 |
CGraphicsModule::AssertOK(); |
| 609 |
CGraphic *g = new CGraphic; |
| 610 |
if (!g) { |
| 611 |
fprintf(stderr, "Out of memory\n"); |
| 612 |
ExitFatal(-1); |
| 613 |
} |
| 614 |
g->File = file; |
| 615 |
int bufSize = file.size() + 32; |
| 616 |
char *hashfile = new char[bufSize]; |
| 617 |
sprintf_s(hashfile, bufSize, "%s%d", file.c_str(), HashCount++); |
| 618 |
g->HashFile = hashfile; |
| 619 |
delete[] hashfile; |
| 620 |
g->Width = w; |
| 621 |
g->Height = h; |
| 622 |
GraphicHash[g->HashFile] = g; |
| 623 |
|
| 624 |
return g; |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
** Clone a graphic |
| 629 |
*/ |
| 630 |
CGraphic *CGraphic::Clone() const |
| 631 |
{ |
| 632 |
CGraphic *g = CGraphic::ForceNew(this->File, this->Width, this->Height); |
| 633 |
|
| 634 |
if (this->IsLoaded()) { |
| 635 |
g->Load(); |
| 636 |
} |
| 637 |
|
| 638 |
return g; |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
** Make a new player color graphic object. Don't reuse a graphic from the |
| 643 |
** hash table. |
| 644 |
** |
| 645 |
** @param file Filename |
| 646 |
** @param w Width of a frame (optional) |
| 647 |
** @param h Height of a frame (optional) |
| 648 |
** |
| 649 |
** @return New graphic object |
| 650 |
*/ |
| 651 |
CPlayerColorGraphic *CPlayerColorGraphic::ForceNew(const std::string &file, int w, int h) |
| 652 |
{ |
| 653 |
CGraphicsModule::AssertOK(); |
| 654 |
CPlayerColorGraphic *g = new CPlayerColorGraphic; |
| 655 |
if (!g) { |
| 656 |
fprintf(stderr, "Out of memory\n"); |
| 657 |
ExitFatal(-1); |
| 658 |
} |
| 659 |
g->File = file; |
| 660 |
size_t bufSize = file.size() + 32; |
| 661 |
char *hashfile = new char[bufSize]; |
| 662 |
sprintf_s(hashfile, bufSize, "%s%d", file.c_str(), HashCount++); |
| 663 |
g->HashFile = hashfile; |
| 664 |
delete[] hashfile; |
| 665 |
g->Width = w; |
| 666 |
g->Height = h; |
| 667 |
GraphicHash[g->HashFile] = g; |
| 668 |
|
| 669 |
return g; |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
** Load the size of a graphic, but not the actual pixels. |
| 674 |
** Also, unlike Load(), this does not abort the whole process |
| 675 |
** if the file cannot be loaded. |
| 676 |
** |
| 677 |
** @param[out] w width of the graphic, in pixels |
| 678 |
** @param[out] h height of the graphic, in pixels |
| 679 |
** |
| 680 |
** @return true on success, or false on error. |
| 681 |
*/ |
| 682 |
bool CGraphic::LoadGraphicSize(int *w, int *h) |
| 683 |
{ |
| 684 |
if (GraphicWidth == 0 && GraphicHeight == 0) { |
| 685 |
if (LoadGraphicPNG(this, true) == -1) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
Assert(GraphicWidth != 0 && GraphicHeight != 0); |
| 691 |
*w = GraphicWidth; |
| 692 |
*h = GraphicHeight; |
| 693 |
return true; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
/** |
| 698 |
** Load a graphic |
| 699 |
*/ |
| 700 |
void CGraphic::Load() |
| 701 |
{ |
| 702 |
if (Surface) { |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
CGraphicsModule::AssertOK(); |
| 707 |
|
| 708 |
// TODO: More formats? |
| 709 |
if (LoadGraphicPNG(this, false) == -1) { |
| 710 |
fprintf(stderr, "Can't load the graphic `%s'\n", File.c_str()); |
| 711 |
ExitFatal(-1); |
| 712 |
} |
| 713 |
|
| 714 |
if (!Width) { |
| 715 |
Width = GraphicWidth; |
| 716 |
} |
| 717 |
if (!Height) { |
| 718 |
Height = GraphicHeight; |
| 719 |
} |
| 720 |
|
| 721 |
Assert(Width <= GraphicWidth && Height <= GraphicHeight); |
| 722 |
|
| 723 |
if ((GraphicWidth / Width) * Width != GraphicWidth || |
| 724 |
(GraphicHeight / Height) * Height != GraphicHeight) { |
| 725 |
fprintf(stderr, "Invalid graphic (width, height) %s\n", File.c_str()); |
| 726 |
fprintf(stderr, "Expected: (%d,%d) Found: (%d,%d)\n", |
| 727 |
Width, Height, GraphicWidth, GraphicHeight); |
| 728 |
ExitFatal(-1); |
| 729 |
} |
| 730 |
|
| 731 |
NumFrames = GraphicWidth / Width * GraphicHeight / Height; |
| 732 |
|
| 733 |
if (UseOpenGL) { |
| 734 |
MakeTexture(this); |
| 735 |
Graphics.push_back(this); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
** Free a SDL surface |
| 741 |
** |
| 742 |
** @param surface SDL surface to free |
| 743 |
*/ |
| 744 |
static void FreeSurface(SDL_Surface **surface) |
| 745 |
{ |
| 746 |
if (!*surface) { |
| 747 |
return; |
| 748 |
} |
| 749 |
|
| 750 |
unsigned char *pixels = NULL; |
| 751 |
|
| 752 |
if ((*surface)->flags & SDL_PREALLOC) { |
| 753 |
pixels = (unsigned char *)(*surface)->pixels; |
| 754 |
} |
| 755 |
|
| 756 |
SDL_FreeSurface(*surface); |
| 757 |
delete[] pixels; |
| 758 |
*surface = NULL; |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
** Free a graphic |
| 763 |
** |
| 764 |
** @param g Pointer to the graphic |
| 765 |
*/ |
| 766 |
void CGraphic::Free(CGraphic *g) |
| 767 |
{ |
| 768 |
if (!g) { |
| 769 |
return; |
| 770 |
} |
| 771 |
|
| 772 |
Assert(g->Refs); |
| 773 |
|
| 774 |
--g->Refs; |
| 775 |
if (!g->Refs) { |
| 776 |
// No more uses of this graphic |
| 777 |
if (UseOpenGL) { |
| 778 |
if (g->Textures) { |
| 779 |
glDeleteTextures(g->NumTextures, g->Textures); |
| 780 |
delete[] g->Textures; |
| 781 |
} |
| 782 |
CPlayerColorGraphic *cg = dynamic_cast<CPlayerColorGraphic *>(g); |
| 783 |
if (cg) { |
| 784 |
for (int i = 0; i < PlayerMax; ++i) { |
| 785 |
if (cg->PlayerColorTextures[i]) { |
| 786 |
glDeleteTextures(cg->NumTextures, cg->PlayerColorTextures[i]); |
| 787 |
delete[] cg->PlayerColorTextures[i]; |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
if (GraphicsState == ModuleOK) { |
| 793 |
Graphics.remove(g); |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
FreeSurface(&g->Surface); |
| 798 |
FreeSurface(&g->SurfaceFlip); |
| 799 |
|
| 800 |
if (!g->HashFile.empty()) { |
| 801 |
if (GraphicsState == ModuleOK) { |
| 802 |
GraphicHash.erase(g->HashFile); |
| 803 |
} |
| 804 |
} |
| 805 |
delete g; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
** Free OpenGL graphics |
| 811 |
*/ |
| 812 |
void FreeOpenGLGraphics() |
| 813 |
{ |
| 814 |
CGraphicsModule::AssertOK(); |
| 815 |
|
| 816 |
std::list<CGraphic *>::iterator i; |
| 817 |
for (i = Graphics.begin(); i != Graphics.end(); ++i) { |
| 818 |
if ((*i)->Textures) { |
| 819 |
glDeleteTextures((*i)->NumTextures, (*i)->Textures); |
| 820 |
} |
| 821 |
CPlayerColorGraphic *cg = dynamic_cast<CPlayerColorGraphic *>(*i); |
| 822 |
if (cg) { |
| 823 |
for (int j = 0; j < PlayerMax; ++j) { |
| 824 |
if (cg->PlayerColorTextures[j]) { |
| 825 |
glDeleteTextures(cg->NumTextures, cg->PlayerColorTextures[j]); |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
** Reload OpenGL graphics |
| 834 |
*/ |
| 835 |
void ReloadGraphics() |
| 836 |
{ |
| 837 |
CGraphicsModule::AssertOK(); |
| 838 |
std::list<CGraphic *>::iterator i; |
| 839 |
for (i = Graphics.begin(); i != Graphics.end(); ++i) { |
| 840 |
if ((*i)->Textures) { |
| 841 |
delete[] (*i)->Textures; |
| 842 |
(*i)->Textures = NULL; |
| 843 |
MakeTexture(*i); |
| 844 |
} |
| 845 |
CPlayerColorGraphic *cg = dynamic_cast<CPlayerColorGraphic *>(*i); |
| 846 |
if (cg) { |
| 847 |
for (int j = 0; j < PlayerMax; ++j) { |
| 848 |
if (cg->PlayerColorTextures[j]) { |
| 849 |
delete[] cg->PlayerColorTextures[j]; |
| 850 |
cg->PlayerColorTextures[j] = NULL; |
| 851 |
MakePlayerColorTexture(cg, j); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
/** |
| 859 |
** Flip graphic and store in graphic->SurfaceFlip |
| 860 |
*/ |
| 861 |
void CGraphic::Flip() |
| 862 |
{ |
| 863 |
if (UseOpenGL) { |
| 864 |
// OpenGL mode does not use SurfaceFlip; instead, |
| 865 |
// it swaps the texture coordinates when drawing. |
| 866 |
return; |
| 867 |
} |
| 868 |
|
| 869 |
int i; |
| 870 |
int j; |
| 871 |
SDL_Surface *s; |
| 872 |
|
| 873 |
if (SurfaceFlip) { |
| 874 |
return; |
| 875 |
} |
| 876 |
|
| 877 |
s = SurfaceFlip = SDL_ConvertSurface(Surface, Surface->format, SDL_SWSURFACE); |
| 878 |
if (Surface->flags & SDL_SRCCOLORKEY) { |
| 879 |
SDL_SetColorKey(SurfaceFlip, SDL_SRCCOLORKEY | SDL_RLEACCEL, |
| 880 |
Surface->format->colorkey); |
| 881 |
} |
| 882 |
|
| 883 |
SDL_LockSurface(Surface); |
| 884 |
SDL_LockSurface(s); |
| 885 |
switch (s->format->BytesPerPixel) { |
| 886 |
case 1: |
| 887 |
for (i = 0; i < s->h; ++i) { |
| 888 |
for (j = 0; j < s->w; ++j) { |
| 889 |
((char *)s->pixels)[j + i * s->pitch] = |
| 890 |
((char *)Surface->pixels)[s->w - j - 1 + i * Surface->pitch]; |
| 891 |
} |
| 892 |
} |
| 893 |
break; |
| 894 |
case 3: |
| 895 |
for (i = 0; i < s->h; ++i) { |
| 896 |
for (j = 0; j < s->w; ++j) { |
| 897 |
memcpy(&((char *)s->pixels)[j + i * s->pitch], |
| 898 |
&((char *)Surface->pixels)[(s->w - j - 1) * 3 + i * Surface->pitch], 3); |
| 899 |
} |
| 900 |
} |
| 901 |
break; |
| 902 |
case 4: |
| 903 |
for (i = 0; i < s->h; ++i) { |
| 904 |
for (j = 0; j < s->w; ++j) { |
| 905 |
*(Uint32 *)&((char *)s->pixels)[j * 4 + i * s->pitch] = |
| 906 |
*(Uint32 *)&((char *)Surface->pixels)[(s->w - j - 1) * 4 + i * Surface->pitch]; |
| 907 |
} |
| 908 |
} |
| 909 |
break; |
| 910 |
} |
| 911 |
SDL_UnlockSurface(Surface); |
| 912 |
SDL_UnlockSurface(s); |
| 913 |
} |
| 914 |
|
| 915 |
/** |
| 916 |
** Convert the SDL surface to the display format |
| 917 |
*/ |
| 918 |
void CGraphic::UseDisplayFormat() |
| 919 |
{ |
| 920 |
if (UseOpenGL) { |
| 921 |
return; |
| 922 |
} |
| 923 |
|
| 924 |
SDL_Surface *s; |
| 925 |
|
| 926 |
s = Surface; |
| 927 |
if (s->format->Amask != 0) { |
| 928 |
Surface = SDL_DisplayFormatAlpha(s); |
| 929 |
} else { |
| 930 |
Surface = SDL_DisplayFormat(s); |
| 931 |
} |
| 932 |
SDL_FreeSurface(s); |
| 933 |
|
| 934 |
if (SurfaceFlip) { |
| 935 |
s = SurfaceFlip; |
| 936 |
if (s->format->Amask != 0) { |
| 937 |
SurfaceFlip = SDL_DisplayFormatAlpha(s); |
| 938 |
} else { |
| 939 |
SurfaceFlip = SDL_DisplayFormat(s); |
| 940 |
} |
| 941 |
SDL_FreeSurface(s); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
/** |
| 946 |
** Find the next power of 2 >= x |
| 947 |
*/ |
| 948 |
static int PowerOf2(int x) |
| 949 |
{ |
| 950 |
int i; |
| 951 |
for (i = 1; i < x; i <<= 1) ; |
| 952 |
return i; |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
** Make an OpenGL texture or textures out of a graphic object. |
| 957 |
** |
| 958 |
** @param g The graphic object. |
| 959 |
** @param texture Texture. |
| 960 |
** @param colors Unit colors. |
| 961 |
** @param ow Offset width. |
| 962 |
** @param oh Offset height. |
| 963 |
*/ |
| 964 |
static void MakeTextures2(CGraphic *g, GLuint texture, CUnitColors *colors, |
| 965 |
int ow, int oh) |
| 966 |
{ |
| 967 |
int h; |
| 968 |
int w; |
| 969 |
unsigned char *tex; |
| 970 |
unsigned char *tp; |
| 971 |
const unsigned char *sp; |
| 972 |
Uint32 ckey; |
| 973 |
int useckey; |
| 974 |
int bpp; |
| 975 |
unsigned char alpha; |
| 976 |
Uint32 b; |
| 977 |
Uint32 c; |
| 978 |
Uint32 pc; |
| 979 |
SDL_PixelFormat *f; |
| 980 |
int maxw; |
| 981 |
int maxh; |
| 982 |
|
| 983 |
useckey = g->Surface->flags & SDL_SRCCOLORKEY; |
| 984 |
f = g->Surface->format; |
| 985 |
bpp = f->BytesPerPixel; |
| 986 |
ckey = f->colorkey; |
| 987 |
|
| 988 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 989 |
maxw = std::min(g->GraphicWidth - ow, GLMaxTextureSize); |
| 990 |
maxh = std::min(g->GraphicHeight - oh, GLMaxTextureSize); |
| 991 |
w = PowerOf2(maxw); |
| 992 |
h = PowerOf2(maxh); |
| 993 |
tex = new unsigned char[w * h * 4]; |
| 994 |
memset(tex, 0, w * h * 4); |
| 995 |
if (g->Surface->flags & SDL_SRCALPHA) { |
| 996 |
alpha = f->alpha; |
| 997 |
} else { |
| 998 |
alpha = 0xff; |
| 999 |
} |
| 1000 |
|
| 1001 |
SDL_LockSurface(g->Surface); |
| 1002 |
glBindTexture(GL_TEXTURE_2D, texture); |
| 1003 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
| 1004 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
| 1005 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 1006 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 1007 |
for (int i = 0; i < maxh; ++i) { |
| 1008 |
sp = (const unsigned char *)g->Surface->pixels + ow * bpp + |
| 1009 |
(oh + i) * g->Surface->pitch; |
| 1010 |
tp = tex + i * w * 4; |
| 1011 |
for (int j = 0; j < maxw; ++j) { |
| 1012 |
if (bpp == 1) { |
| 1013 |
if (useckey && *sp == ckey) { |
| 1014 |
tp[3] = 0; |
| 1015 |
} else { |
| 1016 |
SDL_Color p = f->palette->colors[*sp]; |
| 1017 |
tp[0] = p.r; |
| 1018 |
tp[1] = p.g; |
| 1019 |
tp[2] = p.b; |
| 1020 |
tp[3] = alpha; |
| 1021 |
} |
| 1022 |
if (colors) { |
| 1023 |
for (int z = 0; z < PlayerColorIndexCount; ++z) { |
| 1024 |
if (*sp == PlayerColorIndexStart + z) { |
| 1025 |
SDL_Color p = colors->Colors[z]; |
| 1026 |
tp[0] = p.r; |
| 1027 |
tp[1] = p.g; |
| 1028 |
tp[2] = p.b; |
| 1029 |
tp[3] = 0xff; |
| 1030 |
break; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
++sp; |
| 1035 |
} else { |
| 1036 |
if (bpp == 4) { |
| 1037 |
c = *(Uint32 *)sp; |
| 1038 |
} else { |
| 1039 |
c = (sp[f->Rshift >> 3] << f->Rshift) | |
| 1040 |
(sp[f->Gshift >> 3] << f->Gshift) | |
| 1041 |
(sp[f->Bshift >> 3] << f->Bshift); |
| 1042 |
c |= ((alpha | (alpha << 8) | (alpha << 16) | (alpha << 24)) ^ |
| 1043 |
(f->Rmask | f->Gmask | f->Bmask)); |
| 1044 |
} |
| 1045 |
*(Uint32 *)tp = c; |
| 1046 |
if (colors) { |
| 1047 |
b = (c & f->Bmask) >> f->Bshift; |
| 1048 |
if (b && ((c & f->Rmask) >> f->Rshift) == 0 && |
| 1049 |
((c & f->Gmask) >> f->Gshift) == b) { |
| 1050 |
pc = ((colors->Colors[0].r * b / 255) << f->Rshift) | |
| 1051 |
((colors->Colors[0].g * b / 255) << f->Gshift) | |
| 1052 |
((colors->Colors[0].b * b / 255) << f->Bshift); |
| 1053 |
if (bpp == 4) { |
| 1054 |
pc |= (c & f->Amask); |
| 1055 |
} else { |
| 1056 |
pc |= (0xFFFFFFFF ^ (f->Rmask | f->Gmask | f->Bmask)); |
| 1057 |
} |
| 1058 |
*(Uint32 *)tp = pc; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
sp += bpp; |
| 1062 |
} |
| 1063 |
tp += 4; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
GLint internalformat = GL_RGBA; |
| 1068 |
if (GLTextureCompressionSupported && UseGLTextureCompression) { |
| 1069 |
internalformat = GL_COMPRESSED_RGBA; |
| 1070 |
} |
| 1071 |
|
| 1072 |
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex); |
| 1073 |
|
| 1074 |
#ifdef DEBUG |
| 1075 |
int x; |
| 1076 |
if ((x = glGetError())) { |
| 1077 |
DebugPrint("glTexImage2D(%x)\n" _C_ x); |
| 1078 |
} |
| 1079 |
#endif |
| 1080 |
SDL_UnlockSurface(g->Surface); |
| 1081 |
delete[] tex; |
| 1082 |
} |
| 1083 |
|
| 1084 |
/** |
| 1085 |
** Make an OpenGL texture or textures out of a graphic object. |
| 1086 |
** |
| 1087 |
** @param g The graphic object. |
| 1088 |
** @param player Player number. |
| 1089 |
** @param colors Unit colors. |
| 1090 |
*/ |
| 1091 |
static void MakeTextures(CGraphic *g, int player, CUnitColors *colors) |
| 1092 |
{ |
| 1093 |
int i; |
| 1094 |
int tw; |
| 1095 |
int th; |
| 1096 |
GLuint *textures; |
| 1097 |
|
| 1098 |
tw = (g->GraphicWidth - 1) / GLMaxTextureSize + 1; |
| 1099 |
th = (g->GraphicHeight - 1) / GLMaxTextureSize + 1; |
| 1100 |
|
| 1101 |
i = g->GraphicWidth % GLMaxTextureSize; |
| 1102 |
if (i == 0) { |
| 1103 |
i = GLMaxTextureSize; |
| 1104 |
} |
| 1105 |
g->TextureWidth = (GLfloat)i / PowerOf2(i); |
| 1106 |
i = g->GraphicHeight % GLMaxTextureSize; |
| 1107 |
if (i == 0) { |
| 1108 |
i = GLMaxTextureSize; |
| 1109 |
} |
| 1110 |
g->TextureHeight = (GLfloat)i / PowerOf2(i); |
| 1111 |
|
| 1112 |
g->NumTextures = tw * th; |
| 1113 |
if (g->NumTextures > 1) { |
| 1114 |
tw = tw; |
| 1115 |
} |
| 1116 |
CPlayerColorGraphic *cg = dynamic_cast<CPlayerColorGraphic *>(g); |
| 1117 |
if (!colors || !cg) { |
| 1118 |
textures = g->Textures = new GLuint[g->NumTextures]; |
| 1119 |
glGenTextures(g->NumTextures, g->Textures); |
| 1120 |
} else { |
| 1121 |
textures = cg->PlayerColorTextures[player] = new GLuint[cg->NumTextures]; |
| 1122 |
glGenTextures(cg->NumTextures, cg->PlayerColorTextures[player]); |
| 1123 |
} |
| 1124 |
|
| 1125 |
for (int j = 0; j < th; ++j) { |
| 1126 |
for (i = 0; i < tw; ++i) { |
| 1127 |
MakeTextures2(g, textures[j * tw + i], colors, GLMaxTextureSize * i, GLMaxTextureSize * j); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
/** |
| 1133 |
** Make an OpenGL texture or textures out of a graphic object. |
| 1134 |
** |
| 1135 |
** @param g The graphic object. |
| 1136 |
*/ |
| 1137 |
void MakeTexture(CGraphic *g) |
| 1138 |
{ |
| 1139 |
if (g->Textures) { |
| 1140 |
return; |
| 1141 |
} |
| 1142 |
|
| 1143 |
MakeTextures(g, 0, NULL); |
| 1144 |
} |
| 1145 |
|
| 1146 |
/** |
| 1147 |
** Make an OpenGL texture with the player colors. |
| 1148 |
** |
| 1149 |
** @param g The graphic to texture with player colors. |
| 1150 |
** @param player Player number to make textures for. |
| 1151 |
*/ |
| 1152 |
void MakePlayerColorTexture(CPlayerColorGraphic *g, int player) |
| 1153 |
{ |
| 1154 |
if (g->PlayerColorTextures[player]) { |
| 1155 |
return; |
| 1156 |
} |
| 1157 |
|
| 1158 |
MakeTextures(g, player, &Players[player].UnitColors); |
| 1159 |
} |
| 1160 |
|
| 1161 |
/** |
| 1162 |
** Resize a graphic |
| 1163 |
** |
| 1164 |
** @param w New width of graphic. |
| 1165 |
** @param h New height of graphic. |
| 1166 |
*/ |
| 1167 |
void CGraphic::Resize(int w, int h) |
| 1168 |
{ |
| 1169 |
int i; |
| 1170 |
int j; |
| 1171 |
unsigned char *data; |
| 1172 |
unsigned char *pixels; |
| 1173 |
int x; |
| 1174 |
int bpp; |
| 1175 |
|
| 1176 |
Assert(Surface); // can't resize before it's been loaded |
| 1177 |
|
| 1178 |
if (GraphicWidth == w && GraphicHeight == h) { |
| 1179 |
return; |
| 1180 |
} |
| 1181 |
|
| 1182 |
// Resizing the same image multiple times looks horrible |
| 1183 |
// If the image has already been resized then get a clean copy first |
| 1184 |
if (Resized) { |
| 1185 |
FreeSurface(&Surface); |
| 1186 |
FreeSurface(&SurfaceFlip); |
| 1187 |
|
| 1188 |
if (UseOpenGL && Textures) { |
| 1189 |
glDeleteTextures(NumTextures, Textures); |
| 1190 |
delete[] Textures; |
| 1191 |
Textures = NULL; |
| 1192 |
} |
| 1193 |
|
| 1194 |
this->Width = this->Height = 0; |
| 1195 |
this->Load(); |
| 1196 |
|
| 1197 |
Resized = false; |
| 1198 |
if (GraphicWidth == w && GraphicHeight == h) { |
| 1199 |
return; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
Resized = true; |
| 1204 |
|
| 1205 |
bpp = Surface->format->BytesPerPixel; |
| 1206 |
if (bpp == 1) { |
| 1207 |
SDL_Color pal[256]; |
| 1208 |
Uint32 ckey; |
| 1209 |
int useckey; |
| 1210 |
|
| 1211 |
SDL_LockSurface(Surface); |
| 1212 |
|
| 1213 |
pixels = (unsigned char *)Surface->pixels; |
| 1214 |
data = new unsigned char[w * h]; |
| 1215 |
x = 0; |
| 1216 |
|
| 1217 |
for (i = 0; i < h; ++i) { |
| 1218 |
for (j = 0; j < w; ++j) { |
| 1219 |
data[x] = pixels[(i * Height / h) * Surface->pitch + j * Width / w]; |
| 1220 |
++x; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
SDL_UnlockSurface(Surface); |
| 1225 |
memcpy(pal, Surface->format->palette->colors, sizeof(SDL_Color) * 256); |
| 1226 |
useckey = Surface->flags & SDL_SRCCOLORKEY; |
| 1227 |
ckey = Surface->format->colorkey; |
| 1228 |
SDL_FreeSurface(Surface); |
| 1229 |
|
| 1230 |
Surface = SDL_CreateRGBSurfaceFrom(data, w, h, 8, w, 0, 0, 0, 0); |
| 1231 |
SDL_SetPalette(Surface, SDL_LOGPAL | SDL_PHYSPAL, pal, 0, 256); |
| 1232 |
if (useckey) { |
| 1233 |
SDL_SetColorKey(Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, ckey); |
| 1234 |
} |
| 1235 |
} else { |
| 1236 |
int ix, iy; |
| 1237 |
float fx, fy, fz; |
| 1238 |
unsigned char *p1, *p2, *p3, *p4; |
| 1239 |
|
| 1240 |
SDL_LockSurface(Surface); |
| 1241 |
|
| 1242 |
pixels = (unsigned char *)Surface->pixels; |
| 1243 |
data = new unsigned char[w * h * bpp]; |
| 1244 |
x = 0; |
| 1245 |
|
| 1246 |
for (i = 0; i < h; ++i) { |
| 1247 |
fy = (float)i * Height / h; |
| 1248 |
iy = (int)fy; |
| 1249 |
fy -= iy; |
| 1250 |
for (j = 0; j < w; ++j) { |
| 1251 |
fx = (float)j * Width / w; |
| 1252 |
ix = (int)fx; |
| 1253 |
fx -= ix; |
| 1254 |
fz = (fx + fy) / 2; |
| 1255 |
|
| 1256 |
p1 = &pixels[iy * Surface->pitch + ix * bpp]; |
| 1257 |
p2 = (iy != Surface->h - 1) ? |
| 1258 |
&pixels[(iy + 1) * Surface->pitch + ix * bpp] : |
| 1259 |
p1; |
| 1260 |
p3 = (ix != Surface->w - 1) ? |
| 1261 |
&pixels[iy * Surface->pitch + (ix + 1) * bpp] : |
| 1262 |
p1; |
| 1263 |
p4 = (iy != Surface->h - 1 && ix != Surface->w - 1) ? |
| 1264 |
&pixels[(iy + 1) * Surface->pitch + (ix + 1) * bpp] : |
| 1265 |
p1; |
| 1266 |
|
| 1267 |
data[x * bpp + 0] = static_cast<unsigned char>( |
| 1268 |
(p1[0] * (1 - fy) + p2[0] * fy + |
| 1269 |
p1[0] * (1 - fx) + p3[0] * fx + |
| 1270 |
p1[0] * (1 - fz) + p4[0] * fz) / 3.0 + .5); |
| 1271 |
data[x * bpp + 1] = static_cast<unsigned char>( |
| 1272 |
(p1[1] * (1 - fy) + p2[1] * fy + |
| 1273 |
p1[1] * (1 - fx) + p3[1] * fx + |
| 1274 |
p1[1] * (1 - fz) + p4[1] * fz) / 3.0 + .5); |
| 1275 |
data[x * bpp + 2] = static_cast<unsigned char>( |
| 1276 |
(p1[2] * (1 - fy) + p2[2] * fy + |
| 1277 |
p1[2] * (1 - fx) + p3[2] * fx + |
| 1278 |
p1[2] * (1 - fz) + p4[2] * fz) / 3.0 + .5); |
| 1279 |
if (bpp == 4) { |
| 1280 |
data[x * bpp + 3] = static_cast<unsigned char>( |
| 1281 |
(p1[3] * (1 - fy) + p2[3] * fy + |
| 1282 |
p1[3] * (1 - fx) + p3[3] * fx + |
| 1283 |
p1[3] * (1 - fz) + p4[3] * fz) / 3.0 + .5); |
| 1284 |
} |
| 1285 |
++x; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
int Rmask = Surface->format->Rmask; |
| 1290 |
int Gmask = Surface->format->Gmask; |
| 1291 |
int Bmask = Surface->format->Bmask; |
| 1292 |
int Amask = Surface->format->Amask; |
| 1293 |
|
| 1294 |
SDL_UnlockSurface(Surface); |
| 1295 |
SDL_FreeSurface(Surface); |
| 1296 |
|
| 1297 |
Surface = SDL_CreateRGBSurfaceFrom(data, w, h, 8 * bpp, w * bpp, |
| 1298 |
Rmask, Gmask, Bmask, Amask); |
| 1299 |
} |
| 1300 |
|
| 1301 |
Width = GraphicWidth = w; |
| 1302 |
Height = GraphicHeight = h; |
| 1303 |
|
| 1304 |
if (UseOpenGL && Textures) { |
| 1305 |
glDeleteTextures(NumTextures, Textures); |
| 1306 |
delete[] Textures; |
| 1307 |
Textures = NULL; |
| 1308 |
MakeTexture(this); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
/** |
| 1313 |
** Check if a pixel is transparent |
| 1314 |
** |
| 1315 |
** @param x X coordinate |
| 1316 |
** @param y Y coordinate |
| 1317 |
** |
| 1318 |
** @return True if the pixel is transparent, False otherwise |
| 1319 |
*/ |
| 1320 |
bool CGraphic::TransparentPixel(int x, int y) |
| 1321 |
{ |
| 1322 |
unsigned char *p; |
| 1323 |
int bpp; |
| 1324 |
bool ret; |
| 1325 |
|
| 1326 |
bpp = Surface->format->BytesPerPixel; |
| 1327 |
if ((bpp == 1 && !(Surface->flags & SDL_SRCCOLORKEY)) || bpp == 3) { |
| 1328 |
return false; |
| 1329 |
} |
| 1330 |
|
| 1331 |
ret = 0; |
| 1332 |
SDL_LockSurface(Surface); |
| 1333 |
p = (unsigned char *)Surface->pixels + y * Surface->pitch + x * bpp; |
| 1334 |
if (bpp == 1) { |
| 1335 |
if (*p == Surface->format->colorkey) { |
| 1336 |
ret = true; |
| 1337 |
} |
| 1338 |
} else { |
| 1339 |
if (p[Surface->format->Ashift >> 3] == 255) { |
| 1340 |
ret = true; |
| 1341 |
} |
| 1342 |
} |
| 1343 |
SDL_UnlockSurface(Surface); |
| 1344 |
|
| 1345 |
return ret; |
| 1346 |
} |
| 1347 |
|
| 1348 |
/** |
| 1349 |
** Make shadow sprite |
| 1350 |
** |
| 1351 |
** @todo FIXME: 32bpp |
| 1352 |
*/ |
| 1353 |
void CGraphic::MakeShadow() |
| 1354 |
{ |
| 1355 |
SDL_Color colors[256]; |
| 1356 |
|
| 1357 |
// Set all colors in the palette to black and use 50% alpha |
| 1358 |
memset(colors, 0, sizeof(colors)); |
| 1359 |
|
| 1360 |
SDL_SetPalette(Surface, SDL_LOGPAL | SDL_PHYSPAL, colors, 0, 256); |
| 1361 |
SDL_SetAlpha(Surface, SDL_SRCALPHA | SDL_RLEACCEL, 128); |
| 1362 |
|
| 1363 |
if (!UseOpenGL) { |
| 1364 |
if (SurfaceFlip) { |
| 1365 |
SDL_SetPalette(SurfaceFlip, SDL_LOGPAL | SDL_PHYSPAL, colors, 0, 256); |
| 1366 |
SDL_SetAlpha(SurfaceFlip, SDL_SRCALPHA | SDL_RLEACCEL, 128); |
| 1367 |
} |
| 1368 |
} |
| 1369 |
if (UseOpenGL) { |
| 1370 |
if (Textures) { |
| 1371 |
glDeleteTextures(NumTextures, Textures); |
| 1372 |
delete[] Textures; |
| 1373 |
Textures = NULL; |
| 1374 |
} |
| 1375 |
MakeTexture(this); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
void FreeGraphics() |
| 1380 |
{ |
| 1381 |
CGraphicsModule::AssertOK(); |
| 1382 |
std::map<std::string, CGraphic *>::iterator i; |
| 1383 |
while (!GraphicHash.empty()) { |
| 1384 |
i = GraphicHash.begin(); |
| 1385 |
CGraphic::Free((*i).second); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
//@} |