10 std::list<SDL_Surface*> Graphics::allTextures = std::list<SDL_Surface*>();
11 Screen * Graphics::screen = NULL;
13 int Graphics::screenWidth = 0;
14 int Graphics::screenHeight = 0;
15 bool Graphics::initialised = false;
19 Texture::Texture(const char * filename, bool newDrawCentred) : surface(NULL), texture(0), drawCentred(newDrawCentred)
22 printf("Texture::Texture - loading \"%s\".\n", filename);
25 surface = Graphics::LoadTextureBMP(filename);
28 fprintf(stderr, "Texture::Texture - Could not open texture from file \"%s\"! ABORT\n", filename);
32 GLenum texture_format;
33 GLint nOfColours = surface->format->BytesPerPixel;
36 case 4: //contains alpha
37 texture_format = (surface->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA;
39 case 3: //does not contain alpha
40 texture_format = (surface->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR;
43 fprintf(stderr,"Texture::Texture - Could not understand SDL_Surface format (%d colours)! ABORT\n", nOfColours);
48 glGenTextures(1, &texture);
49 glBindTexture(GL_TEXTURE_2D, texture);
50 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
51 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
52 glTexImage2D(GL_TEXTURE_2D, 0, nOfColours, surface->w, surface->h,0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
59 printf("Texture::~Texture - %p has been deleted. glDeleteTexture and SDL_FreeSurface here.\n", (void*)(this));
61 glDeleteTextures(1, &texture);
62 //SDL_FreeSurface(surface);
65 void Texture::DrawColour(int x, int y, double angle, double scale, Colour colour)
67 if (scale > surface->w || scale > surface->h)
69 Graphics::DrawPixel(x/scale,y/scale,colour);
73 glColor3f(colour.r,colour.g,colour.b);
74 Draw(x,y,angle,scale);
79 void Texture::Draw(int x, int y, double angle , double scale )
81 //Draws the CENTRE of the texture at x, y, rotated by angle
84 printf(" Texture::Draw - Drawing %p at (%d, %d) ; angle %2f ; scale % 2f\n", (void*)(this), x, y, angle, scale);
87 //if (x/scale < 0 || x/scale > Graphics::ScreenWidth() || y/scale < 0 || y/scale > Graphics::ScreenHeight() )
90 glPushMatrix(); //NOT deprecated
93 glTranslatef(x/scale, y/scale,0);
95 if (scale > surface->w || scale > surface->h)
97 Graphics::DrawPixel(0,0, Colour(255,255,255));
101 glRotated(angle, 0, 0, 1);
102 glEnable(GL_TEXTURE_2D);
103 glBindTexture(GL_TEXTURE_2D, texture);
109 glTexCoord2i(0,0); glVertex3f(-0.5f/scale*surface->w ,-0.5f/scale*surface->h,0); //bottom left
110 glTexCoord2i(1,0); glVertex3f(0.5f/scale*surface->w,-0.5f/scale*surface->h,0); //bottom right
111 glTexCoord2i(1,1); glVertex3f(0.5f/scale*surface->w,0.5f/scale*surface->h,0); //top right
112 glTexCoord2i(0,1); glVertex3f(-0.5f/scale*surface->w,0.5f/scale*surface->h,0); //top left
116 glTexCoord2i(0,0); glVertex3f(0 ,0,0); //bottom left
117 glTexCoord2i(1,0); glVertex3f(1.0f/scale*surface->w,0,0); //bottom right
118 glTexCoord2i(1,1); glVertex3f(1.0f/scale*surface->w,1.0f/scale*surface->h,0); //top right
119 glTexCoord2i(0,1); glVertex3f(0,1.0f/scale*surface->h,0); //top left
123 glDisable(GL_TEXTURE_2D);
130 Font::Font(const char * filename, int newWidth, int newHeight) : Texture(filename), width(newWidth), height(newHeight)
140 void Font::DrawText(const char * string, int x, int y, double angle, double scale)
143 printf("Font::DrawText - drawing \"%s\"\n", string);
145 glPushMatrix(); //NOT deprecated
146 glTranslatef(x, y,0);
147 glRotated(angle, 0, 0, 1);
149 glEnable(GL_TEXTURE_2D);
150 glBindTexture(GL_TEXTURE_2D, texture);
153 for (int ii=0; string[ii] != '\0'; ++ii)
155 if (string[ii] != ' ')
158 glTranslatef(ii*(float)(width)/(float)(scale),0,0);
160 int index = (int)(string[ii]) - (int)('!');
161 if (index < 0 || index > (int)('~') - (int)('!'))
162 index = (int)('~') - (int)('!') + 1;
164 float start = (float)(((((float)(index))*((float)(width)))-3.0f)/((float)surface->w));
165 float end = (float)(((((float)(index+1))*((float)(width)))-4.0f)/((float)surface->w));
166 if (start < 0) {start = 0;} if (end > 1) {end = 1;}
168 glTexCoord2f(start,0); glVertex3f(-0.5f/scale*width ,-0.5f/scale*height,0); //bottom left
169 glTexCoord2f(end,0); glVertex3f(0.5f/scale*width,-0.5f/scale*height,0); //bottom right
170 glTexCoord2f(end,1); glVertex3f(0.5f/scale*width,0.5f/scale*height,0); //top right
171 glTexCoord2f(start,1); glVertex3f(-0.5f/scale*width,0.5f/scale*height,0); //top left
172 //printf("Index %d - Drawing %c - maps to %f->%f\n", index,string[ii],start,end);
180 glDisable(GL_TEXTURE_2D);
186 void Graphics::Initialise(const char * caption, int newWidth, int newHeight)
190 std::cerr << "Graphics have already been initialised! Fatal Error\n";
193 screenWidth = newWidth; screenHeight = newHeight;
195 if (SDL_Init(SDL_INIT_VIDEO) != 0)
197 std::cerr << "Couldn't init SDL!\n";
200 // atexit(Graphics::Destroy); BREAKS THINGS
202 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //According to sulix does not matter. (much)
207 screen = SDL_SetVideoMode(screenWidth,screenHeight, 32, SDL_OPENGL);
208 if ( screen == NULL )
210 std::cerr << "Couldn't set " << screenWidth << "x" << screenHeight << "x32 video mode: " << SDL_GetError() << "\n";
214 //COMES AFTER SETVIDEO MODE
215 glEnable(GL_TEXTURE_2D);
216 glClearColor(1,1,1,0); //Set clear colour (white) here
217 glViewport(0,0,screenWidth,screenHeight); //DOES matter
218 glClear(GL_COLOR_BUFFER_BIT);
219 glMatrixMode(GL_PROJECTION);
221 glOrtho(0,screenWidth,screenHeight,0,-1,1);
222 glMatrixMode(GL_MODELVIEW);
224 glDisable(GL_DEPTH_TEST);
225 SDL_WM_SetCaption( caption, NULL);
227 Graphics::initialised = true;
231 void Graphics::Destroy()
233 list<SDL_Surface*>::iterator i(allTextures.begin());
234 while (i != allTextures.end())
236 SDL_FreeSurface((*i));
242 SDL_Surface * Graphics::LoadTextureBMP(const char * file)
244 SDL_Surface * tmp = SDL_LoadBMP(file);
247 //assert(tmp != NULL);
250 if (Graphics::screen != NULL)
252 SDL_Surface * tex = SDL_DisplayFormat(tmp);
253 SDL_FreeSurface(tmp);
255 allTextures.push_back(tex);
261 void Graphics::SaveTextureBMP(SDL_Surface * tex, const char * file)
263 SDL_SaveBMP(tex, file);
267 void Graphics::DrawTexture(SDL_Surface * tex, int destX, int destY, int srcX, int srcY, int w, int h)
269 if (w < 0) {w = tex->w - srcX;}
270 if (h < 0) {h = tex->h - srcY;}
271 Graphics::DrawTexture(screen, tex, destX, destY, srcX, srcY, w, h);
274 void Graphics::DrawTexture(SDL_Surface * dest, SDL_Surface * tex, int destX, int destY, int srcX, int srcY, int width, int height)
276 if ((destX < 0)||(destX >= dest->w)||(destY < 0)||(destY >= dest->h)
277 ||(srcX < 0)||(srcX >= tex->w)||(srcY < 0)||(srcY >= tex->h))
280 assert(dest->format->BitsPerPixel == 32);
281 assert(tex->format->BitsPerPixel == 32);
283 if (SDL_MUSTLOCK(tex))
284 SDL_LockSurface(tex);
286 if (SDL_MUSTLOCK(dest))
287 SDL_LockSurface(dest);
291 Colour transparent = Graphics::GetPixel(tex, srcX, srcY);
292 //printf("transparent from %d %d\n", srcX, srcY);
294 for (int xOff = 0; xOff < width; xOff++)
297 for (int yOff = 0; yOff < height; yOff++)
299 Colour nextColour = Graphics::GetPixel(tex, srcX+xOff, srcY+yOff);
300 if (nextColour != transparent)
302 Graphics::DrawPixel(dest, destX + xOff, destY + yOff, nextColour);
307 if (SDL_MUSTLOCK(tex))
308 SDL_UnlockSurface(tex);
310 if (SDL_MUSTLOCK(dest))
311 SDL_UnlockSurface(dest);
315 void Graphics::ClearScreen()
317 //SDL_FillRect(screen, NULL ,Graphics::MakeColour(0,0,0));
318 glClear(GL_COLOR_BUFFER_BIT);
322 void Graphics::UpdateScreen()
324 SDL_GL_SwapBuffers();
328 void Graphics::DrawPixel(int x, int y, Colour colour)
330 DrawPixel(screen, x, y, colour);
333 void Graphics::DrawPixel(SDL_Surface * dest, int x, int y, Colour colour)
336 glColor4f(colour.r/255, colour.g/255, colour.b/255, colour.a);
342 void Graphics::DrawGrid(int gridWidth, int gridHeight, Colour colour)
344 for (int x = 0; x < screen->w; x+=gridWidth)
346 Graphics::DrawLine(x,0, x,screen->h - 1, colour);
348 for (int y = 0; y < screen->h; y+=gridHeight)
350 Graphics::DrawLine(0,y, screen->w - 1,y, colour);
354 Uint8 Graphics::MakeColour(int R, int G, int B, int Alpha)
356 return SDL_MapRGB(screen->format,R,G,B);
359 Colour Graphics::GetPixel(int x, int y)
361 return Graphics::GetPixel(screen, x, y);
364 Colour Graphics::GetPixel(SDL_Surface * src, int x, int y)
366 //Convert the pixels to 32 bit
367 Uint8 * pixels = (Uint8*)src->pixels;
368 //Get the requested pixel
370 if (((y > 0)&&(y < src->h)) && ((x > 0)&&(x < src->w)))
371 return ConvertColour(pixels[ ( y * src->w ) + x ]);
372 return Colour(0,0,0,0);
378 void Graphics::DrawLine(int x1, int y1, int x2, int y2, Colour colour,double scale)
380 //printf("DRAW LINE\n");
381 glColor4f(colour.r/255,colour.g/255,colour.b/255,colour.a);
383 glVertex2f(x1/scale, y1/scale); // origin of the line
384 glVertex2f(x2/scale, y2/scale); // ending point of the line
391 void Graphics::DrawLineDashed(int x1, int y1, int x2, int y2, Colour colour, double scale)
393 glLineStipple(8, 0xAAAA);
394 glEnable(GL_LINE_STIPPLE);
395 DrawLine(x1,y1,x2,y2,colour,scale);
396 glDisable(GL_LINE_STIPPLE);
400 void Graphics::DrawRectangle(int topX, int topY, int botX, int botY, Colour colour, double scale)
402 glColor4f(colour.r/255,colour.g/255,colour.b/255,colour.a);
404 glVertex2f(topX/scale, topY/scale); // origin of the rectangle
405 glVertex2f(botX/scale, topY/scale); // point1
406 glVertex2f(botX/scale, botY/scale); // point2
407 glVertex2f(topX/scale, botY/scale); // point3
408 glVertex2f(topX/scale, topY/scale); // point4
412 Colour Graphics::ConvertColour(Uint8 from)
414 SDL_PixelFormat * fmt=screen->format;
420 temp=from&fmt->Rmask; /* Isolate red component */
421 temp=temp>>fmt->Rshift;/* Shift it down to 8-bit */
422 temp=temp<<fmt->Rloss; /* Expand to a full 8-bit number */
423 result.r = (float)(temp);
426 temp=from&fmt->Gmask; /* Isolate red component */
427 temp=temp>>fmt->Gshift;/* Shift it down to 8-bit */
428 temp=temp<<fmt->Gloss; /* Expand to a full 8-bit number */
429 result.g = (float)(temp);
432 temp=from&fmt->Bmask; /* Isolate red component */
433 temp=temp>>fmt->Bshift;/* Shift it down to 8-bit */
434 temp=temp<<fmt->Bloss; /* Expand to a full 8-bit number */
435 result.b = (float)(temp);
438 temp=from&fmt->Amask; /* Isolate red component */
439 temp=temp>>fmt->Ashift;/* Shift it down to 8-bit */
440 temp=temp<<fmt->Aloss; /* Expand to a full 8-bit number */
441 result.a = (float)(temp);