2015-09-26 14:49:21 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Parallel Realities
|
|
|
|
Copyright (C) 2011, 2012, 2013 Guus Sliepen
|
2019-05-22 00:47:32 +02:00
|
|
|
Copyright (C) 2015-2019 Julie Marchant <onpon4@riseup.net>
|
2015-09-26 14:49:21 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 3
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-11-02 23:53:05 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-08-04 21:58:38 +02:00
|
|
|
#include "SDL.h"
|
|
|
|
|
2016-11-26 00:21:31 +01:00
|
|
|
#include "defs.h"
|
|
|
|
#include "structs.h"
|
|
|
|
|
2016-01-02 22:59:48 +01:00
|
|
|
#include "engine.h"
|
2015-09-26 14:49:21 +02:00
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
SDL_Surface *screen;
|
2016-11-26 00:41:55 +01:00
|
|
|
LinkedRect *screen_bufferHead;
|
|
|
|
LinkedRect *screen_bufferTail;
|
2015-09-26 14:49:21 +02:00
|
|
|
|
|
|
|
void screen_blit(SDL_Surface *image, int x, int y)
|
|
|
|
{
|
|
|
|
gfx_blit(image, x, y, screen);
|
|
|
|
}
|
2015-10-11 18:04:52 +02:00
|
|
|
|
2019-05-31 00:09:15 +02:00
|
|
|
void screen_blitText(int i, int x, int y)
|
|
|
|
{
|
|
|
|
if (x == -1)
|
|
|
|
x = (screen->w - gfx_textSprites[i].image->w) / 2;
|
|
|
|
|
|
|
|
screen_blit(gfx_textSprites[i].image,
|
|
|
|
(x + (int)gfx_textSprites[i].x), (y +(int)gfx_textSprites[i].y));
|
|
|
|
}
|
|
|
|
|
2015-11-07 01:36:20 +01:00
|
|
|
int screen_renderString(const char *in, int x, int y, int fontColor)
|
|
|
|
{
|
|
|
|
return gfx_renderString(in, x, y, fontColor, 0, screen);
|
|
|
|
}
|
|
|
|
|
2019-06-06 04:13:48 +02:00
|
|
|
int screen_renderUnicode(const char *in, int x, int y, int fontColor)
|
|
|
|
{
|
|
|
|
return gfx_renderUnicode(in, x, y, fontColor, 0, screen);
|
|
|
|
}
|
|
|
|
|
2015-11-07 01:43:34 +01:00
|
|
|
/*
|
|
|
|
Draws the background surface that has been loaded
|
|
|
|
*/
|
|
|
|
void screen_drawBackground()
|
|
|
|
{
|
2019-05-30 22:46:55 +02:00
|
|
|
if (gfx_background != NULL)
|
|
|
|
screen_blit(gfx_background, 0, 0);
|
2015-11-07 01:43:34 +01:00
|
|
|
}
|
|
|
|
|
2015-11-02 23:53:05 +01:00
|
|
|
void screen_addBuffer(int x, int y, int w, int h)
|
|
|
|
{
|
2019-05-23 17:25:54 +02:00
|
|
|
LinkedRect *rect;
|
|
|
|
|
|
|
|
rect = malloc(sizeof(*rect));
|
|
|
|
if (rect == NULL)
|
|
|
|
{
|
|
|
|
engine_warn("Failed to allocate memor for screen buffer");
|
|
|
|
return;
|
|
|
|
}
|
2015-11-02 23:53:05 +01:00
|
|
|
|
|
|
|
rect->next = NULL;
|
|
|
|
rect->x = x;
|
|
|
|
rect->y = y;
|
|
|
|
rect->w = w;
|
|
|
|
rect->h = h;
|
|
|
|
|
|
|
|
screen_bufferTail->next = rect;
|
|
|
|
screen_bufferTail = rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void screen_flushBuffer()
|
|
|
|
{
|
2016-11-26 00:41:55 +01:00
|
|
|
LinkedRect *prevRect = screen_bufferHead;
|
|
|
|
LinkedRect *rect = screen_bufferHead;
|
2015-11-02 23:53:05 +01:00
|
|
|
screen_bufferTail = screen_bufferHead;
|
|
|
|
|
|
|
|
while (rect->next != NULL)
|
|
|
|
{
|
|
|
|
rect = rect->next;
|
|
|
|
|
|
|
|
prevRect->next = rect->next;
|
2019-05-20 00:22:53 +02:00
|
|
|
free(rect);
|
2015-11-02 23:53:05 +01:00
|
|
|
rect = prevRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
screen_bufferHead->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void screen_unBuffer()
|
|
|
|
{
|
2016-11-26 00:41:55 +01:00
|
|
|
LinkedRect *prevRect = screen_bufferHead;
|
|
|
|
LinkedRect *rect = screen_bufferHead;
|
2015-11-02 23:53:05 +01:00
|
|
|
screen_bufferTail = screen_bufferHead;
|
|
|
|
|
|
|
|
while (rect->next != NULL)
|
|
|
|
{
|
|
|
|
rect = rect->next;
|
|
|
|
|
|
|
|
SDL_Rect blitRect;
|
|
|
|
|
|
|
|
blitRect.x = rect->x;
|
|
|
|
blitRect.y = rect->y;
|
|
|
|
blitRect.w = rect->w;
|
|
|
|
blitRect.h = rect->h;
|
|
|
|
|
2015-11-07 01:43:34 +01:00
|
|
|
if (SDL_BlitSurface(gfx_background, &blitRect, screen, &blitRect) < 0)
|
2015-11-02 23:53:05 +01:00
|
|
|
{
|
|
|
|
printf("BlitSurface error: %s\n", SDL_GetError());
|
2016-01-02 22:59:48 +01:00
|
|
|
engine_showError(2, "");
|
2015-11-02 23:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prevRect->next = rect->next;
|
2019-05-20 00:22:53 +02:00
|
|
|
free(rect);
|
2015-11-02 23:53:05 +01:00
|
|
|
rect = prevRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
screen_bufferHead->next = NULL;
|
|
|
|
}
|
2015-11-07 03:35:07 +01:00
|
|
|
|
|
|
|
void screen_clear(Uint32 color)
|
|
|
|
{
|
|
|
|
SDL_FillRect(screen, NULL, color);
|
|
|
|
}
|
2015-12-31 16:47:27 +01:00
|
|
|
|
|
|
|
void screen_drawRect(int x, int y, int w, int h, Uint8 red, Uint8 green, Uint8 blue)
|
|
|
|
{
|
|
|
|
gfx_drawRect(screen, x, y, w, h, red, green, blue);
|
|
|
|
}
|
2019-05-30 19:14:32 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
2019-06-06 04:13:48 +02:00
|
|
|
|
2019-05-30 19:14:32 +02:00
|
|
|
// Calculate dimensions
|
|
|
|
if (new_ratio > default_ratio)
|
|
|
|
{
|
|
|
|
// Wide screen
|
|
|
|
w = (DEFAULT_SCREEN_HEIGHT * w) / h;
|
|
|
|
h = DEFAULT_SCREEN_HEIGHT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Tall screen
|
|
|
|
h = (DEFAULT_SCREEN_WIDTH * h) / w;
|
2019-05-31 05:09:10 +02:00
|
|
|
w = DEFAULT_SCREEN_WIDTH;
|
2019-05-30 19:14:32 +02:00
|
|
|
}
|
2019-06-06 04:13:48 +02:00
|
|
|
|
2019-05-30 19:14:32 +02:00
|
|
|
// Free previous surface (if it exists)
|
|
|
|
if (screen != NULL)
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(screen);
|
|
|
|
screen = NULL;
|
|
|
|
}
|
2019-06-06 04:13:48 +02:00
|
|
|
|
2019-05-30 19:14:32 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|