starfighter/code/graphics.cpp

160 lines
3.5 KiB
C++

/*
Copyright (C) 2003 Parallel Realities
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 "Starfighter.h"
Graphics graphics;
Star star[200];
bool collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1)
{
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
bool collision(object *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image[0]->w;
float h0 = object1->image[0]->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
bool collision(collectables *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image->w;
float h0 = object1->image->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
SDL_Surface *loadImage(const char *filename)
{
SDL_Surface *image, *newImage;
#if USEPACK
unpack(filename, PAK_IMG);
image = IMG_Load_RW(engine.sdlrw, 1);
#else
image = IMG_Load(filename);
#endif
if (image == NULL) {
printf("Couldn't load %s: %s\n", filename, SDL_GetError());
showErrorAndExit(0, filename);
}
newImage = SDL_DisplayFormat(image);
if ( newImage ) {
SDL_FreeSurface(image);
} else {
// This happens when we are loading the window icon image
newImage = image;
}
return graphics.setTransparent(newImage);
}
/*
Simply draws the stars in their positions on screen and moves
them around. They are wrapped around using the wrapFloat()
function, as defined above, and putpixel as defined in graphics.cpp
*/
void doStarfield()
{
/* Lock the screen for direct access to the pixels */
if (SDL_MUSTLOCK(graphics.screen))
{
if (SDL_LockSurface(graphics.screen) < 0 )
{
showErrorAndExit(2, "");
}
}
int color = 0;
SDL_Rect r;
for (int i = 0 ; i < 200 ; i++)
{
if (star[i].speed == 3)
color = graphics.white;
else if (star[i].speed == 2)
color = graphics.lightGrey;
else if (star[i].speed == 1)
color = graphics.darkGrey;
wrapFloat(&(star[i].x += (engine.ssx * star[i].speed)), 0, 799);
wrapFloat(&(star[i].y += (engine.ssy * star[i].speed)), 0, 599);
graphics.putpixel(graphics.screen, (int)star[i].x, (int)star[i].y, color);
r.x = (int)star[i].x;
r.y = (int)star[i].y;
r.w = 1;
r.h = 1;
graphics.addBuffer(r.x, r.y, r.w, r.h);
}
if (SDL_MUSTLOCK(graphics.screen))
{
SDL_UnlockSurface(graphics.screen);
}
}
int isOnScreen(int x, int y, int w, int h)
{
if ((x + w > 0) && (x < 800) && (y + h > 0) && (y < 600))
return 1;
return 0;
}