Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================stdafx.h
- // 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"
- //#include <stdio.h>
- #include <iostream>
- #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
- {
- 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 "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))
- //=============================================================glfw_30.cpp
- // 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;
- int mouse_curr_x, mouse_curr_y;
- float t;
- bool allow_add_flag;
- class cell;
- class map;
- class body;
- 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)
- {
- if (button == GLFW_MOUSE_BUTTON_RIGHT)
- {
- if (action == GLFW_PRESS) {
- int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
- 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\n", mouse_curr_x, mouse_curr_y);
- int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
- 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_T == key && action == GLFW_PRESS) { gravity(); }
- }
- 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));
- 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..........\n");
- string command;
- back:
- getline(cin, command);
- 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;
- i = 0;
- int ggg;
- for (x = 0; x < SPLIT_X; x++) {
- allow_add_flag = false;
- int curr_x_min = x * (int)(SCREEN_WIDTH / SPLIT_X);
- int curr_x_max = (x+1) * (int)(SCREEN_WIDTH / SPLIT_X);
- if (mouse_curr_x > curr_x_min && mouse_curr_x < curr_x_max) {
- int tmpx = (int)(mouse_curr_x / (SCREEN_WIDTH / SPLIT_X));
- int tmpy = (int)(mouse_curr_y / (SCREEN_HEIGHT / SPLIT_Y));
- //if ('e' == m->get_cell_type(tmpx, tmpy)) {
- allow_add_flag = true;
- //}
- }
- }*/
- //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();
- */
- t = 0;
- //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_obj();
- m->draw_net();
- //list->draw_objects();
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
- //=============================================================cell.h
- class cell {
- public:
- //encapsulation err
- int id;
- char type;
- bool highlighted;
- void set_id(int);
- void set_coord(int, int);
- void set_type(char);
- void draw_cell();
- void erase();
- private:
- int map_pos_x, map_pos_y;
- coord botleft;
- coord topleft;
- coord topright;
- coord botright;
- int size_y, size_x;
- };
- //=============================================================cell.cpp
- #include "stdafx.h"
- using namespace std;
- void cell::set_id(int cid) {
- this->id = cid;
- //printf("id:%d ", this->id);
- }
- void cell::set_type(char ctype) {
- this->type = ctype;
- //printf("t:%c ", this->type);
- }
- void cell::set_coord(int x, int y) {
- this->size_x = (int)(SCREEN_WIDTH / SPLIT_X);
- this->size_y = (int)(SCREEN_HEIGHT / SPLIT_Y);
- this->map_pos_x = x;
- this->map_pos_y = y;
- this->topleft.x = (int)(x * this->size_x);
- this->topleft.y = (int)(REVD(y) * this->size_y);
- this->topright.x = (int)(x * this->size_x + this->size_x);
- this->topright.y = (int)(REVD(y) * this->size_y);
- this->botright.x = (int)(x * this->size_x + this->size_x);
- this->botright.y = (int)(REVD(y) * this->size_y + this->size_y);
- this->botleft.x = (int)(x * this->size_x);
- this->botleft.y = (int)(REVD(y) * this->size_y + this->size_y);
- }
- void cell::draw_cell() {
- if ('e' == this->type) {
- glColor3f(0.5, 0.5, 0.5);
- }
- if ('c' == this->type) {
- glColor3f(1, 0, 0);
- }
- if ('b' == this->type) {
- glColor3f(0, 0, 1);
- }
- if (this->highlighted) {
- glColor3f(0.6, 0.6, 0.6);
- }
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0f, 0.0f);
- glVertex2f(this->botleft.x, this->botleft.y);
- glTexCoord2f(0.0f, 1.0f);
- glVertex2f(this->topleft.x, this->topleft.y);
- glTexCoord2f(1.0f, 1.0f);
- glVertex2f(this->topright.x, this->topright.y);
- glTexCoord2f(1.0f, 0.0f);
- glVertex2f(this->botright.x, this->botright.y);
- glEnd();
- if ('b' == this->type) {
- glColor3f(0.55, 0.55, 0.55);
- glBegin(GL_POLYGON);
- glVertex2f(this->topleft.x + 40, this->topleft.y + this->size_y / 2);
- glVertex2f(this->topleft.x + 40, SCREEN_HEIGHT + 100);
- glVertex2f(this->topright.x - 40, SCREEN_HEIGHT + 100);
- glVertex2f(this->topright.x - 40, this->topright.y + this->size_y / 2);
- glEnd();
- }
- }
- void cell::erase() {
- this->type = 'e';
- this->id = 0;
- this->highlighted = false;
- }
- //=============================================================map.h
- class map {
- public:
- void set_map(int, int);
- void clean_map();
- void draw_net();
- void draw_obj();
- void print_map();
- void dispell_map();
- void set_cell(int, int, int, char);
- char get_cell_type(int, int);
- void erase_cell(int, int);
- void highlight_cell(int, int);
- private:
- int arr_size_y;
- int arr_size_x;
- cell** map_arr;
- int cell_size_x;
- int cell_size_y;
- };
- //=============================================================map.cpp
- #include "stdafx.h"
- using namespace std;
- void map::set_map(int rows, int cols) {
- this->arr_size_y = rows;
- this->arr_size_x = cols;
- this->cell_size_x = (int)(SCREEN_WIDTH / SPLIT_X);
- this->cell_size_y = (int)(SCREEN_HEIGHT / SPLIT_Y);
- cell** map = new cell*[this->arr_size_y];
- this->map_arr = map;
- //this->map_arr = (cell **)malloc(this->arr_size_y * sizeof(cell *));
- // a[0]
- // a[1]
- // a[2]
- int i, j;
- /*for (i = 0; i < this->arr_size_y; i++) {
- this->map_arr[i] = (cell *)malloc(this->arr_size_x * sizeof(cell));
- }*/
- for (i = 0; i < this->arr_size_y; i++) {
- this->map_arr[i] = new cell[this->arr_size_x];
- }
- for (j = 0; j < this->arr_size_y; j++) {
- for (i = 0; i < this->arr_size_x; i++) {
- this->map_arr[i][j].set_id(0);
- this->map_arr[i][j].set_type('e');
- this->map_arr[i][j].set_coord(i, j);
- this->map_arr[i][j].highlighted = false;
- }
- }
- for (j = 0; j < this->arr_size_y; j++) {
- for (i = 0; i < this->arr_size_x; i++) {
- printf("%d:%d ", i, j);
- }
- printf("\n");
- }
- }
- void map::clean_map() {
- for (int i = 0; i < this->arr_size_y; i++) {
- delete[] this->map_arr[i];
- }
- delete[] this->map_arr;
- }
- void map::draw_net() {
- int i, j;
- for (i = 0; i < SCREEN_WIDTH; i += SCREEN_WIDTH / this->arr_size_x) {
- glColor3f(1, 1, 1);
- for (j = 0; j < SCREEN_HEIGHT; j += SCREEN_HEIGHT / this->arr_size_y) {
- glBegin(GL_LINES);
- glVertex2f(i, 0);
- glVertex2f(i, SCREEN_HEIGHT);
- glEnd();
- glBegin(GL_LINES);
- glVertex2f(0, j);
- glVertex2f(SCREEN_WIDTH, j);
- glEnd();
- }
- }
- }
- void map::draw_obj() {
- int i, j;
- for (j = 0; j < this->arr_size_y; j++) {
- for (i = 0; i < this->arr_size_x; i++) {
- //if ('e' != map_arr[i][j].type) {
- map_arr[i][j].draw_cell();
- //}
- }
- }
- }
- void map::print_map() {
- int i, j;
- printf("\ncurrent map:\n");
- for (j = 0; j < this->arr_size_y; j++) {
- for (i = 0; i < this->arr_size_x; i++) {
- if ('e' == this->map_arr[i][j].type) {
- printf("- ");
- }
- if ('c' == this->map_arr[i][j].type) {
- printf("C ");
- }
- if ('b' == this->map_arr[i][j].type) {
- printf("B ");
- }
- }
- printf("\n");
- }
- }
- void map::dispell_map() {
- int i, j;
- for (j = 0; j < this->arr_size_y; j++) {
- for (i = 0; i < this->arr_size_x; i++) {
- this->map_arr[i][j].highlighted = false;
- }
- }
- }
- void map::set_cell(int cid, int x, int y, char ctype) {
- this->map_arr[x][y].set_id(cid);
- this->map_arr[x][y].set_type(ctype);
- this->map_arr[x][y].set_coord(x, y);
- }
- void map::highlight_cell(int x, int y) {
- this->map_arr[x][y].highlighted = true;
- }
- char map::get_cell_type(int x, int y) {
- return this->map_arr[x][y].type;
- }
- void map::erase_cell(int x, int y) {
- this->map_arr[x][y].erase();
- }
- //=============================================================body.h
- class body {
- public:
- char *name;
- int id;
- int type, num, mass; // 1 - cargo, 2 - block
- body *up;
- body *down;
- body *right;
- body *left;
- void set(int, int, int, int, float, float);
- void draw();
- private:
- float curr_x, curr_y;
- float init_x, init_y;
- float center_x, center_y;
- int h, w;
- };
- //=============================================================body.cpp
- #include "stdafx.h"
- using namespace std;
- void body::set(int bid, int num, int type, int mass, float pos_x, float pos_y) {
- this->id = bid;
- this->init_x = pos_x;
- this->init_y = pos_y;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- this->type = type;
- this->num = num;
- this->mass = mass;
- this->up = NULL;
- this->down = NULL;
- this->right = NULL;
- this->left = NULL;
- this->h = 20;
- this->w = 20;
- }
- void body::draw() {
- float pos_x = this->curr_x;
- float pos_y = this->curr_y;
- // 1 = cargo, 2 = block
- glBegin(GL_POLYGON);
- if (1 == this->type) glColor3f(1, 0, 0);
- if (2 == this->type) glColor3f(0, 0, 1);
- glVertex2i(pos_x, pos_y);
- glVertex2i(pos_x + this->w, pos_y);
- glVertex2i(pos_x + this->w, pos_y + this->h);
- glVertex2i(pos_x, pos_y + this->h);
- glEnd();
- }
- //=============================================================dll.h
- class dll {
- public:
- void append_front(int, body);
- void append_back(int, body);
- node* get_front();
- node* get_back();
- void display_forward();
- void display_backward();
- void display_objects();
- void draw_objects();
- void destroy_list();
- dll() { front = NULL; back = NULL; }
- ~dll() { destroy_list(); }
- private:
- node *front;
- node *back;
- };
- //=============================================================dll.cpp
- #include "stdafx.h"
- using namespace std;
- void dll::append_front(int x, body b) {
- node *n = new node(x);
- n->obj = b;
- if (front == NULL) {
- front = n;
- back = n;
- }
- else {
- front->prev_l = n;
- n->next_r = front;
- front = n;
- }
- cout << "fornt appended" << endl;
- }
- void dll::append_back(int x, body b) {
- node *n = new node(x);
- n->obj = b;
- if (back == NULL) {
- front = n;
- back = n;
- }
- else {
- back->next_r = n;
- n->prev_l = back;
- back = n;
- }
- cout << "back appended" << endl;
- }
- node* dll::get_front() {
- return this->front;
- }
- node* dll::get_back() {
- return this->back;
- }
- void dll::display_forward() {
- node *temp = front;
- cout << "\n\nnodes in forward order:" << endl;
- while (temp != NULL) {
- cout << temp->value << " ";
- temp = temp->next_r;
- }
- }
- void dll::display_backward() {
- node *temp = back;
- cout << "\n\nnodes in reverse order:" << endl;
- while (temp != NULL) {
- cout << temp->value << " ";
- temp = temp->prev_l;
- }
- }
- void dll::display_objects() {
- node *temp = front;
- cout << "\n\nobjects in forward order:" << endl;
- while (NULL != temp) {
- printf("(%d", temp->value);
- if (1 == temp->obj.type) printf("C ");
- if (2 == temp->obj.type) printf("B ");
- printf("[id:%d]) ", temp->obj.id);
- temp = temp->next_r;
- }
- }
- void dll::draw_objects() {
- node *temp = front;
- while (NULL != temp) {
- temp->obj.draw();
- if (temp->obj.down != NULL) temp->obj.down->draw();
- temp = temp->next_r;
- }
- }
- void dll::destroy_list() {
- node *T = back;
- while (T != NULL) {
- node *T2 = T;
- T = T->prev_l;
- delete T2;
- }
- front = NULL;
- back = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement