LuaEngine v0.5.5

Code refactor.
More font function supported.
This commit is contained in:
Kirigaya Kazuto 2020-07-05 17:15:10 +08:00
parent 466dd6ee73
commit 08237332c0
8 changed files with 206 additions and 144 deletions

154
Font.cpp
View File

@ -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<TTF_Font>(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<SDL_Renderer>(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<TTF_Font>(L, 1, "LuaEngineFont");
@ -103,6 +63,103 @@ int font_renderutf8(lua_State* L)
}
}
int font_renderutf8_solid(lua_State* L)
{
auto font = lua_checkpointer<TTF_Font>(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<SDL_Renderer>(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<TTF_Font>(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<SDL_Renderer>(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);

View File

@ -1,6 +1,34 @@
#include "include.h"
#include <vector>
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00 // Win10
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#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:

21
SocketLinux.cpp Normal file
View File

@ -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

18
SocketWin.cpp Normal file
View File

@ -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

70
TCP.cpp
View File

@ -1,59 +1,25 @@
#include "TCP.h"
#include "Socket.h"
#include <cstring>
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00 // Win10
#include <winsock2.h>
#include <ws2tcpip.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.
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 <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
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;

21
TCP.h
View File

@ -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
*/

View File

@ -1,9 +1,19 @@
#include "UDP.h"
#include "Socket.h"
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00 // Win10
#include <winsock2.h>
#include <ws2tcpip.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
*/
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

16
UDP.h
View File

@ -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
*/