int surface_create(int width, int height) { if (GLEW_ARB_framebuffer_object) { GLuint fbo; unsigned int id, w=(int)width, h=(int)height; //get the integer width and height, and prepare to search for an id if (enigma::surface_max==0) { enigma::surface_array=new enigma::surface*[1]; enigma::surface_max=1; } for (id=0; enigma::surface_array[id]!=NULL; id++) { if (id+1>=enigma::surface_max) { enigma::surface **oldarray=enigma::surface_array; enigma::surface_array=new enigma::surface*[enigma::surface_max+1]; for (unsigned int i=0; i<enigma::surface_max; i++) enigma::surface_array[i]=oldarray[i]; enigma::surface_array[enigma::surface_max]=NULL; enigma::surface_max++; delete[] oldarray; } } enigma::surface_array[id] = new enigma::surface; enigma::surface_array[id]->width = w; enigma::surface_array[id]->height = h; glGenFramebuffers(1, &fbo); int texture = enigma::graphics_create_texture(w,h,w,h,0,false); glPushAttrib(GL_TEXTURE_BIT); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureStructs[texture]->gltex, 0); glDrawBuffer(GL_COLOR_ATTACHMENT0); glReadBuffer(GL_COLOR_ATTACHMENT0); glClearColor(1,1,1,0); glClear(GL_COLOR_BUFFER_BIT); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, enigma::bound_framebuffer); enigma::surface_array[id]->tex = texture; enigma::surface_array[id]->fbo = fbo; texture_reset(); glPopAttrib(); return id; } return -1; }
void surface_reset_target(void) { texture_reset(); enigma::bound_framebuffer = 0; glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glPopAttrib(); glPopMatrix(); }
void surface_set_target(int id) { get_surface(surf,id); texture_reset(); //This fixes several consecutive surface_set_target() calls without surface_reset_target. if (enigma::bound_framebuffer != 0) glPopAttrib(); glPopMatrix(); enigma::bound_framebuffer = surf->fbo; glBindFramebuffer(GL_DRAW_FRAMEBUFFER, surf->fbo); //bind it glPushMatrix(); //So you can pop it in the reset glPushAttrib(GL_VIEWPORT_BIT); //same screen_set_viewport(0, 0, surf->width, surf->height); d3d_set_projection_ortho(0, 0, surf->width, surf->height, 0); }
void screen_init() { enigma::gui_width = window_get_region_width_scaled(); enigma::gui_height = window_get_region_height_scaled(); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (!view_enabled) { glClearColor(0,0,0,0); screen_set_viewport(0, 0, window_get_region_width_scaled(), window_get_region_height_scaled()); d3d_set_projection_ortho(0, 0, room_width, room_height, 0); } else { for (view_current = 0; view_current < 7; view_current++) { if (view_visible[(int)view_current]) { int vc = (int)view_current; glClearColor(0,0,0,0); screen_set_viewport(view_xport[vc], view_yport[vc], view_wport[vc], view_hport[vc]); d3d_set_projection_ortho(view_xview[vc], view_yview[vc], view_wview[vc], view_hview[vc], 0); break; } } } glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glEnable(GL_ALPHA_TEST); glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glAlphaFunc(GL_ALWAYS,0); texture_reset(); draw_set_color(c_white); }
void screen_redraw() { // Should implement extended lost device checking //if (d3ddev == NULL ) return; // Clean up any textures that ENIGMA may still think are binded but actually are not texture_reset(); d3ddev->BeginScene(); // begins the 3D scene dsprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_DO_NOT_ADDREF_TEXTURE); if (!view_enabled) { D3DVIEWPORT9 pViewport = { 0, 0, (DWORD)window_get_region_width_scaled(), (DWORD)window_get_region_height_scaled(), 0, 1.0f }; d3ddev->SetViewport(&pViewport); D3DXMATRIX matTrans, matScale; // Calculate a translation matrix D3DXMatrixTranslation(&matTrans, -0.5, -room_height - 0.5, 0); D3DXMatrixScaling(&matScale, 1, -1, 1); // Calculate our world matrix by multiplying the above (in the correct order) D3DXMATRIX matWorld = matTrans * matScale; // Set the matrix to be applied to anything we render from now on d3ddev->SetTransform( D3DTS_VIEW, &matWorld); D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixOrthoOffCenterLH(&matProjection, 0, (FLOAT)room_width, 0, (FLOAT)room_height, 0.0f, // the near view-plane 1.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection transform if (background_showcolor) { int clearcolor = ((int)background_color) & 0x00FFFFFF; // clear the window to the background color d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(__GETR(clearcolor), __GETG(clearcolor), __GETB(clearcolor)), 1.0f, 0); // clear the depth buffer } // Clear the depth buffer if 3d mode is on at the beginning of the draw step. if (enigma::d3dMode) d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); draw_back(); // Apply and clear stored depth changes. for (map<int,pair<double,double> >::iterator it = id_to_currentnextdepth.begin(); it != id_to_currentnextdepth.end(); it++) { enigma::object_graphics* inst_depth = (enigma::object_graphics*)enigma::fetch_instance_by_id((*it).first); if (inst_depth != NULL) { drawing_depths[(*it).second.first].draw_events->unlink(inst_depth->depth.myiter); inst_iter* mynewiter = drawing_depths[(*it).second.second].draw_events->add_inst(inst_depth->depth.myiter->inst); if (instance_event_iterator == inst_depth->depth.myiter) { instance_event_iterator = inst_depth->depth.myiter->prev; } inst_depth->depth.myiter = mynewiter; } } id_to_currentnextdepth.clear(); if (enigma::particles_impl != NULL) { const double high = numeric_limits<double>::max(); const double low = drawing_depths.rbegin() != drawing_depths.rend() ? drawing_depths.rbegin()->first : -numeric_limits<double>::max(); (enigma::particles_impl->draw_particlesystems)(high, low); } bool stop_loop = false; for (enigma::diter dit = drawing_depths.rbegin(); dit != drawing_depths.rend(); dit++) { if (dit->second.tiles.size()) //glCallList(drawing_depths[dit->second.tiles[0].depth].tilelist); texture_reset(); enigma::inst_iter* push_it = enigma::instance_event_iterator; //loop instances for (enigma::instance_event_iterator = dit->second.draw_events->next; enigma::instance_event_iterator != NULL; enigma::instance_event_iterator = enigma::instance_event_iterator->next) { enigma::instance_event_iterator->inst->myevent_draw(); if (enigma::room_switching_id != -1) { stop_loop = true; break; } } enigma::instance_event_iterator = push_it; if (stop_loop) break; //particles if (enigma::particles_impl != NULL) { const double high = dit->first; dit++; const double low = dit != drawing_depths.rend() ? dit->first : -numeric_limits<double>::max(); dit--; (enigma::particles_impl->draw_particlesystems)(high, low); } } //return; } else { bool stop_loop = false; for (view_current = 0; view_current < 7; view_current++) { if (view_visible[(int)view_current]) { int vc = (int)view_current; int vob = (int)view_object[vc]; if (vob != -1) { object_basic *instanceexists = fetch_instance_by_int(vob); if (instanceexists) { object_planar* vobr = (object_planar*)instanceexists; double vobx = vobr->x, voby = vobr->y; //int bbl=*vobr.x+*vobr.bbox_left,bbr=*vobr.x+*vobr.bbox_right,bbt=*vobr.y+*vobr.bbox_top,bbb=*vobr.y+*vobr.bbox_bottom; //if (bbl<view_xview[vc]+view_hbor[vc]) view_xview[vc]=bbl-view_hbor[vc]; double vbc_h, vbc_v; (view_hborder[vc] > view_wview[vc]/2) ? vbc_h = view_wview[vc]/2 : vbc_h = view_hborder[vc]; (view_vborder[vc] > view_hview[vc]/2) ? vbc_v = view_hview[vc]/2 : vbc_v = view_vborder[vc]; if (view_hspeed[vc] == -1) { if (vobx < view_xview[vc] + vbc_h) view_xview[vc] = vobx - vbc_h; else if (vobx > view_xview[vc] + view_wview[vc] - vbc_h) view_xview[vc] = vobx + vbc_h - view_wview[vc]; } else { if (vobx < view_xview[vc] + vbc_h) { view_xview[vc] -= view_hspeed[vc]; if (view_xview[vc] < vobx - vbc_h) view_xview[vc] = vobx - vbc_h; } else if (vobx > view_xview[vc] + view_wview[vc] - vbc_h) { view_xview[vc] += view_hspeed[vc]; if (view_xview[vc] > vobx + vbc_h - view_wview[vc]) view_xview[vc] = vobx + vbc_h - view_wview[vc]; } } if (view_vspeed[vc] == -1) { if (voby < view_yview[vc] + vbc_v) view_yview[vc] = voby - vbc_v; else if (voby > view_yview[vc] + view_hview[vc] - vbc_v) view_yview[vc] = voby + vbc_v - view_hview[vc]; } else { if (voby < view_yview[vc] + vbc_v) { view_yview[vc] -= view_vspeed[vc]; if (view_yview[vc] < voby - vbc_v) view_yview[vc] = voby - vbc_v; } if (voby > view_yview[vc] + view_hview[vc] - vbc_v) { view_yview[vc] += view_vspeed[vc]; if (view_yview[vc] > voby + vbc_v - view_hview[vc]) view_yview[vc] = voby + vbc_v - view_hview[vc]; } } if (view_xview[vc] < 0) view_xview[vc] = 0; else if (view_xview[vc] > room_width - view_wview[vc]) view_xview[vc] = room_width - view_wview[vc]; if (view_yview[vc] < 0) view_yview[vc] = 0; else if (view_yview[vc] > room_height - view_hview[vc]) view_yview[vc] = room_height - view_hview[vc]; } } D3DVIEWPORT9 pViewport = { (DWORD)view_xport[vc], (DWORD)view_yport[vc], (DWORD)(window_get_region_width_scaled() - view_xport[vc]), (DWORD)(window_get_region_height_scaled() - view_yport[vc]), 0, 1.0f }; d3ddev->SetViewport(&pViewport); D3DXMATRIX matTrans, matScale; // Calculate a translation matrix D3DXMatrixTranslation(&matTrans, -view_xview[vc] - 0.5, -view_yview[vc] - room_height - 0.5, 0); D3DXMatrixScaling(&matScale, 1, -1, 1); // Calculate our world matrix by multiplying the above (in the correct order) D3DXMATRIX matWorld = matTrans * matScale; // Set the matrix to be applied to anything we render from now on d3ddev->SetTransform( D3DTS_VIEW, &matWorld); D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixOrthoOffCenterLH(&matProjection, 0, (FLOAT)view_wview[vc], 0, (FLOAT)view_hview[vc], 0.0f, // the near view-plane 1.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection transform if (background_showcolor && view_current == 0) { int clearcolor = ((int)background_color) & 0x00FFFFFF; // clear the window to the background color d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(__GETR(clearcolor), __GETG(clearcolor), __GETB(clearcolor)), 1.0f, 0); } // Clear the depth buffer if 3d mode is on at the beginning of the draw step. if (enigma::d3dMode) d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); if (view_current == 0) { draw_back(); } // Apply and clear stored depth changes. for (map<int,pair<double,double> >::iterator it = id_to_currentnextdepth.begin(); it != id_to_currentnextdepth.end(); it++) { enigma::object_graphics* inst_depth = (enigma::object_graphics*)enigma::fetch_instance_by_id((*it).first); if (inst_depth != NULL) { drawing_depths[(*it).second.first].draw_events->unlink(inst_depth->depth.myiter); inst_iter* mynewiter = drawing_depths[(*it).second.second].draw_events->add_inst(inst_depth->depth.myiter->inst); if (instance_event_iterator == inst_depth->depth.myiter) { instance_event_iterator = inst_depth->depth.myiter->prev; } inst_depth->depth.myiter = mynewiter; } } id_to_currentnextdepth.clear(); if (enigma::particles_impl != NULL) { const double high = numeric_limits<double>::max(); const double low = drawing_depths.rbegin() != drawing_depths.rend() ? drawing_depths.rbegin()->first : -numeric_limits<double>::max(); (enigma::particles_impl->draw_particlesystems)(high, low); } for (enigma::diter dit = drawing_depths.rbegin(); dit != drawing_depths.rend(); dit++) { if (dit->second.tiles.size()) //glCallList(drawing_depths[dit->second.tiles[0].depth].tilelist); texture_reset(); enigma::inst_iter* push_it = enigma::instance_event_iterator; //loop instances for (enigma::instance_event_iterator = dit->second.draw_events->next; enigma::instance_event_iterator != NULL; enigma::instance_event_iterator = enigma::instance_event_iterator->next) { enigma::instance_event_iterator->inst->myevent_draw(); if (enigma::room_switching_id != -1) { stop_loop = true; break; } } enigma::instance_event_iterator = push_it; if (stop_loop) break; //particles if (enigma::particles_impl != NULL) { const double high = dit->first; dit++; const double low = dit != drawing_depths.rend() ? dit->first : -numeric_limits<double>::max(); dit--; (enigma::particles_impl->draw_particlesystems)(high, low); } } if (stop_loop) break; } } view_current = 0; } int culling = d3d_get_culling(); d3d_set_culling(rs_none); // Now process the sub event of draw called draw gui // It is for drawing GUI elements without view scaling and transformation if (enigma::gui_used) { // Now process the sub event of draw called draw gui // It is for drawing GUI elements without view scaling and transformation D3DVIEWPORT9 pViewport = { 0, 0, window_get_region_width_scaled(), window_get_region_height_scaled(), 0, 1.0f }; d3ddev->SetViewport(&pViewport); D3DXMATRIX matTrans, matScale; // Calculate a translation matrix D3DXMatrixTranslation(&matTrans, -0.5, -room_height - 0.5, 0); D3DXMatrixScaling(&matScale, 1, -1, 1); // Calculate our world matrix by multiplying the above (in the correct order) D3DXMATRIX matWorld = matTrans * matScale; // Set the matrix to be applied to anything we render from now on d3ddev->SetTransform( D3DTS_VIEW, &matWorld); D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixOrthoOffCenterLH(&matProjection, 0, (FLOAT)enigma::gui_width, 0, (FLOAT)enigma::gui_height, 0.0f, // the near view-plane 1.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection transform //dsprite->SetWorldViewRH(NULL, &matWorld); // Clear the depth buffer if hidden surface removal is on at the beginning of the draw step. if (enigma::d3dMode) d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); bool stop_loop = false; for (enigma::diter dit = drawing_depths.rbegin(); dit != drawing_depths.rend(); dit++) { enigma::inst_iter* push_it = enigma::instance_event_iterator; //loop instances for (enigma::instance_event_iterator = dit->second.draw_events->next; enigma::instance_event_iterator != NULL; enigma::instance_event_iterator = enigma::instance_event_iterator->next) { enigma::instance_event_iterator->inst->myevent_drawgui(); if (enigma::room_switching_id != -1) { stop_loop = true; break; } } enigma::instance_event_iterator = push_it; if (stop_loop) break; } } // Textures should be clamped when rendering 2D sprites and stuff, so memorize it. DWORD wrapu, wrapv, wrapw; d3ddev->GetSamplerState( 0, D3DSAMP_ADDRESSU, &wrapu ); d3ddev->GetSamplerState( 0, D3DSAMP_ADDRESSV, &wrapv ); d3ddev->GetSamplerState( 0, D3DSAMP_ADDRESSW, &wrapw ); d3ddev->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); d3ddev->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); d3ddev->SetSamplerState( 0, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP ); // The D3D sprite batcher uses clockwise face culling which is default but can't tell if // this here should memorize it and force it to CW all the time and then reset what the user had // or not. //d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); dsprite->End(); // And now reset the texture repetition. d3ddev->SetSamplerState( 0, D3DSAMP_ADDRESSU, wrapu ); d3ddev->SetSamplerState( 0, D3DSAMP_ADDRESSV, wrapv ); d3ddev->SetSamplerState( 0, D3DSAMP_ADDRESSW, wrapw ); // reset the culling d3d_set_culling(culling); d3ddev->EndScene(); // ends the 3D scene screen_refresh(); }
void d3d_primitive_begin(int kind) { texture_reset(); glBegin(ptypes_by_id[kind]); }
void draw_unbind_all() { texture_reset(); }
void glsl_program_reset() { //NOTE: Texture must be reset first so the Global VBO can draw and let people use shaders on text. texture_reset(); glUseProgram(0); }