From a859f7ec4298174007150661058624405f208f8a Mon Sep 17 00:00:00 2001 From: Berke Date: Fri, 2 May 2025 22:11:56 +0300 Subject: [PATCH 1/2] co-op pong game --- examples/pong/pong.tile | 152 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 examples/pong/pong.tile diff --git a/examples/pong/pong.tile b/examples/pong/pong.tile new file mode 100644 index 0000000..816e1ca --- /dev/null +++ b/examples/pong/pong.tile @@ -0,0 +1,152 @@ +/* + 05.02.2025 + berkenvr + + make app ARGS=".\examples\pong\pong.tile -o .\examples\pong\pong.bin -gen-tasm -l raylib.dll" + tvm .\examples\pong\pong.bin +*/ + +native func InitWindow(width: ci32, height: ci32, title: cptr): cvoid; +native func WindowShouldClose(): ci8; +native func CloseWindow(): cvoid; +native func BeginDrawing(): cvoid; +native func EndDrawing(): cvoid; +native func ClearBackground(color: cu32): cvoid; +native func SetTargetFPS(fps: ci32): cvoid; +native func GetFrameTime(): cf32; +native func DrawRectangle(posX: ci32, posY: ci32, width: ci32, height: ci32, color: cu32): cvoid; +native func DrawCircle(centerX: ci32, centerY: ci32, radius: cf32, color: cu32): cvoid; +native func IsKeyDown(key: ci16): ci8; + +screenWidth: int = 800; +screenHeight: int = 600; +paddleWidth: int = 10; +paddleHeight: int = 50; +paddleSpeed: int = 400; +ballSize: int = 10; + +func run_game(): void { + leftX: int = 50; + leftY: float = (screenHeight - paddleHeight) / 2.0; + + rightX: int = screenWidth - 50 - paddleWidth; + rightY: float = (screenHeight - paddleHeight) / 2.0; + + ballX: float = screenWidth / 2.0 - ballSize / 2.0; + ballY: float = screenHeight / 2.0 - ballSize / 2.0; + ballRadius: float = ballSize / 2.0; + ballSpeed: float = 230.0; + velX: float = ballSpeed; + velY: float = ballSpeed; + + while ((bool)WindowShouldClose() == false) { + dt: float = GetFrameTime(); + + // Left Side Player: W/S + if((bool)IsKeyDown('W')) { + leftY = leftY - paddleSpeed * dt; + } else if((bool)IsKeyDown('S')) { + leftY = leftY + paddleSpeed * dt; + } + + // Right Side Player: Up, Down Arrow Keys + if((bool)IsKeyDown(265)) { + rightY = rightY - paddleSpeed * dt; + } else if((bool)IsKeyDown(264)) { + rightY = rightY + paddleSpeed * dt; + } + + // Boundaries + if(leftY < 0.0) { + leftY = 0.0; + } else if(leftY + paddleHeight > screenHeight) { + leftY = screenHeight - paddleHeight; + } + if(rightY < 0.0) { + rightY = 0.0; + } else if(rightY + paddleHeight > screenHeight) { + rightY = screenHeight - paddleHeight; + } + + // Ball movement + ballX = ballX + velX * dt; + ballY = ballY + velY * dt; + + // Top and bottom edge tab + if(ballY <= 0.0) { + ballY = 0.0; + velY = -velY; + } else if(ballY + ballSize >= screenHeight) { + ballY = screenHeight - ballSize; + velY = -velY; + } + + // Left paddle collision + if(ballX + ballSize > leftX && + ballX < leftX + paddleWidth && + ballY + ballSize > leftY && + ballY < leftY + paddleHeight ) + { + ballX = leftX + paddleWidth; + velX = -velX; + + centerY: float = ballY + ballRadius; + paddleMid: float = leftY + paddleHeight / 2.0; + + if(centerY < paddleMid) { + velY = -ballSpeed; + } else { + velY = ballSpeed; + } + } + + // Right paddle collision + if(ballX < rightX + paddleWidth && + ballX + ballSize > rightX && + ballY + ballSize > rightY && + ballY < rightY + paddleHeight) { + + ballX = rightX - ballSize; + velX = -velX; + + centerY: float = ballY + ballRadius; + paddleMid: float = rightY + paddleHeight / 2.0; + + if(centerY < paddleMid) { + velY = -ballSpeed; + } else { + velY = ballSpeed; + } + } + + // Reset after score + if(ballX < 0.0) { + ballX = screenWidth / 2.0 - ballSize / 2.0; + ballY = screenHeight / 2.0 - ballSize / 2.0; + velX = -ballSpeed; + } else if(ballX > screenWidth) { + ballX = screenWidth / 2.0 - ballSize / 2.0; + ballY = screenHeight / 2.0 - ballSize / 2.0; + velX = ballSpeed; + } + + BeginDrawing(); + ClearBackground(0x000000FF); + DrawRectangle(leftX, (int)leftY, paddleWidth, paddleHeight, 0xFF0000FF); + DrawRectangle(rightX, (int)rightY, paddleWidth, paddleHeight, 0x0000FFFF); + DrawCircle( + (int)(ballX + ballRadius), + (int)(ballY + ballRadius), + ballRadius, + 0xFFFFFFFF + ); + EndDrawing(); + } +} + +func main(argc: int): void { + InitWindow(screenWidth, screenHeight, "Pong"); + SetTargetFPS(60); + run_game(); + CloseWindow(); +} From 84b625238697deb0714481afd3e80d72751c5178 Mon Sep 17 00:00:00 2001 From: Berke Date: Sat, 3 May 2025 16:01:04 +0300 Subject: [PATCH 2/2] fixed #37 --- src/tile/AntlrToStatement.java | 30 +++++++++++++++++++----------- src/tile/Program.java | 7 ++++++- src/tile/app/Tile.java | 10 +++++++++- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/tile/AntlrToStatement.java b/src/tile/AntlrToStatement.java index fd52ab9..2f101c3 100644 --- a/src/tile/AntlrToStatement.java +++ b/src/tile/AntlrToStatement.java @@ -6,10 +6,10 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; import gen.antlr.tile.tileParser; import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.tree.ParseTree; import gen.antlr.tile.tileParser.BlockStmtContext; import gen.antlr.tile.tileParser.ExpressionStmtContext; @@ -541,20 +541,28 @@ public Statement visitImportStmt(ImportStmtContext ctx) { String rawPath = ctx.importTarget().STRING_LITERAL().getText(); Path importPath = Paths.get(rawPath.replace("\"", "")); - if (!Files.exists(importPath)) { + Path baseDir = Program.programPaths.getLast(); + Path combined = baseDir.resolve(importPath).normalize(); + + if (!Files.exists(combined)) { Log.error("Failed to import: file not found: " + rawPath); } - tileParser parser = Tile.createTileParser(importPath.toString()); - - tileParser.ProgramContext importedCtx = parser.program(); - AntlrToProgram visitor = new AntlrToProgram(); - Program importedProgram = visitor.visit(importedCtx); - - importedProgram.setBaseDirectory(importPath.getParent()); - importedProgram.markAsImported(); + Program.programPaths.add(combined.getParent()); + Program importedProgram; + try { + tileParser parser = Tile.createTileParser(combined.toString()); + tileParser.ProgramContext importedCtx = parser.program(); + AntlrToProgram visitor = new AntlrToProgram(); + importedProgram = visitor.visit(importedCtx); + + importedProgram.setBaseDirectory(combined); + importedProgram.markAsImported(); + } finally { + Program.programPaths.removeLast(); + } - return new ImportStmt(importPath.toString(), importedProgram); + return new ImportStmt(combined.toString(), importedProgram); } @Override diff --git a/src/tile/Program.java b/src/tile/Program.java index edcd961..dbc68e5 100644 --- a/src/tile/Program.java +++ b/src/tile/Program.java @@ -4,6 +4,7 @@ import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; @@ -27,7 +28,7 @@ public class Program extends Generator { private static int tasmGlobalVarIdx = 0; public static Map globalVariableSymbols = new HashMap<>(); public static Deque parentStack = new ArrayDeque<>(); - + public static List programPaths = new ArrayList<>(); private static boolean _err; private Path baseDirectory; @@ -51,6 +52,10 @@ public void setBaseDirectory(Path baseDirectory) { this.baseDirectory = baseDirectory; } + public Path getBaseDirectory() { + return baseDirectory; + } + public void markAsImported() { this.isImportedFile = true; } diff --git a/src/tile/app/Tile.java b/src/tile/app/Tile.java index 13fda62..b944614 100644 --- a/src/tile/app/Tile.java +++ b/src/tile/app/Tile.java @@ -4,6 +4,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import org.antlr.v4.runtime.tree.ParseTree; @@ -13,7 +15,6 @@ import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.Token; import gen.antlr.tile.tileLexer; import gen.antlr.tile.tileParser; @@ -59,6 +60,7 @@ public static void main(String args[]) { } AntlrToProgram programVisitor = new AntlrToProgram(); + Program.programPaths.add(getProgramDir(results)); Program program = programVisitor.visit(ast); if (Program.getError() == false) { @@ -158,4 +160,10 @@ public static String getFileNameWithoutExtension(String filePath) { return fileName; } + + private static Path getProgramDir(ArgResults results) { + Path filePath = Paths.get(System.getProperty("user.dir"), results.inputFile); + Path dir = filePath.getParent(); + return dir != null ? dir : Paths.get(System.getProperty("user.dir")); + } }