Guest User

Untitled

a guest
Aug 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #ifndef MOUSE_H_
  2. #define MOUSE_H_
  3.  
  4. class Mouse {
  5.  
  6. public:
  7. bool leftButtonDown = false;
  8. bool mouseMoves = false;
  9.  
  10. struct MouseCoords {
  11. int x = -1;
  12. int y = -1;
  13. };
  14.  
  15. MouseCoords start_coords;
  16. MouseCoords move_coords;
  17. Mouse(){}
  18.  
  19. void setState(Uint32 eventType, SDL_Event event){
  20.  
  21. if ( eventType == SDL_MOUSEBUTTONDOWN) {
  22. this->leftButtonDown = true;
  23. SDL_GetMouseState( &this->start_coords.x, &this->start_coords.y );
  24. }
  25.  
  26. if ( eventType == SDL_MOUSEBUTTONUP ) {
  27. this->leftButtonDown = false;
  28. }
  29.  
  30. if ( eventType == SDL_MOUSEMOTION ) {
  31. this->mouseMoves = true;
  32. this->move_coords.x = event.motion.x;
  33. this->move_coords.y = event.motion.y;
  34. } else {
  35. this->mouseMoves = false;
  36. }
  37. }
  38.  
  39. /**
  40. * Provides coordinates when mousebutton down
  41. */
  42. MouseCoords getCoordinates(){
  43. return move_coords;
  44. }
  45.  
  46. /**
  47. * Refresh the coordinates. MUST be called after Mouse::clearCoordinates();
  48. */
  49. void clearCoordinates(){
  50. this->move_coords.x = -1;
  51. this->move_coords.y = -1;
  52.  
  53. }
  54.  
  55. /**
  56. * Creates selector rect, call this in the render loop
  57. */
  58. void createSelectBox(SDL_Renderer *renderer){
  59. if(this->leftButtonDown) {
  60. Block rect(
  61. this->start_coords.x,
  62. this->move_coords.y,
  63. this->start_coords.y -this->move_coords.y ,
  64. this->move_coords.x - this->start_coords.x
  65. );
  66. rect.draw( renderer );
  67. this->clearCoordinates();
  68. }
  69. }
  70. };
  71.  
  72. #endif
  73.  
  74. #ifndef BLOCK_H_
  75. #define BLOCK_H_
  76. /**
  77. * Standard Class which can be used to build a player, enemy, or something similar
  78. */
  79.  
  80. class Block{
  81. SDL_Rect rect;
  82. public:
  83. Block(int x=10, int y=10, int w=10, int h=10, int filled=true){
  84. rect.x = x;
  85. rect.y = y;
  86. rect.w = w;
  87. rect.h = h;
  88. }
  89.  
  90. /**
  91. * draw with the given rendererer
  92. */
  93. void draw(SDL_Renderer *renderer){
  94. SDL_SetRenderDrawColor( renderer , 200 , 155 , 255 , 255);
  95. SDL_RenderDrawRect( renderer, &rect );
  96. SDL_RenderPresent( renderer );
  97. }
  98.  
  99. bool framed(){
  100. return 0;
  101. }
  102.  
  103. };
  104. #endif
  105.  
  106. SDL_Window *window;
  107. SDL_Renderer *renderer;
  108. Mouse mouse;
  109. Block rect;
  110.  
  111.  
  112. void renderWindow(){
  113. window = SDL_CreateWindow("commanding rects", 100, 100, 700, 600, 0);
  114. renderer = SDL_CreateRenderer(window, -1, 0);
  115. }
  116.  
  117. void renderGame(Mouse mouse){
  118. SDL_RenderSetLogicalSize( renderer, 400, 300 );
  119. SDL_SetRenderDrawColor( renderer, 0, 0, 0, 0);
  120. SDL_RenderClear( renderer );
  121. SDL_RenderPresent( renderer );
  122. mouse.createSelectBox( renderer );
  123.  
  124. }
  125.  
  126.  
  127. void gameLoop(){
  128. bool game_run = true;
  129. while (game_run) {
  130. SDL_Event event;
  131. while( SDL_PollEvent(&event)){
  132. if ( event.type == SDL_QUIT ) {
  133. game_run = false;
  134. }
  135. mouse.setState(event.type, event);
  136. }
  137. renderGame(mouse);
  138. SDL_Delay( 15 );
  139. }
  140.  
  141. }
  142.  
  143.  
  144. int main(){
  145. renderWindow();
  146. gameLoop();
  147. return 0;
  148. }
  149.  
  150. #ifndef MOUSE_H_
  151. #define MOUSE_H_
  152.  
  153. class Mouse {
  154.  
  155. public:
  156. bool leftButtonDown = false;
  157. bool mouseMoves = false;
  158.  
  159. struct MouseCoords {
  160. int x = -1;
  161. int y = -1;
  162. };
  163.  
  164. MouseCoords start_coords;
  165. MouseCoords move_coords;
  166. Mouse(){}
  167.  
  168. void setState(Uint32 eventType, SDL_Event event){
  169.  
  170. if ( eventType == SDL_MOUSEBUTTONDOWN) {
  171. this->leftButtonDown = true;
  172. this->start_coords.x = event.button.x;
  173. this->start_coords.y = event.button.y;
  174. }
  175.  
  176. if ( eventType == SDL_MOUSEBUTTONUP ) {
  177. this->leftButtonDown = false;
  178. }
  179.  
  180. if ( eventType == SDL_MOUSEMOTION ) {
  181. this->move_coords.x = event.motion.x;
  182. this->move_coords.y = event.motion.y;
  183. }
  184. }
  185.  
  186. /**
  187. * Provides coordinates when mousebutton down
  188. */
  189. MouseCoords getCoordinates(){
  190. return move_coords;
  191. }
  192.  
  193.  
  194. /**
  195. * Creates selector rect, call this in the render loop
  196. */
  197. void createSelectBox(SDL_Renderer *renderer){
  198.  
  199. if(this->leftButtonDown) {
  200.  
  201. float width = this->move_coords.x - this->start_coords.x;
  202. float height = this->start_coords.y - this->move_coords.y;
  203. float x = this->start_coords.x ;
  204. float y = this->move_coords.y;
  205.  
  206. Block rect(x, y,width, height);
  207. rect.draw( renderer );
  208.  
  209.  
  210.  
  211. }
  212.  
  213.  
  214. }
  215. };
  216.  
  217. #endif
Add Comment
Please, Sign In to add comment