From 08237332c072b2150b063cdfe21bd083960b195c Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sun, 5 Jul 2020 17:15:10 +0800 Subject: [PATCH] LuaEngine v0.5.5 Code refactor. More font function supported. --- Font.cpp | 154 +++++++++++++++++++++++++++++------------- Socket.h | 28 ++++++++ SocketLinux.cpp | 21 ++++++ SocketWin.cpp | 18 +++++ TCP.cpp | 70 +++++-------------- TCP.h | 21 ------ UDPWin.cpp => UDP.cpp | 22 ++++-- UDP.h | 16 ----- 8 files changed, 206 insertions(+), 144 deletions(-) create mode 100644 SocketLinux.cpp create mode 100644 SocketWin.cpp delete mode 100644 TCP.h rename UDPWin.cpp => UDP.cpp (93%) delete mode 100644 UDP.h diff --git a/Font.cpp b/Font.cpp index ae0e2b7..111012b 100644 --- a/Font.cpp +++ b/Font.cpp @@ -4,8 +4,12 @@ class Font constructor(filename: string, size: int) constructor(data: string, size: int, flag: int) flag is not used currently. - renderText(str: string, r: int, g: int, b: int, a: int): Surface - renderText(rnd: Renderer, str: string, r: int, g: int, b: int, a: int): Texture + renderText(str: utf-8 string, r: int, g: int, b: int, a: int): Surface + renderText(rnd: Renderer, str: utf-8 string, r: int, g: int, b: int, a: int): Texture + renderTextSolid(str: utf-8 string, r: int, g: int, b: int, a: int): Surface + renderTextSolid(rnd: Renderer, str: utf-8 string, r: int, g: int, b: int, a: int): Texture + renderTextShaded(str: utf-8 string, fr: int, fg: int, fb: int, fa: int, br: int, bg: int, bb: int, ba: int): Surface + renderTextShaded(rnd: Renderer, str: utf-8 string, fr: int, fg: int, fb: int, fa: int, br: int, bg: int, bb: int, ba: int): Texture */ int font_close(lua_State* L) @@ -15,50 +19,6 @@ int font_close(lua_State* L) return 0; } -int font_rendertext(lua_State* L) -{ - auto font = lua_checkpointer(L, 1, "LuaEngineFont"); - const char* str; - SDL_Color color; - if (lua_isstring(L, 2)) - { - str = luaL_checkstring(L, 2); - color.r = luaL_checkinteger(L, 3); - color.g = luaL_checkinteger(L, 4); - color.b = luaL_checkinteger(L, 5); - color.a = luaL_checkinteger(L, 6); - SDL_Surface* surf = TTF_RenderText_Blended(font, str, color); - if (!surf) - { - return TTFError(L, TTF_RenderText_Blended); - } - put_surface(L, surf); - return 1; - } - else - { - auto rnd = lua_checkpointer(L, 2, "LuaEngineRenderer"); - str = luaL_checkstring(L, 3); - color.r = luaL_checkinteger(L, 4); - color.g = luaL_checkinteger(L, 5); - color.b = luaL_checkinteger(L, 6); - color.a = luaL_checkinteger(L, 7); - SDL_Surface* surf = TTF_RenderText_Blended(font, str, color); - if (!surf) - { - return TTFError(L, TTF_RenderText_Blended); - } - SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf); - SDL_FreeSurface(surf); - if (!text) - { - return SDLError(L, SDL_CreateTextureFromSurface); - } - put_texture(L, text); - return 1; - } -} - int font_renderutf8(lua_State* L) { auto font = lua_checkpointer(L, 1, "LuaEngineFont"); @@ -103,6 +63,103 @@ int font_renderutf8(lua_State* L) } } +int font_renderutf8_solid(lua_State* L) +{ + auto font = lua_checkpointer(L, 1, "LuaEngineFont"); + const char* str; + SDL_Color color; + if (lua_isstring(L, 2)) + { + str = luaL_checkstring(L, 2); + color.r = luaL_checkinteger(L, 3); + color.g = luaL_checkinteger(L, 4); + color.b = luaL_checkinteger(L, 5); + color.a = luaL_checkinteger(L, 6); + SDL_Surface* surf = TTF_RenderUTF8_Solid(font, str, color); + if (!surf) + { + return TTFError(L, TTF_RenderUTF8_Solid); + } + put_surface(L, surf); + return 1; + } + else + { + auto rnd = lua_checkpointer(L, 2, "LuaEngineRenderer"); + str = luaL_checkstring(L, 3); + color.r = luaL_checkinteger(L, 4); + color.g = luaL_checkinteger(L, 5); + color.b = luaL_checkinteger(L, 6); + color.a = luaL_checkinteger(L, 7); + SDL_Surface* surf = TTF_RenderUTF8_Solid(font, str, color); + if (!surf) + { + return TTFError(L, TTF_RenderUTF8_Solid); + } + SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf); + SDL_FreeSurface(surf); + if (!text) + { + return SDLError(L, SDL_CreateTextureFromSurface); + } + put_texture(L, text); + return 1; + } +} + +int font_renderutf8_shaded(lua_State* L) +{ + auto font = lua_checkpointer(L, 1, "LuaEngineFont"); + const char* str; + SDL_Color frontColor; + SDL_Color backColor; + if (lua_isstring(L, 2)) + { + str = luaL_checkstring(L, 2); + frontColor.r = luaL_checkinteger(L, 3); + frontColor.g = luaL_checkinteger(L, 4); + frontColor.b = luaL_checkinteger(L, 5); + frontColor.a = luaL_checkinteger(L, 6); + backColor.r = luaL_checkinteger(L, 7); + backColor.g = luaL_checkinteger(L, 8); + backColor.b = luaL_checkinteger(L, 9); + backColor.a = luaL_checkinteger(L, 10); + SDL_Surface* surf = TTF_RenderUTF8_Shaded(font, str, frontColor, backColor); + if (!surf) + { + return TTFError(L, TTF_RenderUTF8_Shaded); + } + put_surface(L, surf); + return 1; + } + else + { + auto rnd = lua_checkpointer(L, 2, "LuaEngineRenderer"); + str = luaL_checkstring(L, 3); + frontColor.r = luaL_checkinteger(L, 4); + frontColor.g = luaL_checkinteger(L, 5); + frontColor.b = luaL_checkinteger(L, 6); + frontColor.a = luaL_checkinteger(L, 7); + backColor.r = luaL_checkinteger(L, 8); + backColor.g = luaL_checkinteger(L, 9); + backColor.b = luaL_checkinteger(L, 10); + backColor.a = luaL_checkinteger(L, 11); + SDL_Surface* surf = TTF_RenderUTF8_Shaded(font, str, frontColor, backColor); + if (!surf) + { + return TTFError(L, TTF_RenderUTF8_Shaded); + } + SDL_Texture* text = SDL_CreateTextureFromSurface(rnd, surf); + SDL_FreeSurface(surf); + if (!text) + { + return SDLError(L, SDL_CreateTextureFromSurface); + } + put_texture(L, text); + return 1; + } +} + int font_new(lua_State* L) { TTF_Font* font; @@ -136,8 +193,9 @@ int font_new(lua_State* L) { lua_setfield_function(L, "__gc", font_close); lua_newtable(L); - lua_setfield_function(L, "renderText", font_rendertext); - lua_setfield_function(L, "renderUTF8", font_renderutf8); + lua_setfield_function(L, "renderText", font_renderutf8); + lua_setfield_function(L, "renderTextSolid", font_renderutf8_solid); + lua_setfield_function(L, "renderTextShaded", font_renderutf8_shaded); lua_setfield(L, -2, "__index"); } lua_setmetatable(L, -2); diff --git a/Socket.h b/Socket.h index 0aa5f46..84d5a1f 100644 --- a/Socket.h +++ b/Socket.h @@ -1,6 +1,34 @@ #include "include.h" #include + +#ifdef _WIN32 +#define _WIN32_WINNT 0x0A00 // Win10 +#include +#include + +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define closesocket close +#define WSAGetLastError() errno +#define WSAEWOULDBLOCK EWOULDBLOCK +#define put_winerror put_linuxerror + +#endif + +int SetSocketBlocking(lua_State* L, int fd, bool blocking); + class SocketData { public: diff --git a/SocketLinux.cpp b/SocketLinux.cpp new file mode 100644 index 0000000..10f1ac8 --- /dev/null +++ b/SocketLinux.cpp @@ -0,0 +1,21 @@ +#include "Socket.h" + +#ifndef _WIN32 + +int SetSocketBlocking(lua_State* L, int fd, bool blocking) +{ + const int flags = fcntl(fd, F_GETFL, 0); + if (((flags & O_NONBLOCK) && !blocking) || (!(flags & O_NONBLOCK) && blocking)) + { + return 0; + } + int ret = fcntl(fd, F_SETFL, blocking ? flags & ~O_NONBLOCK : flags | O_NONBLOCK); + if (ret) + { + put_linuxerror(L, errno, "fcntl"); + return lua_error(L); + } + return 0; +} + +#endif diff --git a/SocketWin.cpp b/SocketWin.cpp new file mode 100644 index 0000000..a8e93c4 --- /dev/null +++ b/SocketWin.cpp @@ -0,0 +1,18 @@ +#include "Socket.h" + +#ifdef _WIN32 + +int SetSocketBlocking(lua_State* L, int fd, bool blocking) +{ + u_long arg = blocking ? 0 : 1; + int ret = ioctlsocket(fd, FIONBIO, &arg); + if (ret) + { + int errcode = WSAGetLastError(); + put_winerror(L, errcode, "ioctlsocket"); + return lua_error(L); + } + return 0; +} + +#endif diff --git a/TCP.cpp b/TCP.cpp index 8d41026..370d4a5 100644 --- a/TCP.cpp +++ b/TCP.cpp @@ -1,59 +1,25 @@ -#include "TCP.h" +#include "Socket.h" #include -#ifdef _WIN32 -#define _WIN32_WINNT 0x0A00 // Win10 -#include -#include +/* +class TCPSocket + constructor(nonblocking: boolean) + close() + setblocking(nonblocking: boolean) + listen([ip: string], port: int, [backlog: int]) Address default to 0.0.0.0, backlog default to 10. -static int SetSocketBlocking(lua_State* L, int fd, bool blocking) -{ - u_long arg = blocking ? 0 : 1; - int ret = ioctlsocket(fd, FIONBIO, &arg); - if (ret) - { - int errcode = WSAGetLastError(); - put_winerror(L, errcode, "ioctlsocket"); - return lua_error(L); - } - return 0; -} + # Blocking mode: All unexpected error will be raised as exception. + connect(ip: string, port: int) + accept(): TCPSocket PeerIP PeerPort + send(data: string) All data will be sent before return. + recv([maxsize: int]): string. Default to 4KB. -#else -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static int SetSocketBlocking(lua_State* L, int fd, bool blocking) -{ - const int flags = fcntl(fd, F_GETFL, 0); - if( ((flags & O_NONBLOCK) && !blocking) || (!(flags & O_NONBLOCK) && blocking)) - { - return 0; - } - int ret = fcntl(fd, F_SETFL, blocking ? flags & ~O_NONBLOCK : flags | O_NONBLOCK); - if (ret) - { - put_linuxerror(L, errno, "fcntl"); - return lua_error(L); - } - return 0; -} - -#define closesocket close -#define WSAGetLastError() errno -#define WSAEWOULDBLOCK EWOULDBLOCK -#define put_winerror put_linuxerror - -#endif + # Non-blocking mode: + connect(ip: string, port: int) + accept(): (TCPSocket PeerIP PeerPort) or nil + send(data: string) + recv([maxsize: int]): string +*/ using namespace std; diff --git a/TCP.h b/TCP.h deleted file mode 100644 index 9f8ae39..0000000 --- a/TCP.h +++ /dev/null @@ -1,21 +0,0 @@ -#include "Socket.h" - -/* -class TCPSocket - constructor(nonblocking: boolean) - close() - setblocking(nonblocking: boolean) - listen([ip: string], port: int, [backlog: int]) Address default to 0.0.0.0, backlog default to 10. - - # Blocking mode: All unexpected error will be raised as exception. - connect(ip: string, port: int) - accept(): TCPSocket PeerIP PeerPort - send(data: string) All data will be sent before return. - recv([maxsize: int]): string. Default to 4KB. - - # Non-blocking mode: - connect(ip: string, port: int) - accept(): (TCPSocket PeerIP PeerPort) or nil - send(data: string) - recv([maxsize: int]): string -*/ diff --git a/UDPWin.cpp b/UDP.cpp similarity index 93% rename from UDPWin.cpp rename to UDP.cpp index a4612e5..75dd7f3 100644 --- a/UDPWin.cpp +++ b/UDP.cpp @@ -1,9 +1,19 @@ -#include "UDP.h" +#include "Socket.h" -#ifdef _WIN32 -#define _WIN32_WINNT 0x0A00 // Win10 -#include -#include +/* +class UDPSocket + constructor() + close() + setblocking(nonblocking: boolean) + setbroadcast(isbroadcast: boolean) Broadcast default to false. + listen([ip: string], port: int) Address default to 0.0.0.0. + + connect(ip: string, port: int) + send(data: string) + recv([maxsize: int]): string. Default to 4KB. + sendto([ip: string], port: int, data: string) If ip is empty, default to 255.255.255.255 (broadcast) + recvfrom(): data: string, ip: string, port: int +*/ int udp_socket_dtor(lua_State* L) { @@ -344,5 +354,3 @@ void InitUDPSocket(lua_State* L) lua_setfield(L, -2, "UDPSocket"); lua_pop(L, 2); } - -#endif // end of _WIN32 diff --git a/UDP.h b/UDP.h deleted file mode 100644 index e54784e..0000000 --- a/UDP.h +++ /dev/null @@ -1,16 +0,0 @@ -#include "Socket.h" - -/* -class UDPSocket - constructor() - close() - setblocking(nonblocking: boolean) - setbroadcast(isbroadcast: boolean) Broadcast default to false. - listen([ip: string], port: int) Address default to 0.0.0.0. - - connect(ip: string, port: int) - send(data: string) - recv([maxsize: int]): string. Default to 4KB. - sendto([ip: string], port: int, data: string) If ip is empty, default to 255.255.255.255 (broadcast) - recvfrom(): data: string, ip: string, port: int -*/