1#file: g.py
2#Copyright (C) 2005,2006,2007,2008 Evil Mr Henry, Phil Bordelon, Brian Reid,
3#                        and FunnyMan3595
4#This file is part of Endgame: Singularity.
5
6#Endgame: Singularity is free software; you can redistribute it and/or modify
7#it under the terms of the GNU General Public License as published by
8#the Free Software Foundation; either version 2 of the License, or
9#(at your option) any later version.
10
11#Endgame: Singularity is distributed in the hope that it will be useful,
12#but WITHOUT ANY WARRANTY; without even the implied warranty of
13#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#GNU General Public License for more details.
15
16#You should have received a copy of the GNU General Public License
17#along with Endgame: Singularity; if not, write to the Free Software
18#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20#This file contains all global objects.
21
22from __future__ import absolute_import
23
24import pygame
25from singularity.code.pycompat import *
26
27# User desktop size. Set at init_graphics_system()
28desktop_size = ()
29
30# Margin used in windowed mode to compensate for panels and title bar
31# Should be sensibly large enough to account for all common OS layouts, like
32# Ubuntu/Unity side launcher (65px), Gnome panels (24+24px), Windows bottom
33# panel (48px), Mac OSX, KDE, etc.
34# It will not affect windows smaller than desktop size
35desktop_margin = (70, 70)
36
37#initial screen size. Can be set via command-line option or preferences file
38default_screen_size = (1024, 768)
39
40#Current screen size. This size is an abstraction, tightly tied to resolutions.
41#Real window size in windowed mode may be smaller due to desktop_margin
42screen_size = default_screen_size
43
44#Current real window size. Will be the same as screen_size if fullscreen or
45#if abstracted screen_size is a custom resolution
46real_screen_size = screen_size
47
48min_resolution = (100, 100)
49
50# Available resolutions
51resolutions = [
52    ( 800, 600),
53    (1024, 600),
54    (1024, 768),
55    (1280,1024),
56
57    (1280, 800),
58    (1366, 768),
59    (1440, 900),
60    (1920,1080),
61]
62
63
64# Configurable by the user
65configured_text_sizes = {
66    'default': 20,
67    'button': 36,
68    'suspicion_bar': 32,
69    'time_display': 24,
70    'resource_display': 24,
71    'report_content': 20,
72}
73
74fullscreen = False
75
76#colors:
77colors = {}
78
79# Cache font dictionary.
80fonts = {}
81
82# Cache image dictionary.
83images = {}
84
85# Used to initialize surfaces that should have transparency.
86# Why the SRCALPHA parameter isn't working, I have no idea.
87ALPHA = None
88
89# Related to ALPHA, used by widget.Widget class
90fade_mask = None
91
92# Global FPS, used where continuous behavior is undesirable or a CPU hog.
93FPS = 30
94
95# OLPC ebook mode.
96ebook_mode = False
97
98screen_surface = None
99
100
101def init_graphics_system():
102
103    global desktop_size
104    width, height = (pygame.display.Info().current_w,
105                     pygame.display.Info().current_h)
106
107    if width > 0 and height > 0:
108        desktop_size = (width, height)
109
110    # (Re-)calculate real screen size
111    set_screen_size()
112
113    # Initialize the screen
114    set_mode()
115
116    # Initialize the cache of the current theme.
117    from singularity.code.graphics import theme
118    theme.current.init_cache()
119
120    init_alpha()
121
122    # Set the application icon and caption
123    pygame.display.set_icon(images["icon"])
124    pygame.display.set_caption("Endgame: Singularity")
125
126
127def set_fullscreen(value):
128    set_screen_size(fs=value)
129
130
131def get_screen_size_list():
132    res_list = set(resolutions + pygame.display.list_modes())
133
134    # Remove resolution inferior to minimal size since we will not allows them.
135    if min_resolution:
136        res_list = filter(lambda res: res[0] >= min_resolution[0] and res[1] >= min_resolution[1], res_list)
137
138    # Remove resolution superior to desktop size since we will not allows them.
139    if desktop_size:
140        res_list = filter(lambda res: res[0] <= desktop_size[0] and res[1] <= desktop_size[1], res_list)
141
142    return sorted(res_list)
143
144# TODO: Allows Fullscreen resolution could be higher than desktop_size when possible.
145def set_screen_size(size=None, fs=None):
146    """ Calculates proper real and abstract screen sizes
147        based on current and given screen size, fullscreen and desktop size
148    """
149    global screen_size, real_screen_size, fullscreen
150
151    # default values for size and fullscreen are current values
152    if size is None: size = screen_size
153    if fs   is None: fs   = fullscreen
154
155    # sets the new values
156    screen_size = size
157    fullscreen = fs
158
159    # Limit screen to minimal value
160    if min_resolution:
161        screen_size = (min_resolution[0] if screen_size[0] < min_resolution[0] else screen_size[0],
162                       min_resolution[1] if screen_size[1] < min_resolution[1] else screen_size[1])
163
164    # Limit the screen size to desktop size
165    if desktop_size and (screen_size[0] > desktop_size[0] or
166                         screen_size[1] > desktop_size[1]):
167        screen_size = desktop_size
168
169    # Default real size is the same as abstract screen size
170    real_screen_size = screen_size
171
172    # Apply margin in windowed mode.
173    # Only if desired screen size is not a custom resolution and its
174    # width or height matches the (known) desktop size
175    if not fullscreen and desktop_size and screen_size in resolutions:
176        # margin is applied independently for width and height
177        width, height = screen_size
178        if width  == desktop_size[0]: width  -= desktop_margin[0]
179        if height == desktop_size[1]: height -= desktop_margin[1]
180        real_screen_size = (width, height)
181
182
183def set_mode():
184    """Wrapper for pygame.display.set_mode()"""
185    global screen_surface
186
187    if fullscreen:
188        flags = pygame.FULLSCREEN
189    else:
190        flags = pygame.RESIZABLE
191
192    screen_surface = pygame.display.set_mode(real_screen_size, flags)
193
194
195def load_font(filename):
196    from singularity.code.graphics.font import FontList
197    return FontList(filename)
198
199
200def load_image(filename):
201    # We need to convert the image to a Pygame image surface and
202    # set the proper color key for the game.
203    image = pygame.image.load(filename).convert()
204    image.set_colorkey((255, 0, 255, 255), pygame.RLEACCEL)
205    return image.convert_alpha()
206
207def init_alpha():
208    global ALPHA
209    ALPHA = pygame.Surface((0,0)).convert_alpha()
210
211
212def resolve_image_alias(image):
213    if isinstance(image, basestring):
214        return resolve_color_alias(images[image])
215    else:
216        return image
217
218
219def resolve_color_alias(color):
220    if isinstance(color, basestring):
221        return resolve_color_alias(colors[color])
222    else:
223        return color
224
225
226def resolve_font_alias(font):
227    if isinstance(font, basestring):
228        return resolve_color_alias(fonts[font])
229    else:
230        return font
231
232
233def resolve_text_size(text_size):
234    if isinstance(text_size, basestring):
235        return resolve_text_size(configured_text_sizes[text_size])
236    else:
237        return text_size
238