The Game Engine  1
Background.cpp
Go to the documentation of this file.
1 #include "Background.h"
2 #include "Game.h"
3 #include <iostream>
4 using namespace std;
5 
6 Background::Background( std::string name, std::string filename, bool wrap, Point2D pos) :
7 WorldObject(name, pos, -10000),
8  wrapping(wrap)
9 {
10  load(filename);
11  cout<<"background \""<<mName<<"\" created"<<endl;
12 }
13 
15  glDeleteTextures(1,&mBackground.image);
16  for(unsigned int i=0,end=collisionData.size(); i<end; ++i)
17  delete collisionData[i];
18 }
19 
21  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
22  glColor4f(1, 1, 1, mTransparency);
23  glLoadIdentity();
24  //cam pos
25  Point2D movePos=mPos-Game::game()->mCamera.pos;
26  glTranslated(movePos.x, movePos.y, 0);//move to position
27  glRotatef(mAngle,0.0f,0.0f,1.0f);//rotate shape
28  glScaled(mScale, mScale, mScale);
29  glBindTexture(GL_TEXTURE_2D, mBackground.image);
30  glBegin( GL_QUADS );
31  // Top-left vertex (corner)
32  glTexCoord2f( 0, 0 );
33  glVertex3d( 0, 0, 0 );
34 
35  // Bottom-left vertex (corner)
36  glTexCoord2f( 1, 0 );
37  glVertex3d( width, 0, 0 );
38 
39  // Bottom-right vertex (corner)
40  glTexCoord2f( 1, 1 );
41  glVertex3d( width, height, 0 );
42 
43  // Top-right vertex (corner)
44  glTexCoord2f( 0, 1 );
45  glVertex3d( 0, height, 0 );
46  glEnd();
47  glLoadIdentity();
48  //revert to default texture
49  glBindTexture(GL_TEXTURE_2D, 0);
50 }
51 
53  if(mUnused) return;
55 }
56 
57 
58 int Background::load(std::string filename) {
60  SDL_Surface *temp=NULL;
61  if((temp = LoadImage("Backgrounds/"+filename)) == NULL) return -1;//Load image for Frame
62  cout<<"Loaded file"<<endl;
63 
65  //SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, transparency[0], transparency[1], transparency[2]));
66 
68  SDL_Surface* surface = SDL_DisplayFormatAlpha(temp);
69  SDL_FreeSurface(temp);
70  width=surface->w;
71  height=surface->h;
72 
73  // Check that the image’s width is a power of 2
74  if ( (surface->w & (surface->w - 1)) != 0 ) {
75  cout<<"warning: "<<filename<<"'s width is not a power of 2"<<endl;
76  }
77 
78  // Also check if the height is a power of 2
79  if ( (surface->h & (surface->h - 1)) != 0 ) {
80  cout<<"warning: "<<filename<<"'s height is not a power of 2"<<endl;
81  }
82 
83  //get number of channels in the SDL surface
84  GLint nofcolors=surface->format->BytesPerPixel;
85 
86  GLenum texture_format=0;
87 
88  //contains an alpha channel
89  if(surface->format->Rmask==0x000000ff)
90  texture_format=GL_RGBA;
91  else
92  texture_format=GL_BGRA;
93  if(!(nofcolors==4 || nofcolors==3))
94  {
95  cout<<"warning: the image is not truecolor...this will break "<<endl;
96  }
97 
98  // allocate a texture name
99  glGenTextures( 1, &mBackground.image );
100 
101  // select our current texture
102  glBindTexture( GL_TEXTURE_2D, mBackground.image );
103 
104  // select modulate to mix texture with color for shading
105  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
106 
107  // when texture area is small, bilinear filter the closest mipmap
108  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
109  // when texture area is large, bilinear filter the original
110  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
111 
112  // the texture wraps over at the edges (repeat)
113  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
114  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
115 
116  glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0,
117  texture_format, GL_UNSIGNED_BYTE, surface->pixels );
118 
119  SDL_FreeSurface(surface);
120  return 0;
121 }
122 
123 void Background::setWrap(bool tf) {
124  wrapping=tf;
125 }
126 
128  return wrapping;
129 }
130 
133 }
134