}
}
-void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color)
+void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 c)
{
- 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;
- }
+ //keep in bounds
+ if(x>0 && y>0 && x < screen->w && y < screen->h){
+ Uint32 color = c&0xFFFFFF;
+ //Uint32 color1 = SDL_MapRGB(screen->format, c&0xff, c&0xFF00, c&0xFF0000);
+ 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;
+ }
+ }
+ }
}
/**
\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);
+void DrawCircle(SDL_Surface* screen, const Point2D wPos, const Point2D pos, Uint32 color){
+ DrawPixel(screen, wPos.x + pos.x, wPos.y + pos.y, color);
+ DrawPixel(screen, wPos.x - pos.x, wPos.y + pos.y, color);
+ DrawPixel(screen, wPos.x + pos.x, wPos.y - pos.y, color);
+ DrawPixel(screen, wPos.x - pos.x, wPos.y - pos.y, color);
+ DrawPixel(screen, wPos.x + pos.y, wPos.y + pos.x, color);
+ DrawPixel(screen, wPos.x - pos.y, wPos.y + pos.x, color);
+ DrawPixel(screen, wPos.x + pos.y, wPos.y - pos.x, color);
+ DrawPixel(screen, wPos.x - pos.y, wPos.y - pos.x, color);
}
/**
This function is tasked with drawing lines by the midpoint method.
//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;
yMax = start.y;
}
else {
- x = end.x;
- y = end.y;
- xMax = start.x;
- yMax = start.y;
+ x = start.x;
+ y = start.y;
+ xMax = end.x;
+ yMax = end.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;
+
+ //if divide by 0 draw a vertical line
+ if(dX) {
+ //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);
+ }
+ }
+ else {
+ while(y < yMax){
+ DrawPixel(screen, x, y, color);
y++;
}
- x++;
- sum += 2 * dY;
- DrawPixel(screen, x, y, color);
}
Sulock(screen);
}
void CollisionRectangle::draw(const Point2D& pos){
+ Point2D nPos = pos + mPos;
//top
- Point2D startPos = pos + mPos ;
- Point2D endPos = pos + mPos ;
+ Point2D startPos = nPos + Point2D(-width/2, -height/2);
+ Point2D endPos = nPos + Point2D(width/2, -height/2);
DrawLine(Game::game()->Screen(), startPos, endPos, color);
//bottom
+ startPos = nPos + Point2D(-width/2, height/2);
+ endPos = nPos + Point2D(width/2, height/2);
+ DrawLine(Game::game()->Screen(), startPos, endPos, color);
//left
+ startPos = nPos + Point2D(-width/2, -height/2);
+ endPos = nPos + Point2D(-width/2, height/2);
+ DrawLine(Game::game()->Screen(), startPos, endPos, color);
//right
+ startPos = nPos + Point2D(width/2, -height/2);
+ endPos = nPos + Point2D(width/2, height/2);
+ DrawLine(Game::game()->Screen(), startPos, endPos, color);
}
void CollisionCircle::draw(const Point2D& pos){
+ Slock(Game::game()->Screen());
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);
+ DrawCircle(Game::game()->Screen(), pos + mPos, Point2D(x,y), color);
//calculate other points
while(x<y){
x++;
midpt += 2 * (x-y) + 1;
}
//draw pts
- DrawCircle(Game::game()->Screen(), pos + mPos, color);
+ DrawCircle(Game::game()->Screen(), pos + mPos, Point2D(x,y), color);
}
Sulock(Game::game()->Screen());
}