Ejemplo n.º 1
0
def main():
    glfw.init()

    glfw.window_hint(glfw.RESIZABLE, False)
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)

    w, h = 800, 640

    globs.window = glfw.create_window(w, h, 'PlatformPrototype', None, None)
    x = glfw.make_context_current(globs.window)

    # initialize libraries
    init_quadarray()
    init_textbox(w=2000, h=2000)  # dimension of glyph atlas

    globs.spf = 0.015  # 60ish fps

    # this stuff gets rendered by the render_loop
    globs.quadarrays = []
    globs.textboxes = []

    # launch loops (order matters here)
    listen.launch(game_loop())
    render_init(w, h)
    load_assets()
    edit_init()
    play_init()

    # the main loop.
    while not glfw.window_should_close(globs.window):
        glfw.wait_events_timeout(globs.spf / 10)
        listen.trigger_timers()

    glfw.terminate()
Ejemplo n.º 2
0
def main():
    glfw.init()

    glfw.window_hint(glfw.RESIZABLE, False)
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)

    w, h = 800, 800

    globs.window = glfw.create_window(w, h, 'Tesseland', None, None)
    glfw.make_context_current(globs.window)

    init_polygon_shader()

    levels_init()

    globs.spf = 0.015  # 60ish fps
    listen.launch(game_loop())

    render_init(w, h)
    play_init()
    hud_init()

    # the main loop.
    while not glfw.window_should_close(globs.window):
        glfw.wait_events_timeout(globs.spf / 10)
        listen.trigger_timers()

    glfw.terminate()
Ejemplo n.º 3
0
def main():
    glfw.init()

    # Common window hints and their defaults

    glfw.window_hint(glfw.FOCUS_ON_SHOW, True)

    # for windowed windows
    glfw.window_hint(glfw.RESIZABLE, True)
    glfw.window_hint(glfw.VISIBLE, True)
    glfw.window_hint(glfw.DECORATED, True)
    glfw.window_hint(glfw.FOCUSED, True)
    glfw.window_hint(glfw.FLOATING, False)
    glfw.window_hint(glfw.MAXIMIZED, False)

    # for full screen windows
    # To make full screen, pass a monitor as the 5th argument to glfw.create_window,
    # or use glfw.set_window_monitor(w,mon).
    # Auto_iconify means to minimize when focus lost.
    glfw.window_hint(glfw.AUTO_ICONIFY, True)
    glfw.window_hint(glfw.CENTER_CURSOR, True)

    # opengl options
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)

    glfw.window_hint(glfw.DEPTH_BITS, 24)
    glfw.window_hint(glfw.STENCIL_BITS, 8)

    # transparency
    glfw.window_hint(glfw.TRANSPARENT_FRAMEBUFFER, False)
    # later call glfw.set_window_opacity(w, 0.5)
    # and use glfw.get_window_opacity(w)

    # for X11
    glfw.window_hint_string(glfw.X11_CLASS_NAME, "")
    glfw.window_hint_string(glfw.X11_INSTANCE_NAME, "")

    #####################

    global w
    w = glfw.create_window(800, 600, 'Window Title', None, None)
    glfw.make_context_current(w)

    # glfw.set_window_title(w, "Window Title")

    # controlling window size
    # get/set interchangable
    # glfw.set_window_pos(w, x, y)
    # glfw.set_window_size(w, width, height)
    # glfw.set_window_size_limits(w, minW, minH, maxW, maxH)
    # glfw.set_window_aspect_ratio(w, 16, 9)
    # get only
    # glfw.get_window_frame_size(w)   # with borders and all, get only
    # glfw.get_framebuffer_size(w)    # get only

    # window status
    # glfw.iconify_window(w) / glfw.restore_window(w)
    # glfw.get_window_attrib(w, glfw.ICONIFIED)
    # glfw.maximize_window(w) / glfw.restore_window(w)
    # glfw.get_window_attrib(w, glfw.MAXIMIZED)
    # glfw.hide_window / glfw.show_window
    # glfw.get_window_attrib(w, glfw.VISIBLE)
    # glfw.focus_window(w) or glfw.request_window_attention(w)
    # glfw.get_window_attrib(w, glfw.FOCUSED)
    # glfw.set_window_should_close(w, False)

    # clipboard
    # glfw.get_clipboard_string(w)
    # glfw.set_clipboard_string(w, "hello")

    def clock():
        while True:
            dt = yield from listen.wait(0.2)

    listen.launch(clock())

    ####################
    shaderProgram = make_shader_program()
    glUseProgram(shaderProgram)

    vao = glGenVertexArrays(1)
    glBindVertexArray(vao)

    tri_vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, tri_vbo)
    v1, v2, v3 = Vec(0, 0, 0), Vec(1, 1, 0), Vec(1, 0, 0)
    vertices = Vec(v1, v2, v3)
    glBufferData(GL_ARRAY_BUFFER, vertices.size, vertices.buffer,
                 GL_STATIC_DRAW)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    ###################

    def fbresize():
        x = None
        while True:
            if x is None: x, y = glfw.get_framebuffer_size(w)
            else: _, x, y = yield from listen.on_framebuffer_size(w)

            # adjust viewport
            glViewport(0, 0, x, y)

            # adjust projection
            projection.set_uniform(shaderProgram, "projection")
            projection.clear()
            projection.ortho(0, x, 0, y, 0, 100)

    listen.launch(fbresize())

    import time, math
    t0 = time.time()

    while not glfw.window_should_close(w):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        projection.set_uniform(shaderProgram, "modelview")

        projection.push()
        theta = 10 * (time.time() - t0)
        projection.translate(100, 100, 0.0)
        projection.translate(10 * math.sin(theta), 10 * math.cos(theta), 0.0)

        projection.scale(100, 100, 1)
        glDrawArrays(GL_TRIANGLES, 0, 3)
        projection.pop()

        glfw.swap_buffers(w)

        # glfw.poll_events()  # non-blocking
        # glfw.wait_events()  # blocks until an event is received
        # glfw.wait_events_timeout(0.4) # blocks until event or timeout
        # To unblock the main thread from wait_events(), use glfw.post_empty_event()

        glfw.wait_events_timeout(0.01)  # 100 fps
        listen.trigger_timers()

    glfw.terminate()
