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>
- #define SCREEN_WIDTH 1000
- #define SCREEN_HEIGHT 1000
- #define G_CONST 9.78
- using namespace std;
- float t;
- GLdouble A, B, C, D;
- float delta_pos(float velocity, float acceleration);
- class body;
- class block;
- class cargo;
- class pendulum;
- class map {
- public:
- int size_y;
- int size_x;
- void set_map(int, int);
- void clean_map();
- void draw_map();
- void print_map();
- void set_cell(int, int);
- private:
- int** map_arr;
- };
- void map::set_map(int rows, int cols) {
- this->size_y = rows;
- this->size_x = cols;
- this->map_arr = new int*[this->size_y];
- // a[0]
- // a[1]
- // a[2]
- // a[3]
- int i, j;
- for (i = 0; i < this->size_y; ++i) {
- this->map_arr[i] = new int[this->size_x]; // a[0][0] a[0][1] a[0][2] a[0][3]
- }
- for (i = 0; i < this->size_y; i++) {
- for (j = 0; j < this->size_x; j++) {
- this->map_arr[i][j] = 0;
- }
- }
- }
- void map::clean_map() {
- for (int i = 0; i < this->size_y; i++) {
- delete[] this->map_arr[i];
- }
- delete[] this->map_arr;
- }
- void map::draw_map() {
- int i, j;
- for (i = 0; i < SCREEN_WIDTH; i += SCREEN_WIDTH / this->size_x) {
- glColor3f(0, 1, 0);
- for (j = 0; j < SCREEN_HEIGHT; j += SCREEN_HEIGHT / this->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::print_map() {
- int i, j;
- printf("current map:\n");
- for (i = 0; i < this->size_x; i++) {
- for (j = 0; j < this->size_y; j++) {
- if (0 == this->map_arr[i][j]) {
- printf("0 ");
- }
- else {
- printf("1 ");
- }
- }
- printf("\n");
- }
- }
- 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();
- }
- 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 c_n, b_n;
- c_n = b_n = 0;
- printf("insert scheme:\n");
- string command;
- getline(cin, command);
- count_position(command);
- map *m = new map();
- m->set_map(10, 10);
- for (i = 0; i < command.length(); i++) {
- // 1 = cargo, 2 = block
- if ('c' == command[i]) {
- c.set(c_n, 1, 10, 100 + 70 * i, 150);
- list->append_back(c_n, c);
- c_n++;
- if ('(' == command[i + 1]) {
- }
- }
- if ('b' == command[i]) {
- b.set(b_n, 2, 0, 100 + 70 * i, 300);
- list->append_back(b_n, b);
- b_n++;
- if ('b' == command[i + 1]) {
- b.set(b_n, 2, 0, 100 + 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]) {
- body extra_c;
- extra_c.set(c_n, 1, 10, 100 + 70 * i, 100);
- b.down = &extra_c;
- c_n++;
- i += 3;
- }
- }
- }
- }
- list->display_forward();
- list->display_objects();
- t = 0;
- 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_map();
- list->draw_objects();
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement