קוד:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>
// ================== Declare global variables =======================
// ================================================== =================
SDL_Surface *screen, *background, *user;
//testing
SDL_Surface *shot;
int ShotX, ShotY, ShotAngle, ShotSpeed=1;
int draw = 0;
//endtesting
int MouseX, MouseY, MouseClicked;
int UserX, UserY;
int xV=0, yV=0;
int Gravity = 1;
int SCREEN_WIDTH = 800;
int SCREEN_HEIGHT = 600;
int SCREEN_BPP = 32;
// ================== Functions ======================================
// ================================================== =================
void collisions()
{
if(UserX > SCREEN_WIDTH) UserX = SCREEN_WIDTH;
if(UserX < 0) UserX = 0;
if(UserY > SCREEN_HEIGHT - 48) UserY = SCREEN_HEIGHT - 48; yV = 0;
}
void movements()
{
UserX = UserX + xV;
UserY = UserY + yV;
yV = yV + Gravity;
if(draw==1)
{
ShotX += (int)(ShotSpeed * cos(ShotAngle));
ShotY += (int)(ShotSpeed * sin(ShotAngle));
}
collisions();
}
void shoot()
{
ShotX = UserX;
ShotY = UserY;
ShotAngle = (int)atan2(UserY - MouseY, UserX - MouseX);
draw = 1;
}
int init()
{
if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) return 0;
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if(!screen) return 0;
background = SDL_LoadBMP("Pictures/background.bmp");
user = SDL_LoadBMP("Pictures/user.bmp");
Uint32 user_color = SDL_MapRGB( user->format, 255, 255, 255);
SDL_SetColorKey(user, SDL_RLEACCEL | SDL_SRCCOLORKEY, user_color);
//testing
shot = SDL_LoadBMP("Pictures/shot_two.bmp");
//endtesting
return 1;
}
void shutdown(void)
{
SDL_FreeSurface( background );
SDL_FreeSurface( shot );
SDL_FreeSurface( user );
SDL_Quit();
}
void renderscene(void)
{
SDL_BlitSurface( background, NULL, screen, NULL );
SDL_Rect r;
r.x = UserX;
r.y = UserY;
SDL_BlitSurface(user, NULL, screen, &r);
r.x = ShotX;
r.y = ShotY;
SDL_BlitSurface(shot, NULL, screen, &r);
SDL_Flip( screen );
}
// ====================== main ===========================================
// ================================================== =====================
int main(int argc, char *argv[])
{
int done = 0;
Uint8* keys;
SDL_Event event;
atexit(shutdown);
if(init() == 0) {
printf("Unable to Intialise Program: %s\n\n", SDL_GetError() );
}
while(done != 1){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
done = 1;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) done = 1;
if(event.key.keysym.sym == SDLK_a) {} // move left
if(event.key.keysym.sym == SDLK_d) {} // move right
if(event.key.keysym.sym == SDLK_SPACE) {} // jump
if(event.key.keysym.sym == SDLK_LCTRL) shoot();
} // end cases
} // end key events
SDL_GetMouseState(&MouseX, &MouseY);
if(!SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) MouseClicked = 1;
if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1) && MouseClicked == 1)
{
MouseClicked = 0;
// shooting
//test
if(draw == 0) draw = 1;
if(draw == 1) draw = 0;
//endtest
}
movements();
renderscene();
} // end game loop
return 0;
} // end application