Advertisement
Ladies_Man

ccg scheme c-b-c works

Nov 30th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.06 KB | None | 0 0
  1. 3
  2. c
  3. 1337 150 350 10
  4. 322
  5. b
  6. 322 200 800 0
  7. 1337 0 322
  8. c
  9. 1488 250 500 100
  10. 322
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. stdafx
  19. // stdafx.h : include file for standard system include files,
  20. // or project specific include files that are used frequently, but
  21. // are changed infrequently
  22. //
  23. #pragma once
  24.  
  25. #include "targetver.h"
  26.  
  27. #define _CRT_SECURE_NO_WARNINGS
  28.  
  29. //if print is lagging switch this
  30. //#include <stdio.h>       
  31. #include <iostream>
  32. #include <vector>
  33. #include <tchar.h>
  34. #include <stdlib.h>
  35.  
  36. #define _USE_MATH_DEFINES
  37. #include <cmath>
  38. #include <math.h>
  39.  
  40. #include <ctime>
  41. #include <time.h>
  42.  
  43. #include <Windows.h>
  44.  
  45. #include <string>
  46.  
  47. #define GLEW_STATIC
  48. #include <GL\glew.h>
  49. #include <GLFW\glfw3.h>
  50.  
  51. typedef struct { float x, y; } coord;
  52.  
  53.  
  54. #include "body.h"
  55.  
  56.  
  57. #include "cell.h"
  58. #include "map.h"
  59.  
  60. #include "node.h"
  61. #include "dll.h"
  62.  
  63. #define SCREEN_WIDTH 1000
  64. #define SCREEN_HEIGHT 1000
  65. #define ROOF_HEIGHT 950
  66. #define FLOOR_HEIGHT 50
  67. #define SPLIT_X 10
  68. #define SPLIT_Y 10
  69. #define G_CONST 9.781665
  70. #define MAXRANDID 99999
  71.  
  72. #define REVD(COORD_Y) ((SPLIT_Y - 1) - (COORD_Y))
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. node.h
  82. #pragma once
  83. class node
  84. {
  85. public:
  86.     int id;
  87.     char type;
  88.     int listpos;
  89.     int mass;
  90.     float velo, accel;
  91.     bool halt;
  92.  
  93.     node *next_node;
  94.     node *prev_node;
  95.  
  96.     node *cp_left_node, *cp_center_node, *cp_right_node;
  97.     int cp_left_id, cp_center_id, cp_right_id;
  98.  
  99.     coord init;
  100.     coord curr;
  101.  
  102.     void set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass);
  103.     void set_cp_ids(int left, int center, int right);
  104.     void set_cp_ids(int center);
  105.     void draw_body();
  106.     void draw_ropes();
  107.  
  108.     void fall(float t);
  109.  
  110.     node *get_next_by_type(char type);
  111.     node *get_prev_by_type(char type);
  112.     coord get_cp_left_pos();
  113.     coord get_cp_center_pos();
  114.     coord get_cp_right_pos();
  115.  
  116.     node(int id);
  117.     ~node();
  118.  
  119. private:
  120.    
  121.     int size_x, size_y, size_border, size_rope;
  122.     coord cp_left_pos, cp_center_pos, cp_right_pos;     //connection points
  123.  
  124. };
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. node.cpp
  134. #include "stdafx.h"
  135.  
  136. using namespace std;
  137.  
  138. node::node(int x)
  139. {
  140.     id = x;
  141.     next_node = NULL;
  142.     prev_node = NULL;
  143.     halt = false;
  144. }
  145.  
  146. node::~node()
  147. {
  148. }
  149.  
  150. void node::set_list_param(char type, int id, int listpos, float pos_x, float pos_y, int mass) {
  151.     this->init.x = pos_x;
  152.     this->init.y = pos_y;
  153.     this->curr.x = pos_x;
  154.     this->curr.y = pos_y;
  155.  
  156.     this->type = type;
  157.  
  158.     this->id = id;
  159.  
  160.     this->listpos = listpos;
  161.  
  162.     this->mass = mass;
  163.  
  164.     this->size_x = 100;
  165.     this->size_y = 100;
  166.     this->size_border = 5;
  167.     this->size_rope = 3;
  168.  
  169.     this->velo = 0;
  170.     this->accel = (-1)*G_CONST;
  171.  
  172.     this->halt = false;
  173. }
  174.  
  175. void node::set_cp_ids(int left, int center, int right) {
  176.     this->cp_left_id = left;
  177.     this->cp_center_id = center;
  178.     this->cp_right_id = right;
  179. }
  180.  
  181. void node::set_cp_ids(int center) {
  182.     this->cp_center_id = center;
  183. }
  184.  
  185. node* node::get_next_by_type(char type) {
  186.  
  187.     node *tmp = this;
  188.     tmp = tmp->next_node;
  189.  
  190.     while (tmp != NULL) {
  191.         if (tmp->type == type) {
  192.             return tmp;
  193.         }
  194.         tmp = tmp->next_node;
  195.     }
  196.     return NULL;
  197. }
  198.  
  199. node* node::get_prev_by_type(char type) {
  200.  
  201.     node *tmp = this;
  202.     tmp = tmp->prev_node;
  203.  
  204.     while (tmp != NULL) {
  205.         if (tmp->type == type) {
  206.             return tmp;
  207.         }
  208.         tmp = tmp->prev_node;
  209.     }
  210.     return NULL;
  211. }
  212.  
  213. coord node::get_cp_left_pos() {
  214.     coord retval;
  215.     retval.x = this->curr.x - this->size_x / 2;
  216.     retval.y = this->curr.y;
  217.  
  218.     this->cp_left_pos.x = retval.x;
  219.     this->cp_left_pos.y = retval.y;
  220.  
  221.     return retval;
  222. }
  223.  
  224. coord node::get_cp_center_pos() {
  225.    
  226.     if ('b' == this->type) {
  227.         cp_center_pos = curr;
  228.         return this->curr;
  229.     }
  230.  
  231.     coord retval;
  232.  
  233.     retval.x = this->curr.x;
  234.     retval.y = this->curr.y + this->size_y / 2;
  235.  
  236.     this->cp_center_pos.x = retval.x;
  237.     this->cp_center_pos.y = retval.y;
  238.  
  239.     return retval;
  240. }
  241.  
  242. coord node::get_cp_right_pos() {
  243.     coord retval;
  244.     retval.x = this->curr.x + this->size_x / 2;
  245.     retval.y = this->curr.y;
  246.  
  247.     this->cp_right_pos.x = retval.x;
  248.     this->cp_right_pos.y = retval.y;
  249.  
  250.     return retval;
  251. }
  252.  
  253. void draw_line(float from_x, float from_y, float to_x, float to_y) {
  254.     glBegin(GL_LINES);
  255.         glVertex2f(from_x, from_y);
  256.         glVertex2f(to_x, to_y);
  257.     glEnd();
  258. }
  259.  
  260. void draw_line(coord from, coord to) {
  261.     glBegin(GL_LINES);
  262.         glVertex2f(from.x, from.y);
  263.         glVertex2f(to.x, to.y);
  264.     glEnd();
  265. }
  266.  
  267.  
  268. void node::draw_body() {
  269.     float center_x = this->curr.x;
  270.     float center_y = this->curr.y;
  271.  
  272.     int size_x = this->size_x;
  273.     int size_y = this->size_y;
  274.     int rad = size_x / 2;
  275.  
  276.     int border = this->size_border;
  277.  
  278.     if ('c' == this->type) {
  279.  
  280.         coord lb, lt, rt, rb;
  281.  
  282.         lb.x = center_x - size_x / 2;
  283.         lb.y = center_y - size_y / 2;
  284.  
  285.         lt.x = center_x - size_x / 2;
  286.         lt.y = center_y + size_y / 2;
  287.  
  288.         rt.x = center_x + size_x / 2;
  289.         rt.y = center_y + size_y / 2;
  290.  
  291.         rb.x = center_x + size_x / 2;
  292.         rb.y = center_y - size_y / 2;
  293.  
  294.         //outer black
  295.         glBegin(GL_POLYGON);
  296.         glColor3f(0, 0, 0);
  297.  
  298.         glVertex2f(lb.x, lb.y);
  299.         glVertex2f(lt.x, lt.y);
  300.         glVertex2f(rt.x, rt.y);
  301.         glVertex2f(rb.x, rb.y);
  302.         glEnd();
  303.  
  304.         //inner white
  305.         glBegin(GL_POLYGON);
  306.         glColor3f(1, 1, 1);
  307.  
  308.         glVertex2f(lb.x + border, lb.y + border);
  309.         glVertex2f(lt.x + border, lt.y - border);
  310.         glVertex2f(rt.x - border, rt.y - border);
  311.         glVertex2f(rb.x - border, rb.y + border);
  312.         glEnd();
  313.     }
  314.  
  315.     if ('b' == this->type) {
  316.  
  317.         GLfloat phi = 0, theta = 0;
  318.  
  319.         //outer black
  320.         glBegin(GL_POLYGON);
  321.         glColor3f(0, 0, 0);
  322.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  323.             glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
  324.         }
  325.         glEnd();
  326.  
  327.         //inner white
  328.         glBegin(GL_POLYGON);
  329.         glColor3f(1, 1, 1);
  330.         for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  331.             glVertex2f(center_x + (rad - border)*cos(phi), center_y + (rad - border)*sin(phi));
  332.         }
  333.         glEnd();
  334.  
  335.     }
  336. }
  337.  
  338. void node::draw_ropes() {
  339.  
  340.     node *next = this->next_node;
  341.    
  342.     if (NULL == next) {
  343.         return;
  344.     }
  345.  
  346.     glLineWidth((GLfloat)this->size_rope);
  347.     glColor3f(0, 0, 0);
  348.  
  349.     //block's central connection point
  350.     if ('b' == this->type) {
  351.         if (0 == this->cp_center_id) {
  352.             coord this_block_cp_center = this->get_cp_center_pos();
  353.             draw_line(this_block_cp_center.x, this_block_cp_center.y, this_block_cp_center.x, ROOF_HEIGHT);
  354.         }
  355.         else {
  356.             coord next_cargo_cp = this->next_node->get_cp_center_pos();
  357.             draw_line(this->get_cp_center_pos(), next_cargo_cp);
  358.         }
  359.     }
  360.  
  361.  
  362.  
  363.  
  364.     if ('c' == this->type && 'b' == next->type) {
  365.         coord this_cargo_cp = this->get_cp_center_pos();
  366.         coord next_block_cp_left = next->get_cp_left_pos();
  367.  
  368.         draw_line(this_cargo_cp, next_block_cp_left);
  369.     }
  370.  
  371.     if ('b' == this->type && 'b' == next->type) {
  372.         coord this_block_cp_right = this->get_cp_right_pos();
  373.         coord next_block_cp_left = next->get_cp_left_pos();
  374.  
  375.         draw_line(this_block_cp_right, next_block_cp_left);
  376.     }
  377.  
  378.     if ('b' == this->type && 'c' == next->type) {
  379.         coord this_block_cp_right = this->get_cp_right_pos();
  380.         coord next_cargo_cp = next->get_cp_center_pos();
  381.  
  382.         draw_line(this_block_cp_right, next_cargo_cp);
  383.     }
  384.  
  385.    
  386. }
  387.  
  388. void node::fall(float t) {
  389.  
  390.     if (0 != this->mass) {
  391.         float a, v, y0;
  392.         float top_limit = (float)ROOF_HEIGHT;
  393.         node *next_cargo = get_next_by_type(this->type);
  394.         node *prev_cargo = get_prev_by_type(this->type);
  395.  
  396.         if ('c' == this->type) {
  397.             if (NULL != this->next_node) {
  398.                 top_limit = this->next_node->curr.y - this->next_node->size_y / 2;
  399.             }
  400.             if (NULL != this->prev_node) {
  401.                 top_limit = this->prev_node->curr.y - this->prev_node->size_y / 2;
  402.             }
  403.         }
  404.  
  405.         if (NULL == prev_cargo && NULL != next_cargo) {
  406.             a = G_CONST *(next_cargo->mass - this->mass) / (next_cargo->mass + this->mass);
  407.             v = this->velo;
  408.             y0 = this->curr.y;
  409.  
  410.             next_cargo->accel = -a;
  411.  
  412.             if (FLOOR_HEIGHT <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
  413.                 this->curr.x = this->curr.x;
  414.  
  415.                 this->curr.y = y0 + v*t + (a*t*t) / 2;
  416.             }
  417.             else {
  418.                 this->halt = true;
  419.             }
  420.             return;
  421.         }
  422.         /*if (NULL != prev_cargo && NULL == next_cargo) {
  423.             if (FLOOR_HEIGHT <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
  424.  
  425.                 this->curr.x = this->curr.x;
  426.  
  427.                 this->curr.y = this->curr.y + this->velo*t + (this->accel*t*t) / 2;
  428.  
  429.             }
  430.             else {
  431.                 this->halt = true;
  432.             }
  433.             return;
  434.         }*/
  435.  
  436.         if (FLOOR_HEIGHT <= this->curr.y - this->size_y / 2 && top_limit >= this->curr.y + this->size_y / 2) {
  437.  
  438.             this->curr.x = this->curr.x;
  439.  
  440.             this->curr.y = this->curr.y + this->velo*t + (this->accel*t*t) / 2;
  441.  
  442.         }
  443.         else {
  444.             this->halt = true;
  445.         }
  446.        
  447.  
  448.     }
  449.  
  450. }
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458. dll.h
  459. class dll {
  460. public:
  461.     bool phys_enabled;
  462.  
  463.     void append_front(int id);
  464.     void append_back(int id);
  465.     void destroy_list();
  466.  
  467.     int get_length();
  468.     node* get_front();
  469.     node* get_back();
  470.     node* get_by_id(int id);
  471.     node* get_by_pos(int pos);
  472.  
  473.     void display_forward();
  474.     void display_backward();
  475.     void display_objects();
  476.  
  477.     void draw_objects();
  478.     void draw_ropes();
  479.     void set_connection_points();
  480.  
  481.     void enable_fall(float time);
  482.     void check_movement();
  483.  
  484.     dll() { front = NULL; back = NULL; length = 0; phys_enabled = false; }
  485.     ~dll() { destroy_list(); }
  486.  
  487.  
  488. private:
  489.     int length;
  490.  
  491.     node *front;
  492.     node *back;
  493.  
  494. };
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502. dll.cpp
  503. #include "stdafx.h"
  504.  
  505. using namespace std;
  506.  
  507. void dll::append_front(int x) {
  508.  
  509.     node *n = new node(x);
  510.  
  511.     if (front == NULL) {
  512.         front = n;
  513.         back = n;
  514.     }
  515.     else {
  516.         front->prev_node = n;
  517.         n->next_node = front;
  518.         front = n;
  519.     }
  520.  
  521.     this->length++;
  522.  
  523.     printf("fornt appended\n");
  524. }
  525.  
  526. void dll::append_back(int x) {
  527.  
  528.     node *n = new node(x);
  529.  
  530.     if (back == NULL) {
  531.         front = n;
  532.         back = n;
  533.     }
  534.     else {
  535.         back->next_node = n;
  536.         n->prev_node = back;
  537.         back = n;
  538.     }
  539.  
  540.     this->length++;
  541.  
  542.     printf("back appended\n");
  543. }
  544.  
  545. int dll::get_length() {
  546.     return this->length;
  547. }
  548.  
  549. node* dll::get_front() {
  550.     return this->front;
  551. }
  552.  
  553. node* dll::get_back() {
  554.     return this->back;
  555. }
  556.  
  557. node* dll::get_by_id(int id) {
  558.  
  559.     node *tmp = front;
  560.     node *retval;
  561.  
  562.     while (tmp != NULL) {
  563.         if (id == tmp->id) {
  564.             retval = tmp;
  565.             return retval;
  566.         }
  567.         tmp = tmp->next_node;
  568.     }
  569.     return NULL;
  570. }
  571.  
  572. node* dll::get_by_pos(int pos) {
  573.  
  574.     node *tmp = front;
  575.     node *retval;
  576.  
  577.     int i = 0;
  578.     while (tmp != NULL) {
  579.         if (i == pos) {
  580.             retval = tmp;
  581.             return retval;
  582.         }
  583.         i++;
  584.         tmp = tmp->next_node;
  585.     }
  586.     return NULL;
  587. }
  588.  
  589. void dll::display_forward() {
  590.     node *tmp = front;
  591.  
  592.     printf("\n\nnodes(%d) in forward order:\n", this->length);
  593.  
  594.     while (tmp != NULL) {
  595.         printf("%d   ", tmp->id);
  596.         tmp = tmp->next_node;
  597.     }
  598.     printf("\n");
  599. }
  600.  
  601. void dll::display_backward() {
  602.     node *tmp = back;
  603.  
  604.     printf("\n\nnodes(%d) in reverse order:\n", this->length);
  605.  
  606.     while (tmp != NULL) {
  607.         printf("%d   ", tmp->id);
  608.         tmp = tmp->prev_node;
  609.     }
  610.     printf("\n");
  611. }
  612.  
  613. void dll::display_objects() {
  614.     node *tmp = front;
  615.  
  616.     printf("\n\nobjects(%d) in forward order:\n", this->length);
  617.  
  618.     while (NULL != tmp) {
  619.         printf("(id:%d", tmp->id);
  620.         if ('c' == tmp->type)
  621.             printf("C, ");
  622.         if ('b' == tmp->type)
  623.             printf("B, ");
  624.         printf("listpos:%d, mass:%d)   ", tmp->listpos, tmp->mass);
  625.         tmp = tmp->next_node;
  626.     }
  627.     printf("\n");
  628. }
  629.  
  630. void dll::draw_objects() {
  631.     node *tmp = front;
  632.  
  633.     while (NULL != tmp) {
  634.         tmp->draw_body();
  635.         tmp = tmp->next_node;
  636.     }
  637. }
  638.  
  639. void dll::draw_ropes() {
  640.  
  641.     node *tmp = front;
  642.     int i;
  643.  
  644.     for (i = 0; i < this->length; i++) {
  645.         tmp->draw_ropes();
  646.         tmp = tmp->next_node;
  647.  
  648.     }
  649.  
  650.  
  651. }
  652.  
  653. void dll::destroy_list() {
  654.     node *T = back;
  655.  
  656.     while (T != NULL) {
  657.         node *T2 = T;
  658.         T = T->prev_node;
  659.         delete T2;
  660.     }
  661.  
  662.     front = NULL;
  663.     back = NULL;
  664. }
  665.  
  666. void dll::enable_fall(float t) {
  667.    
  668.     if (this->phys_enabled) {
  669.         node *tmp = front;
  670.  
  671.         while (NULL != tmp) {
  672.             tmp->fall(t);
  673.             tmp = tmp->next_node;
  674.         }
  675.     }
  676. }
  677.  
  678. void dll::check_movement() {
  679.  
  680.     node *tmp = front;
  681.  
  682.     while (NULL != tmp) {
  683.         if (tmp->halt)
  684.             this->phys_enabled = false;
  685.         tmp = tmp->next_node;
  686.     }
  687.  
  688. }
  689.  
  690. void dll::set_connection_points() {
  691.    
  692.     node *tmp = front;
  693.  
  694.     while (NULL != tmp) {
  695.         if ('c' == tmp->type) {
  696.             int cp_center_id = tmp->cp_center_id;
  697.             tmp->cp_center_node = this->get_by_id(cp_center_id);
  698.         }
  699.         if ('b' == tmp->type) {
  700.             tmp->cp_left_node = this->get_by_id(tmp->cp_left_id);
  701.             tmp->cp_center_node = this->get_by_id(tmp->cp_center_id);
  702.             tmp->cp_right_node = this->get_by_id(tmp->cp_right_id);
  703.         }
  704.         tmp = tmp->next_node;
  705.     }
  706. }
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713. glfw30
  714.  
  715. // glfw_30.cpp : Defines the entry point for the console application.
  716. //  http://www.glfw.org/docs/latest/quick.html
  717.  
  718. #include "stdafx.h"
  719.  
  720. #include <GLAux.h>
  721. #pragma comment(lib,"glaux.lib")
  722.  
  723.  
  724. using namespace std;
  725.  
  726. GLdouble A, B, C, D;
  727.  
  728. float t = 0;
  729.  
  730. int mouse_curr_x, mouse_curr_y;
  731.  
  732.  
  733. class cell;
  734. class map;
  735.  
  736. class node;
  737. class dll;
  738.  
  739.  
  740. map *m;
  741. dll *list;
  742.  
  743. int count_command(string command, char type) {
  744.  
  745.     int i, comm_len = command.length();
  746.     int retval = 0;
  747.  
  748.  
  749.     if ('e' == type) {
  750.         for (i = 0; i < comm_len; i++) {
  751.             if ('b' == command[i] || 'c' == command[i]) {
  752.                 retval++;
  753.             }
  754.         }
  755.         return retval;
  756.     }
  757.  
  758.     for (i = 0; i < comm_len; i++) {
  759.         if (type == command[i]) {
  760.             retval++;
  761.         }
  762.     }
  763.  
  764.     return retval;
  765. }
  766.  
  767. int count_further(string command, int curr_pos) {
  768.  
  769.     int i, j, num_of_same_chars;
  770.     int seq_end_pos;
  771.     char curr_sym = command[curr_pos];
  772.  
  773.     if (curr_pos < command.length()) {
  774.  
  775.         num_of_same_chars = 1;
  776.         seq_end_pos = curr_pos;
  777.  
  778.         for (i = curr_pos; i < command.length(); i++) {
  779.             if (curr_sym == command[i] && curr_sym == command[i + 1]) {
  780.                 num_of_same_chars++;
  781.                 seq_end_pos = i;
  782.             }
  783.             if (curr_sym == command[i] && curr_sym != command[i + 1]) {
  784.                 seq_end_pos = i + 1;
  785.                 break;
  786.             }
  787.         }
  788.  
  789.         return seq_end_pos;
  790.     }
  791.     else {
  792.         printf("\ncount_further incorrect input!\n");
  793.         return -1;
  794.     }
  795.    
  796. }
  797.  
  798. bool lookup_next_sym(string command, int curr_pos) {
  799.  
  800.     int i;
  801.     char curr_sym = command[curr_pos];
  802.     char opposite;
  803.     if ('b' == curr_sym) opposite = 'c';
  804.     if ('c' == curr_sym) opposite = 'b';
  805.  
  806.     for (i = curr_pos; i < command.length(); i++) {
  807.         if (command[i] == curr_sym) continue;
  808.         if (command[i] == opposite) break;
  809.     }
  810.     int last_pos_of_curr_sym = i - 1;
  811.  
  812.     return true;
  813.  
  814. }
  815.  
  816. char nearest_next_sym(string command, int curr_pos) {
  817.  
  818.     char curr_sym = command[curr_pos];
  819.     int seq_len = count_further(command, curr_pos);
  820.  
  821.     if (curr_pos + seq_len < command.length()) {
  822.         return command[curr_pos + seq_len];
  823.     }
  824.     else
  825.     {
  826.         return 'x';
  827.     }
  828. }
  829.  
  830.  
  831.  
  832.  
  833. GLuint texture[1];
  834.  
  835. void load_textures()
  836. {
  837.  
  838.     AUX_RGBImageRec *texture1;
  839.     texture1 = auxDIBImageLoadA("tex_snoop.bmp");
  840.     glGenTextures(1, &texture[0]);
  841.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  842.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  843.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  844.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
  845.  
  846.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  847. }
  848.  
  849. void draw_constraints(int h) {
  850.    
  851.     glLineWidth(2.0);
  852.     glColor3f(0, 0, 0);
  853.     int i, n = 15;
  854.     float di = SCREEN_WIDTH / n;
  855.    
  856.     if (h > SCREEN_HEIGHT / 2) {
  857.         glBegin(GL_POLYGON);
  858.             glColor3f(1, 1, 1);
  859.             glVertex2f(0, h);
  860.             glVertex2f(0, SCREEN_HEIGHT);
  861.             glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
  862.             glVertex2f(SCREEN_WIDTH, h);
  863.         glEnd();
  864.         for (i = 0; i < SCREEN_WIDTH; i += (int)di) {
  865.             glBegin(GL_LINES);
  866.                 glColor3f(0,0,0);
  867.                 glVertex2f(i, h);
  868.                 glVertex2f(i + di, h + di);
  869.             glEnd();
  870.         }
  871.     }
  872.     else {
  873.         glBegin(GL_POLYGON);
  874.             glColor3f(1, 1, 1);
  875.             glVertex2f(0, -h);
  876.             glVertex2f(0, h);
  877.             glVertex2f(SCREEN_WIDTH, h);
  878.             glVertex2f(SCREEN_WIDTH, -h);
  879.         glEnd();
  880.         for (i = 0; i < SCREEN_WIDTH; i += (int)di) {
  881.             glBegin(GL_LINES);
  882.                 glColor3f(0,0,0);
  883.                 glVertex2f(i, h);
  884.                 glVertex2f(i - di, h - di);
  885.             glEnd();
  886.         }
  887.     }
  888.  
  889.     glBegin(GL_LINES);
  890.         glColor3f(0,0,0);
  891.         glVertex2f(0, h);
  892.         glVertex2f(SCREEN_WIDTH, h);
  893.     glEnd();
  894. }
  895.  
  896. static void cursor_callback(GLFWwindow* window, double x, double y)
  897. {
  898.     mouse_curr_x = (int)x;
  899.     mouse_curr_y = (int)y;
  900.  
  901.     int tmpx = (int)(x / (SCREEN_WIDTH / SPLIT_X));
  902.     int tmpy = (int)(y / (SCREEN_HEIGHT / SPLIT_Y));
  903.  
  904.     //printf("%d\t%d\t%c\n", (int)x, (int)y, m->get_cell_type(tmpx, tmpy));
  905.  
  906.     m->dispell_map();
  907.     //m->highlight_cell(tmpx, tmpy);
  908. }
  909.  
  910. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  911. {
  912.     int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
  913.     int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
  914.  
  915.     if (button == GLFW_MOUSE_BUTTON_RIGHT)
  916.     {
  917.         if (action == GLFW_PRESS) {
  918.            
  919.  
  920.             printf("%d\t%d\t%c\n", (int)mouse_curr_x, (int)mouse_curr_y, m->get_cell_type(tmpx, tmpy));
  921.         }
  922.         if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  923.     }
  924.  
  925.     if (button == GLFW_MOUSE_BUTTON_LEFT)
  926.     {
  927.         if (action == GLFW_PRESS) {
  928.  
  929.             printf("M: %d\t%d\t%c(before click)\n", mouse_curr_x, mouse_curr_y, m->get_cell_type(tmpx, tmpy));
  930.  
  931.             if ('e' == m->get_cell_type(tmpx, tmpy)) {
  932.                 m->erase_cell(tmpx, tmpy);
  933.                 m->set_cell(1, tmpx, tmpy, 'b');
  934.                 return;
  935.             }
  936.             if ('b' == m->get_cell_type(tmpx, tmpy)) {
  937.                 m->erase_cell(tmpx, tmpy);
  938.                 m->set_cell(1, tmpx, tmpy, 'c');
  939.                 return;
  940.             }
  941.             if ('c' == m->get_cell_type(tmpx, tmpy)) {
  942.                 m->erase_cell(tmpx, tmpy);
  943.                 m->set_cell(1, tmpx, tmpy, 'e');
  944.                 return;
  945.             }
  946.            
  947.            
  948.         }
  949.        
  950.     }
  951. }
  952.  
  953. static void resize_callback(GLFWwindow* window, int width, int height)
  954. {
  955.     //      windowWidth = width;
  956.     //      windowHeight = height;
  957.  
  958.     glViewport(0, 0, width, height);
  959.     glMatrixMode(GL_PROJECTION);
  960.     glLoadIdentity();
  961.     glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
  962.  
  963.     glMatrixMode(GL_MODELVIEW);
  964.     glLoadIdentity();
  965.  
  966.     A = width / 4.0;
  967.     B = 0.0;
  968.     C = D = height / 2.0;
  969.  
  970.     //printf("Reshape occured\n");
  971. }
  972.  
  973. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  974. {
  975.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  976.         glfwSetWindowShouldClose(window, GL_TRUE);
  977.     if (GLFW_KEY_S == key && action == GLFW_PRESS) {
  978.         m->validate_map();
  979.     }
  980.     if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
  981.         if (!list->phys_enabled) {
  982.             list->phys_enabled = true;
  983.         }
  984.         else {
  985.             list->phys_enabled = false;
  986.         }
  987.     }
  988. }
  989.  
  990. static void error_callback(int error, const char* description)
  991. {
  992.     fputs(description, stderr);
  993. }
  994.  
  995.  
  996.  
  997.  
  998. int main(int argc, _TCHAR* argv[])
  999. {
  1000.     if (!glfwInit()) {
  1001.         printf("glfwInit failed\n");
  1002.         return -1;
  1003.     }
  1004.  
  1005.     glfwSetErrorCallback(error_callback);
  1006.  
  1007.     GLFWwindow* window;
  1008.  
  1009.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  1010.     //      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  1011.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  1012.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
  1013.     if (window == NULL) {
  1014.         printf("glfwOpenWindow failed.\n");
  1015.         glfwTerminate();
  1016.         return -2;
  1017.     }
  1018.  
  1019.     int i, attrib;
  1020.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  1021.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  1022.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  1023.  
  1024.     glfwMakeContextCurrent(window);
  1025.  
  1026.     glfwSetKeyCallback(window, keyboard_callback);
  1027.     glfwSetFramebufferSizeCallback(window, resize_callback);
  1028.     glfwSetMouseButtonCallback(window, mouse_callback);
  1029.     glfwSetCursorPosCallback(window, cursor_callback);
  1030.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  1031.  
  1032.     srand(time(NULL));
  1033.  
  1034.     FILE *fp;
  1035.     fp = fopen("scheme.txt", "r");
  1036.     if (NULL == fp) {
  1037.         printf("cannot open file\n");
  1038.     }
  1039.  
  1040.  
  1041.     list = new dll();
  1042.  
  1043.    
  1044.  
  1045.    
  1046.  
  1047.     m = new map();
  1048.     m->set_map(SPLIT_X, SPLIT_Y);
  1049.  
  1050.  
  1051.     printf("reading scheme\n");
  1052.  
  1053.  
  1054.    
  1055.  
  1056.     int num;
  1057.     fscanf(fp, "%d", &num);
  1058.  
  1059.     for (i = 0; i < num; i++) {
  1060.         char el_type[6];
  1061.         printf("type:");
  1062.         fscanf(fp, "%s", el_type);
  1063.  
  1064.         if (0 == strcmp(el_type, "stop"))
  1065.             break;
  1066.  
  1067.         node *curr_node;
  1068.         int pos_x, pos_y, mass;
  1069.         int body_name, conn_left_name, conn_right_name, conn_center_name;
  1070.        
  1071.         if (0 == strcmp(el_type, "block") || 0 == strcmp(el_type, "b")) {
  1072.  
  1073.             printf("name pos_x pos_y mass:");
  1074.             fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
  1075.             printf("connection: L C R:");
  1076.             fscanf(fp, "%d%d%d", &conn_left_name, &conn_center_name, &conn_right_name);
  1077.  
  1078.             list->append_back(body_name);
  1079.             curr_node = list->get_by_id(body_name);
  1080.             curr_node->set_list_param('b', body_name, i, pos_x, pos_y, mass);
  1081.             curr_node->set_cp_ids(conn_left_name, conn_center_name, conn_right_name);
  1082.            
  1083.             printf("block:%d[x:%d y:%d m:%d CL:%d CC:%d CR:%d]\n\n", body_name, pos_x, pos_y, mass, conn_left_name, conn_right_name, conn_right_name);
  1084.         }
  1085.  
  1086.         if (0 == strcmp(el_type, "cargo") || 0 == strcmp(el_type, "c")) {
  1087.  
  1088.             printf("name pos_x pos_y mass:");
  1089.             fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
  1090.             printf("connection: T B:");
  1091.             fscanf(fp, "%d", &conn_center_name);
  1092.  
  1093.             list->append_back(body_name);
  1094.             curr_node = list->get_by_id(body_name);
  1095.             curr_node->set_list_param('c', body_name, i, pos_x, pos_y, mass);
  1096.             curr_node->set_cp_ids(conn_center_name);
  1097.  
  1098.             printf("cargo:%d[x:%d y:%d m:%d CC:%d]\n\n", body_name, pos_x, pos_y, mass, conn_center_name);
  1099.         }
  1100.  
  1101.     }
  1102.  
  1103.     fclose(fp);
  1104.     printf("file has been closed\n");
  1105.  
  1106.     list->set_connection_points();
  1107.     list->phys_enabled = false;
  1108.  
  1109.  
  1110.     list->display_objects();
  1111.  
  1112.  
  1113.     int ps = 1;
  1114.     node *nn = list->get_by_pos(ps);
  1115.     int tidsean = nn->next_node->id;
  1116.     printf("next to listpos=%d is:%d\n", ps, tidsean);
  1117.  
  1118.  
  1119.  
  1120.     /*
  1121.     int id, id2;
  1122.     char c_type = 'c', b_type = 'b', e_type = 'e';
  1123.     int b_pos_x, b_pos_y, c_pos_x, c_pos_y, c_n, b_n;
  1124.    
  1125.     c_n = b_n = 0;
  1126.  
  1127.     c_pos_x = 0;
  1128.     c_pos_y = 6;
  1129.  
  1130.     b_pos_x = 0;
  1131.     b_pos_y = 3;
  1132.    
  1133.     char prev_sym = '0', curr_sym, next_sym;
  1134.     int body_num, block_num, cargo_num;
  1135.     int seq_start_pos, seq_end_pos, len_of_seq;
  1136.    
  1137.     int x;
  1138.    
  1139.  
  1140.     //command = "b^bc^b^cc";
  1141.     //command = "cbb(cc)bc";
  1142.  
  1143.     body_num = count_command(command, 'e');
  1144.     cargo_num = count_command(command, c_type);
  1145.     block_num = count_command(command, b_type);
  1146.     printf("bodies:%d cargos:%d blocks:%d\n", body_num, cargo_num, block_num);
  1147.    
  1148.     for (i = 0; i < command.length(); i++) {
  1149.  
  1150.         if (i > 0)
  1151.             prev_sym = command[i - 1];
  1152.         curr_sym = command[i];
  1153.         next_sym = command[i + 1];
  1154.         seq_start_pos = i;
  1155.         seq_end_pos = count_further(command, seq_start_pos);
  1156.         len_of_seq = seq_end_pos - seq_start_pos;
  1157.         char nearest_sym = command[seq_start_pos + len_of_seq];
  1158.         printf("[%d_%d]%c:%d   .%c\n", seq_start_pos, seq_end_pos, curr_sym, len_of_seq, nearest_sym);
  1159.        
  1160.  
  1161.         //i = seq_end_pos-1;
  1162.        
  1163.         if ('b' == curr_sym && len_of_seq > 1) {
  1164.  
  1165.             printf(" b.. ");
  1166.  
  1167.             if ('(' == nearest_sym || '^' == nearest_sym) {
  1168.                 len_of_seq -= 1;
  1169.             }
  1170.            
  1171.             for (j = 0; j < len_of_seq; j++) {
  1172.                
  1173.                 id = 100 + rand() % MAXRANDID;
  1174.  
  1175.                 if (0 == j % 2) {
  1176.                     m->set_cell(id, b_pos_x++, b_pos_y, b_type);
  1177.                 }
  1178.                 else {
  1179.                     m->set_cell(id, b_pos_x++, b_pos_y + 1, b_type);
  1180.                 }
  1181.                
  1182.             }
  1183.  
  1184.             i = i + j - 1;
  1185.  
  1186.             continue;
  1187.            
  1188.         }
  1189.         if ('b' == curr_sym && '(' == next_sym) {
  1190.            
  1191.             m->set_cell(1, b_pos_x, b_pos_y, b_type);
  1192.  
  1193.             int num_of_cargos = 2;
  1194.             //int num_of_cargos = count_further(command, i + 2);
  1195.             printf(" b(str[%d]=%c(n:%d)) ", i + 2, command[i + 2], num_of_cargos);
  1196.  
  1197.  
  1198.             for (j = 0; j < num_of_cargos; j++) {
  1199.  
  1200.                 id = 100 + rand() % MAXRANDID;
  1201.                 m->set_cell(id, b_pos_x, c_pos_y++, c_type);
  1202.  
  1203.             }
  1204.  
  1205.             b_pos_x++;
  1206.             c_pos_x++;
  1207.  
  1208.             i = i + num_of_cargos;
  1209.         }
  1210.        
  1211.         //if ('b' == curr_sym && '^' == next_sym) {
  1212.         //  id = 100 + rand() % MAXRANDID;
  1213.         //  m->set_cell(id, b_pos_x++, b_pos_y, b_type);
  1214.  
  1215.         //  int tmp_seq_start = i;
  1216.         //  int tmp_seq_end = count_further(command, tmp_seq_start, next_sym);
  1217.         // 
  1218.         //  int tmp_b_pos_y = b_pos_y;
  1219.         //  for (j = 0; j < tmp_seq_end; j++) {
  1220.         //      tmp_b_pos_y++;
  1221.         //  }
  1222.  
  1223.         //  id = 100 + rand() % MAXRANDID;
  1224.         //  m->set_cell(id, b_pos_x++, tmp_b_pos_y, b_type);
  1225.         //  i = tmp_seq_end;
  1226.  
  1227.         //  continue;
  1228.         //  }
  1229.         if ('c' == curr_sym && len_of_seq > 1 && len_of_seq <= 3) {
  1230.             for (j = 0; j < len_of_seq; j++) {
  1231.  
  1232.                 id = 100 + rand() % MAXRANDID;
  1233.                 m->set_cell(id, c_pos_x, c_pos_y + j, c_type);
  1234.             }
  1235.  
  1236.             i = i + len_of_seq - 1;
  1237.  
  1238.             continue;
  1239.         }
  1240.     }
  1241.     */
  1242.    
  1243.     printf("\n");
  1244.     /*
  1245.     for (i = 0; i < command.length(); i++) {
  1246.         // 1 = cargo, 2 = block
  1247.  
  1248.         id = 100 + rand() % MAXRANDID;
  1249.         id2 = 100 + rand() % MAXRANDID;
  1250.  
  1251.         if ('c' == command[i]) {
  1252.  
  1253.             m->set_cell(id, c_pos_x++, c_pos_y, c_type); //======
  1254.  
  1255.             c.set(id, c_n, 1, 10, 50 + 70 * i, 150);
  1256.             list->append_back(c_n, c);
  1257.             c_n++;
  1258.  
  1259.             printf(" c ");
  1260.             continue;
  1261.            
  1262.         }
  1263.         if ('b' == command[i] && 'b' == command[i+1]) {
  1264.  
  1265.             m->set_cell(id, b_pos_x++, b_pos_y, b_type); //======
  1266.  
  1267.             b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
  1268.             list->append_back(b_n, b);
  1269.             b_n++;
  1270.  
  1271.            
  1272.             m->set_cell(id2, b_pos_x++, b_pos_y - 1, b_type);
  1273.  
  1274.             b.set(id2, b_n, 2, 0, 50 + 70 * (i + 1), 200);
  1275.             list->append_back(b_n, b);
  1276.             b_n++;
  1277.  
  1278.             i += 1;
  1279.  
  1280.             printf(" bb ");
  1281.             continue;
  1282.            
  1283.         }
  1284.         if ('b' == command[i] && '(' == command[i+1] && 'c' == command[i+2]) {
  1285.  
  1286.             m->set_cell(id, b_pos_x, b_pos_y, b_type); //======
  1287.  
  1288.             b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
  1289.             list->append_back(b_n, b);
  1290.             b_n++;
  1291.  
  1292.             m->set_cell(id2, b_pos_x, c_pos_y - 2, c_type);
  1293.             b_pos_x++;
  1294.  
  1295.             b.set(id2, c_n, 1, 10, 50 + 70 * i, 150);
  1296.             list->append_back(c_n, b);
  1297.             c_n++;
  1298.  
  1299.             i += 2;
  1300.            
  1301.             printf(" b(c ");
  1302.             continue;
  1303.         }
  1304.        
  1305.         if ('b' == command[i]) {
  1306.  
  1307.             m->set_cell(id, b_pos_x++, b_pos_y, b_type); //======
  1308.  
  1309.             b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
  1310.             list->append_back(b_n, b);
  1311.             b_n++;
  1312.  
  1313.             printf(" b ");
  1314.  
  1315.             continue;
  1316.         }
  1317.        
  1318.     }
  1319.  
  1320.     list->display_forward();
  1321.     list->display_objects();
  1322.     */
  1323.  
  1324.     //load_textures();
  1325.     //glEnable(GL_TEXTURE_2D);
  1326.  
  1327.    
  1328.  
  1329.     while (!glfwWindowShouldClose(window))
  1330.     {
  1331.         glClearColor(0.6, 0.6, 0.6, 1);
  1332.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1333.  
  1334.         draw_constraints(ROOF_HEIGHT);
  1335.         draw_constraints(FLOOR_HEIGHT);
  1336.  
  1337.         if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
  1338.             m->print_map();
  1339.         }
  1340.        
  1341.         //m->draw_map();
  1342.  
  1343.  
  1344.         list->draw_objects();
  1345.         list->draw_ropes();
  1346.         list->check_movement();
  1347.         list->enable_fall(t);
  1348.  
  1349.         if (list->phys_enabled) {
  1350.             t += 0.0002;
  1351.         }
  1352.        
  1353.  
  1354.         glfwSwapBuffers(window);
  1355.         glfwPollEvents();
  1356.         //glfwWaitEvents();
  1357.     }
  1358.  
  1359.     glfwDestroyWindow(window);
  1360.     glfwTerminate();
  1361.  
  1362.     return 0;
  1363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement