Ladies_Man

ccg double linked list w/ pointers to obj

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