Skip to content

Commit d34aa41

Browse files
committed
LuaEngine v0.4
SDL2_gfx built-in support
1 parent 71059fd commit d34aa41

11 files changed

+16905
-1
lines changed

Renderer.cpp

+135
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "include.h"
2+
#include <SDL2/SDL2_gfxPrimitives.h>
23

34
/*
45
class Renderer
@@ -8,6 +9,15 @@ class Renderer
89
copyTo(text: Texture, x: int, y: int, [w: int, h: int])
910
copyFill(text: Texture, x: int, y: int, w: int, h: int)
1011
copyFullFill(text: Texture)
12+
drawPoint(x: int, y: int)
13+
drawLine(x1: int, y1: int, x2: int, y2: int)
14+
drawRect(x: int, y: int, w: int, h: int)
15+
fillRect(x: int, y: int, w: int, h: int)
16+
fillTriangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, [r: int, g: int, b:int, a: int]) #SDL2_gfx#
17+
getColor(): r: int, g: int, b: int, a: int
18+
setColor(r: int, g: int, b: int, a: int)
19+
getBlendMode(): string
20+
setBlendMode(mode: string)
1121
*/
1222

1323
int render_close(lua_State* L)
@@ -212,6 +222,128 @@ int render_fillrect(lua_State* L)
212222
return 0;
213223
}
214224

225+
int render_filltriangle(lua_State* L)
226+
{
227+
auto rnd = lua_checkpointer<SDL_Renderer>(L, 1, "LuaEngineRenderer");
228+
229+
int x1 = luaL_checkinteger(L, 2);
230+
int y1 = luaL_checkinteger(L, 3);
231+
int x2 = luaL_checkinteger(L, 4);
232+
int y2 = luaL_checkinteger(L, 5);
233+
int x3 = luaL_checkinteger(L, 6);
234+
int y3 = luaL_checkinteger(L, 7);
235+
236+
Uint8 r, g, b, a;
237+
if (SDL_GetRenderDrawColor(rnd, &r, &g, &b, &a))
238+
{
239+
return SDLError(L, SDL_GetRenderDrawColor);
240+
}
241+
SDL_BlendMode mode;
242+
if (SDL_GetRenderDrawBlendMode(rnd, &mode))
243+
{
244+
return SDLError(L, SDL_GetRenderDrawBlendMode);
245+
}
246+
247+
Uint8 rr, gg, bb, aa;
248+
rr = lua_isnone(L, 8) ? r : luaL_checkinteger(L, 8);
249+
gg = lua_isnone(L, 9) ? g : luaL_checkinteger(L, 9);
250+
bb = lua_isnone(L, 10) ? b : luaL_checkinteger(L, 10);
251+
aa = lua_isnone(L, 11) ? a : luaL_checkinteger(L, 11);
252+
253+
int ret = filledTrigonRGBA(rnd, x1, y1, x2, y2, x3, y3, rr, gg, bb, aa);
254+
255+
if (SDL_SetRenderDrawBlendMode(rnd, mode))
256+
{
257+
return SDLError(L, SDL_SetRenderDrawBlendMode);
258+
}
259+
if (SDL_SetRenderDrawColor(rnd, r, g, b, a))
260+
{
261+
return SDLError(L, SDL_SetRenderDrawColor);
262+
}
263+
264+
if(ret)
265+
{
266+
return SDLError(L, filledTrigonRGBA);
267+
}
268+
269+
return 0;
270+
}
271+
272+
int render_getblendmode(lua_State* L)
273+
{
274+
auto rnd = lua_checkpointer<SDL_Renderer>(L, 1, "LuaEngineRenderer");
275+
SDL_BlendMode mode;
276+
if (SDL_GetRenderDrawBlendMode(rnd, &mode))
277+
{
278+
return SDLError(L, SDL_GetRenderDrawBlendMode);
279+
}
280+
switch (mode)
281+
{
282+
case SDL_BlendMode::SDL_BLENDMODE_ADD:
283+
lua_pushstring(L, "add");
284+
break;
285+
case SDL_BlendMode::SDL_BLENDMODE_BLEND:
286+
lua_pushstring(L, "blend");
287+
break;
288+
case SDL_BlendMode::SDL_BLENDMODE_INVALID:
289+
lua_pushstring(L, "invalid");
290+
break;
291+
case SDL_BlendMode::SDL_BLENDMODE_MOD:
292+
lua_pushstring(L, "mod");
293+
break;
294+
case SDL_BlendMode::SDL_BLENDMODE_MUL:
295+
lua_pushstring(L, "mul");
296+
break;
297+
case SDL_BlendMode::SDL_BLENDMODE_NONE:
298+
lua_pushstring(L, "none");
299+
break;
300+
default:
301+
lua_pushstring(L, "unknown");
302+
break;
303+
}
304+
return 1;
305+
}
306+
307+
int render_setblendmode(lua_State* L)
308+
{
309+
auto rnd = lua_checkpointer<SDL_Renderer>(L, 1, "LuaEngineRenderer");
310+
const char* modestr = luaL_checkstring(L, 2);
311+
SDL_BlendMode mode;
312+
if (strcmp(modestr, "add") == 0)
313+
{
314+
mode = SDL_BlendMode::SDL_BLENDMODE_ADD;
315+
}
316+
else if (strcmp(modestr, "blend") == 0)
317+
{
318+
mode = SDL_BlendMode::SDL_BLENDMODE_BLEND;
319+
}
320+
else if (strcmp(modestr, "invalid") == 0)
321+
{
322+
mode = SDL_BlendMode::SDL_BLENDMODE_INVALID;
323+
}
324+
else if (strcmp(modestr, "mod") == 0)
325+
{
326+
mode = SDL_BlendMode::SDL_BLENDMODE_MOD;
327+
}
328+
else if (strcmp(modestr, "mul") == 0)
329+
{
330+
mode = SDL_BlendMode::SDL_BLENDMODE_MUL;
331+
}
332+
else if (strcmp(modestr, "none") == 0)
333+
{
334+
mode = SDL_BlendMode::SDL_BLENDMODE_NONE;
335+
}
336+
else
337+
{
338+
return 0;
339+
}
340+
if (SDL_SetRenderDrawBlendMode(rnd, mode))
341+
{
342+
return SDLError(L, SDL_SetRenderDrawBlendMode);
343+
}
344+
return 0;
345+
}
346+
215347
int render_setcolor(lua_State* L)
216348
{
217349
auto rnd = lua_checkpointer<SDL_Renderer>(L, 1, "LuaEngineRenderer");
@@ -267,8 +399,11 @@ int render_new(lua_State* L)
267399
lua_setfield_function(L, "drawLine", render_drawline);
268400
lua_setfield_function(L, "drawRect", render_drawrect);
269401
lua_setfield_function(L, "fillRect", render_fillrect);
402+
lua_setfield_function(L, "fillTriangle", render_filltriangle);
270403
lua_setfield_function(L, "getColor", render_getcolor);
271404
lua_setfield_function(L, "setColor", render_setcolor);
405+
lua_setfield_function(L, "getBlendMode", render_getblendmode);
406+
lua_setfield_function(L, "setBlendMode", render_setblendmode);
272407
lua_setfield(L, -2, "__index");
273408
}
274409
lua_setmetatable(L, -2);

include/SDL2/SDL2_framerate.h

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
3+
SDL2_framerate.h: framerate manager
4+
5+
Copyright (C) 2012-2014 Andreas Schiffler
6+
7+
This software is provided 'as-is', without any express or implied
8+
warranty. In no event will the authors be held liable for any damages
9+
arising from the use of this software.
10+
11+
Permission is granted to anyone to use this software for any purpose,
12+
including commercial applications, and to alter it and redistribute it
13+
freely, subject to the following restrictions:
14+
15+
1. The origin of this software must not be misrepresented; you must not
16+
claim that you wrote the original software. If you use this software
17+
in a product, an acknowledgment in the product documentation would be
18+
appreciated but is not required.
19+
20+
2. Altered source versions must be plainly marked as such, and must not be
21+
misrepresented as being the original software.
22+
23+
3. This notice may not be removed or altered from any source
24+
distribution.
25+
26+
Andreas Schiffler -- aschiffler at ferzkopp dot net
27+
28+
*/
29+
30+
#ifndef _SDL2_framerate_h
31+
#define _SDL2_framerate_h
32+
33+
/* Set up for C function definitions, even when using C++ */
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
38+
/* --- */
39+
40+
#include "SDL.h"
41+
42+
/* --------- Definitions */
43+
44+
/*!
45+
\brief Highest possible rate supported by framerate controller in Hz (1/s).
46+
*/
47+
#define FPS_UPPER_LIMIT 200
48+
49+
/*!
50+
\brief Lowest possible rate supported by framerate controller in Hz (1/s).
51+
*/
52+
#define FPS_LOWER_LIMIT 1
53+
54+
/*!
55+
\brief Default rate of framerate controller in Hz (1/s).
56+
*/
57+
#define FPS_DEFAULT 30
58+
59+
/*!
60+
\brief Structure holding the state and timing information of the framerate controller.
61+
*/
62+
typedef struct {
63+
Uint32 framecount;
64+
float rateticks;
65+
Uint32 baseticks;
66+
Uint32 lastticks;
67+
Uint32 rate;
68+
} FPSmanager;
69+
70+
/* ---- Function Prototypes */
71+
72+
#ifdef _MSC_VER
73+
# if defined(DLL_EXPORT) && !defined(LIBSDL2_GFX_DLL_IMPORT)
74+
# define SDL2_FRAMERATE_SCOPE __declspec(dllexport)
75+
# else
76+
# ifdef LIBSDL2_GFX_DLL_IMPORT
77+
# define SDL2_FRAMERATE_SCOPE __declspec(dllimport)
78+
# endif
79+
# endif
80+
#endif
81+
#ifndef SDL2_FRAMERATE_SCOPE
82+
# define SDL2_FRAMERATE_SCOPE extern
83+
#endif
84+
85+
/* Functions return 0 or value for sucess and -1 for error */
86+
87+
SDL2_FRAMERATE_SCOPE void SDL_initFramerate(FPSmanager * manager);
88+
SDL2_FRAMERATE_SCOPE int SDL_setFramerate(FPSmanager * manager, Uint32 rate);
89+
SDL2_FRAMERATE_SCOPE int SDL_getFramerate(FPSmanager * manager);
90+
SDL2_FRAMERATE_SCOPE int SDL_getFramecount(FPSmanager * manager);
91+
SDL2_FRAMERATE_SCOPE Uint32 SDL_framerateDelay(FPSmanager * manager);
92+
93+
/* --- */
94+
95+
/* Ends C function definitions when using C++ */
96+
#ifdef __cplusplus
97+
}
98+
#endif
99+
100+
#endif /* _SDL2_framerate_h */

0 commit comments

Comments
 (0)