def load(self, renderer): """Load the graphical data into textures compatible with renderer.""" # mainly from pytmx.tmxloader tmxdata = self.tmx # convert angles to float: for ob in self.tmx.objects: ob.rotation = float(ob.rotation) for ts in tmxdata.tilesets: path = os.path.join(os.path.dirname(tmxdata.filename), ts.source) colorkey = getattr(ts, 'trans', None) if colorkey: # Convert HTML-format hex color to (r, g, b): colorkey = tuple( int(colorkey[x:x + 2], 16) for x in range(0, 6, 2)) surface = sdl.image.load(path) if not surface: raise Exception(sdl.getError()) try: if colorkey and surface.format.BitsPerPixel == 8: i = sdl.mapRGB(surface.format, *colorkey) assert sdl.setColorKey(surface, 1, i) == 0 ts.image = sdl.createTextureFromSurface(renderer, surface) finally: sdl.freeSurface(surface) rc, format, access, w, h = sdl.queryTexture(ts.image) # margins and spacing tilewidth = ts.tilewidth + ts.spacing tileheight = ts.tileheight + ts.spacing tile_size = ts.tilewidth, ts.tileheight # some tileset images may be slightly larger than the tile area # ie: may include a banner, copyright, etc. # this compensates for that width = int((( (w - ts.margin * 2 + ts.spacing) / tilewidth) * tilewidth) - ts.spacing) height = int((( (h - ts.margin * 2 + ts.spacing) / tileheight) * tileheight) - ts.spacing) # trim off any pixels on the right side that isn't a tile # this happens if extra graphics are included on the left, # but they are not actually part of the tileset width -= (w - ts.margin) % tilewidth # using product avoids the overhead of nested loops p = itertools.product( xrange(ts.margin, height + ts.margin, tileheight), xrange(ts.margin, width + ts.margin, tilewidth))
def textureSize(t): _, _, _, width, height = sdl.queryTexture(t) return width, height
def get_size(self): out = sdl.queryTexture(self.handle) assert out[0] == 0, "invalid texture" return out[3], out[4]
def main(): event = sdl.Event() sdl.init(sdl.INIT_VIDEO) # Initialize test framework # state = SDLTest_CommonCreateState(argv, sdl.INIT_VIDEO) # if not state: # return 1 # for (i = 1; i < argc;) { # # consumed = SDLTest_CommonArg(state, i) # if consumed == 0: # sdl.Log("Usage: %s %s\n" % (argv[0], SDLTest_CommonUsage(state))) # return 1 # i += consumed # if not SDLTest_CommonInit(state): # quit(2) drawstates = [DrawState()] for i in range(len(drawstates)): drawstate = drawstates[i] drawstate.window = sdl.createWindow("Scale %d" % i, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, sdl.WINDOW_SHOWN) drawstate.renderer = sdl.createRenderer(drawstate.window, -1, 0) drawstate.sprite = LoadTexture(drawstate.renderer, "icon.bmp", True) drawstate.background = LoadTexture(drawstate.renderer, "sample.bmp", False) if not drawstate.sprite or not drawstate.background: quit(2) rc, format, access, w, h = sdl.queryTexture(drawstate.sprite) drawstate.sprite_rect.w = w drawstate.sprite_rect.h = h drawstate.scale_direction = 1 # Main render loop frames = 0 then = sdl.getTicks() done = 0 while not done: # Check for events frames += 1 while sdl.pollEvent(event): if event.type == sdl.QUIT: done = 1 for i in range(len(drawstates)): if not drawstates[i].window: continue Draw(drawstates[i]) # Print out some timing information now = sdl.getTicks() if now > then: fps = (frames * 1000) / (now - then) sys.stderr.write("%2.2f frames per second\n" % (fps)) # TODO for x in drawstates: free stuff quit(0) return 0
def __init__(self, image): """ The image argument is a preloaded and converted pg.Surface object. """ self.image = image self.width, self.height = sdl.queryTexture(image)[-2:]