Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions examples/pong/pong.tile
Original file line number Diff line number Diff line change
@@ -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();
}
30 changes: 19 additions & 11 deletions src/tile/AntlrToStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/tile/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +28,7 @@ public class Program extends Generator {
private static int tasmGlobalVarIdx = 0;
public static Map<String, Variable> globalVariableSymbols = new HashMap<>();
public static Deque<Statement> parentStack = new ArrayDeque<>();

public static List<Path> programPaths = new ArrayList<>();
private static boolean _err;

private Path baseDirectory;
Expand All @@ -51,6 +52,10 @@ public void setBaseDirectory(Path baseDirectory) {
this.baseDirectory = baseDirectory;
}

public Path getBaseDirectory() {
return baseDirectory;
}

public void markAsImported() {
this.isImportedFile = true;
}
Expand Down
10 changes: 9 additions & 1 deletion src/tile/app/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"));
}
}