|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
import random |
|
|
|
|
import math |
|
|
|
|
from math import sqrt |
|
|
|
|
import ctypes |
|
|
|
|
import sdl2 |
|
|
|
|
import sdl2.ext |
|
|
|
|
@ -14,8 +15,8 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
self.view_size = (800, 600) |
|
|
|
|
self.target_size = (800, 600) |
|
|
|
|
self.cell_size = 132 |
|
|
|
|
self.view_offset_x = 0 |
|
|
|
|
self.view_offset_y = 0 |
|
|
|
|
self.view_offset_x = 400 |
|
|
|
|
self.view_offset_y = 200 |
|
|
|
|
self.surface_width = 0 |
|
|
|
|
self.surface_height = 0 |
|
|
|
|
self.window = sdl2.ext.Window("My Game", size=self.view_size) |
|
|
|
|
@ -25,6 +26,7 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
self.sprite_managers = {} |
|
|
|
|
self.factory = sdl2.ext.SpriteFactory(renderer=self.renderer) |
|
|
|
|
self.fonts = self.generate_fonts("assets/decterm.ttf") |
|
|
|
|
self.cursor = (0, 0) |
|
|
|
|
|
|
|
|
|
def get_perf_counter(self): |
|
|
|
|
return int(sdl2.SDL_GetPerformanceCounter()) |
|
|
|
|
@ -39,6 +41,9 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
elif event.type == sdl2.SDL_KEYDOWN: |
|
|
|
|
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8') |
|
|
|
|
return f"KEYDOWN:{key}".lower() |
|
|
|
|
elif event.type == sdl2.SDL_MOUSEMOTION: |
|
|
|
|
x, y = event.motion.x, event.motion.y |
|
|
|
|
return f"MOUSEMOTION:{x}:{y}" |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
def clear_screen(self, color=(0, 0, 0, 255)): # Aggiunto valore alfa di default |
|
|
|
|
@ -54,26 +59,55 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
def load_spritesheet(self, name, path): |
|
|
|
|
surface = sdl2.ext.load_image(path.replace('.json', '.png')) |
|
|
|
|
texture = sdl2.ext.Texture(self.renderer, surface) |
|
|
|
|
self.sprite_managers[name] = SpriteManager(path, surface, texture) |
|
|
|
|
self.sprite_managers[name] = SpriteManager(path, surface, texture, self.cell_size) |
|
|
|
|
|
|
|
|
|
def load_tilesheet(self, name, path): |
|
|
|
|
surface = sdl2.ext.load_image(path) |
|
|
|
|
self.tile_managers[name] = TileManager(path.replace('.png', '.xml'), surface) |
|
|
|
|
texture = sdl2.ext.Texture(self.renderer, surface) |
|
|
|
|
|
|
|
|
|
self.tile_managers[name] = TileManager(path.replace('.png', '.xml'), surface, texture, self.cell_size) |
|
|
|
|
|
|
|
|
|
def load_sprite(self, spritesheet, name): |
|
|
|
|
return self.tile_managers[spritesheet].get_tile_rect(name) |
|
|
|
|
|
|
|
|
|
def render_sprite(self, name, x, y, frame): |
|
|
|
|
def render_sprite(self, name, x, y, frame, occlusion=0): |
|
|
|
|
srcrect, total_frames = self.sprite_managers[name].get_frame_rect(frame) |
|
|
|
|
|
|
|
|
|
y -= self.cell_size // 2 |
|
|
|
|
if occlusion: |
|
|
|
|
original_color_mods_r_g_b_a = self.apply_texture_color_mdod(self.sprite_managers[name].spritesheet_texture, occlusion) |
|
|
|
|
self.renderer.copy(self.sprite_managers[name].spritesheet_texture, |
|
|
|
|
dstrect=(x, y, srcrect[2], srcrect[3]), srcrect=srcrect) |
|
|
|
|
dstrect=(x, y, self.cell_size, self.cell_size), |
|
|
|
|
srcrect=srcrect) |
|
|
|
|
if occlusion: |
|
|
|
|
sdl2.SDL_SetTextureColorMod(self.sprite_managers[name].spritesheet_texture.tx, |
|
|
|
|
*original_color_mods_r_g_b_a) |
|
|
|
|
return (frame + 1) % total_frames |
|
|
|
|
|
|
|
|
|
def render_tile(self, spritesheet_name, cell, x, y): |
|
|
|
|
tile_name = cell.get('tile') |
|
|
|
|
occlusion = cell.get('occlusion', 0) |
|
|
|
|
texture = self.tile_managers[spritesheet_name].get_tilesheet_texture() |
|
|
|
|
tile_rect = self.tile_managers[spritesheet_name].get_tile_rect(tile_name) |
|
|
|
|
|
|
|
|
|
if tile_rect is not None: |
|
|
|
|
iso_x, iso_y = self.iso_transform(x, y) |
|
|
|
|
y_offset = self.cell_size // 2 - tile_rect[3] |
|
|
|
|
dst_rect = (iso_x, iso_y + y_offset, |
|
|
|
|
tile_rect[2], tile_rect[3]) |
|
|
|
|
|
|
|
|
|
# Salva i valori originali dei moduli di colore |
|
|
|
|
original_color_mods_r_g_b_a = self.apply_texture_color_mdod(texture, occlusion) |
|
|
|
|
|
|
|
|
|
# Copia la texture con il nuovo modulo di colore |
|
|
|
|
self.renderer.copy(self.tile_managers[spritesheet_name].get_tilesheet_texture(), |
|
|
|
|
sdl2.SDL_Rect(*tile_rect), |
|
|
|
|
sdl2.SDL_Rect(*dst_rect)) |
|
|
|
|
|
|
|
|
|
sdl2.SDL_SetTextureColorMod(texture.tx, *original_color_mods_r_g_b_a) |
|
|
|
|
|
|
|
|
|
def create_background(self, map, spritesheet_name): |
|
|
|
|
self.surface_width = round(self.cell_size * len(map[0]) * math.sqrt(2)) |
|
|
|
|
self.surface_height = round(self.cell_size * len(map) * math.sqrt(2) / 2) |
|
|
|
|
self.surface_width = round(self.cell_size * len(map[0])*sqrt(2)) |
|
|
|
|
self.surface_height = round(self.cell_size//2 * len(map)*sqrt(2)) |
|
|
|
|
bg_surface = sdl2.SDL_CreateRGBSurface(0, self.surface_width, self.surface_height, 32, 0, 0, 0, 0) |
|
|
|
|
|
|
|
|
|
def blit_tile(tile, x, y): |
|
|
|
|
@ -81,9 +115,10 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
if tile_rect is not None: |
|
|
|
|
vertical_offset = self.cell_size - tile_rect[3] |
|
|
|
|
horizontal_offset = (self.cell_size - tile_rect[2]) |
|
|
|
|
x, y = self.iso_transform(x, y) |
|
|
|
|
dst_rect = (x + horizontal_offset- self.cell_size//2 + self.surface_width // 2, |
|
|
|
|
y + vertical_offset, |
|
|
|
|
iso_x, iso_y = self.iso_transform(x, y) |
|
|
|
|
iso_y += vertical_offset |
|
|
|
|
iso_x += horizontal_offset |
|
|
|
|
dst_rect = (iso_x, iso_y, |
|
|
|
|
tile_rect[2], tile_rect[3]) |
|
|
|
|
sdl2.SDL_BlitSurface(self.tile_managers[spritesheet_name].get_tilesheet_surface(), |
|
|
|
|
sdl2.SDL_Rect(*tile_rect), |
|
|
|
|
@ -92,18 +127,15 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
#sdl2.SDL_FillRect(bg_surface, sdl2.SDL_Rect(*dst_rect), sdl2.SDL_MapRGB(bg_surface.contents.format, 0, 0, 255)) |
|
|
|
|
|
|
|
|
|
for y, row in enumerate(map): |
|
|
|
|
for x, tile_name in enumerate(row): |
|
|
|
|
if map[y][x] == 1: |
|
|
|
|
blit_tile("landscapeTiles_066", x, y) |
|
|
|
|
else: |
|
|
|
|
variation = random.choice(["067", "083", "075", "081"]) |
|
|
|
|
blit_tile(f"landscapeTiles_{variation}", x, y) |
|
|
|
|
for x, cell in enumerate(row): |
|
|
|
|
tile_name = cell.get('tile') |
|
|
|
|
blit_tile(tile_name, x, y) |
|
|
|
|
|
|
|
|
|
self.background_texture = self.factory.from_surface(bg_surface) |
|
|
|
|
|
|
|
|
|
def render_background(self): |
|
|
|
|
self.renderer.copy(self.background_texture, |
|
|
|
|
dstrect=(self.view_offset_x, self.view_offset_y, self.surface_width, self.surface_height), |
|
|
|
|
dstrect=(0, 0, self.surface_width, self.surface_height), |
|
|
|
|
srcrect=(0,0, self.surface_width, self.surface_height)) |
|
|
|
|
|
|
|
|
|
def get_frame_time(self, perf_counter): |
|
|
|
|
@ -116,3 +148,37 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui):
|
|
|
|
|
else: |
|
|
|
|
delay = 0 |
|
|
|
|
sdl2.SDL_Delay(delay) |
|
|
|
|
|
|
|
|
|
def draw_cursor(self): |
|
|
|
|
x, y = self.cursor |
|
|
|
|
x += self.view_offset_x - self.cell_size // 2 |
|
|
|
|
y -= self.view_offset_y - self.cell_size // 4 |
|
|
|
|
c_x, c_y = self.inv_iso_transform(x, y) |
|
|
|
|
print(c_x, c_y) |
|
|
|
|
iso_x, iso_y = self.iso_transform(c_x, c_y) |
|
|
|
|
self.renderer.draw_line(points=[(iso_x, iso_y), (iso_x + self.cell_size//2, iso_y + self.cell_size//4)], color=(255, 0, 0, 255)) |
|
|
|
|
self.renderer.draw_line(points=[(iso_x + self.cell_size//2, iso_y + self.cell_size//4), (iso_x + self.cell_size, iso_y)], color=(255, 0, 0, 255)) |
|
|
|
|
self.renderer.draw_line(points=[(iso_x + self.cell_size, iso_y), (iso_x + self.cell_size//2, iso_y - self.cell_size//4)], color=(255, 0, 0, 255)) |
|
|
|
|
self.renderer.draw_line(points=[(iso_x + self.cell_size//2, iso_y - self.cell_size//4), (iso_x, iso_y)], color=(255, 0, 0, 255)) |
|
|
|
|
return c_x, c_y |
|
|
|
|
|
|
|
|
|
def apply_texture_color_mdod(self, texture, occlusion): |
|
|
|
|
color_mod_r = int(255 * (1 - occlusion)) |
|
|
|
|
color_mod_g = int(255 * (1 - occlusion)) |
|
|
|
|
color_mod_b = int(255 * (1 - occlusion)) |
|
|
|
|
original_color_mods_r_g_b_a = [0] * 3 |
|
|
|
|
r_ptr = ctypes.pointer(ctypes.c_ubyte()) |
|
|
|
|
g_ptr = ctypes.pointer(ctypes.c_ubyte()) |
|
|
|
|
b_ptr = ctypes.pointer(ctypes.c_ubyte()) |
|
|
|
|
sdl2.SDL_GetTextureColorMod(texture.tx, |
|
|
|
|
r_ptr, |
|
|
|
|
g_ptr, |
|
|
|
|
b_ptr) |
|
|
|
|
original_color_mods_r_g_b_a[0] = r_ptr.contents.value |
|
|
|
|
original_color_mods_r_g_b_a[1] = g_ptr.contents.value |
|
|
|
|
original_color_mods_r_g_b_a[2] = b_ptr.contents.value |
|
|
|
|
sdl2.SDL_SetTextureColorMod(texture.tx, |
|
|
|
|
color_mod_r, |
|
|
|
|
color_mod_g, |
|
|
|
|
color_mod_b) |
|
|
|
|
return original_color_mods_r_g_b_a |