Advertisement
Ladies_Man

ccg map 1

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