Hi!
The SDL_Rect ene2C, playerC won't move.
Here's is the code!
#include "sdl\sdl.h"
#include "iostream"
#undef main
const int WIN_W = 640;
const int WIN_H = 480;
const char* WIN_T = " Sentoki's quest ";
bool collision(SDL_Rect* playerC, SDL_Rect* ene2C)
{
if(playerC->y >= ene2C->y + ene2C->h)
return 0;
if(playerC->x >= ene2C->x + ene2C->w)
return 0;
if(playerC->y + playerC->h <= ene2C->y)
return 0;
if(playerC->x + playerC->w <= ene2C->x)
return 0;
return 1;
}
void drawSprite(SDL_Surface* imageSurface,
SDL_Surface* screenSurface,
int srcX, int srcY,
int dstX, int dstY,
int width, int height);
using namespace std;
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface* screen = SDL_SetVideoMode(WIN_W,WIN_H, 0, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
SDL_WM_SetCaption(WIN_T, 0);
SDL_Surface* bitmap = SDL_LoadBMP("sprite.bmp");
SDL_Surface* ene2 = NULL;
SDL_Surface* back = SDL_LoadBMP("back.bmp");
SDL_Surface* bul = NULL;
SDL_Surface* gui1 = SDL_LoadBMP("GUI1.bmp");
SDL_Surface* fore = SDL_LoadBMP("fore.bmp");
SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));
SDL_SetColorKey(gui1, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));
int playerX = 300;
int playerY = 200;
int playerW = 32;
int playerH = 46;
int playerImx = 9;
int playerImy = 0;
int bulX = 10000;
int bulY = 10000;
int bulW = 57;
int bulH = 36;
int bulImx = 40;
int bulImy = 80;
int backX = 0;
int backY = 0;
int backW = 1280;
int backH = 480;
int backImx = 1;
int backImy = 1;
int ene2X = 700;
int ene2Y = 200;
int ene2W = 50;
int ene2H = 50;
int ene2Imx = 504;
int ene2Imy = 352;
int foreX = 1000;
int foreY = 0;
int foreW = 640;
int foreH = 480;
int foreImx = 0;
int foreImy = 0;
int gui1X = 40000;
int gui1Y = 440;
int gui1W = 120;
int gui1H = 36;
int gui1Imx = 21;
int gui1Imy = 33;
SDL_Event event;
bool gameRunning = true;
bool keysHeld[323] = {false};
while(gameRunning)
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
gameRunning = false;
}
if(event.type == SDL_KEYDOWN)
{
keysHeld[event.key.keysym.sym] = true;
}
if(event.type == SDL_KEYUP)
{
keysHeld[event.key.keysym.sym] = false;
}
}
if(keysHeld[SDLK_ESCAPE])
{
gameRunning = false;
}
if(keysHeld[SDLK_UP])
{
playerY -= 1;
cout << "up \n";
}
if(keysHeld[SDLK_LEFT])
{
playerX -= 1;
cout << "left \n";
}
if(keysHeld[SDLK_RIGHT])
{
playerX += 1;
cout << "Right \n";
}
if(keysHeld[SDLK_DOWN])
{
playerY += 1;
cout << "down \n";
}
if(keysHeld[SDLK_SPACE])
{
bulX = playerX -10;
bulY = playerY +22;
}
if(keysHeld[SDLK_r])
{
ene2X = 700;
ene2Y = 20;
gui1X = 320;
gui1Y = 240;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
drawSprite(back,
screen,
backImx, backImy,
backX, backY,
backW, backH);
drawSprite(bitmap,
screen,
playerImx, playerImy,
playerX, playerY,
playerW, playerH);
drawSprite(bitmap,
screen,
bulImx, bulImy,
bulX, bulY,
bulW, bulH);
drawSprite(gui1,
screen,
gui1Imx, gui1Imy,
gui1X, gui1Y,
gui1W, gui1H);
drawSprite(bitmap,
screen,
ene2Imx, ene2Imy,
ene2X, ene2Y,
ene2W, ene2H);
SDL_Rect playerC;
SDL_FillRect (screen,&playerC, SDL_MapRGB(screen->format, 255, 0, 255));
playerC.x = 400;
playerC.y = 100;
playerC.w = 50;
playerC.h = 50;
SDL_Rect ene2C;
SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
ene2C.x = 1;
ene2C.y = 1;
ene2C.w = 50;
ene2C.h = 50;
SDL_Rect bulC{10000,10000,50,30};
SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
bulX += 3;
ene2X -= 1;
if(keysHeld[SDLK_1])
{
playerC.x++;
}
ene2C.x++;
if(playerY >= ene2Y);
else ene2Y -= 1;
if(playerY <= ene2Y);
else ene2Y += 1;
if(playerY >= 71);
else playerY += 1;
if(playerY + 44 <= 397);
else playerY -= 1;
if(collision(&playerC,&ene2C))
{
gui1X = 320;
gui1Y = 240;
}
SDL_Flip(screen);
}
SDL_FreeSurface(bitmap);
SDL_FreeSurface(bul);
SDL_FreeSurface(gui1);
SDL_FreeSurface(fore);
SDL_FreeSurface(ene2);
SDL_Quit();
return 0;
}
void drawSprite(SDL_Surface* imageSurface,
SDL_Surface* screenSurface,
int srcX, int srcY,
int dstX, int dstY,
int width, int height)
{
SDL_Rect srcRect;
srcRect.x = srcX;
srcRect.y = srcY;
srcRect.w = width;
srcRect.h = height;
SDL_Rect dstRect;
dstRect.x = dstX;
dstRect.y = dstY;
dstRect.w = width;
dstRect.h = height;
SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}
I think the problem is somewhere at
SDL_Rect playerC;
SDL_FillRect (screen,&playerC, SDL_MapRGB(screen->format, 255, 0, 255));
playerC.x = 400;
playerC.y = 100;
playerC.w = 50;
playerC.h = 50;
SDL_Rect ene2C;
SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
ene2C.x = 1;
ene2C.y = 1;
ene2C.w = 50;
ene2C.h = 50;
SDL_Rect bulC{10000,10000,50,30};
SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
bulX += 3;
ene2X -= 1;
if(keysHeld[SDLK_1])
{
playerC.x++;
}
ene2C.x++;
if(playerY >= ene2Y);
else ene2Y -= 1;
if(playerY <= ene2Y);
else ene2Y += 1;
if(playerY >= 71);
else playerY += 1;
if(playerY + 44 <= 397);
else playerY -= 1;
if(collision(&playerC,&ene2C))
{
gui1X = 320;
gui1Y = 240;
}
Thanks for any help!