From f302448365a36eef4ac04d85271082bdca9d8acc Mon Sep 17 00:00:00 2001
From: Rob Loach <robloach@gmail.com>
Date: Sat, 25 Jan 2025 13:21:51 -0500
Subject: [PATCH] cloean up

---
 src/love/graphics.cpp | 12 ++++++++++--
 src/love/script.cpp   | 31 +++++++++++++++++++++++++++++++
 src/love/sound.cpp    | 14 --------------
 src/love/sound.h      |  1 -
 4 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/src/love/graphics.cpp b/src/love/graphics.cpp
index b52e1e52..b5da78c6 100644
--- a/src/love/graphics.cpp
+++ b/src/love/graphics.cpp
@@ -229,10 +229,18 @@ std::string graphics::getDefaultFilter() {
 
 
 int graphics::getWidth() {
-	return getScreen()->width;
+	pntr_image* screen = getScreen();
+	if (screen != NULL) {
+		return screen->width;
+	}
+	return 800;
 }
 int graphics::getHeight() {
-	return getScreen()->height;
+	pntr_image* screen = getScreen();
+	if (screen != NULL) {
+		return screen->height;
+	}
+	return 600;
 }
 
 Point graphics::getDimensions() {
diff --git a/src/love/script.cpp b/src/love/script.cpp
index 497e16a6..401cd567 100644
--- a/src/love/script.cpp
+++ b/src/love/script.cpp
@@ -109,10 +109,41 @@ std::string script::evalString(const std::string& code, const std::string& filen
 	return chai.eval<std::string>(contents, Exception_Handler(), filename);
 }
 
+void chailove_print(const std::string& message) {
+	pntr_app_log(PNTR_APP_LOG_INFO, message.c_str());
+}
+
+void chailove_dofile(const std::string& filename) {
+	ChaiLove* chailove = ChaiLove::getInstance();
+	const std::string& contents = chailove->filesystem.read(filename);
+	if (!contents.empty()) {
+		chailove->script->evalString(contents, filename);
+	}
+	else {
+		pntr_app_log_ex(PNTR_APP_LOG_ERROR, "[ChaiLove] dofile() script is empty: %s", filename.c_str());
+	}
+}
+
+void chailove_error(const std::string& message) {
+	pntr_app_log(PNTR_APP_LOG_ERROR, message.c_str());
+}
+
+void chailove_error_level(const std::string& message, int level) {
+	(void)level;
+	pntr_app_log(PNTR_APP_LOG_ERROR, message.c_str());
+}
+
 script::script(const std::string& file) {
 	#ifdef __HAVE_CHAISCRIPT__
 	ChaiLove* app = ChaiLove::getInstance();
 
+	// Override some of the global Lua functions
+	// https://www.lua.org/manual/5.4/manual.html#6.1
+	chai.add(fun(&chailove_print), "print");
+	chai.add(fun(&chailove_dofile), "dofile");
+	chai.add(fun(&chailove_error), "error");
+	chai.add(fun(&chailove_error_level), "error");
+
 	// ChaiScript Standard Library Additions
 	// This adds some basic type definitions to ChaiScript.
 	chai.add(bootstrap::standard_library::vector_type<std::vector<int>>("VectorInt"));
diff --git a/src/love/sound.cpp b/src/love/sound.cpp
index 3df32c47..211ed10b 100644
--- a/src/love/sound.cpp
+++ b/src/love/sound.cpp
@@ -14,7 +14,6 @@ using love::Types::Audio::SoundData;
 namespace love {
 
 bool sound::load(pntr_app* application) {
-	//audio_mixer_init(44100);
 	app = application;
 	m_loaded = true;
 	return true;
@@ -36,25 +35,12 @@ void sound::unload() {
 	}
 	sounds.clear();
 
-	// Close the audio mixer.
-	//audio_mixer_done();
 	m_loaded = false;
 	app = NULL;
 }
 
-
 SoundData* sound::newSoundData(const std::string& filename) {
 	return ChaiLove::getInstance()->audio.newSource(filename);
 }
 
-void sound::update() {
-	// int bufferSize = 44100 / 60;
-	// float samples[1470] = { 0 };  // 44100 / 60 * 2
-	// int16_t samples2[1470] = { 0 };  // 44100 / 60 * 2
-	// audio_mixer_mix(samples, bufferSize, 1.0, false);
-	// convert_float_to_s16(samples2, samples, 2 * bufferSize);
-	// audio_batch_cb(samples2, bufferSize);
-}
-
-
 }  // namespace love
diff --git a/src/love/sound.h b/src/love/sound.h
index 3e53bb53..0f8db82f 100644
--- a/src/love/sound.h
+++ b/src/love/sound.h
@@ -16,7 +16,6 @@ class sound {
 	bool isLoaded();
 	bool hasAudio();
 	void unload();
-	void update();
 
 	~sound();