11 std::list<SDL_Surface*> Graphics::allTextures = std::list<SDL_Surface*>();
12 Screen * Graphics::screen = NULL;
14 int Graphics::screenWidth = 0;
15 int Graphics::screenHeight = 0;
16 bool Graphics::initialised = false;
20 Texture::Texture(const char * filename, bool newDrawCentred) : surface(NULL), texture(0), drawCentred(newDrawCentred)
23 printf("Texture::Texture - loading \"%s\".\n", filename);
26 surface = Graphics::LoadTextureBMP(filename);
29 fprintf(stderr, "Texture::Texture - Could not open texture from file \"%s\"! ABORT\n", filename);
33 GLenum texture_format;
34 GLint nOfColours = surface->format->BytesPerPixel;
37 case 4: //contains alpha
38 texture_format = (surface->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
40 case 3: //does not contain alpha
41 texture_format = (surface->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;
44 fprintf(stderr,"Texture::Texture - Could not understand SDL_Surface format (%d colours)! ABORT\n", nOfColours);
49 glGenTextures(1, &texture);
50 glBindTexture(GL_TEXTURE_2D, texture);
51 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
52 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
53 glTexImage2D(GL_TEXTURE_2D, 0, nOfColours, surface->w, surface->h,0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
60 printf("Texture::~Texture - %p has been deleted. glDeleteTexture and SDL_FreeSurface here.\n", (void*)(this));
62 glDeleteTextures(1, &texture);
63 //SDL_FreeSurface(surface);
66 void Texture::DrawColour(int x, int y, double angle, double scale, Colour colour)
68 if (scale > surface->w || scale > surface->h)
70 Graphics::DrawPixel(x/scale,y/scale,colour);
74 glColor3f(colour.r,colour.g,colour.b);
75 Draw(x,y,angle,scale);
80 void Texture::Draw(int x, int y, double angle , double scale )
82 //Draws the CENTRE of the texture at x, y, rotated by angle
85 printf(" Texture::Draw - Drawing %p at (%d, %d) ; angle %2f ; scale % 2f\n", (void*)(this), x, y, angle, scale);
88 //if (x/scale < 0 || x/scale > Graphics::ScreenWidth() || y/scale < 0 || y/scale > Graphics::ScreenHeight() )
91 glPushMatrix(); //NOT deprecated
94 glTranslatef(x/scale, y/scale,0);
96 if (scale > surface->w || scale > surface->h)
98 Graphics::DrawPixel(0,0, Colour(255,255,255));
102 glRotated(angle, 0, 0, 1);
103 glEnable(GL_TEXTURE_2D);
104 glBindTexture(GL_TEXTURE_2D, texture);
110 glTexCoord2i(0,0); glVertex3f(-0.5f/scale*surface->w ,-0.5f/scale*surface->h,0); //bottom left
111 glTexCoord2i(1,0); glVertex3f(0.5f/scale*surface->w,-0.5f/scale*surface->h,0); //bottom right
112 glTexCoord2i(1,1); glVertex3f(0.5f/scale*surface->w,0.5f/scale*surface->h,0); //top right
113 glTexCoord2i(0,1); glVertex3f(-0.5f/scale*surface->w,0.5f/scale*surface->h,0); //top left
117 glTexCoord2i(0,0); glVertex3f(0 ,0,0); //bottom left
118 glTexCoord2i(1,0); glVertex3f(1.0f/scale*surface->w,0,0); //bottom right
119 glTexCoord2i(1,1); glVertex3f(1.0f/scale*surface->w,1.0f/scale*surface->h,0); //top right
120 glTexCoord2i(0,1); glVertex3f(0,1.0f/scale*surface->h,0); //top left
124 glDisable(GL_TEXTURE_2D);
132 Font::Font(const char * filename, int newWidth, int newHeight) : Texture(filename), width(newWidth), height(newHeight)
142 void Font::DrawText(const char * string, int x, int y, double angle, double scale)
145 printf("Font::DrawText - drawing \"%s\"\n", string);
147 glPushMatrix(); //NOT deprecated
148 glTranslatef(x, y,0);
149 glRotated(angle, 0, 0, 1);
151 glEnable(GL_TEXTURE_2D);
152 glBindTexture(GL_TEXTURE_2D, texture);
155 for (int ii=0; string[ii] != '\0'; ++ii)
157 if (string[ii] != ' ')
160 glTranslatef(ii*(float)(width)/(float)(scale),0,0);
162 int index = (int)(string[ii]) - (int)('!');
163 if (index < 0 || index > (int)('~') - (int)('!'))
164 index = (int)('~') - (int)('!') + 1;
166 float start = (float)(((((float)(index))*((float)(width)))-3.0f)/((float)surface->w));
167 float end = (float)(((((float)(index+1))*((float)(width)))-4.0f)/((float)surface->w));
168 if (start < 0) {start = 0;} if (end > 1) {end = 1;}
170 glTexCoord2f(start,0); glVertex3f(-0.5f/scale*width ,-0.5f/scale*height,0); //bottom left
171 glTexCoord2f(end,0); glVertex3f(0.5f/scale*width,-0.5f/scale*height,0); //bottom right
172 glTexCoord2f(end,1); glVertex3f(0.5f/scale*width,0.5f/scale*height,0); //top right
173 glTexCoord2f(start,1); glVertex3f(-0.5f/scale*width,0.5f/scale*height,0); //top left
174 //printf("Index %d - Drawing %c - maps to %f->%f\n", index,string[ii],start,end);
182 glDisable(GL_TEXTURE_2D);
188 void Graphics::Initialise(const char * caption, int newWidth, int newHeight)
192 std::cerr << "Graphics have already been initialised! Fatal Error\n";
195 screenWidth = newWidth; screenHeight = newHeight;
197 if (SDL_Init(SDL_INIT_VIDEO) != 0)
199 std::cerr << "Couldn't init SDL!\n";
202 // atexit(Graphics::Destroy); BREAKS THINGS
204 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //According to sulix does not matter. (much)
209 screen = SDL_SetVideoMode(screenWidth,screenHeight, 32, SDL_OPENGL);
210 if ( screen == NULL )
212 std::cerr << "Couldn't set " << screenWidth << "x" << screenHeight << "x32 video mode: " << SDL_GetError() << "\n";
216 //COMES AFTER SETVIDEO MODE
217 glEnable(GL_TEXTURE_2D);
218 glClearColor(1,1,1,0); //Set clear colour (white) here
219 glViewport(0,0,screenWidth,screenHeight); //DOES matter
220 glClear(GL_COLOR_BUFFER_BIT);
221 glMatrixMode(GL_PROJECTION);
223 glOrtho(0,screenWidth,screenHeight,0,-1,1);
224 glMatrixMode(GL_MODELVIEW);
226 glDisable(GL_DEPTH_TEST);
227 SDL_WM_SetCaption( caption, NULL);
229 Graphics::initialised = true;
233 void Graphics::Destroy()
235 list<SDL_Surface*>::iterator i(allTextures.begin());
236 while (i != allTextures.end())
238 SDL_FreeSurface((*i));
244 SDL_Surface * Graphics::LoadTextureBMP(const char * file)
246 SDL_Surface * tmp = SDL_LoadBMP(file);
249 //assert(tmp != NULL);
252 if (Graphics::screen != NULL)
254 SDL_Surface * tex = SDL_DisplayFormat(tmp);
255 SDL_FreeSurface(tmp);
257 allTextures.push_back(tex);
263 void Graphics::SaveTextureBMP(SDL_Surface * tex, const char * file)
265 SDL_SaveBMP(tex, file);
269 void Graphics::DrawTexture(SDL_Surface * tex, int destX, int destY, int srcX, int srcY, int w, int h)
271 if (w < 0) {w = tex->w - srcX;}
272 if (h < 0) {h = tex->h - srcY;}
273 Graphics::DrawTexture(screen, tex, destX, destY, srcX, srcY, w, h);
276 void Graphics::DrawTexture(SDL_Surface * dest, SDL_Surface * tex, int destX, int destY, int srcX, int srcY, int width, int height)
278 if ((destX < 0)||(destX >= dest->w)||(destY < 0)||(destY >= dest->h)
279 ||(srcX < 0)||(srcX >= tex->w)||(srcY < 0)||(srcY >= tex->h))
282 assert(dest->format->BitsPerPixel == 32);
283 assert(tex->format->BitsPerPixel == 32);
285 if (SDL_MUSTLOCK(tex))
286 SDL_LockSurface(tex);
288 if (SDL_MUSTLOCK(dest))
289 SDL_LockSurface(dest);
293 Colour transparent = Graphics::GetPixel(tex, srcX, srcY);
294 //printf("transparent from %d %d\n", srcX, srcY);
296 for (int xOff = 0; xOff < width; xOff++)
299 for (int yOff = 0; yOff < height; yOff++)
301 Colour nextColour = Graphics::GetPixel(tex, srcX+xOff, srcY+yOff);
302 if (nextColour != transparent)
304 Graphics::DrawPixel(dest, destX + xOff, destY + yOff, nextColour);
309 if (SDL_MUSTLOCK(tex))
310 SDL_UnlockSurface(tex);
312 if (SDL_MUSTLOCK(dest))
313 SDL_UnlockSurface(dest);
317 void Graphics::ClearScreen()
319 //SDL_FillRect(screen, NULL ,Graphics::MakeColour(0,0,0));
320 glClear(GL_COLOR_BUFFER_BIT);
324 void Graphics::UpdateScreen()
326 SDL_GL_SwapBuffers();
330 void Graphics::DrawPixel(int x, int y, Colour colour)
332 DrawPixel(screen, x, y, colour);
335 void Graphics::DrawPixel(SDL_Surface * dest, int x, int y, Colour colour)
338 glColor4f(colour.r/255, colour.g/255, colour.b/255, colour.a);
344 void Graphics::DrawGrid(int gridWidth, int gridHeight, Colour colour)
346 for (int x = 0; x < screen->w; x+=gridWidth)
348 Graphics::DrawLine(x,0, x,screen->h - 1, colour);
350 for (int y = 0; y < screen->h; y+=gridHeight)
352 Graphics::DrawLine(0,y, screen->w - 1,y, colour);
356 Uint8 Graphics::MakeColour(int R, int G, int B, int Alpha)
358 return SDL_MapRGB(screen->format,R,G,B);
361 Colour Graphics::GetPixel(int x, int y)
363 return Graphics::GetPixel(screen, x, y);
366 Colour Graphics::GetPixel(SDL_Surface * src, int x, int y)
368 //Convert the pixels to 32 bit
369 Uint8 * pixels = (Uint8*)src->pixels;
370 //Get the requested pixel
372 if (((y > 0)&&(y < src->h)) && ((x > 0)&&(x < src->w)))
373 return ConvertColour(pixels[ ( y * src->w ) + x ]);
374 return Colour(0,0,0,0);
380 void Graphics::DrawLine(int x1, int y1, int x2, int y2, Colour colour,double scale)
382 //printf("DRAW LINE\n");
383 glColor4f(colour.r/255,colour.g/255,colour.b/255,colour.a);
385 glVertex2f(x1/scale, y1/scale); // origin of the line
386 glVertex2f(x2/scale, y2/scale); // ending point of the line
393 void Graphics::DrawLineDashed(int x1, int y1, int x2, int y2, Colour colour, double scale)
395 glLineStipple(8, 0xAAAA);
396 glEnable(GL_LINE_STIPPLE);
397 DrawLine(x1,y1,x2,y2,colour,scale);
398 glDisable(GL_LINE_STIPPLE);
402 void Graphics::DrawRectangle(int topX, int topY, int botX, int botY, Colour colour, double scale)
404 glColor4f(colour.r/255,colour.g/255,colour.b/255,colour.a);
406 glVertex2f(topX/scale, topY/scale); // origin of the rectangle
407 glVertex2f(botX/scale, topY/scale); // point1
408 glVertex2f(botX/scale, botY/scale); // point2
409 glVertex2f(topX/scale, botY/scale); // point3
410 glVertex2f(topX/scale, topY/scale); // point4
414 Colour Graphics::ConvertColour(Uint8 from)
416 SDL_PixelFormat * fmt=screen->format;
422 temp=from&fmt->Rmask; /* Isolate red component */
423 temp=temp>>fmt->Rshift;/* Shift it down to 8-bit */
424 temp=temp<<fmt->Rloss; /* Expand to a full 8-bit number */
425 result.r = (float)(temp);
428 temp=from&fmt->Gmask; /* Isolate red component */
429 temp=temp>>fmt->Gshift;/* Shift it down to 8-bit */
430 temp=temp<<fmt->Gloss; /* Expand to a full 8-bit number */
431 result.g = (float)(temp);
434 temp=from&fmt->Bmask; /* Isolate red component */
435 temp=temp>>fmt->Bshift;/* Shift it down to 8-bit */
436 temp=temp<<fmt->Bloss; /* Expand to a full 8-bit number */
437 result.b = (float)(temp);
440 temp=from&fmt->Amask; /* Isolate red component */
441 temp=temp>>fmt->Ashift;/* Shift it down to 8-bit */
442 temp=temp<<fmt->Aloss; /* Expand to a full 8-bit number */
443 result.a = (float)(temp);
447 void Graphics::Wait(int n)
452 #endif //BUILD_GRAPHICS