Пример #1
0
int main(int argc, char* argv[])
{
  Window win(glm::ivec2(600,600));

  while (!win.shouldClose()) {
    if (win.isKeyPressed(GLFW_KEY_ESCAPE)) {
      win.stop();
    }

    auto t = win.getTime();
    float x1(0.5 + 0.5 * std::sin(t)); float y1(0.5 + 0.5 * std::cos(t));
    float x2(0.5 + 0.5 * std::sin(2.0*t)); float y2(0.5 + 0.5 * std::cos(2.0*t));
    float x3(0.5 + 0.5 * std::sin(t-10.f)); float y3(0.5 + 0.5 * std::cos(t-10.f));

    win.drawPoint(x1, y1, 255, 0, 0);
    win.drawPoint(x2, y2, 0, 255, 0);
    win.drawPoint(x3, y3, 0, 0, 255);

    auto m = win.mousePosition();
    win.drawLine(0.1f,0.1f, 0.8f,0.1f, 255,0,0);

    win.drawLine(0.0f, m.y, 0.01f, m.y, 0,0,0);
    win.drawLine(0.99f, m.y,1.0f, m.y, 0,0,0);

    win.drawLine(m.x, 0.0f, m.x, 0.01f, 0,0,0);
    win.drawLine(m.x, 0.99f, m.x, 1.0f, 0,0,0);

    //Begrenzungsrahmen (beim Mac unteres linkes Viertel)
    win.drawLine(1.0f, 0.0f, 1.0f, 1.0f, 0, 0, 0);
    win.drawLine(0.0, 1.0f, 1.0f, 1.0f, 0, 0, 0);

    //Zeichnen eines Rechtecks mit Anpassung der Linienfarbe bei Mouseover
    Rectangle rectangle{{0.35, 0.2}, 0.3, 0.4, {0, 255, 0}};
    draw_inside(win, rectangle, m);

    //Test der translate-Funktion an einem Viereck-Punkt (funktioniert nicht)
    auto rectangle1(rectangle);
    auto rectangle2(rectangle);
    rectangle1.color({206, 19, 182});
    rectangle1.translate(0.2, 0.3);
    rectangle2.color({206, 19, 182});
    rectangle2.translate(-0.2, 0.3);
    
    draw_inside(win, rectangle1, m);
    draw_inside(win, rectangle2, m);

    //Zeichnen eines Kreises
    Circle circle{{0.5, 0.32}, 0.1, {255, 0, 0}};
    Circle circle1{{0.23,0.75}, 0.05, {0}};
    Circle circle2{{0.65,0.75}, 0.05, {0}};
    circle1.draw(win);
    circle2.draw(win);
    
    draw_inside(win, circle, m);
    
    win.update();
  }

  return 0;
}
Пример #2
0
void make_crater_list() {
	int j;
        float theta;

	listnum = glGenLists(1);
	glNewList(listnum, GL_COMPILE);

	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glColor3f(0.27, 0.3, 0.27);
	for(j = 0; j < NUM_CRATERS; j++) {
		glPushMatrix();
		glTranslatef(x[j], 0.0, y[j]);

		glBegin(GL_TRIANGLE_STRIP);
		if (BINC * sqrt(x[j]*x[j] + y[j]*y[j]) > 2 * M_PI / 5) {
			draw_outside(radius[j], height[j], 2 * M_PI / 5 );
			draw_inside(radius[j], height[j], 2 * M_PI / 5);
 		}
		draw_outside(radius[j], height[j], BINC * sqrt(x[j]*x[j] + y[j]*y[j]));
		draw_inside(radius[j], height[j], BINC * sqrt(x[j]*x[j] + y[j]*y[j]));
		glEnd();
		glPopMatrix();
	}

        do_terrain();

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glColor3f(0.05, 0.07, 0.05);
	for(j = 0; j < NUM_CRATERS; j++) {
		glPushMatrix();
		glTranslatef(x[j], 0.0, y[j]);

		glBegin(GL_QUADS);
		draw_lines(radius[j], height[j], WINC);
		glEnd();

		glPopMatrix();
	}

	glEndList();
}
Пример #3
0
void
gear(int nt, float wd, float ir, float or, float sa, float tp, float tip, int ns, Profile * ip)
{
  /**
   * nt - number of teeth 
   * wd - width of gear at teeth
   * ir - inside radius absolute scale
   * or - radius at outside of wheel (tip of tooth) ratio of ir
   * sa - angle of slant of teeth on gear (1 = slant of one whole tooth)
   * tp - ratio of tooth in slice of circle (0..1] (1 = teeth are touching at base)
   * tip - ratio of tip of tooth (0..tp] (cant be wider that base of tooth)
   * ns - number of elements in wheel width profile
   * *ip - list of float pairs {start radius, width, ...} (width is ratio to wd)
   *
   */

  /* gear lying on xy plane, z for width. all normals calulated 
     (normalized) */

  float prev;
  int k, t;

  /* estimat # times to divide circle */
  if (nt <= 0)
    circle_subdiv = 64;
  else {
    /* lowest multiple of number of teeth */
    circle_subdiv = nt;
    while (circle_subdiv < 64)
      circle_subdiv += nt;
  }

  /* --- draw wheel face --- */

  /* draw horzontal, vertical faces for each section. if first
     section radius not zero, use wd for 0.. first if ns == 0
     use wd for whole face. last width used to edge.  */

  if (ns <= 0) {
    flat_face(0.0, ir, wd);
  } else {
    /* draw first flat_face, then continue in loop */
    if (ip[0].rad > 0.0) {
      flat_face(0.0, ip[0].rad * ir, wd);
      prev = wd;
      t = 0;
    } else {
      flat_face(0.0, ip[1].rad * ir, ip[0].wid * wd);
      prev = ip[0].wid;
      t = 1;
    }
    for (k = t; k < ns; k++) {
      if (prev < ip[k].wid) {
        draw_inside(prev * wd, ip[k].wid * wd, ip[k].rad * ir);
      } else {
        draw_outside(prev * wd, ip[k].wid * wd, ip[k].rad * ir);
      }
      prev = ip[k].wid;
      /* - draw to edge of wheel, add final face if needed - */
      if (k == ns - 1) {
        flat_face(ip[k].rad * ir, ir, ip[k].wid * wd);

        /* now draw side to match tooth rim */
        if (ip[k].wid < 1.0) {
          draw_inside(ip[k].wid * wd, wd, ir);
        } else {
          draw_outside(ip[k].wid * wd, wd, ir);
        }
      } else {
        flat_face(ip[k].rad * ir, ip[k + 1].rad * ir, ip[k].wid * wd);
      }
    }
  }

  /* --- tooth side faces --- */
  tooth_side(nt, ir, or, tp, tip, wd);

  /* --- tooth hill surface --- */
}
Пример #4
0
// function to test threads
// This function will run concurrently.
void* avatar(void* ptr) {

    // Initial variables
    int sockfd = 0;
    struct sockaddr_in servaddr;
    avatarInfo a = *((avatarInfo *) ptr);
    fprintf(a.pLog, "\n\nTHREAD FOR %i", a.avID);

    ///////////////////////// create socket

    //Create a socket for the client
    //If sockfd<0 there was an error in the creation of the socket
    if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
        perror("Problem in creating the socket");
        exit(2);
    }

    //Creation of the socket
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr= inet_addr(a.ip);
    servaddr.sin_port =  htons(a.MazePort); //convert to big-endian order

    //Connection of the client to the socket
    int connected = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    if (connected <0) {
        perror("Problem in connecting to the server");
        exit(3);
    }
    fprintf(a.pLog, "Avatar %d connected to socket: %i\n", a.avID, connected);


    //////////////////////////// send initial message

    AM_Message *ready = calloc(1, sizeof(AM_Message));
    if (!ready) {
        perror("No memory\n");
        exit(4);
    }

    ready->type = htonl(AM_AVATAR_READY);
    ready->avatar_ready.AvatarId = htonl(a.avID);

    //send ready message to server
    int sent = send(sockfd, ready, sizeof(AM_Message), 0);
    fprintf(a.pLog, "Avatar ready message sent: %i, for av %i\n", sent, a.avID);
    free(ready);
    sleep(1);

    ////////////////////////// initialize a move message and a rec message

    AM_Message *rec_message = calloc(1, sizeof(AM_Message));
    if(!rec_message) {
        perror("\nNo memory");
        exit(4);
    }

    ////////////////////////////////// listen to server

    while (1) {
        memset(rec_message, 0, sizeof(AM_Message));
        //printf("\n thread %i, socket %i", a.avID, sockfd);
        int x = recv(sockfd, rec_message, sizeof(AM_Message), 0);
        if ( x== 0) {
            //error: server terminated prematurely
            //printf("\n server error");
            return NULL;
        }

        ///////////////////////////////////////// if turnID matches avID, make a move
        if(ntohl(rec_message->type) == AM_AVATAR_TURN) {
            pthread_mutex_lock(&turn_lock);
            // if turn id is my id
            int move = -1;
            if(ntohl(rec_message->avatar_turn.TurnId) == a.avID) {
                // write board to the log
                fprintf(a.pLog, "\n\nits my turn: %i", a.avID);
                fprintf(a.pLog, "\nCurrent board:");
                XYPos pos;
                //look through the positions received from the server and add them to the Avatars, if they aren't there, or use them to update the maze based on the last move
                for(int b = 0; b < a.nAvatars; b++) {
                    pos.x = ntohl(rec_message->avatar_turn.Pos[b].x);
                    pos.y = ntohl(rec_message->avatar_turn.Pos[b].y);
                    fprintf(a.pLog, "\nPosition of avatar %i - x: %i y: %i", b,pos.x, pos.y);
                    //printf("\nCurrent position of avatar %i - x: %i y: %i", b,pos.x, pos.y);
                    //printf("\nAvatar %d: pos.x: %i, pos.y: %i, direction: %d, last_move: %d \n", b, Avatars[b].pos.x, Avatars[b].pos.y, Avatars[b].direction, Avatars[b].last_move);
                    if (Avatars[b].last_move == -1) { //if the avatar doesn't have a position yet
                        Avatars[b].pos = pos;
                        Avatars[b].last_move = M_NULL_MOVE;
                    }
                    else if (Avatars[b].last_move != M_NULL_MOVE) {
                        if ((pos.x == Avatars[b].pos.x) && (pos.y == Avatars[b].pos.y)) {
                            fprintf(a.pLog, "Avatar %d encountered a wall and did not move.", b);
                            AddWall(Avatars[b].pos.y, Avatars[b].pos.x, Avatars[b].last_move, 1);
                            Avatars[b].last_move = M_NULL_MOVE;
                        }
                        else {
                            fprintf(a.pLog, "Avatar %d moved successfully.", b);
                            switch(Avatars[b].last_move) {
                            case M_NORTH:
                                if (maze->maze[Avatars[b].pos.y][Avatars[b].pos.x].north_wall != 2) {
                                    AddWall(Avatars[b].pos.y, Avatars[b].pos.x, Avatars[b].last_move, 0);
                                }
                                break;
                            case M_SOUTH:
                                if (maze->maze[Avatars[b].pos.y][Avatars[b].pos.x].south_wall != 2) {
                                    AddWall(Avatars[b].pos.y, Avatars[b].pos.x, Avatars[b].last_move, 0);
                                }
                                break;
                            case M_EAST:
                                if (maze->maze[Avatars[b].pos.y][Avatars[b].pos.x].east_wall != 2) {
                                    AddWall(Avatars[b].pos.y, Avatars[b].pos.x, Avatars[b].last_move, 0);
                                }
                                break;
                            case M_WEST:
                                if (maze->maze[Avatars[b].pos.y][Avatars[b].pos.x].west_wall != 2) {
                                    AddWall(Avatars[b].pos.y, Avatars[b].pos.x, Avatars[b].last_move, 0);
                                }
                                break;
                            default:
                                AddWall(Avatars[b].pos.y, Avatars[b].pos.x, Avatars[b].last_move, 0);
                                break;
                            }
                            Avatars[b].pos = pos;
                            Avatars[b].direction = Avatars[b].last_move;
                            Avatars[b].last_move = M_NULL_MOVE;
                        }
                    }

                }

                ////////////////graphics////////////////
                //initscr();
                clear();
                raw();
                //start_color();
                create_border(maze->num_col, maze->num_row);
                draw_inside(maze);
                //draw_fakes(maze);
                int f;
                for (f = 0; f<a.nAvatars; f++) {
                    draw_avatar(2*Avatars[f].pos.y+1, 2*Avatars[f].pos.x+1);
                }
                //unsigned int microseconds;
                //microseconds = 200;
                //usleep(microseconds);
                refresh();

                /* Determine the direction of the move for the current Avatar */

                /* Avatar 0 has a fixed location - it never moves */
                if (a.avID == 0) {
                    if(!final_destination) {
                        final_destination = (XYPos *) calloc(1, sizeof(XYPos));
                        final_destination->x = Avatars[a.avID].pos.x;
                        final_destination->y = Avatars[a.avID].pos.y;
                    }
                    move = M_NULL_MOVE;
                }
                // if(!final_destination){
                //   for(int i = 0; i < a.nAvatars; i++){
                //     if (i == a.avID) continue;
                //     //if the Avatar is in the same place as another Avatar, save position as final_destination
                //     if((Avatars[i].pos.x == Avatars[a.avID].pos.x) && (Avatars[i].pos.y == Avatars[a.avID].pos.y)){
                //       final_destination = (XYPos *) calloc(1, sizeof(XYPos));
                //       final_destination->x = Avatars[a.avID].pos.x;
                //       final_destination->y = Avatars[a.avID].pos.y;
                //       move = M_NULL_MOVE;
                //       break;
                //     }
                //   }
                // }
                /* Determine the direction of the move for Avatars that aren't Avatar 0 */
                //if Avatar is at final_destination, it should not move
                if((final_destination) && (Avatars[a.avID].pos.x == final_destination->x) && (Avatars[a.avID].pos.y == final_destination ->y)) {
                    move = M_NULL_MOVE;
                }
                else { //if the Avatar is alone, use the rightHandRule to determine the next move
                    move = rightHandRule(Avatars[a.avID]);
                }

                //temporary fix to diagnose the initial -1 rightHandRule return
                if(move == -1) {
                    ClearFakeWalls(Avatars[a.avID].pos.y, Avatars[a.avID].pos.x);
                    move = rightHandRule(Avatars[a.avID]);
                    move = (move == -1) ? M_NULL_MOVE : move;
                }
                Avatars[a.avID].last_move = move;
                //int move = rand() % 4;
                // write move to the log
                fprintf(a.pLog, "\nMove: %i", move);
                //printf("\nMove: %i", move);

                //send a move message for the current avatar
                AM_Message *ready = calloc(1, sizeof(AM_Message));
                if (!ready) {
                    perror("No memory\n");
                    exit(4);
                }
                ready->type = htonl(AM_AVATAR_MOVE);
                ready->avatar_move.AvatarId = htonl(a.avID);
                ready->avatar_move.Direction =htonl(move);

                //send ready message to server
                int sent = send(sockfd, ready, sizeof(AM_Message), 0);
                fprintf(a.pLog, "\nAvatar move message sent: %i, for av %i", sent, a.avID);
                free(ready);
                //sleep(1);
            }
            pthread_mutex_unlock(&turn_lock);
        }

        // else if the message is success, break
        else if(ntohl(rec_message->type) == AM_MAZE_SOLVED) {
            pthread_mutex_lock(&solved_lock);
            if(!thread_return) {
                time_t myTime;
                char buff[100];
                thread_return = (char *) calloc(100, sizeof(char));
                myTime = time(NULL);
                strftime(buff, 100, "%a %d %Y, %H:%M", localtime(&myTime));
                //printf("%s\n", buff);
                sprintf(thread_return, "\nMaze Solved on %s!\n", buff);
                //printf("\nSolved!\n");
                //free(rec_message);
                //free(ptr);
            }

            //stop at solution, wait for an input to end graphics
            //refresh();
            //sleep(1);
            //clear();
            //printw("Maze solved!");
            //getch();
            //clear();
            //endwin();
            //delwin(stdscr);

            if(a.avID == 0) {
                //stop at solution, wait for an input to end graphics
                refresh();
                sleep(1);
                clear();
                printw("Maze solved!");
                getch();
                clear();
                //endwin();
            }
            pthread_mutex_unlock(&solved_lock);
            break;
        }

        else if(ntohl(rec_message->type) == AM_TOO_MANY_MOVES) {
            pthread_mutex_lock(&too_many_moves_lock);
            printf("\nToo many moves! You lose.\n");
            //fprintf(a.pLog,"\nToo many moves! You lose.\n");
            thread_return = "\nToo many moves! You lose.\n";
            //free(rec_message);
            //free(ptr);
            pthread_mutex_unlock(&too_many_moves_lock);
            break;
        }

        else if(IS_AM_ERROR(ntohl(rec_message->type))) {
            pthread_mutex_lock(&error_lock);
            thread_return = (char *) calloc(100, sizeof(char));
            printf("\nReceived Error code\n");
            sprintf(thread_return, "\nReceived Error code: %u\n", ntohl(rec_message->type));
            //free(rec_message);
            //free(ptr);
            pthread_mutex_unlock(&error_lock);
            break;
        }
    }
    //CleanupMaze();
    free(rec_message);
    free(ptr);
    pthread_exit(thread_return);
}