00001 #include <iostream>
00002 #include <string>
00003 #include "graphics.h"
00004 #include "util.h"
00005 #include "background.h"
00006 #include <iostream>
00007
00008 using namespace std;
00009
00010 Background::Background (Graphics &G) : staticImage (NULL), GD(G)
00011 {
00012 xOffset = 0;
00013 yOffset = 0;
00014 needRedraw = false;
00015 staticBackground = false;
00016 }
00017
00018
00019
00020 Background::Background (string img, Graphics &G) : GD(G)
00021 {
00022 SetStatic(img);
00023
00024 SetPosition (0, 0);
00025 }
00026
00027 Background::~Background()
00028 {
00029 if (staticImage)
00030 Graphics::freeImage(staticImage);
00031 }
00032
00033
00034
00035 void Background::SetStatic (string img)
00036 {
00037 SetStatic (Graphics::loadImage (img));
00038 }
00039
00040 void Background::SetStatic (Surface *surf)
00041 {
00042 const int xmul = 2;
00043 const int ymul = 2;
00044
00045 if (surf) {
00046 const int SURFW = surf->w * xmul;
00047 const int SURFH = surf->h * ymul;
00048
00049
00050 staticImage = Graphics::CreateSurfaceFrom (surf, SURFW, SURFH);
00051 for (int y = 0; y < ymul; y++) {
00052 for (int x = 0; x < xmul; x++) {
00053 Graphics::blit (staticImage, x * surf->w, y * surf->h, surf);
00054 }
00055 }
00056
00057 Graphics::TexturizeSurface (staticImage);
00058 staticBackground = true;
00059 needRedraw = true;
00060
00061 xMax = (float)(SURFW - GD.getWidth());
00062 yMax = (float)(SURFH - GD.getHeight());
00063 }
00064 }
00065
00066
00067
00068
00069
00070 bool Background::Draw ()
00071 {
00072 Rect D = Util_BuildRect (-xOffset, -yOffset, GD.getWidth(), GD.getHeight());
00073 Rect S = Util_BuildRect (0, 0, staticImage->w, staticImage->h);
00074
00075 DrawRect (GD.getMainSurface(), D, S);
00076
00077
00078 return true;
00079 }
00080
00081 bool Background::Draw (Surface *surf)
00082 {
00083 Rect S;
00084
00085 if (staticBackground && needRedraw) {
00086 S = Util_BuildRect (0, 0, surf->w, surf->h);
00087 DrawRect (surf, S);
00088
00089 needRedraw = false;
00090 return true;
00091 } else
00092
00093 return false;
00094 }
00095
00096
00097
00098
00099 void Background::DrawRect (Rect &R)
00100 {
00101 DrawRect (GD.getMainSurface(), R);
00102 }
00103
00104
00105
00106 void Background::DrawRect (Surface *surf, Rect &R)
00107 {
00108 Rect Src, Dest = R;
00109
00110 Src.x = R.x + (int)(xOffset + 0.5);
00111 Src.y = R.y + (int)(yOffset + 0.5);
00112 Src.w = R.w;
00113 Src.h = R.h;
00114
00115 DrawRect (surf, Dest, Src);
00116 }
00117
00118 void Background::DrawRect (Surface *dest, Rect &D, Rect &S)
00119 {
00120 Graphics::blit (dest, D, staticImage, S);
00121 }
00122
00123
00124 bool Background::SetPosition (float x, float y)
00125 {
00126 if (x >= xMax || y >= yMax || x < 0 || y < 0) {
00127 cout << "Invalid values for Background::SetPosition (" << x << ", " << y << ")" << endl;
00128 return false;
00129 }
00130
00131 if (x != xOffset || y != yOffset)
00132 needRedraw = true;
00133
00134 xOffset = x;
00135 yOffset = y;
00136 return needRedraw;
00137 }
00138