Advertisement
Ladies_Man

ccg THE_GREAT_MERGE

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