The convention of using a "y-down" coordinate system, where the origin (0,0) is at the top-left and the y-value increases downwards, is common in computer graphics, particularly for 2D rendering and windowing systems. SDL follows this convention.
This practice dates back to early computing and television technology:
Most operating system windowing APIs (like Windows GDI, macOS Quartz, X11) also use a top-left origin for window coordinates. SDL, aiming for cross-platform consistency and integration with these systems, adopts the same convention. This makes it easier to translate coordinates between SDL surfaces/windows and the underlying OS graphics context.
For many 2D graphics tasks, like rendering UI elements, sprites, or tilemaps, working from the top-left often feels intuitive. Layouts are typically defined relative to the top and left edges of containers or the screen.
The y-up system, where y increases upwards, is standard in mathematics (e.g., Cartesian coordinates) and often used in 3D graphics APIs (like OpenGL's normalized device coordinates, though projection matrices often flip the y-axis to match the y-down framebuffer).
While SDL uses y-down for window coordinates, mouse events, and surface rendering, you can always perform mathematical conversions if a y-up system is more natural for specific calculations (like physics simulations). This typically involves subtracting the y-coordinate from the height of the relevant area (window or world space).
int windowHeight = GameWindow.GetHeight();
int y_up_coordinate = windowHeight - event.motion.y;
We delve deeper into coordinate systems, transformations, and managing different spaces (world space, screen space) later in the course, particularly when we introduce cameras and rendering scenes.
Answers to questions are automatically generated and may not have been reviewed.
Discover how to process mouse input, including position tracking and button presses