diff options
author | chai <chaifix@163.com> | 2018-11-03 21:11:41 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-11-03 21:11:41 +0800 |
commit | 281f8feabffd69928a0a0f08aa31f70b36f5e6bd (patch) | |
tree | a22413447a5480a5dffe625c677ad7c4140350c5 | |
parent | f6ef5d6c0188e7dc6b39803ca37fe736f0eb3962 (diff) |
*增加游戏目录选择
-rw-r--r-- | bin/Jin.exe | bin | 843776 -> 845824 bytes | |||
-rw-r--r-- | bin/SDL2.dll | bin | 771072 -> 771072 bytes | |||
-rw-r--r-- | build/vc++/jin/jin.vcxproj.user | 4 | ||||
-rw-r--r-- | src/3rdparty/buildvm/buildvm.exe | bin | 62976 -> 62976 bytes | |||
-rw-r--r-- | src/3rdparty/minilua/minilua.exe | bin | 112640 -> 112640 bytes | |||
-rw-r--r-- | src/3rdparty/ogl/OpenGL.h | 6 | ||||
-rw-r--r-- | src/jin/main.cpp | 78 | ||||
-rw-r--r-- | src/libjin/Common/je_common.h | 1 | ||||
-rw-r--r-- | src/libjin/Common/je_exception.cpp | 46 | ||||
-rw-r--r-- | src/libjin/Common/je_exception.h | 29 | ||||
-rw-r--r-- | src/libjin/Filesystem/je_asset_database.cpp | 9 | ||||
-rw-r--r-- | src/libjin/Filesystem/je_asset_database.h | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/je_bitmap.cpp | 21 | ||||
-rw-r--r-- | src/libjin/Graphics/je_bitmap.h | 3 | ||||
-rw-r--r-- | src/libjin/Graphics/je_window.cpp | 10 | ||||
-rw-r--r-- | src/lua/embed/boot.lua.h | 4 | ||||
-rw-r--r-- | src/lua/modules/audio/je_lua_audio.cpp | 18 | ||||
-rw-r--r-- | src/lua/modules/graphics/je_lua_graphics.cpp | 20 |
18 files changed, 192 insertions, 59 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex c485576..80c9584 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/SDL2.dll b/bin/SDL2.dll Binary files differindex beff75a..a7e5c5f 100644 --- a/bin/SDL2.dll +++ b/bin/SDL2.dll diff --git a/build/vc++/jin/jin.vcxproj.user b/build/vc++/jin/jin.vcxproj.user index bfad347..5943b15 100644 --- a/build/vc++/jin/jin.vcxproj.user +++ b/build/vc++/jin/jin.vcxproj.user @@ -3,9 +3,13 @@ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <LocalDebuggerWorkingDirectory>$(ProjectDir)..\..\..\bin</LocalDebuggerWorkingDirectory> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> + <LocalDebuggerCommandArguments> + </LocalDebuggerCommandArguments> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <LocalDebuggerWorkingDirectory>$(ProjectDir)..\..\..\bin</LocalDebuggerWorkingDirectory> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> + <LocalDebuggerCommandArguments> + </LocalDebuggerCommandArguments> </PropertyGroup> </Project>
\ No newline at end of file diff --git a/src/3rdparty/buildvm/buildvm.exe b/src/3rdparty/buildvm/buildvm.exe Binary files differindex bbd01e7..0d45b9a 100644 --- a/src/3rdparty/buildvm/buildvm.exe +++ b/src/3rdparty/buildvm/buildvm.exe diff --git a/src/3rdparty/minilua/minilua.exe b/src/3rdparty/minilua/minilua.exe Binary files differindex 5c72000..f26a026 100644 --- a/src/3rdparty/minilua/minilua.exe +++ b/src/3rdparty/minilua/minilua.exe diff --git a/src/3rdparty/ogl/OpenGL.h b/src/3rdparty/ogl/OpenGL.h index 04043fe..3e416c7 100644 --- a/src/3rdparty/ogl/OpenGL.h +++ b/src/3rdparty/ogl/OpenGL.h @@ -36,6 +36,7 @@ namespace ogl2d void popColor(); void flushError(); GLuint genTexture(); + void deleteTexture(GLuint texture); void bindTexture(GLuint texture = 0); inline GLuint curTexture() { @@ -151,6 +152,11 @@ namespace ogl2d glBindTexture(GL_TEXTURE_2D, texture); } + void OpenGL::deleteTexture(GLuint texture) + { + glDeleteTextures(1, &texture); + } + void OpenGL::setTexParameter(GLenum pname, GLint param) { glTexParameteri(GL_TEXTURE_2D, pname, param); diff --git a/src/jin/main.cpp b/src/jin/main.cpp index d4f08a0..e129b78 100644 --- a/src/jin/main.cpp +++ b/src/jin/main.cpp @@ -1,36 +1,27 @@ #ifdef _WIN32 - #include <SDL2/SDL_Main.h> - #include <direct.h> +#include <SDL2/SDL_Main.h> +#include <direct.h> #endif #include "lua/luax.h" #include "lua/jin.h" #include "libjin/jin.h" + +#ifdef _WIN32 +#include <shlobj.h> #include <Windows.h> +#endif + +#define EXECUTABLE_DIR "./" +using namespace std; using namespace JinEngine::Filesystem; using namespace JinEngine::Lua; -static void setParamters(lua_State* L, int argc, char* args[]) +// Load game under cwd. +static void load(const char* cwd) { - // Add args to field. - luax_newtable(L); - for (int i = 0; i < argc; ++i) - luax_setrawstring(L, -2, i + 1, args[i]); - luax_setfield(L, -2, "args"); - // Push current working directory. - // Absolute directory. - Buffer cwd = Buffer(1024); -#ifdef _WIN32 - _getcwd((char*)&cwd, cwd.size()); -#elif defined __unix__ -#elif defined __APPLE__ -#endif - luax_setfieldstring(L, "cwd", (char*)&cwd); -} -int main(int argc, char* args[]) -{ // Global lua runtime. lua_State* L = luax_newstate(); @@ -38,8 +29,10 @@ int main(int argc, char* args[]) luax_openlibs(L); // Open jin module. luaopen_jin(L); - // Set parameters. - setParamters(L, argc, args); + // Set current working directory. + luax_newtable(L); + luax_setfieldstring(L, "cwd", cwd); + luax_setfield(L, -2, "args"); // Clear lua stack. luax_clearstack(L); @@ -49,5 +42,46 @@ int main(int argc, char* args[]) // Close lua lib. luax_close(L); +} + +std::string BrowseFolder() +{ + TCHAR path[MAX_PATH]; + + BROWSEINFO bi = { 0 }; + bi.lpszTitle = ("Browse game folder..."); + bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; + bi.lpfn = nullptr; + LPITEMIDLIST pidl = SHBrowseForFolder(&bi); + + if (pidl != 0) + { + SHGetPathFromIDList(pidl, path); + + IMalloc * imalloc = 0; + if (SUCCEEDED(SHGetMalloc(&imalloc))) + { + imalloc->Free(pidl); + imalloc->Release(); + } + + return path; + } + + return ""; +} + +int main(int argc, char* args[]) +{ + string cwd = EXECUTABLE_DIR; +#ifdef _WIN32 + if (argc > 1) + cwd = args[1]; + else + cwd = BrowseFolder(); +#endif + if (!cwd.empty()) + load(cwd.c_str()); + return 0; }
\ No newline at end of file diff --git a/src/libjin/Common/je_common.h b/src/libjin/Common/je_common.h index 67505c1..31b67c6 100644 --- a/src/libjin/Common/je_common.h +++ b/src/libjin/Common/je_common.h @@ -1,6 +1,7 @@ #ifndef __JE_COMMON_H__ #define __JE_COMMON_H__ +#include "je_exception.h" #include "je_array.hpp" #endif
\ No newline at end of file diff --git a/src/libjin/Common/je_exception.cpp b/src/libjin/Common/je_exception.cpp index e69de29..5489a42 100644 --- a/src/libjin/Common/je_exception.cpp +++ b/src/libjin/Common/je_exception.cpp @@ -0,0 +1,46 @@ +#include <stdarg.h> + +#include "je_exception.h" + +namespace JinEngine +{ + + Exception::Exception(const char *fmt, ...) + { + va_list args; + int size_buffer = 256, size_out; + char *buffer; + while (true) + { + buffer = new char[size_buffer]; + memset(buffer, 0, size_buffer); + + va_start(args, fmt); + size_out = vsnprintf(buffer, size_buffer, fmt, args); + va_end(args); + + // see http://perfec.to/vsnprintf/pasprintf.c + // if size_out ... + // == -1 --> output was truncated + // == size_buffer --> output was truncated + // == size_buffer-1 --> ambiguous, /may/ have been truncated + // > size_buffer --> output was truncated, and size_out + // bytes would have been written + if (size_out == size_buffer || size_out == -1 || size_out == size_buffer - 1) + size_buffer *= 2; + else if (size_out > size_buffer) + size_buffer = size_out + 2; // to avoid the ambiguous case + else + break; + + delete[] buffer; + } + mMessage = std::string(buffer); + delete[] buffer; + } + + Exception::~Exception() throw() + { + } + +} // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/Common/je_exception.h b/src/libjin/Common/je_exception.h index cf11f51..c319ebd 100644 --- a/src/libjin/Common/je_exception.h +++ b/src/libjin/Common/je_exception.h @@ -2,18 +2,41 @@ #define __JE_EXCEPTION_H__ #include <exception> +#include <string> namespace JinEngine { /// - /// Built-in exception class. + /// Jin Exception. /// class Exception : public std::exception { public: - Exception(); - const char* what() const throw(); + + /// + /// Creates a new Exception according to printf-rules. + /// + /// @param fmt The format string (see printf). + /// + Exception(const char *fmt, ...); + virtual ~Exception() throw(); + + /// + /// Returns a string containing reason for the exception. + /// + /// @return A description of the exception. + /// + inline virtual const char *what() const throw() + { + return mMessage.c_str(); + } + + private: + /// + /// Exception message. + /// + std::string mMessage; }; diff --git a/src/libjin/Filesystem/je_asset_database.cpp b/src/libjin/Filesystem/je_asset_database.cpp index f2c5681..a8524c5 100644 --- a/src/libjin/Filesystem/je_asset_database.cpp +++ b/src/libjin/Filesystem/je_asset_database.cpp @@ -5,6 +5,8 @@ #include <stdlib.h> #include <stdio.h> /* defines FILENAME_MAX */ +#include "../common/je_exception.h" + #include "je_asset_database.h" namespace JinEngine @@ -34,14 +36,13 @@ namespace JinEngine } } - bool AssetDatabase::read(const char* path, Buffer& buffer) + void AssetDatabase::read(const char* path, Buffer& buffer) { size_t size; byte* data = (byte*)smtread(mSmt, path, &size); - if (data == nullptr) - return false; + if (data == nullptr) + throw Exception("Could not read file %s.", path); buffer.bind(data, size); - return true; } Buffer* read(const char* path) diff --git a/src/libjin/Filesystem/je_asset_database.h b/src/libjin/Filesystem/je_asset_database.h index e61aebf..e8b1bd1 100644 --- a/src/libjin/Filesystem/je_asset_database.h +++ b/src/libjin/Filesystem/je_asset_database.h @@ -70,7 +70,7 @@ namespace JinEngine /// @param buffer Buffer to fill. /// @return True if read sucessful, otherwise return false. /// - bool read(const char* path, Buffer& buffer); + void read(const char* path, Buffer& buffer); /// /// Read file and return data content. diff --git a/src/libjin/Graphics/je_bitmap.cpp b/src/libjin/Graphics/je_bitmap.cpp index c11028f..711b8b5 100644 --- a/src/libjin/Graphics/je_bitmap.cpp +++ b/src/libjin/Graphics/je_bitmap.cpp @@ -1,6 +1,7 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb/stb_image.h" +#include "../common/je_exception.h" #include "../filesystem/je_asset_database.h" #include "../math/je_math.h" @@ -35,8 +36,11 @@ namespace JinEngine return nullptr; int w, h; void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha); - if (data == nullptr) - return nullptr; + if (data == nullptr) + { + throw Exception("Could not create bitmap from image data."); + return nullptr; + } Bitmap* bitmap = new Bitmap(); bitmap->pixels = (Color*)data; bitmap->width = w; @@ -73,6 +77,8 @@ namespace JinEngine width = w; height = h; pixels = new Color[w*h]; + if (pixels == nullptr) + throw Exception("Not enough memory."); } Bitmap::~Bitmap() @@ -94,6 +100,8 @@ namespace JinEngine if (pixels != nullptr) delete[] pixels; pixels = new Color[w*h]; + if (pixels == nullptr) + throw Exception("Not enough memory."); size_t s = w * h * sizeof(Color); memcpy(pixels, p, s); width = w; @@ -106,6 +114,8 @@ namespace JinEngine return; if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1)) return; + if (x + y * width >= width * height) + throw Exception("Pixel <%d, %d> of bitmap is out of range.", x, y); pixels[x + y * width] = c; } @@ -114,7 +124,8 @@ namespace JinEngine if (pixels != nullptr) delete[] pixels; pixels = new Color[w*h]; - size_t s = w * h * sizeof(Color); + if (pixels == nullptr) + throw Exception("Not enough memory."); width = w; height = h; for (int x = 0; x < w; ++x) @@ -126,8 +137,10 @@ namespace JinEngine } } - void Bitmap::setPixels(Color* p) + void Bitmap::setPixels(Color* p, int count) { + if (count > width * height) + throw Exception("Pixels are out of range."); size_t s = width * height * sizeof(Color); memcpy(pixels, p, s); } diff --git a/src/libjin/Graphics/je_bitmap.h b/src/libjin/Graphics/je_bitmap.h index bb7bbfa..b0b7dad 100644 --- a/src/libjin/Graphics/je_bitmap.h +++ b/src/libjin/Graphics/je_bitmap.h @@ -122,8 +122,9 @@ namespace JinEngine /// Set pixels with given color data. /// /// @param colors New pixels' colors. + /// @param count Number of pixels. /// - void setPixels(Color* colors); + void setPixels(Color* colors, int count); /// /// Get pixel in given position. diff --git a/src/libjin/Graphics/je_window.cpp b/src/libjin/Graphics/je_window.cpp index 7265898..86ccd29 100644 --- a/src/libjin/Graphics/je_window.cpp +++ b/src/libjin/Graphics/je_window.cpp @@ -70,15 +70,19 @@ namespace JinEngine return false; SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_MakeCurrent(mWnd, ctx); - // default configuration + // Default configuration gl.setClearColor(0, 0, 0, 0xff); - gl.pushColor(0xff, 0xff, 0xff, 0xff); + glClear(GL_COLOR_BUFFER_BIT); + gl.pushColor(0xff, 0xff, 0xff, 0xff); gl.enable(GL_BLEND); gl.enable(GL_TEXTURE_2D); gl.setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - // bind to default canvas + // Bind to default canvas Canvas::unbind(); Shader::unuse(); + // Avoid white blinnk. + swapBuffers(); + return true; } diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h index 212a1e5..2369b0f 100644 --- a/src/lua/embed/boot.lua.h +++ b/src/lua/embed/boot.lua.h @@ -1,8 +1,8 @@ /* boot.lua */ static const char* boot_lua = R"( -jin.args[2] = jin.args[2] or '.' +local cwd = jin.args['cwd'] or '.' jin.filesystem.init() -jin.filesystem.mount(jin.args[2]) +jin.filesystem.mount(cwd) ------------------------------------------------------------------------- -- Config game diff --git a/src/lua/modules/audio/je_lua_audio.cpp b/src/lua/modules/audio/je_lua_audio.cpp index 0f2f713..698d88a 100644 --- a/src/lua/modules/audio/je_lua_audio.cpp +++ b/src/lua/modules/audio/je_lua_audio.cpp @@ -78,28 +78,28 @@ namespace JinEngine AssetDatabase* fs = AssetDatabase::get(); const char* f = luax_checkstring(L, 1); Buffer b; - if (!fs->exists(f)) + try { - error(L, "No such image %s", f); - goto fail; + if (!fs->exists(f)) + throw Exception("No such source file %s.", f); + fs->read(f, b); } - if (!fs->read(f, b)) + catch (Exception& e) { error(L, "Failed to read source file %s", f); - goto fail; + luax_pushnil(L); + return 1; } Source* src = Source::createSource((void*)&b, b.size()); if (src == nullptr) { error(L, "Failed to decode source file %s", f); - goto fail; + luax_pushnil(L); + return 1; } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); proxy->bind(new Ref<Source>(src, JIN_AUDIO_SOURCE)); return 1; - fail: - luax_pushnil(L); - return 1; } LUA_IMPLEMENT int l_destroy(lua_State* L) diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 6324e00..7efb2e7 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -128,30 +128,30 @@ namespace JinEngine { const char* f = luax_checkstring(L, 1); AssetDatabase* fs = AssetDatabase::get(); - if (!fs->exists(f)) + Buffer b; + try { - error(L, "No such image file %s", f); - goto fail; + if (!fs->exists(f)) + throw Exception("No such image file %s.", f); + fs->read(f, b); } - Buffer b; - if (!fs->read(f, b)) + catch (Exception& e) { error(L, "Failed to read image %s", f); - goto fail; + luax_pushnil(L); + return 1; } bitmap = Bitmap::createBitmap(&b, b.size()); if (bitmap == nullptr) { error(L, "Failed to decode image file %s", f); - goto fail; + luax_pushnil(L); + return 1; } } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP)); return 1; - fail: - luax_pushnil(L); - return 1; } /* jin.graphics.newTexture(bitmap) */ |