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>
- #define SCREEN_WIDTH 700
- #define SCREEN_HEIGHT 500
- #define G_CONST 9.78
- using namespace std;
- float t;
- GLdouble A, B, C, D;
- float delta_pos(float velocity, float acceleration);
- class system;
- class body;
- class block;
- class cargo;
- class pendulum;
- class body {
- public:
- char *name;
- int type, num, mass;
- 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 = 30;
- }
- void body::draw() {
- float pos_x = this->curr_x;
- float pos_y = this->curr_y;
- glBegin(GL_POLYGON);
- 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();
- }
- class system {
- public:
- char *name;
- int el_num;
- int c_num, b_num;
- void draw_all();
- void add_elem(body*);
- private:
- };
- void system::draw_all() {
- int i;
- for (i = 0; i < this->el_num; i++) {
- }
- }
- void system::add_elem(body* elem) {
- }
- /*
- class block {
- public:
- char *name;
- int ord_num;
- block* left_block;
- block* right_block;
- cargo* left_cargo;
- cargo* right_cargo;
- void init();
- void add_right_block(block*);
- void add_left_block(block*);
- void add_right_cargo(cargo*);
- void add_left_cargo(cargo*);
- block(int n, float pos_x, float pos_y) {
- this->ord_num = n;
- this->curr_x = pos_x;
- this->curr_y = pos_y;
- }
- private:
- float curr_x, curr_y;
- void draw(float, float);
- };
- void block::draw(float pos_x, float pos_y) {
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 1);
- 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::init() {
- float pos_x = this->curr_x;
- float pos_y = this->curr_y;
- this->draw(pos_x, pos_y);
- }
- void block::add_right_block(block* b) {
- this->right_block = b;
- b->left_block = this;
- }
- void block::add_left_block(block* b) {
- this->left_block = b;
- b->right_block = this;
- }
- */
- 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);
- }
- /*
- typedef struct node {
- int info;
- node* prev_left;
- node* next_right;
- node* above;
- node* below;
- body* body;
- };*/
- struct Node
- {
- int value;
- Node *next_r, *prev_l;
- Node(int y) { value = y; next_r = prev_l = NULL;}
- };
- class dll {
- public:
- void append_front(int);
- void append_back(int);
- void display_forward();
- void display_backward();
- void destroy_list();
- dll() { front = NULL; back = NULL; }
- ~dll(){ destroy_list(); }
- private:
- Node *front;
- Node *back;
- };
- void dll::append_front(int x) {
- Node *n = new Node(x);
- if (front == NULL) {
- front = n;
- back = n;
- } else {
- front->prev_l = n;
- n->next_r = front;
- front = n;
- }
- cout << "appended in fornt" << endl;
- }
- void dll::append_back(int x) {
- Node *n = new Node(x);
- if (back == NULL) {
- front = n;
- back = n;
- } else {
- back->next_r = n;
- n->prev_l = back;
- back = n;
- }
- cout << "appended to back" << endl;
- }
- 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::destroy_list() {
- Node *T = back;
- while (T != NULL) {
- Node *T2 = T;
- T = T->prev_l;
- delete T2;
- }
- front = NULL;
- back = NULL;
- }
- 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 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);
- GLfloat phi = 0, center = 0;
- int i;
- t = 0;
- while (!glfwWindowShouldClose(window))
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
- i += 5;
- }
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
- i -= 5;
- }
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
- phi += 0.3;
- }
- if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
- phi -= 0.3;
- }
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement