Skip to content

Commit 3c37c59

Browse files
committed
Add Context.new_console method.
Examples have been updated to use the new method, the older way to do this has been deprecated.
1 parent 9eaf8e2 commit 3c37c59

5 files changed

Lines changed: 64 additions & 20 deletions

File tree

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
Added
12+
- New context method `Context.new_console`.
13+
14+
Deprecated
15+
- `Context.recommended_console_size` has been replaced with
16+
`Context.new_console`.
1117

1218
11.17.0 - 2020-10-30
1319
--------------------

docs/tcod/getting-started.rst

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Dynamically-sized console
6161

6262
The next example shows a more advanced setup. A maximized window is created
6363
and the console is dynamically scaled to fit within it. If the window is
64-
resized then the console will be replaced by a new dynamically sized console.
64+
resized then the console will be resized to match it.
6565

6666
Because a tileset wasn't manually loaded in this example an OS dependent
6767
fallback font will be used. This is useful for prototyping but it's not
@@ -70,12 +70,17 @@ platforms.
7070

7171
The `integer_scaling` parameter to :any:`Context.present` prevents the console
7272
from being slightly stretched, since the console will rarely be the prefect
73-
size a small border will exist.
73+
size a small border will exist. This border is black by default but can be
74+
changed to another color.
7475

7576
You'll need to consider things like the console being too small for your code
7677
to handle or the tiles being small compared to an extra large monitor
77-
resolution. :any:`Context.recommended_console_size` can be given a minimum
78-
size that it will never go below.
78+
resolution. :any:`Context.new_console` can be given a minimum size that it
79+
will never go below.
80+
81+
You can call :any:`Context.new_console` every frame or only when the window
82+
is resized. This example creates a new console every frame instead of
83+
clearing the console every frame and replacing it only on resizing the window.
7984

8085
Example::
8186

@@ -91,10 +96,8 @@ Example::
9196
with tcod.context.new( # New window with pixel resolution of width×height.
9297
width=WIDTH, height=HEIGHT, sdl_window_flags=FLAGS
9398
) as context:
94-
# Create the console based on the context.
95-
console = tcod.Console(*context.recommended_console_size())
9699
while True:
97-
console.clear()
100+
console = context.new_console()
98101
console.print(0, 0, "Hello World")
99102
context.present(console, integer_scaling=True)
100103

@@ -104,8 +107,7 @@ Example::
104107
if event.type == "QUIT":
105108
raise SystemExit()
106109
if event.type == "WINDOWRESIZED":
107-
# Replace the console with one that fits the new resolution.
108-
console = tcod.Console(*context.recommended_console_size())
110+
pass # The next call to context.new_console may return a different size.
109111

110112

111113
if __name__ == "__main__":

examples/eventget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def main() -> None:
2323
with tcod.context.new(
2424
width=WIDTH, height=HEIGHT, sdl_window_flags=FLAGS
2525
) as context:
26-
console = tcod.Console(*context.recommended_console_size())
26+
console = context.new_console()
2727
while True:
2828
# Display all event items.
2929
console.clear()
@@ -42,7 +42,7 @@ def main() -> None:
4242
if event.type == "QUIT":
4343
raise SystemExit()
4444
if event.type == "WINDOWRESIZED":
45-
console = tcod.Console(*context.recommended_console_size())
45+
console = context.new_console()
4646
if event.type == "MOUSEMOTION":
4747
motion_desc = str(event)
4848
else:

tcod/context.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
from typing import Any, Iterable, List, Optional, Tuple
5454

5555
import tcod
56-
from tcod._internal import _check, _check_warn, pending_deprecate
56+
from tcod._internal import _check, _check_warn, pending_deprecate, deprecate
5757
from tcod.loader import ffi, lib
5858
import tcod.event
5959
import tcod.tileset
@@ -263,25 +263,61 @@ def change_tileset(self, tileset: Optional[tcod.tileset.Tileset]) -> None:
263263
)
264264
)
265265

266-
def recommended_console_size(
267-
self, min_columns: int = 1, min_rows: int = 1
268-
) -> Tuple[int, int]:
269-
"""Return the recommended (columns, rows) of a console for this
270-
context.
266+
def new_console(
267+
self,
268+
min_columns: int = 1,
269+
min_rows: int = 1,
270+
magnification: float = 1.0,
271+
) -> tcod.console.Console:
272+
"""Return a new console sized for this context.
271273
272-
The times where it's the most useful to call this method are:
274+
`min_columns` and `min_rows` are the minimum size to use for the new
275+
console.
276+
277+
`magnification` determines the apparent size of the tiles on the output
278+
display. A `magnification` larger then 1.0 will output smaller
279+
consoles, which will show as larger tiles when presented.
280+
`magnification` must be greater than zero.
281+
282+
The times where it is the most useful to call this method are:
273283
274284
* After the context is created, even if the console was given a
275285
specific size.
276286
* After the :any:`change_tileset` method is called.
277287
* After any window resized event, or any manual resizing of the window.
278288
289+
.. versionadded:: 11.18
290+
"""
291+
if magnification < 0:
292+
raise ValueError(
293+
"Magnification must be greater than zero. (Got %f)"
294+
% magnification
295+
)
296+
size = ffi.new("int[2]")
297+
_check(
298+
lib.TCOD_context_recommended_console_size(
299+
self._context_p, magnification, size, size + 1
300+
)
301+
)
302+
width, height = max(min_columns, size[0]), max(min_rows, size[1])
303+
return tcod.console.Console(width, height)
304+
305+
@deprecate("This method has been replaced by Context.new_console.")
306+
def recommended_console_size(
307+
self, min_columns: int = 1, min_rows: int = 1
308+
) -> Tuple[int, int]:
309+
"""Return the recommended (columns, rows) of a console for this
310+
context.
311+
279312
`min_columns`, `min_rows` are the lowest values which will be returned.
313+
314+
.. deprecated::
315+
This method has been replaced by :any:`Context.new_console`.
280316
"""
281317
with ffi.new("int[2]") as size:
282318
_check(
283319
lib.TCOD_context_recommended_console_size(
284-
self._context_p, size, size + 1
320+
self._context_p, 1.0, size, size + 1
285321
)
286322
)
287323
return max(min_columns, size[0]), max(min_rows, size[1])

0 commit comments

Comments
 (0)