def RenderDebug(self, world, render_dt): ''' This probably doesn't belong here... Should move into a subsystem elsewhere. This is an easy light way of having a quick debugger that can print the hero's stats. Move this elsewhere later on. Tutorial: https://egdev.wordpress.com/2014/03/14/python-sdl2-ttf-test/ ''' # Open font font = sdl2.sdlttf.TTF_OpenFont( '/home/prestonh/Desktop/Programming/gamedev/shoot/shoot/resources/fonts/computer_modern/cmunsx.ttf', 12) color = sdl2.SDL_Color(40, 255, 40) # Create debug text entity = world.entity_manager.entities['hero_0'] debug_lines = [] debug_lines.append('fps:' + str(round(1. / render_dt))) debug_lines.append('(x,y):' + str(round(entity.kinematics.x, 3)) + ', ' + str(round(entity.kinematics.y, 3))) debug_lines.append('(vx,vy):' + str(round(entity.kinematics.vx, 3)) + ', ' + str(round(entity.kinematics.vy, 3))) debug_text = '' for debug_line in debug_lines: debug_text += debug_line + '\n' # Render the debug text # Create surface and texture debug_width = 200 text_surface = sdl2.sdlttf.TTF_RenderText_Blended_Wrapped( font, debug_text, color, debug_width) text_texture = sdl2.SDL_CreateTextureFromSurface( self.sdl_renderer, text_surface) # Render surface and texture window_rect = self.GetWindowRect() w = ctypes.pointer(ctypes.c_int(0)) h = ctypes.pointer(ctypes.c_int(0)) sdl2.SDL_QueryTexture(text_texture, None, None, w, h) x = window_rect.w - debug_width y = 50 #window_rect.h - h.contents.value debug_rect = sdl2.SDL_Rect(x, y, w.contents.value, h.contents.value) sdl2.SDL_RenderCopy(self.sdl_renderer, text_texture, None, debug_rect) # Free resources sdl2.SDL_FreeSurface(text_surface) sdl2.sdlttf.TTF_CloseFont(font) pass
def __init__(self, renderer: sdl2.render.SDL_Renderer, container: "entities.Entity", filename: str): # Container of this component self.container = container # Never explicitly deallocated, but reclaimed by the operating system # when the game exits. self.texture = texture_from_bmp(renderer, filename) # Dynamically determine width and height of sprite over using constants. w = ctypes.pointer(ctypes.c_int(0)) h = ctypes.pointer(ctypes.c_int(0)) sdl(sdl2.SDL_QueryTexture(self.texture, None, None, w, h)) self.width = float(w.contents.value) self.height = float(h.contents.value)
def renderTexture(tex, ren, x, y): """ :type ren: SDL_Renderer :type tex: SDL_Texture """ #Setup the destination rectangle to be at the position we want dst = sdl2.SDL_Rect(x, y) w = ctypes.c_int() h = ctypes.c_int() #Query the texture to get its width and height to use sdl2.SDL_QueryTexture(tex, None, None, w, h) dst.w = w.value dst.h = h.value sdl2.SDL_RenderCopy(ren.sdlrenderer, tex, None, dst)
def draw(self, renderer, font, x, y, color=Color.white): global _reuse_rect tex = self.texture_for_renderer(renderer, font) w = ctypes.c_int32() h = ctypes.c_int32() w.value = 0 h.value = 0 sdl.SDL_QueryTexture(tex, None, None, ctypes.byref(w), ctypes.byref(h)) _reuse_rect.x = x _reuse_rect.y = y _reuse_rect.w = w.value _reuse_rect.h = h.value sdl.SDL_SetTextureColorMod(tex, color.red, color.green, color.blue) sdl.SDL_RenderCopy(renderer, tex, None, _reuse_rect)
def render(self, renderer): i = 0 for game in self.currentState.roms: currentColor = WHITE if self.currentState.getCurrentRom() == game: currentColor = RED surf = sdl2.sdlttf.TTF_RenderText_Blended( self.font, game.name.encode("utf-8"), currentColor).contents text = sdl2.SDL_CreateTextureFromSurface(renderer, surf) sdl2.SDL_SetTextureBlendMode(text, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_FreeSurface(surf) w, h = c_int(), c_int() sdl2.SDL_QueryTexture(text, None, None, byref(w), byref(h)) sdl2.SDL_RenderCopy(renderer, text, None, sdl2.SDL_Rect(50, 50 + (i * 40), w, h)) i += 1
def render(self, renderer): if self.currentSystem != self.currentState.getCurrentSystem().name: image = sdl2.sdlimage.IMG_Load( b"images/backdrops/" + self.currentState.getCurrentSystem().name.encode('utf-8') + b".jpg") self.text = sdl2.SDL_CreateTextureFromSurface(renderer, image) sdl2.SDL_FreeSurface(image) sdl2.SDL_SetTextureBlendMode(self.text, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureAlphaMod(self.text, c_uint8(48)) sdl2.SDL_QueryTexture(self.text, None, None, byref(self.imageWidth), byref(self.imageHeight)) self.currentSystem = self.currentState.getCurrentSystem().name percent = 720 / self.imageHeight.value sdl2.SDL_RenderCopy( renderer, self.text, None, sdl2.SDL_Rect(0, 0, int(self.imageWidth.value * percent), int(self.imageHeight.value * percent)))
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 text_dimensions(self, texture): width = pointer(c_int(0)) height = pointer(c_int(0)) sdl2.SDL_QueryTexture(texture, None, None, width, height) return width.contents.value, height.contents.value