AMesaBuffer GLAPIENTRY AMesaCreateBuffer(AMesaVisual visual, BITMAP* bmp) { AMesaBuffer buffer; /* Buffer and visual must share the same color depth */ if (bitmap_color_depth(bmp) != visual->Depth) return NULL; buffer = (AMesaBuffer) malloc(sizeof(struct amesa_buffer)); if (!buffer) return NULL; buffer->FrontBuffer = bmp; buffer->BackBuffer = NULL; buffer->Active = NULL; buffer->ReadActive = NULL; buffer->DepthBuffer = NULL; if (visual->DBFlag) { buffer->BackBuffer = create_bitmap_ex(visual->Depth, bmp->w, bmp->h); if (!buffer->BackBuffer) { free(buffer); return NULL; } } if (visual->GLVisual->depthBits > 0) { buffer->DepthBuffer = create_zbuffer(bmp); if (!buffer->DepthBuffer) { if (buffer->BackBuffer) destroy_bitmap(buffer->BackBuffer); free(buffer); return NULL; } set_zbuffer(buffer->DepthBuffer); } buffer->GLBuffer = _mesa_create_framebuffer(visual->GLVisual, 0, /*depth buffer is handled by Allegro */ visual->GLVisual->stencilBits > 0, /*software stencil buffer*/ visual->GLVisual->accumRedBits > 0,/*software accum buffer*/ GL_FALSE ); /*software alpha buffer*/ if (!buffer->GLBuffer) { if (buffer->BackBuffer) destroy_bitmap(buffer->BackBuffer); if (buffer->DepthBuffer) destroy_zbuffer(buffer->DepthBuffer); free(buffer); return NULL; } buffer->Width = bmp->w; buffer->Height = bmp->h; return buffer; }
void TextureUtil::initGrx() { Config * config = Config::getInstance(); #if EM_USE_SDL cerr << "Initing SDL" << endl << endl; if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { cerr << "Couldn't initialize SDL video" << SDL_GetError() << endl; exit(1); } if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { cerr << "Couldn't initialize SDL joystick: " << SDL_GetError() << endl << endl; } else { int njoystick = SDL_NumJoysticks(); cerr << njoystick << " joysticks were found." << endl; if (njoystick != 0) { cerr << "The names of the joysticks are:" << endl; for(int a=0; a<njoystick; a++ ) { cerr << " " << SDL_JoystickName(a) << endl; } cerr << "Using " << SDL_JoystickName(0) << endl << endl; SDL_JoystickOpen(0); SDL_JoystickEventState(SDL_ENABLE); } } // See if we should detect the display depth if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 2 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 3 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 3 ); } else if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 16 ) { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); } else { SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 ); } SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); /* Initialize the display */ SDL_Surface* screen = SDL_SetVideoMode(config->getWidth(), config->getHeight(), config->getBpp(), SDL_OPENGL | (config->useFullScreen() ? SDL_FULLSCREEN : 0)); // if (config->useFullScreen()) { SDL_ShowCursor(SDL_DISABLE); // } SDL_WM_SetCaption("Emilia Pinball", NULL); if (screen == NULL) { cerr << "Couldn't set video mode: " << SDL_GetError() << endl; exit(1); } cerr << "Vendor : " << glGetString( GL_VENDOR ) << endl; cerr << "Renderer : " << glGetString( GL_RENDERER ) << endl; cerr << "Version : " << glGetString( GL_VERSION ) << endl; cerr << "Extensions : " << glGetString( GL_EXTENSIONS ) << endl << endl; //TODO: that would be usefull to report CPU/RAM specs also //!rzr int value; SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value ); cerr << "SDL_GL_RED_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value ); cerr << "SDL_GL_GREEN_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value ); cerr << "SDL_GL_BLUE_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value ); cerr << "SDL_GL_DEPTH_SIZE: " << value << endl; SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value ); cerr << "SDL_GL_DOUBLEBUFFER: " << value << endl << endl; this->resizeView(config->getWidth(), config->getHeight()); #endif // EM_USE_SDL #if EM_USE_ALLEGRO //config->setSize(320, 240); allegro_init(); install_keyboard(); install_timer(); install_mouse(); COLOR_MAP colorMap; RGB_MAP rgbMap; COLOR_MAP transMap; RGB* paPalette = (RGB*) calloc(256, sizeof(RGB)); generate_332_palette(paPalette); // create rgb table create_rgb_table(&rgbMap, paPalette, NULL); rgb_map = &rgbMap; // create light table and setup the truecolor blending functions. create_light_table(&colorMap, paPalette, 0, 0, 0, NULL); color_map = &colorMap; // texture and flat polygons are 50% transparent create_trans_table(&transMap, paPalette, 128, 128, 128, NULL); set_trans_blender(0, 0, 0, 128); // set the graphics mode int tc = GFX_AUTODETECT_WINDOWED, tw = config->getWidth(); int th = config->getHeight(), tbpp = 16; /* set_gfx_mode(GFX_SAFE, 320, 200, 0, 0); set_palette(desktop_palette); if (!gfx_mode_select_ex(&tc, &tw, &th, &tbpp)) { allegro_exit(); cerr << "Error setting safe graphics mode" << endl; } */ set_color_depth(tbpp); if (set_gfx_mode(tc, tw, th, 0, 0) != 0) { allegro_exit(); cerr << "Error setting graphics mode " << endl << allegro_error << endl; } set_palette(paPalette); config->setSize(tw, th); set_projection_viewport(0, 0, tw, th); // Create back buffer. backbuffer = create_bitmap(tw, th); clear(backbuffer); zbuffer = create_zbuffer(backbuffer); set_zbuffer(zbuffer); clear_zbuffer(zbuffer, 0); #endif // EM_USE_ALLEGRO }
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(); }