glViewport from top left, not bottom left

Bonjour.

The program I am writing is completely 2D. All
the math and logic assumes a coordinate system
where x = 0 is the left and y = 0 is the top of
the window.

The problem:

glViewport, which I expect to use a lot, in
various parts of the code, expects the y value to
be the bottom of the window. Sooner or later,
I’m bound to forget to adjust y and end up with
a blank screen or other hard to track down visual
error.

Essentially I’m looking for something like this:

void cam_viewport(int x, int y, int w, int h)
{
  glViewport(x, adjust_y(y), w, h);
}

Any ideas how this can be implemented? I’m sure
the solution is simple but I can’t quite seem to
get it.

glWievport(x, y - h, w, h);

Yes, I tried that and got a totally blank screen.

There’s probably something suspect in my code
elsewhere.

If I do this:

void cam_viewport(int x, int y, int w, int h)
{
  glViewport(x, y - h, w, h);
}

cam_viewport(0, 0, 640, 480);

GL_INVALID_VALUE is generated by passing a
negative value to glViewport().

Sorry, it should be:
glViewport(x, sh - y - h, w, h);
where sh = screen height (in pixels)

Yes, that’s it.

Why I couldn’t work this out alone, I’ll never
know. I seem to be having one of those weeks.

Thank you.