From 168154d25b48d9b353711197a512b9a8b603dca8 Mon Sep 17 00:00:00 2001 From: Anatoli Steinmark Date: Sat, 26 Nov 2011 16:22:11 +0100 Subject: [PATCH] restructure project to reduce number of directories --- projects/vs/.gitignore | 1 - projects/vs/engine/engine.vcxproj | 20 +- projects/vs/engine/engine.vcxproj.filters | 83 ++ src/{video => graphics}/ITexture.hpp | 0 src/{image => graphics}/corona/image.cpp | 0 src/{image => graphics}/image.hpp | 0 .../sdl_opengl/Texture.cpp | 0 .../sdl_opengl/Texture.hpp | 0 src/{video => graphics}/sdl_opengl/video.cpp | 0 src/{video => graphics}/video.hpp | 0 src/{filesystem => io}/File.cpp | 0 src/{filesystem => io}/File.hpp | 0 src/{filesystem => io}/boost/filesystem.cpp | 0 src/io/{io.cpp => data.cpp} | 2 +- src/io/{io.hpp => data.hpp} | 4 +- src/{filesystem => io}/filesystem.hpp | 0 src/main.cpp | 6 +- src/script/script.cpp | 1254 ++++++++--------- src/script/script.hpp | 8 +- src/{audio => sound}/ISound.hpp | 0 src/{audio => sound}/ISoundEffect.hpp | 0 src/{audio => sound}/audiere/Sound.cpp | 0 src/{audio => sound}/audiere/Sound.hpp | 0 src/{audio => sound}/audiere/SoundEffect.cpp | 0 src/{audio => sound}/audiere/SoundEffect.hpp | 0 src/{audio => sound}/audiere/audio.cpp | 0 src/{audio => sound}/audio.hpp | 0 src/{compression => util}/ZStream.cpp | 0 src/{compression => util}/ZStream.hpp | 0 29 files changed, 687 insertions(+), 691 deletions(-) create mode 100644 projects/vs/engine/engine.vcxproj.filters rename src/{video => graphics}/ITexture.hpp (100%) rename src/{image => graphics}/corona/image.cpp (100%) rename src/{image => graphics}/image.hpp (100%) rename src/{video => graphics}/sdl_opengl/Texture.cpp (100%) rename src/{video => graphics}/sdl_opengl/Texture.hpp (100%) rename src/{video => graphics}/sdl_opengl/video.cpp (100%) rename src/{video => graphics}/video.hpp (100%) rename src/{filesystem => io}/File.cpp (100%) rename src/{filesystem => io}/File.hpp (100%) rename src/{filesystem => io}/boost/filesystem.cpp (100%) rename src/io/{io.cpp => data.cpp} (99%) rename src/io/{io.hpp => data.hpp} (96%) rename src/{filesystem => io}/filesystem.hpp (100%) rename src/{audio => sound}/ISound.hpp (100%) rename src/{audio => sound}/ISoundEffect.hpp (100%) rename src/{audio => sound}/audiere/Sound.cpp (100%) rename src/{audio => sound}/audiere/Sound.hpp (100%) rename src/{audio => sound}/audiere/SoundEffect.cpp (100%) rename src/{audio => sound}/audiere/SoundEffect.hpp (100%) rename src/{audio => sound}/audiere/audio.cpp (100%) rename src/{audio => sound}/audio.hpp (100%) rename src/{compression => util}/ZStream.cpp (100%) rename src/{compression => util}/ZStream.hpp (100%) diff --git a/projects/vs/.gitignore b/projects/vs/.gitignore index 02d4c63..2d74c84 100644 --- a/projects/vs/.gitignore +++ b/projects/vs/.gitignore @@ -3,7 +3,6 @@ *.suo *.user *.opensdf -*.filters Release/* Debug/* */Release/* diff --git a/projects/vs/engine/engine.vcxproj b/projects/vs/engine/engine.vcxproj index f69b826..ae932cc 100644 --- a/projects/vs/engine/engine.vcxproj +++ b/projects/vs/engine/engine.vcxproj @@ -11,26 +11,26 @@ - - - - - - - + + + + + - + + + + - - + diff --git a/projects/vs/engine/engine.vcxproj.filters b/projects/vs/engine/engine.vcxproj.filters new file mode 100644 index 0000000..edce81f --- /dev/null +++ b/projects/vs/engine/engine.vcxproj.filters @@ -0,0 +1,83 @@ + + + + + + + + sound + + + io + + + + graphics + + + io + + + io + + + graphics + + + input + + + io + + + sound + + + sound + + + system + + + io + + + script + + + util + + + graphics + + + graphics + + + + + + + + {3a4cae03-f45b-4ea2-80e4-5b5758452450} + + + {6b003bb2-697f-48ac-b21e-e8df78d31c69} + + + {ef8ced1d-0c53-4f75-930a-443192126360} + + + {8643e61f-a487-463a-9fde-d3f54fc1bcf4} + + + {c6a3113e-2674-4b0d-8b83-5c442eba0bd6} + + + {b236274d-ca70-42b4-9a61-c7d6e703e6e7} + + + {c4ac175d-c8a2-4b8f-81aa-b6a3fb2e2e49} + + + \ No newline at end of file diff --git a/src/video/ITexture.hpp b/src/graphics/ITexture.hpp similarity index 100% rename from src/video/ITexture.hpp rename to src/graphics/ITexture.hpp diff --git a/src/image/corona/image.cpp b/src/graphics/corona/image.cpp similarity index 100% rename from src/image/corona/image.cpp rename to src/graphics/corona/image.cpp diff --git a/src/image/image.hpp b/src/graphics/image.hpp similarity index 100% rename from src/image/image.hpp rename to src/graphics/image.hpp diff --git a/src/video/sdl_opengl/Texture.cpp b/src/graphics/sdl_opengl/Texture.cpp similarity index 100% rename from src/video/sdl_opengl/Texture.cpp rename to src/graphics/sdl_opengl/Texture.cpp diff --git a/src/video/sdl_opengl/Texture.hpp b/src/graphics/sdl_opengl/Texture.hpp similarity index 100% rename from src/video/sdl_opengl/Texture.hpp rename to src/graphics/sdl_opengl/Texture.hpp diff --git a/src/video/sdl_opengl/video.cpp b/src/graphics/sdl_opengl/video.cpp similarity index 100% rename from src/video/sdl_opengl/video.cpp rename to src/graphics/sdl_opengl/video.cpp diff --git a/src/video/video.hpp b/src/graphics/video.hpp similarity index 100% rename from src/video/video.hpp rename to src/graphics/video.hpp diff --git a/src/filesystem/File.cpp b/src/io/File.cpp similarity index 100% rename from src/filesystem/File.cpp rename to src/io/File.cpp diff --git a/src/filesystem/File.hpp b/src/io/File.hpp similarity index 100% rename from src/filesystem/File.hpp rename to src/io/File.hpp diff --git a/src/filesystem/boost/filesystem.cpp b/src/io/boost/filesystem.cpp similarity index 100% rename from src/filesystem/boost/filesystem.cpp rename to src/io/boost/filesystem.cpp diff --git a/src/io/io.cpp b/src/io/data.cpp similarity index 99% rename from src/io/io.cpp rename to src/io/data.cpp index 2df2043..2664aca 100644 --- a/src/io/io.cpp +++ b/src/io/data.cpp @@ -2,7 +2,7 @@ #include #include "../common/platform.hpp" #include "endian.hpp" -#include "io.hpp" +#include "data.hpp" //----------------------------------------------------------------- diff --git a/src/io/io.hpp b/src/io/data.hpp similarity index 96% rename from src/io/io.hpp rename to src/io/data.hpp index 7af6d74..72474f1 100644 --- a/src/io/io.hpp +++ b/src/io/data.hpp @@ -1,5 +1,5 @@ -#ifndef IO_HPP -#define IO_HPP +#ifndef DATA_HPP +#define DATA_HPP #include "../common/types.hpp" #include "IStream.hpp" diff --git a/src/filesystem/filesystem.hpp b/src/io/filesystem.hpp similarity index 100% rename from src/filesystem/filesystem.hpp rename to src/io/filesystem.hpp diff --git a/src/main.cpp b/src/main.cpp index 957c4e2..7af4498 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,10 +5,10 @@ #include "version.hpp" #include "Log.hpp" #include "Config.hpp" -#include "filesystem/filesystem.hpp" #include "system/system.hpp" -#include "video/video.hpp" -#include "audio/audio.hpp" +#include "io/filesystem.hpp" +#include "graphics/video.hpp" +#include "sound/audio.hpp" #include "input/input.hpp" #include "script/script.hpp" diff --git a/src/script/script.cpp b/src/script/script.cpp index dd56fee..29d9480 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -8,16 +8,14 @@ #include #include #include "../common/ArrayPtr.hpp" -#include "../Log.hpp" #include "../error.hpp" -#include "../io/endian.hpp" -#include "../io/io.hpp" -#include "../io/Blob.hpp" -#include "../image/image.hpp" #include "../system/system.hpp" -#include "../filesystem/filesystem.hpp" -#include "../video/video.hpp" -#include "../audio/audio.hpp" +#include "../io/endian.hpp" +#include "../io/data.hpp" +#include "../io/filesystem.hpp" +#include "../graphics/image.hpp" +#include "../graphics/video.hpp" +#include "../sound/audio.hpp" #include "../input/input.hpp" #include "script.hpp" #include "typetags.hpp" @@ -159,7 +157,7 @@ SQRESULT ThrowError(const char* format, ...) * * ******************************************************************/ -/***************************** RECT *******************************/ +/************************* RECT OBJECT ****************************/ #define SETUP_RECT_OBJECT() \ Recti* This = 0; \ @@ -462,7 +460,7 @@ static void init_rect_class() sq_poptop(g_VM); // pop class } -/***************************** VEC2 *******************************/ +/************************* VEC2 OBJECT ****************************/ #define SETUP_VEC2_OBJECT() \ Vec2i* This = 0; \ @@ -936,7 +934,7 @@ static void register_core_api() * * ******************************************************************/ -/***************************** STREAM *****************************/ +/************************* STREAM INTERFACE ***********************/ #define SETUP_STREAM_OBJECT() \ IStream* This = 0; \ @@ -1820,173 +1818,440 @@ static void init_blob_class() sq_poptop(g_VM); // pop class } -/*********************** GLOBAL FUNCTIONS *************************/ +/*************************** FILE OBJECT **************************/ -//----------------------------------------------------------------- -static ScriptFuncReg script_io_functions[] = { - {0,0} -}; +#define SETUP_FILE_OBJECT() \ + IFile* This = 0; \ + if (SQ_FAILED(sq_getinstanceup(v, 1, (SQUserPointer*)&This, TT_FILE))) { \ + THROW_ERROR("Invalid type of environment object, expected a File instance") \ + } //----------------------------------------------------------------- -static ScriptConstReg script_io_constants[] = { - {0,0} -}; +static SQInteger script_file_destructor(SQUserPointer p, SQInteger size) +{ + assert(p); + ((IFile*)p)->drop(); + return 0; +} //----------------------------------------------------------------- -static void register_io_api() +// File.getName() +static SQInteger script_file_getName(HSQUIRRELVM v) { - // init classes - init_stream_class(); // Stream must be initialized before any class that inherits from it - init_blob_class(); - - // register classes - sq_pushstring(g_VM, "Stream", -1); - sq_pushobject(g_VM, g_StreamClass); - sq_newslot(g_VM, -3, SQFalse); - - sq_pushstring(g_VM, "Blob", -1); - sq_pushobject(g_VM, g_BlobClass); - sq_newslot(g_VM, -3, SQFalse); + SETUP_FILE_OBJECT() + RET_STRING(This->getName().c_str()) +} - // register functions - for (int i = 0; script_io_functions[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_io_functions[i].funcname, -1); - sq_newclosure(g_VM, script_io_functions[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_io_functions[i].debugname); - sq_newslot(g_VM, -3, SQFalse); - } +//----------------------------------------------------------------- +// File._typeof() +static SQInteger script_file__typeof(HSQUIRRELVM v) +{ + SETUP_FILE_OBJECT() + RET_STRING("File") +} - // register constants - for (int i = 0; script_io_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_io_constants[i].name, -1); - sq_pushinteger(g_VM, script_io_constants[i].value); - sq_newslot(g_VM, -3, SQFalse); +//----------------------------------------------------------------- +// File._tostring() +static SQInteger script_file__tostring(HSQUIRRELVM v) +{ + SETUP_FILE_OBJECT() + std::ostringstream oss; + oss << "getName() << "\""; + } else { + oss << "N/A"; } + oss << ")>"; + RET_STRING(oss.str().c_str()) } -/****************************************************************** - * * - * GRAPHICS * - * * - ******************************************************************/ - -/**************************** CANVAS *****************************/ +//----------------------------------------------------------------- +void CreateFileDerivedClass() +{ + sq_pushobject(g_VM, g_FileClass); + bool succeeded = SQ_SUCCEEDED(sq_newclass(g_VM, SQTrue)); + assert(succeeded); +} -#define SETUP_CANVAS_OBJECT() \ - Canvas* This = 0; \ - if (SQ_FAILED(sq_getinstanceup(v, 1, (SQUserPointer*)&This, TT_CANVAS))) { \ - THROW_ERROR("Invalid type of environment object, expected a Canvas instance") \ - } +//----------------------------------------------------------------- +void BindFile(IFile* file) +{ + assert(g_FileClass._type == OT_CLASS); + assert(file); + sq_pushobject(g_VM, g_FileClass); // push file class + sq_createinstance(g_VM, -1); + sq_remove(g_VM, -2); // pop file class + sq_setreleasehook(g_VM, -1, script_file_destructor); + sq_setinstanceup(g_VM, -1, (SQUserPointer)file); + file->grab(); // grab a new reference +} //----------------------------------------------------------------- -static SQInteger script_canvas_destructor(SQUserPointer p, SQInteger size) +IFile* GetFile(SQInteger idx) { - assert(p); - ((Canvas*)p)->drop(); + SQUserPointer p = 0; + if (SQ_SUCCEEDED(sq_getinstanceup(g_VM, idx, &p, TT_FILE))) { + return (IFile*)p; + } return 0; } //----------------------------------------------------------------- -// Canvas(width, height [, pixels]) -static SQInteger script_canvas_constructor(HSQUIRRELVM v) +static ScriptFuncReg script_file_methods[] = { + {"getName", "File.getName", script_file_getName }, + {"_typeof", "File._typeof", script_file__typeof }, + {"_tostring", "File._tostring", script_file__tostring }, + {0,0} +}; + +//----------------------------------------------------------------- +static ScriptFuncReg script_file_static_methods[] = { + {0,0} +}; + +//----------------------------------------------------------------- +static ScriptConstReg script_file_constants[] = { + {"IN", IFile::IN }, + {"OUT", IFile::OUT }, + {"APPEND", IFile::APPEND }, + {0,0} +}; + +//----------------------------------------------------------------- +static void init_file_class() { - SETUP_CANVAS_OBJECT() - CHECK_MIN_NARGS(2) - GET_ARG_INT(1, width) - GET_ARG_INT(2, height) - GET_OPTARG_BLOB(3, pixels) - if (width <= 0) { - THROW_ERROR("Invalid width") - } - if (height <= 0) { - THROW_ERROR("Invalid height") + CreateStreamDerivedClass(); // inherit from Stream + sq_newclass(g_VM, SQTrue); // create class + sq_settypetag(g_VM, -1, TT_FILE); + + // define methods + for (int i = 0; script_file_methods[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_file_methods[i].funcname, -1); + sq_newclosure(g_VM, script_file_methods[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_file_methods[i].debugname); + sq_newslot(g_VM, -3, SQFalse); } - if (pixels && pixels->getSize() != width * height * Canvas::GetNumBytesPerPixel()) { - THROW_ERROR("Invalid pixels") + + // define static methods + for (int i = 0; script_file_static_methods[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_file_static_methods[i].funcname, -1); + sq_newclosure(g_VM, script_file_static_methods[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_file_static_methods[i].debugname); + sq_newslot(g_VM, -3, SQTrue); } - This = Canvas::Create(width, height); - if (pixels) { - memcpy(This->getPixels(), pixels->getBuffer(), pixels->getSize()); + + // define constants + for (int i = 0; script_file_constants[i].name != 0; i++) { + sq_pushstring(g_VM, script_file_constants[i].name, -1); + sq_pushinteger(g_VM, script_file_constants[i].value); + sq_newslot(g_VM, -3, SQTrue); } - sq_setinstanceup(v, 1, (SQUserPointer)This); - sq_setreleasehook(v, 1, script_canvas_destructor); - RET_VOID() + + // get a strong reference + sq_resetobject(&g_FileClass); + sq_getstackobj(g_VM, -1, &g_FileClass); + sq_addref(g_VM, &g_FileClass); + + sq_poptop(g_VM); // pop class } +/*********************** GLOBAL FUNCTIONS *************************/ + //----------------------------------------------------------------- -// Canvas.Load(filename) -static SQInteger script_canvas_Load(HSQUIRRELVM v) +// OpenFile(filename [, mode = File.IN]) +static SQInteger script_OpenFile(HSQUIRRELVM v) { - CHECK_NARGS(1) + CHECK_MIN_NARGS(1) GET_ARG_STRING(1, filename) - FilePtr file = OpenFile(filename); + GET_OPTARG_INT(2, mode, IFile::IN) + if (mode != IFile::IN && + mode != IFile::OUT && + mode != IFile::APPEND) + { + THROW_ERROR("Invalid mode") + } + FilePtr file = OpenFile(filename, mode); if (!file) { THROW_ERROR("Could not open file") } - CanvasPtr image = LoadImage(file.get()); - if (!image) { - THROW_ERROR("Could not load image") - } - RET_CANVAS(image.get()) + RET_FILE(file.get()) } //----------------------------------------------------------------- -// Canvas.LoadFromStream(stream) -static SQInteger script_canvas_LoadFromStream(HSQUIRRELVM v) +// DoesFileExist(filename) +static SQInteger script_DoesFileExist(HSQUIRRELVM v) { CHECK_NARGS(1) - GET_ARG_STREAM(1, stream) - if (!stream->isOpen() || !stream->isReadable()) { - THROW_ERROR("Invalid stream") - } - CanvasPtr image = LoadImage(stream); - if (!image) { - THROW_ERROR("Could not load image") - } - RET_CANVAS(image.get()) + GET_ARG_STRING(1, filename) + RET_BOOL(DoesFileExist(filename)) } //----------------------------------------------------------------- -// Canvas.save(filename) -static SQInteger script_canvas_save(HSQUIRRELVM v) +// IsRegularFile(filename) +static SQInteger script_IsRegularFile(HSQUIRRELVM v) { - SETUP_CANVAS_OBJECT() CHECK_NARGS(1) GET_ARG_STRING(1, filename) - FilePtr file = OpenFile(filename, IFile::OUT); - if (!file) { - THROW_ERROR("Could not open file") - } - if (!SaveImage(This, file.get())) { - THROW_ERROR("Could not save image") - } - RET_VOID() + RET_BOOL(IsRegularFile(filename)) } //----------------------------------------------------------------- -// Canvas.saveToStream(stream) -static SQInteger script_canvas_saveToStream(HSQUIRRELVM v) +// IsDirectory(filename) +static SQInteger script_IsDirectory(HSQUIRRELVM v) { - SETUP_CANVAS_OBJECT() CHECK_NARGS(1) - GET_ARG_STREAM(1, stream) - if (!stream->isOpen() || !stream->isReadable()) { - THROW_ERROR("Invalid stream") - } - if (!SaveImage(This, stream)) { - THROW_ERROR("Could not save image") - } - RET_VOID() + GET_ARG_STRING(1, filename) + RET_BOOL(IsDirectory(filename)) } //----------------------------------------------------------------- -// Canvas.getPixels() -static SQInteger script_canvas_getPixels(HSQUIRRELVM v) +// GetFileModTime(filename) +static SQInteger script_GetFileModTime(HSQUIRRELVM v) { - SETUP_CANVAS_OBJECT() - BlobPtr pixels = Blob::Create(This->getNumPixels() * Canvas::GetNumBytesPerPixel()); - memcpy(pixels->getBuffer(), This->getPixels(), pixels->getSize()); - RET_BLOB(pixels.get()) + CHECK_NARGS(1) + GET_ARG_STRING(1, filename) + RET_INT(GetFileModTime(filename)) +} + +//----------------------------------------------------------------- +// CreateDirectory(directory) +static SQInteger script_CreateDirectory(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_STRING(1, directory) + if (!CreateDirectory(directory)) { + THROW_ERROR("Could not create directory") + } + RET_VOID() +} + +//----------------------------------------------------------------- +// RemoveFile(filename) +static SQInteger script_RemoveFile(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_STRING(1, filename) + if (!RemoveFile(filename)) { + THROW_ERROR("Could not remove file or directory") + } + RET_VOID() +} + +//----------------------------------------------------------------- +// RenameFile(filenameFrom, filenameTo) +static SQInteger script_RenameFile(HSQUIRRELVM v) +{ + CHECK_NARGS(2) + GET_ARG_STRING(1, filenameFrom) + GET_ARG_STRING(2, filenameTo) + if (!RenameFile(filenameFrom, filenameTo)) { + THROW_ERROR("Could not rename file or directory") + } + RET_VOID() +} + +//----------------------------------------------------------------- +// GetFileList(directory) +static SQInteger script_GetFileList(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_STRING(1, directory) + std::vector file_list; + if (!GetFileList(directory, file_list)) { + THROW_ERROR("Could not get file list") + } + sq_newarray(v, file_list.size()); + if (file_list.size() > 0) { + for (int i = 0; i < (int)file_list.size(); ++i) { + sq_pushinteger(v, i); + sq_pushstring(v, file_list[i].c_str(), -1); + sq_rawset(v, -3); + } + } + return 1; +} + +//----------------------------------------------------------------- +static ScriptFuncReg script_io_functions[] = { + {"OpenFile", "OpenFile", script_OpenFile }, + {"DoesFileExist", "DoesFileExist", script_DoesFileExist }, + {"IsRegularFile", "IsRegularFile", script_IsRegularFile }, + {"IsDirectory", "IsDirectory", script_IsDirectory }, + {"GetFileModTime", "GetFileModTime", script_GetFileModTime }, + {"CreateDirectory", "CreateDirectory", script_CreateDirectory }, + {"RemoveFile", "RemoveFile", script_RemoveFile }, + {"RenameFile", "RenameFile", script_RenameFile }, + {"GetFileList", "GetFileList", script_GetFileList }, + {0,0} +}; + +//----------------------------------------------------------------- +static ScriptConstReg script_io_constants[] = { + {0,0} +}; + +//----------------------------------------------------------------- +static void register_io_api() +{ + // init classes + init_stream_class(); // Stream must be initialized before any class that inherits from it + init_blob_class(); // requires Stream to be initialized first + init_file_class(); // requires Stream to be initialized first + + // register classes + sq_pushstring(g_VM, "Stream", -1); + sq_pushobject(g_VM, g_StreamClass); + sq_newslot(g_VM, -3, SQFalse); + + sq_pushstring(g_VM, "Blob", -1); + sq_pushobject(g_VM, g_BlobClass); + sq_newslot(g_VM, -3, SQFalse); + + sq_pushstring(g_VM, "File", -1); + sq_pushobject(g_VM, g_FileClass); + sq_newslot(g_VM, -3, SQFalse); + + // register functions + for (int i = 0; script_io_functions[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_io_functions[i].funcname, -1); + sq_newclosure(g_VM, script_io_functions[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_io_functions[i].debugname); + sq_newslot(g_VM, -3, SQFalse); + } + + // register constants + for (int i = 0; script_io_constants[i].name != 0; i++) { + sq_pushstring(g_VM, script_io_constants[i].name, -1); + sq_pushinteger(g_VM, script_io_constants[i].value); + sq_newslot(g_VM, -3, SQFalse); + } +} + +/****************************************************************** + * * + * GRAPHICS * + * * + ******************************************************************/ + +/************************* CANVAS OBJECT **************************/ + +#define SETUP_CANVAS_OBJECT() \ + Canvas* This = 0; \ + if (SQ_FAILED(sq_getinstanceup(v, 1, (SQUserPointer*)&This, TT_CANVAS))) { \ + THROW_ERROR("Invalid type of environment object, expected a Canvas instance") \ + } + +//----------------------------------------------------------------- +static SQInteger script_canvas_destructor(SQUserPointer p, SQInteger size) +{ + assert(p); + ((Canvas*)p)->drop(); + return 0; +} + +//----------------------------------------------------------------- +// Canvas(width, height [, pixels]) +static SQInteger script_canvas_constructor(HSQUIRRELVM v) +{ + SETUP_CANVAS_OBJECT() + CHECK_MIN_NARGS(2) + GET_ARG_INT(1, width) + GET_ARG_INT(2, height) + GET_OPTARG_BLOB(3, pixels) + if (width <= 0) { + THROW_ERROR("Invalid width") + } + if (height <= 0) { + THROW_ERROR("Invalid height") + } + if (pixels && pixels->getSize() != width * height * Canvas::GetNumBytesPerPixel()) { + THROW_ERROR("Invalid pixels") + } + This = Canvas::Create(width, height); + if (pixels) { + memcpy(This->getPixels(), pixels->getBuffer(), pixels->getSize()); + } + sq_setinstanceup(v, 1, (SQUserPointer)This); + sq_setreleasehook(v, 1, script_canvas_destructor); + RET_VOID() +} + +//----------------------------------------------------------------- +// Canvas.Load(filename) +static SQInteger script_canvas_Load(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_STRING(1, filename) + FilePtr file = OpenFile(filename); + if (!file) { + THROW_ERROR("Could not open file") + } + CanvasPtr image = LoadImage(file.get()); + if (!image) { + THROW_ERROR("Could not load image") + } + RET_CANVAS(image.get()) +} + +//----------------------------------------------------------------- +// Canvas.LoadFromStream(stream) +static SQInteger script_canvas_LoadFromStream(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_STREAM(1, stream) + if (!stream->isOpen() || !stream->isReadable()) { + THROW_ERROR("Invalid stream") + } + CanvasPtr image = LoadImage(stream); + if (!image) { + THROW_ERROR("Could not load image") + } + RET_CANVAS(image.get()) +} + +//----------------------------------------------------------------- +// Canvas.save(filename) +static SQInteger script_canvas_save(HSQUIRRELVM v) +{ + SETUP_CANVAS_OBJECT() + CHECK_NARGS(1) + GET_ARG_STRING(1, filename) + FilePtr file = OpenFile(filename, IFile::OUT); + if (!file) { + THROW_ERROR("Could not open file") + } + if (!SaveImage(This, file.get())) { + THROW_ERROR("Could not save image") + } + RET_VOID() +} + +//----------------------------------------------------------------- +// Canvas.saveToStream(stream) +static SQInteger script_canvas_saveToStream(HSQUIRRELVM v) +{ + SETUP_CANVAS_OBJECT() + CHECK_NARGS(1) + GET_ARG_STREAM(1, stream) + if (!stream->isOpen() || !stream->isReadable()) { + THROW_ERROR("Invalid stream") + } + if (!SaveImage(This, stream)) { + THROW_ERROR("Could not save image") + } + RET_VOID() +} + +//----------------------------------------------------------------- +// Canvas.getPixels() +static SQInteger script_canvas_getPixels(HSQUIRRELVM v) +{ + SETUP_CANVAS_OBJECT() + BlobPtr pixels = Blob::Create(This->getNumPixels() * Canvas::GetNumBytesPerPixel()); + memcpy(pixels->getBuffer(), This->getPixels(), pixels->getSize()); + RET_BLOB(pixels.get()) } //----------------------------------------------------------------- @@ -2228,552 +2493,141 @@ static SQInteger script_canvas__dump(HSQUIRRELVM v) RET_VOID() -throw_write_error: - THROW_ERROR("Write error") -} - -//----------------------------------------------------------------- -// Canvas._load(stream) -static SQInteger script_canvas__load(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_STREAM(1, stream) - - if (!stream->isOpen() || !stream->isReadable()) { - THROW_ERROR("Invalid input stream") - } - - // read dimensions - i32 width; - i32 height; - if (!readi32l(stream, width) || - !readi32l(stream, height)) - { - THROW_ERROR("Read error") - } - if (width * height <= 0) { - THROW_ERROR("Invalid dimensions") - } - - CanvasPtr instance = Canvas::Create(width, height); - - // read pixels - int pixels_size = width * height * Canvas::GetNumBytesPerPixel(); - if (stream->read(instance->getPixels(), pixels_size) != pixels_size) { - THROW_ERROR("Read error") - } - - RET_CANVAS(instance.get()) -} - -//----------------------------------------------------------------- -void BindCanvas(Canvas* canvas) -{ - assert(g_CanvasClass._type == OT_CLASS); - assert(canvas); - sq_pushobject(g_VM, g_CanvasClass); // push canvas class - sq_createinstance(g_VM, -1); - sq_remove(g_VM, -2); // pop canvas class - sq_setreleasehook(g_VM, -1, script_canvas_destructor); - sq_setinstanceup(g_VM, -1, (SQUserPointer)canvas); - canvas->grab(); // grab a new reference -} - -//----------------------------------------------------------------- -Canvas* GetCanvas(SQInteger idx) -{ - SQUserPointer p = 0; - if (SQ_SUCCEEDED(sq_getinstanceup(g_VM, idx, &p, TT_CANVAS))) { - return (Canvas*)p; - } - return 0; -} - -//----------------------------------------------------------------- -static ScriptFuncReg script_canvas_methods[] = { - {"constructor", "Canvas.constructor", script_canvas_constructor }, - {"save", "Canvas.save", script_canvas_save }, - {"saveToStream", "Canvas.saveToStream", script_canvas_saveToStream }, - {"getPixels", "Canvas.getPixels", script_canvas_getPixels }, - {"cloneSection", "Canvas.cloneSection", script_canvas_cloneSection }, - {"getPixel", "Canvas.getPixel", script_canvas_getPixel }, - {"setPixel", "Canvas.setPixel", script_canvas_setPixel }, - {"getPixelByIndex", "Canvas.getPixelByIndex", script_canvas_getPixelByIndex }, - {"setPixelByIndex", "Canvas.setPixelByIndex", script_canvas_setPixelByIndex }, - {"resize", "Canvas.resize", script_canvas_resize }, - {"fill", "Canvas.fill", script_canvas_fill }, - {"_get", "Canvas._get", script_canvas__get }, - {"_set", "Canvas._set", script_canvas__set }, - {"_typeof", "Canvas._typeof", script_canvas__typeof }, - {"_tostring", "Canvas._tostring", script_canvas__tostring }, - {"_nexti", "Canvas._nexti", script_canvas__nexti }, - {"_cloned", "Canvas._cloned", script_canvas__cloned }, - {0,0} -}; - -//----------------------------------------------------------------- -static ScriptFuncReg script_canvas_static_methods[] = { - {"Load", "Canvas.Load", script_canvas_Load }, - {"LoadFromStream", "Canvas.LoadFromStream", script_canvas_LoadFromStream }, - {"_dump", "Canvas._dump", script_canvas__dump }, - {"_load", "Canvas._load", script_canvas__load }, - {0,0} -}; - -//----------------------------------------------------------------- -static ScriptConstReg script_canvas_constants[] = { - {0,0} -}; - -//----------------------------------------------------------------- -static void init_canvas_class() -{ - sq_newclass(g_VM, SQFalse); // create class - sq_settypetag(g_VM, -1, TT_CANVAS); - - // define methods - for (int i = 0; script_canvas_methods[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_canvas_methods[i].funcname, -1); - sq_newclosure(g_VM, script_canvas_methods[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_canvas_methods[i].debugname); - sq_newslot(g_VM, -3, SQFalse); - } - - // define static methods - for (int i = 0; script_canvas_static_methods[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_canvas_static_methods[i].funcname, -1); - sq_newclosure(g_VM, script_canvas_static_methods[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_canvas_static_methods[i].debugname); - sq_newslot(g_VM, -3, SQTrue); - } - - // define constants - for (int i = 0; script_canvas_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_canvas_constants[i].name, -1); - sq_pushinteger(g_VM, script_canvas_constants[i].value); - sq_newslot(g_VM, -3, SQTrue); - } - - // get a strong reference - sq_resetobject(&g_CanvasClass); - sq_getstackobj(g_VM, -1, &g_CanvasClass); - sq_addref(g_VM, &g_CanvasClass); - - sq_poptop(g_VM); // pop class -} - -/*********************** GLOBAL FUNCTIONS *************************/ - -//----------------------------------------------------------------- -// CreateColor(red, green, blue [, alpha = 255]) -static SQInteger script_CreateColor(HSQUIRRELVM v) -{ - CHECK_MIN_NARGS(3) - GET_ARG_INT(1, red) - GET_ARG_INT(2, green) - GET_ARG_INT(3, blue) - GET_OPTARG_INT(4, alpha, 255) - RET_INT(RGBA::Pack(red, green, blue, alpha)) -} - -//----------------------------------------------------------------- -// UnpackRed(color) -static SQInteger script_UnpackRed(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_INT(1, color) - RET_INT(RGBA::Unpack((u32)color).red) -} - -//----------------------------------------------------------------- -// UnpackGreen(color) -static SQInteger script_UnpackGreen(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_INT(1, color) - RET_INT(RGBA::Unpack((u32)color).green) -} - -//----------------------------------------------------------------- -// UnpackBlue(color) -static SQInteger script_UnpackBlue(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_INT(1, color) - RET_INT(RGBA::Unpack((u32)color).blue) -} - -//----------------------------------------------------------------- -// UnpackAlpha(color) -static SQInteger script_UnpackAlpha(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_INT(1, color) - RET_INT(RGBA::Unpack((u32)color).alpha) -} - -//----------------------------------------------------------------- -static ScriptFuncReg script_graphics_functions[] = { - {"CreateColor", "CreateColor", script_CreateColor }, - {"UnpackRed", "UnpackRed", script_UnpackRed }, - {"UnpackGreen", "UnpackGreen", script_UnpackGreen }, - {"UnpackBlue", "UnpackBlue", script_UnpackBlue }, - {"UnpackAlpha", "UnpackAlpha", script_UnpackAlpha }, - {0,0} -}; - -//----------------------------------------------------------------- -static ScriptConstReg script_graphics_constants[] = { - {"BLACK", RGBA::Pack( 0, 0, 0) }, - {"WHITE", RGBA::Pack(255, 255, 255) }, - {"RED", RGBA::Pack(255, 0, 0) }, - {"GREEN", RGBA::Pack(0, 255, 0) }, - {"BLUE", RGBA::Pack(0, 0, 255) }, - {"YELLOW", RGBA::Pack(255, 255, 0) }, - {0,0} -}; - -//----------------------------------------------------------------- -static void register_graphics_api() -{ - // init classes - init_canvas_class(); - - // register classes - sq_pushstring(g_VM, "Canvas", -1); - sq_pushobject(g_VM, g_CanvasClass); - sq_newslot(g_VM, -3, SQFalse); - - // register functions - for (int i = 0; script_graphics_functions[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_graphics_functions[i].funcname, -1); - sq_newclosure(g_VM, script_graphics_functions[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_graphics_functions[i].debugname); - sq_newslot(g_VM, -3, SQFalse); - } - - // register constants - for (int i = 0; script_graphics_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_graphics_constants[i].name, -1); - sq_pushinteger(g_VM, script_graphics_constants[i].value); - sq_newslot(g_VM, -3, SQFalse); - } -} - -/****************************************************************** - * * - * FILESYSTEM * - * * - ******************************************************************/ - -/***************************** FILE *******************************/ - -#define SETUP_FILE_OBJECT() \ - IFile* This = 0; \ - if (SQ_FAILED(sq_getinstanceup(v, 1, (SQUserPointer*)&This, TT_FILE))) { \ - THROW_ERROR("Invalid type of environment object, expected a File instance") \ - } - -//----------------------------------------------------------------- -static SQInteger script_file_destructor(SQUserPointer p, SQInteger size) -{ - assert(p); - ((IFile*)p)->drop(); - return 0; -} - -//----------------------------------------------------------------- -// File.getName() -static SQInteger script_file_getName(HSQUIRRELVM v) -{ - SETUP_FILE_OBJECT() - RET_STRING(This->getName().c_str()) -} - -//----------------------------------------------------------------- -// File._typeof() -static SQInteger script_file__typeof(HSQUIRRELVM v) -{ - SETUP_FILE_OBJECT() - RET_STRING("File") -} - -//----------------------------------------------------------------- -// File._tostring() -static SQInteger script_file__tostring(HSQUIRRELVM v) -{ - SETUP_FILE_OBJECT() - std::ostringstream oss; - oss << "getName() << "\""; - } else { - oss << "N/A"; - } - oss << ")>"; - RET_STRING(oss.str().c_str()) -} - -//----------------------------------------------------------------- -void CreateFileDerivedClass() -{ - sq_pushobject(g_VM, g_FileClass); - bool succeeded = SQ_SUCCEEDED(sq_newclass(g_VM, SQTrue)); - assert(succeeded); -} - -//----------------------------------------------------------------- -void BindFile(IFile* file) -{ - assert(g_FileClass._type == OT_CLASS); - assert(file); - sq_pushobject(g_VM, g_FileClass); // push file class - sq_createinstance(g_VM, -1); - sq_remove(g_VM, -2); // pop file class - sq_setreleasehook(g_VM, -1, script_file_destructor); - sq_setinstanceup(g_VM, -1, (SQUserPointer)file); - file->grab(); // grab a new reference -} - -//----------------------------------------------------------------- -IFile* GetFile(SQInteger idx) -{ - SQUserPointer p = 0; - if (SQ_SUCCEEDED(sq_getinstanceup(g_VM, idx, &p, TT_FILE))) { - return (IFile*)p; - } - return 0; -} - -//----------------------------------------------------------------- -static ScriptFuncReg script_file_methods[] = { - {"getName", "File.getName", script_file_getName }, - {"_typeof", "File._typeof", script_file__typeof }, - {"_tostring", "File._tostring", script_file__tostring }, - {0,0} -}; - -//----------------------------------------------------------------- -static ScriptFuncReg script_file_static_methods[] = { - {0,0} -}; - -//----------------------------------------------------------------- -static ScriptConstReg script_file_constants[] = { - {"IN", IFile::IN }, - {"OUT", IFile::OUT }, - {"APPEND", IFile::APPEND }, - {0,0} -}; - -//----------------------------------------------------------------- -static void init_file_class() -{ - CreateStreamDerivedClass(); // inherit from Stream - sq_newclass(g_VM, SQTrue); // create class - sq_settypetag(g_VM, -1, TT_FILE); - - // define methods - for (int i = 0; script_file_methods[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_file_methods[i].funcname, -1); - sq_newclosure(g_VM, script_file_methods[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_file_methods[i].debugname); - sq_newslot(g_VM, -3, SQFalse); - } - - // define static methods - for (int i = 0; script_file_static_methods[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_file_static_methods[i].funcname, -1); - sq_newclosure(g_VM, script_file_static_methods[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_file_static_methods[i].debugname); - sq_newslot(g_VM, -3, SQTrue); - } - - // define constants - for (int i = 0; script_file_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_file_constants[i].name, -1); - sq_pushinteger(g_VM, script_file_constants[i].value); - sq_newslot(g_VM, -3, SQTrue); - } - - // get a strong reference - sq_resetobject(&g_FileClass); - sq_getstackobj(g_VM, -1, &g_FileClass); - sq_addref(g_VM, &g_FileClass); - - sq_poptop(g_VM); // pop class -} - -/*********************** GLOBAL FUNCTIONS *************************/ - -//----------------------------------------------------------------- -// OpenFile(filename [, mode = File.IN]) -static SQInteger script_OpenFile(HSQUIRRELVM v) -{ - CHECK_MIN_NARGS(1) - GET_ARG_STRING(1, filename) - GET_OPTARG_INT(2, mode, IFile::IN) - if (mode != IFile::IN && - mode != IFile::OUT && - mode != IFile::APPEND) - { - THROW_ERROR("Invalid mode") - } - FilePtr file = OpenFile(filename, mode); - if (!file) { - THROW_ERROR("Could not open file") - } - RET_FILE(file.get()) -} - -//----------------------------------------------------------------- -// DoesFileExist(filename) -static SQInteger script_DoesFileExist(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_STRING(1, filename) - RET_BOOL(DoesFileExist(filename)) -} - -//----------------------------------------------------------------- -// IsRegularFile(filename) -static SQInteger script_IsRegularFile(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_STRING(1, filename) - RET_BOOL(IsRegularFile(filename)) +throw_write_error: + THROW_ERROR("Write error") } //----------------------------------------------------------------- -// IsDirectory(filename) -static SQInteger script_IsDirectory(HSQUIRRELVM v) +// Canvas._load(stream) +static SQInteger script_canvas__load(HSQUIRRELVM v) { CHECK_NARGS(1) - GET_ARG_STRING(1, filename) - RET_BOOL(IsDirectory(filename)) -} + GET_ARG_STREAM(1, stream) -//----------------------------------------------------------------- -// GetFileModTime(filename) -static SQInteger script_GetFileModTime(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_STRING(1, filename) - RET_INT(GetFileModTime(filename)) -} + if (!stream->isOpen() || !stream->isReadable()) { + THROW_ERROR("Invalid input stream") + } -//----------------------------------------------------------------- -// CreateDirectory(directory) -static SQInteger script_CreateDirectory(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_STRING(1, directory) - if (!CreateDirectory(directory)) { - THROW_ERROR("Could not create directory") + // read dimensions + i32 width; + i32 height; + if (!readi32l(stream, width) || + !readi32l(stream, height)) + { + THROW_ERROR("Read error") + } + if (width * height <= 0) { + THROW_ERROR("Invalid dimensions") } - RET_VOID() -} -//----------------------------------------------------------------- -// RemoveFile(filename) -static SQInteger script_RemoveFile(HSQUIRRELVM v) -{ - CHECK_NARGS(1) - GET_ARG_STRING(1, filename) - if (!RemoveFile(filename)) { - THROW_ERROR("Could not remove file or directory") + CanvasPtr instance = Canvas::Create(width, height); + + // read pixels + int pixels_size = width * height * Canvas::GetNumBytesPerPixel(); + if (stream->read(instance->getPixels(), pixels_size) != pixels_size) { + THROW_ERROR("Read error") } - RET_VOID() + + RET_CANVAS(instance.get()) } //----------------------------------------------------------------- -// RenameFile(filenameFrom, filenameTo) -static SQInteger script_RenameFile(HSQUIRRELVM v) +void BindCanvas(Canvas* canvas) { - CHECK_NARGS(2) - GET_ARG_STRING(1, filenameFrom) - GET_ARG_STRING(2, filenameTo) - if (!RenameFile(filenameFrom, filenameTo)) { - THROW_ERROR("Could not rename file or directory") - } - RET_VOID() + assert(g_CanvasClass._type == OT_CLASS); + assert(canvas); + sq_pushobject(g_VM, g_CanvasClass); // push canvas class + sq_createinstance(g_VM, -1); + sq_remove(g_VM, -2); // pop canvas class + sq_setreleasehook(g_VM, -1, script_canvas_destructor); + sq_setinstanceup(g_VM, -1, (SQUserPointer)canvas); + canvas->grab(); // grab a new reference } //----------------------------------------------------------------- -// GetFileList(directory) -static SQInteger script_GetFileList(HSQUIRRELVM v) +Canvas* GetCanvas(SQInteger idx) { - CHECK_NARGS(1) - GET_ARG_STRING(1, directory) - std::vector file_list; - if (!GetFileList(directory, file_list)) { - THROW_ERROR("Could not get file list") - } - sq_newarray(v, file_list.size()); - if (file_list.size() > 0) { - for (int i = 0; i < (int)file_list.size(); ++i) { - sq_pushinteger(v, i); - sq_pushstring(v, file_list[i].c_str(), -1); - sq_rawset(v, -3); - } + SQUserPointer p = 0; + if (SQ_SUCCEEDED(sq_getinstanceup(g_VM, idx, &p, TT_CANVAS))) { + return (Canvas*)p; } - return 1; + return 0; } //----------------------------------------------------------------- -static ScriptFuncReg script_filesystem_functions[] = { - {"OpenFile", "OpenFile", script_OpenFile }, - {"DoesFileExist", "DoesFileExist", script_DoesFileExist }, - {"IsRegularFile", "IsRegularFile", script_IsRegularFile }, - {"IsDirectory", "IsDirectory", script_IsDirectory }, - {"GetFileModTime", "GetFileModTime", script_GetFileModTime }, - {"CreateDirectory", "CreateDirectory", script_CreateDirectory }, - {"RemoveFile", "RemoveFile", script_RemoveFile }, - {"RenameFile", "RenameFile", script_RenameFile }, - {"GetFileList", "GetFileList", script_GetFileList }, +static ScriptFuncReg script_canvas_methods[] = { + {"constructor", "Canvas.constructor", script_canvas_constructor }, + {"save", "Canvas.save", script_canvas_save }, + {"saveToStream", "Canvas.saveToStream", script_canvas_saveToStream }, + {"getPixels", "Canvas.getPixels", script_canvas_getPixels }, + {"cloneSection", "Canvas.cloneSection", script_canvas_cloneSection }, + {"getPixel", "Canvas.getPixel", script_canvas_getPixel }, + {"setPixel", "Canvas.setPixel", script_canvas_setPixel }, + {"getPixelByIndex", "Canvas.getPixelByIndex", script_canvas_getPixelByIndex }, + {"setPixelByIndex", "Canvas.setPixelByIndex", script_canvas_setPixelByIndex }, + {"resize", "Canvas.resize", script_canvas_resize }, + {"fill", "Canvas.fill", script_canvas_fill }, + {"_get", "Canvas._get", script_canvas__get }, + {"_set", "Canvas._set", script_canvas__set }, + {"_typeof", "Canvas._typeof", script_canvas__typeof }, + {"_tostring", "Canvas._tostring", script_canvas__tostring }, + {"_nexti", "Canvas._nexti", script_canvas__nexti }, + {"_cloned", "Canvas._cloned", script_canvas__cloned }, {0,0} }; //----------------------------------------------------------------- -static ScriptConstReg script_filesystem_constants[] = { +static ScriptFuncReg script_canvas_static_methods[] = { + {"Load", "Canvas.Load", script_canvas_Load }, + {"LoadFromStream", "Canvas.LoadFromStream", script_canvas_LoadFromStream }, + {"_dump", "Canvas._dump", script_canvas__dump }, + {"_load", "Canvas._load", script_canvas__load }, {0,0} }; //----------------------------------------------------------------- -static void register_filesystem_api() -{ - // init classes - init_file_class(); +static ScriptConstReg script_canvas_constants[] = { + {0,0} +}; - // register classes - sq_pushstring(g_VM, "File", -1); - sq_pushobject(g_VM, g_FileClass); - sq_newslot(g_VM, -3, SQFalse); +//----------------------------------------------------------------- +static void init_canvas_class() +{ + sq_newclass(g_VM, SQFalse); // create class + sq_settypetag(g_VM, -1, TT_CANVAS); - // register functions - for (int i = 0; script_filesystem_functions[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_filesystem_functions[i].funcname, -1); - sq_newclosure(g_VM, script_filesystem_functions[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_filesystem_functions[i].debugname); + // define methods + for (int i = 0; script_canvas_methods[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_canvas_methods[i].funcname, -1); + sq_newclosure(g_VM, script_canvas_methods[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_canvas_methods[i].debugname); sq_newslot(g_VM, -3, SQFalse); } - // register constants - for (int i = 0; script_filesystem_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_filesystem_constants[i].name, -1); - sq_pushinteger(g_VM, script_filesystem_constants[i].value); - sq_newslot(g_VM, -3, SQFalse); + // define static methods + for (int i = 0; script_canvas_static_methods[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_canvas_static_methods[i].funcname, -1); + sq_newclosure(g_VM, script_canvas_static_methods[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_canvas_static_methods[i].debugname); + sq_newslot(g_VM, -3, SQTrue); } -} -/****************************************************************** - * * - * VIDEO * - * * - ******************************************************************/ + // define constants + for (int i = 0; script_canvas_constants[i].name != 0; i++) { + sq_pushstring(g_VM, script_canvas_constants[i].name, -1); + sq_pushinteger(g_VM, script_canvas_constants[i].value); + sq_newslot(g_VM, -3, SQTrue); + } + + // get a strong reference + sq_resetobject(&g_CanvasClass); + sq_getstackobj(g_VM, -1, &g_CanvasClass); + sq_addref(g_VM, &g_CanvasClass); -/**************************** TEXTURE *****************************/ + sq_poptop(g_VM); // pop class +} + +/************************ TEXTURE OBJECT **************************/ #define SETUP_TEXTURE_OBJECT() \ ITexture* This = 0; \ @@ -3049,6 +2903,54 @@ static void init_texture_class() /*********************** GLOBAL FUNCTIONS *************************/ +//----------------------------------------------------------------- +// CreateColor(red, green, blue [, alpha = 255]) +static SQInteger script_CreateColor(HSQUIRRELVM v) +{ + CHECK_MIN_NARGS(3) + GET_ARG_INT(1, red) + GET_ARG_INT(2, green) + GET_ARG_INT(3, blue) + GET_OPTARG_INT(4, alpha, 255) + RET_INT(RGBA::Pack(red, green, blue, alpha)) +} + +//----------------------------------------------------------------- +// UnpackRed(color) +static SQInteger script_UnpackRed(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_INT(1, color) + RET_INT(RGBA::Unpack((u32)color).red) +} + +//----------------------------------------------------------------- +// UnpackGreen(color) +static SQInteger script_UnpackGreen(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_INT(1, color) + RET_INT(RGBA::Unpack((u32)color).green) +} + +//----------------------------------------------------------------- +// UnpackBlue(color) +static SQInteger script_UnpackBlue(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_INT(1, color) + RET_INT(RGBA::Unpack((u32)color).blue) +} + +//----------------------------------------------------------------- +// UnpackAlpha(color) +static SQInteger script_UnpackAlpha(HSQUIRRELVM v) +{ + CHECK_NARGS(1) + GET_ARG_INT(1, color) + RET_INT(RGBA::Unpack((u32)color).alpha) +} + //----------------------------------------------------------------- // GetSupportedVideoModes() static SQInteger script_GetSupportedVideoModes(HSQUIRRELVM v) @@ -3468,7 +3370,12 @@ static SQInteger script_DrawSubImageQuad(HSQUIRRELVM v) } //----------------------------------------------------------------- -static ScriptFuncReg script_video_functions[] = { +static ScriptFuncReg script_graphics_functions[] = { + {"CreateColor", "CreateColor", script_CreateColor }, + {"UnpackRed", "UnpackRed", script_UnpackRed }, + {"UnpackGreen", "UnpackGreen", script_UnpackGreen }, + {"UnpackBlue", "UnpackBlue", script_UnpackBlue }, + {"UnpackAlpha", "UnpackAlpha", script_UnpackAlpha }, {"GetSupportedVideoModes", "GetSupportedVideoModes", script_GetSupportedVideoModes }, {"OpenWindow", "OpenWindow", script_OpenWindow }, {"IsWindowOpen", "IsWindowOpen", script_IsWindowOpen }, @@ -3496,44 +3403,55 @@ static ScriptFuncReg script_video_functions[] = { }; //----------------------------------------------------------------- -static ScriptConstReg script_video_constants[] = { +static ScriptConstReg script_graphics_constants[] = { + {"BLACK", RGBA::Pack( 0, 0, 0) }, + {"WHITE", RGBA::Pack(255, 255, 255) }, + {"RED", RGBA::Pack(255, 0, 0) }, + {"GREEN", RGBA::Pack(0, 255, 0) }, + {"BLUE", RGBA::Pack(0, 0, 255) }, + {"YELLOW", RGBA::Pack(255, 255, 0) }, {0,0} }; //----------------------------------------------------------------- -static void register_video_api() +static void register_graphics_api() { // init classes + init_canvas_class(); init_texture_class(); // register classes + sq_pushstring(g_VM, "Canvas", -1); + sq_pushobject(g_VM, g_CanvasClass); + sq_newslot(g_VM, -3, SQFalse); + sq_pushstring(g_VM, "Texture", -1); sq_pushobject(g_VM, g_TextureClass); sq_newslot(g_VM, -3, SQFalse); // register functions - for (int i = 0; script_video_functions[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_video_functions[i].funcname, -1); - sq_newclosure(g_VM, script_video_functions[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_video_functions[i].debugname); + for (int i = 0; script_graphics_functions[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_graphics_functions[i].funcname, -1); + sq_newclosure(g_VM, script_graphics_functions[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_graphics_functions[i].debugname); sq_newslot(g_VM, -3, SQFalse); } // register constants - for (int i = 0; script_video_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_video_constants[i].name, -1); - sq_pushinteger(g_VM, script_video_constants[i].value); + for (int i = 0; script_graphics_constants[i].name != 0; i++) { + sq_pushstring(g_VM, script_graphics_constants[i].name, -1); + sq_pushinteger(g_VM, script_graphics_constants[i].value); sq_newslot(g_VM, -3, SQFalse); } } /****************************************************************** * * - * AUDIO * + * SOUND * * * ******************************************************************/ -/***************************** SOUND ******************************/ +/************************* SOUND OBJECT ***************************/ #define SETUP_SOUND_OBJECT() \ ISound* This = 0; \ @@ -3776,7 +3694,7 @@ static void init_sound_class() sq_poptop(g_VM); // pop class } -/************************** SOUNDEFFECT ***************************/ +/********************** SOUNDEFFECT OBJECT ************************/ #define SETUP_SOUNDEFFECT_OBJECT() \ ISoundEffect* This = 0; \ @@ -3989,7 +3907,7 @@ static ScriptConstReg script_audio_constants[] = { }; //----------------------------------------------------------------- -static void register_audio_api() +static void register_sound_api() { // init classes init_sound_class(); @@ -4532,11 +4450,11 @@ static void register_input_api() /****************************************************************** * * - * COMPRESSION * + * UTILITY * * * ******************************************************************/ -/**************************** ZSTREAM *****************************/ +/************************* ZSTREAM OBJECT *************************/ #define SETUP_ZSTREAM_OBJECT() \ ZStream* This = 0; \ @@ -4760,17 +4678,17 @@ static void init_zstream_class() /*********************** GLOBAL FUNCTIONS *************************/ //----------------------------------------------------------------- -static ScriptFuncReg script_compression_functions[] = { +static ScriptFuncReg script_utility_functions[] = { {0,0} }; //----------------------------------------------------------------- -static ScriptConstReg script_compression_constants[] = { +static ScriptConstReg script_utility_constants[] = { {0,0} }; //----------------------------------------------------------------- -static void register_compression_api() +static void register_utility_api() { // init classes init_zstream_class(); @@ -4781,29 +4699,27 @@ static void register_compression_api() sq_newslot(g_VM, -3, SQFalse); // register functions - for (int i = 0; script_compression_functions[i].funcname != 0; i++) { - sq_pushstring(g_VM, script_compression_functions[i].funcname, -1); - sq_newclosure(g_VM, script_compression_functions[i].funcptr, 0); - sq_setnativeclosurename(g_VM, -1, script_compression_functions[i].debugname); + for (int i = 0; script_utility_functions[i].funcname != 0; i++) { + sq_pushstring(g_VM, script_utility_functions[i].funcname, -1); + sq_newclosure(g_VM, script_utility_functions[i].funcptr, 0); + sq_setnativeclosurename(g_VM, -1, script_utility_functions[i].debugname); sq_newslot(g_VM, -3, SQFalse); } // register constants - for (int i = 0; script_compression_constants[i].name != 0; i++) { - sq_pushstring(g_VM, script_compression_constants[i].name, -1); - sq_pushinteger(g_VM, script_compression_constants[i].value); + for (int i = 0; script_utility_constants[i].name != 0; i++) { + sq_pushstring(g_VM, script_utility_constants[i].name, -1); + sq_pushinteger(g_VM, script_utility_constants[i].value); sq_newslot(g_VM, -3, SQFalse); } } /****************************************************************** * * - * SYSTEM * + * GENERAL FUNCTIONS * * * ******************************************************************/ -/*********************** GLOBAL FUNCTIONS *************************/ - //----------------------------------------------------------------- // GetTime() static SQInteger script_GetTime(HSQUIRRELVM v) @@ -5738,14 +5654,12 @@ bool InitScript(const Log& log) // register sphere API register_core_api(); + register_system_api(); register_io_api(); - register_graphics_api(); // maybe merge graphics and video? - register_filesystem_api(); // must be registered after io, because file needs stream to be initialized first; maybe merge filesystem and io? - register_video_api(); - register_audio_api(); + register_graphics_api(); + register_sound_api(); register_input_api(); - register_compression_api(); // maybe name it util and put in there the upcoming encryption api as well? - register_system_api(); + register_utility_api(); sq_poptop(g_VM); // done registering, pop root table diff --git a/src/script/script.hpp b/src/script/script.hpp index ec1f2c6..36072fe 100644 --- a/src/script/script.hpp +++ b/src/script/script.hpp @@ -11,11 +11,11 @@ #include "../io/IFile.hpp" #include "../io/Blob.hpp" #include "../graphics/Canvas.hpp" -#include "../video/ITexture.hpp" -#include "../audio/ISound.hpp" -#include "../audio/ISoundEffect.hpp" +#include "../graphics/ITexture.hpp" +#include "../sound/ISound.hpp" +#include "../sound/ISoundEffect.hpp" #include "../input/joystick.hpp" -#include "../compression/ZStream.hpp" +#include "../util/ZStream.hpp" HSQUIRRELVM GetVM(); diff --git a/src/audio/ISound.hpp b/src/sound/ISound.hpp similarity index 100% rename from src/audio/ISound.hpp rename to src/sound/ISound.hpp diff --git a/src/audio/ISoundEffect.hpp b/src/sound/ISoundEffect.hpp similarity index 100% rename from src/audio/ISoundEffect.hpp rename to src/sound/ISoundEffect.hpp diff --git a/src/audio/audiere/Sound.cpp b/src/sound/audiere/Sound.cpp similarity index 100% rename from src/audio/audiere/Sound.cpp rename to src/sound/audiere/Sound.cpp diff --git a/src/audio/audiere/Sound.hpp b/src/sound/audiere/Sound.hpp similarity index 100% rename from src/audio/audiere/Sound.hpp rename to src/sound/audiere/Sound.hpp diff --git a/src/audio/audiere/SoundEffect.cpp b/src/sound/audiere/SoundEffect.cpp similarity index 100% rename from src/audio/audiere/SoundEffect.cpp rename to src/sound/audiere/SoundEffect.cpp diff --git a/src/audio/audiere/SoundEffect.hpp b/src/sound/audiere/SoundEffect.hpp similarity index 100% rename from src/audio/audiere/SoundEffect.hpp rename to src/sound/audiere/SoundEffect.hpp diff --git a/src/audio/audiere/audio.cpp b/src/sound/audiere/audio.cpp similarity index 100% rename from src/audio/audiere/audio.cpp rename to src/sound/audiere/audio.cpp diff --git a/src/audio/audio.hpp b/src/sound/audio.hpp similarity index 100% rename from src/audio/audio.hpp rename to src/sound/audio.hpp diff --git a/src/compression/ZStream.cpp b/src/util/ZStream.cpp similarity index 100% rename from src/compression/ZStream.cpp rename to src/util/ZStream.cpp diff --git a/src/compression/ZStream.hpp b/src/util/ZStream.hpp similarity index 100% rename from src/compression/ZStream.hpp rename to src/util/ZStream.hpp