generated from sigonasr2/JavaProjectTemplate
DrawCircle implementation
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
d9847a5fa6
commit
d20c20490c
@ -4,6 +4,7 @@ import sig.engine.AnimatedSprite;
|
||||
import sig.engine.Color;
|
||||
import sig.engine.Font;
|
||||
import sig.engine.Key;
|
||||
import sig.engine.Mouse;
|
||||
import sig.engine.Panel;
|
||||
import sig.engine.Point;
|
||||
import sig.engine.Sprite;
|
||||
@ -57,20 +58,20 @@ public class JavaProjectTemplate {
|
||||
|
||||
game.Clear(Color.BRIGHT_BLUE);
|
||||
|
||||
game.Draw(100,20,Color.BLACK);
|
||||
game.Draw_Line(10,10,35,35,Color.BLACK);
|
||||
game.Draw(100,20,Color.BLACK); //That's not a dead pixel, it's a black one!
|
||||
game.Draw_Line(10,10,35,35,Color.BLACK); //Line Drawing
|
||||
|
||||
game.Draw_Sprite(450,75,bookSpr,Color.GREEN);
|
||||
game.Draw_Sprite(450,75+bookSpr.getHeight(),bookSpr,Transform.VERTICAL);
|
||||
game.Draw_Sprite(450,75,bookSpr,Color.GREEN); //Sprite drawing (with green tint)
|
||||
game.Draw_Sprite(450,75+bookSpr.getHeight(),bookSpr,Transform.VERTICAL); //Sprite drawing with vertical flip
|
||||
|
||||
game.Draw_Animated_Sprite(pl.x,pl.y,pl.spr,pl.animationFrame);
|
||||
game.Draw_Animated_Sprite(pl.x,pl.y,pl.spr,pl.animationFrame); //Animated Sprite drawing
|
||||
|
||||
game.Draw_Text(10,40,"Hello World!",Font.PROFONT_12);
|
||||
game.Draw_Text_Ext(10,52,"Hello World 2!",Font.PROFONT_36,Color.MAGENTA);
|
||||
game.Draw_Text(10,40,"Mouse X: "+Mouse.x+" Mouse Y:"+Mouse.y,Font.PROFONT_12); //Draw Mouse coordinates in tiny font
|
||||
game.Draw_Text_Ext(10,52,"Hello World 2!",Font.PROFONT_36,Color.MAGENTA); //Draw in larger font
|
||||
|
||||
game.Fill_Triangle(160,160,190,190,50,250,new Color(255,0,0,150));
|
||||
game.Fill_Triangle(160,160,190,190,50,250,new Color(255,0,0,150)); //Draw a colored triangle
|
||||
|
||||
game.FillTexturedTriangle(
|
||||
game.FillTexturedTriangle( //Define the uv tex coords of a triangle and a sprite and draw a texture onto it
|
||||
List.of(
|
||||
new Point<Double>(400d,400d),
|
||||
new Point<Double>(400d,550d),
|
||||
|
10
src/sig/engine/Mouse.java
Normal file
10
src/sig/engine/Mouse.java
Normal file
@ -0,0 +1,10 @@
|
||||
package sig.engine;
|
||||
|
||||
public class Mouse {
|
||||
public static int x;
|
||||
public static int y;
|
||||
public static Point<Integer> mousePosition = new Point<Integer>(0,0);
|
||||
public static Point<Integer> GetPos(){
|
||||
return new Point<Integer>(x,y);
|
||||
}
|
||||
}
|
@ -50,7 +50,6 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
public double nanaY = 0;
|
||||
public int button = 0;
|
||||
public HashMap<Integer,Boolean> MOUSE = new HashMap<>();
|
||||
private Point<Integer> mousePosition = new Point<Integer>(0,0);
|
||||
private MouseScrollValue scrollWheel=null;
|
||||
public static final int UPDATE_LOOP_FRAMERATE = 244;
|
||||
public static final long UPDATE_LOOP_NANOTIME = (long)((1d/UPDATE_LOOP_FRAMERATE)*1000000000l);
|
||||
@ -163,12 +162,16 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
this.addMouseMotionListener(new MouseMotionListener(){
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
mousePosition.set(e.getX(),e.getY());
|
||||
Mouse.x=e.getX();
|
||||
Mouse.y=e.getY();
|
||||
Mouse.mousePosition.set(e.getX(),e.getY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
mousePosition.set(e.getX(),e.getY());
|
||||
Mouse.x=e.getX();
|
||||
Mouse.y=e.getY();
|
||||
Mouse.mousePosition.set(e.getX(),e.getY());
|
||||
}
|
||||
});
|
||||
this.addMouseWheelListener(new MouseWheelListener(){
|
||||
@ -764,121 +767,6 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
if (y > y3) return;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* void PixelGameEngine::FillTexturedTriangle(const std::vector<olc::vf2d>& vPoints, std::vector<olc::vf2d> vTex, std::vector<olc::Pixel> vColour, olc::Sprite* sprTex)
|
||||
{
|
||||
olc::vi2d p1 = vPoints[0];
|
||||
olc::vi2d p2 = vPoints[1];
|
||||
olc::vi2d p3 = vPoints[2];
|
||||
|
||||
if (p2.y < p1.y){std::swap(p1.y, p2.y); std::swap(p1.x, p2.x); std::swap(vTex[0].x, vTex[1].x); std::swap(vTex[0].y, vTex[1].y); std::swap(vColour[0], vColour[1]);}
|
||||
if (p3.y < p1.y){std::swap(p1.y, p3.y); std::swap(p1.x, p3.x); std::swap(vTex[0].x, vTex[2].x); std::swap(vTex[0].y, vTex[2].y); std::swap(vColour[0], vColour[2]);}
|
||||
if (p3.y < p2.y){std::swap(p2.y, p3.y); std::swap(p2.x, p3.x); std::swap(vTex[1].x, vTex[2].x); std::swap(vTex[1].y, vTex[2].y); std::swap(vColour[1], vColour[2]);}
|
||||
|
||||
olc::vi2d dPos1 = p2 - p1;
|
||||
olc::vf2d dTex1 = vTex[1] - vTex[0];
|
||||
int dcr1 = vColour[1].r - vColour[0].r;
|
||||
int dcg1 = vColour[1].g - vColour[0].g;
|
||||
int dcb1 = vColour[1].b - vColour[0].b;
|
||||
int dca1 = vColour[1].a - vColour[0].a;
|
||||
|
||||
olc::vi2d dPos2 = p3 - p1;
|
||||
olc::vf2d dTex2 = vTex[2] - vTex[0];
|
||||
int dcr2 = vColour[2].r - vColour[0].r;
|
||||
int dcg2 = vColour[2].g - vColour[0].g;
|
||||
int dcb2 = vColour[2].b - vColour[0].b;
|
||||
int dca2 = vColour[2].a - vColour[0].a;
|
||||
|
||||
float dax_step = 0, dbx_step = 0, dcr1_step = 0, dcr2_step = 0, dcg1_step = 0, dcg2_step = 0, dcb1_step = 0, dcb2_step = 0, dca1_step = 0, dca2_step = 0;
|
||||
olc::vf2d vTex1Step, vTex2Step;
|
||||
|
||||
if (dPos1.y)
|
||||
{
|
||||
dax_step = dPos1.x / (float)abs(dPos1.y);
|
||||
vTex1Step = dTex1 / (float)abs(dPos1.y);
|
||||
dcr1_step = dcr1 / (float)abs(dPos1.y);
|
||||
dcg1_step = dcg1 / (float)abs(dPos1.y);
|
||||
dcb1_step = dcb1 / (float)abs(dPos1.y);
|
||||
dca1_step = dca1 / (float)abs(dPos1.y);
|
||||
}
|
||||
|
||||
if (dPos2.y)
|
||||
{
|
||||
dbx_step = dPos2.x / (float)abs(dPos2.y);
|
||||
vTex2Step = dTex2 / (float)abs(dPos2.y);
|
||||
dcr2_step = dcr2 / (float)abs(dPos2.y);
|
||||
dcg2_step = dcg2 / (float)abs(dPos2.y);
|
||||
dcb2_step = dcb2 / (float)abs(dPos2.y);
|
||||
dca2_step = dca2 / (float)abs(dPos2.y);
|
||||
}
|
||||
|
||||
olc::vi2d vStart;
|
||||
olc::vi2d vEnd;
|
||||
int vStartIdx;
|
||||
|
||||
for (int pass = 0; pass < 2; pass++)
|
||||
{
|
||||
if (pass == 0)
|
||||
{
|
||||
vStart = p1; vEnd = p2; vStartIdx = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
dPos1 = p3 - p2;
|
||||
dTex1 = vTex[2] - vTex[1];
|
||||
dcr1 = vColour[2].r - vColour[1].r;
|
||||
dcg1 = vColour[2].g - vColour[1].g;
|
||||
dcb1 = vColour[2].b - vColour[1].b;
|
||||
dca1 = vColour[2].a - vColour[1].a;
|
||||
dcr1_step = 0; dcg1_step = 0; dcb1_step = 0; dca1_step = 0;
|
||||
|
||||
if (dPos2.y) dbx_step = dPos2.x / (float)abs(dPos2.y);
|
||||
if (dPos1.y)
|
||||
{
|
||||
dax_step = dPos1.x / (float)abs(dPos1.y);
|
||||
vTex1Step = dTex1 / (float)abs(dPos1.y);
|
||||
dcr1_step = dcr1 / (float)abs(dPos1.y);
|
||||
dcg1_step = dcg1 / (float)abs(dPos1.y);
|
||||
dcb1_step = dcb1 / (float)abs(dPos1.y);
|
||||
dca1_step = dca1 / (float)abs(dPos1.y);
|
||||
}
|
||||
|
||||
vStart = p2; vEnd = p3; vStartIdx = 1;
|
||||
}
|
||||
|
||||
if (dPos1.y)
|
||||
{
|
||||
for (int i = vStart.y; i <= vEnd.y; i++)
|
||||
{
|
||||
int ax = int(vStart.x + (float)(i - vStart.y) * dax_step);
|
||||
int bx = int(p1.x + (float)(i - p1.y) * dbx_step);
|
||||
|
||||
olc::vf2d tex_s(vTex[vStartIdx].x + (float)(i - vStart.y) * vTex1Step.x, vTex[vStartIdx].y + (float)(i - vStart.y) * vTex1Step.y);
|
||||
olc::vf2d tex_e(vTex[0].x + (float)(i - p1.y) * vTex2Step.x, vTex[0].y + (float)(i - p1.y) * vTex2Step.y);
|
||||
|
||||
olc::Pixel col_s(vColour[vStartIdx].r + uint8_t((float)(i - vStart.y) * dcr1_step), vColour[vStartIdx].g + uint8_t((float)(i - vStart.y) * dcg1_step),
|
||||
vColour[vStartIdx].b + uint8_t((float)(i - vStart.y) * dcb1_step), vColour[vStartIdx].a + uint8_t((float)(i - vStart.y) * dca1_step));
|
||||
|
||||
olc::Pixel col_e(vColour[0].r + uint8_t((float)(i - p1.y) * dcr2_step), vColour[0].g + uint8_t((float)(i - p1.y) * dcg2_step),
|
||||
vColour[0].b + uint8_t((float)(i - p1.y) * dcb2_step), vColour[0].a + uint8_t((float)(i - p1.y) * dca2_step));
|
||||
|
||||
if (ax > bx) { std::swap(ax, bx); std::swap(tex_s, tex_e); std::swap(col_s, col_e); }
|
||||
|
||||
float tstep = 1.0f / ((float)(bx - ax));
|
||||
float t = 0.0f;
|
||||
|
||||
for (int j = ax; j < bx; j++)
|
||||
{
|
||||
olc::Pixel pixel = PixelLerp(col_s, col_e, t);
|
||||
if (sprTex != nullptr) pixel *= sprTex->Sample(tex_s.lerp(tex_e, t));
|
||||
Draw(j, i, pixel);
|
||||
t += tstep;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public void FillTexturedTriangle(List<Point<Double>>vPoints,List<Point<Double>>vTex,List<Color>vColour,Sprite sprTex){
|
||||
try {
|
||||
@ -1006,6 +894,81 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
}
|
||||
} catch (CloneNotSupportedException e) {}
|
||||
}
|
||||
/*void PixelGameEngine::DrawCircle(int32_t x, int32_t y, int32_t radius, Pixel p, uint8_t mask)
|
||||
{ // Thanks to IanM-Matrix1 #PR121
|
||||
if (radius < 0 || x < -radius || y < -radius || x - GetDrawTargetWidth() > radius || y - GetDrawTargetHeight() > radius)
|
||||
return;
|
||||
|
||||
if (radius > 0)
|
||||
{
|
||||
int x0 = 0;
|
||||
int y0 = radius;
|
||||
int d = 3 - 2 * radius;
|
||||
|
||||
while (y0 >= x0) // only formulate 1/8 of circle
|
||||
{
|
||||
// Draw even octants
|
||||
if (mask & 0x01) Draw(x + x0, y - y0, p);// Q6 - upper right right
|
||||
if (mask & 0x04) Draw(x + y0, y + x0, p);// Q4 - lower lower right
|
||||
if (mask & 0x10) Draw(x - x0, y + y0, p);// Q2 - lower left left
|
||||
if (mask & 0x40) Draw(x - y0, y - x0, p);// Q0 - upper upper left
|
||||
if (x0 != 0 && x0 != y0)
|
||||
{
|
||||
if (mask & 0x02) Draw(x + y0, y - x0, p);// Q7 - upper upper right
|
||||
if (mask & 0x08) Draw(x + x0, y + y0, p);// Q5 - lower right right
|
||||
if (mask & 0x20) Draw(x - y0, y + x0, p);// Q3 - lower lower left
|
||||
if (mask & 0x80) Draw(x - x0, y - y0, p);// Q1 - upper left left
|
||||
}
|
||||
|
||||
if (d < 0)
|
||||
d += 4 * x0++ + 6;
|
||||
else
|
||||
d += 4 * (x0++ - y0--) + 10;
|
||||
}
|
||||
}
|
||||
else
|
||||
Draw(x, y, p);
|
||||
} */
|
||||
|
||||
public void Draw_Circle(int x, int y, int radius, Color col) {
|
||||
Draw_Circle(x,y,radius,col,(byte)0xFF);
|
||||
}
|
||||
|
||||
//The mask indicates which pie slices to draw, each bit of the mask represents 1/8th of a pie slice. By default all pie slices are drawn.
|
||||
public void Draw_Circle(int x, int y, int radius, Color col, byte mask) {
|
||||
if (radius < 0 || x < -radius || y < -radius || x - JavaProjectTemplate.WINDOW_WIDTH > radius || y - JavaProjectTemplate.WINDOW_HEIGHT > radius)
|
||||
return;
|
||||
|
||||
if (radius > 0)
|
||||
{
|
||||
int x0 = 0;
|
||||
int y0 = radius;
|
||||
int d = 3 - 2 * radius;
|
||||
|
||||
while (y0 >= x0) // only formulate 1/8 of circle
|
||||
{
|
||||
// Draw even octants
|
||||
if ((mask & 0x01)!=0) Draw(x + x0, y - y0, col);// Q6 - upper right right
|
||||
if ((mask & 0x04)!=0) Draw(x + y0, y + x0, col);// Q4 - lower lower right
|
||||
if ((mask & 0x10)!=0) Draw(x - x0, y + y0, col);// Q2 - lower left left
|
||||
if ((mask & 0x40)!=0) Draw(x - y0, y - x0, col);// Q0 - upper upper left
|
||||
if (x0 != 0 && x0 != y0)
|
||||
{
|
||||
if ((mask & 0x02)!=0) Draw(x + y0, y - x0, col);// Q7 - upper upper right
|
||||
if ((mask & 0x08)!=0) Draw(x + x0, y + y0, col);// Q5 - lower right right
|
||||
if ((mask & 0x20)!=0) Draw(x - y0, y + x0, col);// Q3 - lower lower left
|
||||
if ((mask & 0x80)!=0) Draw(x - x0, y - y0, col);// Q1 - upper left left
|
||||
}
|
||||
|
||||
if (d < 0)
|
||||
d += 4 * x0++ + 6;
|
||||
else
|
||||
d += 4 * (x0++ - y0--) + 10;
|
||||
}
|
||||
}
|
||||
else
|
||||
Draw(x, y, col);
|
||||
}
|
||||
|
||||
Color ColorLerp(Color c1, Color c2, float t)
|
||||
{ return new Color((int)((c2.r * t) + c1.r * (1.0f - t)),(int)((c2.g * t) + c1.g * (1.0f - t)),(int)((c2.b * t) + c1.b * (1.0f - t)),(int)((c2.a * t) + c1.a * (1.0f - t))); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user