def game_loop(): # put the fps meter into this textbox globs.fpsbox = TextBox("IBMPlexSans-Regular.ttf", size=30, color=Vec(0, 0, 0), pos=Vec(5, 5, 0.9)) globs.textboxes.append(globs.fpsbox) dt_count, dt_sum = 0, 0 # tally up the dt's dt = 1 # first value of dt while True: # render, and compute how much time it took t0 = glfw.get_time() listen.dispatch("on_frame", dt) listen.dispatch("on_render") dt_render = glfw.get_time() - t0 # wait a bit to match the fps rate if needed _, dt_wait = yield from listen.wait(max(globs.spf - dt_render, 0)) dt = dt_wait + dt_render # put the average fps into the textbox dt_count += 1 dt_sum += dt if dt_sum >= 0.5: # every half second globs.fpsbox.text = str(int(dt_count / dt_sum * 10) / 10) + "fps" dt_count, dt_sum = 0, 0
def load_assets(): spritesheets = [] spritesheets.append("assets/starseed.json") spritesheets.append("assets/dino.json") # all the assets get put in here globs.assets = {} for fname in spritesheets: # frames is a list of dictionaries with {"texture", "duration", "w,", "h", "texcoords"} # sheet is a dictionary of lists, lists containing subsets fo the frames frames, sheet = load_spritesheet(fname) # post-process the sheet a little bit, populate globs.assets for key in sheet: if key in globs.assets: print("Duplicate asset", key, "in", fname) continue # if the key ends in "-right", populate this list with some "-left" frames flipped_frames = [] for frame in sheet[key]: # add the "size" key to the frame. Quadarray uses this to decide how big the texture is. frame["size"] = Vec( sheet[key][0]["w"] / globs.cam["pixels_per_unit"], sheet[key][0]["h"] / globs.cam["pixels_per_unit"]) # if key ends in "-right", copy the frame with the texcoords flipped l<->r if key[-6:] == "-right": flipped_frames.append({ "texture": frame["texture"], "duration": frame["duration"], "w": frame["w"], "h": frame["h"], "texcoords": { "tl": frame["texcoords"]["tr"], "tr": frame["texcoords"]["tl"], "bl": frame["texcoords"]["br"], "br": frame["texcoords"]["bl"], }, "size": frame["size"] }) # populate globs.assets globs.assets[key] = sheet[key] if key[-6:] == "-right": globs.assets[key[:-6] + "-left"] = flipped_frames globs.assets["circle"] = load_png("assets/circle.png") globs.assets["circle"]["size"] = Vec( globs.assets["circle"]["w"], globs.assets["circle"]["h"]) / globs.cam["pixels_per_unit"]
def update(self): if len(self.points) == 0: # clear the VBO glBindBuffer(GL_ARRAY_BUFFER, self.vbo) glBufferData(GL_ARRAY_BUFFER, 0, None, GL_STATIC_DRAW) glBindBuffer(GL_ARRAY_BUFFER, 0) return glBindVertexArray(self.vao) pts = Vec(*self.points) glBindBuffer(GL_ARRAY_BUFFER, self.vbo) glBufferData(GL_ARRAY_BUFFER, pts.size, pts.buffer, GL_STATIC_DRAW) glBindBuffer(GL_ARRAY_BUFFER, 0) glBindVertexArray(0)
def load_assets(): from patpygl.quadarray import QuadArray spritesheets = [] spritesheets.append("assets/grassland.json") spritesheets.append("assets/dino.json") # all the assets get put in here globs.assets = {} for fname in spritesheets: # frames is a list of dictionaries with {"texture", "duration", "w,", "h", "texcoords"} # sheet is a dictionary of lists, lists containing subsets fo the frames frames, sheet = load_spritesheet(fname) # post-process the sheet a little bit, populate globs.assets for key in sheet: if key in globs.assets: print("Duplicate asset", key, "in", fname) continue # if the key ends in "-right", populate this list with some "-left" frames flipped_frames = [] for frame in sheet[key]: # add the "size" key to the frame. Quadarray uses this to decide how big the texture is. frame["size"] = Vec( sheet[key][0]["w"] / globs.cam["pixels_per_unit"], sheet[key][0]["h"] / globs.cam["pixels_per_unit"]) # if key ends in "-right", copy the frame with the texcoords flipped l<->r if key[-6:] == "-right": flipped_frames.append({ "texture": frame["texture"], "duration": frame["duration"], "w": frame["w"], "h": frame["h"], "texcoords": { "tl": frame["texcoords"]["tr"], "tr": frame["texcoords"]["tl"], "bl": frame["texcoords"]["br"], "br": frame["texcoords"]["bl"], }, "size": frame["size"] }) # populate globs.assets globs.assets[key] = sheet[key] if key[-6:] == "-right": globs.assets[key[:-6] + "-left"] = flipped_frames # load HUD elements, note window is 800x640 for element in globs.hud_elements: hud_asset = load_png(element["sprite"]) hud_asset["size"] = Vec( element["size"]["x"], element["size"]["y"]) / globs.cam[ "pixels_per_unit"] # 16 pixels per unit in the camera hud_array = QuadArray(hud_asset) hud_array.quads.append( Vec(element["location"]["x"], element["location"]["y"], 0.5)) # +z draws on top of -z hud_array.update() globs.hud_quadarrays.append(hud_array)