diff --git a/bootloader.py b/bootloader.py index 9cb3337..94a7144 100644 --- a/bootloader.py +++ b/bootloader.py @@ -4,21 +4,18 @@ import os import sys import uuid -def is_frozen(): - import imp - return (hasattr(sys, 'frozen') or # new py2exe - hasattr(sys, 'importers') # old py2exe - or imp.is_frozen('__main__')) # tools/freeze - if __name__ == '__main__': - if not is_frozen(): + try: + dir = os.path.dirname(sys.executable) + if sys.frozen == 'windows_exe': + sys.stdout = open(os.path.join(dir, 'punyverse.log'), 'a') + except AttributeError: sys.exit('This is only meant to be ran frozen.') - sys.path.insert(0, os.path.dirname(sys.executable)) + sys.path.insert(0, dir) import punyverse._model import punyverse._glgeom - with open('punyverse\__main__.py', 'r') as f: - code = f.read() + with open('punyverse\__main__.py', 'r') as code: exec(code) diff --git a/launcher.py b/launcher.py index 2664caf..911e7ae 100644 --- a/launcher.py +++ b/launcher.py @@ -25,7 +25,7 @@ for res in resources: data.append((dir, [join(parent, file)])) setup( - console=[{'dest_base': 'punyverse_debug', 'script': 'bootloader.py'}], + console=[{'dest_base': 'punyverse_debug', 'script': 'bootloader.py'}, 'small_images.py'], windows=[{'dest_base': 'punyverse', 'script': 'bootloader.py'}], data_files=data, options={'py2exe': { @@ -34,7 +34,7 @@ setup( '_ssl', 'unittest', 'doctest', 'PIL', 'email', 'distutils', 'pyglet.window.carbon', 'pyglet.window.xlib', 'pyglet.media.drivers.alsa', - 'win32wnet', 'netbios' + 'win32wnet', 'netbios', 'pgmagick' ], 'dll_excludes': ['MPR.dll', 'w9xpopen.exe'], } diff --git a/small_images.py b/small_images.py new file mode 100644 index 0000000..a0620cc --- /dev/null +++ b/small_images.py @@ -0,0 +1,115 @@ +import sys +import os + +from pyglet.gl import GLint, glGetIntegerv, GL_MAX_TEXTURE_SIZE +from ctypes import byref + +buf = GLint() +glGetIntegerv(GL_MAX_TEXTURE_SIZE, byref(buf)) +max_texture = buf.value + +del byref, GLint, glGetIntegerv, GL_MAX_TEXTURE_SIZE, buf + +def resize(width, height, target): + factor = (target + .0) / max(width, height) + return int(width * factor), int(height * factor) + +def fits(width, height): + return width < max_texture and height < max_texture + +def make_name(image, suffix): + name, ext = os.path.splitext(image) + return '%s_%s%s' % (name, suffix, ext) + +try: + import pgmagick + + def get_image(image): + return pgmagick.Image(image) + + def get_size(image): + size = image.size() + return size.width(), size.height() + + def scale(image, width, height): + image.filterType(pgmagick.FilterTypes.LanczosFilter) + image.scale(pgmagick.Geometry(width, height)) + return image + + def save(image, file): + image.write(file) +except ImportError: + import Image + + def get_image(image): + return Image.open(image) + + def get_size(image): + return image.size + + def scale(image, width, height): + return image.resize((width, height), Image.ANTIALIAS) + + def save(image, file): + image.save(file) + +def shrink(file): + image = get_image(file) + width, height = get_size(image) + if fits(width, height): + print 'no need' + return + width, height = resize(width, height, 2048) + if fits(width, height): + size = 'medium' + else: + width, height = resize(width, height, 1024) # 1024 is minimum + size = 'small' + print 'size %s, %dx%d...' % (size, width, height), + name = make_name(file, size) + if not os.path.exists(name): + image = scale(image, width, height) + print 'saved to:', os.path.basename(name) + save(image, name) + else: + print + +textures = [ + 'earth.jpg', + 'moon.jpg', + 'mars.jpg', + 'jupiter.jpg', + 'saturn.jpg', + 'moons/io.jpg', + 'moons/europa.jpg', + 'moons/ganymede.jpg', + 'moons/callisto.jpg', + 'moons/titan.jpg', + 'moons/rhea.jpg', + 'moons/iapetus.jpg', + 'moons/dione.jpg', + 'moons/tethys.jpg', + 'moons/enceladus.jpg', + 'moons/mimas.jpg', +] + +def frozen(): + import imp + return (hasattr(sys, "frozen") or # new py2exe + hasattr(sys, "importers") # old py2exe + or imp.is_frozen("__main__")) # tools/freeze + +def get_main_dir(): + if frozen(): + return os.path.dirname(sys.executable) + return os.path.dirname(__file__) + +def main(): + texture = os.path.join(get_main_dir(), 'punyverse', 'assets', 'textures') + for file in textures: + print 'Resizing %s:' % file, + file = os.path.join(texture, file.replace('/', os.sep)) + shrink(file) + +if __name__ == '__main__': + main()