Thanks in advice. I will send all the code i think is necessary and just reply if you need more.
If there is anything else that looks strange just tell me, i want to learn as much as possible.
Here is game.h
#include "base.h"
#include "player.h"
#ifndef GAME_H
#define GAME_H
class game : public base
{
public:
game(void);
~game(void);
private:
bool InitSDL();
void GameLoop();
void HandleEvents();
void UpdateGame();
void Render();
void LoadMap(string filename);
void BlitMap();
bool running;
bool fullscreen;
SDL_Event event;
bool keys[400];
player * Player;
SDL_Surface * screen,*Background,*Blocks;
SDL_Rect BackgroundBox;
vector< vector<int> > map;
};
#endif
Here is game.cpp
#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;
}
Here is player.h
#include "base.h"
#ifndef PLAYER_H
#define PLAYER_H
class player : public base
{
public:
player(void);
void SetXvel(int vel);
void SetYvel(int vel);
void CurrentMap(vector<vector<int> > Map);
void MovePlayer();
void Blit(SDL_Surface * screen);
~player(void);
private:
vector< vector<int> > map;
SDL_Surface * image;
SDL_Rect box;
int Xvel,Yvel;
bool Ground;
};
#endif
here is player.cpp
#include "player.h"
player::player(void):
Yvel(3),
Xvel(5)
{
image = Load_Image("Player.png");
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,200,0,200));
box.w = image->w;
box.h = image->h;
box.x = SCREEN_WIDTH/2 - box.w/2;
box.y = SCREEN_HEIGHT/2;
Ground = false;
}
void player::SetXvel(int vel)
{
Xvel = vel;
}
void player::SetYvel(int vel)
{
Yvel = vel;
}
void player::MovePlayer()
{
box.x += Xvel;
box.y += Yvel;
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++)
{
if(map[j][k] == 0)
{
continue;
}
SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
if(CheckCollision(box,DestRect))
{
if(box.x+box.w >= DestRect.x)
{
box.x-=Xvel;
}
else if(box.x <= DestRect.x + DestRect.w)
{
box.x-=Xvel;
}
if(box.y+box.h >= DestRect.y)
{
box.y-=Yvel;
}
else if(box.y <= DestRect.y + DestRect.h)
{
box.y-=Yvel;
}
}
}
}
}
void player::CurrentMap(vector<vector<int> > Map)
{
map = Map;
}
void player::Blit(SDL_Surface * screen)
{
SDL_BlitSurface(image,NULL,screen,&box);
}
player::~player(void)
{
SDL_FreeSurface(image);
}
Here is a class which contains the most basic stuffs that all classes need. its called base
Here is base.h
#ifndef BASE_H
#define BASE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
using namespace std;
class base
{
public:
static const int SCREEN_WIDTH = 800;
static const int SCREEN_HEIGHT = 600;
static const int SCREEN_BPP = 32;
static const int FPS = 60;
static const int Gravity = 2;
static const int vel = 2;
static const int TILE_SIZE = 50;
static SDL_Rect coord;
base(void);
SDL_Surface * Load_Image(string filename);
bool CheckCollision(SDL_Rect r1,SDL_Rect r2);
~base(void);
};
#endif
Here is base.cpp
#include "base.h"
base::base(void)
{
}
SDL_Surface * base::Load_Image(string filename)
{
SDL_Surface * IMG = IMG_Load(filename.c_str());
if(IMG != NULL)
{
SDL_Surface * Opt = SDL_DisplayFormat(IMG);
SDL_FreeSurface(IMG);
cout << "Loading image " << filename << " Succeeded!" << endl;
return Opt;
}else{
cout << "Error: image " << filename << " could not be loaded" << endl;
return NULL;
}
}
bool base::CheckCollision(SDL_Rect r1,SDL_Rect r2)
{
if(r1.x > r2.x+r2.w){
return false;
}
else if(r1.y > r2.y+r2.h){
return false;
}
else if(r1.x+r1.w < r2.x){
return false;
}
else if(r1.y+r1.h < r2.y){
return false;
}else{
return true;
}
}
base::~base(void)
{
}
Edited by stoffe1100, 02 June 2012 - 08:37 AM.






