From 281f8feabffd69928a0a0f08aa31f70b36f5e6bd Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 3 Nov 2018 21:11:41 +0800 Subject: =?UTF-8?q?*=E5=A2=9E=E5=8A=A0=E6=B8=B8=E6=88=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Common/je_common.h | 1 + src/libjin/Common/je_exception.cpp | 46 +++++++++++++++++++++++++++++ src/libjin/Common/je_exception.h | 29 ++++++++++++++++-- src/libjin/Filesystem/je_asset_database.cpp | 9 +++--- src/libjin/Filesystem/je_asset_database.h | 2 +- src/libjin/Graphics/je_bitmap.cpp | 21 ++++++++++--- src/libjin/Graphics/je_bitmap.h | 3 +- src/libjin/Graphics/je_window.cpp | 10 +++++-- 8 files changed, 105 insertions(+), 16 deletions(-) (limited to 'src/libjin') 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 + +#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 +#include 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 #include /* 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(x, 0, width - 1) || without(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; } -- cgit v1.1-26-g67d0