Clean up texture handing.

This commit is contained in:
Quantum 2018-08-23 21:27:19 -04:00
parent f9d83a2add
commit 3079c612f6
2 changed files with 48 additions and 75 deletions

View file

@ -110,18 +110,19 @@ class Applet(pyglet.window.Window):
self.moving = True self.moving = True
self.info_precise = False self.info_precise = False
self.atmosphere = True self.atmosphere = True
self.cloud = not texture.badcard self.cloud = True
self.tick = self.world.tick_length self.tick = self.world.tick_length
self.ticks = [1, 2, 5, 10, 20, 40, 60, # Second range self.ticks = [
120, 300, 600, 1200, 1800, 2700, 3600, # Minute range 1, 2, 5, 10, 20, 40, 60, # Second range
7200, 14400, 21600, 43200, 86400, # Hour range 120, 300, 600, 1200, 1800, 2700, 3600, # Minute range
172800, 432000, 604800, # 2, 5, 7 days 7200, 14400, 21600, 43200, 86400, # Hour range
1209600, 2592000, # 2 week, 1 month 172800, 432000, 604800, # 2, 5, 7 days
5270400, 7884000, 15768000, 31536000, # 2, 3, 6, 12 months 1209600, 2592000, # 2 week, 1 month
63072000, 157680000, 315360000, # 2, 5, 10 years 5270400, 7884000, 15768000, 31536000, # 2, 3, 6, 12 months
630720000, 1576800000, 3153600000, # 20, 50, 100 years 63072000, 157680000, 315360000, # 2, 5, 10 years
] 630720000, 1576800000, 3153600000, # 20, 50, 100 years
]
self.__time_per_second_cache = None self.__time_per_second_cache = None
self.__time_per_second_value = None self.__time_per_second_value = None
self.__time_accumulate = 0 self.__time_accumulate = 0

View file

