Advertisement
Ladies_Man

ccg simplified to nodes + dll

Nov 29th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.27 KB | None | 0 0
  1. // stdafx.h : include file for standard system include files,
  2. // or project specific include files that are used frequently, but
  3. // are changed infrequently
  4. //
  5. #pragma once
  6.  
  7. #include "targetver.h"
  8.  
  9. #define _CRT_SECURE_NO_WARNINGS
  10.  
  11. //if print is lagging switch this
  12. //#include <stdio.h>       
  13. #include <iostream>
  14. #include <vector>
  15. #include <tchar.h>
  16. #include <stdlib.h>
  17.  
  18. #define _USE_MATH_DEFINES
  19. #include <cmath>
  20. #include <math.h>
  21.  
  22. #include <ctime>
  23. #include <time.h>
  24.  
  25. #include <Windows.h>
  26.  
  27. #include <string>
  28.  
  29. #define GLEW_STATIC
  30. #include <GL\glew.h>
  31. #include <GLFW\glfw3.h>
  32.  
  33. typedef struct { int x, y; } coord;
  34.  
  35.  
  36. #include "body.h"
  37.  
  38. /*struct node {
  39.     //char value[10];
  40.     int value;
  41.     body obj;
  42.     node *next_r, *prev_l;
  43.  
  44.     node(int y) { value = y; next_r = prev_l = NULL; }
  45. };*/
  46.  
  47.  
  48. #include "cell.h"
  49. #include "map.h"
  50. #include "node.h"
  51. #include "dll.h"
  52.  
  53. #define SCREEN_WIDTH 1000
  54. #define SCREEN_HEIGHT 1000
  55. #define SPLIT_X 10
  56. #define SPLIT_Y 10
  57. #define G_CONST -9.78
  58. #define MAXRANDID 99999
  59.  
  60. #define REVD(COORD_Y) ((SPLIT_Y - 1) - (COORD_Y))
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. node.h
  73. #pragma once
  74. class node
  75. {
  76. public:
  77.     int id;
  78.     char type;
  79.     int listpos;
  80.  
  81.     node *next_node;
  82.     node *prev_node;
  83.  
  84.     float init_pos_x;
  85.     float init_pos_y;
  86.     int mass;
  87.  
  88.     void set(char type, int id, int listpos, float pos_x, float pos_y, int mass);
  89.     void draw();
  90.  
  91.     node(int);
  92.     ~node();
  93.  
  94. private:
  95.     float velocity, accel;
  96.     float curr_x, curr_y;
  97.     int size_x, size_y;
  98. };
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. node.cpp
  106. #include "stdafx.h"
  107.  
  108. using namespace std;
  109.  
  110. node::node(int x)
  111. {
  112.     id = x;
  113.     next_node = NULL;
  114.     prev_node = NULL;
  115. }
  116.  
  117. node::~node()
  118. {
  119. }
  120.  
  121. void node::set(char type, int id, int listpos, float pos_x, float pos_y, int mass) {
  122.     this->init_pos_x = pos_x;
  123.     this->init_pos_y = pos_y;
  124.  
  125.     this->curr_x = pos_x;
  126.     this->curr_y = pos_y;
  127.  
  128.     this->type = type;
  129.  
  130.     this->id = id;
  131.  
  132.     this->listpos = listpos;
  133.  
  134.     this->mass = mass;
  135.  
  136.     this->size_x = 100;
  137.     this->size_y = 100;
  138.  
  139. }
  140.  
  141. void node::draw() {
  142.     float center_x = this->curr_x;
  143.     float center_y = this->curr_y;
  144.  
  145.     int size_x = this->size_x;
  146.     int size_y = this->size_y;
  147.     int rad = size_x / 2;
  148.  
  149.     int border = 5;
  150.  
  151.     if ('c' == this->type) {
  152.  
  153.         coord lb, lt, rt, rb;
  154.  
  155.         lb.x = center_x - size_x / 2;
  156.         lb.y = center_y - size_y / 2;
  157.  
  158.         lt.x = center_x - size_x / 2;
  159.         lt.y = center_y + size_y / 2;
  160.  
  161.         rt.x = center_x + size_x / 2;
  162.         rt.y = center_y + size_y / 2;
  163.  
  164.         rb.x = center_x + size_x / 2;
  165.         rb.y = center_y - size_y / 2;
  166.  
  167.         //outer black
  168.         glBegin(GL_POLYGON);
  169.         glColor3f(0, 0, 0);
  170.  
  171.         glVertex2i(lb.x, lb.y);
  172.         glVertex2i(lt.x, lt.y);
  173.         glVertex2i(rt.x, rt.y);
  174.         glVertex2i(rb.x, rb.y);
  175.         glEnd();
  176.  
  177.         //inner white
  178.         glBegin(GL_POLYGON);
  179.         glColor3f(1, 1, 1);
  180.  
  181.         glVertex2i(lb.x + border, lb.y + border);
  182.         glVertex2i(lt.x + border, lt.y - border);
  183.         glVertex2i(rt.x - border, rt.y - border);
  184.         glVertex2i(rb.x - border, rb.y + border);
  185.         glEnd();
  186.     }
  187.  
  188.     if ('b' == this->type) {
  189.  
  190.         GLfloat phi = 0, theta = 0;
  191.  
  192.         //outer black
  193.         glBegin(GL_POLYGON);
  194.         glColor3f(0, 0, 0);
  195.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  196.             glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  197.         }
  198.         glEnd();
  199.  
  200.         //inner white
  201.         glBegin(GL_POLYGON);
  202.         glColor3f(1, 1, 1);
  203.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  204.             glVertex2f(center_x + (rad - border)*cos(phi), center_y + (rad - border)*sin(phi));
  205.         }
  206.         glEnd();
  207.  
  208.     }
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. dll.h
  217. class dll {
  218. public:
  219.  
  220.     void append_front(int id);
  221.     void append_back(int id);
  222.     node* get_front();
  223.     node* get_back();
  224.     node* get_by_id(int id);
  225.     node* get_by_pos(int pos);
  226.     void display_forward();
  227.     void display_backward();
  228.     void display_objects();
  229.     void draw_objects();
  230.     void destroy_list();
  231.     void enable_fall(float time);
  232.     void connect_bodies(node from, node to);
  233.  
  234.     dll() { front = NULL; back = NULL; }
  235.     ~dll() { destroy_list(); }
  236.  
  237.  
  238. private:
  239.  
  240.     node *front;
  241.     node *back;
  242.  
  243. };
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251. node.cpp
  252. #include "stdafx.h"
  253.  
  254. using namespace std;
  255.  
  256. void dll::append_front(int x) {
  257.  
  258.     node *n = new node(x);
  259.  
  260.     if (front == NULL) {
  261.         front = n;
  262.         back = n;
  263.     }
  264.     else {
  265.         front->prev_node = n;
  266.         n->next_node = front;
  267.         front = n;
  268.     }
  269.  
  270.     printf("fornt appended\n");
  271. }
  272.  
  273. void dll::append_back(int x) {
  274.  
  275.     node *n = new node(x);
  276.  
  277.     if (back == NULL) {
  278.         front = n;
  279.         back = n;
  280.     }
  281.     else {
  282.         back->next_node = n;
  283.         n->prev_node = back;
  284.         back = n;
  285.     }
  286.  
  287.     printf("back appended\n");
  288. }
  289.  
  290. node* dll::get_front() {
  291.     return this->front;
  292. }
  293.  
  294. node* dll::get_back() {
  295.     return this->back;
  296. }
  297.  
  298. node* dll::get_by_id(int id) {
  299.  
  300.     node *temp = front;
  301.     node *retval;
  302.  
  303.     while (temp != NULL) {
  304.         if (id == temp->id) {
  305.             retval = temp;
  306.             return retval;
  307.         }
  308.         temp = temp->next_node;
  309.     }
  310.     return NULL;
  311. }
  312.  
  313. node* dll::get_by_pos(int pos) {
  314.  
  315.     node *temp = front;
  316.     node *retval;
  317.  
  318.     int i = 0;
  319.     while (temp != NULL) {
  320.         if (i == pos) {
  321.             retval = temp;
  322.             return retval;
  323.         }
  324.         i++;
  325.         temp = temp->next_node;
  326.     }
  327.     return NULL;
  328. }
  329.  
  330. void dll::display_forward() {
  331.     node *temp = front;
  332.  
  333.     printf("\n\nnodes in forward order:\n");
  334.  
  335.     while (temp != NULL) {
  336.         printf("%d   ", temp->id);
  337.         temp = temp->next_node;
  338.     }
  339.     printf("\n");
  340. }
  341.  
  342. void dll::display_backward() {
  343.     node *temp = back;
  344.  
  345.     printf("\n\nnodes in reverse order:\n");
  346.  
  347.     while (temp != NULL) {
  348.         printf("%d   ", temp->id);
  349.         temp = temp->prev_node;
  350.     }
  351.     printf("\n");
  352. }
  353.  
  354. void dll::display_objects() {
  355.     node *temp = front;
  356.  
  357.     cout << "\n\nobjects in forward order:" << endl;
  358.  
  359.     while (NULL != temp) {
  360.         printf("(id:%d", temp->id);
  361.         if ('c' == temp->type)
  362.             printf("C ");
  363.         if ('b' == temp->type)
  364.             printf("B ");
  365.         printf("listpos:%d)   ", temp->listpos);
  366.         temp = temp->next_node;
  367.     }
  368.     printf("\n");
  369. }
  370.  
  371. void dll::draw_objects() {
  372.     node *temp = front;
  373.  
  374.     while (NULL != temp) {
  375.         temp->draw();
  376.         temp = temp->next_node;
  377.     }
  378. }
  379.  
  380. void dll::destroy_list() {
  381.     node *T = back;
  382.  
  383.     while (T != NULL) {
  384.         node *T2 = T;
  385.         T = T->prev_node;
  386.         delete T2;
  387.     }
  388.  
  389.     front = NULL;
  390.     back = NULL;
  391. }
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399. glfw30
  400.  
  401. // glfw_30.cpp : Defines the entry point for the console application.
  402. //  http://www.glfw.org/docs/latest/quick.html
  403.  
  404. #include "stdafx.h"
  405.  
  406. #include <GLAux.h>
  407. #pragma comment(lib,"glaux.lib")
  408.  
  409.  
  410. using namespace std;
  411.  
  412. GLdouble A, B, C, D;
  413.  
  414. float t = 0;
  415.  
  416. int mouse_curr_x, mouse_curr_y;
  417.  
  418.  
  419. class cell;
  420. class map;
  421. class body;
  422. class node;
  423. class dll;
  424.  
  425.  
  426. map *m;
  427.  
  428.  
  429. int count_command(string command, char type) {
  430.  
  431.     int i, comm_len = command.length();
  432.     int retval = 0;
  433.  
  434.  
  435.     if ('e' == type) {
  436.         for (i = 0; i < comm_len; i++) {
  437.             if ('b' == command[i] || 'c' == command[i]) {
  438.                 retval++;
  439.             }
  440.         }
  441.         return retval;
  442.     }
  443.  
  444.     for (i = 0; i < comm_len; i++) {
  445.         if (type == command[i]) {
  446.             retval++;
  447.         }
  448.     }
  449.  
  450.     return retval;
  451. }
  452.  
  453. int count_further(string command, int curr_pos) {
  454.  
  455.     int i, j, num_of_same_chars;
  456.     int seq_end_pos;
  457.     char curr_sym = command[curr_pos];
  458.  
  459.     if (curr_pos < command.length()) {
  460.  
  461.         num_of_same_chars = 1;
  462.         seq_end_pos = curr_pos;
  463.  
  464.         for (i = curr_pos; i < command.length(); i++) {
  465.             if (curr_sym == command[i] && curr_sym == command[i + 1]) {
  466.                 num_of_same_chars++;
  467.                 seq_end_pos = i;
  468.             }
  469.             if (curr_sym == command[i] && curr_sym != command[i + 1]) {
  470.                 seq_end_pos = i + 1;
  471.                 break;
  472.             }
  473.         }
  474.  
  475.         return seq_end_pos;
  476.     }
  477.     else {
  478.         printf("\ncount_further incorrect input!\n");
  479.         return -1;
  480.     }
  481.    
  482. }
  483.  
  484. bool lookup_next_sym(string command, int curr_pos) {
  485.  
  486.     int i;
  487.     char curr_sym = command[curr_pos];
  488.     char opposite;
  489.     if ('b' == curr_sym) opposite = 'c';
  490.     if ('c' == curr_sym) opposite = 'b';
  491.  
  492.     for (i = curr_pos; i < command.length(); i++) {
  493.         if (command[i] == curr_sym) continue;
  494.         if (command[i] == opposite) break;
  495.     }
  496.     int last_pos_of_curr_sym = i - 1;
  497.  
  498.     return true;
  499.  
  500. }
  501.  
  502. char nearest_next_sym(string command, int curr_pos) {
  503.  
  504.     char curr_sym = command[curr_pos];
  505.     int seq_len = count_further(command, curr_pos);
  506.  
  507.     if (curr_pos + seq_len < command.length()) {
  508.         return command[curr_pos + seq_len];
  509.     }
  510.     else
  511.     {
  512.         return 'x';
  513.     }
  514. }
  515.  
  516.  
  517.  
  518.  
  519. GLuint texture[1];
  520.  
  521. void load_textures()
  522. {
  523.  
  524.     AUX_RGBImageRec *texture1;
  525.     texture1 = auxDIBImageLoadA("tex_snoop.bmp");
  526.     glGenTextures(1, &texture[0]);
  527.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  528.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  529.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  530.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  531.  
  532.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  533. }
  534.  
  535.  
  536.  
  537. static void cursor_callback(GLFWwindow* window, double x, double y)
  538. {
  539.     mouse_curr_x = (int)x;
  540.     mouse_curr_y = (int)y;
  541.  
  542.     int tmpx = (int)(x / (SCREEN_WIDTH / SPLIT_X));
  543.     int tmpy = (int)(y / (SCREEN_HEIGHT / SPLIT_Y));
  544.  
  545.     //printf("%d\t%d\t%c\n", (int)x, (int)y, m->get_cell_type(tmpx, tmpy));
  546.  
  547.     m->dispell_map();
  548.     m->highlight_cell(tmpx, tmpy);
  549. }
  550.  
  551. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  552. {
  553.     int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
  554.     int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
  555.  
  556.     if (button == GLFW_MOUSE_BUTTON_RIGHT)
  557.     {
  558.         if (action == GLFW_PRESS) {
  559.            
  560.  
  561.             printf("%d\t%d\t%c\n", (int)mouse_curr_x, (int)mouse_curr_y, m->get_cell_type(tmpx, tmpy));
  562.         }
  563.         if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  564.     }
  565.  
  566.     if (button == GLFW_MOUSE_BUTTON_LEFT)
  567.     {
  568.         if (action == GLFW_PRESS) {
  569.  
  570.             printf("M: %d\t%d\t%c(before click)\n", mouse_curr_x, mouse_curr_y, m->get_cell_type(tmpx, tmpy));
  571.  
  572.             if ('e' == m->get_cell_type(tmpx, tmpy)) {
  573.                 m->erase_cell(tmpx, tmpy);
  574.                 m->set_cell(1, tmpx, tmpy, 'b');
  575.                 return;
  576.             }
  577.             if ('b' == m->get_cell_type(tmpx, tmpy)) {
  578.                 m->erase_cell(tmpx, tmpy);
  579.                 m->set_cell(1, tmpx, tmpy, 'c');
  580.                 return;
  581.             }
  582.             if ('c' == m->get_cell_type(tmpx, tmpy)) {
  583.                 m->erase_cell(tmpx, tmpy);
  584.                 m->set_cell(1, tmpx, tmpy, 'e');
  585.                 return;
  586.             }
  587.            
  588.            
  589.         }
  590.        
  591.     }
  592. }
  593.  
  594. static void resize_callback(GLFWwindow* window, int width, int height)
  595. {
  596.     //      windowWidth = width;
  597.     //      windowHeight = height;
  598.  
  599.     glViewport(0, 0, width, height);
  600.     glMatrixMode(GL_PROJECTION);
  601.     glLoadIdentity();
  602.     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
  603.  
  604.     glMatrixMode(GL_MODELVIEW);
  605.     glLoadIdentity();
  606.  
  607.     A = width / 4.0;
  608.     B = 0.0;
  609.     C = D = height / 2.0;
  610.  
  611.     //printf("Reshape occured\n");
  612. }
  613.  
  614. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  615. {
  616.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  617.         glfwSetWindowShouldClose(window, GL_TRUE);
  618.     if (GLFW_KEY_S == key && action == GLFW_PRESS) {
  619.         m->validate_map();
  620.     }
  621. }
  622.  
  623. static void error_callback(int error, const char* description)
  624. {
  625.     fputs(description, stderr);
  626. }
  627.  
  628.  
  629.  
  630.  
  631. int main(int argc, _TCHAR* argv[])
  632. {
  633.     if (!glfwInit()) {
  634.         printf("glfwInit failed\n");
  635.         return -1;
  636.     }
  637.  
  638.     glfwSetErrorCallback(error_callback);
  639.  
  640.     GLFWwindow* window;
  641.  
  642.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  643.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  644.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  645.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
  646.     if (window == NULL) {
  647.         printf("glfwOpenWindow failed.\n");
  648.         glfwTerminate();
  649.         return -2;
  650.     }
  651.  
  652.     int i, j, k, attrib;
  653.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  654.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  655.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  656.  
  657.     glfwMakeContextCurrent(window);
  658.  
  659.     glfwSetKeyCallback(window, keyboard_callback);
  660.     glfwSetFramebufferSizeCallback(window, resize_callback);
  661.     glfwSetMouseButtonCallback(window, mouse_callback);
  662.     glfwSetCursorPosCallback(window, cursor_callback);
  663.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  664.  
  665.     srand(time(NULL));
  666.  
  667.     FILE *fp;
  668.     fp = fopen("scheme.txt", "r");
  669.     if (NULL == fp) {
  670.         printf("cannot open file\n");
  671.     }
  672.  
  673.  
  674.     dll *list = new dll();
  675.     //body c, b;
  676.  
  677.     int id, id2;
  678.     char c_type = 'c', b_type = 'b', e_type = 'e';
  679.     int b_pos_x, b_pos_y, c_pos_x, c_pos_y, c_n, b_n;
  680.  
  681.     c_n = b_n = 0;
  682.  
  683.     m = new map();
  684.     m->set_map(SPLIT_X, SPLIT_Y);
  685.  
  686.  
  687.     printf("insert scheme:\n");
  688.  
  689.     string command;
  690.  
  691.    
  692.  
  693.     int num = 0;
  694.     fscanf(fp, "%d", &num);
  695.     /*
  696.     for (i = 0; i < num; i++) {
  697.         //char body_val[10];
  698.         int body_val;
  699.         body body_entity;
  700.         fscanf(fp, "%d", &body_val);
  701.         body_entity.id = i;
  702.         list->append_back(body_val, body_entity);
  703.     }
  704.  
  705.     list->display_forward();
  706.  
  707.     int tidsean = list->get_by_value(1488)->obj.id;
  708.     printf("\nid by value_1488:%d\n", tidsean);
  709.     tidsean = list->get_by_pos(3)->obj.id;
  710.     printf("id by pos_3:%d\n", tidsean);
  711.     */
  712.     for (i = 0; i < num; i++) {
  713.         char el_type[6];
  714.         printf("type:");
  715.         fscanf(fp, "%s", el_type);
  716.  
  717.         //body body_entity, body_entity_conn;
  718.         int pos_x, pos_y, mass;
  719.         int body_name, conn_left_name, conn_right_name, conn_top_name, conn_bot_name;
  720.        
  721.         if (0 == strcmp(el_type, "block") || 0 == strcmp(el_type, "b")) {
  722.  
  723.             printf("name pos_x pos_y mass:");
  724.             fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
  725.             printf("connection: L R T B:");
  726.             fscanf(fp, "%d%d%d%d", &conn_left_name, &conn_right_name, &conn_top_name, &conn_bot_name);
  727.  
  728.             /*if (0 != strcmp(conn_left_name, "e")) {
  729.                 body_entity.left =
  730.             }*/
  731.  
  732.             //body_entity.set('b', body_name, (float)pos_x, (float)pos_y, mass);
  733.             list->append_back(body_name);
  734.             node *curr_node = list->get_by_id(body_name);
  735.             curr_node->set('b', body_name, i, pos_x, pos_y, mass);
  736.            
  737.             printf("block:%d[x:%d y:%d m:%d CL:%d CR:%d CT:%d CB:%d] has been added.\n\n", body_name, pos_x, pos_y, mass, conn_left_name, conn_right_name, conn_top_name, conn_bot_name);
  738.         }
  739.  
  740.         if (0 == strcmp(el_type, "cargo") || 0 == strcmp(el_type, "c")) {
  741.  
  742.             printf("name pos_x pos_y mass:");
  743.             fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
  744.             printf("connection: T B:");
  745.             fscanf(fp, "%d%d", &conn_top_name, &conn_bot_name);
  746.  
  747.             //body_entity.set('c', body_name, (float)pos_x, (float)pos_y, mass);
  748.             list->append_back(body_name);
  749.             node *curr_node = list->get_by_id(body_name);
  750.             curr_node->set('c', body_name, i, pos_x, pos_y, mass);
  751.  
  752.             printf("cargo:%d[x:%d y:%d m:%d CT:%d CB:%d] has beed added.\n\n", body_name, pos_x, pos_y, mass, conn_top_name, conn_bot_name);
  753.         }
  754.  
  755.     }
  756.  
  757.     fclose(fp);
  758.     printf("file has been closed\n");
  759.  
  760.     list->display_objects();
  761.  
  762.    
  763.     for (i = 1; i < num; i++) {
  764.  
  765.         node *na = list->get_by_pos(i - 1);
  766.         node *nb = list->get_by_pos(i);
  767.  
  768.         //list->connect_bodies(&a, &b);
  769.  
  770.     }
  771.  
  772.     int ps = 2;
  773.     node *nn = list->get_by_pos(ps);
  774.     int tidsean = nn->next_node->id;
  775.     printf("next to pos=%d is:%d\n", ps, tidsean);
  776.  
  777.  
  778.     c_pos_x = 0;
  779.     c_pos_y = 6;
  780.  
  781.     b_pos_x = 0;
  782.     b_pos_y = 3;
  783.    
  784.     char prev_sym = '0', curr_sym, next_sym;
  785.     int body_num, block_num, cargo_num;
  786.     int seq_start_pos, seq_end_pos, len_of_seq;
  787.    
  788.     int x;
  789.    
  790.  
  791.     //command = "b^bc^b^cc";
  792.     //command = "cbb(cc)bc";
  793.  
  794.     body_num = count_command(command, 'e');
  795.     cargo_num = count_command(command, c_type);
  796.     block_num = count_command(command, b_type);
  797.     printf("bodies:%d cargos:%d blocks:%d\n", body_num, cargo_num, block_num);
  798.    
  799.     for (i = 0; i < command.length(); i++) {
  800.  
  801.         if (i > 0)
  802.             prev_sym = command[i - 1];
  803.         curr_sym = command[i];
  804.         next_sym = command[i + 1];
  805.         seq_start_pos = i;
  806.         seq_end_pos = count_further(command, seq_start_pos);
  807.         len_of_seq = seq_end_pos - seq_start_pos;
  808.         char nearest_sym = command[seq_start_pos + len_of_seq];
  809.         printf("[%d_%d]%c:%d   .%c\n", seq_start_pos, seq_end_pos, curr_sym, len_of_seq, nearest_sym);
  810.        
  811.  
  812.         //i = seq_end_pos-1;
  813.        
  814.         if ('b' == curr_sym && len_of_seq > 1) {
  815.  
  816.             printf(" b.. ");
  817.  
  818.             if ('(' == nearest_sym || '^' == nearest_sym) {
  819.                 len_of_seq -= 1;
  820.             }
  821.            
  822.             for (j = 0; j < len_of_seq; j++) {
  823.                
  824.                 id = 100 + rand() % MAXRANDID;
  825.  
  826.                 if (0 == j % 2) {
  827.                     m->set_cell(id, b_pos_x++, b_pos_y, b_type);
  828.                 }
  829.                 else {
  830.                     m->set_cell(id, b_pos_x++, b_pos_y + 1, b_type);
  831.                 }
  832.                
  833.             }
  834.  
  835.             i = i + j - 1;
  836.  
  837.             continue;
  838.            
  839.         }
  840.         if ('b' == curr_sym && '(' == next_sym) {
  841.            
  842.             m->set_cell(1, b_pos_x, b_pos_y, b_type);
  843.  
  844.             int num_of_cargos = 2;
  845.             //int num_of_cargos = count_further(command, i + 2);
  846.             printf(" b(str[%d]=%c(n:%d)) ", i + 2, command[i + 2], num_of_cargos);
  847.  
  848.  
  849.             for (j = 0; j < num_of_cargos; j++) {
  850.  
  851.                 id = 100 + rand() % MAXRANDID;
  852.                 m->set_cell(id, b_pos_x, c_pos_y++, c_type);
  853.  
  854.             }
  855.  
  856.             b_pos_x++;
  857.             c_pos_x++;
  858.  
  859.             i = i + num_of_cargos;
  860.         }
  861.         /*
  862.         if ('b' == curr_sym && '^' == next_sym) {
  863.             id = 100 + rand() % MAXRANDID;
  864.             m->set_cell(id, b_pos_x++, b_pos_y, b_type);
  865.  
  866.             int tmp_seq_start = i;
  867.             int tmp_seq_end = count_further(command, tmp_seq_start, next_sym);
  868.            
  869.             int tmp_b_pos_y = b_pos_y;
  870.             for (j = 0; j < tmp_seq_end; j++) {
  871.                 tmp_b_pos_y++;
  872.             }
  873.  
  874.             id = 100 + rand() % MAXRANDID;
  875.             m->set_cell(id, b_pos_x++, tmp_b_pos_y, b_type);
  876.             i = tmp_seq_end;
  877.  
  878.             continue;
  879.             }*/
  880.         if ('c' == curr_sym && len_of_seq > 1 && len_of_seq <= 3) {
  881.             for (j = 0; j < len_of_seq; j++) {
  882.  
  883.                 id = 100 + rand() % MAXRANDID;
  884.                 m->set_cell(id, c_pos_x, c_pos_y + j, c_type);
  885.             }
  886.  
  887.             i = i + len_of_seq - 1;
  888.  
  889.             continue;
  890.         }
  891.     }
  892.  
  893.    
  894.     printf("\n");
  895.     /*
  896.     for (i = 0; i < command.length(); i++) {
  897.         // 1 = cargo, 2 = block
  898.  
  899.         id = 100 + rand() % MAXRANDID;
  900.         id2 = 100 + rand() % MAXRANDID;
  901.  
  902.         if ('c' == command[i]) {
  903.  
  904.             m->set_cell(id, c_pos_x++, c_pos_y, c_type); //======
  905.  
  906.             c.set(id, c_n, 1, 10, 50 + 70 * i, 150);
  907.             list->append_back(c_n, c);
  908.             c_n++;
  909.  
  910.             printf(" c ");
  911.             continue;
  912.            
  913.         }
  914.         if ('b' == command[i] && 'b' == command[i+1]) {
  915.  
  916.             m->set_cell(id, b_pos_x++, b_pos_y, b_type); //======
  917.  
  918.             b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
  919.             list->append_back(b_n, b);
  920.             b_n++;
  921.  
  922.            
  923.             m->set_cell(id2, b_pos_x++, b_pos_y - 1, b_type);
  924.  
  925.             b.set(id2, b_n, 2, 0, 50 + 70 * (i + 1), 200);
  926.             list->append_back(b_n, b);
  927.             b_n++;
  928.  
  929.             i += 1;
  930.  
  931.             printf(" bb ");
  932.             continue;
  933.            
  934.         }
  935.         if ('b' == command[i] && '(' == command[i+1] && 'c' == command[i+2]) {
  936.  
  937.             m->set_cell(id, b_pos_x, b_pos_y, b_type); //======
  938.  
  939.             b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
  940.             list->append_back(b_n, b);
  941.             b_n++;
  942.  
  943.             m->set_cell(id2, b_pos_x, c_pos_y - 2, c_type);
  944.             b_pos_x++;
  945.  
  946.             b.set(id2, c_n, 1, 10, 50 + 70 * i, 150);
  947.             list->append_back(c_n, b);
  948.             c_n++;
  949.  
  950.             i += 2;
  951.            
  952.             printf(" b(c ");
  953.             continue;
  954.         }
  955.        
  956.         if ('b' == command[i]) {
  957.  
  958.             m->set_cell(id, b_pos_x++, b_pos_y, b_type); //======
  959.  
  960.             b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
  961.             list->append_back(b_n, b);
  962.             b_n++;
  963.  
  964.             printf(" b ");
  965.  
  966.             continue;
  967.         }
  968.        
  969.     }
  970.  
  971.     list->display_forward();
  972.     list->display_objects();
  973.     */
  974.  
  975.  
  976.     //load_textures();
  977.     //glEnable(GL_TEXTURE_2D);
  978.  
  979.     while (!glfwWindowShouldClose(window))
  980.     {
  981.         glClearColor(0.5, 0.5, 0.5, 1);
  982.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  983.  
  984.         if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
  985.             m->print_map();
  986.         }
  987.         if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  988.             i -= 5;
  989.         }
  990.  
  991.         //m->draw_map();
  992.  
  993.  
  994.         list->draw_objects();
  995.         //list->enable_fall(t);
  996.         t += 0.0001;
  997.  
  998.         glfwSwapBuffers(window);
  999.         glfwPollEvents();
  1000.         //glfwWaitEvents();
  1001.     }
  1002.  
  1003.     glfwDestroyWindow(window);
  1004.     glfwTerminate();
  1005.  
  1006.     return 0;
  1007. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement