diff --git a/src/engine.c b/src/engine.c index be1ba39..a993b36 100644 --- a/src/engine.c +++ b/src/engine.c @@ -270,12 +270,7 @@ void engine_setMode() engine.useMusic = useMusic; engine.autoPause = autoPause; - screen = SDL_CreateRGBSurface(0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0xff0000, 0xff00, 0xff, 0xff000000); - if (screen == NULL) - { - printf("Couldn't create %ix%ix32 surface: %s\n", DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SDL_GetError()); - exit(1); - } + screen_adjustDimensions(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT); window = SDL_CreateWindow("Project: Starfighter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -289,21 +284,7 @@ void engine_setMode() SDL_SetWindowIcon(window, gfx_loadImage("gfx/alienDevice.png")); SDL_SetWindowFullscreen(window, engine.fullScreen ? FULLSCREEN : 0); - renderer = SDL_CreateRenderer(window, -1, 0); - if (renderer == NULL) - { - printf("Could not create renderer: %s\n", SDL_GetError()); - exit(1); - } - - SDL_RenderSetLogicalSize(renderer, screen->w, screen->h); - - renderer_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen->w, screen->h); - if (renderer_texture == NULL) - { - printf("Couldn't create %ix%ix32 texture: %s\n", screen->w, screen->h, SDL_GetError()); - exit(1); - } + renderer_reset(); #ifndef NOSOUND if (engine.useAudio) diff --git a/src/player.c b/src/player.c index aaa21fb..170de5f 100644 --- a/src/player.c +++ b/src/player.c @@ -337,6 +337,8 @@ void player_getInput() case SDL_WINDOWEVENT: if (engine.event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + screen_adjustDimensions(engine.event.window.data1, engine.event.window.data2); + renderer_reset(); screen_clear(black); renderer_update(); screen_clear(black); diff --git a/src/renderer.c b/src/renderer.c index ce3023c..5474f2e 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -23,6 +23,7 @@ along with this program. If not, see . #include "structs.h" #include "screen.h" +#include "window.h" SDL_Renderer *renderer; SDL_Texture *renderer_texture; @@ -36,3 +37,28 @@ void renderer_update() SDL_RenderCopy(renderer, renderer_texture, NULL, NULL); SDL_RenderPresent(renderer); } + +// Call after screen_adjustDimensions. +void renderer_reset() +{ + if (renderer == NULL) + { + renderer = SDL_CreateRenderer(window, -1, 0); + if (renderer == NULL) + { + printf("Could not create renderer: %s\n", SDL_GetError()); + exit(1); + } + } + SDL_RenderSetLogicalSize(renderer, screen->w, screen->h); + + if (renderer_texture != NULL) + SDL_DestroyTexture(renderer_texture); + + renderer_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen->w, screen->h); + if (renderer_texture == NULL) + { + printf("Couldn't create %ix%ix32 texture: %s\n", screen->w, screen->h, SDL_GetError()); + exit(1); + } +} diff --git a/src/renderer.h b/src/renderer.h index 7f990a2..58af6f8 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -29,5 +29,6 @@ extern SDL_Renderer *renderer; extern SDL_Texture *renderer_texture; void renderer_update(); +void renderer_reset(); #endif diff --git a/src/screen.c b/src/screen.c index f09a2bc..948161a 100644 --- a/src/screen.c +++ b/src/screen.c @@ -133,3 +133,43 @@ void screen_drawRect(int x, int y, int w, int h, Uint8 red, Uint8 green, Uint8 b { gfx_drawRect(screen, x, y, w, h, red, green, blue); } + +/* +Adjust the dimensions of the screen to match the aspect ratio of the +window, using DEFAULT_SCREEN_WIDTH and DEFAULT_SCREEN_HEIGHT as a +baseline. +*/ +void screen_adjustDimensions(int w, int h) +{ + double default_ratio = (double)DEFAULT_SCREEN_WIDTH / (double)DEFAULT_SCREEN_HEIGHT; + double new_ratio = (double)w / (double)h; + + // Calculate dimensions + if (new_ratio > default_ratio) + { + // Wide screen + w = (DEFAULT_SCREEN_HEIGHT * w) / h; + h = DEFAULT_SCREEN_HEIGHT; + } + else + { + // Tall screen + w = DEFAULT_SCREEN_WIDTH; + h = (DEFAULT_SCREEN_WIDTH * h) / w; + } + + // Free previous surface (if it exists) + if (screen != NULL) + { + SDL_FreeSurface(screen); + screen = NULL; + } + + // Create the surface + screen = SDL_CreateRGBSurface(0, w, h, 32, 0xff0000, 0xff00, 0xff, 0xff000000); + if (screen == NULL) + { + printf("Couldn't create %ix%ix32 surface: %s\n", w, h, SDL_GetError()); + exit(1); + } +} diff --git a/src/screen.h b/src/screen.h index 33f0842..5196739 100644 --- a/src/screen.h +++ b/src/screen.h @@ -38,5 +38,6 @@ void screen_flushBuffer(); void screen_unBuffer(); void screen_clear(Uint32 color); void screen_drawRect(int x, int y, int w, int h, Uint8 red, Uint8 green, Uint8 blue); +void screen_adjustDimensions(int w, int h); #endif