<commit_before>#!/usr/bin/env python3

import sys
import argparse
import math

import pygame

import cart

class Renderer:
    def __init__(self, cartridge):
        self.cartridge = cartridge
        self.tile_surface_cache = {}

    def render(self):
        clock = pygame.time.Clock()
        pygame.init()
        display_surface = pygame.display.set_mode((1024, 768))
        # display_surface = pygame.display.set_mode((1440, 900), pygame.FULLSCREEN)
        view_surface = pygame.Surface((cart.Map.section_width * cart.TileMap.tile_width, cart.Map.section_height * cart.TileMap.tile_width))
        scroll_buffer = ScrollBuffer(renderer=self)
        scroll_x = 0
        # testing sprites
        bob = self.surface_for_tile(0xD, attr=2)
        bob_x = 0
        # -
        is_running = True
        while is_running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    is_running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                        scroll_x = 1
                    elif event.key == pygame.K_LEFT:
                        scroll_x = -1
                elif event.type == pygame.KEYUP:
                    scroll_x = 0
            if scroll_x != 0:
                if abs(scroll_x) < 10:
                    if scroll_x > 0:
                        scroll_x += 1
                    else:
                        scroll_x -= 1
                scroll_buffer.scroll(scroll_x, 0)
            scroll_buffer.render(view_surface)
            # testing sprites
            bob_x += scroll_x
            sprite_coord = scroll_buffer.map_to_view_coord((cart.Map.section_height / 2 * cart.TileMap.tile_width + bob_x, cart.Map.section_height * cart.TileMap.tile_width - 5 * cart.TileMap.tile_width))
            view_surface.blit(bob, sprite_coord)
            # may want option for smoothscale
            pygame.transform.scale(view_surface, (1024, 768), display_surface)
            pygame.display.update()
            clock.tick(60)
        pygame.quit()

    def surface_for_map_tile(self, map_row, map_col):
        tile_number = self.cartridge.map.get_tile(map_row, map_col)
        if tile_number <= 0:
            return None
        attr = self.cartridge.map.get_attr(map_row, map_col)
        return self.surface_for_tile(tile_number, attr)

    def surface_for_tile(self, tile_number, attr=0):
        tile_lookup = (tile_number, attr)
        if tile_lookup in self.tile_surface_cache:
            return self.tile_surface_cache[tile_lookup]
        transform = (attr >> 4) & 0xF
        palette = attr & 0xF
        tile = self.cartridge.tile_map[tile_number - 1]
        surface = pygame.Surface((cart.TileMap.tile_width, cart.TileMap.tile_width))
        pix_array = pygame.PixelArray(surface)
        for row in range(0, cart.TileMap.tile_width):
            for col in range(0, cart.TileMap.tile_width):
                p = tile[row][col]
                if p > 0:
                    color = self.cartridge.lookup_background_color(palette, p)
                else:
                    color = self.cartridge.lookup_universal_background_color()
                if transform == 0:
                    pix_array[col, row] = color
                elif transform == 1:  # vertical axis flip
                    pix_array[7 - col, row] = color
        self.tile_surface_cache[tile_lookup] = surface
        return surface
      
        
class ScrollBuffer:
    quad_width = cart.Map.section_width * cart.TileMap.tile_width
    quad_height = cart.Map.section_height * cart.TileMap.tile_width
    
    def __init__(self, renderer):
        self.renderer = renderer
        self.quadrants = [
            pygame.Surface((cart.Map.section_width * cart.TileMap.tile_width, cart.Map.section_height * cart.TileMap.tile_width))
            for i in range(0, 4)
        ]
        self.quadrants[0].fill((255, 0, 0))
        self.quadrants[1].fill((0, 255, 0))
        self.quadrants[2].fill((0, 0, 255))
        self.quadrants[3].fill((255, 0, 255))
        # left, top, right, bottom
        self.view_rect = (
            ScrollBuffer.quad_width / 2,
            ScrollBuffer.quad_height / 2,
            ScrollBuffer.quad_width / 2 + ScrollBuffer.quad_width,
            ScrollBuffer.quad_height / 2 + ScrollBuffer.quad_height
        )
        self.map_coord = (0, 0)<commit_msg>Started work on following camera mechanic<commit_after>#!/usr/bin/env python3

import sys
import argparse
import math

import pygame

import cart