@ -9,26 +9,14 @@ from io import BytesIO
import six import six
from pyglet import image from pyglet import image
from pyglet.gl import * from pyglet.gl import *
from six.moves import zip from six.moves import zip, range
try: try:
from punyverse._glgeom import bgr_to_rgb, flip_vertical from ._glgeom import bgr_to_rgb, flip_vertical
except ImportError: except ImportError:
import warnings import warnings
warnings.warn('Compile _glgeom.c, or double the start up time.') warnings.warn('Compile _glgeom.c, or double the start up time.')
# Use magick when _glgeom is not compiled (is actually slower)
try:
from pgmagick import Blob, Image
except ImportError:
magick = False
else:
magick = True
from six.moves import range
def bgr_to_rgb(source, width, height, alpha=False): def bgr_to_rgb(source, width, height, alpha=False):
length = len(source) length = len(source)
depth = length // (width * height) depth = length // (width * height)
@ -53,8 +41,6 @@ except ImportError:
y2 = height - y1 - 1 y2 = height - y1 - 1
result[y1 * row:y1 * row + row] = source[y2 * row:y2 * row + row] result[y1 * row:y1 * row + row] = source[y2 * row:y2 * row + row]
return six.binary_type(result) return six.binary_type(result)
else:
magick = False
__all__ = ['load_texture', 'load_clouds', 'load_image', 'get_best_texture'] __all__ = ['load_texture', 'load_clouds', 'load_image', 'get_best_texture']
@ -63,12 +49,11 @@ cache = {}
max_texture = None max_texture = None
power_of_two = None power_of_two = None
badcard = False
bgra = False bgra = False
def init(): def init():
global max_texture, power_of_two, badcard, bgra, magick global max_texture, power_of_two, bgra
if max_texture is None: if max_texture is None:
buf = c_int() buf = c_int()
@ -84,40 +69,38 @@ is_power2 = lambda num: num != 0 and ((num & (num - 1)) == 0)
def image_info(data): def image_info(data):
data = str(data) data = six.binary_type(data)
size = len(data) size = len(data)
height = -1 height = -1
width = -1 width = -1
content_type = '' content_type = ''
# handle GIFs # handle GIFs
if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'): if size >= 10 and data[:6] in (b'GIF87a', b'GIF89a'):
# Check to see if content_type is correct # Check to see if content_type is correct
content_type = 'image/gif' content_type = 'image/gif'
w, h = struct.unpack("<HH", data[6:10]) w, h = struct.unpack('<HH', data[6:10])
width = int(w) width = int(w)
height = int(h) height = int(h)
# See PNG 2. Edition spec (http://www.w3.org/TR/PNG/) # See PNG 2. Edition spec (http://www.w3.org/TR/PNG/)
# Bytes 0-7 are below, 4-byte chunk length, then 'IHDR' # Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
# and finally the 4-byte width, height # and finally the 4-byte width, height
elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n') elif size >= 24 and data.startswith(b'\211PNG\r\n\032\n') and data[12:16] == b'IHDR':
and (data[12:16] == 'IHDR')):
content_type = 'image/png' content_type = 'image/png'
w, h = struct.unpack(">LL", data[16:24]) w, h = struct.unpack('>LL', data[16:24])
width = int(w) width = int(w)
height = int(h) height = int(h)
# Maybe this is for an older PNG version. # Maybe this is for an older PNG version.
elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'): elif size >= 16 and data.startswith(b'\211PNG\r\n\032\n'):
# Check to see if we have the right content type
content_type = 'image/png' content_type = 'image/png'
w, h = struct.unpack(">LL", data[8:16]) w, h = struct.unpack('>LL', data[8:16])
width = int(w) width = int(w)
height = int(h) height = int(h)
# handle JPEGs # handle JPEGs
elif (size >= 2) and data.startswith('\377\330'): elif size >= 2 and data.startswith(b'\377\330'):
content_type = 'image/jpeg' content_type = 'image/jpeg'
jpeg = BytesIO(data) jpeg = BytesIO(data)
jpeg.read(2) jpeg.read(2)
@ -130,13 +113,11 @@ def image_info(data):
b = jpeg.read(1) b = jpeg.read(1)
if 0xC0 <= ord(b) <= 0xC3: if 0xC0 <= ord(b) <= 0xC3:
jpeg.read(3) jpeg.read(3)
h, w = struct.unpack(">HH", jpeg.read(4)) height, width = struct.unpack('>HH', jpeg.read(4))
break break
else: else:
jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0]) - 2) jpeg.read(int(struct.unpack('>H', jpeg.read(2))[0]) - 2)
b = jpeg.read(1) b = jpeg.read(1)
width = int(w)
height = int(h)
except struct.error: except struct.error:
pass pass
except ValueError: except ValueError:
@ -162,47 +143,38 @@ def load_image(file, path):
try: try:
file = open(path, 'rb') file = open(path, 'rb')
except IOError: except IOError:
print('exists not') print('does not exist')
raise ValueError('Texture exists not') raise ValueError('Texture does not exist')
type, width, height = image_info(file.read(65536)) type, width, height = image_info(file.read(65536))
file.seek(0, 0) file.seek(0, 0)
if type: if type:
check_size(width, height) check_size(width, height)
if magick: try:
file.close() raw = image.load(path, file=file)
file = Image(path.encode('mbcs' if os.name == 'nt' else 'utf8')) except Exception:
geo = file.size() print('cannot be loaded')
check_size(geo.width(), geo.height()) raise ValueError('cannot be loaded')
print()
blob = Blob()
file.flip()
file.write(blob, 'RGBA')
texture = blob.data
mode = GL_RGBA
else:
try:
raw = image.load(path, file=file)
except IOError:
print('exists not')
raise ValueError('Texture exists not')
width, height = raw.width, raw.height width, height = raw.width, raw.height
check_size(width, height) check_size(width, height)
print() print()
mode = GL_RGBA if 'A' in raw.format else GL_RGB mode = GL_RGBA if 'A' in raw.format else GL_RGB
# Flip from BGR to RGB
if raw.format in ('BGR', 'BGRA'): # Flip from BGR to RGB
if bgra: if raw.format in ('BGR', 'BGRA'):
mode = {GL_RGBA: GL_BGRA, GL_RGB: GL_BGR}[mode] if bgra:
texture = raw.data mode = GL_BGRA if 'A' in raw.format else GL_BGR
else:
texture = bgr_to_rgb(raw.data, width, height, 'A' in raw.format)
elif raw.format in ('RGB', 'RGBA'):
texture = raw.data texture = raw.data
else: else:
texture = raw.get_data('RGBA', width * 4) texture = bgr_to_rgb(raw.data, width, height, 'A' in raw.format)
elif raw.format in ('RGB', 'RGBA'):
texture = raw.data
else:
texture = raw.get_data('RGBA', width * 4)
return path, width, height, len(raw.format), mode, flip_vertical(texture, width, height) return path, width, height, len(raw.format), mode, flip_vertical(texture, width, height)
@ -227,6 +199,7 @@ def load_texture(file):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
gluBuild2DMipmaps(GL_TEXTURE_2D, depth, width, height, mode, GL_UNSIGNED_BYTE, texture) gluBuild2DMipmaps(GL_TEXTURE_2D, depth, width, height, mode, GL_UNSIGNED_BYTE, texture)
cache[path] = id cache[path] = id
return id return id
@ -260,7 +233,6 @@ def load_clouds(file):
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, six.binary_type(pixels)) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, six.binary_type(pixels))
cache[path] = id cache[path] = id
return id return id