[Add] Added AABB rendering.

This commit is contained in:
Tamir Atias 2012-04-12 20:05:05 +03:00
parent 7aa8d9adb2
commit a140cccd04
2 changed files with 57 additions and 29 deletions

View File

@ -1,10 +1,12 @@
#include <SDL/SDL.h>
#include <SDL/SDL_image.h> #include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include "AABB.h" #include "AABB.h"
#include "../Texture/Texture.h"
typedef Uint32 DWORD;
AABB::AABB(void) { AABB::AABB(void) {
_sprite = 0; _surface = NULL;
_texture = NULL;
// _max represents the top left. // _max represents the top left.
_max = Vec2(0,0); _max = Vec2(0,0);
// _min is the bottom left. // _min is the bottom left.
@ -21,9 +23,13 @@ AABB::AABB(Vec2 &min, Vec2 &max) {
} }
AABB::~AABB(void) { AABB::~AABB(void) {
if(_sprite) { if(_surface) {
SDL_FreeSurface(_sprite); SDL_FreeSurface(_surface);
_sprite = NULL; _surface = NULL;
}
if(_texture) {
textureManager.Destroy(_texture);
_texture = NULL;
} }
} }
@ -55,54 +61,61 @@ bool AABB::InCollision(AABB* otherAABB) {
void AABB::CreateAABBFromSprite(const char* filename) { void AABB::CreateAABBFromSprite(const char* filename) {
std::string tempName(filename); std::string tempName(filename);
tempName += ".png"; tempName += ".png";
_sprite = IMG_Load(filename);
if(!_sprite) { _surface = IMG_Load(filename);
if(!_surface) {
return;
}
_texture = textureManager.Load(filename);
if(_texture) {
SDL_FreeSurface(_surface);
return; return;
} }
// I have no methods here, hopefully KonoM will have it // I have no methods here, hopefully KonoM will have it
// implemented real soon... // implemented real soon...
//float spriteWidth = _sprite->w; //float spriteWidth = _surface->w;
//float spriteHeight = _sprite->h; //float spriteHeight = _surface->h;
// Find the min, look through until we find a first instance of a white color. // Find the min, look through until we find a first instance of a white color.
bool found = false; bool found = false;
int color = 0; int color = 0;
DWORD* pixels = (DWORD*)_sprite->pixels; Uint32* pixels = (Uint32*)_surface->pixels;
for(int width = 0; width < _sprite->w; width++) { for(int width = 0; width < _surface->w; width++) {
for(int height = 0; height < _sprite->h; height++) { for(int height = 0; height < _surface->h; height++) {
// FUCKING PAIN IN THE ASS MOTHERFUCKER!!!! // FUCKING PAIN IN THE ASS MOTHERFUCKER!!!!
DWORD offset = height * screen->pitch + width; Uint32 offset = height * screen->pitch + width;
if(((DWORD)pixels[offset]) != 0 && !found) { if(((DWORD)pixels[offset]) != 0 && !found) {
_min = Vec2((float)width, (float)height); _min = Vec2((float)width, (float)height);
found = true; found = true;
color = ((DWORD)pixels[offset]); color = ((Uint32)pixels[offset]);
// Break out of these god forsaken loops. // Break out of these god forsaken loops.
width = _sprite->w; width = _surface->w;
height = _sprite->h; height = _surface->h;
} }
} }
} }
// Let's try to find the max.x now.. // Let's try to find the max.x now..
_max.x = (float)_sprite->w; _max.x = (float)_surface->w;
found = false; found = false;
for(int width = (int)_min.x; width < _sprite->w; width++) { for(int width = (int)_min.x; width < _surface->w; width++) {
DWORD offset = (DWORD)_min.y * screen->pitch + width; Uint32 offset = (Uint32)_min.y * screen->pitch + width;
if(((DWORD)pixels[offset] != color && !found)) { if(((Uint32)pixels[offset] != color && !found)) {
found = true; found = true;
_max.x = (float)width; _max.x = (float)width;
} }
} }
// Now for the max.y // Now for the max.y
_max.y = (float)_sprite->h; _max.y = (float)_surface->h;
found = false; found = false;
for(int height = (int)_min.y; height < _sprite->w; height++) { for(int height = (int)_min.y; height < _surface->w; height++) {
DWORD offset = height * screen->pitch + (DWORD)_min.x; Uint32 offset = height * screen->pitch + (Uint32)_min.x;
if(((DWORD)pixels[offset]) != color && !found) { if(((Uint32)pixels[offset]) != color && !found) {
found = true; found = true;
_max.y = (float)height; _max.y = (float)height;
break; break;
@ -110,10 +123,23 @@ void AABB::CreateAABBFromSprite(const char* filename) {
} }
_staticMax = _max; _staticMax = _max;
_staticMin = _min; _staticMin = _min;
delete _sprite;
_sprite = 0; SDL_FreeSurface(_surface);
_surface = NULL;
} }
void AABB::Render(void) { void AABB::Render(void) {
// FUCK YOU KonoM!! glEnable(GL_TEXTURE_2D);
BindTexture(_texture->GetTexID());
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(_min.x, _min.y);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(_max.x, _min.y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(_max.x, _max.y);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(_min.x, _max.y);
glEnd();
} }

View File

@ -13,6 +13,7 @@
*/ */
struct SDL_Surface; struct SDL_Surface;
class Texture;
class AABB { class AABB {
public: public:
@ -42,5 +43,6 @@ private:
Vec2 _staticMin; Vec2 _staticMin;
Vec2 _staticMax; Vec2 _staticMax;
SDL_Surface* _sprite; SDL_Surface* _surface;
Texture* _texture;
}; };