diff options
-rw-r--r-- | bin/Jin.exe | bin | 535040 -> 572928 bytes | |||
-rw-r--r-- | bin/jin.exe | bin | 535040 -> 572928 bytes | |||
-rw-r--r-- | bin/main.lua | 98 | ||||
-rw-r--r-- | build/vc++/jin.vcxproj | 6 | ||||
-rw-r--r-- | build/vc++/libjin/libjin.vcxproj | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Bitmap.cpp | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Bitmap.h | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Drawable.cpp | 4 | ||||
-rw-r--r-- | src/libjin/Graphics/Font.cpp | 4 | ||||
-rw-r--r-- | src/libjin/Graphics/Shader.cpp | 21 | ||||
-rw-r--r-- | src/libjin/Graphics/Shader.h | 1 | ||||
-rw-r--r-- | src/libjin/Graphics/Shapes.cpp | 29 | ||||
-rw-r--r-- | src/libjin/Graphics/Window.cpp | 4 | ||||
-rw-r--r-- | src/lua/embed/boot.lua.h | 5 | ||||
-rw-r--r-- | src/lua/embed/keyboard.lua.h | 2 | ||||
-rw-r--r-- | src/lua/modules/event/event.cpp | 2 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 4 |
17 files changed, 105 insertions, 81 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex b8399cd..6305b49 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex b8399cd..6305b49 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/bin/main.lua b/bin/main.lua index eeec09e..96898cf 100644 --- a/bin/main.lua +++ b/bin/main.lua @@ -1,25 +1,30 @@ -io.stdout:setvbuf("no") -local shader -local img -local img2 -local canvas -local sw, sh = jin.graphics.getSize() +local boundary = { + x = 200, + y = 100, + w = 100, + h = 80, +} + +local bg = { + x = 200, + y = 100, + w = 300, + h = 200 +} +local p = { + x = boundary.x + 10, + y = boundary.y + 10, + speed = 100, +} + function jin.core.onLoad() - local str = jin.filesystem.read("metaball.shader") - shader = jin.graphics.newShader(str) - local w, h = 256, 240 - local bitmap = jin.graphics.newBitmap(w, h, {255, 255, 0, 255}) - local b = bitmap:clone() - bitmap = nil - -- local bitmap2 = jin.graphics.newBitmap("img2.bmp") - img = jin.graphics.newTexture(b) - -- img2 = jin.graphics.newTexture(bitmap2) - canvas = jin.graphics.newCanvas(200, 200) + end --- extern vec3 iResolution; --- extern number iGlobalTime; --- extern vec4 iMouse; -local mx, my = 0, 0 + +local function clamp(x, a, b) + return math.min(math.max(x, a), b) +end + function jin.core.onEvent(e) if e.type == "Quit" then jin.core.stop() @@ -27,26 +32,41 @@ function jin.core.onEvent(e) if e.type == "KeyDown" then if e.key == "Escape" then jin.core.stop() + end + if e.key == "Left" then + p.x = p.x - p.speed + elseif e.key == "Right" then + p.x = p.x + p.speed + elseif e.key == "Up" then + p.y = p.y - p.speed + elseif e.key == "Down" then + p.y = p.y + p.speed end - end - if e.type == "MouseMotion" then - -- if e.button == "left" then - mx = e.x - my = e.y - -- end - end + p.x = clamp(p.x, boundary.x, boundary.x + boundary.w) + p.y = clamp(p.y, boundary.y, boundary.y + boundary.h) + end +end + +local function updateBg() + local x0 = boundary.x + local x1 = boundary.x + boundary.w + local tx = (p.x - x0) / (x1 - x0) + bg.x = tx * (x1 - x0 - bg.w) + x0 + local y0 = boundary.y + local y1 = boundary.y + boundary.h + local ty = (p.y - y0) / (y1 - y0) + bg.y = ty * (y1 - y0 - bg.h) + y0 +end + +function jin.core.onUpdate(dt) + updateBg() end -local dt = 0 function jin.core.onDraw() - dt = dt + 0.1 - jin.graphics.bindCanvas(canvas) - jin.graphics.useShader(shader) - shader:sendNumber("iGlobalTime", dt ) - shader:sendVec3("iResolution", {sw, sh, 1}) - shader:sendVec4("iMouse", {mx, my, mx, my}) - jin.graphics.draw(img, 0, 0, 1, 1) - jin.graphics.unuseShader() - jin.graphics.unbindCanvas() - jin.graphics.draw(canvas, 0, 0, 2, 2) -end
\ No newline at end of file + jin.graphics.setColor(100, 100, 100, 255) + jin.graphics.rect("fill", bg.x, bg.y, bg.w, bg.h) + jin.graphics.setColor(255,255,255,255) + jin.graphics.rect("line", boundary.x, boundary.y, boundary.w, boundary.h) + jin.graphics.setColor(255,0,0,255) + jin.graphics.rect("fill", p.x, p.y, 3, 3) +end diff --git a/build/vc++/jin.vcxproj b/build/vc++/jin.vcxproj index 16064de..41626e1 100644 --- a/build/vc++/jin.vcxproj +++ b/build/vc++/jin.vcxproj @@ -21,7 +21,7 @@ <PropertyGroup Label="Globals"> <ProjectGuid>{A3E35ECA-62EB-45CE-8152-674FBC7F7A3B}</ProjectGuid> <RootNamespace>jin</RootNamespace> - <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion> + <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> <ProjectName>jin(min version)</ProjectName> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> @@ -82,7 +82,7 @@ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ClCompile> <WarningLevel>Level3</WarningLevel> - <Optimization>MinSpace</Optimization> + <Optimization>Disabled</Optimization> <SDLCheck>true</SDLCheck> <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <AdditionalIncludeDirectories>$(SolutionDir)..\..\src\;$(SolutionDir)libs\SDL2-2.0.5\include;$(SolutionDir)libs\LuaJIT-2.0.5\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> @@ -109,7 +109,7 @@ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ClCompile> <WarningLevel>Level3</WarningLevel> - <Optimization>MinSpace</Optimization> + <Optimization>Disabled</Optimization> <FunctionLevelLinking>true</FunctionLevelLinking> <IntrinsicFunctions>true</IntrinsicFunctions> <SDLCheck>true</SDLCheck> diff --git a/build/vc++/libjin/libjin.vcxproj b/build/vc++/libjin/libjin.vcxproj index 76d6226..dc0ed9b 100644 --- a/build/vc++/libjin/libjin.vcxproj +++ b/build/vc++/libjin/libjin.vcxproj @@ -22,7 +22,7 @@ <VCProjectVersion>15.0</VCProjectVersion> <ProjectGuid>{9EE02090-C15E-4520-9C05-C435E45EF2FC}</ProjectGuid> <RootNamespace>libjin</RootNamespace> - <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion> + <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> <ProjectName>libjin(min version)</ProjectName> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> diff --git a/src/libjin/Graphics/Bitmap.cpp b/src/libjin/Graphics/Bitmap.cpp index 28b59af..acfde58 100644 --- a/src/libjin/Graphics/Bitmap.cpp +++ b/src/libjin/Graphics/Bitmap.cpp @@ -60,7 +60,7 @@ namespace graphics stbi_image_free(pixels); } - void Bitmap::bindPixels(Color* p, int w, int h) + void Bitmap::bind(Color* p, int w, int h) { if (pixels != nullptr) delete[] pixels; diff --git a/src/libjin/Graphics/Bitmap.h b/src/libjin/Graphics/Bitmap.h index 53b374b..ab84388 100644 --- a/src/libjin/Graphics/Bitmap.h +++ b/src/libjin/Graphics/Bitmap.h @@ -20,7 +20,7 @@ namespace graphics ~Bitmap(); /* init pixels */ - void bindPixels(Color* pixels, int w, int h); + void bind(Color* pixels, int w, int h); void resetPixels(const Color* pixels, int w, int h); void resetPixels(const Color& pixels, int w, int h); /* modify pixels */ diff --git a/src/libjin/Graphics/Drawable.cpp b/src/libjin/Graphics/Drawable.cpp index 008d54e..675c54d 100644 --- a/src/libjin/Graphics/Drawable.cpp +++ b/src/libjin/Graphics/Drawable.cpp @@ -42,9 +42,13 @@ namespace graphics glPushMatrix(); glMultMatrixf((const GLfloat*)t.getElements()); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, textCoord); glVertexPointer(2, GL_FLOAT, 0, vertCoord); glDrawArrays(GL_QUADS, 0, 4); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); /* pop the model matrix */ glPopMatrix(); diff --git a/src/libjin/Graphics/Font.cpp b/src/libjin/Graphics/Font.cpp index 4988702..b95f78b 100644 --- a/src/libjin/Graphics/Font.cpp +++ b/src/libjin/Graphics/Font.cpp @@ -133,9 +133,13 @@ namespace graphics // forward to next character xc += xadvance + spacing; + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, texCoord); glVertexPointer(2, GL_FLOAT, 0, verCoord); glDrawArrays(GL_QUADS, 0, 4); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glBindTexture(GL_TEXTURE_2D, 0); diff --git a/src/libjin/Graphics/Shader.cpp b/src/libjin/Graphics/Shader.cpp index 7f849a6..12d4db0 100644 --- a/src/libjin/Graphics/Shader.cpp +++ b/src/libjin/Graphics/Shader.cpp @@ -11,9 +11,15 @@ namespace graphics using namespace jin::filesystem; + /** + * default_texture + * base_shader + * SHADER_FORMAT_SIZE + * formatShader + */ #include "base.shader.h" - /* + /** * https://stackoverflow.com/questions/27941496/use-sampler-without-passing-through-value * The default value of a sampler variable is 0. From the GLSL 3.30 spec, * section "4.3.5 Uniforms": @@ -57,8 +63,7 @@ namespace graphics JSLProgram::JSLProgram(const char* program) : currentTextureUnit(DEFAULT_TEXTURE_UNIT) { - int size = strlen(program) + SHADER_FORMAT_SIZE; - Buffer b = Buffer(size); + Buffer b = Buffer(strlen(program) + SHADER_FORMAT_SIZE); formatShader((char*)b.data, program); GLuint shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(shader, 1, (const GLchar**)&b.data, NULL); @@ -92,7 +97,9 @@ namespace graphics { glUseProgram(pid); currentJSLProgram = this; - bindDefaultTexture(); + /* bind default texture */ + int loc = glGetUniformLocation(pid, default_texture); + glUniform1i(loc, DEFAULT_TEXTURE_UNIT); } /*static*/ void JSLProgram::unuse() @@ -101,12 +108,6 @@ namespace graphics currentJSLProgram = nullptr; } - void JSLProgram::bindDefaultTexture() - { - int loc = glGetUniformLocation(pid, default_texture); - glUniform1i(loc, DEFAULT_TEXTURE_UNIT); - } - GLint JSLProgram::claimTextureUnit(const std::string& name) { std::map<std::string, GLint>::iterator unit = textureUnits.find(name); diff --git a/src/libjin/Graphics/Shader.h b/src/libjin/Graphics/Shader.h index 70e6c1d..83a2831 100644 --- a/src/libjin/Graphics/Shader.h +++ b/src/libjin/Graphics/Shader.h @@ -39,7 +39,6 @@ namespace graphics GLint claimTextureUnit(const std::string& name); JSLProgram(const char* program); - void bindDefaultTexture(); GLuint pid; GLint currentTextureUnit; diff --git a/src/libjin/Graphics/Shapes.cpp b/src/libjin/Graphics/Shapes.cpp index 13935d2..6cf0af1 100644 --- a/src/libjin/Graphics/Shapes.cpp +++ b/src/libjin/Graphics/Shapes.cpp @@ -14,14 +14,18 @@ namespace graphics void point(int x, int y) { float vers[] = { x + 0.5f , y + 0.5f }; + glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)vers); glDrawArrays(GL_POINTS, 0, 1); + glDisableClientState(GL_VERTEX_ARRAY); } void points(int n, GLshort* p) { + glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_SHORT, 0, (GLvoid*)p); glDrawArrays(GL_POINTS, 0, n); + glDisableClientState(GL_VERTEX_ARRAY); } void line(int x1, int y1, int x2, int y2) @@ -31,8 +35,10 @@ namespace graphics x2, y2 }; - glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)verts); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, (const GLvoid*)verts); glDrawArrays(GL_LINES, 0, 2); + glDisableClientState(GL_VERTEX_ARRAY); } void circle(RenderMode mode, int x, int y, int r) @@ -72,22 +78,13 @@ namespace graphics polygon(mode, coords, 3); } + /* TODO: 内存占用很多 */ void polygon_line(float* p, int count) { - float* verts = new float[count * 4]; - for (int i = 0; i < count; ++i) - { - // each line has two point n,n+1 - verts[i * 4] = p[i * 2]; - verts[i * 4 + 1] = p[i * 2 + 1]; - verts[i * 4 + 2] = p[(i + 1) % count * 2]; - verts[i * 4 + 3] = p[(i + 1) % count * 2 + 1]; - } - - glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)verts); - glDrawArrays(GL_LINES, 0, count * 2); - - delete[] verts; + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, (const GLvoid*)p); + glDrawArrays(GL_LINE_LOOP, 0, count); + glDisableClientState(GL_VERTEX_ARRAY); } void polygon(RenderMode mode, float* p, int count) @@ -98,8 +95,10 @@ namespace graphics } else if (mode == FILL) { + glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, (const GLvoid*)p); glDrawArrays(GL_POLYGON, 0, count); + glDisableClientState(GL_VERTEX_ARRAY); } } diff --git a/src/libjin/Graphics/Window.cpp b/src/libjin/Graphics/Window.cpp index 68ebe47..1fb60cc 100644 --- a/src/libjin/Graphics/Window.cpp +++ b/src/libjin/Graphics/Window.cpp @@ -73,8 +73,6 @@ namespace graphics glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); /* avoid white screen blink on windows */ swapBuffers(); /* bind to default canvas */ @@ -87,8 +85,6 @@ namespace graphics /* disable opengl */ glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); /* close window */ SDL_DestroyWindow(wnd); SDL_Quit(); diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h index f7ffc43..35bfdfc 100644 --- a/src/lua/embed/boot.lua.h +++ b/src/lua/embed/boot.lua.h @@ -45,9 +45,9 @@ function jin.core.run() local current = previous while jin.core.running() do for _, e in pairs(jin.event.poll()) do - if e.type == "keydown" then + if e.type == "KeyDown" then jin.keyboard.set(e.key, true) - elseif e.type == "keyup" then + elseif e.type == "KeyUp" then jin.keyboard.set(e.key, false) end call(jin.core.onEvent, e) @@ -78,6 +78,7 @@ jin.core.setHandler = function(handler) jin.core.onDraw = handler.onDraw end +-- TODO: Ĭͼbase64 jin.nogame = { cs = 64, sw = jin.graphics.getWidth(), diff --git a/src/lua/embed/keyboard.lua.h b/src/lua/embed/keyboard.lua.h index 77bf3a9..ee8428f 100644 --- a/src/lua/embed/keyboard.lua.h +++ b/src/lua/embed/keyboard.lua.h @@ -4,7 +4,7 @@ jin.keyboard = jin.keyboard or {} local keys = {} -function jin.keyboard.isDown(k) +function jin.keyboard.isPressed(k) return keys[k] end diff --git a/src/lua/modules/event/event.cpp b/src/lua/modules/event/event.cpp index be54cf4..71b4a33 100644 --- a/src/lua/modules/event/event.cpp +++ b/src/lua/modules/event/event.cpp @@ -36,7 +36,7 @@ namespace lua case EventType::KEY_DOWN: case EventType::KEY_UP: - luax_setfieldstring(L, "type", EventType::KEY_DOWN ? "KeyDown" : "KeyUp"); + luax_setfieldstring(L, "type", e.type == EventType::KEY_DOWN ? "KeyDown" : "KeyUp"); luax_setfieldstring(L, "key", getKeyName(e.key.keysym.sym)); break; diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index d374f51..679b820 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -260,7 +260,7 @@ namespace lua return 0; } - static int l_palette(lua_State * L) + static int l_getColor(lua_State * L) { luax_pushnumber(L, context.curRenderColor.r); luax_pushnumber(L, context.curRenderColor.g); @@ -541,7 +541,7 @@ namespace lua { "clear", l_clear }, { "draw", l_draw }, { "setColor", l_setColor }, - { "palette", l_palette }, + { "getColor", l_getColor }, { "present", l_present }, /* font */ { "box", l_box }, |