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 <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 REVD(X) ((SPLIT_Y) - (X))
- using namespace std;
- float t;
- GLdouble A, B, C, D;
- float delta_pos(float velocity, float acceleration);
- class map;
- class body;
- class block;
- class cargo;
- class pendulum;
- typedef struct {
- int x, y;
- } coord;
- typedef struct {
- coord botleft, topleft, topright, botright;
- int type;
- } cell;
- 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);
- private:
- int map_size_y;
- int map_size_x;
- cell **map_arr;
- int cell_size_x;
- int cell_size_y;
- };
- void map::set_map(int rows, int cols) {
- this->map_size_y = rows;
- this->map_size_x = cols;
- this->cell_size_x = (int)(SCREEN_WIDTH / SPLIT_X);
- this->cell_size_y = (int)(SCREEN_HEIGHT / SPLIT_Y);
- this->map_arr = (cell **)malloc(this->map_size_y * sizeof(cell *));
- // a[0]
- // a[1]
- // a[2]
- // a[3]
- int i, j;
- for (i = 0; i < this->map_size_y; i++) {
- this->map_arr[i] = (cell *)malloc(this->map_size_x * sizeof(cell)); // a[0][0] a[0][1] a[0][2] a[0][3]
- }
- for (i = 0; i < this->map_size_y; i++) {
- for (j = 0; j < this->map_size_x; j++) {
- this->map_arr[i][j].type = 0;
- }
- }
- }
- void map::clean_map() {
- for (int i = 0; i < this->map_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->map_size_x) {
- glColor3f(1, 1, 1);
- for (j = 0; j < SCREEN_HEIGHT; j += SCREEN_HEIGHT / this->map_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->map_size_y; j++) {
- for (i = 0; i < this->map_size_x; i++) {
- if (0 != map_arr[i][j].type) {
- if (1 == map_arr[i][j].type) {
- glColor3f(1, 0, 0);
- }
- if (2 == map_arr[i][j].type) {
- glColor3f(0, 0, 1);
- }
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0f, 0.0f);
- glVertex2f(map_arr[i][j].botleft.x, map_arr[i][j].botleft.y);
- glTexCoord2f(0.0f, 1.0f);
- glVertex2f(map_arr[i][j].topleft.x, map_arr[i][j].topleft.y);
- glTexCoord2f(1.0f, 1.0f);
- glVertex2f(map_arr[i][j].topright.x, map_arr[i][j].topright.y);
- glTexCoord2f(1.0f, 0.0f);
- glVertex2f(map_arr[i][j].botright.x, map_arr[i][j].botright.y);
- glEnd();
- }
- }
- }
- }
- void map::print_map() {
- int i, j;
- printf("\ncurrent map:\n");
- for (j = 0; j < this->map_size_y; j++) {
- for (i = 0; i < this->map_size_x; i++) {
- if (0 == this->map_arr[i][j].type) {
- printf("- ");
- }
- if (1 == this->map_arr[i][j].type) {
- printf("C ");
- }
- if (2 == this->map_arr[i][j].type) {
- printf("B ");
- }
- }
- printf("\n");
- }
- }
- void map::set_cell(int x, int y, int type) {
- this->map_arr[x][REVD(y)].type = type;
- this->map_arr[x][REVD(y)].botleft.x = (int)(x * this->cell_size_x);// -this->cell_size_x / 2);
- this->map_arr[x][REVD(y)].botleft.y = (int)(y * this->cell_size_y);// + this->cell_size_y / 2);
- this->map_arr[x][REVD(y)].topleft.x = (int)(x * this->cell_size_x);// - this->cell_size_x / 2);
- this->map_arr[x][REVD(y)].topleft.y = (int)(y * this->cell_size_y + this->cell_size_y);
- this->map_arr[x][REVD(y)].topright.x = (int)(x * this->cell_size_x + this->cell_size_x);
- this->map_arr[x][REVD(y)].topright.y = (int)(y * this->cell_size_y + this->cell_size_y);
- this->map_arr[x][REVD(y)].botright.x = (int)(x * this->cell_size_x + this->cell_size_x);
- this->map_arr[x][REVD(y)].botright.y = (int)(y * this->cell_size_y);// + this->cell_size_y / 2);
- }
- class body {
- public:
- char *name;
- int type, num, mass; // 1 - cargo, 2 - block
- body *up;
- body *down;
- body *right;
- body *left;
- void set(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 num, int type, int mass, float pos_x, float pos_y) {
- 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();
- }
- float delta_pos(float velocity, float acceleration);
- class block {
- public:
- char *name;
- int mass;
- void grav();
- void init();
- block(float pos_x, float pos_y) {
- this->init_x = pos_x;
- this->init_y = pos_y;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- this->visibility = true;
- }
- private:
- float init_x, init_y;
- float curr_x, curr_y;
- bool visibility;
- void draw(float pos_x, float pos_y);
- };
- void block::draw(float pos_x, float pos_y) {
- glBegin(GL_POLYGON);
- glColor3f(1, 0, 0);
- glVertex2i(pos_x, pos_y);
- glVertex2i(pos_x + 20, pos_y);
- glVertex2i(pos_x + 20, pos_y + 20);
- glVertex2i(pos_x, pos_y + 20);
- glEnd();
- }
- void block::grav() {
- if (this->curr_y > 0) {
- float pos_x = this->curr_x + delta_pos(0, 0);
- float pos_y = this->curr_y - delta_pos(-2, G_CONST);
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- }
- else {
- this->curr_y = 0;
- this->visibility = false;
- }
- this->draw(this->curr_x, this->curr_y);
- printf("x:%f, y:%f\n", this->curr_x, this->curr_y);
- }
- void block::init() {
- float pos_x = this->init_x;
- float pos_y = this->init_y;
- this->draw(pos_x, pos_y);
- }
- float delta_pos(float velocity, float acceleration) {
- //s = s0 + v*t + (a*t^2)/2
- float delta = velocity * t + (acceleration * t * t) / 2;
- printf("delta=%f\n", delta);
- t += 0.0001;
- return delta;
- }
- class cargo {
- public:
- char *name;
- int ord_num;
- int mass;
- int move_direction;
- block* left_block;
- block* right_block;
- cargo* above_cargo;
- cargo* below_cargo;
- void grav(bool);
- void init();
- void add_right_block(block*);
- void add_left_block(block*);
- void add_above_cargo(cargo*);
- void add_below_cargo(cargo*);
- cargo(int n, float pos_x, float pos_y) {
- this->ord_num = n;
- this->init_x = pos_x;
- this->init_y = pos_y;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- this->velo_x = 0;
- this->velo_y = 0;
- if (this->velo_y == 0) {
- this->move_direction = -1;
- }
- }
- private:
- float init_x, init_y;
- float curr_x, curr_y;
- float velo_x, velo_y;
- void draw(float, float);
- };
- void cargo::draw(float pos_x, float pos_y) {
- glBegin(GL_POLYGON);
- glColor3f(0, 1, 0);
- glVertex2i(pos_x, pos_y);
- glVertex2i(pos_x + 25, pos_y);
- glVertex2i(pos_x + 25, pos_y + 25);
- glVertex2i(pos_x, pos_y + 25);
- glEnd();
- }
- void cargo::grav(bool fall) {
- if (fall) {
- float pos_y;
- if (this->curr_y <= 0) {
- this->curr_y = 0;
- this->velo_y = sqrt(2 * G_CONST * this->init_y) * 0.8;
- this->move_direction = 1;
- }
- if (this->curr_y >= 0) {
- if (this->move_direction < 0) {
- float pos_y = (G_CONST * t * t) / 2;
- this->curr_y -= pos_y;
- }
- if (this->move_direction > 0) {
- float pos_y = this->velo_y * t - (G_CONST * t * t) / 2;
- this->curr_y += pos_y;
- }
- }
- }
- this->draw(this->curr_x, this->curr_y);
- t += 0.001;
- printf("x:%f, y:%f, v=%f\n", this->curr_x, this->curr_y, this->velo_y);
- }
- void cargo::init() {
- float pos_x = this->init_x;
- float pos_y = this->init_y;
- this->draw(pos_x, pos_y);
- }
- 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("CG ");
- if (2 == temp->obj.type) printf("BK ");
- printf("[m:%d]) ", temp->obj.mass);
- 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;
- }
- void count_position(string command) {
- int comm_len = command.length();
- }
- 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, 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);
- dll *list = new dll();
- body c, b;
- int b_pos_x, b_pos_y, b_pos_y_low, c_pos_x, c_pos_y, c_pos_y_low, b_type, c_type, c_n, b_n;
- c_n = b_n = 0;
- printf("insert scheme:\n..........\n");
- string command;
- back:
- getline(cin, command);
- if (command.length() > 10) {
- printf("must be no longer than 10\n");
- goto back;
- }
- count_position(command);
- map *m = new map();
- m->set_map(SPLIT_X, SPLIT_Y);
- c_type = 1;
- b_type = 2;
- c_pos_x = 0;
- c_pos_y = 4;
- c_pos_y_low = 2;
- b_pos_x = 0;
- b_pos_y = 7;
- b_pos_y_low = 5;
- for (i = 0; i < command.length(); i++) {
- // 1 = cargo, 2 = block
- if ('c' == command[i]) {
- m->set_cell(c_pos_x++, c_pos_y, c_type); //======
- c.set(c_n, 1, 10, 50 + 70 * i, 150);
- list->append_back(c_n, c);
- c_n++;
- }
- if ('b' == command[i] && 'b' == command[i+1]) {
- m->set_cell(b_pos_x++, b_pos_y, b_type); //======
- b.set(b_n, 2, 0, 50 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- m->set_cell(b_pos_x++, b_pos_y - 2, b_type);
- b.set(b_n, 2, 0, 50 + 70 * (i + 1), 200);
- list->append_back(b_n, b);
- b_n++;
- i += 2;
- /*if ('b' == command[i + 1]) {
- m->set_cell(x_pos++, 5, 2); //======
- b.set(b_n, 2, 0, 50 + 70 * (i + 1), 200);
- list->append_back(b_n, b);
- b_n++;
- i++;
- }
- if ('(' == command[i + 1]) {
- if ('c' == command[i + 2] && ')' == command[i+3]) {
- m->set_cell(x_pos++, 1, 1); //======
- body extra_c;
- extra_c.set(c_n, 1, 10, 50 + 70 * i, 100);
- b.down = &extra_c;
- c_n++;
- i += 3;
- }
- }*/
- }
- if ('b' == command[i] && '(' == command[i+1] && 'c' == command[i+2]) {
- m->set_cell(b_pos_x, b_pos_y, b_type); //======
- b.set(b_n, 2, 0, 50 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- m->set_cell(b_pos_x, c_pos_y - 2, c_type);
- b_pos_x++;
- b.set(c_n, 1, 10, 50 + 70 * i, 150);
- list->append_back(c_n, b);
- c_n++;
- i += 2;
- }
- if ('b' == command[i]) {
- m->set_cell(b_pos_x++, b_pos_y, b_type); //======
- b.set(b_n, 2, 0, 50 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- }
- }
- list->display_forward();
- list->display_objects();
- t = 0;
- //load_textures();
- //glEnable(GL_TEXTURE_2D);
- while (!glfwWindowShouldClose(window))
- {
- 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