int main() { int exit = 0; int snum; LINE_LOOP *map = load_map("map.txt", &snum, 0.2); init(); float cam_x = 0.0, cam_y = 2.0, cam_z = 30.0, cam_ax = 0.0, cam_ay = 0.0; printf("%d %d", sizeof(float), sizeof(float *)); while(!exit) { VECTOR cam_dir = vector(SPEED * cos(M_PI * 0.5 - cam_ay * M_PI / 180.0), SPEED * sin(cam_ay * M_PI / 180.0 - M_PI * 0.5)); int mx, my; get_mouse_mickeys(&mx, &my); //position_mouse(240, 320); cam_ay += mx * 0.03; cam_ax -= my * 0.03; if(cam_ax > 60) cam_ax = 60; if(cam_ax < -40) cam_ax = -40; if(keypressed()) { if(key[KEY_ESC]) { exit = 1; } if(key[KEY_W]) { cam_x += cam_dir.x*10.0; cam_z += cam_dir.y*10.0; } if(key[KEY_S]) { cam_x -= cam_dir.x*10.0; cam_z -= cam_dir.y*10.0; } if(key[KEY_A]) { cam_x += cam_dir.y*10.0; cam_z += -cam_dir.x*10.0; } if(key[KEY_D]) { cam_x -= cam_dir.y*10.0; cam_z -= -cam_dir.x*10.0; } if(key[KEY_LEFT]) { cam_ay -= 0.09; } if(key[KEY_RIGHT]) { cam_ay += 0.09; } if(key[KEY_PGDN]) { cam_ax -= 0.09; } if(key[KEY_PGUP]) { cam_ax += 0.09; } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); transform(-cam_x, -cam_y, -cam_z, -cam_ax, cam_ay, 0.0); glBegin(GL_QUADS); glColor3ub(128, 128, 128); glVertex3f(-300.0, 0.0, 300.0); glVertex3f(300.0, 0.0, 300.0); glVertex3f(300.0, 0.0, -300.0); glVertex3f(-300.0, 0.0, -300.0); draw_map_3d(map, snum); glEnd(); allegro_gl_flip(); } free_map(map, snum); readkey(); return 0; }
void AppMain() { Vector3 camFr(0.476459, -0.334238, 0.813186); Vector3 camUp(00.168968, 0.942489, 0.288382); Vector3 camRi = camUp ^ camFr; Vector3 camPos(-6, 10, -13); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50, (float)SCREEN_W / (float)SCREEN_H, // Aspect ratio 0.1, 1000); // Far plane while (!g_keys[KEY_ESC]) { glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); float camPosMag = camPos.Mag(); float moveSpeed = 0.01f * camPosMag; int velx, vely; get_mouse_mickeys(&velx, &vely); if (mouse_b & 2) { camFr.RotateAroundY((float)-velx / 100.0f); camUp.RotateAroundY((float)-velx / 100.0f); camRi = camUp ^ camFr; camFr.RotateAround(camRi, (float)vely / 100.0f); camUp.RotateAround(camRi, (float)vely / 100.0f); moveSpeed *= 3.0f; } if (key[KEY_W]) camPos += camFr * moveSpeed; if (key[KEY_S]) camPos -= camFr * moveSpeed; if (key[KEY_A]) camPos += camRi * moveSpeed; if (key[KEY_D]) camPos -= camRi * moveSpeed; if (key[KEY_Q]) camPos.y -= moveSpeed; if (key[KEY_E]) camPos.y += moveSpeed; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Vector3 lookAtPos = camPos + camFr; gluLookAt(camPos.x, camPos.y, camPos.z, lookAtPos.x, lookAtPos.y, lookAtPos.z, camUp.x, camUp.y, camUp.z); DrawGrid(camPos); // *** Do the maths we actually want to test MainUserLoop(); allegro_gl_flip(); } return 0; }
int main(int argc, char **argv) { BITMAP *bmp = NULL; int x, y, mx, my; int result, mode = GFX_AUTODETECT; allegro_init(); install_keyboard(); if (install_mouse() < 0) { allegro_message("This example requires a mouse to run!"); return -1; } jpgalleg_init(); if (cpu_capabilities & CPU_MMX) popup_menu[2].flags = D_SELECTED; if ((argc > 1) && (!strcmp(argv[1], "-window"))) mode = GFX_AUTODETECT_WINDOWED; set_color_depth(32); if (set_gfx_mode(mode, 640, 480, 0, 0)) { set_color_depth(16); if (set_gfx_mode(mode, 640, 480, 0, 0)) { set_color_depth(15); if (set_gfx_mode(mode, 640, 480, 0, 0)) { allegro_message("Unable to init truecolor 640x480 gfx mode: %s", allegro_error); return -1; } } } clear_to_color(screen, gui_mg_color); show_mouse(screen); alert("JPG image load/save example for " JPGALLEG_VERSION_STRING, NULL, "Press right mouse button for contextual menu, ESC to quit.", "Ok", NULL, 0, 0); while (!key[KEY_ESC]) { x = mouse_x; y = mouse_y; get_mouse_mickeys(&mx, &my); mx /= 2; my /= 2; bmp = settings_dialog[PREVIEW].dp; if (bmp) { old_sx = sx; old_sy = sy; if (bmp->w > SCREEN_W) { if ((x == 0) && (mx < 0) && (sx > 0)) sx = MAX(0, sx + mx); if ((x == SCREEN_W - 1) && (mx > 0) && (sx < bmp->w - SCREEN_W - 1)) sx = MIN(sx + mx, bmp->w - SCREEN_W - 1); if (key[KEY_LEFT] && (sx > 0)) sx = MAX(0, sx - 8); if (key[KEY_RIGHT] && (sx < bmp->w - SCREEN_W - 1)) sx = MIN(sx + 8, bmp->w - SCREEN_W - 1); } if (bmp->h > SCREEN_H) { if ((y == 0) && (my < 0) && (sy > 0)) sy = MAX(0, sy + my); if ((y == SCREEN_H - 1) && (my > 0) && (sy < bmp->h - SCREEN_H - 1)) sy = MIN(sy + my, bmp->h - SCREEN_H - 1); if (key[KEY_UP] && (sy > 0)) sy = MAX(0, sy - 8); if (key[KEY_DOWN] && (sy < bmp->h - SCREEN_H - 1)) sy = MIN(sy + 8, bmp->h - SCREEN_H - 1); } if ((sx != old_sx) || (sy != old_sy)) draw_image(bmp, sx, sy); } if (mouse_b & 0x2) { result = do_menu(popup_menu, x, y); while(key[KEY_ESC]) ; if (result == 3) break; draw_image((BITMAP *)settings_dialog[PREVIEW].dp, sx, sy); } } if (bmp) destroy_bitmap(bmp); return 0; }
void Jugador::Actualizar() { int x, y; get_mouse_mickeys(&x, &y); int dx, dy ; dx = orientacion.GetX() + y ; if ( dx < -85 ) { dx = -85 ; } if ( dx > 85 ) { dx = 85 ; } dy = ((int)(orientacion.GetY() + x))%360 ; orientacion.SetX(dx); orientacion.SetY(dy); Vector mov(0,0,0); if ( movimiento.GetY() > -0.1 ) { if ( key[KEY_UP] ) { mov.SetX(mov.GetX()+ 0.1 * cos((orientacion.GetY()-90)*PIOVER180) ); mov.SetZ(mov.GetZ()+ 0.1 * sin((orientacion.GetY()-90)*PIOVER180) ); } if ( key[KEY_DOWN] ) { mov.SetX(mov.GetX()+ 0.1 * cos((orientacion.GetY()+90)*PIOVER180) ); mov.SetZ(mov.GetZ()+ 0.1 * sin((orientacion.GetY()+90)*PIOVER180) ); } if ( key[KEY_RIGHT] ) { mov.SetX(mov.GetX()+ 0.1 * cos((orientacion.GetY())*PIOVER180) ); mov.SetZ(mov.GetZ()+ 0.1 * sin((orientacion.GetY())*PIOVER180) ); } if ( key[KEY_LEFT] ) { mov.SetX(mov.GetX()+ 0.1 * cos((orientacion.GetY()+180)*PIOVER180) ); mov.SetZ(mov.GetZ()+ 0.1 * sin((orientacion.GetY()+180)*PIOVER180) ); } } movimiento.SetX( mov.GetX() ); movimiento.SetZ( mov.GetZ() ); movimiento.SetY( movimiento.GetY() - 0.005 ); if ( movimiento.GetY() < -0.3 ) { velocidadMuerte = true ; } nivel->DesplazarItem(posicion, movimiento, radio); posicion = posicion + movimiento ; if ( ((movimiento.GetY() > -0.1 ) && velocidadMuerte) || posicion.GetY() < 0.0 ) { nivel->MuereJugador(); } }
int main() { int exit_flag = 0, i, j; MAT16F tmat; int vnum, node_num; VERTEX *map_vertex; STATIC_BSP_NODE *tree; init(); load_static_bsp("map.bsp", &map_vertex, &vnum, &tree, &node_num); printf("Vertex Number: %d.\nNode Number: %d.\n", vnum, node_num); LOCK_VARIABLE(fps); LOCK_VARIABLE(frame_count); LOCK_FUNCTION(update_fps); install_int(update_fps, 1000); float frame = 0.0; VEC3F view_volume[5]; VEC3F cam_pos = vec3f(0.0, 3.0, 0.0), cam_dir, cam_dir_normal, cam_ang = vec3f(0.0, 0.0, 0.0); while(!exit_flag) { int mx, my; get_mouse_mickeys(&mx, &my); position_mouse(SCREEN_W / 2, SCREEN_H / 2); cam_ang.x += my * 0.001; cam_ang.y -= mx * 0.001; cam_dir.x = CAM_SPEED * cos(0.5 * M_PI + cam_ang.y); cam_dir.y = CAM_SPEED * cos(0.5 * M_PI + cam_ang.x); cam_dir.z = CAM_SPEED * sin(0.5 * M_PI + cam_ang.y); cam_dir_normal = vec3f(-cam_dir.z, 0.0, cam_dir.x); if(key[KEY_ESC]) { exit_flag = 1; } if(key[KEY_W]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir); } if(key[KEY_S]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir); } if(key[KEY_A]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir_normal); } if(key[KEY_D]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir_normal); } set_fov(90.0); if(mouse_b > 1) set_fov(30.0); build_view_volume(view_volume); reset_mat16f(tmat); rotate_x_mat16f(tmat, cam_ang.x); rotate_y_mat16f(tmat, cam_ang.y); rotate_z_mat16f(tmat, 0.0); translate_mat16f(tmat, cam_pos.x, cam_pos.y, cam_pos.z); for(i = 0; i < 5; i++) transform_vec3f(&view_volume[i], view_volume[i], tmat); reset_mat16f(tmat); translate_mat16f(tmat, -cam_pos.x, -cam_pos.y, -cam_pos.z); rotate_z_mat16f(tmat, 0.0); rotate_y_mat16f(tmat, -cam_ang.y); rotate_x_mat16f(tmat, -cam_ang.x); for(i = 0; i < vnum; i++) { transform_vec3f(&map_vertex[i].trans, map_vertex[i].object, tmat); project_vertex(&map_vertex[i]); } for(i = 0; i < node_num; i++) tree[i].flag = 0; int checks = 0; mark_nodes_inside_volume(tree, 0, map_vertex, view_volume, 5, &checks); int count = 0; for(i = 0; i < node_num; i++) if(tree[i].flag == 1) count++; clear_to_color(buffer, 0); clear_to_color(BASE_INT_z_buffer, BASE_INT_z_buffer_precision); traverse_tree(tree, 0, cam_pos, map_vertex, -1); textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps); textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps); textprintf_ex(buffer, font, 10, 20, makecol(255, 255, 255), 0, "Rendered: %d of nodes, %d of faces.", (int)((float)checks / (float)node_num * 100.0), (int)((float)count / (float)node_num * 100.0)); //textprintf_ex(buffer, font, 10, 20, makecol(255, 255, 255), 0, "%d", checks); blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); frame_count++; } deinit_engine(); for(i = 0; i < node_num; i++) free(tree[i].poly.vind); free(tree); free(map_vertex); return 0; }
int main(int argc, char *argv[]) { struct sched_param scp; scp.sched_priority = 60; sched_setscheduler(0, SCHED_FIFO, &scp); mlockall(MCL_FUTURE); allegro_init(); set_color_depth(24); set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0); install_keyboard(); install_mouse(); font = load_font("font.fnt", NULL, NULL); BITMAP *wsbuffer = create_bitmap(1024, 768); Box workspace(0, 0, 1024, 768, COLOR_PLAYER, Box::TYPE_FLAT); BarCounter bc(902, 16); workspace.addChild(new Image(0, 0, 32, 768, "left.bmp")); workspace.addChild(new Image(992, 0, 32, 768, "right.bmp")); workspace.addChild(&bc); workspace.addChild(new PeakMeter(907, 50)); BITMAP *splash = load_bitmap("splash.bmp", 0); blit(splash, screen, 0, 0, 0, 0, 1024, 768); destroy_bitmap(splash); sound->init(162, &bc); // FIXME changing tempo. loader = new Loader(); for(int i = 0; i < NUMBER_PLAYERS; i++){ workspace.addChild(new Player(96 * i, COLOR_PLAYER)); sound->update(); } workspace.paint(); show_mouse(screen); bool mbdown = false; bool wsneedsredraw = false; int dragstart_x = 0; int dragstart_y = 0; int mickey_x = 0; int mickey_y = 0; bool taking_screenshot = false; while(!(key[KEY_LSHIFT] && key[KEY_LCONTROL] && key[KEY_ESC])){ sound->update(); bc.paintIfNeeded(); // Screenshot if(key[KEY_F12] && !taking_screenshot){ taking_screenshot = true; save_bitmap("shot.bmp", screen, 0); } else if(taking_screenshot){ taking_screenshot = false; } if(loader->needsDisplay()){ if(!wsneedsredraw){ scare_mouse(); blit(screen, wsbuffer, 0, 0, 0, 0, 1024, 768); unscare_mouse(); loader->paint(); wsneedsredraw = true; } if(key[KEY_ESC]) loader->cancelDisplay(); } else if(wsneedsredraw) { scare_mouse(); blit(wsbuffer, screen, 0, 0, 0, 0, 1024, 768); bc.paint(); workspace.paintIfNeeded(); unscare_mouse(); wsneedsredraw = false; } else { workspace.paintIfNeeded(); } if((mouse_b) && !mbdown) { if(loader->needsDisplay()) { loader->mouseEvent(mouse_x, mouse_y, mouse_b); } else { workspace.mouseEvent(mouse_x, mouse_y, mouse_b); } mbdown = true; dragstart_x = mouse_x; dragstart_y = mouse_y; show_mouse(0); get_mouse_mickeys(&mickey_x, &mickey_y); } else if (!mouse_b && mbdown) { mbdown = false; position_mouse(dragstart_x, dragstart_y); show_mouse(screen); } else if (mbdown) { // Trigger drag events get_mouse_mickeys(&mickey_x, &mickey_y); if(!loader->needsDisplay()){ if(mickey_x != 0 || mickey_y != 0) workspace.dragEvent(dragstart_x, dragstart_y, mickey_x, mickey_y); } } usleep(100); } // sound->system->release(); delete logg; set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); return 0; }
int main() { int exit_flag = 0, i; MAT16F tmat, modelmat; VEC3F pos = vec3f(0.0, 0.0, 0.5), ang = vec3f(0.0, 0.0, 0.0); int vnum, snum; VEC2F *map_vec2f; WALL_SEGMENT *map_segment; VERTEX *map_vertex; MD2_MODEL mdl; VERTEX *model_verts; TRI *model_tris; VERTEX floor[4]; floor[0].local = vec3f(-200.0, 0.0, 200.0); floor[1].local = vec3f(200.0, 0.0, 200.0); floor[2].local = vec3f(200.0, 0.0, -200.0); floor[3].local = vec3f(-200.0, 0.0, -200.0); NODE *head = NULL; init(); if(load_md2("data/babe.md2", &mdl)) { allegro_message("Error: I need the MD2 model, stupid!"); exit(1); } convert_md2_to_base(&model_verts, &model_tris, &mdl, 0.2); load_map("map.txt", &map_vec2f, &map_segment, &vnum, &snum, 1.0); head = (NODE *)malloc(sizeof(NODE)); head->val = val; head->a = map_segment[0].a; head->b = map_segment[0].b; head->n = NORMALIZED_NORMAL_VEC2F(map_vec2f[map_segment[0].a], map_vec2f[map_segment[0].b]); head->parent = NULL; head->left = NULL; head->right = NULL; val++; for(i = 1; i < snum; i++) { add_node(head, &val, map_segment[i].a, map_segment[i].b, &map_vec2f, &vnum); val++; } map_vertex = (VERTEX *)malloc(vnum * 2 * sizeof(VERTEX)); for(i = 0; i < vnum; i++) { map_vertex[i * 2].local = USCALE_VEC3F(vec3f(map_vec2f[i].x, 0.0, map_vec2f[i].y), 0.2); map_vertex[i * 2 + 1].local = USCALE_VEC3F(vec3f(map_vec2f[i].x, WALL_HEIGHT, map_vec2f[i].y), 0.2); map_vec2f[i] = USCALE_VEC2F(map_vec2f[i], 0.2); } LOCK_VARIABLE(fps); LOCK_VARIABLE(frame_count); LOCK_FUNCTION(update_fps); install_int(update_fps, 1000); float frame = 0.0; VEC3F cam_pos = vec3f(0.0, 3.0, 0.0), cam_dir, cam_dir_normal, cam_ang = vec3f(0.0, 0.0, 0.0); while(!exit_flag) { set_texture_mapping_mode(0); int mx, my; get_mouse_mickeys(&mx, &my); position_mouse(SCREEN_W / 2, SCREEN_H / 2); cam_ang.x += my * 0.001; cam_ang.y -= mx * 0.001; cam_dir.x = SPEED * cos(0.5 * M_PI + cam_ang.y); cam_dir.y = 0.0; cam_dir.z = SPEED * sin(0.5 * M_PI + cam_ang.y); cam_dir_normal = vec3f(-cam_dir.z, 0.0, cam_dir.x); if(key[KEY_ESC]) { exit_flag = 1; } if(key[KEY_W]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir); } if(key[KEY_S]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir); } if(key[KEY_A]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir_normal); } if(key[KEY_D]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir_normal); } //if(key[KEY_SPACE]) { set_texture_mapping_mode(1); } reset_mat16f(tmat); translate_mat16f(tmat, -cam_pos.x, -cam_pos.y, -cam_pos.z); rotate_mat16f(tmat, -cam_ang.x, -cam_ang.y, 0.0); convert_md2_frame_to_base(model_verts, &mdl, frame, 0.2); frame += 0.1; reset_mat16f(modelmat); rotate_mat16f(modelmat, M_PI * 1.5, 0.0, 0.0); translate_mat16f(modelmat, 0.0, 5.0, 0.0); translate_mat16f(modelmat, -cam_pos.x, -cam_pos.y, -cam_pos.z); rotate_mat16f(modelmat, -cam_ang.x, -cam_ang.y, 0.0); for(i = 0; i < vnum * 2; i++) transform_vec3f(&map_vertex[i].world, map_vertex[i].local, tmat); for(i = 0; i < 4; i++) transform_vec3f(&floor[i].world, floor[i].local, tmat); for(i = 0; i < mdl.header.num_vertices; i++) transform_vec3f(&model_verts[i].world, model_verts[i].local, modelmat); clear_bitmap(buffer); clear_to_color(zbuffer, ZBUFFER_PRECISION); bind_texture(texture); VEC2F p = vec2f(cam_pos.x, cam_pos.z); VEC2F v = vec2f(cam_dir.x, cam_dir.z); traverse_tree(head, p, v, map_vec2f, map_vertex); bind_texture(skin); for(i = 0; i < mdl.header.num_tris; i++) { update_tri_normal(&model_tris[i], model_verts); model_tris[i].n = NORMALIZED_VEC3F(model_tris[i].n); if(cull_backface(&model_tris[i], model_verts)) render_tri(&model_tris[i], model_verts); } bind_texture(texture2); TRI temp; temp.a = 0; temp.b = 1; temp.c = 2; temp.tex[0] = vec2f(0.0, 0.0); temp.tex[1] = vec2f(20.0, 0.0); temp.tex[2] = vec2f(20.0, 20.0); render_tri(&temp, floor); temp.a = 2; temp.b = 3; temp.c = 0; temp.tex[0] = vec2f(20.0, 20.0); temp.tex[1] = vec2f(20.0, 0.0); temp.tex[2] = vec2f(0.0, 0.0); render_tri(&temp, floor); textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps); blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); frame_count++; } destroy_bitmap(texture2); destroy_tree(head); free_md2(&mdl); free(model_tris); free(model_verts); free(map_vertex); free(map_vec2f); free(map_segment); destroy_bitmap(skin); destroy_bitmap(texture); destroy_bitmap(zbuffer); destroy_bitmap(buffer); deinit_renderer(); return 0; }
int main() { int mickeyx = 0; int mickeyy = 0; BITMAP *custom_cursor; char msg[80]; int c = 0; allegro_init(); install_keyboard(); install_mouse(); install_timer(); set_gfx_mode(GFX_VGA, 320, 200, 0, 0); set_pallete(desktop_pallete); do { /* the mouse position is stored in the variables mouse_x and mouse_y */ sprintf(msg, "mouse_x = %-5d", mouse_x); textout(screen, font, msg, 16, 16, 255); sprintf(msg, "mouse_y = %-5d", mouse_y); textout(screen, font, msg, 16, 32, 255); /* or you can use this function to measure the speed of movement. * Note that we only call it every fourth time round the loop: * there's no need for that other than to slow the numbers down * a bit so that you will have time to read them... */ c++; if ((c & 3) == 0) get_mouse_mickeys(&mickeyx, &mickeyy); sprintf(msg, "mickey_x = %-7d", mickeyx); textout(screen, font, msg, 16, 64, 255); sprintf(msg, "mickey_y = %-7d", mickeyy); textout(screen, font, msg, 16, 80, 255); /* the mouse button state is stored in the variable mouse_b */ if (mouse_b & 1) textout(screen, font, "left button is pressed ", 16, 112, 255); else textout(screen, font, "left button not pressed", 16, 112, 255); if (mouse_b & 2) textout(screen, font, "right button is pressed ", 16, 128, 255); else textout(screen, font, "right button not pressed", 16, 128, 255); if (mouse_b & 4) textout(screen, font, "middle button is pressed ", 16, 144, 255); else textout(screen, font, "middle button not pressed", 16, 144, 255); vsync(); } while (!keypressed()); clear_keybuf(); /* To display a mouse pointer, call show_mouse(). There are several * things you should be aware of before you do this, though. For one, * it won't work unless you call install_timer() first. For another, * you must never draw anything onto the screen while the mouse * pointer is visible. So before you draw anything, be sure to turn * the mouse off with show_mouse(NULL), and turn it back on again when * you are done. */ clear(screen); textout_centre(screen, font, "Press a key to change cursor", SCREEN_W/2, SCREEN_H/2, 255); show_mouse(screen); readkey(); show_mouse(NULL); /* create a custom mouse cursor bitmap... */ custom_cursor = create_bitmap(32, 32); clear(custom_cursor); for (c=0; c<8; c++) circle(custom_cursor, 16, 16, c*2, c); /* select the custom cursor and set the focus point to the middle of it */ set_mouse_sprite(custom_cursor); set_mouse_sprite_focus(16, 16); clear(screen); textout_centre(screen, font, "Press a key to quit", SCREEN_W/2, SCREEN_H/2, 255); show_mouse(screen); readkey(); show_mouse(NULL); destroy_bitmap(custom_cursor); return 0; }
int main() { int exit = 0; int snum; LINE_LOOP *map = load_map("map.txt", &snum, 0.2); init(); glEnable(GL_LIGHTING); glShadeModel(GL_FLAT); float global_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); glEnable(GL_LIGHT0); // Create light components float ambientLight[] = { 0.7f, 0.7f, 0.7f, 1.0f }; float diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f }; float specularLight[] = { 0.0f, 0.0f, 0.0f, 1.0f }; float position[] = { -1.5f, 30.0f, 0.0f, 1.0f }; // Assign created components to GL_LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); //glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); glLightfv(GL_LIGHT0, GL_POSITION, position); float cam_x = 0.0, cam_y = 2.0, cam_z = 30.0, cam_ax = 0.0, cam_ay = 0.0; while(!exit) { VECTOR cam_dir = vector(SPEED * cos(M_PI * 0.5 - cam_ay * M_PI / 180.0), SPEED * sin(cam_ay * M_PI / 180.0 - M_PI * 0.5)); int mx, my; get_mouse_mickeys(&mx, &my); //position_mouse(240, 320); cam_ay += mx * 0.03; cam_ax -= my * 0.03; if(cam_ax > 60) cam_ax = 60; if(cam_ax < -40) cam_ax = -40; if(keypressed()) { if(key[KEY_ESC]) { exit = 1; } if(key[KEY_W]) { cam_x += cam_dir.x; cam_z += cam_dir.y; } if(key[KEY_S]) { cam_x -= cam_dir.x; cam_z -= cam_dir.y; } if(key[KEY_A]) { cam_x += cam_dir.y; cam_z += -cam_dir.x; } if(key[KEY_D]) { cam_x -= cam_dir.y; cam_z -= -cam_dir.x; } if(key[KEY_LEFT]) { cam_ay -= 0.09; } if(key[KEY_RIGHT]) { cam_ay += 0.09; } if(key[KEY_PGDN]) { cam_ax -= 0.09; } if(key[KEY_PGUP]) { cam_ax += 0.09; } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLightfv(GL_LIGHT0, GL_POSITION, position); transform(-cam_x, -cam_y, -cam_z, -cam_ax, cam_ay, 0.0); glBegin(GL_QUADS); glNormal3f(0.0, 1.0, 0.0); glColor3ub(255, 0, 0); glVertex3f(-300.0, 0.0, 300.0); glColor3ub(0, 255, 0); glVertex3f(300.0, 0.0, 300.0); glColor3ub(0, 0, 255); glVertex3f(300.0, 0.0, -300.0); glColor3ub(255, 255, 0); glVertex3f(-300.0, 0.0, -300.0); draw_map_3d(map, snum); glEnd(); allegro_gl_flip(); } free_map(map, snum); readkey(); return 0; }
static int AM_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp) { static int mz; static int hidingmouse; int mickeyx = 0; int mickeyy = 0; int mickeyz = 0; int buttons = 0; poll_mouse(); if (ALLEGRO_SUB_VERSION >2){ //mouse_on_screen() not available on version 4.2 -> starting with 4.3 if (mouse_on_screen()){ //mouse inside microwindows window if (hidingmouse == 1){ //already shown? GdShowCursor(&scrdev); hidingmouse == 0; } }else{ if (hidingmouse == 0){ //already hidden? GdHideCursor(&scrdev); hidingmouse=1; } } } /* microwindows reads the motion */ get_mouse_mickeys(&mickeyx,&mickeyy); #if 0 *dx=mickeyx; *dy=mickeyy; #else /* return position */ *dx=mouse_x; *dy=mouse_y; #endif //calculate wheel button (up/down) if(mouse_z != mz) mickeyz = mouse_z - mz; else mickeyz = 0; mz = mouse_z; *dz = 0; *bp = 0; buttons = 0; //the buttons seem to be returned different than documented in allegro if (mouse_b & 1) buttons |= MWBUTTON_L; if (mouse_b & 2) buttons |= MWBUTTON_R; if (mouse_b & 4) buttons |= MWBUTTON_M; if (mickeyz > 0) buttons |= MWBUTTON_SCROLLUP; if (mickeyz < 0) buttons |= MWBUTTON_SCROLLDN; *bp = buttons; /* return absolute mouse position */ return MOUSE_ABSPOS; }
void game_process() { static float lastdir, dir, bladir[2] = {0, 0}; int xm, ym; int shoot = 0; get_mouse_mickeys(&xm ,&ym); if (!go) { if (AI[turn] == 0) { float turnlen; turndx[turn] += xm / 100.0; turndy[turn] += ym / 100.0; if (turndx[turn] == 0 && turndy[turn] == 0) turndx[turn] = 1; turnlen = sqrt(turndx[turn] * turndx[turn] + turndy[turn] * turndy[turn]); turndx[turn] = turndx[turn] / turnlen; turndy[turn] = turndy[turn] / turnlen; if (mouse_b & 1) shoot = 1; } else { if (!think[turn]) { int min; lastdir = bladir[turn]; dir = 0; if (!ignore[turn]) { switch (memorized[turn]) { case 0: scan[turn] = pi * 2.0 / 3.0; dir = mindir[turn][0] = pi * 2 * rnd(); memorized[turn] = 1; break; case 1: memdist[turn][0] = mindist[turn]; dir = mindir[turn][1] = mindir[turn][0] + scan[turn]; memorized[turn] = 2; break; case 2: memdist[turn][1] = mindist[turn]; dir = mindir[turn][2] = mindir[turn][0] - scan[turn]; memorized[turn] = 3; break; case 3: memdist[turn][2] = mindist[turn]; if (memdist[turn][0] < memdist[turn][1]) { if (memdist[turn][0] < memdist[turn][2]) { min = 0; } else { min = 2; } } else { if (memdist[turn][1] < memdist[turn][2]) { min = 1; } else { min = 2; } } memdist[turn][0] = memdist[turn][min]; mindir[turn][0] = mindir[turn][min]; scan[turn] *= 0.5; dir = mindir[turn][1] = mindir[turn][0] + scan[turn]; memorized[turn] = 2; if (scan[turn] < 0.0003) { scan[turn] = pi * 2.0 / 3.0; dir = mindir[turn][0] = pi * 2 * rnd(); memorized[turn] = 1; } break; } bladir[turn] = dir; } else { dir = bladir[turn]; ignore[turn] = 0; } if (power[turn]) { int r = (rand() >> 8) & 3; dir = r * pi / 2 + pi / 4; ignore[turn] = 1; } else { int r = (rand() >> 8) & 3; if (r == 0) { int b; float minbon = 1000 * 1000; for (b = 0; b < bonusnum; b++) { if (bonustype[b] == 0 || (bonustype[b] == 1 && life[turn] < 100)) { float dx, dy; float dis; dx = bonusx[b] - basex[turn]; dy = bonusy[b] - basey[turn]; if (dx < -MW / 2) dx += MW; if (dy < -MH / 2) dy += MH; if (dx > MW / 2) dx -= MW; if (dx > MH / 2) dy -= MH; dis = dx * dx + dy * dy; if (dis < minbon) { minbon = dis; dir = atan2(dy, dx); ignore[turn] = 1; } } } } } mindist[turn] = 10000; think[turn] = FPS + FPS * rnd(); } else {
int main() { int exit = 0, i, vnum, pnum; VEC3F pos = vec3f(0.0, 0.0, 0.3), ang = ZERO_VEC3F; MAT16F tmat; VERTEX *vertex; POLY3D *poly; init(); load_map("quake_map2.txt", &vertex, &poly, &vnum, &pnum, 0.2); LOCK_VARIABLE(fps); LOCK_VARIABLE(frame_count); LOCK_FUNCTION(update_fps); install_int(update_fps, 1000); VEC3F cam_pos = vec3f(0.0, 3.0, 0.0), cam_dir, cam_dir_normal, cam_ang = vec3f(0.0, 0.0, 0.0); while(!exit) { int mx, my; get_mouse_mickeys(&mx, &my); position_mouse(SCREEN_W / 2, SCREEN_H / 2); cam_ang.x += my * 0.001; cam_ang.y -= mx * 0.001; cam_dir.x = CAM_SPEED * cos(0.5 * M_PI + cam_ang.y); cam_dir.y = CAM_SPEED * cos(0.5 * M_PI + cam_ang.x); cam_dir.z = CAM_SPEED * sin(0.5 * M_PI + cam_ang.y); cam_dir_normal = vec3f(-cam_dir.z, 0.0, cam_dir.x); if(key[KEY_ESC]) { exit = 1; } if(key[KEY_W]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir); } if(key[KEY_S]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir); } if(key[KEY_A]) { cam_pos = VEC3F_SUM(cam_pos, cam_dir_normal); } if(key[KEY_D]) { cam_pos = VEC3F_DIFF(cam_pos, cam_dir_normal); } set_fov(90.0); if(mouse_b > 1) set_fov(30.0); reset_mat16f(tmat); translate_mat16f(tmat, -cam_pos.x, -cam_pos.y, -cam_pos.z); rotate_z_mat16f(tmat, 0.0); rotate_y_mat16f(tmat, -cam_ang.y); rotate_x_mat16f(tmat, -cam_ang.x); for(i = 0; i < vnum; i++) { transform_vec3f(&vertex[i].trans, vertex[i].object, tmat); project_vertex(&vertex[i]); } clear_to_color(buffer, 0); clear_to_color(BASE_INT_z_buffer, BASE_INT_z_buffer_precision); for(i = 0; i < pnum; i++) { update_poly3d_normal(&poly[i], vertex); V0 = vertex[poly[i].vind[0]].trans; global_n = USCALE_VEC3F(NORMALIZED_VEC3F(poly[i].normal), -1.0); VEC3F N = global_n; DIST = -VEC3F_DOT_PRODUCT(V0, N); get_axes(vertex[poly[i].vind[0]].trans, vertex[poly[i].vind[1]].trans, vertex[poly[i].vind[2]].trans, to_vec3f(poly[i].texcoord[0]), to_vec3f(poly[i].texcoord[1]), to_vec3f(poly[i].texcoord[2]), &A, &B, &C); T0 = to_vec3f(poly[i].texcoord[0]); if(cull_backface(&poly[i], vertex)) render_poly3d(&poly[i], vertex); } textprintf_ex(buffer, font, 10, 10, makecol(255, 255, 255), 0, "FPS: %d", fps); blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); frame_count++; } for(i = 0; i < pnum; i++) destroy_poly3d(&poly[i]); free(vertex); deinit_engine(); return 0; }
void view3d(MAPGEN &mapgen, float hflat, float lflat){ lc=0; MAP map(mapgen,hflat,lflat); BITMAP *buffer=create_bitmap(480,320); ZBUFFER *zbuf=create_zbuffer(buffer); set_zbuffer(zbuf); MATRIX_f roller,camera; // create_scene(4*(mapgen.size()*mapgen.size()), (mapgen.size()*mapgen.size())); set_projection_viewport(0, 0, buffer->w, buffer->h); float xfront, yfront, zfront; float xup, yup, zup; float heading=0,pitch=0,roll=0; int fov=32; float x=128,y=128,z=128; float xs=0,ys=0,zs=0; int fps=0,frames[101]; for(int i=0;i<101;i++) frames[i]=0; int mx,my,tc; //To get rid of that annoying roll I reset mickeys. get_mouse_mickeys(&mx,&my); while(!(mouse_b || key[KEY_ESC])){ tc=lc; lc=0; while(!mouse_b && tc){ tc--; fps-=frames[100]; for(int i=100;i>0;i--) frames[i]=frames[i-1]; frames[0]=0; fps+=frames[1]; /* if(key[KEY_Q]) y++; if(key[KEY_Z]) y--; if(key[KEY_W]){ x+=sin(heading); z+=cos(heading); } if(key[KEY_S]){ x-=sin(heading); z-=cos(heading); } if(key[KEY_A]){ x+=sin(heading+M_PI/2); z+=cos(heading+M_PI/2); } if(key[KEY_D]){ x-=sin(heading+M_PI/2); z-=cos(heading+M_PI/2); } */ if(key[KEY_Q]) ys++; if(key[KEY_Z]) ys--; if(key[KEY_W]){ xs+=sin(heading); zs+=cos(heading); } if(key[KEY_S]){ xs-=sin(heading); zs-=cos(heading); } if(key[KEY_A]){ xs+=sin(heading+M_PI/2); zs+=cos(heading+M_PI/2); roll+=.02; } if(key[KEY_D]){ xs-=sin(heading+M_PI/2); zs-=cos(heading+M_PI/2); roll-=.02; } xs*=.99; ys*=.9; zs*=.99; x+=xs; y+=ys; z+=zs; get_mouse_mickeys(&mx,&my); position_mouse(SCREEN_W/2,SCREEN_H/2); heading-=mx/100.; pitch-=my/100.; if(pitch>M_PI/3) pitch=M_PI/3; if(pitch<-M_PI/3) pitch=-M_PI/3; roll-=mx/100.; roll*=.95; /* if(x>=0 && z>=0 && x/tilesize<mapgen.size() && z/tilesize<mapgen.size()) y=200+mapgen.heightmap()[int(x/tilesize)][int(z/tilesize)]*tilesize; */ } /* calculate the in-front vector */ xfront = sin(heading) * cos(pitch); yfront = sin(pitch); zfront = cos(heading) * cos(pitch); /* rotate the up vector around the in-front vector by the roll angle */ get_vector_rotation_matrix_f(&roller, xfront, yfront, zfront, roll*128.0/M_PI); apply_matrix_f(&roller, 0, 1, 0, &xup, &yup, &zup); /* build the camera matrix */ get_camera_matrix_f(&camera, x, y, z, xfront, yfront, zfront, xup, yup, zup, fov, 1); polys_drawn=0; clear_bitmap(buffer); // clear_scene(buffer); clear_zbuffer(zbuf,0); map.render(buffer,&camera,int(x),int(y)); // render_scene(); textprintf_ex(buffer,font,8,8,-1,-1,"FPS: %i",fps); textprintf_ex(buffer,font,8,16,-1,-1,"Polygon count: %i",polys_drawn); blit(buffer, screen, 0, 0, 280, 8, buffer->w, buffer->h); frames[0]++; } destroy_bitmap(buffer); destroy_zbuffer(zbuf); // destroy_scene(); while(mouse_b || key[KEY_ESC]); clear_keybuf(); }
void mouse_poll() { int dx, dy; poll_mouse(); if (curtube == 3) { if (uservia.ifr & 0x18) return; #ifdef WIN32 get_mouse_mickeys(&dx,&dy); #else dx = (mouse_x - mouse_ox); dy = (mouse_y - mouse_oy); mouse_ox = mouse_x; mouse_oy = mouse_y; #endif mx += dx; my += dy; if (mx) { if (mx > 0) mouse_portb |= 8; else mouse_portb &= ~8; if (mouse_xff) mouse_portb ^= 8; if (mx > 0) mx--; else mx++; uservia_set_cb1(mouse_xff); mouse_xff = !mouse_xff; } if (mouse_y != my) { if (my > 0) mouse_portb &= ~0x10; else mouse_portb |= 0x10; if (mouse_yff) mouse_portb ^= 0x10; if (my > 0) my--; else my++; uservia_set_cb2(mouse_yff); mouse_yff = !mouse_yff; } if (mouse_b & 1) mouse_portb &= ~1; else mouse_portb |= 1; if (mouse_b & 2) mouse_portb &= ~4; else mouse_portb |= 4; mouse_portb |= 2; } else if (mouse_amx) { mouse_ff = !mouse_ff; if (mouse_ff) { uservia_set_cb1(0); uservia_set_cb2(0); return; } #ifdef WIN32 get_mouse_mickeys(&dx,&dy); #else dx = (mouse_x - mouse_ox); dy = (mouse_y - mouse_oy); mouse_ox = mouse_x; mouse_oy = mouse_y; // printf("%i,%i - %i, %i\n",mouse_x,mouse_y,dx,dy); #endif mx += dx; my += dy; /*AMX mouse*/ if (mx) { uservia_set_cb1(1); if (mx > 0) mouse_portb |= 1; else mouse_portb &= ~1; if (mx > 0) mx--; else mx++; } else uservia_set_cb1(0); if (my) { uservia_set_cb2(1); if (my < 0) mouse_portb |= 4; else mouse_portb &= ~4; if (my > 0) my--; else my++; } else uservia_set_cb2(0); if (mouse_b & 1) mouse_portb &= ~0x20; else mouse_portb |= 0x20; if (mouse_b & 2) mouse_portb &= ~0x80; else mouse_portb |= 0x80; if (mouse_b & 4) mouse_portb &= ~0x40; else mouse_portb |= 0x40; } if (mousecapture) position_mouse(64, 64); }