Ejemplo n.º 4
0
def main():
    glfw.init()

    # for windowed windows
    glfw.window_hint(glfw.RESIZABLE, True)

    # opengl options
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)

    global w
    w = glfw.create_window(800, 600, 'Window Title', None, None)
    glfw.make_context_current(w)

    ####################
    shaderProgram = make_shader_program()
    glUseProgram(shaderProgram)

    vao = glGenVertexArrays(1)
    glBindVertexArray(vao)

    tri_vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, tri_vbo)
    vertices = Vec(0, 0), Vec(0, 1), Vec(1, 1), Vec(1, 0)
    vertices = Vec(*vertices)
    glBufferData(GL_ARRAY_BUFFER, vertices.size, vertices.buffer,
                 GL_STATIC_DRAW)
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)

    colorUniform = glGetUniformLocation(shaderProgram, "color")
    glUniform4f(colorUniform, 1, 1, 1, 1)
    ###################

    node = PaddingNode(
        vertical="*",
        children=[
            RowsNode(children=[
                RowsNode(children=[
                    HintedNode(width=100, height=300, key="node1"),
                    HintedNode(width=200, height=100, key="node2"),
                    HintedNode(width=150, height=200, key="node3"),
                ])
            ])
        ])

    def fbresize():
        x = None
        while True:
            if x is None: x, y = glfw.get_framebuffer_size(w)
            else: _, x, y = yield from listen.on_framebuffer_size(w)

            # adjust viewport
            glViewport(0, 0, x, y)

            # adjust projection
            projection.set_uniform(shaderProgram, 'projection')
            projection.clear()
            projection.ortho(0, x, 0, y, 0, 100)

            # adjust node
            node_window_fit(node, w)

    listen.launch(fbresize())

    def rectnode(node, color):
        while True:
            yield from node.on_draw()
            colorUniform = glGetUniformLocation(shaderProgram, "color")
            glUniform4f(colorUniform, color.x, color.y, color.z, color.w)

            projection.set_uniform(shaderProgram, 'modelview')

            projection.push()
            projection.translate(node.x, node.y, 0.0)
            projection.scale(node.width, node.height, 1)
            glDrawArrays(GL_TRIANGLE_FAN, 0, 4)
            projection.pop()

    listen.launch(rectnode(node["node1"], Vec(1, 0, 0, 1)))
    listen.launch(rectnode(node["node2"], Vec(0, 1, 0, 1)))
    listen.launch(rectnode(node["node3"], Vec(0, 0, 1, 1)))
    listen.launch(rectnode(node, Vec(0.2, 0.4, 0.3, 1)))

    while not glfw.window_should_close(w):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        node.draw()

        glfw.swap_buffers(w)

        glfw.wait_events_timeout(0.01)  # 100 fps
        listen.trigger_timers()

    glfw.terminate()
