Player *load_player() { Player *p = (Player *)malloc( sizeof(Player) ); DATAFILE *data; BITMAP *t; memset( p, 0, sizeof(Player) ); data = load_datafile( "dat/sprites.dat" ); if (!data) fatal("Error reading datafile 'dat/sprites.dat'\n"); p->still_image = copy_bitmap( (BITMAP *)data[0].dat ); p->moving_images = V_new(); V_register_del_func( p->moving_images, (VectorFunc)destroy_bitmap ); t = copy_bitmap( (BITMAP *)data[0].dat ); V_add( p->moving_images, t ); t = copy_bitmap( (BITMAP *)data[1].dat ); V_add( p->moving_images, t ); t = copy_bitmap( (BITMAP *)data[2].dat ); V_add( p->moving_images, t ); t = copy_bitmap( (BITMAP *)data[1].dat ); V_add( p->moving_images, t ); p->ducking_image = copy_bitmap( (BITMAP *)data[a05_marioduck].dat ); p->jumping_image = copy_bitmap( (BITMAP *)data[a03_mariobat].dat ); p->attack_image = copy_bitmap( (BITMAP *)data[a04_mariobat].dat ); set_pallete( *((PALLETE *)data[Zpallete].dat) ); unload_datafile( data ); p->cur_image = p->still_image; p->width = p->cur_image->w; p->height = p->cur_image->h; p->invul = 0; p->life = MAX_LIFE; p->max_jump = MAX_JUMP; p->inventory = V_new(); p->quantity = IA_new(); p->inv_selection = 0; p->item_in_use = 0; p->cur_item_frame = 0; p->item_animation = V_new(); V_register_del_func( p->item_animation, (VectorFunc)destroy_bitmap ); p->weapon = 1; p->weapon_length = 20; p->weapon_timer = 0; p->weapon_speed = 15; p->weapon_delay = 10; return p; }
void drawRepere(){ Vector origine = V_new(0,0,0); Vector x = V_new(1,0,0); Vector y = V_new(0,1,0); Vector z = V_new(0,0,1); chooseColor(1, 0, 0); drawLine(origine, x); chooseColor(0, 1, 0); drawLine(origine, y); chooseColor(0, 0, 1); drawLine(origine, z); }
int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(width, height); glutInitWindowPosition(50, 50); glutCreateWindow("Transformations matricielles"); glViewport(0, 0, width, height); glClearColor(0,0,0,0); glutDisplayFunc(display); // glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutMouseFunc(mouse); glutIdleFunc(idle); p_light[0]=-10.0; p_light[1]=20.0; p_light[2]=0.0; p_light[3]=1.0; p_aim = V_new(0,0,-2.75); P = P_new(); M = M_new(); glutMainLoop(); return 0; }
void mouse(int button, int state, int x, int y) { float xf = (double)(x-width/2.0)/(width/2.0); float yf = (double)(-y+height/2)/(height/2.0); switch(button){ case GLUT_LEFT_BUTTON : if(state==GLUT_DOWN) { if (!stop) { P_addVertex(P, V_new(xf, yf, 0)); } } break; case GLUT_MIDDLE_BUTTON : if(state==GLUT_DOWN) { fprintf(stderr,"Clic milieu\n"); } break; case GLUT_RIGHT_BUTTON : if(state==GLUT_DOWN) { if (!stop) { P_removeLastVertex(P); } } break; } glutPostRedisplay(); }
void keyboard(unsigned char keycode, int x, int y) { // printf("Touche frapee : %c (code ascii %d)\n",keycode, keycode); if (keycode==27) // ECHAP exit(0); if (keycode=='e') { dim = DIM3; M_perlinExtrude(M, P, 64); stop = 1; } if (keycode=='r') { dim = DIM3; M_revolution(M, P, 128); stop = 1; } //initialisation if (keycode=='i') { //reset things P = P_new(); M = M_new(); p_aim = V_new(0,0,-2.75); theta = 0; phi = 0; stop = 0; dim = DIM2; } if(keycode=='a'){ if(affichage == 0){ M->_is_filled = 1; affichage++; } else if(affichage == 1){ affichage++; } else{ M->_is_filled = 0; affichage = 0; } } //rotations switch(keycode){ case 'z': theta +=10; break; case 's': theta -=10; break; case 'q': phi += 1; break; case 'd': phi -=1; break; } glutPostRedisplay(); }
Vector V_cross(Vector v1, Vector v2) { return V_new( v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x ); }
void V_uxUyFromUz(Vector u_z, Vector *u_x, Vector *u_y) { // If u_z and (0, 1, 0) are colinear... if ((u_z.x == 0) && (u_z.z == 0)) { // *u_x.z = u_z.y in order for the basis to be orthonormal and direct *u_x = V_new(0, 0, u_z.y); // What comes naturally! *u_y = V_new(u_z.y, 0, 0); } else{ double length = V_length(u_z); // Using cross product to get u_x orthogonal to ((0, 1, 0), u_z) *u_x = V_multiply(length, V_unit(V_cross(V_new(0,1,0), u_z))); // (u_x, u_y, u_z) must be direct, therefore u_y = u_z^u_x *u_y = V_multiply(length, V_unit(V_cross(u_z, *u_x))); } }
Vector V_multiply(double lambda, Vector v) { return V_new(lambda*v.x, lambda*v.y, lambda*v.z); }
Vector V_substract(Vector v1, Vector v2) { return V_new(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
Vector V_add(Vector v1, Vector v2) { return V_new(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
Vector V_turnAroundY(Vector p, double r) { return V_new(p.x*cos(r) - p.z*sin(r), p.y, p.x*sin(r) + p.z*cos(r)); }