From 7211e3632aeeb1128a8f29d667149a2b9f381e57 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 9 Aug 2015 14:31:26 +0200 Subject: [PATCH] Make the window resizable, and set a reasonable initial size. Now that SDL takes care of properly scaling the graphics, there is no need for the window to be exactly 640x480. So allow the user to resize it. To cope with high DPI screens, set the initial window size to a multiple of 640x480 if it would fill less than half the width and height. --- src/init.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 1e947c0..a274ad8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -286,14 +286,45 @@ void initSystem() graphics.screen = SDL_CreateRGBSurface(0, 640, 480, 32, 0xff0000, 0xff00, 0xff, 0xff000000); - graphics.window = SDL_CreateWindow("Blobwars: Metal Blob Solid", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, graphics.screen->w, graphics.screen->h, 0); + if (graphics.screen == NULL) + { + printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError()); + exit(1); + } + + // Increase the size of the window if we have large desktop resolutions + SDL_DisplayMode displayMode{0}; + SDL_GetDesktopDisplayMode(0, &displayMode); + int w = graphics.screen->w; + int h = graphics.screen->h; + while (displayMode.w > w * 2 && displayMode.h > h * 2) + { + w *= 2; + h *= 2; + } + + graphics.window = SDL_CreateWindow("Blobwars: Metal Blob Solid", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_RESIZABLE); + + if (graphics.window == NULL) + { + printf("Couldn't create %dx%d window: %s\n", w, h, SDL_GetError()); + exit(1); + } + graphics.renderer = SDL_CreateRenderer(graphics.window, -1, SDL_RENDERER_PRESENTVSYNC); + + if (graphics.renderer == NULL) + { + printf("Couldn't create renderer: %s\n", SDL_GetError()); + exit(1); + } + SDL_RenderSetLogicalSize(graphics.renderer, graphics.screen->w, graphics.screen->h); graphics.texture = SDL_CreateTexture(graphics.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, graphics.screen->w, graphics.screen->h); - if (graphics.window == NULL) + if (graphics.texture == NULL) { - printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError()); + printf("Could not create %dx%d texture: %s\n", graphics.screen->w, graphics.screen->h, SDL_GetError()); exit(1); } -- 2.39.5