Rescale backgrounds when resizing window.

This commit is contained in:
Julie Marchant 2019-05-30 16:46:55 -04:00
parent 12a3fcdbab
commit 840b7fbd30
5 changed files with 27 additions and 8 deletions

View File

@ -386,6 +386,7 @@ void engine_cleanup()
{
gfx_free();
SDL_FreeSurface(gfx_background);
SDL_FreeSurface(gfx_unscaledBackground);
audio_free();
engine_resetLists();
free(engine.bulletHead);

View File

@ -33,6 +33,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "screen.h"
#include "weapons.h"
SDL_Surface *gfx_unscaledBackground;
SDL_Surface *gfx_background;
SDL_Surface *gfx_sprites[SP_MAX];
SDL_Surface *gfx_faceSprites[FS_MAX];
@ -496,21 +497,34 @@ void gfx_free()
}
}
void gfx_loadBackground(const char *filename)
void gfx_scaleBackground()
{
SDL_Surface *new_bg;
if (gfx_background != NULL)
{
SDL_FreeSurface(gfx_background);
gfx_background = NULL;
}
new_bg = gfx_loadImage(filename);
SDL_SetColorKey(new_bg, 0, 0);
gfx_background = gfx_createSurface(screen->w, screen->h);
if (gfx_background == NULL)
engine_error("Failed to create surface for scaled background");
SDL_SetColorKey(gfx_background, 0, 0);
SDL_BlitScaled(new_bg, NULL, gfx_background, NULL);
SDL_FreeSurface(new_bg);
SDL_BlitScaled(gfx_unscaledBackground, NULL, gfx_background, NULL);
}
void gfx_loadBackground(const char *filename)
{
if (gfx_unscaledBackground != NULL)
{
SDL_FreeSurface(gfx_unscaledBackground);
gfx_unscaledBackground = NULL;
}
gfx_unscaledBackground = gfx_loadImage(filename);
if (gfx_unscaledBackground == NULL)
engine_error("Failed to load unscaled background image");
SDL_SetColorKey(gfx_unscaledBackground, 0, 0);
gfx_scaleBackground();
}
void gfx_loadSprites()

View File

@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "defs.h"
#include "structs.h"
extern SDL_Surface *gfx_unscaledBackground;
extern SDL_Surface *gfx_background;
extern SDL_Surface *gfx_sprites[SP_MAX];
extern SDL_Surface *gfx_faceSprites[FS_MAX];
@ -48,6 +49,7 @@ SDL_Surface *gfx_createAlphaRect(int width, int height, Uint8 red, Uint8 green,
void gfx_createMessageBox(SDL_Surface *face, const char *message, int transparent);
SDL_Surface *gfx_loadImage(const char *filename);
void gfx_free();
void gfx_scaleBackground();
void gfx_loadBackground(const char *filename);
void gfx_loadSprites();
void gfx_loadFont();

View File

@ -339,6 +339,7 @@ void player_getInput()
{
screen_adjustDimensions(engine.event.window.data1, engine.event.window.data2);
renderer_reset();
gfx_scaleBackground();
screen_clear(black);
renderer_update();
screen_clear(black);

View File

@ -51,6 +51,7 @@ Draws the background surface that has been loaded
*/
void screen_drawBackground()
{
if (gfx_background != NULL)
screen_blit(gfx_background, 0, 0);
}