Advertisement
Ladies_Man

ccg simple double linked list IMPROVED

Sep 28th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.16 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.     Node *next_r, *prev_l;
  381.     Node(int y) { value = y; next_r = prev_l = NULL;}
  382. };
  383.  
  384. class dll {
  385. public:
  386.  
  387.     void append_front(int);
  388.     void append_back(int);
  389.     void display_forward();
  390.     void display_backward();
  391.     void destroy_list();
  392.  
  393.     dll() { front = NULL; back = NULL; }
  394.     ~dll(){ destroy_list(); }
  395.  
  396. private:
  397.     Node *front;
  398.     Node *back;
  399.  
  400. };
  401.  
  402. void dll::append_front(int x) {
  403.  
  404.     Node *n = new Node(x);
  405.  
  406.     if (front == NULL) {
  407.         front = n;
  408.         back = n;
  409.     } else {
  410.         front->prev_l = n;
  411.         n->next_r = front;
  412.         front = n;
  413.     }
  414.     cout << "appended in fornt" << endl;
  415. }
  416.  
  417. void dll::append_back(int x) {
  418.  
  419.     Node *n = new Node(x);
  420.  
  421.     if (back == NULL) {
  422.         front = n;
  423.         back = n;
  424.     } else {
  425.         back->next_r = n;
  426.         n->prev_l = back;
  427.         back = n;
  428.     }
  429.     cout << "appended to back" << endl;
  430. }
  431.  
  432. void dll::display_forward() {
  433.     Node *temp = front;
  434.  
  435.     cout << "\n\nNodes in forward order:" << endl;
  436.  
  437.     while (temp != NULL) {
  438.         cout << temp->value << "   ";
  439.         temp = temp->next_r;
  440.     }
  441. }
  442.  
  443. void dll::display_backward() {
  444.     Node *temp = back;
  445.  
  446.     cout << "\n\nNodes in reverse order :" << endl;
  447.  
  448.     while (temp != NULL) {
  449.         cout << temp->value << "   ";
  450.         temp = temp->prev_l;
  451.     }
  452. }
  453.  
  454. void dll::destroy_list() {
  455.     Node *T = back;
  456.  
  457.     while (T != NULL) {
  458.         Node *T2 = T;
  459.         T = T->prev_l;
  460.         delete T2;
  461.     }
  462.  
  463.     front = NULL;
  464.     back = NULL;
  465. }
  466.  
  467.  
  468.  
  469. static void cursor_callback(GLFWwindow* window, double x, double y)
  470. {
  471.  
  472. }
  473.  
  474. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  475. {
  476.     if (button == GLFW_MOUSE_BUTTON_RIGHT)
  477.     {
  478.         if (action == GLFW_PRESS) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  479.         if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  480.     }
  481.  
  482.     if (button == GLFW_MOUSE_BUTTON_LEFT)
  483.     {
  484.  
  485.     }
  486. }
  487.  
  488. static void resize_callback(GLFWwindow* window, int width, int height)
  489. {
  490.     //      windowWidth = width;
  491.     //      windowHeight = height;
  492.  
  493.     glViewport(0, 0, width, height);
  494.     glMatrixMode(GL_PROJECTION);
  495.     glLoadIdentity();
  496.     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
  497.  
  498.     glMatrixMode(GL_MODELVIEW);
  499.     glLoadIdentity();
  500.  
  501.     A = width / 4.0;
  502.     B = 0.0;
  503.     C = D = height / 2.0;
  504.  
  505.     printf("Reshape occured\n");
  506. }
  507.  
  508. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  509. {
  510.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  511.         glfwSetWindowShouldClose(window, GL_TRUE);
  512.     //if (GLFW_KEY_T == key && action == GLFW_PRESS) { gravity(); }
  513. }
  514.  
  515. static void error_callback(int error, const char* description)
  516. {
  517.     fputs(description, stderr);
  518. }
  519.  
  520.  
  521.  
  522.  
  523. int main(int argc, _TCHAR* argv[])
  524. {
  525.     if (!glfwInit()) {
  526.         printf("glfwInit failed\n");
  527.         return -1;
  528.     }
  529.  
  530.     glfwSetErrorCallback(error_callback);
  531.  
  532.     GLFWwindow* window;
  533.  
  534.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  535.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  536.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  537.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
  538.     if (window == NULL) {
  539.         printf("glfwOpenWindow failed.\n");
  540.         glfwTerminate();
  541.         return -2;
  542.     }
  543.  
  544.     int attrib;
  545.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  546.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  547.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  548.  
  549.     glfwMakeContextCurrent(window);
  550.  
  551.     glfwSetKeyCallback(window, keyboard_callback);
  552.     glfwSetFramebufferSizeCallback(window, resize_callback);
  553.     glfwSetMouseButtonCallback(window, mouse_callback);
  554.     glfwSetCursorPosCallback(window, cursor_callback);
  555.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  556.    
  557.     GLfloat phi = 0, center = 0;
  558.     int i;
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.     t = 0;
  567.  
  568.  
  569.     while (!glfwWindowShouldClose(window))
  570.     {
  571.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  572.  
  573.         if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
  574.             i += 5;
  575.         }
  576.         if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  577.             i -= 5;
  578.         }
  579.         if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  580.             phi += 0.3;
  581.         }
  582.         if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
  583.             phi -= 0.3;
  584.         }
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.         glfwSwapBuffers(window);
  592.         glfwPollEvents();
  593.         //glfwWaitEvents();
  594.     }
  595.  
  596.     glfwDestroyWindow(window);
  597.     glfwTerminate();
  598.  
  599.     return 0;
  600. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement