Ladies_Man

ccg list nodes point to phys objects improved

Sep 27th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.82 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. #include <stdio.h>
  9. #include <iostream>
  10.  
  11. #define SCREEN_WIDTH 700
  12. #define SCREEN_HEIGHT 500
  13.  
  14. #define G_CONST 9.78
  15.  
  16. using namespace std;
  17.  
  18. float t;
  19.  
  20. GLdouble A, B, C, D;
  21.  
  22. float delta_pos(float velocity, float acceleration);
  23.  
  24. class system;
  25. class body;
  26. class block;
  27. class cargo;
  28. class pendulum;
  29.  
  30.  
  31.  
  32.  
  33.  
  34. class body {
  35. public:
  36.     char *name;
  37.     int type, num, mass;
  38.     body* up;
  39.     body* down;
  40.     body* right;
  41.     body* left;
  42.  
  43.     void set(int, int, int, float, float);
  44.     void draw();
  45.  
  46. private:
  47.     float curr_x, curr_y;
  48.     float init_x, init_y;
  49.     float center_x, center_y;
  50.     int h, w;
  51.  
  52.  
  53. };
  54.  
  55. void body::set(int num, int type, int mass, float pos_x, float pos_y) {
  56.     this->init_x = pos_x;
  57.     this->init_y = pos_y;
  58.  
  59.     this->curr_x = pos_x;
  60.     this->curr_y = pos_y;
  61.  
  62.     this->type = type;
  63.     this->num = num;
  64.     this->mass = mass;
  65.  
  66.     this->up = NULL;
  67.     this->down = NULL;
  68.     this->right = NULL;
  69.     this->left = NULL;
  70.  
  71.     this->h = 20;
  72.     this->w = 30;
  73. }
  74.  
  75. void body::draw() {
  76.     float pos_x = this->curr_x;
  77.     float pos_y = this->curr_y;
  78.     glBegin(GL_POLYGON);
  79.     glColor3f(0, 0, 1);
  80.     glVertex2i(pos_x, pos_y);
  81.     glVertex2i(pos_x + this->w, pos_y);
  82.     glVertex2i(pos_x + this->w, pos_y + this->h);
  83.     glVertex2i(pos_x, pos_y + this->h);
  84.     glEnd();
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. class system {
  93. public:
  94.     char *name;
  95.     int el_num;
  96.     int c_num, b_num;
  97.  
  98.     void draw_all();
  99.     void add_elem(body*);
  100.  
  101. private:
  102.  
  103.  
  104. };
  105.  
  106. void system::draw_all() {
  107.     int i;
  108.     for (i = 0; i < this->el_num; i++) {
  109.  
  110.     }
  111. }
  112.  
  113. void system::add_elem(body* elem) {
  114.  
  115. }
  116.  
  117. /*
  118.  
  119. class block {
  120. public:
  121. char *name;
  122. int ord_num;
  123. block* left_block;
  124. block* right_block;
  125. cargo* left_cargo;
  126. cargo* right_cargo;
  127.  
  128. void init();
  129. void add_right_block(block*);
  130. void add_left_block(block*);
  131. void add_right_cargo(cargo*);
  132. void add_left_cargo(cargo*);
  133.  
  134. block(int n, float pos_x, float pos_y) {
  135. this->ord_num = n;
  136. this->curr_x = pos_x;
  137. this->curr_y = pos_y;
  138. }
  139.  
  140. private:
  141. float curr_x, curr_y;
  142.  
  143. void draw(float, float);
  144. };
  145.  
  146. void block::draw(float pos_x, float pos_y) {
  147. glBegin(GL_POLYGON);
  148. glColor3f(0, 0, 1);
  149. glVertex2i(pos_x, pos_y);
  150. glVertex2i(pos_x + 20, pos_y);
  151. glVertex2i(pos_x + 20, pos_y + 20);
  152. glVertex2i(pos_x, pos_y + 20);
  153. glEnd();
  154. }
  155.  
  156. void block::init() {
  157. float pos_x = this->curr_x;
  158. float pos_y = this->curr_y;
  159.  
  160. this->draw(pos_x, pos_y);
  161. }
  162.  
  163. void block::add_right_block(block* b) {
  164. this->right_block = b;
  165. b->left_block = this;
  166. }
  167.  
  168. void block::add_left_block(block* b) {
  169. this->left_block = b;
  170. b->right_block = this;
  171. }
  172.  
  173.  
  174. */
  175.  
  176. float delta_pos(float velocity, float acceleration);
  177.  
  178.  
  179.  
  180. class block {
  181. public:
  182.     char *name;
  183.     int mass;
  184.  
  185.     void grav();
  186.     void init();
  187.  
  188.     block(float pos_x, float pos_y) {
  189.         this->init_x = pos_x;
  190.         this->init_y = pos_y;
  191.  
  192.         this->curr_x = pos_x;
  193.         this->curr_y = pos_y;
  194.  
  195.         this->visibility = true;
  196.     }
  197.  
  198. private:
  199.     float init_x, init_y;
  200.     float curr_x, curr_y;
  201.     bool visibility;
  202.  
  203.     void draw(float pos_x, float pos_y);
  204. };
  205.  
  206. void block::draw(float pos_x, float pos_y) {
  207.     glBegin(GL_POLYGON);
  208.     glColor3f(1, 0, 0);
  209.     glVertex2i(pos_x, pos_y);
  210.     glVertex2i(pos_x + 20, pos_y);
  211.     glVertex2i(pos_x + 20, pos_y + 20);
  212.     glVertex2i(pos_x, pos_y + 20);
  213.     glEnd();
  214. }
  215.  
  216. void block::grav() {
  217.     if (this->curr_y > 0) {
  218.  
  219.         float pos_x = this->curr_x + delta_pos(0, 0);
  220.         float pos_y = this->curr_y - delta_pos(-2, G_CONST);
  221.  
  222.         this->curr_x = pos_x;
  223.         this->curr_y = pos_y;
  224.  
  225.     }
  226.     else {
  227.         this->curr_y = 0;
  228.         this->visibility = false;
  229.     }
  230.  
  231.     this->draw(this->curr_x, this->curr_y);
  232.  
  233.     printf("x:%f, y:%f\n", this->curr_x, this->curr_y);
  234. }
  235.  
  236. void block::init() {
  237.     float pos_x = this->init_x;
  238.     float pos_y = this->init_y;
  239.  
  240.     this->draw(pos_x, pos_y);
  241. }
  242.  
  243.  
  244. float delta_pos(float velocity, float acceleration) {
  245.     //s = s0 + v*t + (a*t^2)/2
  246.     float delta = velocity * t + (acceleration * t * t) / 2;
  247.     printf("delta=%f\n", delta);
  248.  
  249.     t += 0.0001;
  250.  
  251.     return delta;
  252. }
  253.  
  254.  
  255.  
  256.  
  257. class cargo {
  258. public:
  259.     char *name;
  260.     int ord_num;
  261.     int mass;
  262.     int move_direction;
  263.     block* left_block;
  264.     block* right_block;
  265.     cargo* above_cargo;
  266.     cargo* below_cargo;
  267.  
  268.     void grav(bool);
  269.     void init();
  270.     void add_right_block(block*);
  271.     void add_left_block(block*);
  272.     void add_above_cargo(cargo*);
  273.     void add_below_cargo(cargo*);
  274.  
  275.     cargo(int n, float pos_x, float pos_y) {
  276.         this->ord_num = n;
  277.  
  278.         this->init_x = pos_x;
  279.         this->init_y = pos_y;
  280.  
  281.         this->curr_x = pos_x;
  282.         this->curr_y = pos_y;
  283.  
  284.         this->velo_x = 0;
  285.         this->velo_y = 0;
  286.  
  287.         if (this->velo_y == 0) {
  288.             this->move_direction = -1;
  289.         }
  290.     }
  291.  
  292. private:
  293.     float init_x, init_y;
  294.     float curr_x, curr_y;
  295.     float velo_x, velo_y;
  296.  
  297.     void draw(float, float);
  298. };
  299.  
  300. void cargo::draw(float pos_x, float pos_y) {
  301.     glBegin(GL_POLYGON);
  302.     glColor3f(0, 1, 0);
  303.     glVertex2i(pos_x, pos_y);
  304.     glVertex2i(pos_x + 25, pos_y);
  305.     glVertex2i(pos_x + 25, pos_y + 25);
  306.     glVertex2i(pos_x, pos_y + 25);
  307.     glEnd();
  308. }
  309.  
  310. void cargo::grav(bool fall) {
  311.     if (fall) {
  312.  
  313.         float pos_y;
  314.  
  315.         if (this->curr_y <= 0) {
  316.  
  317.             this->curr_y = 0;
  318.  
  319.             this->velo_y = sqrt(2 * G_CONST * this->init_y) * 0.8;
  320.  
  321.             this->move_direction = 1;
  322.  
  323.         }
  324.  
  325.         if (this->curr_y >= 0) {
  326.  
  327.             if (this->move_direction < 0) {
  328.  
  329.                 float pos_y = (G_CONST * t * t) / 2;
  330.                 this->curr_y -= pos_y;
  331.  
  332.             }
  333.  
  334.             if (this->move_direction > 0) {
  335.  
  336.                 float pos_y = this->velo_y * t - (G_CONST * t * t) / 2;
  337.                 this->curr_y += pos_y;
  338.  
  339.             }
  340.  
  341.         }
  342.     }
  343.  
  344.     this->draw(this->curr_x, this->curr_y);
  345.  
  346.     t += 0.001;
  347.  
  348.     printf("x:%f, y:%f, v=%f\n", this->curr_x, this->curr_y, this->velo_y);
  349. }
  350.  
  351. void cargo::init() {
  352.     float pos_x = this->init_x;
  353.     float pos_y = this->init_y;
  354.  
  355.     this->draw(pos_x, pos_y);
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363. /*
  364. typedef struct node {
  365.     int info;
  366.     node* prev_left;
  367.     node* next_right;
  368.     node* above;
  369.     node* below;
  370.  
  371.     body* body;
  372.  
  373. };*/
  374.  
  375.  
  376.  
  377. struct node
  378. {
  379.     int value;
  380.     body obj;
  381.     node *next_r, *prev_l;
  382.  
  383.     node(int y) { value = y; next_r = prev_l = NULL; }
  384. };
  385.  
  386. class dll {
  387. public:
  388.  
  389.     void append_front(int, body);
  390.     void append_back(int, body);
  391.     void display_forward();
  392.     void display_backward();
  393.     void display_objects();
  394.     void destroy_list();
  395.  
  396.     dll() { front = NULL; back = NULL; }
  397.     ~dll(){ destroy_list(); }
  398.  
  399.  
  400. private:
  401.  
  402.     node *front;
  403.     node *back;
  404.  
  405. };
  406.  
  407. void dll::append_front(int x, body b) {
  408.  
  409.     node *n = new node(x);
  410.     body t;
  411.  
  412.     n->obj = b;
  413.  
  414.     if (front == NULL) {
  415.         front = n;
  416.         back = n;
  417.     } else {
  418.         t = front->obj;
  419.  
  420.         front->prev_l = n;
  421.         n->next_r = front;
  422.  
  423.         n->next_r->obj = t;
  424.  
  425.         front = n;
  426.     }
  427.  
  428.     cout << "appended in fornt" << endl;
  429. }
  430.  
  431. void dll::append_back(int x, body b) {
  432.  
  433.     node *n = new node(x);
  434.     body t;
  435.  
  436.     n->obj = b;
  437.  
  438.     if (back == NULL) {
  439.         front = n;
  440.         back = n;
  441.     } else {
  442.         t = back->obj;
  443.  
  444.         back->next_r = n;
  445.        
  446.         n->obj = b;
  447.  
  448.         n->prev_l = back;
  449.         n->prev_l->obj = t;
  450.  
  451.         back = n;
  452.     }
  453.  
  454.     cout << "appended to back" << endl;
  455. }
  456.  
  457. void dll::display_forward() {
  458.     node *temp = front;
  459.  
  460.     cout << "\n\nnodes in forward order:" << endl;
  461.  
  462.     while (temp != NULL) {
  463.         cout << temp->value << "   ";
  464.         temp = temp->next_r;
  465.     }
  466. }
  467.  
  468. void dll::display_backward() {
  469.     node *temp = back;
  470.  
  471.     cout << "\n\nnodes in reverse order :" << endl;
  472.  
  473.     while (temp != NULL) {
  474.         cout << temp->value << "   ";
  475.         temp = temp->prev_l;
  476.     }
  477. }
  478.  
  479. void dll::display_objects() {
  480.     node *temp = front;
  481.  
  482.     cout << "\n\nobjects in forward order:" << endl;
  483.  
  484.     while (NULL != temp) {
  485.         printf("(%d [n:%d, m:%d])   ", temp->value, temp->obj.num, temp->obj.mass);
  486.         temp = temp->next_r;
  487.     }
  488. }
  489.  
  490. void dll::destroy_list() {
  491.     node *T = back;
  492.  
  493.     while (T != NULL) {
  494.         node *T2 = T;
  495.         T = T->prev_l;
  496.         delete T2;
  497.     }
  498.  
  499.     front = NULL;
  500.     back = NULL;
  501. }
  502.  
  503.  
  504.  
  505. static void cursor_callback(GLFWwindow* window, double x, double y)
  506. {
  507.  
  508. }
  509.  
  510. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  511. {
  512.     if (button == GLFW_MOUSE_BUTTON_RIGHT)
  513.     {
  514.         if (action == GLFW_PRESS) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  515.         if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  516.     }
  517.  
  518.     if (button == GLFW_MOUSE_BUTTON_LEFT)
  519.     {
  520.  
  521.     }
  522. }
  523.  
  524. static void resize_callback(GLFWwindow* window, int width, int height)
  525. {
  526.     //      windowWidth = width;
  527.     //      windowHeight = height;
  528.  
  529.     glViewport(0, 0, width, height);
  530.     glMatrixMode(GL_PROJECTION);
  531.     glLoadIdentity();
  532.     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
  533.  
  534.     glMatrixMode(GL_MODELVIEW);
  535.     glLoadIdentity();
  536.  
  537.     A = width / 4.0;
  538.     B = 0.0;
  539.     C = D = height / 2.0;
  540.  
  541.     printf("Reshape occured\n");
  542. }
  543.  
  544. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  545. {
  546.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  547.         glfwSetWindowShouldClose(window, GL_TRUE);
  548.     //if (GLFW_KEY_T == key && action == GLFW_PRESS) { gravity(); }
  549. }
  550.  
  551. static void error_callback(int error, const char* description)
  552. {
  553.     fputs(description, stderr);
  554. }
  555.  
  556.  
  557.  
  558.  
  559. int main(int argc, _TCHAR* argv[])
  560. {
  561.     if (!glfwInit()) {
  562.         printf("glfwInit failed\n");
  563.         return -1;
  564.     }
  565.  
  566.     glfwSetErrorCallback(error_callback);
  567.  
  568.     GLFWwindow* window;
  569.  
  570.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  571.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  572.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  573.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
  574.     if (window == NULL) {
  575.         printf("glfwOpenWindow failed.\n");
  576.         glfwTerminate();
  577.         return -2;
  578.     }
  579.  
  580.     int attrib;
  581.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  582.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  583.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  584.  
  585.     glfwMakeContextCurrent(window);
  586.  
  587.     glfwSetKeyCallback(window, keyboard_callback);
  588.     glfwSetFramebufferSizeCallback(window, resize_callback);
  589.     glfwSetMouseButtonCallback(window, mouse_callback);
  590.     glfwSetCursorPosCallback(window, cursor_callback);
  591.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  592.    
  593.     GLfloat phi = 0, center = 0;
  594.     int i;
  595.  
  596.  
  597.     dll *list = new dll();
  598.     body aa;
  599.  
  600.     for (i = 0; i < 5; i++) {
  601.         aa.num = i;
  602.         aa.mass = 322 + i;
  603.  
  604.         list->append_back(i, aa);
  605.     }
  606.     list->display_forward();
  607.     list->display_objects();
  608.  
  609.  
  610.     t = 0;
  611.  
  612.  
  613.     while (!glfwWindowShouldClose(window))
  614.     {
  615.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  616.  
  617.         if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
  618.             i += 5;
  619.         }
  620.         if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  621.             i -= 5;
  622.         }
  623.         if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  624.             phi += 0.3;
  625.         }
  626.         if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
  627.             phi -= 0.3;
  628.         }
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.         glfwSwapBuffers(window);
  636.         glfwPollEvents();
  637.         //glfwWaitEvents();
  638.     }
  639.  
  640.     glfwDestroyWindow(window);
  641.     glfwTerminate();
  642.  
  643.     return 0;
  644. }
Advertisement
Add Comment
Please, Sign In to add comment