Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

background.cpp

Go to the documentation of this file.
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;     // Nothing to draw..
00015         staticBackground = false;
00016 }
00017 
00018 
00019 // Load background class using a static image
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 // Utility functions to actually set up a static image
00034 // background.
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                 // make a 3x3 screen of surf
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 // Functions to draw the background onto the surface
00069 // if needed.
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 //      return Draw (GD.getMainSurface());
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 // Needed to replace sprites on the screen
00098 // Rects here are in screen coordinates
00099 void Background::DrawRect (Rect &R)
00100 {
00101         DrawRect (GD.getMainSurface(), R);      
00102 }
00103 
00104 // This function assumes that xOffset and yOffset are valid
00105 // values: i.e. the distance between xOffset and staticImage->w is a full surf->w
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 // Sets the location in the world.
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 

Generated on Sun Dec 8 12:02:19 2002 for nnc by doxygen1.3-rc1