Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // glfw_30.cpp : Defines the entry point for the console application.
- // http://www.glfw.org/docs/latest/quick.html
- #include "stdafx.h"
- #include <time.h>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include <ctime>
- #include <GLAux.h>
- #pragma comment(lib,"glaux.lib")
- #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(X) ((SPLIT_Y) - (X))
- using namespace std;
- GLdouble A, B, C, D;
- float t;
- typedef struct {int x, y;} coord;
- class cell;
- class map;
- class body;
- class cell {
- public:
- //incapsulation err
- int id;
- char type;
- void set_id(int);
- void set_coord(int, int);
- void set_type(char);
- void draw_cell();
- cell() { this->type = 'e'; this->id = 0;
- this->size_x = (int)(SCREEN_WIDTH / SPLIT_X);
- this->size_y = (int)(SCREEN_HEIGHT / SPLIT_Y);
- }
- private:
- int map_pos_x, map_pos_y;
- coord botleft, topleft, topright, botright;
- int size_y, size_x;
- };
- 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->map_pos_x = x;
- this->map_pos_y = y;
- this->topleft.x = (int)(x * this->size_x);
- this->topleft.y = (int)(y * this->size_y);
- this->topright.x = (int)(x * this->size_x + this->size_x);
- this->topright.y = (int)(y * this->size_y);
- this->botright.x = (int)(x * this->size_x + this->size_x);
- this->botright.y = (int)(y * this->size_y - this->size_y);
- this->botleft.x = (int)(x * this->size_x);
- this->botleft.y = (int)(y * this->size_y - this->size_y);
- }
- void cell::draw_cell() {
- if ('e' == this->type) {
- printf("cannot draw empty cell:");
- printf("[x:%d y:%d]", this->map_pos_x, this->map_pos_y);
- return;
- }
- if ('c' == this->type) {
- glColor3f(1, 0, 0);
- }
- if ('b' == this->type) {
- glColor3f(0, 0, 1);
- }
- 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.4, 0.4, 0.4);
- 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();
- }
- }
- class map {
- public:
- void set_map(int, int);
- void clean_map();
- void draw_net();
- void draw_obj();
- void print_map();
- void set_cell(int, int, int, char);
- private:
- int arr_size_y;
- int arr_size_x;
- cell** map_arr;
- int cell_size_x;
- int cell_size_y;
- };
- 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*[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];
- }
- }
- 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::set_cell(int cid, int x, int y, char ctype) {
- this->map_arr[x][REVD(y)].set_id(cid);
- this->map_arr[x][REVD(y)].set_type(ctype);
- this->map_arr[x][REVD(y)].set_coord(x, y);
- }
- 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;
- };
- 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();
- }
- struct node
- {
- int value;
- body obj;
- node *next_r, *prev_l;
- node(int y) { value = y; next_r = prev_l = NULL; }
- };
- 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;
- };
- 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;
- }
- int count_command(string command, char type) {
- int i, comm_len = command.length();
- int retval = 0;
- if (0 == 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, char curr_sym) {
- int i, j, num_of_same_chars;
- int seq_end_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;
- }
- 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)
- {
- }
- static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
- {
- if (button == GLFW_MOUSE_BUTTON_RIGHT)
- {
- if (action == GLFW_PRESS) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
- if (action == GLFW_RELEASE) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
- }
- if (button == GLFW_MOUSE_BUTTON_LEFT)
- {
- }
- }
- 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';
- int b_pos_x, b_pos_y, c_pos_x, c_pos_y, c_n, b_n;
- c_n = b_n = 0;
- printf("insert scheme:\n..........\n");
- string command;
- back:
- getline(cin, command);
- map *m = new map();
- m->set_map(SPLIT_X, SPLIT_Y);
- c_pos_x = 0;
- c_pos_y = 3;
- b_pos_x = 0;
- b_pos_y = 6;
- 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;
- body_num = count_command(command, 0);
- 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, curr_sym);
- len_of_seq = seq_end_pos - seq_start_pos;
- printf("[%d_%d]%c:%d\n", seq_start_pos, seq_end_pos, curr_sym, len_of_seq);
- id = 100 + rand() % MAXRANDID;
- id2 = 100 + rand() % MAXRANDID;
- i = seq_end_pos-1;
- if ('b' == curr_sym && len_of_seq > 1) {
- for (j = 0; j < len_of_seq; j++) {
- 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);
- }
- }
- }
- if ('b' == curr_sym && '^' == next_sym) {
- 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);
- printf(" *%c:%d* ", next_sym, tmp_seq_end-tmp_seq_start);
- int tmp_b_pos_y = b_pos_y;
- for (j = 0; j < tmp_seq_end; j++) {
- tmp_b_pos_y++;
- }
- m->set_cell(id, b_pos_x++, tmp_b_pos_y, b_type);
- i = tmp_seq_end;
- }
- if ('c' == curr_sym && len_of_seq > 1 && len_of_seq <= 3) {
- for (j = 0; j < len_of_seq; j++) {
- m->set_cell(id, c_pos_x, c_pos_y - j, c_type);
- }
- }
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement