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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include "game.h"
SDL_Rect base::coord;
game::game(void)
{
if(!this->InitSDL()){exit(1);}
BackgroundBox.x = BackgroundBox.y = coord.x = coord.y = 0;
coord.w = BackgroundBox.w = 800;
coord.h = BackgroundBox.h = 600;
fullscreen = false;
Background = Load_Image("Background.png");
Blocks = Load_Image("Blocks.png");
this->LoadMap("test.TILEMAP");
Player = new player;
Player->CurrentMap(map);
this->GameLoop();
}
void game::GameLoop()
{
Uint32 start, end;
running = true;
while(running)
{
start = SDL_GetTicks();
this->HandleEvents();
this->UpdateGame();
this->Render();
end = SDL_GetTicks();
if(1000/FPS > end - start)
{
SDL_Delay(1000/FPS - (end - start));
}
}
}
void game::HandleEvents()
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
keys[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
keys[event.key.keysym.sym] = false;
break;
}
}
}
void game::UpdateGame()
{
Player->SetXvel(0);
Player->SetYvel(0);
if(keys[SDLK_a]){
coord.x-=vel;
BackgroundBox.x-=vel;
Player->SetXvel(-3);
if(BackgroundBox.x <= 0)
{
BackgroundBox.x = Background->w - SCREEN_WIDTH;
}
}
else if(keys[SDLK_d]){
coord.x+=vel;
BackgroundBox.x+=vel;
Player->SetXvel(3);
if(BackgroundBox.x >= Background->w - SCREEN_WIDTH)
{
BackgroundBox.x = 0;
}
}
if(keys[SDLK_w]){
Player->SetYvel(-3);
}else if(keys[SDLK_s]){
Player->SetYvel(3);
}
else if(keys[SDLK_ESCAPE]){
running = false;
}else if(keys[SDLK_F5]){
fullscreen = (!fullscreen);
if(fullscreen)
{
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
}else{
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
}
}
Player->MovePlayer();
}
void game::Render()
{
SDL_BlitSurface(Background,&BackgroundBox,screen,NULL);
this->BlitMap();
Player->Blit(screen);
SDL_Flip(screen);
}
void game::LoadMap(string filename)
{
ifstream in(filename.c_str());
int Width,Height,current;
in >> Width >> Height;
if(!in.is_open())
{
cout << "Error: could not load "<< filename << endl;
return;
}
for(int j = 0;j != Height;j++)
{
vector<int> TempVec;
for(int k = 0; k != Width;k++)
{
if(in.eof())
{
cout << "Error: File end reached to early" << endl;
return;
}
in >> current;
TempVec.push_back(current);
}
map.push_back(TempVec);
}
in.close();
}
void game::BlitMap()
{
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
for(int k = start; k != end; k++)
{
SDL_Rect BlockRect = {(map[j][k]-1)*TILE_SIZE,0,TILE_SIZE,TILE_SIZE};
SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
SDL_BlitSurface(Blocks,&BlockRect,screen,&DestRect);
}
}
}
game::~game(void)
{
delete Player;
SDL_FreeSurface(Blocks);
SDL_FreeSurface(Background);
SDL_Quit();
}
bool game::InitSDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
cout << "Error: could not initialize SDL" << endl;
return false;
}else{
cout << "SDL initialized!" << endl;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
cout << "Error: could not initialize the screen" << endl;
return false;
}else{
cout << "screen initialized!" << endl;
}
if(TTF_Init() == -1)
{
cout << "Error: could not initialize TTF" << endl;
return false;
}else{
cout << "TTF initialized" << endl;
}
for(int i = 0; i != 400; i++)
{
keys[i] = false;
}
return true;
}
| |