def __init__(self, ll, font, vw, vh, priv, spacing=1, rel=None): self._ll = ll self._font = font self._space = array.array('I', ' '.encode(self._font.codec))[0] self._tw = self._font.ts.width() self._th = self._font.ts.height() self._vw = int(vw) self._vh = int(vh) self._priv = priv self._spacing = int(spacing) self._rel = rel self._entries = [] self._selection = 0 self._tb = None self._valtbs = [] self._cursortm = None self._cursorl = None self._updated = False self._longestlabel = 0 self._curvalue_u = None self._curvalue_t = None self._curpos = 0 self._dl = display.DisplayList(self._ll, None) self._tbindex = self._dl.append(None) self._cursorindex = self._dl.append(None) self._valueindex = self._dl.append(None) self._dlvalues = 1
def _build_screen(self): self._layers = list() for desc in self._ldescs: if desc.tilemap < 0: l = cg.Layer(self._state.ll, None, "Preview Layer {}".format(desc.name)) l.scale(desc.scalex, desc.scaley) if desc.mode == ScrollMode.LAYER: l.pos(-desc.posx, -desc.posy) else: l.pos(desc.posx, desc.posy) self._layers.append(l) else: vpw = desc.vw if vpw * desc.scalex > self._vw: vpw = self._vw / desc.scalex + 1 vph = desc.vh if desc.vh * desc.scaley > self._vh: vph = self._vh / desc.scaley + 1 stm = display.ScrollingTilemap( self._state.ll, self._get_tileset(desc.tilemap), self._maps[desc.tilemap][0], vpw, vph, self._tmdescs[desc.tilemap].mw, self._tmdescs[desc.tilemap].mh, flags=self._maps[desc.tilemap][1], colormod=self._maps[desc.tilemap][2], optimize=True) stm.scale(desc.scalex, desc.scaley) stm.layer.pos(desc.posx, desc.posy) stm.scroll(desc.scrollx, desc.scrolly) stm.internal.colormod(desc.colormod) stm.internal.blendmode(desc.blendmode) self._layers.append(stm) for num, desc in enumerate(self._ldescs): if desc.relative >= 0: layer = self._layers[num] if desc.tilemap >= 0: layer = layer.layer rlayer = self._layers[desc.relative] if self._ldescs[desc.relative].tilemap >= 0: if self._ldescs[desc.relative].mode == ScrollMode.LAYER: rlayer = rlayer.layer else: rlayer = rlayer.maplayer layer.relative(rlayer) self._set_all() self._dl = display.DisplayList(self._state.ll) for layer in self._layers: self._dl.append(layer.draw)
def __init__(self, state, name): self._state = state settings, self._descs, self._maps, self._layers, _, _ = layers.load_map( name) self._params = None # get the params tilemap out for num, desc in enumerate(self._descs): if desc.name == 'params': self._params = (self._descs[num], self._maps[num]) del self._descs[num] del self._maps[num] # take out layers which display this tilemap # also repoint layers offset by this removed tilemap for num2, layer in enumerate(self._layers): if layer.tilemap == num: del self._layers[num2] # remove references to this removed layer # also repoint indexes offset by this removed layer for layer in self._layers: if layer.relative == num2: layer.relative = -1 elif layer.relative > num2: layer.relative -= 1 elif layer.tilemap > num: layer.tilemap -= 1 break if self._params is None: raise ValueError("No 'params' map found.") self._tw = self._params[0].tw self._th = self._params[0].th for layer in self._layers: layer.scalex = 1.0 layer.scaley = 1.0 playerdesc = self._state.add_tileset(PLAYER_GFX, PLAYER_WIDTH, PLAYER_HEIGHT) playerts = self._state.tileset_desc(playerdesc) playertm = playerts.tilemap(1, 1, "Player Tilemap") playertm.map(0, 0, 1, 1, 1, array.array('I', (0, ))) playertm.update() self._playerl = playertm.layer("Player Layer") self._view = None self._pdirx = 0.0 self._pdiry = 1.0 self._pstate = PlayerState.AIR self._pspeedx = 0.0 self._pspeedy = 0.0 self._playerx = settings['player_x'] self._playery = settings['player_y'] self._move_player() self._dl = display.DisplayList(self._state.ll) self._viewindex = self._dl.append(None) self._dl.append(self._playerl) self._tbindex = self._dl.append(None) self._build_screen()
def __init__(self, renderer, pixfmt, vw, vh, font_filename, font_mapname, font_width, font_height, font_scale): self._renderer = renderer self._vw = int(vw) self._vh = int(vh) self._set_size() sdlfmt = SDL_AllocFormat(pixfmt) self._ll = cg.LayerList(renderer, pixfmt, log_cb_return, None) self._dl = display.DisplayList(self._ll) self._dl.append(display.make_color(0, 0, 0, SDL_ALPHA_OPAQUE)) self._screen = None self._screenindex = self._dl.append(None) self._tilesets = {} self._tilesetrefs = {} self._running = True self._newscreen = False self._resizing = 0.0 self._extratime = 0.0 self._renders = FULL_RENDERS self._font_scale = font_scale ts = self._ll.tileset(font_filename, font_width, font_height, "font") codec = textbox.load_tileset_codec(font_mapname, ts.tiles()) self._font = textbox.Font(ts, codec) self._lastTime = time.monotonic()
def do_main(window, renderer, pixfmt): try: seqname = argv[1] except IndexError: seqname = DEFAULT_SEQ sdlfmt = SDL_AllocFormat(pixfmt) sdl_red = SDL_MapRGBA(sdlfmt, 255, 0, 0, SDL_ALPHA_OPAQUE) sdl_green = SDL_MapRGBA(sdlfmt, 0, 255, 0, SDL_ALPHA_OPAQUE) sdl_blue = SDL_MapRGBA(sdlfmt, 0, 0, 255, SDL_ALPHA_OPAQUE) red = display.make_color(255, 31, 31, 255) green = display.make_color(31, 255, 31, 255) blue = display.make_color(127, 127, 255, 255) transparent_rg = SDL_MapRGBA(sdlfmt, 255, 255, 63, 191) ll = cg.LayerList(renderer, pixfmt, log_cb_return, None) if ll.renderer != renderer: print("Got different renderer back from layerlist") ts1 = cg.Tileset(ll, 32, 32, sdl_blue, 16, 16, "Blue") ts2 = cg.Tileset(ll, "gfx/font.bmp", 8, 8, None) surface = SDL_CreateRGBSurfaceWithFormat(0, 64, 64, 32, pixfmt) ts3 = cg.Tileset(ll, surface, 64, 64, "Square") SDL_FreeSurface(surface) try: ll.target_tileset(window) print("set_target_tileset didn't raise TypeError as expected") except TypeError: pass try: ll.target_tileset(0) print("set_target_tileset didn't raise TypeError as expected") except TypeError: pass ll.target_tileset(ts1); ll.target_tileset(ts2); ll.target_tileset(ts3); ll.target_tileset(None); try: ll.default_render_target(window) print("set_default_render_target didn't raise TypeError as expected") except TypeError: pass tm1 = cg.Tilemap(ll, ts1, 4, 4, "Test 2") tm1.tileset(ts2, "Test 2") SDL_RenderPresent(renderer) del tm1 del ts3 del ts2 del ts1 random.seed(None) pt1 = effects.BouncingPoint(0.0, 0.0, RES_WIDTH - (64 * 4), RES_HEIGHT - (64 * 4), 480.0) pt2 = effects.BouncingPoint(-20.0, -8.0, 64 - 20, 64 - 8, 64.0, x = -20.0, y = -8.0) blendmode = cg.TILEMAP_BLENDMODE_ADD colorrad = 0.0 colorrad2 = 0.25 text = ll.tileset("gfx/font.bmp", 8, 8, None) tm1 = ll.tilemap(text, 8, 8, "DVD Video") tm1.map(2, 2, 5, 5, 2, array.array('u', "oediV DVD ")) tm1.attr_flags(2, 2, 0, 5, 2, array.array('I', (cg.TILEMAP_ROTATE_180, cg.TILEMAP_ROTATE_180, cg.TILEMAP_ROTATE_180, cg.TILEMAP_ROTATE_180, cg.TILEMAP_ROTATE_180))) tm1.attr_colormod(2, 2, 4, 5, 2, array.array('I', (red, green, blue, red, green, blue, red, green, blue, red))) tm1.update(0, 0, 8, 8) l1 = cg.Layer(ll, tm1, "DVD Video"); l1.window(40, 16) l1.scroll_pos(16, 16) l1.rotation_center(20, 8) l1.rotation(180) l1.colormod(transparent_rg) l1.blendmode(blendmode) ts2 = ll.tileset(64, 64, sdl_blue, 64, 64, "Blue Box") tm2 = ts2.tilemap(1, 1, "Blue Box") tm2.update(0, 0, 1, 1) l2 = tm2.layer("Blue Box") l2.scale(4.0, 4.0) osc1 = SDL_CreateTexture(renderer, pixfmt, SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET, 320, 240) display.clear(ll, osc1, 0, 0, 0, SDL_ALPHA_OPAQUE) osc1l = cg.Layer(ll, osc1, "OSC 1 Layer") osc1l.pos(-20, -20) osc1l.scale((320 + (20 * 2)) / 320, (240 + (20 * 2)) / 240) osc1l.rotation(5) osc1l.rotation_center(160, 120) osc2 = SDL_CreateTexture(renderer, pixfmt, SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET, 320, 240) display.clear(ll, osc2, 0, 0, 0, SDL_ALPHA_OPAQUE) osc2l = cg.Layer(ll, osc2, "OSC 2 Layer") bigtm = numpy.zeros(511 * 514, numpy.uint32) bigtm[0:127] = numpy.arange(1, 128) for num in range(127, 512 * 512, 127): bigtm[num:num+127] = bigtm[0:127] bigcm = numpy.zeros(512 * 512, numpy.uint32) bigcm.fill(display.make_color(255, 255, 255, SDL_ALPHA_OPAQUE)) for num in range(1, 511): r, g, b = effects.color_from_rad(numpy.pi * (num / 512) * 2.0, 0, 255) bigcm[num*512+1:num*512+511].fill(display.make_color(r, g, b, SDL_ALPHA_OPAQUE)) #a = num % 2 * 255 #bigcm[num*128+1:num*128+127].fill(display.make_color(a, a, a, SDL_ALPHA_OPAQUE)) stm = display.ScrollingTilemap(ll, text, bigtm, RES_WIDTH, RES_HEIGHT, 512, 512, colormod=bigcm) #stm = display.ScrollingTilemap(ll, text, bigtm, RES_WIDTH, RES_HEIGHT, 32, 32, colormod=bigcm) #stm.layer.scale(2.0, 2.0) pt3 = effects.BouncingPoint(-RES_WIDTH - 10, -RES_HEIGHT - 10, (512 * 8) + 10, (512 * 8) + 10, 1000, minspeed=60) #pt3 = effects.BouncingPoint(-RES_WIDTH - 10, -RES_HEIGHT - 10, (32 * 8) + 10, (32 * 8) + 10, 1000, minspeed=60) scene = display.DisplayList(ll, display.SCREEN) osc1dl = display.DisplayList(ll, osc2) osc1dl.append(osc1l) osc2dl = display.DisplayList(ll, osc1) osc2dl.append(osc2l) scopelid = osc2dl.append(None) scoperid = osc2dl.append(None) scene.append(display.make_color(32, 16, 48, SDL_ALPHA_OPAQUE)) scene.append(lambda: stm.draw()) oscdl = display.DisplayList(ll, None) oscdl.append(lambda: osc2l.scale(1.0, 1.0)) oscdl.append(osc1dl) oscdl.append(osc2dl) oscdl.append(lambda: osc2l.scale(RES_WIDTH/320, RES_HEIGHT/240)) oscdl.append(osc2l) oscid = scene.append(None) l1dl = display.DisplayList(ll, ts2) l1dl.append(l1) l1dl.append(lambda: tm2.update(0, 0, 1, 1)) scene.append(l1dl) scene.append(l2) aud = audio.AudioSystem(log_cb_return, None, 48000, 2, trace=TRACEAUDIO) audbuffers = load_audio(aud, WAVEFORM_HARMONICS) aud.enabled(True) wavout = False seq = None scopell = None scoperl = None scopel = None scoper = None running = True playing = True optimize = False lastTime = time.monotonic() while running: thisTime = time.monotonic() timetaken = thisTime - lastTime try: aud.frame() except Exception as e: print("Sequence raised error") aud.print_full_stats() if seq != None: for s in seq: aud.del_sequence(s) scene.replace(oscid, None) osc2dl.replace(scopelid, None) osc2dl.replace(scoperid, None) scopell = None scoperl = None scopel = None scoper = None seq = None _, exc = aud.error print_tb(exc.__traceback__) print(exc) aud.enabled(True) if scopel != None: # possible race condition between this and aud.frame() but it'll # just result in missed samples, no big deal scopel.update() scoper.update() if seq != None: for s in seq: if s.ended: aud.del_sequence(s) seq.remove(s) print("Sequence ended") if len(seq) == 0: scene.replace(oscid, None) osc2dl.replace(scopelid, None) osc2dl.replace(scoperid, None) scopell = None scoperl = None scopel = None scoper = None seq = None event = SDL_Event() while SDL_PollEvent(event): if event.type == SDL_QUIT: running = False break elif event.type == SDL_KEYDOWN: if event.key.keysym.sym == SDLK_q: running = False elif event.key.keysym.sym == SDLK_p: if seq != None: for s in seq: aud.del_sequence(s) scopell = None scoperl = None scopel = None scoper = None seq = None macros = {"FILTER_SIZE": ((), str(waves.FILTER_TAPS)), "FILTER_SLICES": ((), str(waves.SLICES))} # load first sequence seq = [] try: with open(seqname, "r") as seqfile: seq.append(audio.AudioSequencer(seqfile, audbuffers, macros, trace=TRACEAUDIO)) except Exception as e: aud.print_full_stats() print_tb(e.__traceback__) print(e) seq = None # if it didn't fail, try to see if there are more parts if seq != None: parts = () try: parts = seq[0].get_tag('part-files').split(';') except KeyError: pass if len(parts) > 0: # try loading all the parts for part in parts: try: with open(part, "r") as seqfile: seq.append(audio.AudioSequencer(seqfile, [envslope, benddownslope, bendupslope, noise, filt], macros, trace=TRACEAUDIO)) except Exception as e: aud.print_full_stats() print_tb(e.__traceback__) print(e) seq = None break # if loading all the parts succeeds, try adding them # all to the audio context if seq != None: for s in seq: try: aud.add_sequence(s, enabled=True) except Exception as e: print("Adding sequences failed, ignore warnings about attempt to remove.") for s in seq: aud.del_sequence(s) aud.print_full_stats() print_tb(e.__traceback__) print(e) seq = None break if seq != None: scopel = Scope(renderer, seq[0], 0, pixfmt, 320, 120) scopell = cg.Layer(ll, scopel.texture, "Scope L Layer") scopell.pos(0, 30) osc2dl.replace(scopelid, scopell) scoper = Scope(renderer, seq[0], 1, pixfmt, 320, 120) scoperl = cg.Layer(ll, scoper.texture, "Scope R Layer") scoperl.pos(0, 90) osc2dl.replace(scoperid, scoperl) scene.replace(oscid, oscdl) pass elif event.key.keysym.sym == SDLK_s: if seq != None: for s in seq: aud.del_sequence(s) scene.replace(oscid, None) osc2dl.replace(scopelid, None) osc2dl.replace(scoperid, None) scopell = None scoperl = None scopel = None scoper = None seq = None elif event.key.keysym.sym == SDLK_r: aud.enabled(True) elif event.key.keysym.sym == SDLK_n: aud.enabled(False) elif event.key.keysym.sym == SDLK_i: aud.print_full_stats() elif event.key.keysym.sym == SDLK_w: if wavout: aud.close_wav() wavout = False else: aud.open_wav("output.wav") wavout = True elif event.key.keysym.sym == SDLK_o: optimize = not optimize stm.optimize(optimize) print("Optimization enabled: {}".format(optimize)) pt1.update(timetaken) if pt2.update(timetaken): if blendmode == cg.TILEMAP_BLENDMODE_ADD: blendmode = cg.TILEMAP_BLENDMODE_SUB else: blendmode = cg.TILEMAP_BLENDMODE_ADD l1.blendmode(blendmode) pt3.update(timetaken) x, y = pt1.point l2.pos(int(x), int(y)) x, y = pt2.point l1.pos(int(x), int(y)) x, y = pt3.point stm.scroll(x, y) stm.update() colorrad = colorrad + (numpy.pi * timetaken) colorrad %= numpy.pi * 2 colorrad2 = colorrad2 + (numpy.pi * timetaken) colorrad2 %= numpy.pi * 2 modr, modg, modb = effects.color_from_rad(colorrad, 0, 255) l1.colormod(display.make_color(modr, modg, modb, 255)) if scopell != None: modr, modg, modb = effects.color_from_rad(colorrad, 0, 250) scopell.colormod(display.make_color(modr, modg, modb, SDL_ALPHA_OPAQUE)) modr, modg, modb = effects.color_from_rad(colorrad2, 0, 250) scoperl.colormod(display.make_color(modr, modg, modb, SDL_ALPHA_OPAQUE)) scene.draw(display.SCREEN, trace=TRACEVIDEO) SDL_RenderPresent(renderer) lastTime = thisTime aud.enabled(False) if seq != None: for s in seq: aud.del_sequence(s) scene.replace(oscid, None) osc2dl.replace(scopelid, None) osc2dl.replace(scoperid, None) scopell = None scoperl = None scopel= None scoper= None seq = None