generated from sigonasr2/JavaProjectTemplate
Point now accepts a type instead
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
16f225c423
commit
59eb833694
@ -8,7 +8,7 @@ public class Edge {
|
||||
int max_x;
|
||||
double x_of_min_y;
|
||||
double inverse_slope;
|
||||
public Edge(Point a, Point b) {
|
||||
public Edge(Point<Integer> a, Point<Integer> b) {
|
||||
super();
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
|
@ -50,7 +50,7 @@ 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 mousePosition = new Point(0,0);
|
||||
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);
|
||||
@ -239,11 +239,11 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
|
||||
public void FillCircle(double center_x,double center_y,double r,Color col) {
|
||||
int counter=0;
|
||||
Point[] points = new Point[CIRCLE_PRECISION];
|
||||
List<Point<Integer>> points = new ArrayList<Point<Integer>>();
|
||||
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) {
|
||||
//System.out.println("Loop "+counter+++". Theta:"+theta);
|
||||
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
|
||||
points[counter++]=new Point((int)(Math.round(Math.sin(theta)*r+center_x)),(int)(Math.round(Math.cos(theta)*r+center_y)));
|
||||
points.add(new Point<Integer>((int)(Math.round(Math.sin(theta)*r+center_x)),(int)(Math.round(Math.cos(theta)*r+center_y))));
|
||||
}
|
||||
FillPolygon(0,0,col,points);
|
||||
}
|
||||
@ -251,30 +251,30 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
|
||||
public void FillOval(double center_x,double center_y,double w,double h,Color col) {
|
||||
int counter=0;
|
||||
Point[] points = new Point[CIRCLE_PRECISION];
|
||||
List<Point<Integer>> points = new ArrayList<Point<Integer>>();
|
||||
double r = Math.max(w,h);
|
||||
double ratio = Math.min(w,h)/r;
|
||||
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) {
|
||||
//System.out.println("Loop "+counter+++". Theta:"+theta);
|
||||
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
|
||||
Point newP = new Point((int)(Math.round(Math.sin(theta)*r)),(int)(Math.round(Math.cos(theta)*r)));
|
||||
Point<Integer> newP = new Point<Integer>((int)(Math.round(Math.sin(theta)*r)),(int)(Math.round(Math.cos(theta)*r)));
|
||||
if (w<h) {
|
||||
newP.x=(int)Math.round(newP.x*ratio);
|
||||
} else {
|
||||
newP.y=(int)Math.round(newP.y*ratio);
|
||||
}
|
||||
newP.x+=center_x;
|
||||
newP.y+=center_y;
|
||||
points[counter++]=newP;
|
||||
newP.x+=(int)center_x;
|
||||
newP.y+=(int)center_y;
|
||||
points.add(newP);
|
||||
}
|
||||
FillPolygon(0,0,col,points);
|
||||
}
|
||||
|
||||
public void FillPolygon(double x_offset,double y_offset,Color col,Point...points) {
|
||||
Edge[] edges = new Edge[points.length];
|
||||
public void FillPolygon(double x_offset,double y_offset,Color col,List<Point<Integer>>points) {
|
||||
Edge[] edges = new Edge[points.size()];
|
||||
List<Edge> edges_sorted = new ArrayList<Edge>();
|
||||
for (int i=0;i<points.length;i++) {
|
||||
edges[i] = new Edge(points[i],points[(i+1)%points.length]);
|
||||
for (int i=0;i<points.size();i++) {
|
||||
edges[i] = new Edge(points.get(i),points.get((i+1)%points.size()));
|
||||
if (!Double.isInfinite(edges[i].inverse_slope)) {
|
||||
if (edges_sorted.size()==0) {
|
||||
edges_sorted.add(edges[i]);
|
||||
@ -739,6 +739,122 @@ 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 Clear(Color col){
|
||||
for (int y=0;y<JavaProjectTemplate.WINDOW_HEIGHT;y++) {
|
||||
|
@ -1,35 +1,35 @@
|
||||
package sig.engine;
|
||||
|
||||
public class Point {
|
||||
int x,y;
|
||||
public class Point<T>{
|
||||
T x,y;
|
||||
|
||||
public Point(int x, int y) {
|
||||
public Point(T x, T y) {
|
||||
super();
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
public T getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
public void setX(T x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
public T getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
public void setY(T y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void set(int x,int y) {
|
||||
public void set(T x,T y) {
|
||||
setX(x);setY(y);
|
||||
}
|
||||
|
||||
public void update(int x,int y) {
|
||||
public void update(T x,T y) {
|
||||
set(x,y);
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class String{
|
||||
private StringBuilder sb;
|
||||
private Point bounds = new Point(0,1);
|
||||
private Point<Integer> bounds = new Point<Integer>(0,1);
|
||||
private int currentLineWidth;
|
||||
public String() {
|
||||
this.sb=new StringBuilder();
|
||||
@ -64,7 +64,7 @@ public class String{
|
||||
|
||||
public void clear() {
|
||||
this.sb.setLength(0);
|
||||
bounds = new Point(0,1);
|
||||
bounds = new Point<Integer>(0,1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +108,7 @@ public class String{
|
||||
*/
|
||||
public String replace(int start,int end,java.lang.String str) {
|
||||
this.sb.replace(start,end,str);
|
||||
bounds = new Point(0,1);
|
||||
bounds = new Point<Integer>(0,1);
|
||||
updateBounds(this.sb.toString());
|
||||
return this;
|
||||
}
|
||||
@ -128,7 +128,7 @@ public class String{
|
||||
java.lang.String cutString = this.sb.substring(start);
|
||||
clear();
|
||||
this.sb.append(cutString);
|
||||
bounds = new Point(0,1);
|
||||
bounds = new Point<Integer>(0,1);
|
||||
currentLineWidth=0;
|
||||
updateBounds(this.sb.toString());
|
||||
return this;
|
||||
@ -152,7 +152,7 @@ public class String{
|
||||
java.lang.String cutString = this.sb.substring(start,end);
|
||||
clear();
|
||||
this.sb.append(cutString);
|
||||
bounds = new Point(0,1);
|
||||
bounds = new Point<Integer>(0,1);
|
||||
currentLineWidth=0;
|
||||
updateBounds(this.sb.toString());
|
||||
return this;
|
||||
@ -164,8 +164,8 @@ public class String{
|
||||
public java.lang.String toString() {
|
||||
return this.sb.toString();
|
||||
}
|
||||
public Point getBounds(Font f) {
|
||||
return new Point(bounds.x*f.getGlyphWidth(),length()>0?Math.max(bounds.y*f.getGlyphHeight(),f.getGlyphHeight()):0);
|
||||
public Point<Integer> getBounds(Font f) {
|
||||
return new Point<Integer>(bounds.x*f.getGlyphWidth(),length()>0?Math.max(bounds.y*f.getGlyphHeight(),f.getGlyphHeight()):0);
|
||||
}
|
||||
private void updateBounds(java.lang.String string) {
|
||||
for (int i=0;i<string.length();i++) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user