
import os
import collections
import pickle
import struct
import copy
import itertools
import time

import opengl.cubes as cubes
import game_global as gg
import huffman
import scene_draw
import opengl.image_manip as image_manip
import client_core
import net
from PIL import Image

assert gg.M == cubes.m
assert gg.N == cubes.n

cubes.cubes_init()

# 'display_args.pkl' is generated by running client.py
# with opengl rendering and environment variable
# LOG_RENDER_ARGS=1
try:
    with open('display_args.pkl', 'rb') as f:
        v = pickle.load(f)
except IOError:
    print("cannot read 'display_args.pkl'.")
    print("maybe try to regenerate it, by running one client like")
    print("$ LOG_RENDER_ARGS=display_args.pkl python3 client.py localhost player")
    raise
v = [x for x in v if x is not None] # purge invalid gamestates

gamestate = v[0]
im1 = Image.open("jd_legat.jpg")
im2 = Image.open("cc.jpg")
icon_size = 64
a1 = bytearray(im1.resize((icon_size, icon_size)).convert('RGBA').tobytes())
a2 = bytearray(im2.resize((icon_size, icon_size)).convert('RGBA').tobytes())
text1 = bytearray([
    0, 0, 0, 255,
    255, 255, 255, 255,
    255, 255, 255, 255,
    0, 0, 0, 255])
text2 = bytearray([
    255, 0, 0, 255,
    255, 0, 255, 255,
    255, 255, 0, 255,
    0, 0, 0, 255])
#cubes.set_textures(text1, text2, 2, 2)
cubes.set_textures(a1, a2, icon_size, icon_size)
pixels = scene_draw.render_gamestate(gamestate)
cubes.cubes_image_normalize(pixels)
image_manip.export_bmp_py(pixels, "img_test_texture.bmp".encode('ascii'))

