2014-12-28 23:52:04 +03:00
|
|
|
#include "smalllibc/kosSyst.h"
|
2014-12-28 21:29:59 +03:00
|
|
|
#include "render.h"
|
2014-12-28 23:52:04 +03:00
|
|
|
#include "smalllibc/func.h"
|
2014-12-28 21:29:59 +03:00
|
|
|
#include "image.h"
|
|
|
|
//#include "mymath.h"
|
|
|
|
|
|
|
|
CKosImage::CKosImage(CKosRender *render, RGBA *buffer, int width, int height)
|
|
|
|
{
|
|
|
|
this->isColor = false;
|
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
|
|
|
this->buffer = buffer;
|
|
|
|
this->render = render;
|
|
|
|
this->mode = DRAW_ALPHA;
|
|
|
|
this->frame = 0;
|
|
|
|
this->frameWidth = 0;
|
|
|
|
this->frameHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CKosImage::~CKosImage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKosImage::SetMode(int mode)
|
|
|
|
{
|
|
|
|
this->mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKosImage::SetFrameSize(int width, int height)
|
|
|
|
{
|
|
|
|
this->frameWidth = width;
|
|
|
|
this->frameHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKosImage::Draw(Point position, float angle, RGB color)
|
|
|
|
{
|
|
|
|
this->isColor = true;
|
|
|
|
this->color = color;
|
|
|
|
this->Draw(position, angle);
|
|
|
|
this->isColor = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CKosImage::getPixel(int x, int y)
|
|
|
|
{
|
|
|
|
return y * this->width + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKosImage::Draw(Point position, float angle, int frame)
|
|
|
|
{
|
|
|
|
this->frame = frame;
|
|
|
|
Draw(position, angle);
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:03:06 +03:00
|
|
|
void CKosImage::Draw(Point position, float angle, int frame, RGB color)
|
|
|
|
{
|
|
|
|
this->isColor = true;
|
|
|
|
this->color = color;
|
|
|
|
this->frame = frame;
|
|
|
|
Draw(position, angle);
|
|
|
|
this->isColor = false;
|
|
|
|
}
|
|
|
|
|
2014-12-28 21:29:59 +03:00
|
|
|
void CKosImage::Draw(Point position, float angle)
|
|
|
|
{
|
|
|
|
float alpha;
|
2014-12-30 18:03:06 +03:00
|
|
|
Point p, p1, p2;
|
2014-12-28 21:29:59 +03:00
|
|
|
RGB pixel, newPixel;
|
|
|
|
RGBA addPixel;
|
|
|
|
int PixelID;
|
|
|
|
Point fix;
|
|
|
|
if (angle == 270)
|
|
|
|
fix = Point(0, -1);
|
|
|
|
else
|
|
|
|
if (angle == 180)
|
|
|
|
fix = Point(-1, -1);
|
|
|
|
else
|
|
|
|
if (angle == 90)
|
|
|
|
fix = Point(-1, 0);
|
|
|
|
else
|
|
|
|
fix = Point(0, 0);
|
|
|
|
|
|
|
|
Point center = Point(this->width / 2, this->height / 2);
|
|
|
|
double a = -angle * (3.14 / 180);
|
|
|
|
|
|
|
|
double SinRad = sin(a);
|
|
|
|
double CosRad = cos(a);
|
|
|
|
|
2014-12-30 18:03:06 +03:00
|
|
|
for (int y = 0; y < this->height; ++y)
|
|
|
|
for (int x = 0; x < this->width; ++x)
|
2014-12-28 21:29:59 +03:00
|
|
|
{
|
|
|
|
p1 = Point(x, y) - center;
|
|
|
|
|
|
|
|
p.X = roundInt(p1.X * CosRad - p1.Y * SinRad) + center.X;
|
|
|
|
p.Y = roundInt(p1.X * SinRad + p1.Y * CosRad) + center.Y;
|
|
|
|
|
2014-12-30 18:03:06 +03:00
|
|
|
p2 = Point(x + fix.X + position.X, y + fix.Y + position.Y);
|
|
|
|
|
|
|
|
if (p.X >= 0 && p.X < this->width && p.Y >= 0 && p.Y < this->height
|
|
|
|
&& p2.X >= 0 && p2.Y >= 0 && p2.X < this->render->width && p2.Y < this->render->height)
|
2014-12-28 21:29:59 +03:00
|
|
|
{
|
|
|
|
p.Y += this->frame * this->frameHeight;
|
|
|
|
addPixel = this->buffer[this->getPixel(p.X, p.Y)];
|
2014-12-30 18:03:06 +03:00
|
|
|
PixelID = this->render->getPixel(p2.X, p2.Y);
|
2014-12-28 21:29:59 +03:00
|
|
|
pixel = this->render->buffer[PixelID];
|
|
|
|
|
|
|
|
if (addPixel.a > 0)
|
|
|
|
{
|
|
|
|
if (this->isColor)
|
|
|
|
{
|
|
|
|
addPixel.r = this->color.r;
|
|
|
|
addPixel.g = this->color.g;
|
|
|
|
addPixel.b = this->color.b;
|
|
|
|
}
|
|
|
|
|
|
|
|
alpha = (float)addPixel.a / 255.0f;
|
|
|
|
|
2015-02-26 01:06:51 +03:00
|
|
|
if (this->mode == DRAW_ALPHA)
|
2014-12-28 21:29:59 +03:00
|
|
|
{
|
|
|
|
newPixel.r = di((double)(pixel.r * (1 - alpha) + addPixel.r * alpha));
|
|
|
|
newPixel.g = di((double)(pixel.g * (1 - alpha) + addPixel.g * alpha));
|
|
|
|
newPixel.b = di((double)(pixel.b * (1 - alpha) + addPixel.b * alpha));
|
|
|
|
}
|
|
|
|
else
|
2015-02-26 01:06:51 +03:00
|
|
|
if (this->mode == DRAW_ALPHA_ADD)
|
2014-12-28 21:29:59 +03:00
|
|
|
{
|
|
|
|
newPixel.r = di(min(255, (double)(pixel.r * (1 - alpha) + addPixel.r * alpha)));
|
|
|
|
newPixel.g = di(min(255, (double)(pixel.g * (1 - alpha) + addPixel.g * alpha)));
|
|
|
|
newPixel.b = di(min(255, (double)(pixel.b * (1 - alpha) + addPixel.b * alpha)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newPixel.r = addPixel.r;
|
|
|
|
newPixel.g = addPixel.g;
|
|
|
|
newPixel.b = addPixel.b;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->render->buffer[PixelID] = newPixel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|