void KImage::setPixel(const KPoint &p, const KColor &color) { if(surface == nullptr) { return; } if(p.x >= surface->w || p.y >= surface->h || p.x < 0 || p.y < 0) { return; } uint8_t *pixel = (uint8_t *)surface->pixels; pixel += p.y * surface->pitch; pixel += p.x * surface->format->BytesPerPixel; uint32_t *pixel32 = (uint32_t *)pixel; *pixel32 = colorToUInt(color); }
void KImage::fillRect(const KRect &rect, const KColor &color) { SDL_Rect r = KRectTOSDLRect(rect); if(surface != nullptr) { SDL_FillRect(surface, &r, colorToUInt(color)); } }
void KImage::drawLine(const KPoint &p1, const KPoint &p2, const KColor &color) { if(surface == nullptr) { return; } KPoint p = p1; int dx = abs(p2.x - p1.x); int dy = abs(p2.y - p1.y); int sx = p1.x < p2.x ? 1 : -1; int sy = p1.y < p2.y ? 1 : -1; int e = (dx > dy ? dx : -dy) / 2; int e2; int c = dx > dy ? dx : dy; if(p.x >= surface->w || p.y >= surface->h || p.x < 0 || p.y < 0) { // Line starts out of bounds of image. iterate till it's in bounds. while(c >= 0) { c--; e2 = e; if(e2 > -dx) { // Move horizontally. e -= dy; p.x += sx; } if(e2 < dy) { // Move vertically. e += dx; p.y += sy; } if(p.x < surface->w && p.y < surface->h && p.x >= 0 && p.y >= 0) { // We are now in bounds. break; } } } int bpp = surface->format->BytesPerPixel; int ptch = surface->pitch; uint8_t *pixel = (uint8_t *)surface->pixels; pixel += p.y * ptch; pixel += p.x * bpp; // pre multiply so this is only once. bpp *= sx; ptch *= sy; while(c >= 0) { c--; uint32_t *pixel32 = (uint32_t *)pixel; *pixel32 = colorToUInt(color); e2 = e; if(e2 > -dx) { // Move horizontally. e -= dy; p.x += sx; // Are we still in bounds? if(p.x >= surface->w || p.x < 0) { // We have gone out of bounds, no need to continue. break; } pixel += bpp; } if(e2 < dy) { // Move vertically. e += dx; p.y += sy; // Are we still in bounds? if(p.y >= surface->h || p.y < 0) { // We have gone out of bounds, no need to continue. break; } pixel += ptch; } } }
//draws an empty box onto the surface void Surface::box(const SDL_Color color, SDL_Rect *rect, int thickness){ box(colorToUInt(color), rect, thickness); }
//fills a part of the surface with color void Surface::fill(const SDL_Color color, SDL_Rect *rect){ fill(colorToUInt(color), rect); }