Advertisement
patrikb42

movementproject v0.5

Feb 6th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <stdio.h>
  3. #include "Player.h"
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. const int mainscreen_width = 1920;
  9. const int mainscreen_height = 1060;
  10. const int mainscreen_xpoz = 0;
  11. const int mainscreen_ypoz = 0;
  12.  
  13. const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
  14.  
  15. Player player1(false, false, false, false, 50, 100, 15, "BMPs/character1");
  16. Player player2(false, false, false, false, 100, 100, 15, "BMPs/character2");
  17.  
  18. SDL_Window* mainwindow = NULL;
  19. SDL_Surface* mainwindow_surface = NULL;
  20. SDL_Surface* character = NULL;
  21. SDL_Surface* character2 = NULL;
  22. SDL_Surface* backgrounds = NULL;
  23.  
  24. SDL_Rect character1movement;
  25. SDL_Rect character2movement;
  26.  
  27. SDL_Event movementevent;
  28.  
  29.  
  30. bool startup();
  31.  
  32. bool load();
  33.  
  34. bool getinputs();
  35. void dealwithinputs();
  36. void resetinputs();
  37.  
  38. //void loadbackground(const char* image);
  39.  
  40. void endprogram();
  41.  
  42. bool startup() {
  43.     bool noerror = true;
  44.     if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
  45.         cout << "error on startup: " << SDL_GetError() << endl;
  46.         noerror = false;
  47.     }
  48.     else {
  49.         mainwindow = SDL_CreateWindow("movementproject v0.5", mainscreen_xpoz, mainscreen_ypoz, mainscreen_width, mainscreen_height, SDL_WINDOW_SHOWN);
  50.         if (mainwindow == NULL) {
  51.             cout << "COULD NOT CREATE WINDOW" << SDL_GetError() << endl;
  52.             noerror = false;
  53.         }
  54.         else {
  55.             mainwindow_surface = SDL_GetWindowSurface(mainwindow);
  56.         }
  57.         return noerror;
  58.     }
  59. }
  60.  
  61. bool load() {
  62.     bool noerror = true;
  63.     character = SDL_LoadBMP("BMPs/character.bmp");
  64.     if (character == NULL) {
  65.         character = SDL_LoadBMP("BMPs/defaultpicture.bmp");
  66.         cout << "CHARACTER PICTURE COULDN'T BE LOADED" << SDL_GetError() << endl;
  67.         noerror = false;
  68.     }
  69.     character2 = SDL_LoadBMP("BMPs/character2.bmp");
  70.     if (character2 == NULL) {
  71.         character2 = SDL_LoadBMP("BMPs/defaultpicture.bmp");
  72.         cout << "CHARACTER2 PICTURE COULDN'T BE LOADED" << SDL_GetError() << endl;
  73.         noerror = false;
  74.     }
  75.     else {
  76.         backgrounds = SDL_LoadBMP("BMPs/background.bmp");
  77.         if (backgrounds == NULL) {
  78.             backgrounds = SDL_LoadBMP("BMPs/defaultpicture.bmp");
  79.             cout << "STARTINGBACKGROUND COULND'T BE LOADED" << SDL_GetError() << endl;
  80.             noerror = false;
  81.         }
  82.     }
  83.     return noerror;
  84. }
  85.  
  86. bool getinputs() {
  87.     bool a_key_is_down = false;
  88.     if (movementevent.type == SDL_KEYDOWN) {
  89.         if (currentKeyStates[SDL_SCANCODE_W])
  90.         {
  91.             player1.ismoving_up = true;
  92.             a_key_is_down = true;
  93.         }
  94.         if (currentKeyStates[SDL_SCANCODE_A]) {
  95.             player1.ismoving_left = true;
  96.             a_key_is_down = true;
  97.         }
  98.         if (currentKeyStates[SDL_SCANCODE_S]) {
  99.             player1.ismoving_down = true;
  100.             a_key_is_down = true;
  101.         }
  102.         if (currentKeyStates[SDL_SCANCODE_D]) {
  103.             player1.ismoving_right = true;
  104.             a_key_is_down = true;
  105.         }
  106.  
  107.  
  108.         if (currentKeyStates[SDL_SCANCODE_UP]) {
  109.             player2.ismoving_up = true;
  110.             a_key_is_down = true;
  111.         }
  112.         if (currentKeyStates[SDL_SCANCODE_LEFT]) {
  113.             player2.ismoving_left = true;
  114.             a_key_is_down = true;
  115.         }
  116.         if (currentKeyStates[SDL_SCANCODE_DOWN]) {
  117.             player2.ismoving_down = true;
  118.             a_key_is_down = true;
  119.         }
  120.         if (currentKeyStates[SDL_SCANCODE_RIGHT]) {
  121.             player2.ismoving_right = true;
  122.             a_key_is_down = true;
  123.         }
  124.     }
  125.     cout << a_key_is_down << endl;
  126.     return a_key_is_down;
  127. }
  128.  
  129. void dealwithinputs(){
  130.     if (player1.ismoving_up) {
  131.         character1movement.y -= player1.speed;
  132.     }
  133.     if (player1.ismoving_left) {
  134.         character1movement.x -= player1.speed;
  135.     }
  136.     if (player1.ismoving_down) {
  137.         character1movement.y += player1.speed;
  138.     }
  139.     if (player1.ismoving_right) {
  140.         character1movement.x += player1.speed;
  141.     }
  142.  
  143.  
  144.     if (player2.ismoving_up) {
  145.         character2movement.y -= player1.speed;
  146.     }
  147.     if (player2.ismoving_left) {
  148.         character2movement.x -= player1.speed;
  149.     }
  150.     if (player2.ismoving_down) {
  151.         character2movement.y += player1.speed;
  152.     }
  153.     if (player2.ismoving_right) {
  154.         character2movement.x += player1.speed;
  155.     }
  156. }
  157.  
  158. void resetinputs() {
  159.     if (movementevent.type == SDL_KEYUP) {
  160.         if (currentKeyStates[!SDL_SCANCODE_W])
  161.         {
  162.             player1.ismoving_up = false;
  163.         }
  164.         if (currentKeyStates[!SDL_SCANCODE_A]) {
  165.             player1.ismoving_left = false;
  166.         }
  167.         if (currentKeyStates[!SDL_SCANCODE_S]) {
  168.             player1.ismoving_down = false;
  169.         }
  170.         if (currentKeyStates[!SDL_SCANCODE_D]) {
  171.             player1.ismoving_right = false;
  172.         }
  173.  
  174.  
  175.         if (currentKeyStates[!SDL_SCANCODE_UP]) {
  176.             player2.ismoving_up = false;
  177.         }
  178.         if (currentKeyStates[!SDL_SCANCODE_LEFT]) {
  179.             player2.ismoving_left = false;
  180.         }
  181.         if (currentKeyStates[!SDL_SCANCODE_DOWN]) {
  182.             player2.ismoving_down = false;
  183.         }
  184.         if (currentKeyStates[!SDL_SCANCODE_RIGHT]) {
  185.             player2.ismoving_right = false;
  186.         }
  187.     }
  188. }
  189.  
  190. /* void loadbackground(const char* image){
  191. SDL_Surface* backgrounds = SDL_LoadBMP(image);
  192. if (backgrounds == NULL) {
  193. cout << "background couldn't be loaded!" << endl;
  194. }
  195. } */
  196.  
  197. void endprogram() {
  198.     SDL_FreeSurface(mainwindow_surface);
  199.     mainwindow_surface = NULL;
  200.  
  201.     SDL_FreeSurface(character);
  202.     character = NULL;
  203.  
  204.     SDL_FreeSurface(backgrounds);
  205.     backgrounds = NULL;
  206.  
  207.  
  208.     SDL_DestroyWindow(mainwindow);
  209.     mainwindow = NULL;
  210. }
  211. int main(int argc, char* argv[]) {
  212.     startup();
  213.     if (!startup) {
  214.         cout << "STARTUP FAILED" << endl;
  215.     }
  216.     else {
  217.         load();
  218.         if (!load) {
  219.             cout << "LOADING FAILED" << endl;
  220.         }
  221.         else {
  222.             bool running = true;
  223.  
  224.            
  225.  
  226.             SDL_SetColorKey(character, SDL_TRUE, 0xFF00FF);
  227.  
  228.             while (running) {
  229.                 //loadbackground("BMPs/backgound");
  230.                 SDL_BlitSurface(backgrounds, NULL, mainwindow_surface, NULL);
  231.  
  232.                 SDL_BlitSurface(character, NULL, mainwindow_surface, &character1movement);
  233.                 if (SDL_BlitSurface(character, NULL, mainwindow_surface, &character1movement) != 0) {
  234.                     cout << "character couldn't be loaded! " << endl;
  235.                 }
  236.  
  237.                 SDL_BlitSurface(character2, NULL, mainwindow_surface, &character2movement);
  238.                 if (SDL_BlitSurface(character2, NULL, mainwindow_surface, &character2movement) != 0) {
  239.                     cout << "character2 couldn't be loaded! " << endl;
  240.                 }
  241.                 if (getinputs) {
  242.                     getinputs();
  243.                     dealwithinputs();
  244.                     resetinputs();
  245.                 }
  246.                
  247.  
  248.                 SDL_UpdateWindowSurface(mainwindow);
  249.             }
  250.         }
  251.         }
  252.    
  253.  
  254.     endprogram();
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement