Man... you have to focus on making your code as short as possible...
Firstly for the love of god remove the C's that you put infront of your filenames and class names...
You don't need 2 functions for vertical and horizontal collisions...
I recommend using the "SDL_Rect" way, and for entities you use lazyfoo's collision function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
bool collision( SDL_Rect a, SDL_Rect b )
{
int leftA = a.x, leftB = b.x;
int rightA = a.x + a.w, rightB = b.x + b.w;
int topA = a.y, topB = b.y;
int bottomA = a.y + a.h, bottomB = b.y + b.h;
if( bottomA <= topB )
return false;
else if( topA >= bottomB )
return false;
else if( rightA <= leftB )
return false;
else if( leftA >= rightB )
return false;
return true;
}
|
And for tile collisions, create the function I showed you above, with x&y, and takes the arguments:
bool tile_collision(SDL_Rect a,int tiles[][1000])
, also make your tiles not pointers, in c++ there is no point of using pointers unless your using dynamic memory(don't), or using polymorphism.
If you think that you can't make grass move because of using the int, you can create a last_x, & last_y in your player, do in player's loop:
if(xpos/tile_size != last_x || ypos/tile_size != last_y){last_x=xpos/tile_size;//then y...
, then if the tile is grass, I would insert a "moving_grass_entity" infront of the entity list ontop of that tile that only lasts a sec.
You should first change your game into a state-full system.
Essentially use polymorphism, make an abstract class like this:
1 2 3 4 5 6 7 8
|
class State
{
public:
virtual void Input() = 0;
virtual void Logic() = 0;
virtual void Render() = 0;
virtual ~State(){};
};
|
Then make 2 classes called StartScreen and Game, beside the class's name, do
: public State
, then make sure that it has the 3 functions.
Make an "enum class" called States with "STATE_STARTSCREEN", and "STATE_GAME", and "STATE_NULL".
Make a global variable(in a header like global.h) called
State* CurrentState = STATE_NULL;
Then in main, before the loop do:
CurrentState = new StartScreen();
(and to be neat remember to delete current state after the main loop.)
Now you can use current state in your loop.
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
|
//new starscreen state
while(!quit)
{
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
CurrentState->Input(); //like over here
}
//Clear screen
SDL_SetRenderDrawColor( Renderer, 0, 0, 0, 255 ); //clear with black
SDL_RenderClear( Renderer );
CurrentState->Main(); //or over here
CurrentState->Render(); //and over here
ChangeState(); //I'll explain
//Update Screen
SDL_RenderPresent( Renderer );
SDL_Delay(5); //to save cpu
}
}
//delete current state
|
Now for ChangeState();
Make another global variable
States Next_State = States::STATE_NULL;
.
ChangeState basically checks if next_state is not null, if it isnt it changes the state.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void ChangeState()
{
if(nextState != STATE_NULL)
{
delete CurrentState;
switch(nextState)
{
case STATE_STARTSCREEN:
nextState = STATE_NULL;
CurrentState = new StartScreen();
break;
case STATE_GAME:
nextState = STATE_NULL;
CurrentState = new Game();
break;
}
}
}
|
And now instead of having a class called player, you can just put the variables into Game's class, and in it is the
int tileset[1000][1000];
,your map isnt 1000 tiles big, its just the buffer(if you think your map could be bigger, increase it), you will have a global variable called MAP_WIDTH & MAP_HEIGHT that are defined when you read your map(the first 2 numbers in my file define mine).
And then you will have a vector of entities with polymorphism, with "think" or "logic", and "render" or "draw". The abstract class will also require
int type, x, y;
, and logic requires the parameters of the entities, in reference, and tiles, maybe reference(if the map is not modifiable). Render should also have a parameter of a rect called camera, if you pan over your character.
Also if you need a working example of states here is lazyfoos:
http://lazyfoo.net/articles/article06/index.php