tbftss/src/battle/effects.c

691 lines
13 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2018-04-29 11:01:09 +02:00
Copyright (C) 2015-2018 Parallel Realities
2015-10-20 13:51:49 +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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "effects.h"
static void setRandomFlameHue(Effect *e);
static void setRandomShieldHue(Effect *e);
2016-02-25 00:12:13 +01:00
static void resizeDrawList(void);
2016-02-25 17:43:26 +01:00
static int pointOnScreen(float x, float y);
static SDL_Texture *explosionTexture;
static SDL_Texture *shieldHitTexture;
2015-11-29 09:34:25 +01:00
static SDL_Texture *haloTexture;
static Effect **effectsToDraw;
2016-02-25 00:12:13 +01:00
static int drawCapacity;
void initEffects(void)
{
2015-11-29 09:34:25 +01:00
explosionTexture = getTexture("gfx/effects/explosion.png");
shieldHitTexture = getTexture("gfx/effects/shieldHit.png");
haloTexture = getTexture("gfx/effects/halo.png");
2016-02-25 00:12:13 +01:00
drawCapacity = INITIAL_EFFECT_DRAW_CAPACITY;
effectsToDraw = malloc(sizeof(Effect*) * drawCapacity);
2016-02-25 00:12:13 +01:00
memset(effectsToDraw, 0, sizeof(Effect*) * drawCapacity);
}
2015-10-20 13:51:49 +02:00
void doEffects(void)
{
2016-02-25 17:43:26 +01:00
int i, onScreen;
2015-10-20 13:51:49 +02:00
Effect *e;
Effect *prev = &battle.effectHead;
2016-02-25 00:12:13 +01:00
i = 0;
memset(effectsToDraw, 0, sizeof(Effect*) * drawCapacity);
2015-10-20 13:51:49 +02:00
for (e = battle.effectHead.next ; e != NULL ; e = e->next)
{
e->x += e->dx;
e->y += e->dy;
e->a -= (e->type != EFFECT_ECM) ? 1 : 3;
e->a = MAX(0, e->a);
2015-10-20 13:51:49 +02:00
e->health--;
2015-11-29 09:34:25 +01:00
e->size += e->scaleAmount;
2015-10-20 13:51:49 +02:00
if (e->health <= 0)
{
if (e == battle.effectTail)
2015-10-20 13:51:49 +02:00
{
battle.effectTail = prev;
2015-10-20 13:51:49 +02:00
}
prev->next = e->next;
free(e);
e = prev;
2015-10-20 13:51:49 +02:00
}
else
{
2016-02-25 17:43:26 +01:00
onScreen = 0;
switch (e->type)
{
case EFFECT_LINE:
onScreen = pointOnScreen(e->x - battle.camera.x, e->y - battle.camera.y) || pointOnScreen(e->x + (e->dx * 3) - battle.camera.x, e->y + (e->dy * 3) - battle.camera.y);
break;
case EFFECT_POINT:
onScreen = pointOnScreen(e->x - battle.camera.x, e->y - battle.camera.y);
break;
default:
2016-04-02 17:37:49 +02:00
onScreen = isOnBattleScreen(e->x, e->y, e->size, e->size);
2016-02-25 17:43:26 +01:00
break;
}
2016-03-10 12:56:27 +01:00
if (onScreen)
{
effectsToDraw[i++] = e;
if (i == drawCapacity)
{
resizeDrawList();
}
}
}
2015-10-20 13:51:49 +02:00
prev = e;
}
}
2016-02-25 17:43:26 +01:00
static int pointOnScreen(float x, float y)
{
return collision(x, y, 1, 1, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
static void resizeDrawList(void)
{
int n;
n = drawCapacity + INITIAL_EFFECT_DRAW_CAPACITY;
2016-04-04 12:25:09 +02:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing effect draw capacity: %d -> %d", drawCapacity, n);
effectsToDraw = resize(effectsToDraw, sizeof(Effect*) * drawCapacity, sizeof(Effect*) * n);
drawCapacity = n;
}
2015-10-20 13:51:49 +02:00
void drawEffects(void)
{
int i;
2015-10-20 13:51:49 +02:00
Effect *e;
2015-10-20 13:51:49 +02:00
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
for (i = 0, e = effectsToDraw[i] ; e != NULL ; e = effectsToDraw[++i])
2015-10-20 13:51:49 +02:00
{
SDL_SetRenderDrawColor(app.renderer, e->r, e->g, e->b, e->a);
2015-10-20 13:51:49 +02:00
SDL_SetTextureBlendMode(e->texture, SDL_BLENDMODE_ADD);
SDL_SetTextureAlphaMod(e->texture, e->a);
2015-10-20 13:51:49 +02:00
switch (e->type)
{
case EFFECT_POINT:
SDL_RenderDrawPoint(app.renderer, e->x - battle.camera.x, e->y - battle.camera.y);
break;
2015-10-20 13:51:49 +02:00
case EFFECT_LINE:
SDL_RenderDrawLine(app.renderer, e->x - battle.camera.x, e->y - battle.camera.y, e->x + (e->dx * 3) - battle.camera.x, e->y + (e->dy * 3) - battle.camera.y);
2015-10-20 13:51:49 +02:00
break;
2015-10-20 13:51:49 +02:00
case EFFECT_TEXTURE:
SDL_SetTextureColorMod(e->texture, e->r, e->g, e->b);
blitScaled(e->texture, e->x - battle.camera.x, e->y - battle.camera.y, e->size, e->size, 0);
2015-10-20 13:51:49 +02:00
break;
2015-10-20 13:51:49 +02:00
case EFFECT_HALO:
2015-11-29 09:34:25 +01:00
SDL_SetTextureColorMod(e->texture, e->r, e->g, e->b);
blitScaled(e->texture, e->x - battle.camera.x - (e->size / 2), e->y - battle.camera.y - (e->size / 2), e->size, e->size, 0);
2015-11-29 09:34:25 +01:00
break;
2015-11-29 09:34:25 +01:00
case EFFECT_ECM:
SDL_SetTextureColorMod(e->texture, e->r, e->g, e->b);
blitScaled(e->texture, SCREEN_WIDTH / 2 - (e->size / 2), SCREEN_HEIGHT / 2 - (e->size / 2), e->size, e->size, 0);
2015-10-20 13:51:49 +02:00
break;
}
}
2015-10-20 13:51:49 +02:00
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
}
2015-11-22 17:51:11 +01:00
void drawShieldHitEffect(Entity *e)
{
int size = MAX(e->w, e->h) + 32;
2015-11-22 17:51:11 +01:00
SDL_SetTextureBlendMode(shieldHitTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureAlphaMod(shieldHitTexture, e->shieldHit);
blitScaled(shieldHitTexture, e->x - battle.camera.x, e->y - battle.camera.y, size, size, 1);
2015-11-22 17:51:11 +01:00
}
void addBulletHitEffect(int x, int y, int r, int g, int b)
{
Effect *e;
int i;
for (i = 0 ; i < 4 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_TEXTURE;
e->texture = explosionTexture;
e->size = 16;
e->x = x;
e->y = y;
e->dx = (rand() % 25) - (rand() % 25);
e->dx *= 0.01;
e->dy = (rand() % 25) - (rand() % 25);
e->dy *= 0.01;
e->r = r;
e->g = g;
e->b = b;
e->a = 64;
e->health = e->a;
}
}
2015-10-20 13:51:49 +02:00
void addSmallFighterExplosion(void)
{
Effect *e;
2015-10-20 13:51:49 +02:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-10-20 13:51:49 +02:00
e->type = EFFECT_TEXTURE;
2015-10-20 13:51:49 +02:00
e->x = self->x + (rand() % 16 - rand() % 16);
e->y = self->y + (rand() % 16 - rand() % 16);
e->texture = explosionTexture;
2015-10-20 13:51:49 +02:00
e->size = 32;
2015-10-20 13:51:49 +02:00
setRandomFlameHue(e);
2015-10-20 13:51:49 +02:00
e->a = 128 + (rand() % 128);
e->health = e->a;
2015-10-20 13:51:49 +02:00
e->x -= e->size / 2;
e->y -= e->size / 2;
}
2015-12-13 15:50:54 +01:00
void addDebrisFire(int x, int y)
{
Effect *e;
2015-12-13 15:50:54 +01:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-12-13 15:50:54 +01:00
e->type = EFFECT_TEXTURE;
2015-12-13 15:50:54 +01:00
e->x = x + (rand() % 8 - rand() % 8);
e->y = y + (rand() % 8 - rand() % 8);
e->texture = explosionTexture;
2015-12-16 23:40:26 +01:00
e->size = 4 + rand() % 12;
2015-12-13 15:50:54 +01:00
setRandomFlameHue(e);
2015-12-13 15:50:54 +01:00
e->a = rand() % 256;
e->health = e->a;
2015-12-13 15:50:54 +01:00
e->x -= e->size / 2;
e->y -= e->size / 2;
}
void addSmallExplosion(void)
2015-10-20 13:51:49 +02:00
{
int i;
Effect *e;
2015-10-20 13:51:49 +02:00
for (i = 0 ; i < 32 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-10-20 13:51:49 +02:00
e->type = EFFECT_TEXTURE;
2015-10-20 13:51:49 +02:00
e->x = self->x;
e->y = self->y;
e->dx = (rand() % 25) - (rand() % 25);
e->dx *= 0.025;
e->dy = (rand() % 25) - (rand() % 25);
e->dy *= 0.025;
e->texture = explosionTexture;
2015-10-20 13:51:49 +02:00
e->size = 32 + (rand() % 64);
e->r = 255;
2015-10-20 13:51:49 +02:00
setRandomFlameHue(e);
2015-10-20 13:51:49 +02:00
e->a = 128 + (rand() % 128);
e->health = e->a;
2015-10-20 13:51:49 +02:00
e->x -= e->size / 2;
e->y -= e->size / 2;
}
2015-10-20 13:51:49 +02:00
for (i = 0 ; i < 96 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-10-20 13:51:49 +02:00
e->type = EFFECT_LINE;
e->x = self->x;
e->y = self->y;
e->dx = rand() % 64 - rand() % 64;
e->dx *= 0.1;
e->dy = rand() % 64 - rand() % 64;
e->dy *= 0.1;
2015-10-20 13:51:49 +02:00
e->a = 128;
e->health = e->a;
2015-10-20 13:51:49 +02:00
setRandomFlameHue(e);
}
}
2016-03-30 08:22:58 +02:00
void addMineExplosion(void)
{
int i;
Effect *e;
for (i = 0 ; i < 64 ; i++)
2016-03-30 08:22:58 +02:00
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_TEXTURE;
e->x = self->x + rand() % 64 - rand() % 64;
e->y = self->y + rand() % 64 - rand() % 64;
2016-03-30 08:22:58 +02:00
e->texture = explosionTexture;
e->size = 64 + (rand() % 64);
2016-03-30 08:22:58 +02:00
e->r = 255;
setRandomFlameHue(e);
e->a = 32 + (rand() % 192);
e->health = e->a;
e->x -= e->size / 2;
e->y -= e->size / 2;
}
2016-04-01 11:49:41 +02:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_HALO;
e->x = self->x;
e->y = self->y;
e->size = 64;
e->scaleAmount = 6;
2016-04-01 11:49:41 +02:00
e->texture = haloTexture;
e->r = 255;
e->g = 255;
e->b = 255;
e->a = 128;
e->health = 128;
2016-03-30 08:22:58 +02:00
}
void addLargeExplosion(void)
{
int i;
Effect *e;
for (i = 0 ; i < 64 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_TEXTURE;
e->x = self->x + rand() % 255 - rand() % 255;
e->y = self->y + rand() % 255 - rand() % 255;
e->dx = (rand() % 25) - (rand() % 25);
e->dx *= 0.01;
e->dy = (rand() % 25) - (rand() % 25);
e->dy *= 0.01;
e->texture = explosionTexture;
e->size = 128 + (rand() % 512);
e->r = 255;
setRandomFlameHue(e);
e->a = 128 + (rand() % 128);
e->health = e->a;
e->x -= e->size / 2;
e->y -= e->size / 2;
}
2016-04-05 13:21:17 +02:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_HALO;
e->x = self->x;
e->y = self->y;
e->size = 256;
e->scaleAmount = 4;
e->texture = haloTexture;
e->r = 255;
e->g = 255;
e->b = 255;
e->a = 255;
e->health = 255;
}
void addMissileExplosion(Bullet *b)
{
int i;
Effect *e;
for (i = 0 ; i < 8 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_TEXTURE;
e->x = b->x;
e->y = b->y;
e->dx = (rand() % 25) - (rand() % 25);
e->dx *= 0.025;
e->dy = (rand() % 25) - (rand() % 25);
e->dy *= 0.025;
e->texture = explosionTexture;
e->size = 32 + (rand() % 64);
e->r = 255;
setRandomFlameHue(e);
e->a = 128 + (rand() % 128);
e->health = e->a;
e->x -= e->size / 2;
e->y -= e->size / 2;
}
for (i = 0 ; i < 24 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_LINE;
e->x = b->x;
e->y = b->y;
e->dx = rand() % 64 - rand() % 64;
e->dx *= 0.1;
e->dy = rand() % 64 - rand() % 64;
e->dy *= 0.1;
e->a = 128;
e->health = e->a;
setRandomFlameHue(e);
}
}
2015-10-20 13:51:49 +02:00
void addEngineEffect(void)
{
Effect *e;
2015-12-24 12:54:01 +01:00
float c, s;
int h;
2015-10-20 13:51:49 +02:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-10-20 13:51:49 +02:00
e->type = EFFECT_TEXTURE;
2015-12-24 12:54:01 +01:00
s = sin(TO_RAIDANS(self->angle));
c = cos(TO_RAIDANS(self->angle));
2015-12-24 12:54:01 +01:00
h = self->h / 2;
2015-12-24 12:54:01 +01:00
e->x = -(h * s) + self->x;
e->y = (h * c) + self->y;
2015-12-24 12:54:01 +01:00
e->x += rand() % 4 - rand() % 4;
e->texture = explosionTexture;
2015-10-20 13:51:49 +02:00
e->size = 16;
e->r = 128;
e->g = 128;
e->b = 255;
e->a = 64;
e->health = e->a;
2015-10-20 13:51:49 +02:00
e->x -= e->size / 2;
e->y -= e->size / 2;
}
2015-12-12 16:11:40 +01:00
void addLargeEngineEffect(void)
{
Effect *e;
2015-12-12 16:11:40 +01:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-12-12 16:11:40 +01:00
e->type = EFFECT_TEXTURE;
2015-12-12 16:11:40 +01:00
e->x = self->x;
e->y = self->y;
2015-12-12 16:11:40 +01:00
e->x -= sin(TO_RAIDANS(self->angle)) * 16;
e->y -= -cos(TO_RAIDANS(self->angle)) * 16;
2015-12-12 16:11:40 +01:00
e->x += rand() % 4;
e->x -= rand() % 4;
2015-12-12 16:11:40 +01:00
e->texture = explosionTexture;
e->size = 64;
e->r = 128;
e->g = 128;
e->b = 255;
e->a = 64;
e->health = e->a;
2015-12-12 16:11:40 +01:00
e->x -= e->size / 2;
e->y -= e->size / 2;
}
2015-10-20 13:51:49 +02:00
void addMissileEngineEffect(Bullet *b)
{
Effect *e;
2015-10-20 13:51:49 +02:00
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-10-20 13:51:49 +02:00
e->type = EFFECT_TEXTURE;
2015-10-20 13:51:49 +02:00
e->x = b->x;
e->y = b->y;
2015-10-20 13:51:49 +02:00
e->x -= sin(TO_RAIDANS(b->angle)) * 10;
e->y -= -cos(TO_RAIDANS(b->angle)) * 10;
2015-10-20 13:51:49 +02:00
e->x += rand() % 4;
e->x -= rand() % 4;
e->texture = explosionTexture;
2015-10-20 13:51:49 +02:00
e->size = 12;
setRandomFlameHue(e);
e->a = 128;
e->health = e->a;
2015-10-20 13:51:49 +02:00
e->x -= e->size / 2;
e->y -= e->size / 2;
}
void addShieldSplinterEffect(Entity *ent)
{
int i;
Effect *e;
for (i = 0 ; i < 48 ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
e->type = EFFECT_LINE;
e->x = ent->x;
e->y = ent->y;
e->dx = rand() % 64 - rand() % 64;
e->dx *= 0.1;
e->dy = rand() % 64 - rand() % 64;
e->dy *= 0.1;
e->a = 255;
2016-03-16 22:54:08 +01:00
e->health = e->a;
setRandomShieldHue(e);
}
}
2015-11-29 09:34:25 +01:00
void addECMEffect(Entity *ent)
{
int i;
Effect *e;
2015-11-29 09:34:25 +01:00
for (i = 0 ; i < 3 ; i++)
{
e = malloc(sizeof(Effect));
2015-11-29 09:34:25 +01:00
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
2015-11-29 09:34:25 +01:00
e->type = EFFECT_ECM;
e->x = ent->x;
e->y = ent->y;
e->size = i * 4;
e->scaleAmount = 5;
e->texture = haloTexture;
2015-11-29 09:34:25 +01:00
e->r = 128;
e->g = 128 + (i * 64);
e->b = 255;
e->a = 255;
2016-02-15 16:23:51 +01:00
e->health = 255;
2015-11-29 09:34:25 +01:00
}
}
static void setRandomFlameHue(Effect *e)
{
e->r = 255;
switch (rand() % 4)
{
case 0:
break;
case 1:
e->g = 128;
break;
case 2:
e->g = 255;
break;
case 3:
e->g = e->b = 255;
break;
}
}
static void setRandomShieldHue(Effect *e)
{
e->b = 255;
switch (rand() % 4)
{
case 0:
e->g = 128;
break;
case 1:
e->g = 196;
break;
case 2:
e->g = 255;
break;
case 3:
e->r = e->g = 255;
break;
}
}
void destroyEffects(void)
{
2016-05-15 09:19:26 +02:00
if (effectsToDraw)
{
free(effectsToDraw);
}
effectsToDraw = NULL;
}