Skip to content

Commit

Permalink
Merge pull request #38 from remance/attempt-optimize-draw-map
Browse files Browse the repository at this point in the history
Attempt optimize draw map
  • Loading branch information
remance committed Apr 12, 2023
2 parents c9ef434 + 9cebbdc commit a4fc59e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
13 changes: 10 additions & 3 deletions gamescript/battlemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ def __init__(self, main_dir, screen_scale, height_map):
self.base_image = None # image before adding height map mode
self.image = None # image after adding height map mode

def draw_image(self, base_map, feature_map, place_name, camp_pos, battle):
self.image = pygame.Surface((len(base_map.map_array[0]), len(base_map.map_array)))
self.rect = self.image.get_rect(topleft=(0, 0))
def recolour_map_and_build_move_and_def_arrays(self, feature_map, base_map, battle):

if (type(feature_map), type(base_map)) != (FeatureMap, BaseMap):
raise TypeError()

for row_pos in range(0, self.image.get_width()): # recolour the map
speed_array = []
Expand All @@ -162,6 +163,12 @@ def draw_image(self, base_map, feature_map, place_name, camp_pos, battle):
battle.map_move_array.append(speed_array)
battle.map_def_array.append(def_array)

def draw_image(self, base_map, feature_map, place_name, camp_pos, battle):
self.image = pygame.Surface((len(base_map.map_array[0]), len(base_map.map_array)))
self.rect = self.image.get_rect(topleft=(0, 0))

self.recolour_map_and_build_move_and_def_arrays(feature_map, base_map, battle)

# Blur map to make it look older
data = pygame.image.tostring(self.image, "RGB") # convert image to string data for filtering effect
img = Image.frombytes("RGB", (self.image.get_width(), self.image.get_height()),
Expand Down
3 changes: 0 additions & 3 deletions tests/__init__.py

This file was deleted.

Binary file added tests/base_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/feature_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/height_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/map1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions tests/test_battlemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pygame
import os


def compare_surfaces(surface_a, surface_b):
if surface_a.get_size() != surface_b.get_size(): return False
for x in range(surface_a.get_size()[0]):
for y in range(surface_a.get_size()[1]):
if surface_a.get_at((x, y)) != surface_b.get_at((x, y)): return False
return True


def test_recolour_man_and_build_move_and_def_arrays():
from gamescript.battlemap import BeautifulMap
from gamescript.battlemap import BaseMap
from gamescript.battlemap import FeatureMap
from gamescript.battlemap import HeightMap
from gamescript import datamap

class MinifiedBattle:

def __init__(self):
self.map_move_array = [] # array for pathfinding
self.map_def_array = [] # array for defence calculation

main_dir = os.path.join(os.path.split(os.path.abspath(__file__))[0], '..')
screen_scale = (1.0, 1.0)
ruleset_folder = "historical"
language = "en"

pygame.display.set_mode((0, 0), pygame.HIDDEN)
battle_map_data = datamap.BattleMapData(main_dir, screen_scale, ruleset_folder, language)

height_map = HeightMap()
height_map.draw_image(pygame.image.load(os.path.join(main_dir, "tests/height_map.png")))

battle_map = BeautifulMap(
main_dir=main_dir,
screen_scale=None,
height_map=height_map,
)
battle_map.battle_map_colour = battle_map_data.battle_map_colour

base_map = BaseMap(main_dir=main_dir)
base_map.terrain_colour = battle_map_data.terrain_colour
base_map.draw_image(pygame.image.load(os.path.join(main_dir, "tests/base_map.png")))

feature_map = FeatureMap(main_dir=main_dir)
feature_map.feature_colour = battle_map_data.feature_colour
feature_map.draw_image(pygame.image.load(os.path.join(main_dir, "tests/feature_map.png")))
feature_map.feature_mod = battle_map_data.feature_mod

battle_map.image = pygame.Surface((len(base_map.map_array[0]), len(base_map.map_array)))

mb = MinifiedBattle()
battle_map.recolour_map_and_build_move_and_def_arrays(
feature_map=feature_map,
base_map=base_map,
battle=mb,
)

assert compare_surfaces(battle_map.image, pygame.image.load(os.path.join(main_dir, "tests/map1.png")))

0 comments on commit a4fc59e

Please sign in to comment.