1

I want to understand how do the coordinates of a pixel change when the window size is changed (in my case the size is changed to fit the entire screen). I am trying to achieve this by writing a common code for both cases (i.e. fullscreen and not fullscreen).

From what I have seen, the conversion from one window size to another doesn't just depend on a multiplicative factor.

The not fullscreen case

The fullscreen case (blue arrows are just for indicating the gap)

I programmed the spaceship to be at the left-bottom corner of the window in the not fullscreen case and when I change the size to fullscreen there is a gap between the spaceship and the bottom. How do I keep the spaceship in the corner even in the fullscreen mode?

Here is code for creating the window:

    window = SDL_CreateWindow(title, xpos, ypos, screenWidth / 2,
                          screenHeight / 2, flags);
if (window) {
  std::cout << "Window created!" << std::endl;
} else {
  std::cout << "Error: " << SDL_GetError()
            << std::endl; // SDL_GetError() specifies the error
}

for toggling fullscreen:

  void ToggleFullscreen(SDL_Window *Window) {
  Uint32 FullscreenFlag = SDL_WINDOW_FULLSCREEN;
  bool IsFullscreen = SDL_GetWindowFlags(Window) & FullscreenFlag;
  SDL_SetWindowFullscreen(Window, IsFullscreen ? 0 : FullscreenFlag);
  SDL_ShowCursor(IsFullscreen);
}

And the code for positioning the spaceship (the second and third arguments are the x and y coordinates respectively):

spaceship = new GameObject("Assets/Spaceship.png", 0, (screenHeight * 9) / 20, 0);

The definition of the GameObject constructor being used above (the TextureManager just returns a SDL_Texture of the image):

GameObject::GameObject(const char *textureSheet, int x, int y, bool isAnimated)
    : xpos{x}, ypos{y} {
  objTexture = TextureManager::LoadTexture(textureSheet);
  srcRect.x = 0;
  srcRect.y = 0;
  srcRect.w = 32;
  srcRect.h = 32;
  destRect.x = xpos;
  destRect.y = ypos;
  destRect.h = (Game::screenHeight) / 20;
  destRect.w = (Game::screenHeight) / 20;
}
  • What about the code for positioning the space-ship? Is it the same in each instance? I'd expect to see something that positioned the X at 0 and the Y at windowHeight-spriteHeight. Assuming of course, SDL uses the top-left corner as 0,0 – enhzflep Jun 30 '20 at 06:20
  • @enhzflep I've added the code for the positioning. The coordinates of the spaceship are exactly as you mentioned (0, screenHeight-spriteHeight). – Shrish Shankar Jun 30 '20 at 06:33
  • Are screenHeight and screenWidth related to the physical dimensions of the output device, or are they related to window size? If they're related to window-size, then I suppose your problem lies here, in the setting of `destRect.x` and `destRect.y`, since these values _should_ have different values after you enter/leave full-screen mode. – enhzflep Jun 30 '20 at 08:10
  • 1
    This looks like a problem too: `(screenHeight * 9) / 20` – enhzflep Jun 30 '20 at 08:51
  • @enhzflep screenHeight and screenWidth are the physical dimensions of the output device, I have accessed those values using the 'SDL_GetDesktopDisplayMode()' function. '(screenHeight * 9) / 20' is just '(screenHeight / 2 - screenHeight / 20)' which basically translates to screenHeight - spriteHeight. – Shrish Shankar Jun 30 '20 at 13:19
  • 1
    @Srish - Well I'm all out of guessing ideas. Perhaps you could upload all of the relevant code into the question so that your issue can be reproduced. It's certainly one of the recommendations in the Help Centre. As found here (https://stackoverflow.com/help/how-to-ask) behind the link titled "How to create a Minimal, Complete, and Verifiable example" – enhzflep Jun 30 '20 at 15:23
  • You may be getting the previous display mode, Take a look at [this](https://stackoverflow.com/a/33393689/980129) – Manuel Jun 30 '20 at 22:19
  • @Manuel I tried using SDL_GetCurrentDisplayMode(), and the output doesn't change. – Shrish Shankar Jul 01 '20 at 04:46
  • @ShrishShankar do you have more than one display? SDL_GetCurrentDisplayMode will give the virtual desktop size of the display you ask for. If there are some, you should ask for them in a loop. – Manuel Jul 01 '20 at 13:01

0 Answers0