Esempio n. 1
0
    def __buildWindow(self):
        if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0:
            raise Exception(sdl2.SDL_GetError())

        sdlimage.IMG_Init(sdlimage.IMG_INIT_PNG | sdlimage.IMG_INIT_JPG)

        sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK,
                                 sdl2.SDL_GL_CONTEXT_PROFILE_CORE)

        self.window = sdl2.SDL_CreateWindow(b'ETGG2801 Example', 0, 0,
                                            self.size[0], self.size[1],
                                            sdl2.SDL_WINDOW_OPENGL)

        self.glcontext = sdl2.SDL_GL_CreateContext(self.window)
        if not self.glcontext:
            sdl2.SDL_DestroyWindow(self.window)
            raise Exception(sdl2.SDL_GetError())

        # keep application from receiving text input events
        sdl2.SDL_StopTextInput()
Esempio n. 2
0
def check_image_codecs():
    sdl2.SDL_Init(0)
    try:
        libs = {
            'JPEG': sdlimage.IMG_INIT_JPG,
            'PNG': sdlimage.IMG_INIT_PNG,
            'TIFF': sdlimage.IMG_INIT_TIF,
            'WEBP': sdlimage.IMG_INIT_WEBP
        }
        for lib, flags in libs.items():
            sdlimage.IMG_SetError(b"")
            ret = sdlimage.IMG_Init(flags)
            err = sdlimage.IMG_GetError()
            if err:
                yield lib, err.decode('utf-8')
            if ret & flags == flags:
                yield lib, None
            else:
                yield lib, True
            sdlimage.IMG_Quit()
    finally:
        sdl2.SDL_Quit()
Esempio n. 3
0
    def createWindow(self):
        # Create the window context
        if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0:
            print(sdl2.SDL_GetError())
            return -1
        sdl2ttf.TTF_Init()
        sdl2img.IMG_Init(sdl2img.IMG_INIT_PNG)

        self.window = sdl2.SDL_CreateWindow(self.params["title"].encode(),
                                            sdl2.SDL_WINDOWPOS_UNDEFINED,
                                            sdl2.SDL_WINDOWPOS_UNDEFINED,
                                            self.window_width,
                                            self.window_height,
                                            sdl2.SDL_WINDOW_OPENGL)
        sdl2.SDL_SetWindowBordered(self.window, self.show_borders)
        if not self.window:
            print(sdl2.SDL_GetError())
            return -1

        # Renderer
        self.renderer = sdl2.SDL_CreateRenderer(
            self.window, -1,
            sdl2.SDL_RENDERER_ACCELERATED | sdl2.SDL_RENDERER_PRESENTVSYNC)

        # Build the GUI
        self.buildElements()

        # onStart handler
        if self.onStart is not None:
            if self.onStart[1] is None:
                self.onStart[0]()
            else:
                self.onStart[0](self.onStart[1])

        # look for events
        self._loopForEvents()
