From: Ivan Hernandez Date: Thu, 27 Jan 2011 00:38:34 +0000 (-0600) Subject: added Drawing for collision boxes and circles. Moved Slock / Sulock to X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=17ed2660ced7a9f4d0fa22153a8229b5afbcd1ea;p=IvanGame.git added Drawing for collision boxes and circles. Moved Slock / Sulock to area where drawing took place. --- diff --git a/Collision.cpp b/Collision.cpp index 72ae4bd..53776df 100644 --- a/Collision.cpp +++ b/Collision.cpp @@ -1,13 +1,158 @@ #include "Collision.h" -#include #include "Game.h" -void CollisionRectangle::draw(Point2D& pos){ - Draw_Rect(Game::game()->Screen(), pos.x + mPos.x, pos.y + mPos.y, width, height, color); +//locks the screen for drawing pixels to the screen +void Slock(SDL_Surface *screen) +{ + if ( SDL_MUSTLOCK(screen) ) + { + if ( SDL_LockSurface(screen) < 0 ) + { + return; + } + } +} + +void Sulock(SDL_Surface *screen) +{ + if ( SDL_MUSTLOCK(screen) ) + { + SDL_UnlockSurface(screen); + } +} + +void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color) +{ + switch (screen->format->BytesPerPixel) + { + case 1: // Assuming 8-bpp + { + Uint8 *bufp; + bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; + *bufp = color; + } + break; + case 2: // Probably 15-bpp or 16-bpp + { + Uint16 *bufp; + bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; + *bufp = color; + } + break; + case 3: // Slow 24-bpp mode, usually not used + { + Uint8 *bufp; + bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; + if(SDL_BYTEORDER == SDL_LIL_ENDIAN) + { + bufp[0] = color; + bufp[1] = color >> 8; + bufp[2] = color >> 16; + } else { + bufp[2] = color; + bufp[1] = color >> 8; + bufp[0] = color >> 16; + } + } + break; + case 4: // Probably 32-bpp + { + Uint32 *bufp; + bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; + *bufp = color; + } + break; + } +} + +/** + This function is tasked with drawing the 8 points on a circle so that only 1/8th need be calculated. + \param screen the Screen + \param pos This is the position to draw the points (not & because may want to have an added value pushed in) + \param color the color to be drawn +*/ +void DrawCircle(SDL_Surface* screen, const Point2D pos, Uint32 color){ + DrawPixel(screen, pos.x, pos.y, color); + DrawPixel(screen, -pos.x, pos.y, color); + DrawPixel(screen, pos.x, -pos.y, color); + DrawPixel(screen, -pos.x, -pos.y, color); + DrawPixel(screen, pos.y, pos.x, color); + DrawPixel(screen, -pos.y, pos.x, color); + DrawPixel(screen, pos.y, -pos.x, color); + DrawPixel(screen, -pos.y, -pos.x, color); +} +/** + This function is tasked with drawing lines by the midpoint method. + \param screen the Screen + \param start This is the start position to draw the point (not & because may want to have an added value pushed in) + \param end This is the end position to draw the point (not & because may want to have an added value pushed in) + \param color the color to be drawn +*/ +void DrawLine(SDL_Surface* screen, const Point2D start, Point2D end, Uint32 color){ + Slock(screen); + //values for calculation and max values + int x,y;//start as low vals used for algorithm + int xMax, yMax; + //if the end point is lower swap em + if(end.y < start.y){ + x = end.x; + y = end.y; + xMax = start.x; + yMax = start.y; + } + else { + x = end.x; + y = end.y; + xMax = start.x; + yMax = start.y; + } + //the change in x + int dX = xMax - x; + //change in y + int dY = y - yMax; + int sum = 2 * dY + dX; + DrawPixel(screen, x, y, color); + while( x < xMax){ + if(sum < 0){ + sum += 2 * dX; + y++; + } + x++; + sum += 2 * dY; + DrawPixel(screen, x, y, color); + } + Sulock(screen); +} + +void CollisionRectangle::draw(const Point2D& pos){ + //top + Point2D startPos = pos + mPos ; + Point2D endPos = pos + mPos ; + DrawLine(Game::game()->Screen(), startPos, endPos, color); + //bottom + //left + //right } void CollisionCircle::draw(const Point2D& pos){ - Draw_Circle(Game::game()->Screen(), pos.x + mPos.x, pos.y + mPos.y, radius, color); + int x=0,y=radius;///Screen()); + //draw pts + DrawCircle(Game::game()->Screen(), pos + mPos, color); + //calculate other points + while(xScreen(), pos + mPos, color); + } + Sulock(Game::game()->Screen()); } bool CollisionRectangle::collision(const Collision *c){ diff --git a/fns.h b/fns.h index 54bdbe3..1f04b80 100644 --- a/fns.h +++ b/fns.h @@ -38,10 +38,10 @@ class Point2D Point2D sub(Point2D pt);/**< Subtracts the value of the point from this \return This after modification*/ Point2D mult(double d);/**< Multiplies the values of this by i \return This after modificaiton*/ Point2D div(double d);/**< Divides the values of this by i \return This after modificaiton*/ - Point2D operator+ (Point2D& pt){ return Point2D(x + pt.x, y + pt.y); } - Point2D operator- (Point2D& pt){ return Point2D(x - pt.x, y - pt.y); } + Point2D operator+ (const Point2D& pt) const { return Point2D(x + pt.x, y + pt.y); } + Point2D operator- (const Point2D& pt) const { return Point2D(x - pt.x, y - pt.y); } Point2D operator+= (Point2D& pt){ return add(pt); } - Point2D operator-= (Point2D& pt){ sub(pt); } + Point2D operator-= (Point2D& pt){ return sub(pt); } }; /** Just for more logical names for sizes of objects*/ @@ -64,7 +64,5 @@ class SizeD double w;/**< x position */ double h;/**< y Position */ }; - - //template string to_string(const T& t); #endif diff --git a/main.cpp b/main.cpp index 39da4d7..69cd56a 100644 --- a/main.cpp +++ b/main.cpp @@ -16,70 +16,6 @@ template string to_string(const T& t) os << t; return os.str(); } -//locks the screen for drawing pixels to the screen -void Slock(SDL_Surface *screen) -{ - if ( SDL_MUSTLOCK(screen) ) - { - if ( SDL_LockSurface(screen) < 0 ) - { - return; - } - } -} - -void Sulock(SDL_Surface *screen) -{ - if ( SDL_MUSTLOCK(screen) ) - { - SDL_UnlockSurface(screen); - } -} - -void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) -{ -Uint32 color = SDL_MapRGB(screen->format, R, G, B); - switch (screen->format->BytesPerPixel) - { - case 1: // Assuming 8-bpp - { - Uint8 *bufp; - bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; - *bufp = color; - } - break; - case 2: // Probably 15-bpp or 16-bpp - { - Uint16 *bufp; - bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; - *bufp = color; - } - break; - case 3: // Slow 24-bpp mode, usually not used - { - Uint8 *bufp; - bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; - if(SDL_BYTEORDER == SDL_LIL_ENDIAN) - { - bufp[0] = color; - bufp[1] = color >> 8; - bufp[2] = color >> 16; - } else { - bufp[2] = color; - bufp[1] = color >> 8; - bufp[0] = color >> 16; - } - } - break; - case 4: // Probably 32-bpp - { - Uint32 *bufp; - bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; - *bufp = color; - } - break; - } -} //-------------------------------------------------------------------// // Function : main() - Params : argc, argv