title: c++ sdl draw dynamic rect based on mouse position
body: <p>this might seem obvious to many, but I'm stuck and I cannot find any tutorial which helped me, so it would be great to find the answer here.</p>
  
  <p>I tried to make a rectangle like you have in RTS games, so to speak a dynamic select box, but it does not work correctly. 
  It would also be enough is someone lists up bullet points which each step for creating a box like this</p>
  
  <p>(Don't wonder I'm really new to c++)</p>
  
  <p>Mouse.h :</p>
  
  <pre><code>#ifndef MOUSE_H_
  #define MOUSE_H_
  
  class Mouse {
  
  public:
      bool leftButtonDown = false;
      bool mouseMoves = false;
  
      struct MouseCoords {
          int x = -1;
          int y = -1;
      };
  
      MouseCoords start_coords;
      MouseCoords move_coords;
      Mouse(){}
  
      void setState(Uint32 eventType, SDL_Event event){
  
          if ( eventType == SDL_MOUSEBUTTONDOWN) {
              this-&gt;leftButtonDown = true;
              SDL_GetMouseState( &amp;this-&gt;start_coords.x, &amp;this-&gt;start_coords.y );
          }
  
          if ( eventType == SDL_MOUSEBUTTONUP ) {
              this-&gt;leftButtonDown = false;
          }
  
          if ( eventType == SDL_MOUSEMOTION ) {
              this-&gt;mouseMoves = true;
              this-&gt;move_coords.x = event.motion.x;
              this-&gt;move_coords.y = event.motion.y;
          } else {
              this-&gt;mouseMoves = false;
          }
      }
  
      /**
       * Provides coordinates when mousebutton down
       */
      MouseCoords getCoordinates(){
          return move_coords;
      }
  
      /**
       * Refresh the coordinates. MUST be called after Mouse::clearCoordinates();
       */
      void clearCoordinates(){
          this-&gt;move_coords.x = -1;
          this-&gt;move_coords.y = -1;
  
      }
  
      /**
       * Creates selector rect, call this in the render loop
       */
       void createSelectBox(SDL_Renderer *renderer){
           if(this-&gt;leftButtonDown) {
              Block rect(
                         this-&gt;start_coords.x, 
                         this-&gt;move_coords.y,
                         this-&gt;start_coords.y -this-&gt;move_coords.y ,
                         this-&gt;move_coords.x - this-&gt;start_coords.x
                         );
               rect.draw( renderer );
               this-&gt;clearCoordinates();
           }
       }
  };
  
  #endif
  </code></pre>
  
  <p>Block.h</p>
  
  <pre><code>#ifndef BLOCK_H_
  #define BLOCK_H_
  /**
   * Standard Class which can be used to build a player, enemy, or something similar
   */
  
  class Block{
      SDL_Rect rect;
  public:
      Block(int x=10, int y=10, int w=10, int h=10, int filled=true){
          rect.x = x;
          rect.y = y;
          rect.w = w;
          rect.h = h;
      }
  
      /**
       * draw with the given rendererer
       */
      void draw(SDL_Renderer *renderer){
          SDL_SetRenderDrawColor( renderer , 200 , 155 , 255 , 255);
          SDL_RenderDrawRect( renderer, &amp;rect );
          SDL_RenderPresent( renderer );
      }
  
      bool framed(){
          return 0;
      }
  
  };
  #endif
  </code></pre>
  
  <p>Then in my main.cpp</p>
  
  <pre><code>SDL_Window *window;
  SDL_Renderer *renderer;
  Mouse mouse;
  Block rect;
  
  
  void renderWindow(){
      window = SDL_CreateWindow("commanding rects", 100, 100, 700, 600, 0);
      renderer = SDL_CreateRenderer(window, -1, 0);
  }
  
  void renderGame(Mouse mouse){
      SDL_RenderSetLogicalSize( renderer, 400, 300 );
      SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0);
      SDL_RenderClear( renderer );
      SDL_RenderPresent( renderer );
      mouse.createSelectBox( renderer );
  
  }
  
  
  void gameLoop(){
      bool game_run = true;
      while (game_run) {
          SDL_Event event;
          while( SDL_PollEvent(&amp;event)){
              if ( event.type == SDL_QUIT ) {
                  game_run = false;
              }
              mouse.setState(event.type, event);
          }
          renderGame(mouse);
          SDL_Delay( 15 );
      }
  
  }
  
  
  int main(){
      renderWindow();
      gameLoop();
      return 0;
  }
  </code></pre>