Esempio n. 4
0
def run():
    out_blocks = ""
    out_images = ""
    all_blocks = []

    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_EVERYTHING)
    sdl_image.IMG_Init(sdl_image.IMG_INIT_PNG)

    window = sdl2.ext.Window("BAConv", size=(1024, 384))
    window.show()
    ren = sdl2.ext.Renderer(window, flags=sdl2.SDL_RENDERER_ACCELERATED)
    # makes zoomed graphics blocky (for retro effect)
    sdl2.SDL_SetHint(sdl2.SDL_HINT_RENDER_SCALE_QUALITY, b"nearest")
    # makes the graphics look and act like the desired screen size, even though they may be rendered at a different one
    sdl2.SDL_RenderSetLogicalSize(ren.sdlrenderer, 256, 96)

    frames = []
    count = 0
    limit = 8000  # only deal with this many frames

    pass1 = True  # True means calculate blocks, False means load from pre-calculated file

    if pass1:
        # pass 1 - gather all the blocks
        for i in range(0, int(6565 / 4)):
            # for i in range(43, int(600 / 4)):
            count += 1
            if count > limit:  # stop early
                break
            events = sdl2.ext.get_events()  # otherwise window doesn't update
            for event in events:
                if event.type == sdl2.SDL_QUIT:
                    exit(0)
            ren.clear()
            file = f"image-{i*4+1:03}.png"
            sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
            frame = processImage(
                file, ren)  # also draws left hand side pre-processed
            frames.append(frame)

            renderImage(frame, ren)  # right hand side post-process
            sdl2.SDL_RenderPresent(ren.sdlrenderer)
            window.refresh()

        print(f"Processed {count} frames.")

        values = 0
        size = 0
        for frame in frames:
            values += len(frame)
            size += frame[1] * (len(frame) - 2) + 1
            print(frame)

        print(f"Raw values: {values}")
        print(f"Average values per frame: {values/len(frames)}")
        print(f"Theoretical size: {size/8}")

        exit(0)

        # store blocks in file
        file_lines = []
        for block in all_blocks:
            file_lines.append(block.serialize())

        with open("data_file.txt", "w") as write_file:
            write_file.writelines("\n".join(file_lines))
    else:
        # read in blocks from file (quicker)
        with open("data_file.txt", "r") as read_file:
            file_lines = read_file.readlines()
            all_blocks = []
            for line in file_lines:
                all_blocks.append(Block.fromData(line.split(",")))

    # sort and choose top blocks
    all_blocks.sort(reverse=True)

    # 1024 is currently all you're planning to store
    # todo: choose final number of blocks to use
    # all_blocks=all_blocks[0:1024]
    for i, b in enumerate(all_blocks):
        print(f"Block {i}: {b.count}")

    # pass 2 - build new images with top blocks
    all_images = []
    count = 0
    # for i in range(43, int(6509 / 4)):
    for i in range(0, int(6565 / 4)):
        # for i in range(43, int(600 / 4)):
        count += 1
        if count > limit:  # stop early
            break
        ren.clear()
        file = f"image-{i*4+1:03}.png"
        sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
        image = reprocessImage(
            file, ren, all_blocks)  # also draws left hand side pre-processed
        all_images.append(image)
        renderImage(image, all_blocks, ren)  # right hand side post-process
        events = sdl2.ext.get_events()  # otherwise window doesn't update
        sdl2.SDL_RenderPresent(ren.sdlrenderer)
        window.refresh()

    # play back final frames in loop
    fr = 0
    while True:
        fr = (fr + 1) % len(all_images)
        events = sdl2.ext.get_events()  # otherwise window doesn't update
        for event in events:
            if event.type == sdl2.SDL_QUIT:
                exit(0)
        ren.clear()
        file = f"image-{i*2+1:03}.png"
        sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
        renderImage(all_images[fr], all_blocks, ren)
        sdl2.SDL_RenderPresent(ren.sdlrenderer)
        window.refresh()
Esempio n. 5
0
# external lib import
import os
import ctypes

# import sdl libs
import sdl2.ext
import sdl2.sdlimage as sdl_image

# initialise for loading PNGs and JPGs
sdl_image.IMG_Init(sdl_image.IMG_INIT_PNG)
sdl_image.IMG_Init(sdl_image.IMG_INIT_JPG)

# import my files
import px_entity
import px_vector
from px_vector import Vec3, rand_num
from px_log import log

# globals
graphics_debug = False


class eAlign:
    left, centre, right = range(0, 3)


# color class where components are between 0 and 1
# as intended by nature
class Color(object):
    def __init__(self, r, g, b, a=1):
        self.r = r
Esempio n. 6
0
 def setUpClass(cls):
     SDL_Init(0)
     sdlimage.IMG_Init(sdlimage.IMG_INIT_JPG | sdlimage.IMG_INIT_PNG
                       | sdlimage.IMG_INIT_TIF | sdlimage.IMG_INIT_WEBP)
