aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/3rdparty/luax/luax.h40
-rw-r--r--src/libjin/core/game.h5
-rw-r--r--src/libjin/render/window.cpp41
-rw-r--r--src/libjin/render/window.h23
-rw-r--r--src/libjin/utils/macros.h3
-rw-r--r--src/script/embed/boot.lua.h26
-rw-r--r--src/script/graphics/luaopen_graphics.cpp23
7 files changed, 109 insertions, 52 deletions
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h
index 49036b9..b5ef0f4 100644
--- a/src/3rdparty/luax/luax.h
+++ b/src/3rdparty/luax/luax.h
@@ -50,6 +50,8 @@
*/
#define luax_gettop lua_gettop
+#define luax_gettable lua_gettable
+
/**
* Check userdata type.
*/
@@ -57,6 +59,20 @@
#define luax_checknumber luaL_checknumber
#define luax_checkinteger luaL_checkinteger
#define luax_checkstring luaL_checkstring
+#define luax_checkbool luaL_checkinteger
+//bool luax_checkbool(lua_State *L, int numArg)
+//{
+// bool b = false;
+// if (lua_type(L, numArg) == LUA_TBOOLEAN)
+// {
+// b = lua_toboolean(L, numArg);
+// }
+// else
+// {
+// luaL_typerror(L, numArg, lua_typename(L, LUA_TBOOLEAN));
+// }
+// return b;
+//}
/**
* Oprating tables.
@@ -103,6 +119,30 @@
* If nosuch field push a nil at the top of stack.
*/
#define luax_getfield(L, I, N) lua_getfield(L, I, N)
+inline float luax_getfield_number(lua_State* L, int I, const char* N)
+{
+ luax_getfield(L, I, N);
+ float n = luax_checknumber(L, -1);
+ return n;
+}
+inline int luax_getfield_integer(lua_State* L, int I, const char* N)
+{
+ luax_getfield(L, I, N);
+ int bin = luax_checkinteger(L, -1);
+ return bin;
+}
+inline const char* luax_getfield_string(lua_State* L, int I, const char* N)
+{
+ luax_getfield(L, I, N);
+ const char* str = luax_checkstring(L, -1);
+ return str;
+}
+inline char luax_getfield_bool(lua_State* L, int I, const char* N)
+{
+ luax_getfield(L, I, N);
+ char bin = lua_toboolean(L, -1);
+ return bin;
+}
/**
* Set raw
diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h
index d56b249..069566b 100644
--- a/src/libjin/core/game.h
+++ b/src/libjin/core/game.h
@@ -8,6 +8,11 @@ namespace core
class Game
{
public:
+
+ struct Setting
+ {
+
+ };
static inline Game* get()
{
diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp
index 0179877..d481793 100644
--- a/src/libjin/render/window.cpp
+++ b/src/libjin/render/window.cpp
@@ -1,7 +1,8 @@
#include "window.h"
#include "3rdparty/GLee/GLee.h"
#include "canvas.h"
-#include "../utils/macros.h"
+#include "../utils/utils.h"
+
namespace jin
{
namespace render
@@ -16,18 +17,25 @@ namespace render
Window::~Window()
{
}
-
- void Window::init(int pw, int ph, const char* t)
+
+ onlyonce void Window::init(const Window::Setting& setting)
+ {
+ CallOnce(_init(setting));
+ }
+
+ inline void Window::_init(const Setting& setting)
{
- w = pw;
- h = ph;
+ width = setting.width;
+ height = setting.height;
+ const char* title = setting.title;
+ bool vsync = setting.vsync;
if (wnd)
{
- SDL_DestroyWindow(wnd);
+ SDL_DestroyWindow(wnd);
SDL_FlushEvent(SDL_WINDOWEVENT);
}
-
+
if (ctx)
{
SDL_GL_DeleteContext(ctx);
@@ -39,29 +47,24 @@ namespace render
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
+ SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- int wx = SDL_WINDOWPOS_UNDEFINED,
- wy = SDL_WINDOWPOS_UNDEFINED;
+ int wx = SDL_WINDOWPOS_UNDEFINED,
+ wy = SDL_WINDOWPOS_UNDEFINED;
- wnd = SDL_CreateWindow(t, wx, wy, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
+ wnd = SDL_CreateWindow(title, wx, wy, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
ctx = SDL_GL_CreateContext(wnd);
- // ֱͬ󽵵GPUռ8%->4%
- SDL_GL_SetSwapInterval(1);
+ SDL_GL_SetSwapInterval(vsync ? 1 : 0);
SDL_GL_MakeCurrent(wnd, ctx);
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColor4f(1, 1, 1, 1);
- /**
- * Set the viewport to top-left corner.
- * Bind to the default render buffer.
- */
+
Canvas::unbind();
- // Swap window buffer
swapBuffers();
}
-
+
SDL_Window* Window::getWnd()
{
return wnd;
diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h
index 515ffff..8066f8a 100644
--- a/src/libjin/render/window.h
+++ b/src/libjin/render/window.h
@@ -1,6 +1,8 @@
#ifndef __JIN_RENDER_WINDOW
#define __JIN_RENDER_WINDOW
-#include "SDL2/SDL.h"
+#include <SDL2/SDL.h>
+#include "../utils/utils.h"
+
namespace jin
{
namespace render
@@ -8,8 +10,15 @@ namespace render
class Window
{
public:
-
- void init(int w, int h, const char* t);
+
+ struct Setting
+ {
+ int width, height; // ڴС
+ bool vsync; // ֱͬ
+ const char* title; //
+ };
+
+ onlyonce void init(const Setting& setting);
SDL_Window* getWnd();
@@ -22,12 +31,12 @@ namespace render
inline int getW()
{
- return w;
+ return width;
}
inline int getH()
{
- return h;
+ return height;
}
inline void swapBuffers();
@@ -43,7 +52,9 @@ namespace render
SDL_GLContext ctx;
- int w, h;
+ int width, height;
+
+ inline void _init(const Setting& setting);
};
}
}
diff --git a/src/libjin/utils/macros.h b/src/libjin/utils/macros.h
index 4f739a9..2ca8a9a 100644
--- a/src/libjin/utils/macros.h
+++ b/src/libjin/utils/macros.h
@@ -3,4 +3,7 @@
#define shared
+#define CallOnce(func) static char __dummy__=(func, 1) // ֻܵһ
+#define onlyonce // ֻܵһ
+
#endif \ No newline at end of file
diff --git a/src/script/embed/boot.lua.h b/src/script/embed/boot.lua.h
index 822e1cf..ee1cbd3 100644
--- a/src/script/embed/boot.lua.h
+++ b/src/script/embed/boot.lua.h
@@ -26,14 +26,15 @@ jin.filesystem.mount(jin._argv[2])
local conf = {}
if jin.filesystem.exist("config.lua") then
conf = require "config"
-end
-conf.width = conf.width or 600
+end
+conf.width = conf.width or 600
conf.height = conf.height or 500
-conf.fps = conf.fps or 60
-conf.title = conf.title or ("jin v" .. jin.version())
+conf.fps = conf.fps or 60
+conf.vsync = conf.vsync or false
+conf.title = conf.title or ("jin v" .. jin.version())
--- init video subsystem
-jin.graphics.init(conf.width,conf.height,conf.title)
+-- init video subsystem
+jin.graphics.init(conf)
-- open debug mode, must after jin.graphics.init
if jin._argv[3] == '-d' then
@@ -54,13 +55,18 @@ function jin.core.run()
local onUpdate = jin.core.onUpdate
local present = jin.graphics.present
+ local dstatus = jin.debug.status
+ local drender = jin.debug.render
+
+ local fps = conf.fps
+
if load then
load()
end
local now = second()
local last = now
- local fsec = 1/conf.fps
+ local fsec = 1/fps
local dt = 0
while(running()) do
@@ -71,7 +77,7 @@ function jin.core.run()
sleep(fsec - now + last)
end
- -- handle events
+ -- handle events
for _, e in pairs(poll()) do
if _onEvent then
_onEvent(e)
@@ -99,8 +105,8 @@ function jin.core.run()
end
-- render debug window
- if jin.debug.status() then
- jin.debug.render()
+ if dstatus() then
+ drender()
end
-- swap window buffer
diff --git a/src/script/graphics/luaopen_graphics.cpp b/src/script/graphics/luaopen_graphics.cpp
index 0d240fe..50b1921 100644
--- a/src/script/graphics/luaopen_graphics.cpp
+++ b/src/script/graphics/luaopen_graphics.cpp
@@ -18,13 +18,10 @@ namespace lua
*/
static struct
{
- // current render color
color curRenderColor;
- // currently used font
Font* curFont = 0;
- // default ingame font
Font* defaultFont = 0;
} context;
@@ -35,21 +32,13 @@ namespace lua
*/
static int l_init(lua_State* L)
{
- int w = luax_checknumber(L, 1);
- int h = luax_checknumber(L, 2);
- const char* t = luaL_checkstring(L, 3);
-
- // init video subsystem
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- luax_error(L, "could not init video");
- luax_pushboolean(L, false);
- return 1;
- }
-
- // init window system
Window* wnd = Window::get();
- wnd->init(w, h, t);
+ Window::Setting setting;
+ setting.width = luax_getfield_integer(L, 1, "width");
+ setting.height = luax_getfield_integer(L, 1, "height");
+ setting.title = luax_getfield_string(L, 1, "title");
+ setting.vsync = luax_getfield_bool(L, 1, "vsync");
+ wnd->init(setting);
// set default blend method
glEnable(GL_BLEND);