Skip to content

Commit

Permalink
Fixed window resizing
Browse files Browse the repository at this point in the history
Signed-off-by: Ole Herman Schumacher Elgesem <oleherman93@gmail.com>
  • Loading branch information
olehermanse committed Jun 4, 2019
1 parent fe64cb3 commit 6459f72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
17 changes: 1 addition & 16 deletions mrpg/gui/controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys

import pyglet

from mrpg.core.game import Game, State
from mrpg.gui import wrapper
from mrpg.gui.commons import Color
Expand All @@ -13,7 +11,6 @@ def __init__(self, width=1280, height=720):
self.window = wrapper.init(self, width, height, caption="MRPG")
self.game = Game()
self.gui = GUI(self.window)
self.width, self.height = self.window.get_viewport_size()

if self.game.menu:
self.gui.menu.choices(*self.game.menu.choices)
Expand All @@ -23,24 +20,12 @@ def __init__(self, width=1280, height=720):
self.update_text()

def resize(self, w, h):
width, height = self.window.get_viewport_size()
self.gui.resize(width, height)
self.gui.resize(w, h)

def run(self):
wrapper.run()

def draw(self):
self.window.clear()

pyglet.gl.glClearColor(*Color.float(Color.BLACK))
pyglet.gl.glMatrixMode(pyglet.gl.GL_PROJECTION)
pyglet.gl.glLoadIdentity()

width, height = self.window.get_viewport_size()

pyglet.gl.glOrtho(0, width, 0, height, -1, 1)
pyglet.gl.glMatrixMode(pyglet.gl.GL_MODELVIEW)

self.gui.draw()

def update(self, dt):
Expand Down
21 changes: 19 additions & 2 deletions mrpg/gui/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,26 @@ def on_resize(self, w, h):
w = int(self.ratio * h) # Locked aspect ratio
self.width = w
self.height = h
self.controller.resize(w, h)

def on_draw(self, ):
# Don't use window dimensions for coordinate system, use viewport:
width, height = self.get_viewport_size() # May be scaled (on mac)

# Resize viewport (only necessary on windows):
pyglet.gl.glViewport(0, 0, max(1, width), max(1, height))

self.controller.resize(width, height)

def on_draw(self):
self.clear()

# Use viewport size to set the coordinate system:
pyglet.gl.glMatrixMode(pyglet.gl.GL_PROJECTION)
pyglet.gl.glLoadIdentity()
width, height = self.get_viewport_size()
pyglet.gl.glOrtho(0, width, 0, height, -1, 1)
pyglet.gl.glMatrixMode(pyglet.gl.GL_MODELVIEW)
# The glOrtho call is necessary for high resolution mac font rendering

self.controller.draw()

def on_mouse_motion(self, x, y, dx, dy):
Expand Down

0 comments on commit 6459f72

Please sign in to comment.