Esempio n. 7
0
def run():
    out_blocks = ""
    out_images = ""
    all_blocks = []

    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_EVERYTHING)
    sdl_image.IMG_Init(sdl_image.IMG_INIT_PNG)

    window = sdl2.ext.Window("BAConv", size=(1024, 384))
    window.show()
    ren = sdl2.ext.Renderer(window, flags=sdl2.SDL_RENDERER_ACCELERATED)
    # makes zoomed graphics blocky (for retro effect)
    sdl2.SDL_SetHint(sdl2.SDL_HINT_RENDER_SCALE_QUALITY, b"nearest")
    # makes the graphics look and act like the desired screen size, even though they may be rendered at a different one
    sdl2.SDL_RenderSetLogicalSize(ren.sdlrenderer, 256, 96)
    max_blocks = 0
    total_blocks = 0
    count = 0
    limit = 100

    # pass 1 - gather all the blocks
    for i in range(0, int(6565 / 4)):
        count += 1
        if count > limit:  # stop early
            break
        events = sdl2.ext.get_events()  # otherwise window doesn't update
        ren.clear()
        file = f"image-{i*4+1:03}.png"
        sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
        image, blocks = processImage(
            file, ren)  # also draws left hand side pre-processed

        # add blocks to master list
        for i, block in enumerate(blocks):
            found = False
            for j, b in enumerate(all_blocks):
                if block == b:  # better match found update existing record
                    all_blocks[j].count += blocks[i].count
                    found = True  # found a match of some sort
            if not found:  # totally new block
                all_blocks.append(block)

        num_blocks = len(blocks)
        total_blocks += num_blocks
        if max_blocks < num_blocks:
            max_blocks = num_blocks
        renderImage(image, blocks, ren)  # right hand side post-process
        sdl2.SDL_RenderPresent(ren.sdlrenderer)
        window.refresh()

    print(f"Max blocks: {max_blocks}")
    print(f"Total blocks: {total_blocks}")
    print(f"Average blocks: {total_blocks/count}")
    print(f"Processed {count} frames.")

    # sort and choose top blocks
    all_blocks.sort(reverse=True)

    # 1024 is currently all you're planning to store
    # todo: choose final number of blocks to use
    # all_blocks=all_blocks[0:1024]
    for i, b in enumerate(all_blocks):
        print(f"Block {i}: {b.count}")

    # pass 2 - build new images with top blocks
    all_images = []
    count = 0
    for i in range(43, int(6509 / 2)):
        count += 1
        if count > limit:  # stop early
            break
        ren.clear()
        file = f"image-{i*2+1:03}.png"
        sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
        image = reprocessImage(
            file, ren, all_blocks)  # also draws left hand side pre-processed
        all_images.append(image)
        renderImage(image, all_blocks, ren)  # right hand side post-process
        events = sdl2.ext.get_events()  # otherwise window doesn't update
        sdl2.SDL_RenderPresent(ren.sdlrenderer)
        window.refresh()

    fr = 0
    while True:
        fr = (fr + 1) % len(all_images)
        events = sdl2.ext.get_events()  # otherwise window doesn't update
        ren.clear()
        file = f"image-{i*2+1:03}.png"
        sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
        renderImage(all_images[fr], all_blocks, ren)
        sdl2.SDL_RenderPresent(ren.sdlrenderer)
        window.refresh()
        sdl2.SDL_Delay(10)
Esempio n. 8
0
def run():
    out_blocks = ""
    out_images = ""

    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_EVERYTHING)
    sdl_image.IMG_Init(sdl_image.IMG_INIT_PNG)

    window = sdl2.ext.Window("BAConv", size=(1024, 384))
    window.show()
    ren = sdl2.ext.Renderer(window, flags=sdl2.SDL_RENDERER_ACCELERATED)
    # makes zoomed graphics blocky (for retro effect)
    sdl2.SDL_SetHint(sdl2.SDL_HINT_RENDER_SCALE_QUALITY, b"nearest")
    # makes the graphics look and act like the desired screen size, even though they may be rendered at a different one
    sdl2.SDL_RenderSetLogicalSize(ren.sdlrenderer, 256, 96)
    max_blocks = 0
    total_blocks = 0
    count = 0
    cart = 1

    for i in range(0, int(6565 / 2)):
        count += 1
        # for i in range(254, 355):
        # while True:
        # 	i=304
        events = sdl2.ext.get_events()
        ren.clear()
        file = f"image-{i*2+1:03}.png"
        sdl2.SDL_SetWindowTitle(window.window, file.encode('utf-8'))
        image, blocks = processImage(
            file, ren)  # also draws left hand side pre-processed
        num_blocks = len(blocks)
        total_blocks += num_blocks
        if max_blocks < num_blocks:
            max_blocks = num_blocks
            # print(f"New max:{num_blocks}")
        renderImage(image, blocks, ren)  # right hand side post-process
        sdl2.SDL_RenderPresent(ren.sdlrenderer)
        window.refresh()

        # output encoded image
        out_images += '"'
        for i in range(0, 192):
            if image[i] + 35 == 92:
                out_images += '\\\\'
            else:
                out_images += f"{chr(image[i]+35)}"
        out_images += '",\n'

        # output encoded blocks
        out_blocks += "{"
        for block in blocks[2:]:
            out_blocks += '"'
            for y in range(0, 8):
                val = 0
                for x in range(0, 8):
                    if block[y * 8 + x] == 1:
                        val += 2**x
                out_blocks += f"{val:02x}"
            out_blocks += '",'
        out_blocks += "},\n"

        if count % 40 == 0:

            output = header.replace(
                '$$',
                f"{(cart+1):02}") + out_images + middle + out_blocks + footer
            outfile = open(f"badbadapple{cart:02}.p8", 'w')
            outfile.write(output)
            outfile.close()

            out_images = ''
            out_blocks = ''
            cart += 1

    print(out_images)

    print(f"Max blocks: {max_blocks}")
    print(f"Total blocks: {total_blocks}")
    print(f"Average blocks: {total_blocks/count}")
    print(f"Processed {count} frames.")