diff options
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/Debug/Log.cpp | 39 | ||||
-rw-r--r-- | Runtime/Debug/Log.h | 8 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.cpp | 6 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.h | 1 | ||||
-rw-r--r-- | Runtime/Lua/LuaHelper.cpp | 34 | ||||
-rw-r--r-- | Runtime/Lua/LuaHelper.h | 4 | ||||
-rw-r--r-- | Runtime/Scripting/GL/GL.bind.cpp | 239 |
7 files changed, 309 insertions, 22 deletions
diff --git a/Runtime/Debug/Log.cpp b/Runtime/Debug/Log.cpp index 08c7acf..cc5b2e3 100644 --- a/Runtime/Debug/Log.cpp +++ b/Runtime/Debug/Log.cpp @@ -5,7 +5,11 @@ using namespace std; -//long g_LogTags = LogTag::All; +#ifdef GAMELAB_WIN +#include <windows.h> +static HANDLE s_ConsoleHandle = 0; +#endif + unordered_set<string> s_OpenTags; #ifdef GAMELAB_DEBUG @@ -29,20 +33,47 @@ void log_open_tag(std::string tag) } // https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c +// https://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS +// https://stackoverflow.com/questions/9262270/color-console-output-with-c-in-windows +// https://blog.csdn.net/odaynot/article/details/7722240 void log_info(std::string log) { - cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\033[0m" << endl; +#ifdef GAMELAB_WIN + if (s_ConsoleHandle == 0) { + s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + } + SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); + cout << currentDateTime() << " [Info] " << log << endl; +#else + cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl; +#endif } void log_warning(std::string log) { - cout << "\x1B[93m" << currentDateTime() << " [Wanning] " << log << "\033[0m" << endl; +#ifdef GAMELAB_WIN + if (s_ConsoleHandle == 0) { + s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + } + SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); + cout << currentDateTime() << " [Info] " << log << endl; +#else + cout << "\x1B[93m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl; +#endif } void log_error(std::string log) { - cout << "\x1B[91m" << currentDateTime() << " [Error] " << log << "\033[0m" << endl; +#ifdef GAMELAB_WIN + if (s_ConsoleHandle == 0) { + s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + } + SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_INTENSITY); + cout << currentDateTime() << " [Info] " << log << endl; +#else + cout << "\x1B[91m" << currentDateTime() << " [Info] " << log << "\x1B[0m" << endl; +#endif } void log_error_null_param(std::string funcName, std::string param) diff --git a/Runtime/Debug/Log.h b/Runtime/Debug/Log.h index d66f705..aaaaa78 100644 --- a/Runtime/Debug/Log.h +++ b/Runtime/Debug/Log.h @@ -1,14 +1,6 @@ #pragma once #include <string> -//enum LogTag : unsigned long -//{ -// -// All = ~0 -//}; -// -//extern long g_LogTags; - void log_open_tag(std::string tag); void log_info(std::string log); diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index c9183ff..7c5f043 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -284,6 +284,12 @@ namespace LuaBind return ((t == LUA_TNONE) || (t == LUA_TNIL)); } + bool State::IsTable(int idx) + { + int check = lua_type(mState, idx); + return check == LUA_TTABLE; + } + bool State::IsTableOrUserdata(int idx) { int check = lua_type(mState, idx); diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index c81ba8c..e7e2807 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -85,6 +85,7 @@ namespace LuaBind bool IsNil(int idx); bool IsNilOrNone(int idx); + bool IsTable(int idx); bool IsTableOrUserdata(int idx); bool IsTrueOrNotNil(int idx); bool IsType(int idx, int type); diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index e458e28..2044ba2 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -25,4 +25,38 @@ Vector2 State::GetValue < Vector2 >(int idx, const Vector2 value) int LuaHelper::Call(const char* func, const char* params, ...)
{
return 1;
+}
+
+void LuaHelper::OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName)
+{
+ // 填充类型的元数据
+ lua_newtable(state);
+ int typeIdx = state.GetTop();
+ state.Push("native");
+ state.SetField(typeIdx, "mode");
+ state.Push(clsName);
+ state.SetField(typeIdx, "name");
+ state.Push(pkgName);
+ state.SetField(typeIdx, "package");
+ state.Push(pkgName + '.' + clsName);
+ state.SetField(typeIdx, "fullName");
+ lua_setfield(state, cls, "_type");
+}
+
+bool LuaHelper::IsType(LuaBind::State& state, const char* typeName, int idx)
+{
+ int top = state.GetTop();
+ cc8* type = luaL_typename(state, idx);
+ if (strcmp(type, typeName) == 0)
+ return true;
+ state.GetField(idx, "_type");
+ if (!state.IsTable(-1))
+ {
+ state.Pop(1);
+ return false;
+ }
+ std::string tname = state.GetField(-1, "fullName", "");
+ bool bIsType = tname == typeName;
+ state.SetTop(top);
+ return bIsType;
}
\ No newline at end of file diff --git a/Runtime/Lua/LuaHelper.h b/Runtime/Lua/LuaHelper.h index 10d9421..56dae54 100644 --- a/Runtime/Lua/LuaHelper.h +++ b/Runtime/Lua/LuaHelper.h @@ -10,6 +10,8 @@ class LuaHelper public:
static int Call(const char* func, const char* params, ...);
-};
+ static bool IsType(LuaBind::State& state, const char* typeName, int idx);
+ static void OnRegisterNativeClass(LuaBind::State& state, int cls, std::string clsName, std::string pkgName);
+};
\ No newline at end of file diff --git a/Runtime/Scripting/GL/GL.bind.cpp b/Runtime/Scripting/GL/GL.bind.cpp index 9bc4204..f167237 100644 --- a/Runtime/Scripting/GL/GL.bind.cpp +++ b/Runtime/Scripting/GL/GL.bind.cpp @@ -1,5 +1,5 @@ #include "Runtime/Graphics/OpenGL.h" -#include "Runtime/Lua/LuaBind/LuaBind.h" +#include "Runtime/Lua/LuaHelper.h" #include "Runtime/Debug/Log.h" using namespace LuaBind; @@ -8,21 +8,231 @@ using namespace LuaBind; enum EBufferType { - COLOR_BUFFER = 1, - DEPTH_BUFFER = 2, - STENCIL_BUFFER = 3, + ColorBuffer = 1, + DepthBuffer = 2, + StencilBuffer = 3, }; -// GL.Clear([GL.EBufferType.COLOR_BUFFER, GL.EBufferType.DEPTH_BUFFER, GL.EBufferType.STENCIL_BUFFER]) +enum EPrimitiveType +{ + Triangles = 1, + Lines = 2, + Points = 3, +}; + +enum EMatrixMode +{ + Projection = 1, + ModelView = 2, +}; + +// GL.Clear([GL.EBufferType.ColorBuffer, GL.EBufferType.DepthBuffer, GL.EBufferType.StencilBuffer]) int Clear(lua_State* L) { LUA_BIND_STATE(L); - + GLint clearFlag = 0; + int n = state.GetTop(); + for (int i = 1; i <= n; ++i) + { + int buffer = state.GetValue(i, 0); + if (buffer == EBufferType::ColorBuffer) + clearFlag |= GL_COLOR_BUFFER_BIT; + else if(buffer == EBufferType::DepthBuffer) + clearFlag |= GL_DEPTH_BUFFER_BIT; + else if (buffer == EBufferType::StencilBuffer) + clearFlag |= GL_STENCIL_BUFFER_BIT; + } + glClear(clearFlag); + return 0; +} + +// GL.ClearColor(color32) +// GL.ClearColor(color) +// GL.ClearColor({r,g,b,a}) +int ClearColor(lua_State* L) +{ + LUA_BIND_STATE(L); + if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color", -1)) + { + float r = state.GetField(-1, "r", 0); + float g = state.GetField(-1, "g", 0); + float b = state.GetField(-1, "b", 0); + float a = state.GetField(-1, "a", 0); + glClearColor(r, g, b, a); + } + else if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color32", -1)) + { + float r = state.GetField(-1, "r", 0) / 255.f; + float g = state.GetField(-1, "g", 0) / 255.f; + float b = state.GetField(-1, "b", 0) / 255.f; + float a = state.GetField(-1, "a", 0) / 255.f; + glClearColor(r, g, b, a); + } + else if (LuaHelper::IsType(state, "table", -1)) + { + float r = state.GetField(-1, 1, 0); + float g = state.GetField(-1, 2, 0); + float b = state.GetField(-1, 3, 0); + float a = state.GetField(-1, 4, 0); + glClearColor(r, g, b, a); + } + else + { + state.ErrorType(1, "Color or Color32 or color table"); + } + return 0; +} + +// GL.Begin([EPrimitiveType.Triangles, EPrimitiveType.Lines]); +int Begin(lua_State* L) +{ + LUA_BIND_STATE(L); + int primitive = state.GetValue(1, (int)EPrimitiveType::Triangles); + if (primitive == EPrimitiveType::Triangles) + { + glBegin(GL_TRIANGLES); + } + else if (primitive == EPrimitiveType::Lines) + { + glBegin(GL_LINES); + } + else if(primitive == EPrimitiveType::Points) + { + glBegin(GL_POINTS); + } + else + { + luaL_error(state, "wrong primitive type %d" , primitive); + } + return 0; +} + +int End(lua_State* L) +{ + glEnd(); + return 0; +} + +// GL.Vertex([vector3, vector3Table]) +int Vertex(lua_State* L) +{ + LUA_BIND_STATE(L); + if (LuaHelper::IsType(state, "GameLab.Engine.Math.Vector3", 1)) + { + float x = state.GetField(-1, "x", 0); + float y = state.GetField(-1, "y", 0); + float z = state.GetField(-1, "z", 0); + glVertex3f(x, y, z); + } + else if (LuaHelper::IsType(state, "table", -1)) + { + float x = state.GetField(-1, 1, 0); + float y = state.GetField(-1, 2, 0); + float z = state.GetField(-1, 3, 0); + glVertex3f(x, y, z); + } + else + { + state.ErrorType(1, "Vector3 or vector3 table"); + } + return 0; +} + +// GL.Color([Color, Color32, Color table ]) +int Color(lua_State* L) +{ + LUA_BIND_STATE(L); + int top = state.GetTop(); + if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color", -1)) + { + float r = state.GetField(-1, "r", 0); + float g = state.GetField(-1, "g", 0); + float b = state.GetField(-1, "b", 0); + float a = state.GetField(-1, "a", 0); + glColor4f(r, g, b, a); + } + else if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color32", -1)) + { + float r = state.GetField(-1, "r", 0) / 255.f; + float g = state.GetField(-1, "g", 0) / 255.f; + float b = state.GetField(-1, "b", 0) / 255.f; + float a = state.GetField(-1, "a", 0) / 255.f; + glColor4f(r, g, b, a); + } + else if (LuaHelper::IsType(state, "table", -1)) + { + float r = state.GetField(-1, 1, 0); + float g = state.GetField(-1, 2, 0); + float b = state.GetField(-1, 3, 0); + float a = state.GetField(-1, 4, 0); + glColor4f(r, g, b, a); + } + else + { + state.ErrorType(1, "Color or Color32 or color table"); + } + state.SetTop(top); + return 0; +} + +int LoadIdentity(lua_State* L) +{ + glLoadIdentity(); + return 0; +} + +int PushMatrix(lua_State* L) +{ + glPushMatrix(); + return 0; +} + +int PopMatrix(lua_State* L) +{ + glPopMatrix(); + return 0; +} + +int MatrixMode(lua_State* L) +{ + LUA_BIND_STATE(L); + int matrixMode = state.GetValue(1, (int)EMatrixMode::ModelView); + if (matrixMode == EMatrixMode::ModelView) + { + glMatrixMode(GL_MODELVIEW); + } + else if (matrixMode == EMatrixMode::Projection) + { + glMatrixMode(GL_PROJECTION); + } + else + { + luaL_error(state, "wrong matrix type %d", matrixMode); + } + return 0; +} + +// GL.LoadMatrix([Matrix4x4, mat44ColumnMajorTable]) +int LoadMatrix(lua_State* L) +{ + LUA_BIND_STATE(L); + return 0; } static luaL_Reg glFuncs[] = { + {"ClearColor", ClearColor}, {"Clear", Clear}, + // immediate mode apis + {"Color", Color}, + {"Begin", Begin}, + {"End", End}, + {"Vertex", Vertex }, + {"LoadIdentity", LoadIdentity}, + {"PushMatrix", PushMatrix}, + {"PopMatrix", PopMatrix}, + {"MatrixMode", MatrixMode}, + {"LoadMatrix", LoadMatrix}, {0, 0} }; @@ -41,9 +251,20 @@ int luaopen_GameLab_Engine_GL(lua_State* L) state.RegisterMethods(glFuncs); LUA_BIND_REGISTER_ENUM(state, "EBufferType", - { "ColorBuffer", EBufferType::COLOR_BUFFER }, - { "DepthBuffer", EBufferType::DEPTH_BUFFER}, - { "StencilBuffer", EBufferType::STENCIL_BUFFER} + { "ColorBuffer", EBufferType::ColorBuffer }, + { "DepthBuffer", EBufferType::DepthBuffer}, + { "StencilBuffer", EBufferType::StencilBuffer} + ); + + LUA_BIND_REGISTER_ENUM(state, "EPrimitiveType", + { "Triangles", EPrimitiveType::Triangles }, + { "Lines", EPrimitiveType::Lines }, + { "Points", EPrimitiveType::Points } + ); + + LUA_BIND_REGISTER_ENUM(state, "EMatrixMode", + { "MatrixView", EMatrixMode::ModelView }, + { "Projection", EMatrixMode::Projection} ); return 1; |