I can't figure out how to get my character to collide with my wall. It just goes through it. I currently have no collision commands written. Can anyone help me?
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <windows.h>
using namespace std;
SDL_Surface *screen = NULL;
SDL_Surface *image = NULL;
SDL_Surface *image2 = NULL;
SDL_Surface *back = NULL;
SDL_Rect A;
SDL_Rect B;
SDL_Rect C;
bool init()
{
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());
exit (1);
}
atexit (SDL_Quit);
screen = SDL_SetVideoMode (800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ());
exit (2);
}
SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);
image = SDL_LoadBMP("P1.bmp");
if (!image)
{
printf("*** Error attempting to load file: P1");
exit (3);
}
image2 = SDL_LoadBMP("wall.bmp");
if (!image2)
{
printf("*** Error attempting to load file: wall");
exit (3);
}
back = SDL_LoadBMP("back.bmp");
if (!back)
{
printf("*** Error attempting to load file: back");
exit (3);
}
SDL_EnableKeyRepeat(25,20);
A.x = 0;
A.y = 100;
B.x = 0;
B.y = 0;
C.x = 0;
C.y = 0;
}
void draw()
{
Uint32 colorkey = SDL_MapRGB (image->format, 255, 0, 255);
SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey );
SDL_BlitSurface(back, NULL, screen, &C);
SDL_BlitSurface(image2, NULL, screen, &B);
SDL_BlitSurface(image, NULL, screen, &A);
SDL_Flip (screen);
SDL_Delay (1);
}
int main (int argc, char *argv[])
{
int done = 0;
Uint8 *keys;
init();
while (!done)
{
SDL_Event event;
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) {A.y--;}
if ( keys[SDLK_DOWN] ) {A.y++;}
if ( keys[SDLK_LEFT] ) {A.x--;}
if ( keys[SDLK_RIGHT] ) {A.x++;}
if ( keys[SDLK_ESCAPE] ) { done = 1;}
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
draw ();
}
return 0;
}