I'm in a little trouble with a tile collision engine. It seem something in my logic are wrong but i can't figure it out...
This is the (horrible) code i wrote in C
- {l Code}: {l Select All Code}
//gcc -o maze maze.c -lSDL
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <time.h>
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 320
#define MAX_MAP_X 10
#define MAX_MAP_Y 10
#define BLANK_TILE 0
#define TILE_SIZE 32
#define PLAYER_SPEED 4
#define TIME_STOP 2000
//Global variables
int t, tl = 0, frequency = 1000 / 100, temp, t2; //StopBall variables
int repeat, i, quit=0, paused=0, shoot=0;
SDL_Event event;
SDL_Surface *screen, *textSurface;
SDL_Rect rect; //Main drawing surface
Uint8 *keystate; // keyboard state
int matrice[10][10] =
{
0,0,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,
};
struct _Player
{
int w, h, onGround;
int thinkTime;
int x, y, dirX, dirY;
} Player;
struct _Character {
int x,y;
int dir;
int delay;
} Character;
struct _Game
{
int player_score, cpu_score, status, players, difficult;
} Game;
void restartTimer()
{
//Reset frame per screen counter
t = tl = temp = SDL_GetTicks();
}
int Reset()
{
////Player start position
//Player.x = SCREEN_WIDTH/2;
//Player.y = SCREEN_HEIGHT/2;
}
void checkToMap(struct _Player *e);
void DrawRect(int x, int y, int width, int height, int color)
{
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
SDL_FillRect(screen, &rect, color);
}
void drawbackground()
{
int i, j;
for (i = 0; i <= 10; i++)
{
for (j = 0; j <= 10; j++)
{
if(matrice[i][j]==0) DrawRect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE, 0xffffff);
}
}
}
void Draw()
{
//Draw Maze
drawbackground();
//Draw Player
DrawRect(Player.x, Player.y, TILE_SIZE, TILE_SIZE, 0xff0000);
}
int getInput()
{
int x, y;
x = Player.x;
y = Player.y;
x/=TILE_SIZE;
y/=TILE_SIZE;
// Grab a keystate snapshot
keystate = SDL_GetKeyState( NULL );
/* Handle key presses */
//The paddles can move only if there's nothing on their way
if (keystate[SDLK_LEFT])
{
if(matrice[x-1][y]!=1)
Player.x--;
printf("x %d y %d\n", x, y);
printf("%d \n", matrice[x-1][y]);
}
if (keystate[SDLK_RIGHT])
{
if(matrice[x+1][y]!=1)
{
Player.x++;
printf("x %d y %d\n", x, y);
}
}
if (keystate[SDLK_UP])
{
if(matrice[x][y-1]==0)
Player.y--;
printf("x %d y %d\n", x, y);
printf("%d \n", matrice[x-1][y]);
}
if (keystate[SDLK_DOWN])
{
if(matrice[x][y+1]==0)
Player.y++;
printf("x %d y %d\n", x, y);
printf("%d \n", matrice[x-1][y]);
}
}
int fps_sync ()
{
t = SDL_GetTicks ();
//printf("tl: %d t: %d\n", tl, t);
if (t - tl >= frequency)
{
temp = (t - tl) / frequency; //delta time
tl += temp * frequency;
//printf("temp: %d tl: %d \n", t, tl);
return temp;
}
else
{
SDL_Delay (frequency - (t - tl));
tl += frequency;
return 1;
}
}
void logicLoop()
{
if(!paused)
getInput();
}
int mainLoop()
{
//main game loop
while(!quit)
{
SDL_PollEvent(&event);
repeat = fps_sync ();
for (i = 0; i < repeat; i ++)
{
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE))
quit = 1; /* Window closed */
switch(Game.status)
{
case 0: //Menu
logicLoop();
break;
}
}
//clear the screen
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
//draw to the screen
Draw();
//update the screen
SDL_Flip(screen);
}
}
int main()
{
// init video stuff
// init screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
if(screen == NULL)
{
fprintf(stderr, "Can't initialize SDL: %s\n", SDL_GetError());
exit(-1);
}
SDL_WM_SetCaption("Maze", "Maze");
//Game status 0 means Game Menu
Game.status=0;
Game.players=1;
Game.difficult=1;
Reset();
printf("Start position in matrix: x %d y %d\n", Player.x/32, Player.y/32);
Player.x = Player.x * TILE_SIZE;
Player.y = Player.y * TILE_SIZE;
//main game loop
mainLoop();
printf("Thanks for playing!\n");
SDL_Quit();
return 0;
}
Can someone help me or just let me know if there is any C based tutorial on tile collision for games?
Sorry for my bad english ^^"