def ortho_perspective(self): if self.perspective == PERSPECTIVE_ORTHO: return self.display_aspect = 16 / 9.0 self.ortho_pscalex = ( 2.0 / self.display_width * (self.display_width / self.display_height) ) self.ortho_pscaley = 2.0 / self.display_height gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() left = self.offset_x - 1.0 * self.display_aspect / self.zoom right = self.offset_x + 1.0 * self.display_aspect / self.zoom top = self.offset_y + 1.0 / self.zoom bottom = self.offset_y - 1.0 / self.zoom gl.glOrtho(left, right, bottom, top, -1.0, 1.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() self.gl_left = -1.0 * self.display_aspect self.gl_right = 1.0 * self.display_aspect self.gl_height = 2.0 # * cls.display_aspect self.gl_width = 2.0 * self.display_aspect self.perspective = PERSPECTIVE_ORTHO return self.ortho_pscalex, self.ortho_pscaley
def standard_perspective(self): if self.perspective == PERSPECTIVE_STANDARD: return # aspect_ratio = cls.display_width / cls.display_height self.gl_height = 2.0 self.gl_width = 2.0 * self.display_aspect self.gl_left = -1.0 * self.display_aspect self.fov_y = 45 / self.zoom self.fov_x = self.fov_y * self.display_aspect / self.zoom # left = cls.offset_x - 1.0 * cls.display_aspect / cls.zoom # right = cls.offset_x + 1.0 * cls.display_aspect / cls.zoom # top = cls.offset_y + 1.0 / cls.zoom # bottom = cls.offset_y - 1.0 / cls.zoom gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # gluPerspective(cls.fov_y, cls.display_aspect, 0.1, 10.1) # glTranslatef(-cls.offset_x, -cls.offset_y, 0.0) gl.gluPerspective(45.0, 16.0 / 9.0, 0.1, 100.0) self.projection = gl.glGetFloatv(gl.GL_PROJECTION_MATRIX) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # glScissor(0, display_yoffset, cls.display_width, cls.display_height) # glEnable(GL_SCISSOR_TEST) self.perspective = PERSPECTIVE_STANDARD
def hd_perspective(self): if self.perspective == PERSPECTIVE_HD: return self.display_aspect = 16 / 9.0 self.ortho_pscalex = 1920 / self.display_width # cls.ortho_pscalex = 1920 / cls.display_width * \ # (cls.display_width / cls.display_height) self.ortho_pscaley = 1080 / self.display_height gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() left = 0.0 right = 1920.0 top = 1080.0 bottom = 0.0 gl.glOrtho(left, right, bottom, top, -1.0, 1.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() self.gl_left = left self.gl_right = right self.gl_height = 1080.0 self.gl_width = 1920.0 self.perspective = PERSPECTIVE_HD return self.ortho_pscalex, self.ortho_pscaley
def init_display(): global display global real_display_height # , display_yoffset # global banner_texture, shadow_texture, gloss_texture # global top_texture, top_logo_texture, logo_texture # global missing_cover_texture, default_item_texture # global backdrop_texture logger.debug("Init OpenGL menu display") DisplayLists.clear() # on_resize() # depth = 0 # FIXME: HACK / TESTING # if not Settings.fullscreen_menu: # if windows: # os.environ["SDL_VIDEO_WINDOW_POS"] = "3,29" # else: # os.environ["SDL_VIDEO_WINDOW_POS"] = "0,0" # maximize_window = (not Settings.fullscreen_menu and # Settings.windowed_size is None) # display_info = pygame.display.Info() # dw = display_info.current_w # dh = display_info.current_h # dw, dh = fsui.get_screen_size() # dw, dh = 100, 100 Render.get().display_width = main_window.width Render.get().display_height = main_window.height # if Settings.fullscreen_menu: # print("fullscreen is True") # if windows: # #resolution = (0, 0) # #flags = pygame.OPENGL | pygame.DOUBLEBUF | pygame.NOFRAME \ # # | pygame.FULLSCREEN # os.environ["SDL_VIDEO_WINDOW_POS"] = "0,0" # flags = pygame.OPENGL | pygame.DOUBLEBUF | pygame.NOFRAME # #flags = flags | pygame.FULLSCREEN # #if fs.linux: # # pass # #else: # if dw > dh * 2: # # Assume dual monitor setup - hack for Linux / SDL # resolution = (dw / 2, dh) # else: # resolution = (dw, dh) # else: # fullscreen, but not microsoft windows # #resolution = (0, 0) # flags = pygame.OPENGL | pygame.DOUBLEBUF | pygame.NOFRAME #| # pygame.FULLSCREEN # if dw > dh * 2: # # Assume dual monitor setup - hack for Linux / SDL # resolution = (dw / 2, dh) # else: # resolution = (dw, dh) # if linux: # overscan = Config.get("display/overscan", "0,0,0,0") # try: # overscan = overscan.split(",") # overscan = [int(x.strip()) for x in overscan] # print("using overscan", overscan) # except Exception as e: # print("error parsing overscan from config:", repr(e)) # overscan = [0, 0, 0, 0] # os.environ["SDL_VIDEO_WINDOW_POS"] = "{0},{1}".format( # overscan[0], overscan[1]) # resolution = (resolution[0] - overscan[0] - overscan[2], # resolution[1] - overscan[1] - overscan[3]) # elif macosx: # # FIXME: fullscreen mode does not work very well. -When # # opening a fullscreen emulator from fullscreen, the emulator # # crashes on glViewport. Tested with fs-amiga. # #flags |= pygame.FULLSCREEN # # # for now, we create an almost maximized window, works # # quite well when the dock is set to auto-hide # #resolution = (resolution[0], resolution[1] - 22) # # # FIXME: trying LSUIPresentationMode # os.environ["SDL_VIDEO_WINDOW_POS"] = "0,0" # # # kUIModeNormal = 0 # # kUIModeContentSuppressed = 1 # # kUIModeContentHidden = 2 # # kUIModeAllSuppressed = 4 # kUIModeAllHidden = 3 # kUIOptionAutoShowMenuBar = 1 << 0 # # kUIOptionDisableAppleMenu = 1 << 2 # # kUIOptionDisableProcessSwitch = 1 << 3 # # kUIOptionDisableForceQuit = 1 << 4 # # kUIOptionDisableSessionTerminate = 1 << 5 # # kUIOptionDisableHide = 1 << 6 # # #noinspection PyUnresolvedReferences # import objc # #noinspection PyUnresolvedReferences # from Foundation import NSBundle # bundle = NSBundle.bundleWithPath_( # "/System/Library/Frameworks/Carbon.framework") # objc.loadBundleFunctions( # bundle, globals(), # ((str("SetSystemUIMode"), str("III"), str("")),)) # #noinspection PyUnresolvedReferences # SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar) # # else: # if windows and maximize_window and \ # not Settings.window_decorations: # import ctypes # SPI_GETWORKAREA = 48 # # class RECT(ctypes.Structure): # _fields_ = [ # ("left", ctypes.c_ulong), # ("top", ctypes.c_ulong), # ("right", ctypes.c_ulong), # ("bottom", ctypes.c_ulong)] # # m = ctypes.windll.user32 # r = RECT() # m.SystemParametersInfoA(SPI_GETWORKAREA, 0, ctypes.byref(r), 0) # x = int(r.left) # y = int(r.top) # w = int(r.right) - int(r.left) # h = int(r.bottom) - int(r.top) # print(x, y, w, h) # WINDOWED_SIZE[0] = w # WINDOWED_SIZE[1] = h # os.environ["SDL_VIDEO_WINDOW_POS"] = "{0},{1}".format(x, y) # State.get().allow_minimize = False # # if Settings.windowed_size: # print("Settings.windowed_size", Settings.windowed_size) # WINDOWED_SIZE[0] = Settings.windowed_size[0] # WINDOWED_SIZE[1] = Settings.windowed_size[1] # Render.get().display_width = WINDOWED_SIZE[0] # Render.get().display_height = WINDOWED_SIZE[1] # #if dw > 1400: # # Render.get().display_width = 1280 # # Render.get().display_height = 720 # else: # Render.get().display_width = WINDOWED_SIZE[0] # Render.get().display_height = WINDOWED_SIZE[1] # resolution = (Render.get().display_width, Render.get().display_height) # #print(resolution) # #sys.exit(1) # if Settings.window_decorations: # flags = pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE # else: # flags = pygame.OPENGL | pygame.DOUBLEBUF | pygame.NOFRAME # # display_yoffset = 0 # Mouse.set_visible(False) # # pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 8) # pygame.display.gl_set_attribute(pygame.GL_DEPTH_SIZE, 16) # # Render.get().display_sync = ENABLE_VSYNC # if Render.get().display_sync: # print("enabling vertical sync") # os.environ["__GL_SYNC_TO_VBLANK"] = "1" # pygame.display.gl_set_attribute(pygame.GL_SWAP_CONTROL, 1) # else: # os.environ["__GL_SYNC_TO_VBLANK"] = "0" # pygame.display.gl_set_attribute(pygame.GL_SWAP_CONTROL, 0) # pygame.display.gl_set_attribute(pygame.GL_DOUBLEBUFFER, 1) # fsaa = Config.get_int("video/fsaa", 0) # if fsaa: # pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1) # pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, fsaa) # print("pygame set display mode", resolution, flags, depth) # display = pygame.display.set_mode(resolution, flags, depth) # if not Settings.fullscreen_game: # try: # del os.environ["SDL_VIDEO_WINDOW_POS"] # except KeyError: # pass # # if app.name == "fs-uae-arcade": # pygame.display.set_caption("FS-UAE Arcade") # else: # pygame.display.set_caption("FS Game Center") # # # FIXME: DISABLING MAXIMIZE FOR DEBUGGING # #maximize_window = False # if maximize_window: # print("maximizing window") # SDL_Maximize() # for event in pygame.event.get(): # if event.type == pygame.VIDEORESIZE: # #WINDOWED_SIZE[0] = event.w # #WINDOWED_SIZE[1] = event.h # on_resize((event.w, event.h)) # print("DISPLAY.GET_SIZE", display.get_size()) # else: # on_resize(display.get_size()) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glClearColor(*State.get().fade_from) fs_emu_texturing(True) Texture.splash = Texture("splash.png") for _ in range(0): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # gl.glClear(gl.GL_COLOR_BUFFER_BIT) Render.get().hd_perspective() Texture.splash.render( (1920 - Texture.splash.w) // 2, (1080 - Texture.splash.h) // 2, Texture.splash.w, Texture.splash.h) gl.glFinish() # pygame.display.flip() gl.glFinish() gl.glEnable(gl.GL_DEPTH_TEST)
def render(self, text, x, y, r=1.0, g=1.0, b=1.0, alpha=1.0): global text_cache if not text: return 0 # find cached text entry, if any for i, item in enumerate(text_cache): if item["text"] == text and item["font"] == self: text_cache.pop(i) break else: item = None if item: fs_emu_blending(True) fs_emu_texturing(True) w = item["w"] h = item["h"] gl.glBindTexture(gl.GL_TEXTURE_2D, item["texture"]) gl.glBegin(gl.GL_QUADS) gl.glColor4f(r * alpha, g * alpha, b * alpha, alpha) gl.glTexCoord2d(0.0, 0.0) gl.glVertex2d(x, y) gl.glTexCoord2d(1.0, 0.0) gl.glVertex2d(x + w, y) gl.glTexCoord2d(1.0, 1.0) gl.glVertex2d(x + w, y + h) gl.glTexCoord2d(0.0, 1.0) gl.glVertex2d(x, y + h) gl.glEnd() # re-insert item at front text_cache.insert(0, item) return w, h # calculate size of text required_width, required_height = self.measure(text) # setup fbo render_texture = gl.glGenTextures(1) gl.glBindTexture(gl.GL_TEXTURE_2D, render_texture) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, required_width, required_height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_INT, None, ) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE ) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR ) # FIXME: Mipmapping? mip_mapping = 0 if mip_mapping: gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR, ) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_GENERATE_MIPMAP, gl.GL_TRUE ) gl.glGenerateMipmapEXT(gl.GL_TEXTURE_2D) else: gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR ) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) # Set up some renderbuffer state frame_buffer = gl.GLuint() gl.glGenFramebuffersEXT(1, frame_buffer) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, frame_buffer) gl.glFramebufferTexture2DEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, render_texture, 0, ) status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT) if status != gl.GL_FRAMEBUFFER_COMPLETE_EXT: print("glCheckFramebufferStatusEXT error", status) gl.glPushMatrix() gl.glLoadIdentity() gl.glPushAttrib(int(gl.GL_VIEWPORT_BIT) | int(gl.GL_ENABLE_BIT)) gl.glViewport(0, 0, required_width, required_height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.gluOrtho2D(0, required_width, 0, required_height) gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glEnable(gl.GL_TEXTURE_2D) self.texture.bind() tw = self.texture.w th = self.texture.h gl.glBegin(gl.GL_QUADS) gl.glColor4f(1.0, 1.0, 1.0, 1.0) x2 = 0 h = self.h for ch in text: c = ord(ch) w = self.w[c] s1 = self.x[c] / tw s2 = (self.x[c] + w) / tw t1 = (self.y[c]) / th t2 = (self.y[c] + h) / th gl.glTexCoord2d(s1, t2) gl.glVertex2d(x2, 0) gl.glTexCoord2d(s2, t2) gl.glVertex2d(x2 + w, 0) gl.glTexCoord2d(s2, t1) gl.glVertex2d(x2 + w, h) gl.glTexCoord2d(s1, t1) gl.glVertex2d(x2, h) x2 += w gl.glEnd() gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPopAttrib() gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) gl.glPopMatrix() gl.glDeleteFramebuffersEXT(1, frame_buffer) if mip_mapping: gl.glBindTexture(gl.GL_TEXTURE_2D, render_texture) gl.glGenerateMipmapEXT(gl.GL_TEXTURE_2D) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) new_item = { "font": self, "text": text, "w": required_width, "h": required_height, "texture": render_texture, } text_cache.insert(0, new_item) item = text_cache.pop() if item["texture"]: gl.glDeleteTextures([item["texture"]]) # now the text is in the cache, so call function again return self.render(text, x, y, r, g, b, alpha)