Initial raylib + toy example is working

This commit is contained in:
2026-04-27 14:23:42 +10:00
parent 6ba9adff2f
commit 51932bb671
5 changed files with 202 additions and 12 deletions
+9
View File
@@ -1,3 +1,12 @@
# Pactoy
A test game for The Toy Programming Langauge.
## Raylib for RPi
```bash
#why does raylib fall back to SDL?
cmake -B build -DPLATFORM=SDL -DOPENGL_VERSION=Software
cmake --build build
cd build && sudo make install
```
+3
View File
@@ -0,0 +1,3 @@
var screenWidth = 1280;
var screenHeight = 720;
var screenCaption = "Hello raylib from Toy!";
+17 -12
View File
@@ -4,25 +4,30 @@
#LIBS+=-lm
#LDFLAGS+=
#directories (override the build destination)
export TOY_SOURCEDIR=source
export TOY_REPLDIR=repl
export TOY_OUTDIR=../out
export TOY_OBJDIR=obj
#directories
export PAC_SOURCEDIR=source
export PAC_OUTDIR=out
export PAC_OBJDIR=obj
#Override Toy's build destination
export TOY_SOURCEDIR=Toy/source
export TOY_OUTDIR=../$(PAC_OUTDIR)
export TOY_OBJDIR=$(PAC_OBJDIR)
#targets
all: Toy
all: $(PAC_OUTDIR) Toy source
.PHONY: source
source:
$(MAKE) -C $(PAC_SOURCEDIR)
.PHONY: Toy
Toy:
$(MAKE) -C Toy/source -k
$(MAKE) -C $(TOY_SOURCEDIR)
#util targets
$(TOY_OUTDIR):
mkdir $(TOY_OUTDIR)
$(TOY_OBJDIR):
mkdir $(TOY_OBJDIR)
$(PAC_OUTDIR):
mkdir $(PAC_OUTDIR)
#util commands
.PHONY: clean
+111
View File
@@ -0,0 +1,111 @@
#include "toy_console_colors.h"
#include "raylib.h"
#include "toy_lexer.h"
#include "toy_parser.h"
#include "toy_compiler.h"
#include "toy_vm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//utils
unsigned char* readFile(char* path, int* size) {
//open the file
FILE* file = fopen(path, "rb");
if (file == NULL) {
*size = -1; //missing file error
return NULL;
}
//determine the file's length
fseek(file, 0L, SEEK_END);
*size = ftell(file);
rewind(file);
//make some space
unsigned char* buffer = malloc(*size + 1);
if (buffer == NULL) {
fclose(file);
return NULL;
}
//read the file
if (fread(buffer, sizeof(unsigned char), *size, file) < (unsigned int)(*size)) {
fclose(file);
*size = -2; //singal a read error
return NULL;
}
buffer[(*size)++] = '\0';
//clean up and return
fclose(file);
return buffer;
}
unsigned char* makeCodeFromSource(const char* source) {
Toy_Lexer lexer;
Toy_bindLexer(&lexer, source);
Toy_Parser parser;
Toy_bindParser(&parser, &lexer);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Ast* ast = Toy_scanParser(&bucket, &parser);
unsigned char* code = Toy_compileToBytecode(ast);
Toy_freeBucket(&bucket);
return code;
}
//main file
int main() {
//example Toy controlling the window stuff
int size = 0;
const char* source = (char*)readFile("config.toy", &size);
if (!source) {
fprintf(stderr, "File read error: %d\n", size);
return -1;
}
unsigned char* code = makeCodeFromSource(source);
Toy_VM vm;
Toy_initVM(&vm);
Toy_bindVM(&vm, code, NULL);
Toy_runVM(&vm);
//extract the settings
Toy_Value* screenWidthPtr = Toy_accessScopeAsPointer(vm.scope, Toy_toString(&vm.memoryBucket, "screenWidth") );
Toy_Value* screenHeightPtr = Toy_accessScopeAsPointer(vm.scope, Toy_toString(&vm.memoryBucket, "screenHeight") );
Toy_Value* screenCaptionPtr = Toy_accessScopeAsPointer(vm.scope, Toy_toString(&vm.memoryBucket, "screenCaption") );
int screenWidth = screenWidthPtr != NULL && TOY_VALUE_IS_INTEGER(*screenWidthPtr) ? TOY_VALUE_AS_INTEGER(*screenWidthPtr) : 640;
int screenHeight = screenHeightPtr != NULL && TOY_VALUE_IS_INTEGER(*screenHeightPtr) ? TOY_VALUE_AS_INTEGER(*screenHeightPtr) : 480;
const char* screenCaption = screenCaptionPtr != NULL && TOY_VALUE_IS_STRING(*screenCaptionPtr) ? TOY_VALUE_AS_STRING(*screenCaptionPtr)->leaf.data : "";
InitWindow(screenWidth, screenHeight, screenCaption);
SetTargetFPS(60);
while (!WindowShouldClose()) {
//drawing
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Do you have games on your phone?", 100, 100, 20, LIGHTGRAY);
EndDrawing();
}
CloseWindow();
Toy_freeVM(&vm);
free(code);
return 0;
}
+62
View File
@@ -0,0 +1,62 @@
#compiler settings
CC=gcc
CFLAGS+=-std=c17 -g -Wall -Werror -Wextra -Wpedantic -Wformat=2 -Wno-newline-eof
LIBS+=-lToy -lraylib -lSDL2 -lm
LDFLAGS+=-Wl,-rpath,'$$ORIGIN'
ifeq ($(shell uname),Darwin) #make sure there's enough space for the dylib fix
LDFLAGS+=-Wl,-headerpad_max_install_names
endif
#directories
REPL_ROOTDIR=..
REPL_REPLDIR=.
REPL_SOURCEDIR=$(REPL_ROOTDIR)/$(TOY_SOURCEDIR)
REPL_OUTDIR=$(REPL_ROOTDIR)/$(PAC_OUTDIR)
REPL_OBJDIR=$(TOY_OBJDIR)
#file names
REPL_REPLFILES=$(wildcard $(REPL_REPLDIR)/*.c)
REPL_OBJFILES=$(addprefix $(REPL_OBJDIR)/,$(notdir $(REPL_REPLFILES:.c=.o)))
REPL_TARGETNAME=pactoy
#file extensions
ifeq ($(OS),Windows_NT)
REPL_TARGETEXT=.exe
else
REPL_TARGETEXT=.out
endif
#linker fix
LDFLAGS+=-L$(realpath $(REPL_OUTDIR))
#build the object files, compile the test cases, and run
all: build link
#targets for each step
.PHONY: build
build: $(REPL_OBJDIR) $(REPL_OBJFILES)
.PHONY: link
link: $(REPL_OUTDIR) $(REPL_OUTDIR)/$(REPL_TARGETNAME)$(REPL_TARGETEXT)
#util targets
$(REPL_OUTDIR):
mkdir $(REPL_OUTDIR)
$(REPL_OBJDIR):
mkdir $(REPL_OBJDIR)
#compilation steps
$(REPL_OBJDIR)/%.o: $(REPL_REPLDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(REPL_REPLDIR)) $(addprefix -I,$(REPL_SOURCEDIR)) $(CFLAGS)
$(REPL_OUTDIR)/$(REPL_TARGETNAME)$(REPL_TARGETEXT): $(REPL_OBJFILES)
$(CC) -DTOY_IMPORT $(CFLAGS) -o $@ $(REPL_OBJFILES) $(LDFLAGS) $(LIBS)
ifeq ($(shell uname),Darwin) #dylib fix
otool -L $@
install_name_tool -add_rpath @executable_path/. $@
install_name_tool -change $(REPL_OUTDIR)/libToy.dylib @executable_path/libToy.dylib $@
otool -L $@
endif