Started on adaptive aspect ratio adjustment.

Just one problem: the backgrounds don't get resized and so you get
awful mush on the right side/bottom of the screen. Will fix in
the next commit.
This commit is contained in:
Julie Marchant 2019-05-30 13:14:32 -04:00
parent f10af4aaf3
commit 12a3fcdbab
6 changed files with 72 additions and 21 deletions

View File

@ -270,12 +270,7 @@ void engine_setMode()
engine.useMusic = useMusic; engine.useMusic = useMusic;
engine.autoPause = autoPause; engine.autoPause = autoPause;
screen = SDL_CreateRGBSurface(0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0xff0000, 0xff00, 0xff, 0xff000000); screen_adjustDimensions(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
if (screen == NULL)
{
printf("Couldn't create %ix%ix32 surface: %s\n", DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SDL_GetError());
exit(1);
}
window = SDL_CreateWindow("Project: Starfighter", window = SDL_CreateWindow("Project: Starfighter",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
@ -289,21 +284,7 @@ void engine_setMode()
SDL_SetWindowIcon(window, gfx_loadImage("gfx/alienDevice.png")); SDL_SetWindowIcon(window, gfx_loadImage("gfx/alienDevice.png"));
SDL_SetWindowFullscreen(window, engine.fullScreen ? FULLSCREEN : 0); SDL_SetWindowFullscreen(window, engine.fullScreen ? FULLSCREEN : 0);
renderer = SDL_CreateRenderer(window, -1, 0); renderer_reset();
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);
}
#ifndef NOSOUND #ifndef NOSOUND
if (engine.useAudio) if (engine.useAudio)

View File

@ -337,6 +337,8 @@ void player_getInput()
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if (engine.event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) if (engine.event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{ {
screen_adjustDimensions(engine.event.window.data1, engine.event.window.data2);
renderer_reset();
screen_clear(black); screen_clear(black);
renderer_update(); renderer_update();
screen_clear(black); screen_clear(black);

View File

@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "structs.h" #include "structs.h"
#include "screen.h" #include "screen.h"
#include "window.h"
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *renderer_texture; SDL_Texture *renderer_texture;
@ -36,3 +37,28 @@ void renderer_update()
SDL_RenderCopy(renderer, renderer_texture, NULL, NULL); SDL_RenderCopy(renderer, renderer_texture, NULL, NULL);
SDL_RenderPresent(renderer); 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);
}
}

View File

@ -29,5 +29,6 @@ extern SDL_Renderer *renderer;
extern SDL_Texture *renderer_texture; extern SDL_Texture *renderer_texture;
void renderer_update(); void renderer_update();
void renderer_reset();
#endif #endif

View File

@ -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); 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);
}
}

View File

@ -38,5 +38,6 @@ void screen_flushBuffer();
void screen_unBuffer(); void screen_unBuffer();
void screen_clear(Uint32 color); void screen_clear(Uint32 color);
void screen_drawRect(int x, int y, int w, int h, Uint8 red, Uint8 green, Uint8 blue); 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 #endif