1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
#include "SDL_image.h"
#include "SDL_rotozoom.h"
#include "player.h"
int Num_Players = 4;
SDL_Surface *Screen = NULL;
SDL_Surface *Ships = NULL;
SDL_Surface *Load_Image (std::string FileName);
SDL_Surface *Optimize (SDL_Surface *Original);
void Apply_Surface (int x, int y, SDL_Surface *Source, SDL_Surface *Destination, SDL_Rect *Clip);
bool Init();
bool Load_Files();
int rnd(int Min, int Max);
float dist(int x1, int y1, int x2, int y2);
int main (int argc, char* args[])
{
bool Quit_Program = false;
if (Init() == false)
{
return 1;
}
if (Load_Files() == false)
{
return 2;
}
Player play[4];
for (int i = 0; i < 4; i++)
{
play[i].ID = i;
}
while (Quit_Program == false)
{
for (int i = 0 ; i < Num_Players; i++)
{
play[i].update(play);
Apply_Surface(play[i].screenX, play[i].screenY, play[i].PlayScreen, Screen);
}
if (SDL_Flip (Screen) == -1)
{
return 1;
}
}
return 0;
}
bool Init()
{
if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
{
return false;
}
Screen = SDL_SetVideoMode (800, 600, 32, SDL_SWSURFACE );
if (Screen == NULL )
{
return false;
}
return true;
}
bool Load_Files()
{
Ships = Load_Image ("./graphics/ships.png");
if (Ships == NULL)
{
return false;
}
return true;
}
SDL_Surface *Load_Image (std::string FileName)
{
SDL_Surface* LoadedImage = NULL;
SDL_Surface* OptimizedImage = NULL;
LoadedImage = IMG_Load (FileName.c_str());
if (LoadedImage != NULL)
{
OptimizedImage = SDL_DisplayFormat (LoadedImage);
SDL_FreeSurface (LoadedImage);
if (OptimizedImage != NULL)
{
SDL_SetColorKey (OptimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB (OptimizedImage->format, 0, 0, 0));
}
}
return OptimizedImage;
}
void Apply_Surface (int x, int y, SDL_Surface* Source, SDL_Surface* Destination, SDL_Rect* Clip)
{
SDL_Rect Offset;
Offset.x = x;
Offset.y = y;
SDL_BlitSurface (Source, Clip, Destination, &Offset );
}
int rnd(int Min, int Max)
{
return ((double)rand() / ((double)RAND_MAX + 1.0)) * (Max - Min + 1) + Min;
}
float dist(int x1, int y1, int x2, int y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
SDL_Surface *Get_Ship(int which, float zoom)
{
//Size of ships in original sprite sheet.
int Ship_Width = 500;
int Ship_Height = 315;
int Num_Ships = Ships->h / Ship_Height;
if ((which + 1) > Num_Ships)
{
which = (Num_Ships - 1);
}
SDL_Surface *original = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, Ship_Width, Ship_Height, Screen_BPP, 0, 0, 0, 0);
SDL_Surface *finished = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, (int)(Ship_Width * zoom), (int)(Ship_Height * zoom), Screen_BPP, 0, 0, 0, 0);
SDL_Rect ship_box;
ship_box.x = 0;
ship_box.y = which * Ship_Height;
ship_box.w = Ship_Width;
ship_box.h = Ship_Height;
Apply_Surface(0, 0, Ships, original, &ship_box);
finished = zoomSurface(original, zoom, zoom, SMOOTHING_ON);
SDL_FreeSurface(original);
return finished;
}
|