#include "Collision.h"
-#include <SDL/SDL_draw.h>
#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;///<The relative x,y pos
+ int midpt = 1-radius;///<the midpt of the circle
+ Slock(Game::game()->Screen());
+ //draw pts
+ DrawCircle(Game::game()->Screen(), pos + mPos, color);
+ //calculate other points
+ while(x<y){
+ x++;
+ if(midpt<0)
+ midpt += 2 * x + 1;
+ else{
+ y--;
+ midpt += 2 * (x-y) + 1;
+ }
+ //draw pts
+ DrawCircle(Game::game()->Screen(), pos + mPos, color);
+ }
+ Sulock(Game::game()->Screen());
}
bool CollisionRectangle::collision(const Collision *c){
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*/
double w;/**< x position */
double h;/**< y Position */
};
-
-
//template<class T> string to_string(const T& t);
#endif
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