def render_text(self, renderer, screen_width): # Meters width, height = self.text_dimensions(self.meters_texture) sdl2.SDL_RenderCopyEx( renderer, self.meters_texture, None, sdl2.SDL_Rect(InfoText.offset, InfoText.offset, width, height), 0.0, None, sdl2.SDL_FLIP_NONE) # Time width, height = self.text_dimensions(self.time_texture) sdl2.SDL_RenderCopyEx( renderer, self.time_texture, None, sdl2.SDL_Rect(screen_width - width - InfoText.offset, InfoText.offset, width, height), 0.0, None, sdl2.SDL_FLIP_NONE)
def RenderRoom(self, world): camera = world.entity_manager.entities['camera_0'] camera_rect = sdl2.SDL_Rect(int(camera.kinematics.x),\ int(camera.kinematics.y),\ camera.shape.w,\ camera.shape.h) window_rect = self.GetWindowRect() if world.room.display.texture == None: world.room.display.texture = self.GetTexture( world.room.display.filepath) x_stretch = 1. * window_rect.w / camera_rect.w y_stretch = 1. * window_rect.h / camera_rect.h x_offset = int( (world.room.shape.w - world.room.display.source_rect.w) / 2.) y_offset = int( (world.room.shape.h - world.room.display.source_rect.h) / 2.) destination_x = int((x_offset - camera_rect.x) * x_stretch) destination_y = int((y_offset - camera_rect.y) * y_stretch) destination_w = int(world.room.display.source_rect.w * x_stretch) destination_h = int(world.room.display.source_rect.h * y_stretch) destination_rect = sdl2.SDL_Rect(destination_x, destination_y, destination_w, destination_h) flip = sdl2.SDL_RendererFlip(sdl2.SDL_FLIP_NONE) sdl2.SDL_RenderCopyEx(self.sdl_renderer, world.room.display.texture, world.room.display.source_rect, destination_rect, 0, None, flip)
def RenderEntity(self, entity, camera_rect, window_rect): # Load texture if not already loaded; set texture so don't have to # load it again if entity.display.texture == None: entity.display.texture = self.GetTexture(entity.display.filepath) x_stretch = 1. * window_rect.w / camera_rect.w y_stretch = 1. * window_rect.h / camera_rect.h x_offset = int((entity.shape.w - entity.display.source_rect.w) / 2.) y_offset = int((entity.shape.h - entity.display.source_rect.h) / 2.) destination_x = int( (entity.kinematics.x + x_offset - camera_rect.x) * x_stretch) destination_y = int( (entity.kinematics.y + y_offset - camera_rect.y) * y_stretch) destination_w = int(entity.display.source_rect.w * x_stretch) destination_h = int(entity.display.source_rect.h * y_stretch) destination_rect = sdl2.SDL_Rect(destination_x, destination_y, destination_w, destination_h) flip = sdl2.SDL_RendererFlip(sdl2.SDL_FLIP_NONE) if entity.orientation != None: if entity.orientation.xorientation == 'right': flip = sdl2.SDL_FLIP_NONE elif entity.orientation.xorientation == 'left': flip = sdl2.SDL_RendererFlip(sdl2.SDL_FLIP_HORIZONTAL) sdl2.SDL_RenderCopyEx(self.sdl_renderer, entity.display.texture, entity.display.source_rect, destination_rect, 0, None, flip)
def render(self, renderer, camera_x, camera_y): if self.angle != 0 and self.angle != 180: self.swap_dimensions(True) else: self.swap_dimensions(False) sdl2.SDL_RenderCopyEx( renderer, self.texture, None, sdl2.SDL_Rect(int(self.x - camera_x), int(self.y - camera_y), int(self.width), int(self.height)), 0, None, sdl2.SDL_FLIP_NONE)
def render(self, renderer, camera_x, camera_y): if self.vehicle == None: if self.wearing_mask: sdl2.SDL_RenderCopyEx( renderer, self.texture, sdl2.SDL_Rect(Player.default_width, 0, Player.default_width, Player.render_height), sdl2.SDL_Rect(int(self.x - camera_x), int(self.y - camera_y - self.height), int(self.width), int(Player.render_height)), 0, None, sdl2.SDL_FLIP_NONE) else: sdl2.SDL_RenderCopyEx( renderer, self.texture, sdl2.SDL_Rect(0, 0, Player.default_width, Player.render_height), sdl2.SDL_Rect(int(self.x - camera_x), int(self.y - camera_y - self.height), int(self.width), int(Player.render_height)), 0, None, sdl2.SDL_FLIP_NONE)
def draw_bg(): for block_y in range(VP_HEIGHT_BLOCKS): for block_x in range(VP_WIDTH_BLOCKS): block_index = block_y * VP_WIDTH_BLOCKS + block_x tile = vmp['bgTiles'][block_index] flag = sdl2.SDL_FLIP_HORIZONTAL if tile['flipped'] else sdl2.SDL_FLIP_NONE tileset_block_x = tile['vcn_block'] % 32 tileset_block_y = tile['vcn_block'] // 32 src_rect = sdl2.SDL_Rect(tileset_block_x * 8, tileset_block_y * 8, 8, 8) dst_rect = sdl2.SDL_Rect(block_x * 8, block_y * 8, 8, 8) sdl2.SDL_RenderCopyEx(renderer, bg_texture, src_rect, dst_rect, c_double(0.0), None, flag)
def draw(self, spriterenderer): """Creates the sprite on the given renderer""" cornerx, cornery = self.get_origin() r = sdl2.SDL_Rect(cornerx, cornery, self.sprite.size[0], self.sprite.size[1]) p = self.sprite.point sdl2.SDL_RenderCopyEx( spriterenderer.sdlrenderer, self.sprite.texture, None, r, self.sprite.angle, p, 0, )
def draw_rect(surface, x, y, w, h, angle=0, colour=(0, 0, 0, 0)): if len(colour) == 3: colour = (colour[0] % 256, colour[1] % 256, colour[2] % 256, 255) elif len(colour) == 4: colour = (colour[0] % 256, colour[1] % 256, colour[2] % 256, colour[3] % 256) surf = Surface(w, h) surf.fill((colour[0], colour[1], colour[2], colour[3])) text = sdl2.SDL_CreateTextureFromSurface(surface.renderer, surf.surface) dstrect = sdl2.SDL_Rect(x, y, w, h) sdl2.SDL_RenderCopyEx(surface.renderer, text, None, dstrect, -angle, sdl2.SDL_Point(0, 0), sdl2.SDL_FLIP_NONE)
def blit(self, surface, src=None, dest=None, angle=0): if src: srcrect = sdl2.SDL_Rect(src[0][0], src[0][1], src[1][0] - src[0][0], src[1][1] - src[0][1]) else: srcrect = None if dest: if len(dest) == 2: if src: dstrect = sdl2.SDL_Rect(dest[0] - srcrect.w / 2, dest[1] - srcrect.h / 2, srcrect.w, srcrect.h) else: dstrect = sdl2.SDL_Rect(dest[0] - surface.w / 2, dest[1] - surface.h / 2, surface.w, surface.h) elif len(dest) == 4: dstrect = sdl2.SDL_Rect() dstrect.w = dest[1][0] - dest[0][0] dstrect.h = dest[1][1] - dest[0][1] dstrect.x = dest[0][0] - dstrect.w / 2 dstrect.y = dest[0][1] - dstrect.h / 2 else: raise IndexError("\'dest\' should have 2 or 4 values. ") else: if src: dstrect = sdl2.SDL_Rect(dest[0] - srcrect.w / 2, dest[1] - srcrect.h / 2, srcrect.w, srcrect.h) else: dstrect = sdl2.SDL_Rect(0 - surface.w / 2, 0 - surface.h / 2, surface.w, surface.h) text = sdl2.SDL_CreateTextureFromSurface(self.renderer, surface.surface) sdl2.SDL_RenderCopyEx( self.renderer, text, srcrect, dstrect, -surface.angle - angle, sdl2.SDL_Point(round(surface.w / 2), round(surface.h / 2)), sdl2.SDL_FLIP_NONE)
def draw_wall(wall_mapping_index, wall_position): if wall_mapping_index == 1 or wall_mapping_index == 2: wall_type = wall_mapping_index - 1 else: wall_type = wall_mapping_index - 20 cfg = walls_render_config[wall_position] tiles_offset = cfg.base_offset tileset = vmp['wallTiles'] for t in range(cfg.blk_height): for s in range(cfg.blk_width): if cfg.flip: viewport_block_index = cfg.view_offset + cfg.blk_width - (s + 1) + t * VP_WIDTH_BLOCKS else: viewport_block_index = s + t * VP_WIDTH_BLOCKS + cfg.view_offset viewport_block_x = viewport_block_index % VP_WIDTH_BLOCKS viewport_block_y = viewport_block_index // VP_WIDTH_BLOCKS tile = tileset[tiles_offset + 431*wall_type] flip = tile['flipped'] != cfg.flip # xor flag = sdl2.SDL_FLIP_HORIZONTAL if flip else sdl2.SDL_FLIP_NONE vcn_block = tile['vcn_block'] tileset_block_x = vcn_block % 32 tileset_block_y = vcn_block // 32 src_rect = sdl2.SDL_Rect(tileset_block_x * 8, tileset_block_y * 8, 8, 8) dst_rect = sdl2.SDL_Rect(viewport_block_x * 8, viewport_block_y * 8, 8, 8) sdl2.SDL_RenderCopyEx(renderer, walls_texture, src_rect, dst_rect, c_double(0.0), None, flag) tiles_offset += 1 tiles_offset += cfg.skip
def render(self, context: GameContext, position: Vector2D, size: Size, camera_position: Vector2D, camera_size: Size) -> None: render_position = position \ + Vector2D(-size.width / 2, -size.height / 2) \ - camera_position \ - Vector2D(-camera_size.width / 2, -camera_size.height / 2) rect = sdl2.SDL_Rect() rect.x = round(context.settings.windowWidth * (render_position.x / camera_size.width)) rect.y = round(context.settings.windowHeight * (render_position.y / camera_size.height)) rect.w = round(context.settings.windowWidth * (size.width / camera_size.width)) rect.h = round(context.settings.windowHeight * (size.height / camera_size.height)) render_frame = None if not self.fullRender: render_frame = self.renderFrameSize sdl2.SDL_RenderCopyEx(context.renderer, self.texture, render_frame, rect, 0, None, self.renderFlip)
def draw_texture(renderer: sdl2.render.SDL_Renderer, texture: sdl2.render.SDL_Texture, position: Vec2f, rotation: float) -> None: w = ctypes.pointer(ctypes.c_int(0)) h = ctypes.pointer(ctypes.c_int(0)) sdl(sdl2.SDL_QueryTexture(texture, None, None, w, h)) width = float(w.contents.value) height = float(h.contents.value) # Transforms coordinates to center of sprite rather than default upper left # corner. This makes centering the sprite on screen easier. x = position.x - width / 2 y = position.y - height / 2 sdl( sdl2.SDL_RenderCopyEx( renderer, texture, ctypes.byref(sdl2.SDL_Rect(0, 0, int(width), int(height))), ctypes.byref(sdl2.SDL_Rect(int(x), int(y), int(width), int(height))), rotation, ctypes.byref(sdl2.SDL_Point(int(width / 2), int(height / 2))), sdl2.SDL_FLIP_NONE))
def render(self, renderer, font, screen_height): self.remove_expired_messages() row = 1 for message in self.messages: # Create font surface text_surface = sdl2.sdlttf.TTF_RenderText_Solid( font, str.encode(message.text), self.text_color) # Create texture from surface text_texture = sdl2.SDL_CreateTextureFromSurface( renderer, text_surface) # Free surface sdl2.SDL_FreeSurface(text_surface) # Query dimensions width, height = self.text_dimensions(text_texture) # Render to window sdl2.SDL_RenderCopyEx( renderer, text_texture, None, sdl2.SDL_Rect(MessageStack.x_offset, screen_height - MessageStack.spacing * row, width, height), 0.0, None, sdl2.SDL_FLIP_NONE) # Free texture sdl2.SDL_DestroyTexture(text_texture) row += 1
def render_text(self, renderer, font, screen_width, text, y_position): if text in self.texture_cache: text_texture = self.texture_cache.get(text) else: # Create font surface text_surface = sdl2.sdlttf.TTF_RenderText_Solid( font, str.encode(text), self.text_color) # Create texture from surface text_texture = sdl2.SDL_CreateTextureFromSurface( renderer, text_surface) # Free surface sdl2.SDL_FreeSurface(text_surface) # Add to cache for future use self.texture_cache[text] = text_texture # Query dimensions width, height = self.text_dimensions(text_texture) # Center text text_x = self.center_text(screen_width, width) # Render to window sdl2.SDL_RenderCopyEx(renderer, text_texture, None, sdl2.SDL_Rect(text_x, y_position, width, height), 0.0, None, sdl2.SDL_FLIP_NONE)
def render(self, renderer, camera_x, camera_y): sdl2.SDL_RenderCopyEx(renderer, self.texture, None, sdl2.SDL_Rect(int(self.x - camera_x), int(self.y - camera_y - self.height), int(self.width), int(Civilian.render_height)), 0, None, sdl2.SDL_FLIP_NONE)