aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-05-19 00:09:44 +0800
committerchai <chaifix@163.com>2018-05-19 00:09:44 +0800
commitbda9d88e84a1dccd4df978205a6ba1f141178b0c (patch)
treeb1a9d20b21583e40e8d58a303deb56fd0644e189 /src
parentccd8422bbe59143a4c62c9ce96c785f3ac75ce89 (diff)
增加垂直同步
Diffstat (limited to 'src')
-rw-r--r--src/libjin/render/graphics.h1
-rw-r--r--src/libjin/render/window.cpp28
-rw-r--r--src/libjin/render/window.h11
-rw-r--r--src/script/embed/boot.lua.h95
4 files changed, 73 insertions, 62 deletions
diff --git a/src/libjin/render/graphics.h b/src/libjin/render/graphics.h
index 242b19d..a3125c5 100644
--- a/src/libjin/render/graphics.h
+++ b/src/libjin/render/graphics.h
@@ -34,4 +34,5 @@ namespace render
extern void polygon(RENDER_MODE mode, float* p, int count);
}
}
+
#endif
diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp
index 0d53654..0179877 100644
--- a/src/libjin/render/window.cpp
+++ b/src/libjin/render/window.cpp
@@ -32,40 +32,32 @@ namespace render
{
SDL_GL_DeleteContext(ctx);
}
-
+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
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_DOUBLEBUFFER, 1);
- Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL ;
-
- int wx = SDL_WINDOWPOS_UNDEFINED,
- wy = SDL_WINDOWPOS_UNDEFINED;
+ int wx = SDL_WINDOWPOS_UNDEFINED,
+ wy = SDL_WINDOWPOS_UNDEFINED;
- /* Create window */
- wnd = SDL_CreateWindow(t, wx, wy, w, h, flags);
-
- // Create an opengl context
+ wnd = SDL_CreateWindow(t, wx, wy, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
ctx = SDL_GL_CreateContext(wnd);
+ // ֱͬ󽵵GPUռ8%->4%
+ SDL_GL_SetSwapInterval(1);
SDL_GL_MakeCurrent(wnd, ctx);
-
- // Default clear color
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-
- // Default render color
glColor4f(1, 1, 1, 1);
-
/**
* Set the viewport to top-left corner.
* Bind to the default render buffer.
*/
Canvas::unbind();
-
// Swap window buffer
swapBuffers();
}
@@ -80,5 +72,11 @@ namespace render
return ctx;
}
+ inline void Window::swapBuffers()
+ {
+ if (wnd)
+ SDL_GL_SwapWindow(wnd);
+ }
+
}
} \ No newline at end of file
diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h
index f29c82d..515ffff 100644
--- a/src/libjin/render/window.h
+++ b/src/libjin/render/window.h
@@ -20,21 +20,18 @@ namespace render
return (g_wnd ? g_wnd : (g_wnd = new Window()));
}
- inline int Window::getW()
+ inline int getW()
{
return w;
}
- inline int Window::getH()
+ inline int getH()
{
return h;
}
- inline void Window::swapBuffers()
- {
- if (wnd)
- SDL_GL_SwapWindow(wnd);
- }
+ inline void swapBuffers();
+
private:
Window();
diff --git a/src/script/embed/boot.lua.h b/src/script/embed/boot.lua.h
index bd2574a..822e1cf 100644
--- a/src/script/embed/boot.lua.h
+++ b/src/script/embed/boot.lua.h
@@ -1,8 +1,5 @@
/* boot.lua */
static const char* boot_lua = R"(
---[[
- program entry
-]]
local function _onEvent(e)
-- update keyboard status
@@ -44,24 +41,38 @@ if jin._argv[3] == '-d' then
end
function jin.core.run()
- local now = jin.time.second()
+ local load = jin.core.load
+ local running = jin.core.running
+ local second = jin.time.second
+ local sleep = jin.time.sleep
+ local poll = jin.event.poll
+ local unbind = jin.graphics.unbind
+ local clear = jin.graphics.clear
+ local color = jin.graphics.color
+ local study = jin.graphics.study
+ local onDraw = jin.core.onDraw
+ local onUpdate = jin.core.onUpdate
+ local present = jin.graphics.present
+
+ if load then
+ load()
+ end
+
+ local now = second()
local last = now
local fsec = 1/conf.fps
- -- for loading resources
- if jin.core.load then
- jin.core.load()
- end
local dt = 0
- while(jin.core.running()) do
+
+ while(running()) do
-- frame controle
last = now
- now = jin.time.second()
+ now = second()
if (now - last) < fsec then
- jin.time.sleep(fsec - now + last)
+ sleep(fsec - now + last)
end
-- handle events
- for _, e in pairs(jin.event.poll()) do
+ for _, e in pairs(poll()) do
if _onEvent then
_onEvent(e)
end
@@ -72,29 +83,28 @@ function jin.core.run()
if dt < fsec then
dt = fsec
end
- if jin.core.onUpdate then
- jin.core.onUpdate(dt)
+ if onUpdate then
+ onUpdate(dt)
end
-- bind to default render buffer
- jin.graphics.bind()
- jin.graphics.clear()
- jin.graphics.color()
- jin.graphics.study()
+ unbind()
+ clear()
+ color()
+ study()
-- custom drawing
- if jin.core.onDraw then
- jin.core.onDraw()
+ if onDraw then
+ onDraw()
end
-- render debug window
if jin.debug.status() then
jin.debug.render()
- end
+ end
-- swap window buffer
- jin.graphics.present()
-
+ present()
end
end
@@ -113,23 +123,28 @@ local function onError(msg)
end
end
-if jin.filesystem.exist("main.lua") then
- -- require main game script
- xpcall(function() require"main" end, onError)
- jin.core.run()
-else
- -- no game
- function jin.core.onEvent(e)
- if e.type == 'quit' then
- jin.core.quit()
+local function main()
+ if jin.filesystem.exist("main.lua") then
+ -- require main game script
+ xpcall(function() require"main" end, onError)
+ jin.core.run()
+ else
+ -- no game
+ function jin.core.onEvent(e)
+ if e.type == 'quit' then
+ jin.core.quit()
+ end
end
+ function jin.core.onDraw()
+ jin.graphics.clear(111, 134, 125, 255)
+ local ww, wh = jin.graphics.size()
+ local fw, fh = jin.graphics.box("no game", 20, 1, 20)
+ jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18)
+ end
+ jin.core.run()
end
- function jin.core.onDraw()
- jin.graphics.clear(111, 134, 125, 255)
- local ww, wh = jin.graphics.size()
- local fw, fh = jin.graphics.box("no game", 20, 1, 20)
- jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18)
- end
- jin.core.run()
-end
+end
+
+main()
+
)";