Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // stdafx.h : include file for standard system include files,
- // or project specific include files that are used frequently, but
- // are changed infrequently
- //
- #pragma once
- #include "targetver.h"
- #define _CRT_SECURE_NO_WARNINGS
- //if print is lagging switch this
- //#include <stdio.h>
- #include <iostream>
- #include <vector>
- #include <tchar.h>
- #include <stdlib.h>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <math.h>
- #include <ctime>
- #include <time.h>
- #include <Windows.h>
- #include <string>
- #define GLEW_STATIC
- #include <GL\glew.h>
- #include <GLFW\glfw3.h>
- typedef struct { int x, y; } coord;
- #include "body.h"
- /*struct node {
- //char value[10];
- int value;
- body obj;
- node *next_r, *prev_l;
- node(int y) { value = y; next_r = prev_l = NULL; }
- };*/
- #include "cell.h"
- #include "map.h"
- #include "node.h"
- #include "dll.h"
- #define SCREEN_WIDTH 1000
- #define SCREEN_HEIGHT 1000
- #define SPLIT_X 10
- #define SPLIT_Y 10
- #define G_CONST -9.78
- #define MAXRANDID 99999
- #define REVD(COORD_Y) ((SPLIT_Y - 1) - (COORD_Y))
- node.h
- #pragma once
- class node
- {
- public:
- int id;
- char type;
- int listpos;
- node *next_node;
- node *prev_node;
- float init_pos_x;
- float init_pos_y;
- int mass;
- void set(char type, int id, int listpos, float pos_x, float pos_y, int mass);
- void draw();
- node(int);
- ~node();
- private:
- float velocity, accel;
- float curr_x, curr_y;
- int size_x, size_y;
- };
- node.cpp
- #include "stdafx.h"
- using namespace std;
- node::node(int x)
- {
- id = x;
- next_node = NULL;
- prev_node = NULL;
- }
- node::~node()
- {
- }
- void node::set(char type, int id, int listpos, float pos_x, float pos_y, int mass) {
- this->init_pos_x = pos_x;
- this->init_pos_y = pos_y;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- this->type = type;
- this->id = id;
- this->listpos = listpos;
- this->mass = mass;
- this->size_x = 100;
- this->size_y = 100;
- }
- void node::draw() {
- float center_x = this->curr_x;
- float center_y = this->curr_y;
- int size_x = this->size_x;
- int size_y = this->size_y;
- int rad = size_x / 2;
- int border = 5;
- if ('c' == this->type) {
- coord lb, lt, rt, rb;
- lb.x = center_x - size_x / 2;
- lb.y = center_y - size_y / 2;
- lt.x = center_x - size_x / 2;
- lt.y = center_y + size_y / 2;
- rt.x = center_x + size_x / 2;
- rt.y = center_y + size_y / 2;
- rb.x = center_x + size_x / 2;
- rb.y = center_y - size_y / 2;
- //outer black
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- glVertex2i(lb.x, lb.y);
- glVertex2i(lt.x, lt.y);
- glVertex2i(rt.x, rt.y);
- glVertex2i(rb.x, rb.y);
- glEnd();
- //inner white
- glBegin(GL_POLYGON);
- glColor3f(1, 1, 1);
- glVertex2i(lb.x + border, lb.y + border);
- glVertex2i(lt.x + border, lt.y - border);
- glVertex2i(rt.x - border, rt.y - border);
- glVertex2i(rb.x - border, rb.y + border);
- glEnd();
- }
- if ('b' == this->type) {
- GLfloat phi = 0, theta = 0;
- //outer black
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(center_x + rad*cos(phi), center_y + rad*sin(phi));
- }
- glEnd();
- //inner white
- glBegin(GL_POLYGON);
- glColor3f(1, 1, 1);
- for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
- glVertex2f(center_x + (rad - border)*cos(phi), center_y + (rad - border)*sin(phi));
- }
- glEnd();
- }
- }
- dll.h
- class dll {
- public:
- void append_front(int id);
- void append_back(int id);
- node* get_front();
- node* get_back();
- node* get_by_id(int id);
- node* get_by_pos(int pos);
- void display_forward();
- void display_backward();
- void display_objects();
- void draw_objects();
- void destroy_list();
- void enable_fall(float time);
- void connect_bodies(node from, node to);
- dll() { front = NULL; back = NULL; }
- ~dll() { destroy_list(); }
- private:
- node *front;
- node *back;
- };
- node.cpp
- #include "stdafx.h"
- using namespace std;
- void dll::append_front(int x) {
- node *n = new node(x);
- if (front == NULL) {
- front = n;
- back = n;
- }
- else {
- front->prev_node = n;
- n->next_node = front;
- front = n;
- }
- printf("fornt appended\n");
- }
- void dll::append_back(int x) {
- node *n = new node(x);
- if (back == NULL) {
- front = n;
- back = n;
- }
- else {
- back->next_node = n;
- n->prev_node = back;
- back = n;
- }
- printf("back appended\n");
- }
- node* dll::get_front() {
- return this->front;
- }
- node* dll::get_back() {
- return this->back;
- }
- node* dll::get_by_id(int id) {
- node *temp = front;
- node *retval;
- while (temp != NULL) {
- if (id == temp->id) {
- retval = temp;
- return retval;
- }
- temp = temp->next_node;
- }
- return NULL;
- }
- node* dll::get_by_pos(int pos) {
- node *temp = front;
- node *retval;
- int i = 0;
- while (temp != NULL) {
- if (i == pos) {
- retval = temp;
- return retval;
- }
- i++;
- temp = temp->next_node;
- }
- return NULL;
- }
- void dll::display_forward() {
- node *temp = front;
- printf("\n\nnodes in forward order:\n");
- while (temp != NULL) {
- printf("%d ", temp->id);
- temp = temp->next_node;
- }
- printf("\n");
- }
- void dll::display_backward() {
- node *temp = back;
- printf("\n\nnodes in reverse order:\n");
- while (temp != NULL) {
- printf("%d ", temp->id);
- temp = temp->prev_node;
- }
- printf("\n");
- }
- void dll::display_objects() {
- node *temp = front;
- cout << "\n\nobjects in forward order:" << endl;
- while (NULL != temp) {
- printf("(id:%d", temp->id);
- if ('c' == temp->type)
- printf("C ");
- if ('b' == temp->type)
- printf("B ");
- printf("listpos:%d) ", temp->listpos);
- temp = temp->next_node;
- }
- printf("\n");
- }
- void dll::draw_objects() {
- node *temp = front;
- while (NULL != temp) {
- temp->draw();
- temp = temp->next_node;
- }
- }
- void dll::destroy_list() {
- node *T = back;
- while (T != NULL) {
- node *T2 = T;
- T = T->prev_node;
- delete T2;
- }
- front = NULL;
- back = NULL;
- }
- glfw30
- // glfw_30.cpp : Defines the entry point for the console application.
- // http://www.glfw.org/docs/latest/quick.html
- #include "stdafx.h"
- #include <GLAux.h>
- #pragma comment(lib,"glaux.lib")
- using namespace std;
- GLdouble A, B, C, D;
- float t = 0;
- int mouse_curr_x, mouse_curr_y;
- class cell;
- class map;
- class body;
- class node;
- class dll;
- map *m;
- int count_command(string command, char type) {
- int i, comm_len = command.length();
- int retval = 0;
- if ('e' == type) {
- for (i = 0; i < comm_len; i++) {
- if ('b' == command[i] || 'c' == command[i]) {
- retval++;
- }
- }
- return retval;
- }
- for (i = 0; i < comm_len; i++) {
- if (type == command[i]) {
- retval++;
- }
- }
- return retval;
- }
- int count_further(string command, int curr_pos) {
- int i, j, num_of_same_chars;
- int seq_end_pos;
- char curr_sym = command[curr_pos];
- if (curr_pos < command.length()) {
- num_of_same_chars = 1;
- seq_end_pos = curr_pos;
- for (i = curr_pos; i < command.length(); i++) {
- if (curr_sym == command[i] && curr_sym == command[i + 1]) {
- num_of_same_chars++;
- seq_end_pos = i;
- }
- if (curr_sym == command[i] && curr_sym != command[i + 1]) {
- seq_end_pos = i + 1;
- break;
- }
- }
- return seq_end_pos;
- }
- else {
- printf("\ncount_further incorrect input!\n");
- return -1;
- }
- }
- bool lookup_next_sym(string command, int curr_pos) {
- int i;
- char curr_sym = command[curr_pos];
- char opposite;
- if ('b' == curr_sym) opposite = 'c';
- if ('c' == curr_sym) opposite = 'b';
- for (i = curr_pos; i < command.length(); i++) {
- if (command[i] == curr_sym) continue;
- if (command[i] == opposite) break;
- }
- int last_pos_of_curr_sym = i - 1;
- return true;
- }
- char nearest_next_sym(string command, int curr_pos) {
- char curr_sym = command[curr_pos];
- int seq_len = count_further(command, curr_pos);
- if (curr_pos + seq_len < command.length()) {
- return command[curr_pos + seq_len];
- }
- else
- {
- return 'x';
- }
- }
- GLuint texture[1];
- void load_textures()
- {
- AUX_RGBImageRec *texture1;
- texture1 = auxDIBImageLoadA("tex_snoop.bmp");
- glGenTextures(1, &texture[0]);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- }
- static void cursor_callback(GLFWwindow* window, double x, double y)
- {
- mouse_curr_x = (int)x;
- mouse_curr_y = (int)y;
- int tmpx = (int)(x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(y / (SCREEN_HEIGHT / SPLIT_Y));
- //printf("%d\t%d\t%c\n", (int)x, (int)y, m->get_cell_type(tmpx, tmpy));
- m->dispell_map();
- m->highlight_cell(tmpx, tmpy);
- }
- static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
- {
- int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
- if (button == GLFW_MOUSE_BUTTON_RIGHT)
- {
- if (action == GLFW_PRESS) {
- printf("%d\t%d\t%c\n", (int)mouse_curr_x, (int)mouse_curr_y, m->get_cell_type(tmpx, tmpy));
- }
- if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
- }
- if (button == GLFW_MOUSE_BUTTON_LEFT)
- {
- if (action == GLFW_PRESS) {
- printf("M: %d\t%d\t%c(before click)\n", mouse_curr_x, mouse_curr_y, m->get_cell_type(tmpx, tmpy));
- if ('e' == m->get_cell_type(tmpx, tmpy)) {
- m->erase_cell(tmpx, tmpy);
- m->set_cell(1, tmpx, tmpy, 'b');
- return;
- }
- if ('b' == m->get_cell_type(tmpx, tmpy)) {
- m->erase_cell(tmpx, tmpy);
- m->set_cell(1, tmpx, tmpy, 'c');
- return;
- }
- if ('c' == m->get_cell_type(tmpx, tmpy)) {
- m->erase_cell(tmpx, tmpy);
- m->set_cell(1, tmpx, tmpy, 'e');
- return;
- }
- }
- }
- }
- static void resize_callback(GLFWwindow* window, int width, int height)
- {
- // windowWidth = width;
- // windowHeight = height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- A = width / 4.0;
- B = 0.0;
- C = D = height / 2.0;
- //printf("Reshape occured\n");
- }
- static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (GLFW_KEY_S == key && action == GLFW_PRESS) {
- m->validate_map();
- }
- }
- static void error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- int main(int argc, _TCHAR* argv[])
- {
- if (!glfwInit()) {
- printf("glfwInit failed\n");
- return -1;
- }
- glfwSetErrorCallback(error_callback);
- GLFWwindow* window;
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
- window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Anthony's Porject", NULL, NULL);
- if (window == NULL) {
- printf("glfwOpenWindow failed.\n");
- glfwTerminate();
- return -2;
- }
- int i, j, k, attrib;
- attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
- attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
- attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, keyboard_callback);
- glfwSetFramebufferSizeCallback(window, resize_callback);
- glfwSetMouseButtonCallback(window, mouse_callback);
- glfwSetCursorPosCallback(window, cursor_callback);
- resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
- srand(time(NULL));
- FILE *fp;
- fp = fopen("scheme.txt", "r");
- if (NULL == fp) {
- printf("cannot open file\n");
- }
- dll *list = new dll();
- //body c, b;
- int id, id2;
- char c_type = 'c', b_type = 'b', e_type = 'e';
- int b_pos_x, b_pos_y, c_pos_x, c_pos_y, c_n, b_n;
- c_n = b_n = 0;
- m = new map();
- m->set_map(SPLIT_X, SPLIT_Y);
- printf("insert scheme:\n");
- string command;
- int num = 0;
- fscanf(fp, "%d", &num);
- /*
- for (i = 0; i < num; i++) {
- //char body_val[10];
- int body_val;
- body body_entity;
- fscanf(fp, "%d", &body_val);
- body_entity.id = i;
- list->append_back(body_val, body_entity);
- }
- list->display_forward();
- int tidsean = list->get_by_value(1488)->obj.id;
- printf("\nid by value_1488:%d\n", tidsean);
- tidsean = list->get_by_pos(3)->obj.id;
- printf("id by pos_3:%d\n", tidsean);
- */
- for (i = 0; i < num; i++) {
- char el_type[6];
- printf("type:");
- fscanf(fp, "%s", el_type);
- //body body_entity, body_entity_conn;
- int pos_x, pos_y, mass;
- int body_name, conn_left_name, conn_right_name, conn_top_name, conn_bot_name;
- if (0 == strcmp(el_type, "block") || 0 == strcmp(el_type, "b")) {
- printf("name pos_x pos_y mass:");
- fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
- printf("connection: L R T B:");
- fscanf(fp, "%d%d%d%d", &conn_left_name, &conn_right_name, &conn_top_name, &conn_bot_name);
- /*if (0 != strcmp(conn_left_name, "e")) {
- body_entity.left =
- }*/
- //body_entity.set('b', body_name, (float)pos_x, (float)pos_y, mass);
- list->append_back(body_name);
- node *curr_node = list->get_by_id(body_name);
- curr_node->set('b', body_name, i, pos_x, pos_y, mass);
- 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);
- }
- if (0 == strcmp(el_type, "cargo") || 0 == strcmp(el_type, "c")) {
- printf("name pos_x pos_y mass:");
- fscanf(fp, "%d%d%d%d", &body_name, &pos_x, &pos_y, &mass);
- printf("connection: T B:");
- fscanf(fp, "%d%d", &conn_top_name, &conn_bot_name);
- //body_entity.set('c', body_name, (float)pos_x, (float)pos_y, mass);
- list->append_back(body_name);
- node *curr_node = list->get_by_id(body_name);
- curr_node->set('c', body_name, i, pos_x, pos_y, mass);
- 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);
- }
- }
- fclose(fp);
- printf("file has been closed\n");
- list->display_objects();
- for (i = 1; i < num; i++) {
- node *na = list->get_by_pos(i - 1);
- node *nb = list->get_by_pos(i);
- //list->connect_bodies(&a, &b);
- }
- int ps = 2;
- node *nn = list->get_by_pos(ps);
- int tidsean = nn->next_node->id;
- printf("next to pos=%d is:%d\n", ps, tidsean);
- c_pos_x = 0;
- c_pos_y = 6;
- b_pos_x = 0;
- b_pos_y = 3;
- char prev_sym = '0', curr_sym, next_sym;
- int body_num, block_num, cargo_num;
- int seq_start_pos, seq_end_pos, len_of_seq;
- int x;
- //command = "b^bc^b^cc";
- //command = "cbb(cc)bc";
- body_num = count_command(command, 'e');
- cargo_num = count_command(command, c_type);
- block_num = count_command(command, b_type);
- printf("bodies:%d cargos:%d blocks:%d\n", body_num, cargo_num, block_num);
- for (i = 0; i < command.length(); i++) {
- if (i > 0)
- prev_sym = command[i - 1];
- curr_sym = command[i];
- next_sym = command[i + 1];
- seq_start_pos = i;
- seq_end_pos = count_further(command, seq_start_pos);
- len_of_seq = seq_end_pos - seq_start_pos;
- char nearest_sym = command[seq_start_pos + len_of_seq];
- printf("[%d_%d]%c:%d .%c\n", seq_start_pos, seq_end_pos, curr_sym, len_of_seq, nearest_sym);
- //i = seq_end_pos-1;
- if ('b' == curr_sym && len_of_seq > 1) {
- printf(" b.. ");
- if ('(' == nearest_sym || '^' == nearest_sym) {
- len_of_seq -= 1;
- }
- for (j = 0; j < len_of_seq; j++) {
- id = 100 + rand() % MAXRANDID;
- if (0 == j % 2) {
- m->set_cell(id, b_pos_x++, b_pos_y, b_type);
- }
- else {
- m->set_cell(id, b_pos_x++, b_pos_y + 1, b_type);
- }
- }
- i = i + j - 1;
- continue;
- }
- if ('b' == curr_sym && '(' == next_sym) {
- m->set_cell(1, b_pos_x, b_pos_y, b_type);
- int num_of_cargos = 2;
- //int num_of_cargos = count_further(command, i + 2);
- printf(" b(str[%d]=%c(n:%d)) ", i + 2, command[i + 2], num_of_cargos);
- for (j = 0; j < num_of_cargos; j++) {
- id = 100 + rand() % MAXRANDID;
- m->set_cell(id, b_pos_x, c_pos_y++, c_type);
- }
- b_pos_x++;
- c_pos_x++;
- i = i + num_of_cargos;
- }
- /*
- if ('b' == curr_sym && '^' == next_sym) {
- id = 100 + rand() % MAXRANDID;
- m->set_cell(id, b_pos_x++, b_pos_y, b_type);
- int tmp_seq_start = i;
- int tmp_seq_end = count_further(command, tmp_seq_start, next_sym);
- int tmp_b_pos_y = b_pos_y;
- for (j = 0; j < tmp_seq_end; j++) {
- tmp_b_pos_y++;
- }
- id = 100 + rand() % MAXRANDID;
- m->set_cell(id, b_pos_x++, tmp_b_pos_y, b_type);
- i = tmp_seq_end;
- continue;
- }*/
- if ('c' == curr_sym && len_of_seq > 1 && len_of_seq <= 3) {
- for (j = 0; j < len_of_seq; j++) {
- id = 100 + rand() % MAXRANDID;
- m->set_cell(id, c_pos_x, c_pos_y + j, c_type);
- }
- i = i + len_of_seq - 1;
- continue;
- }
- }
- printf("\n");
- /*
- for (i = 0; i < command.length(); i++) {
- // 1 = cargo, 2 = block
- id = 100 + rand() % MAXRANDID;
- id2 = 100 + rand() % MAXRANDID;
- if ('c' == command[i]) {
- m->set_cell(id, c_pos_x++, c_pos_y, c_type); //======
- c.set(id, c_n, 1, 10, 50 + 70 * i, 150);
- list->append_back(c_n, c);
- c_n++;
- printf(" c ");
- continue;
- }
- if ('b' == command[i] && 'b' == command[i+1]) {
- m->set_cell(id, b_pos_x++, b_pos_y, b_type); //======
- b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- m->set_cell(id2, b_pos_x++, b_pos_y - 1, b_type);
- b.set(id2, b_n, 2, 0, 50 + 70 * (i + 1), 200);
- list->append_back(b_n, b);
- b_n++;
- i += 1;
- printf(" bb ");
- continue;
- }
- if ('b' == command[i] && '(' == command[i+1] && 'c' == command[i+2]) {
- m->set_cell(id, b_pos_x, b_pos_y, b_type); //======
- b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- m->set_cell(id2, b_pos_x, c_pos_y - 2, c_type);
- b_pos_x++;
- b.set(id2, c_n, 1, 10, 50 + 70 * i, 150);
- list->append_back(c_n, b);
- c_n++;
- i += 2;
- printf(" b(c ");
- continue;
- }
- if ('b' == command[i]) {
- m->set_cell(id, b_pos_x++, b_pos_y, b_type); //======
- b.set(id, b_n, 2, 0, 50 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- printf(" b ");
- continue;
- }
- }
- list->display_forward();
- list->display_objects();
- */
- //load_textures();
- //glEnable(GL_TEXTURE_2D);
- while (!glfwWindowShouldClose(window))
- {
- glClearColor(0.5, 0.5, 0.5, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
- m->print_map();
- }
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
- i -= 5;
- }
- //m->draw_map();
- list->draw_objects();
- //list->enable_fall(t);
- t += 0.0001;
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement