Verified Commit 3c02c554 authored by RobLoach's avatar RobLoach
Browse files

Require love. namespace usage

parent 01ec99e3
......@@ -4,6 +4,12 @@ All notable changes to [ChaiLove](https://github.com/RobLoach/ChaiLove) will be
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## 0.14.0 - 2018-xx-xx - Unreleased
### Changed
- Removed global objects, use `love` namespace instead
- Before: `graphics.draw()`
- After: `love.graphics.draw()`
## 0.13.0 - 2017-12-20
### Added
- Documentation
......
......@@ -13,18 +13,26 @@ def conf(t) {
}
def load() {
graphics.setBackgroundColor(54, 172, 248)
love.graphics.setBackgroundColor(54, 172, 248)
// Add the first sprite.
img = graphics.newImage("sprite.png")
img = love.graphics.newImage("sprite.png")
sprites.push_back(Sprite())
// Load the music.
music = audio.newSource("music.wav")
music = love.audio.newSource("music.wav")
music.setLooping(true)
music.play()
}
/**
* ChaiLove Callback; Restart the content.
*/
def reset() {
sprites.clear()
sprites.push_back(Sprite())
}
def update(delta) {
// Update all the sprites.
for (sprite : sprites) {
......@@ -32,10 +40,10 @@ def update(delta) {
}
// Allow increasing the amount of sprites.
if (joystick[0].isDown("up")) {
if (love.joystick[0].isDown("up")) {
sprites.push_back(Sprite())
}
if (joystick[0].isDown("down") && sprites.size() > 1) {
if (love.joystick[0].isDown("down") && sprites.size() > 1) {
sprites.pop_back()
}
}
......@@ -47,10 +55,10 @@ def draw() {
}
// Write the text on the screen.
graphics.setColor(255, 255, 255)
graphics.print("Sprites: " + to_string(sprites.size()), 5, 5)
graphics.print("Press up/down", 5, 20)
graphics.print("FPS: " + to_string(timer.getFPS()), 5, 35)
love.graphics.setColor(255, 255, 255)
love.graphics.print("Sprites: " + to_string(sprites.size()), 5, 5)
love.graphics.print("Press up/down", 5, 20)
love.graphics.print("FPS: " + to_string(love.timer.getFPS()), 5, 35)
}
def mousepressed(x, y, button) {
......@@ -64,31 +72,31 @@ class Sprite {
var yspeed
def Sprite() {
this.x = math.random(graphics.getWidth()) * 1.0f
this.y = math.random(graphics.getHeight()) * 1.0f
this.xspeed = math.random(10) * 50.0f
this.yspeed = math.random(10) * 50.0f
this.x = love.math.random(love.graphics.getWidth()) * 1.0f
this.y = love.math.random(love.graphics.getHeight()) * 1.0f
this.xspeed = love.math.random(10) * 50.0f
this.yspeed = love.math.random(10) * 50.0f
}
def draw() {
graphics.draw(img, this.x, this.y)
love.graphics.draw(img, this.x, this.y)
}
def update(delta) {
this.x += this.xspeed * delta
this.y += this.yspeed * delta
if (this.x + img.getWidth() > graphics.getWidth()) {
if (this.x + img.getWidth() > love.graphics.getWidth()) {
this.xspeed = this.xspeed * -1.0f
this.x = graphics.getWidth() - img.getWidth()
this.x = love.graphics.getWidth() - img.getWidth()
}
if (this.x < 0) {
this.xspeed = this.xspeed * -1.0f
this.x = 0
}
if (this.y + img.getHeight() > graphics.getHeight()) {
if (this.y + img.getHeight() > love.graphics.getHeight()) {
this.yspeed = this.yspeed * -1.0f
this.y = graphics.getHeight() - img.getHeight()
this.y = love.graphics.getHeight() - img.getHeight()
}
if (this.y < 0) {
this.yspeed = this.yspeed * -1.0f
......
......@@ -17,22 +17,22 @@ class Player {
}
def draw() {
graphics.setColor(170, 130, 57);
graphics.rectangle("fill", this.x, this.y, this.width, this.height);
graphics.setColor(102, 153, 153);
graphics.print(to_string(this.score), this.x, 50);
love.graphics.setColor(170, 130, 57);
love.graphics.rectangle("fill", this.x, this.y, this.width, this.height);
love.graphics.setColor(102, 153, 153);
love.graphics.print(to_string(this.score), this.x, 50);
}
def update(delta) {
if (joystick.isDown(0, "up")) {
if (love.joystick.isDown(0, "up")) {
this.y -= 300.0f * delta
}
else if (joystick.isDown(0, "down")) {
else if (love.joystick.isDown(0, "down")) {
this.y += 300.0f * delta
}
if (this.y + this.height > graphics.getHeight()) {
this.y = graphics.getHeight() - this.height;
if (this.y + this.height > love.graphics.getHeight()) {
this.y = love.graphics.getHeight() - this.height;
}
else if (this.y < 0) {
this.y = 0;
......
......@@ -12,15 +12,15 @@ def conf(t) {
}
def load() {
graphics.setBackgroundColor(0, 51, 51);
pongSound = sound.newSoundData("pong.wav")
love.graphics.setBackgroundColor(0, 51, 51);
pongSound = love.sound.newSoundData("pong.wav")
ballReset()
filesystem.load("Player.chai")
love.filesystem.load("Player.chai")
players = [
Player(false, 80.0f, graphics.getHeight() / 2.0f),
Player(true, graphics.getWidth() - 80.0f, graphics.getHeight() / 2.0f)
Player(false, 80.0f, love.graphics.getHeight() / 2.0f),
Player(true, love.graphics.getWidth() - 80.0f, love.graphics.getHeight() / 2.0f)
]
}
......@@ -29,8 +29,8 @@ def draw() {
for (var i = 0; i < 2; ++i) {
players[i].draw()
}
graphics.setColor(170, 68, 57);
graphics.circle("fill", ball["x"], ball["y"], ball["radius"]);
love.graphics.setColor(170, 68, 57);
love.graphics.circle("fill", ball["x"], ball["y"], ball["radius"]);
}
def update(delta) {
......@@ -44,7 +44,7 @@ def update(delta) {
}
}
else if (ball["yspeed"] > 0) {
if (ball["y"] > graphics.getHeight()) {
if (ball["y"] > love.graphics.getHeight()) {
ball["yspeed"] *= -1;
}
}
......@@ -52,7 +52,7 @@ def update(delta) {
players[1].score += 1
ballReset()
}
else if (ball["x"] > graphics.getWidth()) {
else if (ball["x"] > love.graphics.getWidth()) {
players[0].score += 1
ballReset()
}
......@@ -74,8 +74,8 @@ def update(delta) {
def ballReset() {
ball = [
"x": graphics.getWidth() / 2.0f,
"y": graphics.getHeight() / 2.0f,
"x": love.graphics.getWidth() / 2.0f,
"y": love.graphics.getHeight() / 2.0f,
"xspeed": 200.0f,
"yspeed": 100.0f,
"radius": 5
......@@ -110,6 +110,6 @@ def ballCollide(player) {
}
// Make a sound.
audio.play(pongSound)
love.audio.play(pongSound)
return true
}
......@@ -3,15 +3,15 @@ global x
global y
def load() {
graphics.setBackgroundColor(54, 172, 248)
logo = graphics.newImage("logo.png")
love.graphics.setBackgroundColor(54, 172, 248)
logo = love.graphics.newImage("logo.png")
x = 10.0f
y = 10.0f
}
def draw() {
graphics.print("Hello World!", 100, 100)
graphics.draw(logo, x, y)
love.graphics.print("Hello World!", 100, 100)
love.graphics.draw(logo, x, y)
}
def update(delta) {
......
......@@ -13,13 +13,13 @@ global dir
global t = 0.0f
global T
def conf(config) {
def conf(t) {
WIDTH = 32
HEIGHT = 32
LEN = 5
gridScale = 10
config.window.width = WIDTH * gridScale
config.window.height = HEIGHT * gridScale
t.window.width = WIDTH * gridScale
t.window.height = HEIGHT * gridScale
}
/**
......@@ -33,14 +33,14 @@ def reset() {
}
dir = 0
t = 0
T = [math.random(WIDTH - 2), math.random(HEIGHT - 2)]
T = [love.math.random(WIDTH - 2), love.math.random(HEIGHT - 2)]
}
/**
* ChaiLove Callback; Initialize the game.
*/
def load() {
graphics.setBackgroundColor(133, 188, 22)
love.graphics.setBackgroundColor(133, 188, 22)
reset()
}
......@@ -48,22 +48,22 @@ def load() {
def update(dt) {
t = t + dt
if (joystick[0].isDown("up")) {
if (love.joystick[0].isDown("up")) {
if (dir != 2) {
dir = 1
}
}
if (joystick[0].isDown("down")) {
if (love.joystick[0].isDown("down")) {
if (dir != 1) {
dir = 2
}
}
if (joystick[0].isDown("left")) {
if (love.joystick[0].isDown("left")) {
if (dir != 4) {
dir = 3
}
}
if (joystick[0].isDown("right")) {
if (love.joystick[0].isDown("right")) {
if (dir != 3) {
dir = 4
}
......@@ -110,7 +110,7 @@ def update(dt) {
}
if (head[0] == T[0] && head[1] == T[1]) {
T = [math.random(WIDTH - 2), math.random(HEIGHT - 2)]
T = [love.math.random(WIDTH - 2), love.math.random(HEIGHT - 2)]
}
else if (dir > 0) {
var newS = []
......@@ -122,20 +122,20 @@ def update(dt) {
}
def draw() {
graphics.setColor(134, 201, 0)
love.graphics.setColor(134, 201, 0)
for (var y = 0; y < HEIGHT; ++y) {
for (var x = 0; x < WIDTH; ++x) {
graphics.rectangle("fill", x*gridScale, y*gridScale, gridScale - 2, gridScale - 2)
love.graphics.rectangle("fill", x*gridScale, y*gridScale, gridScale - 2, gridScale - 2)
}
}
graphics.setColor(216, 6, 0)
love.graphics.setColor(216, 6, 0)
for (var i = 0; i < S.size(); ++i) {
graphics.rectangle("fill", S[i][0]*gridScale, S[i][1]*gridScale, gridScale - 2, gridScale - 2)
love.graphics.rectangle("fill", S[i][0]*gridScale, S[i][1]*gridScale, gridScale - 2, gridScale - 2)
}
graphics.setColor(2, 116, 132)
graphics.rectangle("fill", T[0]*gridScale, T[1]*gridScale, gridScale - 2, gridScale - 2)
love.graphics.setColor(2, 116, 132)
love.graphics.rectangle("fill", T[0]*gridScale, T[1]*gridScale, gridScale - 2, gridScale - 2)
}
......
......@@ -71,8 +71,10 @@ script::script(const std::string& file) {
chai.register_namespace([](chaiscript::Namespace& love) {
ChaiLove* app = ChaiLove::getInstance();
love["audio"] = var(std::ref(app->audio));
love["config"] = var(std::ref(app->config));
love["event"] = var(std::ref(app->event));
love["filesystem"] = var(std::ref(app->filesystem));
love["font"] = var(std::ref(app->font));
love["graphics"] = var(std::ref(app->graphics));
love["image"] = var(std::ref(app->image));
love["joystick"] = var(std::ref(app->joystick));
......@@ -155,7 +157,6 @@ script::script(const std::string& file) {
chai.add(fun(&Config::window), "window");
chai.add(fun(&Config::modules), "modules");
chai.add(fun(&Config::options), "options");
chai.add_global(var(std::ref(app->config)), "config");
// Joystick
chai.add(user_type<Joystick>(), "Joystick");
......@@ -206,26 +207,21 @@ script::script(const std::string& file) {
chai.add(fun<love::graphics&, graphics, int, int, int, int>(&graphics::clear), "clear");
chai.add(fun<love::graphics&, graphics, int, int, int>(&graphics::clear), "clear");
chai.add(fun<love::graphics&, graphics>(&graphics::clear), "clear");
chai.add_global(var(std::ref(app->graphics)), "graphics");
// Font
chai.add(fun(&font::isOpen), "isOpen");
chai.add_global(var(std::ref(app->font)), "font");
// Keyboard
chai.add(fun<bool, keyboard, const std::string&>(&keyboard::isDown), "isDown");
chai.add(fun(&keyboard::setKeyRepeat), "setKeyRepeat");
chai.add(fun(&keyboard::getKeyFromScancode), "getKeyFromScancode");
chai.add(fun(&keyboard::getScancodeFromKey), "getScancodeFromKey");
chai.add_global(var(std::ref(app->keyboard)), "keyboard");
// Event
chai.add(fun(&event::quit), "quit");
chai.add_global(var(std::ref(app->event)), "event");
// Image
chai.add(fun(&image::newImageData), "newImageData");
chai.add_global(var(std::ref(app->image)), "image");
// Filesystem
chai.add(fun(&filesystem::unmount), "unmount");
......@@ -239,13 +235,11 @@ script::script(const std::string& file) {
chai.add(fun<std::vector<std::string>, filesystem, const std::string&>(&filesystem::lines), "lines");
chai.add(fun<std::vector<std::string>, filesystem, const std::string&, const std::string&>(&filesystem::lines), "lines");
chai.add(fun(&filesystem::load), "load");
chai.add_global(var(std::ref(app->filesystem)), "filesystem");
// System
chai.add(fun(&system::getOS), "getOS");
chai.add(fun(&system::getVersion), "getVersion");
chai.add(fun(&system::getVersionString), "getVersionString");
chai.add_global(var(std::ref(app->system)), "system");
// Mouse
chai.add(fun(&mouse::setVisible), "setVisible");
......@@ -257,29 +251,24 @@ script::script(const std::string& file) {
chai.add(fun(&mouse::getPosition), "getPosition");
chai.add(fun<bool, mouse, const std::string&>(&mouse::isDown), "isDown");
chai.add(fun<bool, mouse, int>(&mouse::isDown), "isDown");
chai.add_global(var(std::ref(app->mouse)), "mouse");
// Sound
chai.add(fun<SoundData*, sound, const std::string&>(&sound::newSoundData), "newSoundData");
chai.add_global(var(std::ref(app->sound)), "sound");
// Audio
chai.add(fun(&audio::play), "play");
chai.add(fun<SoundData*, audio, const std::string&>(&audio::newSource), "newSource");
chai.add(fun(&audio::getVolume), "getVolume");
chai.add(fun(&audio::setVolume), "setVolume");
chai.add_global(var(std::ref(app->audio)), "audio");
// Window
chai.add(fun(&window::setTitle), "setTitle");
chai.add(fun(&window::getTitle), "getTitle");
chai.add_global(var(std::ref(app->window)), "window");
// Timer
chai.add(fun(&timer::getDelta), "getDelta");
chai.add(fun(&timer::getFPS), "getFPS");
chai.add(fun(&timer::step), "step");
chai.add_global(var(std::ref(app->timer)), "timer");
// Joystick
chai.add(fun(&joystick::getJoysticks), "getJoysticks");
......@@ -287,7 +276,6 @@ script::script(const std::string& file) {
chai.add(fun<bool, joystick, int, const std::string&>(&joystick::isDown), "isDown");
chai.add(fun<bool, joystick, int, int>(&joystick::isDown), "isDown");
chai.add(fun(&joystick::operator[]), "[]");
chai.add_global(var(std::ref(app->joystick)), "joystick");
// Math
auto mathlib = chaiscript::extras::math::bootstrap();
......@@ -309,7 +297,6 @@ script::script(const std::string& file) {
chai.add(fun<std::string, math, const std::string&>(&math::compress), "compress");
chai.add(fun<std::string, math, const std::string&, int>(&math::compress), "compress");
chai.add(fun(&math::decompress), "decompress");
chai.add_global(var(std::ref(app->math)), "math");
// Ensure the love namespace is imported and ready.
chai.import("love");
......
......@@ -10,14 +10,14 @@ class filesystem_getDirectoryItems {
def load() {
this.x = 100;
this.y = graphics.getHeight() / 3.0f;
this.files = filesystem.getDirectoryItems("/")
this.y = love.graphics.getHeight() / 3.0f;
this.files = love.filesystem.getDirectoryItems("/")
}
def draw() {
var y = this.y
for (file : this.files) {
graphics.print("- " + file, this.x, y)
love.graphics.print("- " + file, this.x, y)
y += 10
}
}
......
......@@ -9,13 +9,13 @@ class graphics_draw {
}
def load() {
this.image = graphics.newImage("assets/graphics_draw.png");
this.x = graphics.getWidth() / 2.0f - this.image.getWidth() / 2.0f;
this.y = graphics.getHeight() / 2.0f - this.image.getHeight() / 2.0f;
this.image = love.graphics.newImage("assets/graphics_draw.png");
this.x = love.graphics.getWidth() / 2.0f - this.image.getWidth() / 2.0f;
this.y = love.graphics.getHeight() / 2.0f - this.image.getHeight() / 2.0f;
}
def draw() {
graphics.draw(this.image, this.x, this.y);
love.graphics.draw(this.image, this.x, this.y);
}
def update(delta) {
......
......@@ -11,7 +11,7 @@ class graphics_point {
def draw() {
//graphics.setColor(0, 255, 0)
graphics.point(100, 200).point(200, 300)
love.graphics.point(100, 200).point(200, 300)
}
def update(delta) {
......
......@@ -9,12 +9,12 @@ class graphics_print {
def load() {
this.x = 100;
this.y = graphics.getHeight() / 2.0f - 30;
this.y = love.graphics.getHeight() / 2.0f - 30;
}
def draw() {
graphics.setColor(0, 255, 0)
graphics.print("Press left/right to switch tests", this.x, this.y);
love.graphics.setColor(0, 255, 0)
love.graphics.print("Press left/right to switch tests", this.x, this.y);
}
def update(delta) {
......
......@@ -8,27 +8,27 @@ class graphics_print_font {
}
def load() {
this.ttffont = graphics.newFont("assets/Raleway-Regular.ttf", 40)
this.pngfont = graphics.newFont("assets/c64_16x16.png", 16, 16, "\x7f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
this.ttffont = love.graphics.newFont("assets/Raleway-Regular.ttf", 40)
this.pngfont = love.graphics.newFont("assets/c64_16x16.png", 16, 16, "\x7f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
}
def draw() {
// Default font.
graphics.setFont();
love.graphics.setFont();
var text = "Default Font"
graphics.setColor(10, 230, 0)
graphics.print(text, 100, 100)
love.graphics.setColor(10, 230, 0)
love.graphics.print(text, 100, 100)
graphics.setFont(this.ttffont)
love.graphics.setFont(this.ttffont)
text = "Raleway-Regular.ttf"
graphics.setColor(255, 200, 0)
graphics.print(text, 100, 200)
love.graphics.setColor(255, 200, 0)
love.graphics.print(text, 100, 200)
graphics.setFont(this.pngfont)
love.graphics.setFont(this.pngfont)
text = "Commodore 64"
var width = this.pngfont.getWidth(text)
var height = this.pngfont.getHeight()
graphics.print(text, graphics.getWidth() / 2.0f - width / 2.0f, graphics.getHeight() - height)
love.graphics.print(text, love.graphics.getWidth() / 2.0f - width / 2.0f, love.graphics.getHeight() - height)
}
def update(delta) {}
......
......@@ -11,7 +11,6 @@ global testNames = [
]
global tests = []
global currenttest = 0
global defaultFont
def conf(t) {
t.window.width = 460
......@@ -21,7 +20,7 @@ def conf(t) {
def load() {
// Index the test cases.
for (name : testNames) {
filesystem.load(name + ".chai");
love.filesystem.load(name + ".chai");
tests.push_back(eval(name + "()"));
}
......@@ -29,18 +28,16 @@ def load() {
for (test : tests) {
test.load();
}
// Engage the default fault.
defaultFont = graphics.newFont()
}
def draw() {
tests[currenttest].draw();
graphics.setFont(defaultFont)
graphics.setColor(255, 255, 255)
graphics.print("Test " + to_string(currenttest + 1) + "/" + to_string(tests.size()) + ": " + tests[currenttest].name, 5, 5);
graphics.print("FPS " + to_string(timer.getFPS()), 5, 20);
// Use the default font.
love.graphics.setFont()
love.graphics.setColor(255, 255, 255)
love.graphics.print("Test " + to_string(currenttest + 1) + "/" + to_string(tests.size()) + ": " + tests[currenttest].name, 5, 5);
love.graphics.print("FPS " + to_string(love.timer.getFPS()), 5, 20);
}
def update(delta) {
......
......@@ -8,10 +8,10 @@ class mouse_getPosition {
def load() {}
def draw() {
graphics.setColor(255, 150, 0)
var pos = mouse.getPosition()
love.graphics.setColor(255, 150, 0)
var pos = love.mouse.getPosition()
var output = "(" + to_string(pos.x) + "," + to_string(pos.y) + ")"
graphics.print(output, pos.x, pos.y);
love.graphics.print(output, pos.x, pos.y);
}
def update(delta) {}
......
......@@ -6,7 +6,7 @@ global scale = 1.0f
def load() {
logo = love.image.newImageData("assets/graphics_draw.png")
graphics.setBackgroundColor(50, 50, 100)
love.graphics.setBackgroundColor(50, 50, 100)
}
def update(dt) {
......@@ -21,10 +21,10 @@ def update(dt) {
}
def draw() {
graphics.print("Key Pressed: " + keyPressed, 5, 20)
graphics.print("Key Released: " + keyReleased, 5, 100)
love.graphics.print("Key Pressed: " + keyPressed, 5, 20)
love.graphics.print("Key Released: " + keyReleased, 5, 100)
graphics.setColor(255, 0, 0)
love.graphics.setColor(255, 0, 0)
//love.graphics.draw(logo, graphics.getWidth() / 2, graphics.getHeight() / 2, math.rad(angle), 2, 2, 64/2, 64/2)
love.graphics.point(graphics.getWidth() / 2.0f, graphics.getHeight() / 2.0f)
love.graphics.draw(logo, graphics.getWidth() / 2.0f, graphics.getHeight() / 2.0f, math.rad(angle), scale, scale, logo.getWidth() / 2.0f, logo.getHeight() / 2.0f)
......
// Filesystem
var files = filesystem.getDirectoryItems("/")
var files = love.filesystem.getDirectoryItems("/")
var foundUnitChai = false
for (file : files) {
......@@ -10,19 +10,19 @@ for (file : files) {
assert(foundUnitChai, "filesystem.getDirectoryItems()")
var contents = filesystem.read("main.chai")
var contents = love.filesystem.read("main.chai")
var needle = "Unit Testing Framework"
var found = contents.find(needle)
assert(found > 50, "filesystem.read()")
assert(filesystem.exists("math.chai"), "filesystem.exists('math.chai')")
assert(!filesystem.exists("notexist.chai"), "filesystem.exists('notexist.chai')")
assert(love.filesystem.exists("math.chai"), "filesystem.exists('math.chai')")
assert(!love.filesystem.exists("notexist.chai"), "filesystem.exists('notexist.chai')")
assert(filesystem.getSize("filesystem.chai") > 10, "filesystem.getSize()")
assert_equal(filesystem.getSize("notexist.chai"), -1, "filesystem.getSize('notexist.chai')")
assert(love.filesystem.getSize("filesystem.chai") > 10, "filesystem.getSize()")
assert_equal(love.filesystem.getSize("notexist.chai"), -1, "filesystem.getSize('notexist.chai')")
assert(filesystem.isFile("filesystem.chai"), "filesystem.isFile()")
assert(!filesystem.isFile("notexist.chai"), "filesystem.isFile('notexist.chai')")
assert(love.filesystem.isFile("filesystem.chai"), "filesystem.isFile()")
assert(!love.filesystem.isFile("notexist.chai"), "filesystem.isFile('notexist.chai')")