def process(self): freq = SDL_GetPerformanceFrequency() text = "FPS: " + str(freq) textSurface = TTF_RenderText_Shaded(self.font, text, SDL_Color(255, 255, 255), SDL_Color(40, 90, 120)) SDL_BlitSurface(textSurface, None, self.renderer, None)
def __init__(self, renderer, font=None, text="", font_size=16, text_color=SDL_Color(255, 255, 255), background_color=SDL_Color(0, 0, 0)): self.renderer = renderer if font is None: font = FONTS.get_path("04B_20__.TTF") else: font = FONTS.get_path(font) self.font = TTF_OpenFont(font.encode("UTF-8"), font_size) if self.font is None: raise TTF_GetError() self._text = text self.font_size = font_size self.text_color = text_color self.background_color = background_color texture = self._create_texture() super(TextSprite, self).__init__(texture)
def __init__(self, renderer, font=None, text="", font_size=16, text_color=SDL_Color(255, 255, 255), background_color=SDL_Color(0, 0, 0)): self.renderer = renderer if font is None: font = FONTS.get_path("04B_20__.TTF") else: font = FONTS.get_path(font) self.font_manager = FontManager(font, bg_color=background_color) self._text = text self.font_size = font_size self.text_color = text_color self.background_color = background_color surface = self._create_surface() super(SoftSprite, self).__init__(surface, True)
def run(): # You know those from the helloworld.py example. # Initialize the video subsystem, create a window and make it visible. sdl2ext.init() TTF_Init() window = sdl2ext.Window("2D drawing primitives", size=(800, 600)) window.show() # As in colorpalettes.py, explicitly acquire the window's surface to # draw on. windowsurface = window.get_surface() fps = FPS(windowsurface) # We implement the functionality as it was done in colorpalettes.py and # utilise a mapping table to look up the function to be executed, together # with the arguments they should receive functions = ((draw_lines, (windowsurface, 800, 600)), (draw_rects, (windowsurface, 800, 600))) # A storage variable for the function we are currently on, so that we know # which function to execute next. curindex = 0 draw_lines(windowsurface, 800, 600) textSurface = TTF_RenderText_Shaded(font, repr(SDL_GetPerformanceFrequency()), SDL_Color(255, 255, 255), SDL_Color(40, 90, 120)) # The event loop is nearly the same as we used in colorpalettes.py. If you # do not know, what happens here, take a look at colorpalettes.py for a # detailled description. running = True while running: events = sdl2ext.get_events() for event in events: if event.type == SDL_QUIT: running = False break if event.type == SDL_MOUSEBUTTONDOWN: curindex += 1 if curindex >= len(functions): curindex = 0 # In contrast to colorpalettes.py, our mapping table consists # of functions and their arguments. Thus, we get the currently # requested function and argument tuple and execute the # function with the arguments. func, args = functions[curindex] func(*args) break textSurface = TTF_RenderText_Shaded( font, "FPS: " + str(SDL_GetPerformanceFrequency()), SDL_Color(255, 255, 255), SDL_Color(40, 90, 120)) SDL_BlitSurface(textSurface, None, windowsurface, None) window.refresh() TTF_Quit() sdl2ext.quit() return 0
class Colors: """ Map colors""" WHITE = SDL_Color(255, 255, 255) BLACK = SDL_Color(0, 0, 0) RED = SDL_Color(255, 0, 0) GREEN = SDL_Color(0, 255, 0) BLUE = SDL_Color(0, 0, 255)
def _background(self): with _freetype_lock: return ttf_call( TTF_RenderUTF8_Blended, self.font.load(), self.txt.encode('utf-8'), SDL_Color(*self.color), _check_error=lambda rv: not rv )
def test_TTF_Render_Shaded(self): font = sdlttf.TTF_OpenFont(fontfile, 20) color = SDL_Color(0, 0, 0) bgcolor = SDL_Color(255, 255, 255) # Test TTF_RenderText_Shaded sf = sdlttf.TTF_RenderText_Shaded(font, b"Hi there!", color, bgcolor) assert isinstance(sf.contents, surface.SDL_Surface) # Test TTF_RenderUTF8_Shaded teststr = u"Hï thère!".encode('utf-8') sf = sdlttf.TTF_RenderUTF8_Shaded(font, teststr, color, bgcolor) assert isinstance(sf.contents, surface.SDL_Surface) # Test TTF_RenderUNICODE_Shaded # NOTE: no unicode chars because number -> glyph lookup is os-dependent teststr = u"Hi there!" strlen = len(teststr) + 1 # +1 for byte-order mark intstr = unpack('H' * strlen, teststr.encode('utf-16')) strarr = (c_uint16 * strlen)(*intstr) sf = sdlttf.TTF_RenderUNICODE_Shaded(font, strarr, color, bgcolor) assert isinstance(sf.contents, surface.SDL_Surface) # Test TTF_RenderGlyph_Solid sf = sdlttf.TTF_RenderGlyph_Shaded(font, ord("A"), color, bgcolor) assert isinstance(sf.contents, surface.SDL_Surface) sdlttf.TTF_CloseFont(font)
def render(self, text, style="default", align="left", max_width=None): """Renders a string of text to a surface that can then be presented on the screen using :func:`~klibs.KLGraphics.blit`. Args: text (str or numeric): The string or number to be rendered. style (str, optional): The label of the text style with which the font should be rendered. Defaults to the "default" text style if none is specified. align (str, optional): The text justification to use when rendering multi-line text. Can be 'left', 'right', or 'center' (defaults to 'left'). max_width (int, optional): The maximum line width for the rendered text. Lines longer than this value will be wrapped automatically. Defaults to None. Returns: :obj:`~klibs.KLGraphics.KLNumpySurface.NumpySurface`: a NumpySurface object containing the rendered text. """ stl = style if isinstance(style, TextStyle) else self.styles[style] if not isinstance(text, bytes): text = utf8(text).encode('utf-8') rendering_font = stl.font if max_width != None: w, h = ctypes.c_int(0), ctypes.c_int(0) TTF_SizeUTF8(rendering_font, text, ctypes.byref(w), ctypes.byref(h)) needs_wrap = w.value > max_width else: needs_wrap = False if len(text.split(b"\n")) > 1 or needs_wrap: if align not in ["left", "center", "right"]: raise ValueError( "Text alignment must be one of 'left', 'center', or 'right'." ) return self.__wrap__(text, style, rendering_font, align, max_width) if len(text) == 0: text = " " bgra_color = SDL_Color(stl.color[2], stl.color[1], stl.color[0], stl.color[3]) rendered_text = TTF_RenderUTF8_Blended(rendering_font, text, bgra_color).contents surface_array = self.__SDLSurface_to_ndarray(rendered_text) surface = NpS(surface_array) return surface
DEBUG = True GAME_TITLE = b"Puzzle" SCREEN_WIDTH = 1000 SCREEN_HEIGHT = 900 SCREEN_CENTER = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) SCREEN_FPS = 60 SCREEN_TICKS_PER_FRAME = 1000 // SCREEN_FPS CELL_SIZE = 23 BOARD_SIZE = 8 BOARD_POSITION = (SCREEN_CENTER[0] - BOARD_SIZE * CELL_SIZE // 2, SCREEN_CENTER[1] - BOARD_SIZE * CELL_SIZE // 2) PIECES_COUNT = BOARD_SIZE * BOARD_SIZE // 4 WHITE_COLOR = SDL_Color(255, 255, 255, 0) BLACK_COLOR = SDL_Color(0, 0, 0, 0) RED_COLOR = SDL_Color(255, 0, 0, 0) BLUE_COLOR = SDL_Color(0, 255, 0, 0) GREEN_COLOR = SDL_Color(0, 0, 255, 0) YELLOW_COLOR = SDL_Color(255, 255, 0, 0) COLORS = (BLACK_COLOR, WHITE_COLOR, BLUE_COLOR, RED_COLOR, GREEN_COLOR, YELLOW_COLOR) I1 = ( (1, 0, 0, 0), (1, 0, 0, 0), (1, 0, 0, 0), (1, 0, 0, 0), )
def init(): global NUM_UPCOMING_SONGS global UPDATE_TIME global CURR_SONG_NAME_FONT global CURR_SONG_GENRE_FONT global NEXT_SONG_GENRE_FONT global CURR_SONG_NAME_SIZE global CURR_SONG_GENRE_SIZE global NEXT_SONG_GENRE_SIZE global COLOR_THEME global CURR_SONG_GENRE_COLOR global CURR_SONG_NAME_COLOR global NEXT_SONG_GENRE_COLOR global BACKGROUND_COLOR global NEW_SONG_BANNER_ICON global DANCE_IMAGE_PATH global BORDER_PADDING_HORIZONTAL global BORDER_PADDING_VERTICAL global CROSSFADE_DURATION global CROSSFADE_TYPE global ENABLE_CROSSFADE NUM_UPCOMING_SONGS = 4 UPDATE_TIME = 0.75 # Available fonts are Noto<Serif/Sans>-<Regular/Italic/Bold>.ttf and Glametrix.otf CURR_SONG_NAME_FONT = os.path.join(os.getcwd(), "font", "Glametrix.otf") CURR_SONG_GENRE_FONT = os.path.join(os.getcwd(), "font", "NotoSerif-Bold.ttf") NEXT_SONG_GENRE_FONT = os.path.join(os.getcwd(), "font", "NotoSerif-Bold.ttf") CURR_SONG_NAME_SIZE = 3.0 / 32.0 CURR_SONG_GENRE_SIZE = 13.0 / 128.0 NEXT_SONG_GENRE_SIZE = 13.0 / 128.0 COLOR_THEME = 'DARK' if COLOR_THEME == 'LIGHT': CURR_SONG_GENRE_COLOR = SDL_Color(255, 75, 75) CURR_SONG_NAME_COLOR = SDL_Color(175, 175, 175) NEXT_SONG_GENRE_COLOR = SDL_Color(15, 15, 15) BACKGROUND_COLOR = SDL_Color(255, 255, 255) elif COLOR_THEME == 'DARK': CURR_SONG_GENRE_COLOR = SDL_Color(75, 255, 75) CURR_SONG_NAME_COLOR = SDL_Color(165, 165, 165) NEXT_SONG_GENRE_COLOR = SDL_Color(215, 215, 215) BACKGROUND_COLOR = SDL_Color(0, 0, 0) NEW_SONG_BANNER_ICON = os.path.join(os.getcwd(), "icons", "new-banner.png") DANCE_IMAGE_PATH = os.path.join(os.getenv("HOME"), "DanceHUD Settings", "image") BORDER_PADDING_HORIZONTAL = 1.0 / 16.0 BORDER_PADDING_VERTICAL = 1.0 / 32.0 # Types of crossfades - 'OutIn', 'Cross', 'Sine' CROSSFADE_TYPE = 'Cross' CROSSFADE_DURATION = 0.7 ENABLE_CROSSFADE = True