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>
- #define SCREEN_WIDTH 1000
- #define SCREEN_HEIGHT 800
- #define G_CONST 9.78
- float t;
- GLdouble A, B, C, D;
- 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, 15);
- float pos_y = this->curr_y - delta_pos(3, 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.001;
- return delta;
- }
- int count_height(int curr_height) {
- //h = y - (g*t^2)/2
- float h = curr_height - (G_CONST * t * t) / 2;
- t += 0.00001;
- return (int)h;
- }
- 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 b(100, 700);
- //b.init();
- t = 0;
- while (!glfwWindowShouldClose(window))
- {
- // ESC = exit
- // ^, v = change size
- // <--, --> = rotate left, right
- 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;
- }
- b.grav();
- glfwSwapBuffers(window);
- glfwPollEvents();
- //glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- // clean up and exit
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement