Understanding SDL_Surface
Coordinate System (0,0)
Where does the (0,0) coordinate of the surface correspond to on the window?
In SDL (and many other 2D graphics libraries), the coordinate system for surfaces places the origin (0,0)
at the top-left corner of the surface.
- The X-axis increases positively as you move to the right.
- The Y-axis increases positively as you move downwards.
Relation to the Window Surface
When you get the surface associated with an SDL_Window
using SDL_GetWindowSurface()
, this coordinate system applies directly to the drawable client area of that window (the area inside the window frame and below the title bar).
Therefore, (0,0)
on the window surface corresponds to the top-left pixel inside the window's border.
If the window surface has a width W
and height H
(obtainable from WindowSurface->w
and WindowSurface->h
), then:
- The top-right pixel is at
(W-1, 0)
. - The bottom-left pixel is at
(0, H-1)
. - The bottom-right pixel is at
(W-1, H-1)
.
Example: Filling a Rectangle
If you create an SDL_Rect
like this:
SDL_Rect MyRect;
MyRect.x = 10;
MyRect.y = 20;
MyRect.w = 100;
MyRect.h = 50;
And use it with SDL_FillRect()
on the window surface, you will fill a rectangle that starts 10 pixels from the left edge and 20 pixels from the top edge of the window's client area. The rectangle itself will be 100 pixels wide and 50 pixels tall.
Understanding this top-left origin coordinate system is crucial for positioning all visual elements in your SDL application. We will work extensively with coordinates and SDL_Rect
structures in subsequent lessons when we start drawing images and handling object movement.
SDL Surfaces and Colors
Explore SDL surfaces, the canvases for drawing, understand pixel formats, colors, and set your window's background.