class Renderer:
    def __init__(self, cartridge):
        self.cartridge = cartridge
        self.tile_surface_cache = {}

    def render(self):
        clock = pygame.time.Clock()
        pygame.init()
        display_surface = pygame.display.set_mode((1024, 768))
        # display_surface = pygame.display.set_mode((1440, 900), pygame.FULLSCREEN)
        view_surface = pygame.Surface((cart.Map.section_width * cart.TileMap.tile_width, cart.Map.section_height * cart.TileMap.tile_width))
        scroll_buffer = ScrollBuffer(renderer=self)
        scroll_x = 0
        scroll_y = 0
        # testing sprites
        camera = Camera(scroll_buffer=scroll_buffer)
        bob = self.surface_for_tile(0xD, attr=2)
        bob_x = cart.Map.section_width / 2 * cart.TileMap.tile_width
        bob_y = cart.Map.section_height / 2 * cart.TileMap.tile_width
        # -
        is_running = True
        while is_running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    is_running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                        scroll_x = 1
                    elif event.key == pygame.K_LEFT:
                        scroll_x = -1
                    elif event.key == pygame.K_DOWN:
                        scroll_y += 1
                    elif event.key == pygame.K_UP:
                        scroll_y -= 1
                elif event.type == pygame.KEYUP:
                    scroll_x = 0
                    scroll_y = 0
            if scroll_x != 0:
                if abs(scroll_x) < 10:
                    if scroll_x > 0:
                        scroll_x += 1
                    else:
                        scroll_x -= 1
                bob_x += scroll_x
            if scroll_y != 0:
                if abs(scroll_y) < 10:
                    if scroll_y > 0:
                        scroll_y += 1
                    else:
                        scroll_y -= 1
                bob_y += scroll_y
            camera.follow(bob_x, bob_y)
            scroll_buffer.render(view_surface)
            # testing sprites
            sprite_coord = scroll_buffer.map_to_view_coord((bob_x, bob_y))
            view_surface.blit(bob, sprite_coord)
            # may want option for smoothscale
            pygame.transform.scale(view_surface, (1024, 768), display_surface)
            pygame.display.update()
            clock.tick(60)
        pygame.quit()

    def surface_for_map_tile(self, map_row, map_col):
        tile_number = self.cartridge.map.get_tile(map_row, map_col)
        if tile_number <= 0:
            return None
        attr = self.cartridge.map.get_attr(map_row, map_col)
        return self.surface_for_tile(tile_number, attr)

    def surface_for_tile(self, tile_number, attr=0):
        tile_lookup = (tile_number, attr)
        if tile_lookup in self.tile_surface_cache:
            return self.tile_surface_cache[tile_lookup]
        transform = (attr >> 4) & 0xF
        palette = attr & 0xF
        tile = self.cartridge.tile_map[tile_number - 1]
        surface = pygame.Surface((cart.TileMap.tile_width, cart.TileMap.tile_width))
        pix_array = pygame.PixelArray(surface)
        for row in range(0, cart.TileMap.tile_width):
            for col in range(0, cart.TileMap.tile_width):
                p = tile[row][col]
                if p > 0:
                    color = self.cartridge.lookup_background_color(palette, p)
                else:
                    color = self.cartridge.lookup_universal_background_color()
                if transform == 0:
                    pix_array[col, row] = color
                elif transform == 1:  # vertical axis flip
                    pix_array[7 - col, row] = color
        self.tile_surface_cache[tile_lookup] = surface
        return surface
      
        
class Camera:
    FOLLOW_CENTER = 0
    FOLLOW_STATIC = 1
    FOLLOW_REVEAL = 2
    
    def __init__(self, x=0, y=0, follow_mode=0, scroll_buffer=None):
        self.x = 0
        self.y = 0
        self.follow_mode = follow_mode
        self.scroll_buffer = scroll_buffer
        
    def follow(self, x, y):
        # follow center implementation for now
        new_x = x - (cart.Map.section_width * cart.TileMap.tile_width) / 2
        new_y = y - (cart.Map.section_height * cart.TileMap.tile_width) / 2
        delta_x = new_x - self.x
        delta_y = new_y - self.y
        self.x = new_x
        self.y = new_y
        self.scroll_buffer.scroll(delta_x, delta_y)
    
        
class ScrollBuffer:
    quad_width = cart.Map.section_width * cart.TileMap.tile_width
    quad_height = cart.Map.section_height * cart.TileMap.tile_width
    
    def __init__(self, renderer):
        self.renderer = renderer
        self.quadrants = [
            pygame.Surface((cart.Map.section_width * cart.TileMap.tile_width, cart.Map.section_height * cart.TileMap.tile_width))
            for i in range(0, 4)
        ]
        self.quadrants[0].fill((255, 0, 0))
        self.quadrants[1].fill((0, 255, 0))
        self.quadrants[2].fill((0, 0, 255))
        self.quadrants[3].fill((255, 0, 255))
        # left, top, right, bottom
        self.view_rect = (
            ScrollBuffer.quad_width / 2,
            ScrollBuffer.quad_height / 2,
            ScrollBuffer.quad_width / 2 + ScrollBuffer.quad_width,
            ScrollBuffer.quad_height / 2 + ScrollBuffer.quad_height
        )
        self.map_coord = (0, 0)
{'fraction_non_alphanumeric': 0.09287342650411338, 'fraction_numerical': 0.018832391713747645, 'mean_word_length': 1.745578231292517, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '837871', 'n_tokens_mistral': 3239, 'n_tokens_neox': 2964, 'n_words': 615}