9. Screen and Coordinates¶
9.1 Screen¶
- The part within the window where the background color can be changed is called the screen (scene)
- Siv3D can display text, shapes, and images in this area
- The screen size is 800 pixels wide and 600 pixels tall by default
- The screen size may also be called "window size"
- The window size including the frame around the screen, such as the title bar, is called "window area size" to distinguish it
- In normal Siv3D programming, it's sufficient to be aware of only the screen size (window size)
9.2 Changing Screen Size¶
- Change the screen size with
Window::Resize(width, height)
- Once changed, that size is maintained. Usually set at the beginning before the main loop
- The following code changes the screen size to 1280 pixels wide and 720 pixels tall
Change Screen Size
# include <Siv3D.hpp>
void Main()
{
// Resize screen to 1280x720
Window::Resize(1280, 720);
while (System::Update())
{
}
}
9.3 Coordinates¶
- When displaying shapes and images on the screen, you specify coordinates that represent pixel positions on the screen
- Coordinates are represented as
(X, Y)
using two values: X coordinate and Y coordinate - The top-left pixel of the screen has "X coordinate: 0" and "Y coordinate: 0", which is
(0, 0)
- Moving right increases the X coordinate, and moving down increases the Y coordinate
- When the screen size is 800 × 600, the bottom-right pixel of the screen has coordinates
(799, 599)
- The following code displays the coordinates of the mouse cursor in the top-left of the screen
Display Mouse Cursor Coordinates
# include <Siv3D.hpp>
void Main()
{
while (System::Update())
{
ClearPrint();
// Display current mouse cursor coordinates
Print << Cursor::Pos();
}
}
9.4 Classes for Representing Coordinates¶
- The classes
Point
andVec2
are provided for representing coordinates Point
represents coordinates with integers
Vec2
represents coordinates with floating point numbers
Cursor::Pos()
introduced in 9.3 returns the mouse cursor coordinates as aPoint
type
Display Mouse Cursor Coordinates
# include <Siv3D.hpp>
void Main()
{
while (System::Update())
{
ClearPrint();
const Point pos = Cursor::Pos();
// Display current mouse cursor coordinates separated by X and Y coordinates
Print << U"X: " << pos.x;
Print << U"Y: " << pos.y;
}
}
9.5 Converting Point
→ Vec2
¶
Point
→Vec2
can be converted naturally (implicitly)
# include <Siv3D.hpp>
void Main()
{
while (System::Update())
{
ClearPrint();
const Vec2 pos = Cursor::Pos();
// Display current mouse cursor coordinates separated by X and Y coordinates
Print << U"X: " << pos.x;
Print << U"Y: " << pos.y;
}
}
9.6 Converting Vec2
→ Point
¶
- Converting
Vec2
→Point
loses information, so you need to convert explicitly using.asPoint()
- Decimal information is truncated
# include <Siv3D.hpp>
void Main()
{
const Vec2 pos1{ 123.4, 567.8 };
Print << pos1;
// Convert Vec2 → Point
const Point pos2 = pos1.asPoint();
Print << pos2;
while (System::Update())
{
}
}
Review Checklist¶
- Understood the default screen size (800x600)
- Learned that screen size can be changed with
Window::Resize(width, height)
- Understood that coordinates are represented by two values, X and Y coordinates, with the top-left of the screen being
(0, 0)
- Learned that
Point
andVec2
are classes for representing coordinates, wherePoint
uses integers andVec2
uses floating point numbers - Learned that
Point
→Vec2
can be converted implicitly - Learned that
Vec2
→Point
is converted explicitly using.asPoint()