Guest User

Untitled

a guest
Nov 18th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "SDL_image.h"
  3. #include "SDL_mixer.h"
  4. #include "Timer.h"
  5. #include <string>
  6. using namespace::std;
  7.  
  8. // Contant Variables
  9. const int FPS = 60;
  10. const int SpaceShip_w = 20;
  11. const int SpaceShip_h = 20;
  12.  
  13. // Images
  14. SDL_Surface *spaceship = NULL;
  15. SDL_Surface *bullet_left = NULL;
  16. SDL_Surface *bullet_right = NULL;
  17. SDL_Surface *game_bg = NULL;
  18. SDL_Surface *screen = NULL;
  19.  
  20. // Sounds
  21. Mix_Chunk *pass = NULL;
  22.  
  23. // Events
  24. SDL_Event event;
  25.  
  26. class SpaceShip {
  27. public:
  28. int SpaceShip_x, SpaceShip_y;
  29. int SpaceShip_xVel, SpaceShip_yVel;
  30. SpaceShip();
  31. void handle_input();
  32. void move();
  33. void show();
  34. };
  35.  
  36. class Bullet {
  37. public:
  38. Bullet();
  39. void Update();
  40. void Fire(float startX, float startY, float xSpeed, float ySpeed);
  41. void Draw();
  42. private:
  43. bool alive;
  44. float Bullet_x, Bullet_y;
  45. float Bullet_xVel, Bullet_yVel;
  46. };
  47.  
  48. SDL_Surface *load_image(string filename) {
  49. SDL_Surface* loadedImage = NULL;
  50. SDL_Surface* optimizedImage = NULL;
  51. loadedImage = IMG_Load(filename.c_str());
  52. if(loadedImage != NULL) {
  53. optimizedImage = SDL_DisplayFormatAlpha(loadedImage);
  54. SDL_FreeSurface(loadedImage);
  55. }
  56. return optimizedImage;
  57. }
  58.  
  59. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  60. SDL_Rect offset;
  61. offset.x = x;
  62. offset.y = y;
  63. SDL_BlitSurface(source, NULL, destination, &offset);
  64. }
  65.  
  66. bool init()
  67. {
  68. if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
  69. return false;
  70. }
  71. const SDL_VideoInfo* myPointer = SDL_GetVideoInfo();
  72. screen = SDL_SetVideoMode(myPointer->current_w, myPointer->current_h, 32, SDL_FULLSCREEN | SDL_HWSURFACE);
  73. if(screen == NULL) {
  74. return false;
  75. }
  76. if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1) {
  77. return false;
  78. }
  79. SDL_WM_SetCaption("Galactic Wars v1", NULL);
  80. return true;
  81. }
  82.  
  83. bool load_files() {
  84. spaceship = load_image("bin/spaceship.png");
  85. if(spaceship == NULL)
  86. {
  87. return false;
  88. }
  89. bullet_left = load_image("bin/bullet.png");
  90. if(bullet_left == NULL)
  91. {
  92. return false;
  93. }
  94. bullet_right = load_image("bin/bullet.png");
  95. if(bullet_right == NULL)
  96. {
  97. return false;
  98. }
  99. game_bg = load_image("bin/game_bg.jpg");
  100. if(game_bg == NULL) {
  101. return false;
  102. }
  103. pass = Mix_LoadWAV("bin/pass.wav");
  104. if(pass == NULL) {
  105. return false;
  106. }
  107. return true;
  108. }
  109.  
  110. void clean_up() {
  111. SDL_FreeSurface(game_bg);
  112. SDL_FreeSurface(bullet_left);
  113. SDL_FreeSurface(bullet_right);
  114. Mix_FreeChunk(pass);
  115. SDL_Quit();
  116. }
  117.  
  118. SpaceShip::SpaceShip() {
  119. SpaceShip_x = 880;
  120. SpaceShip_y = 900;
  121. SpaceShip_xVel = 0;
  122. SpaceShip_yVel = 0;
  123. }
  124.  
  125. Bullet::Bullet():alive(false),Bullet_x(0),Bullet_y(0),Bullet_xVel(0),Bullet_yVel(0){}
  126.  
  127. void SpaceShip::handle_input() {
  128. if(event.type == SDL_KEYDOWN) {
  129. switch(event.key.keysym.sym) {
  130. case SDLK_w: SpaceShip_yVel -= SpaceShip_h / 2; break;
  131. case SDLK_s: SpaceShip_yVel += SpaceShip_h / 2; break;
  132. case SDLK_a: SpaceShip_xVel -= SpaceShip_w / 2; break;
  133. case SDLK_d: SpaceShip_xVel += SpaceShip_w / 2; break;
  134. }
  135. }
  136. else if(event.type == SDL_KEYUP) {
  137. switch(event.key.keysym.sym) {
  138. case SDLK_w: SpaceShip_yVel += SpaceShip_h / 2; break;
  139. case SDLK_s: SpaceShip_yVel -= SpaceShip_h / 2; break;
  140. case SDLK_a: SpaceShip_xVel += SpaceShip_w / 2; break;
  141. case SDLK_d: SpaceShip_xVel -= SpaceShip_w / 2; break;
  142. }
  143. }
  144. }
  145.  
  146. void SpaceShip::move() {
  147. const SDL_VideoInfo* myPointer = SDL_GetVideoInfo();
  148. SpaceShip_x += SpaceShip_xVel;
  149. if(SpaceShip_x + SpaceShip_w > myPointer->current_w) {
  150. SpaceShip_x = -60;
  151. Mix_PlayChannel(-1, pass, 0) == -1;
  152. }
  153. if(SpaceShip_x < -60) {
  154. SpaceShip_x = SpaceShip_x + SpaceShip_w + myPointer->current_w;
  155. Mix_PlayChannel(-1, pass, 0) == -1;
  156. }
  157. SpaceShip_y += SpaceShip_yVel;
  158. if(SpaceShip_y + SpaceShip_h > myPointer->current_h) {
  159. SpaceShip_y = -60;
  160. Mix_PlayChannel(-1, pass, 0) == -1;
  161. }
  162. if(SpaceShip_y < -60) {
  163. SpaceShip_y = SpaceShip_y + SpaceShip_h + myPointer->current_h;
  164. Mix_PlayChannel(-1, pass, 0) == -1;
  165. }
  166. }
  167.  
  168. void SpaceShip::show() {
  169. apply_surface(SpaceShip_x, SpaceShip_y, spaceship, screen);
  170. }
  171.  
  172. void Bullet::Update() {
  173. if(alive) {
  174. Bullet_x += Bullet_xVel;
  175. Bullet_y += Bullet_yVel;
  176. }
  177. }
  178.  
  179. void Bullet::Fire(float startX, float startY, float xSpeed, float ySpeed) {
  180. Bullet_x = startX;
  181. Bullet_y = startY;
  182. Bullet_xVel = xSpeed;
  183. Bullet_yVel = ySpeed;
  184. alive = true;
  185. }
  186.  
  187. void Bullet::Draw() {
  188. if(alive) {
  189. apply_surface(Bullet_x, Bullet_y, bullet_left, screen);
  190. }
  191. }
  192.  
  193. int main(int argc, char* args[]) {
  194. bool quit = false;
  195. SpaceShip mySpaceShip;
  196. Bullet Left_Bullet;
  197. Timer fps;
  198. int bgX = 0, bgY = 0;
  199. if(init() == false) {
  200. return 1;
  201. }
  202. if(load_files() == false) {
  203. return 1;
  204. }
  205. if(SDL_Flip(screen) == -1) {
  206. return 1;
  207. }
  208. while(quit == false) {
  209. fps.start();
  210. while(SDL_PollEvent(&event)) {
  211. if(event.type == SDL_KEYDOWN) {
  212. switch(event.key.keysym.sym) {
  213. case SDLK_ESCAPE:
  214. quit = true;
  215. break;
  216. case SDLK_SPACE:
  217. Left_Bullet.Fire(SpaceShip_x, SpaceShip_y, 0, 1);
  218. break;
  219. }
  220. }
  221. mySpaceShip.handle_input();
  222. if(event.type == SDL_QUIT) {
  223. quit = true;
  224. }
  225.  
  226. }
  227. bgY += 2;
  228. if(bgY >= game_bg->h) {
  229. bgY = 0;
  230. }
  231. mySpaceShip.move();
  232. Left_Bullet.Update();
  233. apply_surface(bgX, bgY, game_bg, screen);
  234. apply_surface(bgX, bgY-game_bg->h, game_bg, screen);
  235. mySpaceShip.show();
  236. Left_Bullet.Draw();
  237. if(SDL_Flip(screen) ==-1) {
  238. return 1;
  239. }
  240. if(fps.get_ticks() < 1000 / FPS) {
  241. SDL_Delay((1000 / FPS) - fps.get_ticks());
  242. }
  243. }
  244. clean_up();
  245. return 0;
  246. }
Add Comment
Please, Sign In to add comment