Ejemplo n.º 5
0
def main():
    glfw.init()

    glfw.window_hint(glfw.RESIZABLE, False)
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)

    window = glfw.create_window(800, 600, 'Window Title', None, None)
    glfw.make_context_current(window)

    # A standard rectangle
    rect_vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, rect_vbo)
    vertices = Vec(1, 1), Vec(1, 0), Vec(0, 0), Vec(0, 1)
    vertices = Vec(*vertices)
    glBufferData(GL_ARRAY_BUFFER, vertices.size, vertices.buffer,
                 GL_STATIC_DRAW)
    glBindBuffer(GL_ARRAY_BUFFER, 0)

    frames, sheet = load_spritesheet("terrain.json")
    for frame in frames:
        # make a vao for this frame
        frame["vao"] = glGenVertexArrays(1)
        glBindVertexArray(frame["vao"])

        # bind the rect vbo to this vao as attribute 0
        glBindBuffer(GL_ARRAY_BUFFER, rect_vbo)
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
        glEnableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)

        # make a vbo for the texcoords
        tex_coord_vbo = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, tex_coord_vbo)

        # coords = Vec(1.0,1.0),Vec(1.0,0.0),Vec(0.0,0.0),Vec(0.0,1.0)
        coords = [frame["texcoords"][k] for k in ["tr", "br", "bl", "tl"]]
        coords = Vec(*coords)
        glBufferData(GL_ARRAY_BUFFER, coords.size, coords.buffer,
                     GL_STATIC_DRAW)

        # ... and bind it as attribute 1
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
        glEnableVertexAttribArray(1)
        glBindBuffer(GL_ARRAY_BUFFER, 0)

        glBindVertexArray(0)  # unbind vao

    shaderProgram = make_shader_program()
    glUseProgram(shaderProgram)

    glViewport(0, 0, 800, 600)
    projection.set_uniform(shaderProgram, 'projection')
    projection.ortho(0, 800, 0, 600, 0, 1)

    anims = {}
    for key in sheet:
        anim = Animator()
        anim.enqueue(sheet[key], loop=True)
        anims[key] = anim

    projection.scale(3, 3, 0)

    while not glfw.window_should_close(window):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        x = 0
        for key in anims.keys():
            a = anims[key]

            glBindVertexArray(a["vao"])
            glBindTexture(GL_TEXTURE_2D, a["texture"])

            projection.push()
            projection.translate(x, 0, 0)

            projection.scale(a["w"], a["h"], 0)

            glDrawArrays(GL_TRIANGLE_FAN, 0, 4)

            projection.pop()
            x += a["w"]

        glfw.swap_buffers(window)
        glfw.wait_events_timeout(0.01)  # 100 fps
        listen.trigger_timers()

    glfw.terminate()
Ejemplo n.º 6
0
def main():
    glfw.init()

    glfw.window_hint(glfw.RESIZABLE, False)
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)

    w, h = 800, 640
    globs.h = h
    globs.w = w

    globs.window = glfw.create_window(w, h, 'Polygon', None, None)
    x = glfw.make_context_current(globs.window)

    init_polygon_shader()

    # flood-fill colors
    globs.colors = [
        Vec(1.0, 0.0, 1.0, 1.0),
        Vec(1.0, 1.0, 1.0, 1.0),
        Vec(0.5, 0.5, 1.0, 1.0),
        Vec(0.0, 1.0, 1.0, 1.0)
    ]
    globs.colorIndex = 0
    # data to scale and shape the triangle array
    globs.tri = {}
    globs.tri["i_max"] = 7  # number of columns
    globs.tri["j_max"] = 5  # number of rows
    globs.tri[
        "offsetX"] = 50  # x, y (pixel) position of lower left corner of the array
    globs.tri["offsetY"] = 50
    globs.tri["scaleX"] = 50  # 1/2 the width of a triangle
    globs.tri["scaleY"] = 50  # 1/2 the height of a triangle
    # score
    globs.numberClicksThisLevel = 0

    # this stuff gets rendered by the render_loop
    globs.polygons = []
    #generate arrays
    for i in range(0, globs.tri["i_max"]):
        for j in range(0, globs.tri["j_max"]):
            isStaggered = (j % 2 == 1)
            if not isStaggered:
                p1, p2, p3 = grid_index_to_position(i, j, True)  # upward point
                globs.polygons.append(
                    Polygon(globs.colors[0 if isStaggered else 2],
                            [p1, p2, p3]))
                p1, p2, p3 = grid_index_to_position(i, j,
                                                    False)  # downward point
                globs.polygons.append(
                    Polygon(globs.colors[1 if isStaggered else 3],
                            [p1, p2, p3]))
            else:
                p1, p2, p3 = grid_index_to_position(i, j,
                                                    False)  # downward point
                globs.polygons.append(
                    Polygon(globs.colors[1 if isStaggered else 3],
                            [p1, p2, p3]))
                p1, p2, p3 = grid_index_to_position(i, j, True)  # upward point
                globs.polygons.append(
                    Polygon(globs.colors[0 if isStaggered else 2],
                            [p1, p2, p3]))

    # launch loops (order matters here)
    globs.spf = 0.015  # 60ish fps
    listen.launch(game_loop())

    render_init(w, h)
    edit_init()

    # the main loop.
    while not glfw.window_should_close(globs.window):
        glfw.wait_events_timeout(globs.spf / 10)
        listen.trigger_timers()

    glfw.terminate()