Completed
Push — master ( 6cf062...43f138 )
by Matthew
01:00
created

ed2d.platforms.Window.__init__()   A

Complexity

Conditions 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 18
rs 9.4286
1
import sdl2 as sdl
2
3
def _init_video():
4
    ''' Prepare sdl for subsystem initialization, and init sdl video. '''
5
    sdl.SDL_Init(0)
6
    sdl.SDL_InitSubSystem(sdl.SDL_INIT_VIDEO)
7
8
# Init sdl video before finishing
9
_init_video()
10
11
WindowedMode = 1
12
FullscreenMode = 2
13
BorderlessMode = 3
14
15
fullscreenMap = {
16
    WindowedMode: 0,
17
    FullscreenMode: sdl.SDL_WINDOW_FULLSCREEN,
18
    BorderlessMode: sdl.SDL_WINDOW_FULLSCREEN_DESKTOP,
19
}
20
21
class Window(object):
22
    '''
23
    Creates and manages window related resources.
24
    This should also support the creation of multiple windows, if you handle
25
    the events correctly.
26
    '''
27
    def __init__(self, title, width, height, fullscreen):
28
        # TODO - implement fullscreen mode
29
30
        # Convert the title to bytes
31
        self.title = title.encode(encoding='UTF-8')
32
        self.width = width
33
        self.height = height
34
        self.fullscreen = fullscreen
35
36
        self.xpos = sdl.SDL_WINDOWPOS_CENTERED
37
        self.ypos = sdl.SDL_WINDOWPOS_CENTERED
38
39
        self.flags = sdl.SDL_WINDOW_OPENGL | sdl.SDL_WINDOW_RESIZABLE
40
41
        self.flags |= fullscreenMap[self.fullscreen]
42
43
        self.window = sdl.SDL_CreateWindow(self.title,
44
            self.xpos, self.ypos, self.width, self.height, self.flags)
45
46
47
    def make_current(self, context):
48
        ''' Make the specified rendering context apply to the window '''
49
        if context:
50
            window = self.window
51
            self.context = context
52
            context.window = self
53
            context = context.context
54
55
        else:
56
            if self.context:
57
                self.context.window = None
58
59
            window = None
60
            context = None
61
            self.context = None
62
63
        sdl.SDL_GL_MakeCurrent(window, context)
64
65
    def set_fullscreen(self, mode):
66
        '''Set the window fullscreen mode'''
67
        sdl.SDL_SetWindowFullscreen(self.window, fullscreenMap[mode])
68
69
    def flip(self):
70
        ''' Flip the back buffer to the front. '''
71
        sdl.SDL_GL_SwapWindow(self.window)
72
73
    def destroy(self):
74
        ''' Destroy the window '''
75
        sdl.SDL_DestroyWindow(self.window)
76
77
__all__ = ['WindowedMode', 'FullscreenMode', 'BorderlessMode', 'Window']
78