Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //rm snake;
- //c++ -Wall -pedantic -o snake snake.cpp -std=c++11 -pthread -Wno-deprecated-declarations -framework GLUT -framework OpenGL;
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <iostream>
- #include <termios.h>
- #include <chrono>
- #include <thread>
- #include <mutex>
- #include <string>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <cmath>
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #if defined(linux) || defined(_WIN32)
- #include <GL/glut.h>
- #else
- #include <GLUT/GLUT.h>
- #endif
- using namespace std;
- int* getTerminalSize(){
- struct winsize w;
- ioctl(0, TIOCGWINSZ, &w);
- printf("lines %dcolumns %d\n", w.ws_row, w.ws_col);
- return new int[2]{w.ws_col, w.ws_row};
- }
- void clear(){
- }
- void setCaret(int x, int y){
- printf("\033[%d;%dH", x, y);
- }
- void set_mode(int want_key){
- }
- int keys[0xFFFF] = {0};
- int* size = new int[2]{0, 0};
- string* buffer = NULL;
- void resize(int*& newSize){
- string* temp = buffer;
- buffer = new string[newSize[0] * newSize[1]];
- if(temp != NULL){
- delete [] temp;
- }
- size = newSize;
- for(int y = 0; y < size[1]; y++){
- for(int x = 0; x < size[0]; x++){
- buffer[y * size[0] + x] = '_';
- }
- }
- }
- int FPS = 30;
- typedef struct{
- int x;
- int y;
- } Point;
- struct{
- Point** body;
- int length;
- Point vel{0, 0};
- } snake;
- int foodLength = 100;
- Point** food;
- void GameThread(){
- while(1){
- //int* terminalSize = getTerminalSize();
- //resize(terminalSize);
- Point* temp = snake.body[snake.length - 1];
- temp->x = snake.body[0]->x;
- temp->y = snake.body[0]->y;
- for(int i = snake.length - 1; i >= 0; i--){
- snake.body[i] = snake.body[i - 1];
- }
- snake.body[0] = temp;
- Point& head = *snake.body[0];
- head.x += snake.vel.x;
- head.y += snake.vel.y;
- if(head.x < 0 ){
- head.x = size[0] - 1;
- }else if(head.x >= size[0]){
- head.x = 0;
- }
- if(head.y < 0 ){
- head.y = size[1] - 2;
- }else if(head.y >= size[1] - 1){
- head.y = 0;
- }
- for(int i = 0; i < snake.length; i++){
- int x = snake.body[i]->x;
- int y = snake.body[i]->y;
- buffer[y * size[0] + x] = 'a';
- }
- for(int i = 0; i < foodLength; i++){
- int x = food[i]->x;
- int y = food[i]->y;
- if(head.x == x && head.y == y){
- free(food[i]);
- food[i] = food[foodLength - 1];
- foodLength--;
- snake.length++;
- snake.body = (Point**) realloc(snake.body, sizeof(Point*) * snake.length);
- snake.body[snake.length - 1] = new Point{x, y};
- }
- buffer[y * size[0] + x] = 'f';
- }
- for(int y = 0; y < size[1] - 1; y++){
- string line = "";
- for(int x = 0; x < size[0]; x++){
- line += buffer[y * size[0] + x];
- }
- //cout<<line;
- }
- if(snake.vel.x){
- if(keys[GLUT_KEY_UP]){
- snake.vel.y = -1;
- snake.vel.x = 0;
- }
- if(keys[GLUT_KEY_DOWN]){
- snake.vel.y = 1;
- snake.vel.x = 0;
- }
- }else{
- if(keys[GLUT_KEY_LEFT]){
- snake.vel.x = -1;
- snake.vel.y = 0;
- }
- if(keys[GLUT_KEY_RIGHT]){
- snake.vel.x = 1;
- snake.vel.y = 0;
- }
- }
- //setCaret(0, 0);
- this_thread::sleep_for(chrono::nanoseconds(1000000000 / FPS));
- }
- }
- void startGame(){
- snake.vel.x = 1;
- snake.length = 30;
- snake.body = new Point*[snake.length];
- int* terminalSize = getTerminalSize();
- resize(terminalSize);
- for(int i = 0; i < snake.length; i++){
- snake.body[i] = new Point{terminalSize[0] - 1 - i, 10};
- }
- food = new Point*[foodLength];
- for(int i = 0; i < foodLength; i++){
- food[i] = new Point{rand() % size[0], rand() % size[1]};
- }
- Point** body = snake.body;
- body = (Point**) realloc(snake.body, sizeof(Point*) * snake.length);
- snake.body = body;
- //system("setterm -cursor off");
- thread thr(GameThread);
- thr.detach();
- }
- float a = 0;
- void display(){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(-1, 1, 0);
- glScaled((double) 2 / glutGet(GLUT_WINDOW_WIDTH), (double) -2 / glutGet(GLUT_WINDOW_HEIGHT), 1);
- glPolygonMode(GL_FRONT, GL_LINE);
- glPolygonMode(GL_BACK, GL_FILL);
- glTranslatef(50, 50, 0);
- glRotatef(a * 100, 0, 0, 1);
- glTranslatef(-5, -5, 0);
- a += 0.01;
- glBegin(GL_POLYGON);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3f(0.0, 0.0, 0);
- glVertex3f(10, 0.0, 0);
- glVertex3f(10, 0.2, 0);
- glVertex3f(5, 0.2, 0);
- glVertex3f(5, 0.8, 0);
- glVertex3f(10, 0.8, 0);
- glVertex3f(10, 10, 0);
- glVertex3f(0.0, 10, 0);
- glEnd();
- glTranslatef(5, 5, 0);
- glRotatef(a * -100, 0, 0, 1);
- glBegin(GL_POLYGON);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3f(0.0, 0.0, 0);
- glVertex3f(10, 0.0, 0);
- glVertex3f(10, 10, 0);
- glVertex3f(0.0, 10, 0);
- glEnd();
- glFlush();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void keyHandler(int key, int state, int x, int y){
- //cout<<key;
- switch(key) {
- case 27: // Escape key
- exit(0);
- break;
- case GLUT_KEY_UP:
- case GLUT_KEY_RIGHT:
- case GLUT_KEY_DOWN:
- case GLUT_KEY_LEFT:
- keys[key] = state;
- break;
- }
- }
- void keyUpHandler(int key, int x, int y){
- keyHandler(key, 0, x, y);
- }
- void keyDownHandler(int key, int x, int y){
- keyHandler(key, 0, x, y);
- }
- void mouseHandler(int button, int state, int x, int y){
- cout << button << "| " << state << "| " << x << "| " << y << "| " << endl;
- }
- void motionHandler(int x, int y){
- //cout << x << "| " << y << "| " << endl;
- }
- void initGL(){
- int argc = 0;
- char *argv[1] = {};
- glutInit(&argc, argv);
- //glutInitWindowPosition(200, 200);
- glutInitWindowSize(200, 200);
- glutInitDisplayMode(GLUT_RGBA | GLUT_MULTISAMPLE | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("snake");
- glutDisplayFunc(display);
- glutSpecialUpFunc(keyUpHandler);
- glutSpecialFunc(keyDownHandler);
- glutMouseFunc(mouseHandler);
- glutPassiveMotionFunc(motionHandler);
- glutMotionFunc(motionHandler);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glutMainLoop();
- }
- int main() {
- //startGame();
- cout << "asdsdfasdfa sdfasdf \n\r";
- cout << "asd\n";
- initGL();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement