From fe3de52b30e1de7c856773a73ec6e6e73ac0b962 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Mon, 1 Nov 2021 20:08:46 +0000 Subject: [PATCH] World renderer with textures. Co-authored-by: sigonasr2 --- src/sig/Panel.java | 82 +------------- src/sig/SigRenderer.java | 230 ++++++++++++++++++++++++++++++++++----- 2 files changed, 204 insertions(+), 108 deletions(-) diff --git a/src/sig/Panel.java b/src/sig/Panel.java index e542564..bf735ab 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -14,6 +14,7 @@ public class Panel extends JPanel{ startTime = System.nanoTime(); } public void paintComponent(Graphics g) { + //g.clearRect(0,0,SigRenderer.SCREEN_WIDTH,SigRenderer.SCREEN_HEIGHT); g.setColor(Color.BLACK); g.fillRect(0,0,SigRenderer.SCREEN_WIDTH,SigRenderer.SCREEN_HEIGHT); //Vector3f origin = new Vector3f(Math.cos(SigRenderer.rot)*10,0,10+Math.sin(SigRenderer.rot)*10); @@ -32,86 +33,7 @@ public class Panel extends JPanel{ } } }*/ - - final int h=SigRenderer.SCREEN_HEIGHT; - - for (int x=0;x0) {hit=1;} - - if (side==0) { - perpWallDist=sideDistX-deltaDistX; - } else { - perpWallDist=sideDistY-deltaDistY; - } - } - - int lineHeight = (int)(h/perpWallDist); - int drawStart=-lineHeight/2+h/2; - if (drawStart<0) { - drawStart=0; - } - int drawEnd=lineHeight/2+h/2; - if (drawEnd>=h) {drawEnd=h-1;} - - Color col; - switch(SigRenderer.worldMap[mapX][mapY]) { - case 1:{col=Color.RED;}break; - case 2:{col=Color.GREEN;}break; - case 3:{col=Color.BLUE;}break; - case 4:{col=Color.WHITE;}break; - default:{col=Color.YELLOW;}break; - } - - if (side==1) {col=col.darker();} - - g.setColor(col); - g.drawLine(x,drawStart,x,drawEnd); - } + g.drawImage(SigRenderer.buffer2,0,0,null); endTime=System.nanoTime(); SigRenderer.DRAWLOOPTIME = (endTime-startTime)/1000000f; } diff --git a/src/sig/SigRenderer.java b/src/sig/SigRenderer.java index ea7b464..0b98791 100644 --- a/src/sig/SigRenderer.java +++ b/src/sig/SigRenderer.java @@ -5,17 +5,25 @@ import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.awt.Graphics2D; import java.awt.Toolkit; +import java.awt.image.BufferedImage; +import java.awt.Color; +import java.awt.AlphaComposite; +import java.awt.GraphicsEnvironment; +import java.awt.GraphicsConfiguration; public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{ - public final static int SCREEN_WIDTH=1280; - public final static int SCREEN_HEIGHT=720; + public final static int SCREEN_WIDTH=640; + public final static int SCREEN_HEIGHT=480; + public final static int TEXTURE_WIDTH=64; + public final static int TEXTURE_HEIGHT=64; public final static long TIMEPERTICK = 16666667l; public static float DRAWTIME=0; public static float DRAWLOOPTIME=0; public static double x = 22; - public static double y = 12; + public static double y = 11.5; public static double dirX = -1; public static double dirY = 0; public static double planeX = 0; @@ -30,31 +38,35 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene public static int[][] worldMap = new int[][] { - {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1}, - {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1}, - {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, - {1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,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ufferedImage buffer = new BufferedImage(SCREEN_WIDTH,SCREEN_HEIGHT,BufferedImage.TYPE_INT_ARGB); + public static BufferedImage buffer2 = new BufferedImage(SCREEN_WIDTH,SCREEN_HEIGHT,BufferedImage.TYPE_INT_ARGB); public void runGameLoop() { if (UP_KEY) { @@ -89,10 +101,172 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene planeX=planeX*Math.cos(rotSpeed)-planeY*Math.sin(rotSpeed); planeY=oldPlaneX*Math.sin(rotSpeed)+planeY*Math.cos(rotSpeed); } + + //buffer.flush(); + Graphics2D g = buffer.createGraphics(); + g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); + g.fillRect(0,0,SigRenderer.SCREEN_WIDTH,SigRenderer.SCREEN_HEIGHT); + + //reset composite + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); + + final int h=SigRenderer.SCREEN_HEIGHT; + + for (int x=0;x0) {hit=1;} + + if (side==0) { + perpWallDist=sideDistX-deltaDistX; + } else { + perpWallDist=sideDistY-deltaDistY; + } + } + + int lineHeight = (int)(h/perpWallDist); + int drawStart=-lineHeight/2+h/2; + if (drawStart<0) { + drawStart=0; + } + int drawEnd=lineHeight/2+h/2; + if (drawEnd>=h) {drawEnd=h-1;} + + /*Color col; //FLAT COLOR CHOOSER. + switch(SigRenderer.worldMap[mapX][mapY]) { + case 1:{col=Color.RED;}break; + case 2:{col=Color.GREEN;}break; + case 3:{col=Color.BLUE;}break; + case 4:{col=Color.WHITE;}break; + default:{col=Color.YELLOW;}break; + } + + if (side==1) {col=col.darker();} + + g.setColor(col); + g.drawLine(x,drawStart,x,drawEnd);*/ + + int texNum=SigRenderer.worldMap[mapX][mapY]-1; + + double wallX; + if (side==0) { + wallX=SigRenderer.y+perpWallDist*rayDirY; + } else { + wallX=SigRenderer.x+perpWallDist*rayDirX; + } + wallX-=Math.floor(wallX); + + int texX=(int)(wallX*(double)SigRenderer.TEXTURE_WIDTH); + if (side==0&&rayDirX>0){texX=SigRenderer.TEXTURE_WIDTH-texX-1;} + if (side==1&&rayDirY<0){texX=SigRenderer.TEXTURE_WIDTH-texX-1;} + + double step=1d*SigRenderer.TEXTURE_HEIGHT/lineHeight; + double texPos=(drawStart-h/2+lineHeight/2)*step; + for (int y=drawStart;y=1&&y%16>=1)?1:0); + textures[6][TEXTURE_WIDTH*y+x]=65536*ycolor; + textures[7][TEXTURE_WIDTH*y+x]=128+256*128+65536*128; + } + } + Panel p = new Panel(); new Thread() {