Advertisement
Ladies_Man

ccg simple block falling

Sep 20th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. // glfw_30.cpp : Defines the entry point for the console application.
  2. //  http://www.glfw.org/docs/latest/quick.html
  3.  
  4. #include "stdafx.h"
  5. #include <time.h>
  6. #define _USE_MATH_DEFINES
  7. #include <cmath>
  8.  
  9. #define SCREEN_WIDTH 1000
  10. #define SCREEN_HEIGHT 800
  11.  
  12. #define G_CONST 9.78
  13.  
  14. float t;
  15.  
  16. GLdouble A, B, C, D;
  17.  
  18. float delta_pos(float velocity, float acceleration);
  19.  
  20.  
  21.  
  22. class block {
  23. public:
  24.     char *name;
  25.     int mass;
  26.  
  27.     void grav();
  28.     void init();
  29.  
  30.     block(float pos_x, float pos_y) {
  31.         this->init_x = pos_x;
  32.         this->init_y = pos_y;
  33.  
  34.         this->curr_x = pos_x;
  35.         this->curr_y = pos_y;
  36.  
  37.         this->visibility = true;
  38.     }
  39.  
  40. private:
  41.     float init_x, init_y;
  42.     float curr_x, curr_y;
  43.     bool visibility;
  44.  
  45.     void draw(float pos_x, float pos_y);
  46. };
  47.  
  48. void block::draw(float pos_x, float pos_y) {
  49.     glBegin(GL_POLYGON);
  50.         glColor3f(1, 0, 0);
  51.         glVertex2i(pos_x, pos_y);
  52.         glVertex2i(pos_x + 20, pos_y);
  53.         glVertex2i(pos_x + 20, pos_y + 20);
  54.         glVertex2i(pos_x, pos_y + 20);
  55.     glEnd();
  56. }
  57.  
  58. void block::grav() {
  59.     if (this->curr_y > 0) {
  60.  
  61.         float pos_x = this->curr_x + delta_pos(0, 15);
  62.         float pos_y = this->curr_y - delta_pos(3, G_CONST);
  63.  
  64.         this->curr_x = pos_x;
  65.         this->curr_y = pos_y;
  66.  
  67.     } else {
  68.         this->curr_y = 0;
  69.         this->visibility = false;
  70.     }
  71.  
  72.     this->draw(this->curr_x, this->curr_y);
  73.  
  74.     printf("x:%f, y:%f\n", this->curr_x, this->curr_y);
  75. }
  76.  
  77. void block::init() {
  78.     float pos_x = this->init_x;
  79.     float pos_y = this->init_y;
  80.  
  81.     this->draw(pos_x, pos_y);
  82. }
  83.  
  84.  
  85. float delta_pos(float velocity, float acceleration) {
  86.     //s = s0 + v*t + (a*t^2)/2
  87.     float delta = velocity * t + (acceleration * t * t) / 2;
  88.     printf("delta=%f\n", delta);
  89.    
  90.     t += 0.001;
  91.    
  92.     return delta;
  93. }
  94.  
  95. int count_height(int curr_height) {
  96.     //h = y - (g*t^2)/2
  97.     float h = curr_height - (G_CONST * t * t) / 2;
  98.     t += 0.00001;
  99.     return (int)h;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. void gravity() {
  106.     printf("grav");
  107.  
  108.    
  109. }
  110.  
  111. static void cursor_callback(GLFWwindow* window, double x, double y)
  112. {
  113.  
  114. }
  115.  
  116. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  117. {
  118.     if (button == GLFW_MOUSE_BUTTON_RIGHT)
  119.     {
  120.         if (action == GLFW_PRESS) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  121.         if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  122.     }
  123.  
  124.     if (button == GLFW_MOUSE_BUTTON_LEFT)
  125.     {
  126.  
  127.     }
  128. }
  129.  
  130. static void resize_callback(GLFWwindow* window, int width, int height)
  131. {
  132.     //      windowWidth = width;
  133.     //      windowHeight = height;
  134.  
  135.     glViewport(0, 0, width, height);
  136.     glMatrixMode(GL_PROJECTION);
  137.     glLoadIdentity();
  138.     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
  139.  
  140.     glMatrixMode(GL_MODELVIEW);
  141.     glLoadIdentity();
  142.  
  143.     A = width / 4.0;
  144.     B = 0.0;
  145.     C = D = height / 2.0;
  146.  
  147.     printf("Reshape occured\n");
  148. }
  149.  
  150. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  151. {
  152.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  153.         glfwSetWindowShouldClose(window, GL_TRUE);
  154.     if (GLFW_KEY_T == key && action == GLFW_PRESS) { gravity(); }
  155. }
  156.  
  157. static void error_callback(int error, const char* description)
  158. {
  159.     fputs(description, stderr);
  160. }
  161.  
  162.  
  163. int main(int argc, _TCHAR* argv[])
  164. {
  165.     // initialise GLFW
  166.     if (!glfwInit())
  167.     {
  168.         printf("glfwInit failed\n");
  169.         return -1;
  170.     }
  171.  
  172.     glfwSetErrorCallback(error_callback);
  173.  
  174.     GLFWwindow* window;
  175.  
  176.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  177.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  178.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  179.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  180.     if (window == NULL)
  181.     {
  182.         printf("glfwOpenWindow failed.\n");
  183.         glfwTerminate();
  184.         return -2;
  185.     }
  186.  
  187.     int attrib;
  188.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  189.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  190.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  191.  
  192.     glfwMakeContextCurrent(window);
  193.  
  194.     glfwSetKeyCallback(window, keyboard_callback);
  195.     glfwSetFramebufferSizeCallback(window, resize_callback);
  196.     glfwSetMouseButtonCallback(window, mouse_callback);
  197.     glfwSetCursorPosCallback(window, cursor_callback);
  198.  
  199.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  200.     GLfloat phi = 0, center = 0;
  201.     int i = 1;
  202.  
  203.     block b(100, 700);
  204.     //b.init();
  205.  
  206.     t = 0;
  207.  
  208.     while (!glfwWindowShouldClose(window))
  209.     {
  210.         // ESC = exit
  211.         // ^, v = change size
  212.         // <--, --> = rotate left, right
  213.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  214.  
  215.         if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
  216.             i += 5;
  217.         }
  218.         if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  219.             i -= 5;
  220.         }
  221.         if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  222.             phi += 0.3;
  223.         }
  224.         if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
  225.             phi -= 0.3;
  226.         }
  227.  
  228.        
  229.         b.grav();
  230.  
  231.  
  232.        
  233.         glfwSwapBuffers(window);
  234.         glfwPollEvents();
  235.         //glfwWaitEvents();
  236.     }
  237.  
  238.     glfwDestroyWindow(window);
  239.  
  240.     // clean up and exit
  241.     glfwTerminate();
  242.  
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement