starfighter/src/resources.cpp

199 lines
4.5 KiB
C++
Raw Normal View History

/*
Copyright (C) 2003 Parallel Realities
Copyright (C) 2011, 2012, 2013 Guus Sliepen
Copyright (C) 2015 Julian Marchant
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
2015-02-26 17:20:36 +01:00
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
2015-02-26 17:20:36 +01:00
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
2015-02-26 17:20:36 +01:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Starfighter.h"
void loadBackground(const char *filename)
{
2015-11-07 01:43:34 +01:00
if (gfx_background != NULL)
{
2015-11-07 01:43:34 +01:00
SDL_FreeSurface(gfx_background);
gfx_background = NULL;
}
2015-11-07 01:43:34 +01:00
gfx_background = loadImage(filename);
SDL_SetColorKey(gfx_background, 0, 0);
}
void loadGameGraphics()
{
int index;
char string[75] = "";
FILE *fp;
2015-03-30 13:52:44 +02:00
Uint32 *p32;
Uint16 *p16;
Uint8 *p8;
freeGraphics();
shipShape[0] = loadImage("gfx/firefly1.png");
shipShape[1] = loadImage("gfx/firefly2.png");
switch(game.system)
{
case 0:
strcpy(string, "data/resources_spirit.dat");
break;
case 1:
strcpy(string, "data/resources_eyananth.dat");
break;
case 2:
strcpy(string, "data/resources_mordor.dat");
break;
case 3:
strcpy(string, "data/resources_sol.dat");
break;
}
fp = fopen(string, "rb");
if (fp == NULL)
exit(1);
while (fscanf(fp, "%d %s", &index, string) == 2)
{
shipShape[index] = loadImage(string);
}
fclose(fp);
/*
Create images of ships being hit that show a lot of red
*/
for (int i = SHIP_HIT_INDEX ; i < MAX_SHIPSHAPES ; i++)
{
if (shipShape[i - SHIP_HIT_INDEX] == NULL)
continue;
2015-03-01 03:49:06 +01:00
shipShape[i] = createSurface(shipShape[i - SHIP_HIT_INDEX]->w,
2015-04-27 00:49:18 +02:00
shipShape[i - SHIP_HIT_INDEX]->h);
SDL_SetSurfaceBlendMode(shipShape[i - SHIP_HIT_INDEX], SDL_BLENDMODE_NONE);
2015-09-26 14:49:21 +02:00
gfx_blit(shipShape[i - SHIP_HIT_INDEX], 0, 0, shipShape[i]);
SDL_SetSurfaceBlendMode(shipShape[i - SHIP_HIT_INDEX], SDL_BLENDMODE_BLEND);
2015-03-30 13:52:44 +02:00
switch (shipShape[i]->format->BitsPerPixel)
{
2015-03-30 13:52:44 +02:00
case 32:
SDL_LockSurface(shipShape[i]);
2015-03-30 13:52:44 +02:00
p32 = (Uint32 *)shipShape[i]->pixels;
for (int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++)
{
if (p32[j])
p32[j] |= shipShape[i]->format->Rmask;
}
2015-04-27 00:49:18 +02:00
SDL_UnlockSurface(shipShape[i]);
break;
2015-03-30 13:52:44 +02:00
case 16:
SDL_LockSurface(shipShape[i]);
2015-03-30 13:52:44 +02:00
p16 = (Uint16 *)shipShape[i]->pixels;
for (int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++)
{
if (p16[j])
p16[j] |= shipShape[i]->format->Rmask;
}
2015-04-27 00:49:18 +02:00
SDL_UnlockSurface(shipShape[i]);
break;
2015-03-30 13:52:44 +02:00
case 8:
SDL_LockSurface(shipShape[i]);
2015-03-30 13:52:44 +02:00
p8 = (Uint8 *)shipShape[i]->pixels;
for (int j = 0; j < shipShape[i]->w * shipShape[i]->h; j++)
{
if (p8[j])
p8[j] = SDL_MapRGB(shipShape[i]->format, 255, 0, 0);
}
2015-04-27 00:49:18 +02:00
SDL_UnlockSurface(shipShape[i]);
break;
}
SDL_SetColorKey(shipShape[i], SDL_TRUE,
SDL_MapRGB(shipShape[i]->format, 0, 0, 0));
}
strcpy(string, "data/resources_all.dat");
fp = fopen(string, "rb");
while (fscanf(fp, "%d %s", &index, string) == 2)
{
shape[index] = loadImage(string);
}
fclose(fp);
loadBackground(systemBackground[game.system]);
for (int i = 0 ; i < CD_MAX ; i++)
2015-03-07 05:18:31 +01:00
{
if (shipShape[alien_defs[i].imageIndex[0]] != NULL)
2015-03-07 05:18:31 +01:00
{
alien_defs[i].image[0] = shipShape[alien_defs[i].imageIndex[0]];
alien_defs[i].image[1] = shipShape[alien_defs[i].imageIndex[1]];
alien_defs[i].engineX = alien_defs[i].image[0]->w;
alien_defs[i].engineY = (alien_defs[i].image[0]->h / 2);
2015-03-07 05:18:31 +01:00
}
}
setWeaponShapes();
}
/*
Custom loading to alter the font color before doing
all other things
*/
void loadFont()
{
SDL_Surface *image, *newImage;
for (int i = 0 ; i < MAX_FONTSHAPES ; i++)
{
image = IMG_Load("gfx/smallFont.png");
if (image == NULL) {
printf("Couldn't load game font! (%s) Exitting.\n", SDL_GetError());
exit(1);
}
switch(i)
{
case 1:
2013-09-30 16:52:43 +02:00
SDL_SetSurfaceColorMod(image, 255, 0, 0);
break;
case 2:
2013-09-30 16:52:43 +02:00
SDL_SetSurfaceColorMod(image, 255, 255, 0);
break;
case 3:
2013-09-30 16:52:43 +02:00
SDL_SetSurfaceColorMod(image, 0, 255, 0);
break;
case 4:
2013-09-30 16:52:43 +02:00
SDL_SetSurfaceColorMod(image, 0, 255, 255);
break;
case 5:
2013-09-30 16:52:43 +02:00
SDL_SetSurfaceColorMod(image, 0, 0, 10);
break;
}
2013-09-30 16:52:43 +02:00
newImage = SDL_ConvertSurface(image, screen->format, 0);
2015-09-26 14:49:21 +02:00
fontShape[i] = gfx_setTransparent(newImage);
SDL_FreeSurface(image);
}
}