Last active
July 14, 2016 18:50
-
-
Save ltucker/36cf5c1a199ee979e95afcfa882a4afa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local platform = require("platform") | |
require("opengl"):import{"*"} | |
require("sprites"):import{ | |
"Image", | |
"init_program", | |
ortho_mat="glOrtho" | |
} | |
function setup() | |
fbsize = platform.screen_size() | |
device_width = fbsize.width | |
device_height = fbsize.height | |
base_width = 240 | |
base_height = 160 | |
width_ratio = device_width / base_width | |
height_ratio = device_height / base_height | |
-- find the largest integer scale that "fits" | |
-- inside the device boundary | |
scale = math.floor(math.min(width_ratio, height_ratio)) | |
-- size of the device in base pixels | |
base_screen_width = device_width / scale | |
base_screen_height = device_height / scale | |
-- offset of (0,0) on the device in base pixels | |
offset_x = math.floor((base_screen_width - base_width) / 2) | |
offset_y = math.floor((base_screen_height - base_height) / 2) | |
-- projection to apply to transform base pixels to | |
-- screen space. | |
device_projection = glOrtho( | |
-offset_x, base_screen_width-offset_x, | |
base_screen_height-offset_y, -offset_y, | |
1, -1 | |
) | |
BACKGROUND = Image:from_file("background.png") | |
TEST_IMAGE_1 = Image:from_file("test1.png") | |
TEST_IMAGE_2 = Image:from_file("test2.png") | |
end | |
function update() | |
glViewport(0, 0, device_width, device_height) | |
-- letter-box / pillar-box to restrict drawing to | |
-- the virtual screen area only (optional) | |
glDisable(GL_SCISSOR_TEST) | |
glClearColor(0,0,0,1) | |
glClear(GL_COLOR_BUFFER_BIT) | |
glEnable(GL_SCISSOR_TEST) | |
glScissor( | |
offset_x*scale, | |
offset_y*scale, | |
base_width*scale, | |
base_height*scale | |
) | |
blit(BACKGROUND.texture, | |
0, 0, BACKGROUND.width, BACKGROUND.height, | |
0, 0, 1, 1, | |
device_projection | |
) | |
blit(TEST_IMAGE_1.texture, | |
140, 90, TEST_IMAGE_1.width, TEST_IMAGE_1.height, | |
0, 0, 1, 1, | |
device_projection | |
) | |
blit(TEST_IMAGE_2.texture, | |
10, 10, TEST_IMAGE_2.width, TEST_IMAGE_2.height, | |
0, 0, 1, 1, | |
device_projection | |
) | |
end | |
--- basic blit | |
blit_v_shader_src = | |
[[ | |
uniform mat4 viewProjection; | |
attribute vec4 vPosition; | |
attribute vec4 texCoord_in; | |
varying vec2 texCoord; | |
void main() | |
{ | |
texCoord = texCoord_in.xy; | |
gl_Position = viewProjection*vPosition; | |
} | |
]] | |
blit_f_shader_src = | |
[[ | |
precision mediump float; | |
varying vec2 texCoord; | |
uniform sampler2D tex; | |
void main() | |
{ | |
gl_FragColor.rgba = texture2D(tex, texCoord); | |
} | |
]] | |
_blit_program, _blit_vshader, _blit_fshader = init_program(blit_v_shader_src, blit_f_shader_src) | |
BLIT_ARG_VPOSITION = glGetAttribLocation(_blit_program, "vPosition") | |
BLIT_ARG_TEXCOORD = glGetAttribLocation(_blit_program, "texCoord_in") | |
BLIT_ARG_TEX = glGetUniformLocation(_blit_program, "tex") | |
BLIT_ARG_VIEWPROJECTION = glGetUniformLocation(_blit_program, "viewProjection") | |
function blit(tex, x, y, w, h, tx, ty, tw, th, projection) | |
glUseProgram(_blit_program) | |
glActiveTexture(GL_TEXTURE0) | |
glBindTexture(GL_TEXTURE_2D, tex) | |
glUniformMatrix4fv(BLIT_ARG_VIEWPROJECTION, 1, GL_FALSE, projection) | |
glVertexAttribPointer( | |
BLIT_ARG_VPOSITION, 2, GL_FLOAT, GL_FALSE, 0, | |
_quad(x,y,w,h) | |
) | |
glEnableVertexAttribArray(BLIT_ARG_VPOSITION) | |
glVertexAttribPointer(BLIT_ARG_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, | |
_quad(tx,ty,tw,th) | |
) | |
glEnableVertexAttribArray(BLIT_ARG_TEXCOORD) | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) | |
end | |
function _quad(x,y,w,h) | |
local x2 = x + w | |
local y2 = y + h | |
return GLfloatv(8, {x, y2, x2, y2, x, y, x2, y}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment