Advertisement
Guest User

sfml c++

a guest
Apr 7th, 2017
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include<cmath>
  4. //#include"block.h"
  5. //sf::RenderWindow win(sf::VideoMode(800,600),"RPG");
  6. using namespace std;
  7. enum class State{PAUSE,PLAYING};
  8.  
  9. /*
  10. You only need to load a texture once per file. I've moved the texture you're using to a global variable for now to make things a bit easier.
  11. */
  12. sf::Texture tex;
  13.  
  14. class Block{
  15. sf::Sprite spr;
  16. sf::Vector2f pos;
  17.  
  18. public:
  19.     int pos_i;
  20.     Block(float x=0.f,float y=0.f,float hi=0.f,float wd=0.f){
  21.  
  22.         sf::Vector2f size(hi,wd);
  23.         pos.x=x; pos.y=y;
  24.         spr.setTexture(tex);
  25.         /*
  26.         The way the original code was set up, it was using the same bit of art, which makes it really hard to tell what's the hero, and what's the weapon, and what's the map.
  27.         This code picks a part of a 32x32 square in an image, which makes it easier to see what's what.
  28.         Basically, a super simple way to do tilemaps and spritesheets and such.
  29.         */
  30.         spr.setTextureRect(sf::IntRect(size.x,size.y, 32,32));
  31.         spr.setPosition(sf::Vector2f(x,y));
  32. }
  33.  
  34.  
  35.    void draw(sf::RenderWindow* win){
  36.  
  37.              win->draw(spr);
  38.     }
  39.  
  40.  
  41.     void set_size(sf::Vector2f size){spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));}
  42.     sf::Vector2f get_pos(){return pos;}
  43.     sf::Sprite& get_tex(){return spr;}
  44.  
  45.    void set_pos(sf::Vector2f pos){spr.move(pos);}
  46. };
  47.  
  48.  
  49.  
  50.  
  51. class World{
  52. int wd;
  53. int hi;
  54. int level[6][6];
  55.  
  56. public:
  57.  
  58.  Block** block1;
  59.    int count;
  60.     int get_pos(int i,int j){return level[i][j];} //world position.
  61.  
  62.     World():level{
  63.     {1,0,0,0,0,0},
  64.     {1,0,0,0,0,0},
  65.     {1,0,0,0,0,0},
  66.     {1,0,0,0,0,0},
  67.     {1,0,0,0,0,0},
  68.     {1,1,1,1,1,1}}{
  69.     wd=6;
  70.     hi=6;
  71.     block1=new Block*[wd];
  72.     for(int i=0;i<hi;i++)
  73.         block1[i]=new Block[hi];
  74.  
  75.     for(int i=0;i<hi;i++){
  76.         for(int j=0;j<wd;j++){
  77.  
  78.             if(level[i][j]==1){
  79.            Block blk(j*24,i*24,0,0);
  80.         block1[i][j]=blk;
  81.  
  82.         cout<<"  x "<<j<<"  y "<<i<<'\n';
  83.         //cout<<" pos x "<<pos.x<<" pos y "<<pos.y<<'\n';
  84.         }
  85.  
  86.         }
  87.  
  88.  
  89.     }
  90.  
  91. //block[1].set_pos(sf::Vector2f(1.f*16,4.f*16));
  92.  
  93. }
  94.  
  95. void draw(sf::RenderWindow* win){
  96. for(int i=0;i<wd;i++)
  97.     for(int j=0;j<hi;j++){
  98.         block1[i][j].draw(win);
  99.  
  100. }
  101. }
  102. };
  103.  
  104. class weapon{
  105.  Block blc;
  106.  bool m_up;
  107.  bool m_dn;
  108.  bool m_lt;
  109.  bool m_rt;
  110.  sf::Vector2f m_res;
  111.  
  112.  public:
  113.        weapon():blc(0,0,32,0){
  114.            /*
  115.            setOrigin here places the origin of the sprite in the centre. This way, when we rotate the sprite, it spins around the middle
  116.            */
  117.        blc.get_tex().setOrigin(16, 16);
  118.        }
  119.     void move_rt(){m_rt=true;}
  120.     void move_lt(){m_lt=true;}
  121.     void move_up(){m_up=true;}
  122.     void move_dn(){m_dn=true;}
  123.  
  124.     void stop_rt(){m_rt=false;}
  125.     void stop_lt(){m_lt=false;}
  126.     void stop_up(){m_up=false;}
  127.     void stop_dn(){m_dn=false;}
  128.     void faceDown() {
  129.         /*
  130.         the way i drew the person/weapon sprite, they're facing down. Thus, "down" is angle 0, and we go clockwise from there.
  131.         */
  132.         blc.get_tex().setRotation(0);
  133.     }
  134.     void faceUp() {
  135.         blc.get_tex().setRotation(180);
  136.     }
  137.     void faceLeft() {
  138.         blc.get_tex().setRotation(90);
  139.     }
  140.     void faceRight() {
  141.         blc.get_tex().setRotation(270);
  142.     }
  143.     void setPos(sf::Vector2f pos){blc.set_pos(pos);}
  144.     sf::Sprite getWep(){return blc.get_tex();}
  145.     void update( sf::Vector2f pos ,sf::Vector2i mse_pos){
  146.  
  147. if(m_up)
  148.     blc.set_pos(pos - sf::Vector2f(0.f,10.f));
  149. if(m_dn)
  150.     blc.set_pos(pos + sf::Vector2f(0.f,10.f));
  151. if(m_rt)
  152.     blc.set_pos(pos + sf::Vector2f(10.f,0.f));
  153. if(m_lt)
  154.     blc.set_pos(pos - sf::Vector2f(10.f,0.f));
  155.  
  156. /*
  157. I'm not sure of the maths required to make the sprite face where the mouse is pointing. I think what you have here is really close, but I'm not really able to work out the solution right now
  158. */
  159. //float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;
  160.  
  161.  
  162. }
  163.  
  164.  
  165.  
  166. };
  167.  
  168. class Player{
  169.     Block blc;
  170.     weapon Weapon;
  171.     sf::Vector2f m_res;
  172.      sf::Vector2i pos1; //player position
  173.  
  174.     bool m_up;
  175.     bool m_dn;
  176.     bool m_lt;
  177.     bool m_rt;
  178.  
  179.     float m_speed;
  180.     sf::Time last_hit;
  181.  
  182. public:
  183.     Player():blc(0,0,64,0),pos1(0,0){
  184.  
  185.     }
  186.  
  187.     void spawn(sf::Vector2f res,sf::Vector2f loc){
  188.         blc.set_pos(loc);
  189.         pos1.x=loc.x; //set position.
  190.         this->pos1.y=loc.y;
  191.         Weapon.setPos(sf::Vector2f(loc.x+5.0f,loc.y+5.0));
  192.        blc.get_tex().setOrigin(16, 16);
  193.  
  194.  
  195.     m_res.x=res.x;
  196.     m_res.y=res.y;
  197.     /*
  198.     most of the changes I made up in weapon I copied down here. We also want to start the player out facing the "right" way.
  199.     This is more useful if you draw them facing one direction, and want them to start the game pointing in another direction.
  200.     */
  201.     faceDown();
  202.  
  203.     }
  204.     weapon& getWep(){return Weapon;}
  205.     bool hit(sf::Time time_hit);
  206.     sf::FloatRect get_pos(){return blc.get_tex().getGlobalBounds();}
  207.     sf::Vector2f get_cen(){return blc.get_pos();}
  208.     float get_rot(){return blc.get_tex().getRotation();}
  209.     sf::Sprite get_pla(){return blc.get_tex();}
  210.  
  211.     void move_rt(){m_rt=true;}
  212.     void move_lt(){m_lt=true;}
  213.     void move_up(){m_up=true;}
  214.     void move_dn(){m_dn=true;}
  215.     sf::Vector2i get_map_pos(){return pos1;}
  216.     void stop_rt(){m_rt=false;}
  217.     void stop_lt(){m_lt=false;}
  218.     void stop_up(){m_up=false;}
  219.     void stop_dn(){m_dn=false;}
  220.     void faceDown() {
  221.         blc.get_tex().setRotation(0);
  222.         /*
  223.         instead of calling faceDown()/etc for the player AND the weapon when we move the player, we can just call the function for the weapon here, and it'll automatically happen no matter what
  224.         really useful if we want other things to change what direction the player is facing
  225.         you can do the same type of thing in the update functions, to keep the player and weapon connected to each other easier
  226.         */
  227.         Weapon.faceDown();
  228.     }
  229.     void faceUp() {
  230.         blc.get_tex().setRotation(180);
  231.         Weapon.faceDown();
  232.     }
  233.     void faceLeft() {
  234.         blc.get_tex().setRotation(90);
  235.         Weapon.faceLeft();
  236.     }
  237.     void faceRight() {
  238.         blc.get_tex().setRotation(270);
  239.         Weapon.faceRight();
  240.     }
  241.  
  242. void update( sf::Vector2f pos ,sf::Vector2i mse_pos){
  243.  
  244. if(m_up){
  245.         pos1.y+=1; // update player position.
  246.    blc.set_pos(pos - sf::Vector2f(0.f,10.f));}
  247. if(m_dn)
  248.     blc.set_pos(pos + sf::Vector2f(0.f,10.f));
  249. if(m_rt)
  250.     blc.set_pos(pos + sf::Vector2f(10.f,0.f));
  251. if(m_lt)
  252.     blc.set_pos(pos - sf::Vector2f(10.f,0.f));
  253.  
  254.  
  255.  
  256. //float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;
  257.  
  258. //blc.get_tex().setRotation(angle);
  259. }
  260.  
  261.  
  262. };
  263.  
  264.  
  265. int main()
  266. {
  267.         /*
  268.         instead of loading the texture a bunch of times for each sprite we want to draw, we load it once here.
  269.         it takes time to load a texture, so ideally, we want to do it once, then just refer back to that texture when creating a new sprite.
  270.         once you need more than a few textures, you can put them in a resource management class that has a std::vector or std::map, and access them through those data structures.
  271.         for now, a couple of global variables are fine.
  272.         */
  273.         tex.loadFromFile("1.png");
  274.     //State s=State::PAUSE;
  275.     sf::Vector2f res;
  276.     /*
  277.     i made the window a bit smaller than the full screen resolution, just for testing things out
  278.     */
  279.     res.x = 800;
  280.     res.y = 600;
  281.     //res.x=sf::VideoMode::getDesktopMode().width;
  282.     //res.y=sf::VideoMode::getDesktopMode().height;
  283.     sf::RenderWindow win(sf::VideoMode(res.x,res.y),"RPG");
  284.     //sf::RenderWindow win1(sf::VideoMode(res.x,res.y),"RPG");
  285.     sf::View mainView;
  286.     mainView.reset(sf::FloatRect(0,0,res.x,res.y));
  287.     mainView.setViewport(sf::FloatRect(0,0,1.0f,1.0f));
  288.     sf::Clock clock;
  289.     sf::Time gameTotal;
  290.     sf::Vector2f mouseWPos;
  291.     sf::Vector2i mouseSPos;
  292.  
  293.  
  294.     World w;
  295. Player pla;
  296. weapon wep;
  297. sf::Vector2i pos1=pla.get_map_pos();
  298. pla.spawn(res,sf::Vector2f(2.0f,2.0f));
  299.       while(win.isOpen()){
  300.             sf::Vector2i pos1=pla.get_map_pos();
  301.  
  302.             sf::Event eve;
  303.             while(win.pollEvent(eve)){
  304.  
  305.                 if(true){
  306.                   if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
  307.                         //if(w.get_pos(pos1.x,pos1.y+1)==1)
  308.  
  309.                   pla.move_up();
  310.                   pla.getWep().move_up();
  311.                   pla.faceUp();
  312.                   }
  313.                 else{
  314.                     pla.stop_up();
  315.                     pla.getWep().stop_up();}
  316.                 if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
  317.                    pla.move_dn();
  318.                    pla.getWep().move_dn();
  319.                    pla.faceDown();
  320.                    }
  321.                 else{
  322.                     pla.stop_dn();
  323.                     pla.getWep().stop_dn();}
  324.                 if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
  325.                      pla.move_lt();
  326.                      pla.getWep().move_lt();
  327.                    pla.faceLeft();
  328.                      }
  329.                 else{
  330.                     pla.stop_lt();
  331.                     pla.getWep().stop_lt();}
  332.                 if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
  333.                     pla.move_rt();
  334.                     pla.getWep().move_rt();
  335.                    pla.faceRight();
  336.                     }
  337.                 else{
  338.                     pla.stop_rt();
  339.                     pla.getWep().stop_rt();}
  340.  
  341.                     }
  342.                     /*
  343.                     exits the game when the escape key is pressed.
  344.                     */
  345.                 if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
  346.                 win.close();}
  347.  
  348.  
  349.  
  350.                     sf::Vector2f plaPos(pla.get_cen());
  351.                     pla.update(plaPos,sf::Mouse::getPosition());
  352.                     pla.getWep().update(plaPos,sf::Mouse::getPosition());
  353.             }
  354.  
  355.                     clock.restart();
  356.  
  357.  
  358.             win.clear(sf::Color::Black);
  359.             /*
  360.             draw the map, then weapon, then player.
  361.             */
  362.             w.draw(&win);
  363.             win.draw(pla.getWep().getWep());
  364.             win.draw(pla.get_pla());
  365.             win.setView(mainView);
  366.  
  367.  
  368.             win.display();
  369.  
  370.  
  371.         }
  372.     //cout << "Hello world!" << endl;
  373.     return 0;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement