summaryrefslogtreecommitdiffstats
path: root/kmahjongg/Background.cpp
blob: 2cf7296f7967ad366331a905ebc873f450affe3b (plain)
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

#include "Background.h"
#include <tqimage.h>

Background::Background(): tile(true) {

  sourceImage = 0;
  backgroundImage = 0;
  backgroundPixmap = 0;
  backgroundShadowPixmap = 0;
}

Background::~Background() {
  delete sourceImage;
  delete backgroundImage;
  delete backgroundPixmap;
  delete backgroundShadowPixmap;
}

bool Background::load(const TQString &file, short width, short height) {
  w=width;
  h=height; 

  if (file == filename) {
	return true;
  }
  sourceImage = new TQImage();
  backgroundImage = new TQImage();
  backgroundPixmap = new TQPixmap();
  backgroundShadowPixmap = new TQPixmap();

  // try to load the image, return on failure 
  if(!sourceImage->load(file ))
    return false;

  // Just in case the image is loaded 8 bit
  if (sourceImage->depth() != 32)
    *sourceImage = sourceImage->convertDepth(32);  

  // call out to scale/tile the source image into the background image
  sourceToBackground();
  filename = file;
	
   return true;
}

// slot used when tile/scale option changes
void Background::scaleModeChanged() {
	sourceToBackground();
}

void Background::sizeChanged(int newW, int newH) {
	if (newW == w && newH == h)
		return;
	w = newW;
	h = newH;
	sourceToBackground();
}

void Background::sourceToBackground() {

  // Deallocate the old image and create the new one
  if (!backgroundImage->isNull())
    backgroundImage->reset();

  // the new version of kmahjongg uses true color images
  // to avoid the old color limitation.
  backgroundImage->create(w, h, 32);

  // Now we decide if we should scale the incoming image
  // or if we tile. First we check for an exact match which
  // should be true for all images created specifically for us.
  if ((sourceImage->width() == w) && (sourceImage->height() == h)) {
    *backgroundImage = *sourceImage;
    return;
  }
    
  if (tile) {
    // copy new to background wrapping on w and height
    for (int y=0; y<backgroundImage->height(); y++) {
      TQRgb *dest = (TQRgb *) backgroundImage->scanLine(y);
      TQRgb *src = (TQRgb *) sourceImage->scanLine(y % sourceImage->height());
      for (int x=0; x< backgroundImage->width(); x++) {
	*dest = *(src + (x % sourceImage->width()));
	dest++;
      }
    }
  } else {
    *backgroundImage = sourceImage->smoothScale(w, h);
    // Just incase the image is loaded 8 bit
    if (backgroundImage->depth() != 32)
      *backgroundImage = backgroundImage->convertDepth(32);
  }

  // Save a copy of the background as a pixmap for easy and quick
  // blitting.
  backgroundPixmap->convertFromImage(*backgroundImage);

  TQImage tmp;
  tmp.create(backgroundImage->width(), backgroundImage->height(), 32);
  for (int ys=0; ys < tmp.height(); ys++) {
	TQRgb *src = (TQRgb *) backgroundImage->scanLine(ys);
	TQRgb *dst = (TQRgb *) tmp.scanLine(ys);
	for (int xs=0; xs < tmp.width(); xs++) {
		*dst=TQColor(*src).dark(133).rgb();
		src++;
		dst++;
	}
  }	

  backgroundShadowPixmap->convertFromImage(tmp);

  return;
}