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>
- #define SCREEN_WIDTH 700
- #define SCREEN_HEIGHT 500
- #define G_CONST 9.78
- 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, float, float);
- private:
- float curr_x, curr_y;
- float init_x, init_y;
- float center_x, center_y;
- int h, w;
- void draw(float, float);
- };
- void body::set(int num, int type, 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->up = NULL;
- this->down = NULL;
- this->right = NULL;
- this->left = NULL;
- this->h = 20;
- this->w = 30;
- }
- void body::draw(float pos_x, float pos_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 {
- char* name;
- int param;
- node* prev_left;
- node* next_right;
- node* above;
- node* below;
- } *first;
- class dll {
- public:
- char* name;
- node *first;
- node *last;
- int amount;
- void create_list(int);
- void add_first(int);
- void add_last(int);
- void add_after(int, int);
- void display();
- dll() {
- this->first = NULL;
- this->last = NULL;
- this->amount = 0;
- }
- };
- /*
- * Create Double Link List
- */
- void dll::create_list(int value)
- {
- node *s, *temp;
- temp = new(node);
- temp->param = value;
- temp->next_right = NULL;
- if (this->first == NULL)
- {
- temp->prev_left = NULL;
- this->first = temp;
- } else {
- s = this->first;
- while (s->next_right != NULL)
- s = s->next_right;
- s->next_right = temp;
- temp->prev_left = s;
- }
- this->amount++;
- printf("created\n");
- }
- /*
- * Insertion at the beginning
- */
- void dll::add_first(int value)
- {
- if (this->first == NULL)
- {
- printf( "First Create the list.\n" );
- return;
- }
- struct node *temp;
- temp = new(struct node);
- temp->prev_left = NULL;
- temp->param = value;
- temp->next_right = this->first;
- this->first->prev_left = temp;
- this->first = temp;
- this->amount++;
- printf( "Element Inserted\n" );
- }
- /*
- * Insertion of element at a particular position
- */
- void dll::add_after(int pos, int value)
- {
- if (this->first == NULL)
- {
- printf( "First Create the list.\n" );
- return;
- }
- struct node *tmp, *q;
- int i;
- q = this->first;
- for (i = 0; i < pos - 1; i++)
- {
- q = q->next_right;
- if (q == NULL)
- {
- printf("There are less elements\n");
- return;
- }
- }
- tmp = new(struct node);
- tmp->param = value;
- if (q->next_right == NULL)
- {
- q->next_right = tmp;
- tmp->next_right = NULL;
- tmp->prev_left = q;
- }
- else
- {
- tmp->next_right = q->next_right;
- tmp->next_right->prev_left = tmp;
- q->next_right = tmp;
- tmp->prev_left = q;
- }
- this->amount++;
- printf("added after\n");
- }
- void dll::display()
- {
- struct node *q;
- if (this->first == NULL)
- {
- printf( "List empty,nothing to display" );
- return;
- }
- q = this->first;
- printf( "The Doubly Linked List[%d] is :", this->amount );
- while (q != NULL)
- {
- printf("[%d] <-> ", q->param);
- q = q->next_right;
- }
- printf("\n");
- }
- void gravity() {
- printf("grav");
- }
- 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[])
- {
- // initialise GLFW
- 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, "Test app", 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 = 1;
- /*
- block b1(1, 150, 450);
- block b2(2, 300, 450);
- block b3(3, 450, 450);
- b1.right_block = &b2;
- b1.left_block = NULL;
- b2.right_block = &b3;
- b2.left_block = &b1;
- b3.right_block = NULL;
- b3.left_block = &b2;
- */
- //printf("%d->%d->%d", b1.ord_num, b1.right_block);
- cargo c1(1, 200, 400);
- block b(100, 400);
- /*
- int num_of_el;
- printf("\ninsert num of elements:");
- scanf_s("%d", &num_of_el);
- for (i = 0; i < num_of_el; i++) {
- printf("%d elem:\n", i);
- body tmp;
- char type[3];
- float pos_x, pos_y;
- scanf_s("%c %f %f", type, &pos_x, &pos_y);
- if ('c' == type[0]) {
- tmp.set(i, 1, pos_x, pos_y);
- }
- if ('b' == type[0]) {
- tmp.set(i, 2, pos_x, pos_y);
- }
- }
- */
- dll list;
- list.create_list(1);
- node tmp;
- tmp.param = 1;
- list.add_first(0);
- list.display();
- tmp.param = 3;
- list.add_after(2, 3);
- list.display();
- tmp.param = 3;
- list.add_after(3, 2);
- list.display();
- tmp.param = 3;
- list.add_after(4, 2);
- list.display();
- 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;
- }
- //c1.grav(true);
- //b1.init();
- //b2.init();
- //b3.init();
- //b.grav();
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- // clean up and exit
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment