void Renderer::render(bool dosleep) { size_t sleep = (size_t) round(1000 / frameRate_); TimeTracker& tt = *TimeTracker::getInstance(); Canvas& canvas = *Canvas::getInstance(); size_t dur = tt.measure([&]() { updateMutex_.lock(); if(field_ != NULL) { if(isEnabled()) { canvas.clear(); canvas.render(*field_); renderGui(); canvas.update(); resetCanvas_ = true; } else if (resetCanvas_) { canvas.clear(); canvas.reset(); renderGui(); canvas.update(); resetCanvas_ = false; } } updateMutex_.unlock(); }); dur /= 1000; if(dosleep){ if (dur < sleep) std::this_thread::sleep_for(std::chrono::milliseconds(sleep - dur)); } }
void ImGui_Wrapper::update(int width, int height, int dstFbo) { if (!m_VAO) initGui(); ImGuiIO& io = ImGui::GetIO(); io.DisplaySize = ImVec2(static_cast<float>(width), static_cast<float>(height)); auto now = high_resolution_clock::now(); duration<double> delta = now - m_lastTime; m_lastTime = now; auto dt = delta.count(); io.DeltaTime = dt > 0 ? static_cast<float>(dt) : 1 / 60.f; ImGui::NewFrame(); updateObjects(); ImGui::Render(); glBindFramebuffer(GL_FRAMEBUFFER, dstFbo); renderGui(); if (!m_document->animationIsPlaying()) { auto& gui = m_document->getGUI(); TimedFunctions::delayRun(1 / 60.0, [&gui]() { gui.updateView(); }); } for (int i = 0; i < 3; ++i) io.MouseDown[i] = m_mouseButtons[i]; }
void runGame() { hideCursor(); initWorld(); unsigned long curtime = ccTimeMilliseconds(); double acctime = 0.0; while(true){ ccEvent event = updateWindow(); if(event.type == CC_EVENT_WINDOW_QUIT){ break; }else if(event.type == CC_EVENT_KEY_DOWN){ handleKeyDownWorld(event.keyCode); }else if(event.type == CC_EVENT_KEY_UP){ handleKeyUpWorld(event.keyCode); } unsigned long newtime = ccTimeMilliseconds(); double frametime = (newtime - curtime) * 0.001; curtime = newtime; if(frametime > FRAME_CAP){ frametime = FRAME_CAP; } acctime += frametime; bool redraw = false; while(acctime >= FRAME_DELTA){ acctime -= FRAME_DELTA; redraw = updateWorld(); } if(redraw){ renderWorld(2, 1, getWidth() - getGuiWidth() - 3, getHeight() - 8); renderGui(getWidth() - getGuiWidth(), 0); } renderWindow(2); } }
int main() { cfguInit(); CFGU_GetSystemModel(&MODEL_3DS); FILE * file; shouldRenderDebug = true; if ((file = fopen("settings.bin", "r"))) { fread(&shouldRenderDebug,sizeof(bool),1,file); fread(&shouldSpeedup,sizeof(bool),1,file); osSetSpeedupEnable(shouldSpeedup); fclose(file); } sf2d_init(); csndInit(); noItem = newItem(ITEM_NULL, 0); currentMenu = MENU_TITLE; currentSelection = 0; quitGame = false; icons = sfil_load_PNG_buffer(icons2_png, SF2D_PLACE_RAM); font = sfil_load_PNG_buffer(Font_png, SF2D_PLACE_RAM); bottombg = sfil_load_PNG_buffer(bottombg_png, SF2D_PLACE_RAM); dirtColor[0] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 0)); dirtColor[1] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 1)); dirtColor[2] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 2)); dirtColor[3] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 3)); dirtColor[4] = SWAP_UINT32(sf2d_get_pixel(icons, 16, 4)); loadSound(&snd_playerHurt, "resources/playerhurt.raw"); loadSound(&snd_playerDeath, "resources/playerdeath.raw"); loadSound(&snd_monsterHurt, "resources/monsterhurt.raw"); loadSound(&snd_test, "resources/test.raw"); loadSound(&snd_pickup, "resources/pickup.raw"); loadSound(&snd_bossdeath, "resources/bossdeath.raw"); loadSound(&snd_craft, "resources/craft.raw"); bakeLights(); int i; for (i = 0; i < 5; ++i) { minimap[i] = sf2d_create_texture(128, 128, TEXFMT_RGBA8, SF2D_PLACE_RAM); sf2d_texture_tile32(minimap[i]); } sf2d_set_vblank_wait(true); sf2d_set_clear_color(0xFF000000); k_up.input = KEY_DUP | KEY_CPAD_UP | KEY_CSTICK_UP; k_down.input = KEY_DDOWN | KEY_CPAD_DOWN | KEY_CSTICK_DOWN; k_left.input = KEY_DLEFT | KEY_CPAD_LEFT | KEY_CSTICK_LEFT; k_right.input = KEY_DRIGHT | KEY_CPAD_RIGHT | KEY_CSTICK_RIGHT; k_attack.input = KEY_A | KEY_B | KEY_L | KEY_ZR; k_menu.input = KEY_X | KEY_Y | KEY_R | KEY_ZL; k_pause.input = KEY_START; k_accept.input = KEY_A; k_decline.input = KEY_B; k_delete.input = KEY_X; k_menuNext.input = KEY_R; k_menuPrev.input = KEY_L; if ((file = fopen("btnSave.bin", "rb"))) { fread(&k_up.input, sizeof(int), 1, file); fread(&k_down.input, sizeof(int), 1, file); fread(&k_left.input, sizeof(int), 1, file); fread(&k_right.input, sizeof(int), 1, file); fread(&k_attack.input, sizeof(int), 1, file); fread(&k_menu.input, sizeof(int), 1, file); fread(&k_pause.input, sizeof(int), 1, file); fread(&k_accept.input, sizeof(int), 1, file); fread(&k_decline.input, sizeof(int), 1, file); fread(&k_delete.input, sizeof(int), 1, file); fread(&k_menuNext.input, sizeof(int), 1, file); fread(&k_menuPrev.input, sizeof(int), 1, file); fclose(file); } if ((file = fopen("lastTP.bin", "r"))) { char fnbuf[256]; fgets(fnbuf, 256, file); // get directory to texturepack loadTexturePack(fnbuf); fclose(file); } tickCount = 0; initRecipes(); defineTables(); while (aptMainLoop()) { ++tickCount; hidScanInput(); tickKeys(hidKeysHeld(), hidKeysDown()); if (quitGame) break; if (initGame > 0) setupGame(initGame == 1 ? true : false); if (currentMenu == 0) { tick(); sf2d_start_frame(GFX_TOP, GFX_LEFT); offsetX = xscr; offsetY = yscr; sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //RGBA8(12, 12, 12, 255)); //You might think "real" black would be better, but it actually looks better that way renderLightsToStencil(); renderBackground(xscr, yscr); renderEntities(player.x, player.y, &eManager); renderPlayer(); resetStencilStuff(); offsetX = 0; offsetY = 0; if(shouldRenderDebug){ sprintf(fpsstr, " FPS: %.0f, X:%d, Y:%d, E:%d", sf2d_get_fps(), player.x, player.y, eManager.lastSlot[currentLevel]); drawText(fpsstr, 2, 225); } sf2d_end_frame(); sf2d_start_frame(GFX_BOTTOM, GFX_LEFT); if(!shouldRenderMap){ sf2d_draw_texture(bottombg, 0, 0); renderGui(); } else { renderZoomedMap(); } sf2d_end_frame(); } else { tickMenu(currentMenu); renderMenu(currentMenu, xscr, yscr); } sf2d_swapbuffers(); } freeRecipes(); freeLightBakes(); sf2d_free_texture(icons); sf2d_free_texture(minimap[0]); sf2d_free_texture(minimap[1]); sf2d_free_texture(minimap[2]); sf2d_free_texture(minimap[3]); sf2d_free_texture(minimap[4]); freeSounds(); csndExit(); cfguExit(); sf2d_fini(); return 0; }
void GuiTSCtrl::onRender(Point2I offset, const RectI &updateRect) { // Save the current transforms so we can restore // it for child control rendering below. GFXTransformSaver saver; bool renderingToTarget = false; if(!processCameraQuery(&mLastCameraQuery)) { // We have no camera, but render the GUI children // anyway. This makes editing GuiTSCtrl derived // controls easier in the GuiEditor. renderChildControls( offset, updateRect ); return; } GFXTargetRef origTarget = GFX->getActiveRenderTarget(); // Set up the appropriate render style U32 prevRenderStyle = GFX->getCurrentRenderStyle(); Point2F prevProjectionOffset = GFX->getCurrentProjectionOffset(); Point2I renderSize = getExtent(); if(mRenderStyle == RenderStyleStereoSideBySide) { GFX->setCurrentRenderStyle(GFXDevice::RS_StereoSideBySide); GFX->setCurrentProjectionOffset(mLastCameraQuery.projectionOffset); GFX->setStereoEyeOffsets(mLastCameraQuery.eyeOffset); if (!mLastCameraQuery.hasStereoTargets) { // Need to calculate our current viewport here mLastCameraQuery.stereoViewports[0] = updateRect; mLastCameraQuery.stereoViewports[0].extent.x /= 2; mLastCameraQuery.stereoViewports[1] = mLastCameraQuery.stereoViewports[0]; mLastCameraQuery.stereoViewports[1].point.x += mLastCameraQuery.stereoViewports[1].extent.x; } if (!mLastCameraQuery.hasFovPort) { // Need to make our own fovPort mLastCameraQuery.fovPort[0] = CalculateFovPortForCanvas(mLastCameraQuery.stereoViewports[0], mLastCameraQuery); mLastCameraQuery.fovPort[1] = CalculateFovPortForCanvas(mLastCameraQuery.stereoViewports[1], mLastCameraQuery); } GFX->setStereoFovPort(mLastCameraQuery.fovPort); // NOTE: this specifies fov for BOTH eyes GFX->setSteroViewports(mLastCameraQuery.stereoViewports); GFX->setStereoTargets(mLastCameraQuery.stereoTargets); MatrixF myTransforms[2]; if (smUseLatestDisplayTransform) { // Use the view matrix determined from the display device myTransforms[0] = mLastCameraQuery.eyeTransforms[0]; myTransforms[1] = mLastCameraQuery.eyeTransforms[1]; } else { // Use the view matrix determined from the control object myTransforms[0] = mLastCameraQuery.cameraMatrix; myTransforms[1] = mLastCameraQuery.cameraMatrix; QuatF qrot = mLastCameraQuery.cameraMatrix; Point3F pos = mLastCameraQuery.cameraMatrix.getPosition(); Point3F rotEyePos; myTransforms[0].setPosition(pos + qrot.mulP(mLastCameraQuery.eyeOffset[0], &rotEyePos)); myTransforms[1].setPosition(pos + qrot.mulP(mLastCameraQuery.eyeOffset[1], &rotEyePos)); } GFX->setStereoEyeTransforms(myTransforms); // Allow render size to originate from the render target if (mLastCameraQuery.stereoTargets[0]) { renderSize = mLastCameraQuery.stereoViewports[0].extent; renderingToTarget = true; } } else { GFX->setCurrentRenderStyle(GFXDevice::RS_Standard); } if ( mReflectPriority > 0 ) { // Get the total reflection priority. F32 totalPriority = 0; for ( U32 i=0; i < smAwakeTSCtrls.size(); i++ ) if ( smAwakeTSCtrls[i]->isVisible() ) totalPriority += smAwakeTSCtrls[i]->mReflectPriority; REFLECTMGR->update( mReflectPriority / totalPriority, getExtent(), mLastCameraQuery ); } if(mForceFOV != 0) mLastCameraQuery.fov = mDegToRad(mForceFOV); if(mCameraZRot) { MatrixF rotMat(EulerF(0, 0, mDegToRad(mCameraZRot))); mLastCameraQuery.cameraMatrix.mul(rotMat); } Frustum frustum; if(mRenderStyle == RenderStyleStereoSideBySide) { // NOTE: these calculations are essentially overridden later by the fov port settings when rendering each eye. MathUtils::makeFovPortFrustum(&frustum, mLastCameraQuery.ortho, mLastCameraQuery.nearPlane, mLastCameraQuery.farPlane, mLastCameraQuery.fovPort[0]); } else { // set up the camera and viewport stuff: F32 wwidth; F32 wheight; F32 renderWidth = F32(renderSize.x); F32 renderHeight = F32(renderSize.y); F32 aspectRatio = renderWidth / renderHeight; // Use the FOV to calculate the viewport height scale // then generate the width scale from the aspect ratio. if(!mLastCameraQuery.ortho) { wheight = mLastCameraQuery.nearPlane * mTan(mLastCameraQuery.fov / 2.0f); wwidth = aspectRatio * wheight; } else { wheight = mLastCameraQuery.fov; wwidth = aspectRatio * wheight; } F32 hscale = wwidth * 2.0f / renderWidth; F32 vscale = wheight * 2.0f / renderHeight; F32 left = (updateRect.point.x - offset.x) * hscale - wwidth; F32 right = (updateRect.point.x + updateRect.extent.x - offset.x) * hscale - wwidth; F32 top = wheight - vscale * (updateRect.point.y - offset.y); F32 bottom = wheight - vscale * (updateRect.point.y + updateRect.extent.y - offset.y); frustum.set( mLastCameraQuery.ortho, left, right, top, bottom, mLastCameraQuery.nearPlane, mLastCameraQuery.farPlane ); } // Manipulate the frustum for tiled screenshots const bool screenShotMode = gScreenShot && gScreenShot->isPending(); if ( screenShotMode ) { gScreenShot->tileFrustum( frustum ); GFX->setViewMatrix(MatrixF::Identity); } RectI tempRect = updateRect; if (!renderingToTarget) { #ifdef TORQUE_OS_MAC Point2I screensize = getRoot()->getWindowSize(); tempRect.point.y = screensize.y - (tempRect.point.y + tempRect.extent.y); #endif GFX->setViewport( tempRect ); } else { // Activate stereo RT GFX->activateStereoTarget(-1); } // Clear the zBuffer so GUI doesn't hose object rendering accidentally GFX->clear( GFXClearZBuffer , ColorI(20,20,20), 1.0f, 0 ); //GFX->clear( GFXClearTarget, ColorI(255,0,0), 1.0f, 0); GFX->setFrustum( frustum ); if(mLastCameraQuery.ortho) { mOrthoWidth = frustum.getWidth(); mOrthoHeight = frustum.getHeight(); } // We're going to be displaying this render at size of this control in // pixels - let the scene know so that it can calculate e.g. reflections // correctly for that final display result. gClientSceneGraph->setDisplayTargetResolution(renderSize); // Set the GFX world matrix to the world-to-camera transform, but don't // change the cameraMatrix in mLastCameraQuery. This is because // mLastCameraQuery.cameraMatrix is supposed to contain the camera-to-world // transform. In-place invert would save a copy but mess up any GUIs that // depend on that value. MatrixF worldToCamera = mLastCameraQuery.cameraMatrix; worldToCamera.inverse(); GFX->setWorldMatrix( worldToCamera ); mSaveProjection = GFX->getProjectionMatrix(); mSaveModelview = GFX->getWorldMatrix(); mSaveViewport = updateRect; mSaveWorldToScreenScale = GFX->getWorldToScreenScale(); mSaveFrustum = GFX->getFrustum(); mSaveFrustum.setTransform( mLastCameraQuery.cameraMatrix ); // Set the default non-clip projection as some // objects depend on this even in non-reflect cases. gClientSceneGraph->setNonClipProjection( mSaveProjection ); // Give the post effect manager the worldToCamera, and cameraToScreen matrices PFXMGR->setFrameMatrices( mSaveModelview, mSaveProjection ); renderWorld(updateRect); DebugDrawer::get()->render(); // Render the canvas overlay if its available if (mRenderStyle == RenderStyleStereoSideBySide && mStereoGuiTarget.getPointer()) { GFXDEBUGEVENT_SCOPE( StereoGui_Render, ColorI( 255, 0, 0 ) ); MatrixF proj(1); Frustum originalFrustum = GFX->getFrustum(); GFXTextureObject *texObject = mStereoGuiTarget->getTexture(0); const FovPort *currentFovPort = GFX->getStereoFovPort(); const MatrixF *eyeTransforms = GFX->getStereoEyeTransforms(); const Point3F *eyeOffset = GFX->getStereoEyeOffsets(); Frustum gfxFrustum = originalFrustum; for (U32 i=0; i<2; i++) { GFX->activateStereoTarget(i); MathUtils::makeFovPortFrustum(&gfxFrustum, true, gfxFrustum.getNearDist(), gfxFrustum.getFarDist(), currentFovPort[i], eyeTransforms[i]); GFX->setFrustum(gfxFrustum); MatrixF eyeWorldTrans(1); eyeWorldTrans.setPosition(Point3F(eyeOffset[i].x,eyeOffset[i].y,eyeOffset[i].z)); MatrixF eyeWorld(1); eyeWorld.mul(eyeWorldTrans); eyeWorld.inverse(); GFX->setWorldMatrix(eyeWorld); GFX->setViewMatrix(MatrixF::Identity); if (!mStereoOverlayVB.getPointer()) { mStereoOverlayVB.set(GFX, 4, GFXBufferTypeStatic); GFXVertexPCT *verts = mStereoOverlayVB.lock(0, 4); F32 texLeft = 0.0f; F32 texRight = 1.0f; F32 texTop = 1.0f; F32 texBottom = 0.0f; F32 rectRatio = gfxFrustum.getWidth() / gfxFrustum.getHeight(); F32 rectWidth = gfxFrustum.getWidth() * TS_OVERLAY_SCREEN_WIDTH; F32 rectHeight = rectWidth * rectRatio; F32 screenLeft = -rectWidth * 0.5; F32 screenRight = rectWidth * 0.5; F32 screenTop = -rectHeight * 0.5; F32 screenBottom = rectHeight * 0.5; const F32 fillConv = 0.0f; const F32 frustumDepthAdjusted = gfxFrustum.getNearDist() + 0.012; verts[0].point.set( screenLeft - fillConv, frustumDepthAdjusted, screenTop - fillConv ); verts[1].point.set( screenRight - fillConv, frustumDepthAdjusted, screenTop - fillConv ); verts[2].point.set( screenLeft - fillConv, frustumDepthAdjusted, screenBottom - fillConv ); verts[3].point.set( screenRight - fillConv, frustumDepthAdjusted, screenBottom - fillConv ); verts[0].color = verts[1].color = verts[2].color = verts[3].color = ColorI(255,255,255,255); verts[0].texCoord.set( texLeft, texTop ); verts[1].texCoord.set( texRight, texTop ); verts[2].texCoord.set( texLeft, texBottom ); verts[3].texCoord.set( texRight, texBottom ); mStereoOverlayVB.unlock(); } if (!mStereoGuiSB.getPointer()) { // DrawBitmapStretchSR GFXStateBlockDesc bitmapStretchSR; bitmapStretchSR.setCullMode(GFXCullNone); bitmapStretchSR.setZReadWrite(false, false); bitmapStretchSR.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha); bitmapStretchSR.samplersDefined = true; bitmapStretchSR.samplers[0] = GFXSamplerStateDesc::getClampLinear(); bitmapStretchSR.samplers[0].minFilter = GFXTextureFilterPoint; bitmapStretchSR.samplers[0].mipFilter = GFXTextureFilterPoint; bitmapStretchSR.samplers[0].magFilter = GFXTextureFilterPoint; mStereoGuiSB = GFX->createStateBlock(bitmapStretchSR); } GFX->setVertexBuffer(mStereoOverlayVB); GFX->setStateBlock(mStereoGuiSB); GFX->setTexture( 0, texObject ); GFX->setupGenericShaders( GFXDevice::GSModColorTexture ); GFX->drawPrimitive( GFXTriangleStrip, 0, 2 ); } } // Restore the previous matrix state before // we begin rendering the child controls. saver.restore(); // Restore the render style and any stereo parameters GFX->setActiveRenderTarget(origTarget); GFX->setCurrentRenderStyle(prevRenderStyle); GFX->setCurrentProjectionOffset(prevProjectionOffset); if(mRenderStyle == RenderStyleStereoSideBySide && gLastStereoTexture) { GFX->setClipRect(updateRect); GFX->getDrawUtil()->drawBitmapStretch(gLastStereoTexture, updateRect); } // Allow subclasses to render 2D elements. GFX->setClipRect(updateRect); renderGui( offset, updateRect ); if (shouldRenderChildControls()) { renderChildControls(offset, updateRect); } smFrameCount++; }
void GuiTSCtrl::onRender(Point2I offset, const RectI &updateRect) { // Save the current transforms so we can restore // it for child control rendering below. GFXTransformSaver saver; if(!processCameraQuery(&mLastCameraQuery)) { // We have no camera, but render the GUI children // anyway. This makes editing GuiTSCtrl derived // controls easier in the GuiEditor. renderChildControls( offset, updateRect ); return; } // Set up the appropriate render style U32 prevRenderStyle = GFX->getCurrentRenderStyle(); Point2F prevProjectionOffset = GFX->getCurrentProjectionOffset(); Point3F prevEyeOffset = GFX->getStereoEyeOffset(); if(mRenderStyle == RenderStyleStereoSideBySide) { GFX->setCurrentRenderStyle(GFXDevice::RS_StereoSideBySide); GFX->setCurrentProjectionOffset(mLastCameraQuery.projectionOffset); GFX->setStereoEyeOffset(mLastCameraQuery.eyeOffset); } else { GFX->setCurrentRenderStyle(GFXDevice::RS_Standard); } if ( mReflectPriority > 0 ) { // Get the total reflection priority. F32 totalPriority = 0; for ( U32 i=0; i < smAwakeTSCtrls.size(); i++ ) if ( smAwakeTSCtrls[i]->isVisible() ) totalPriority += smAwakeTSCtrls[i]->mReflectPriority; REFLECTMGR->update( mReflectPriority / totalPriority, getExtent(), mLastCameraQuery ); } if(mForceFOV != 0) mLastCameraQuery.fov = mDegToRad(mForceFOV); if(mCameraZRot) { MatrixF rotMat(EulerF(0, 0, mDegToRad(mCameraZRot))); mLastCameraQuery.cameraMatrix.mul(rotMat); } // set up the camera and viewport stuff: F32 wwidth; F32 wheight; F32 renderWidth = (mRenderStyle == RenderStyleStereoSideBySide) ? F32(getWidth())*0.5f : F32(getWidth()); F32 renderHeight = F32(getHeight()); F32 aspectRatio = renderWidth / renderHeight; // Use the FOV to calculate the viewport height scale // then generate the width scale from the aspect ratio. if(!mLastCameraQuery.ortho) { wheight = mLastCameraQuery.nearPlane * mTan(mLastCameraQuery.fov / 2.0f); wwidth = aspectRatio * wheight; } else { wheight = mLastCameraQuery.fov; wwidth = aspectRatio * wheight; } F32 hscale = wwidth * 2.0f / renderWidth; F32 vscale = wheight * 2.0f / renderHeight; Frustum frustum; if(mRenderStyle == RenderStyleStereoSideBySide) { F32 left = 0.0f * hscale - wwidth; F32 right = renderWidth * hscale - wwidth; F32 top = wheight - vscale * 0.0f; F32 bottom = wheight - vscale * renderHeight; frustum.set( mLastCameraQuery.ortho, left, right, top, bottom, mLastCameraQuery.nearPlane, mLastCameraQuery.farPlane ); } else { F32 left = (updateRect.point.x - offset.x) * hscale - wwidth; F32 right = (updateRect.point.x + updateRect.extent.x - offset.x) * hscale - wwidth; F32 top = wheight - vscale * (updateRect.point.y - offset.y); F32 bottom = wheight - vscale * (updateRect.point.y + updateRect.extent.y - offset.y); frustum.set( mLastCameraQuery.ortho, left, right, top, bottom, mLastCameraQuery.nearPlane, mLastCameraQuery.farPlane ); } // Manipulate the frustum for tiled screenshots const bool screenShotMode = gScreenShot && gScreenShot->isPending(); if ( screenShotMode ) { gScreenShot->tileFrustum( frustum ); GFX->setViewMatrix(MatrixF::Identity); } RectI tempRect = updateRect; #ifdef TORQUE_OS_MAC Point2I screensize = getRoot()->getWindowSize(); tempRect.point.y = screensize.y - (tempRect.point.y + tempRect.extent.y); #endif GFX->setViewport( tempRect ); // Clear the zBuffer so GUI doesn't hose object rendering accidentally GFX->clear( GFXClearZBuffer , ColorI(20,20,20), 1.0f, 0 ); GFX->setFrustum( frustum ); if(mLastCameraQuery.ortho) { mOrthoWidth = frustum.getWidth(); mOrthoHeight = frustum.getHeight(); } // We're going to be displaying this render at size of this control in // pixels - let the scene know so that it can calculate e.g. reflections // correctly for that final display result. gClientSceneGraph->setDisplayTargetResolution(getExtent()); // Set the GFX world matrix to the world-to-camera transform, but don't // change the cameraMatrix in mLastCameraQuery. This is because // mLastCameraQuery.cameraMatrix is supposed to contain the camera-to-world // transform. In-place invert would save a copy but mess up any GUIs that // depend on that value. MatrixF worldToCamera = mLastCameraQuery.cameraMatrix; worldToCamera.inverse(); GFX->setWorldMatrix( worldToCamera ); mSaveProjection = GFX->getProjectionMatrix(); mSaveModelview = GFX->getWorldMatrix(); mSaveViewport = updateRect; mSaveWorldToScreenScale = GFX->getWorldToScreenScale(); mSaveFrustum = GFX->getFrustum(); mSaveFrustum.setTransform( mLastCameraQuery.cameraMatrix ); // Set the default non-clip projection as some // objects depend on this even in non-reflect cases. gClientSceneGraph->setNonClipProjection( mSaveProjection ); // Give the post effect manager the worldToCamera, and cameraToScreen matrices PFXMGR->setFrameMatrices( mSaveModelview, mSaveProjection ); renderWorld(updateRect); DebugDrawer::get()->render(); // Restore the previous matrix state before // we begin rendering the child controls. saver.restore(); // Restore the render style and any stereo parameters GFX->setCurrentRenderStyle(prevRenderStyle); GFX->setCurrentProjectionOffset(prevProjectionOffset); GFX->setStereoEyeOffset(prevEyeOffset); // Allow subclasses to render 2D elements. GFX->setClipRect(updateRect); renderGui( offset, updateRect ); renderChildControls(offset, updateRect); smFrameCount++; }
int main() { sf2d_init(); csndInit(); noItem = newItem(ITEM_NULL,0); currentMenu = MENU_TITLE; currentSelection = 0; quitGame = false; icons = sfil_load_PNG_buffer(icons2_png, SF2D_PLACE_RAM); font = sfil_load_PNG_buffer(Font_png, SF2D_PLACE_RAM); //consoleInit(GFX_BOTTOM, NULL); // printf("Press 'Start' to exit.\n"); loadSound(&snd_playerHurt, "resources/playerhurt.raw"); loadSound(&snd_playerDeath, "resources/playerdeath.raw"); loadSound(&snd_monsterHurt, "resources/monsterhurt.raw"); loadSound(&snd_test, "resources/test.raw"); loadSound(&snd_pickup, "resources/pickup.raw"); loadSound(&snd_bossdeath, "resources/bossdeath.raw"); loadSound(&snd_craft, "resources/craft.raw"); int i; for(i = 0;i < 5;++i){ minimap[i] = sf2d_create_texture(128,128,TEXFMT_RGBA8,SF2D_PLACE_RAM); sf2d_texture_tile32(minimap[i]); } sf2d_set_vblank_wait(true); sf2d_set_clear_color(0xFF); /* Default inputs */ k_up.input = KEY_DUP | KEY_CPAD_UP | KEY_CSTICK_UP; k_down.input = KEY_DDOWN | KEY_CPAD_DOWN | KEY_CSTICK_DOWN; k_left.input = KEY_DLEFT | KEY_CPAD_LEFT | KEY_CSTICK_LEFT; k_right.input = KEY_DRIGHT | KEY_CPAD_RIGHT | KEY_CSTICK_RIGHT; k_attack.input = KEY_A | KEY_B | KEY_L | KEY_ZR; k_menu.input = KEY_X | KEY_Y | KEY_R | KEY_ZL; k_pause.input = KEY_START; k_accept.input = KEY_A; k_decline.input = KEY_B; k_delete.input = KEY_X; FILE * file; /* If btnSave exists, then use that. */ if ((file = fopen("btnSave.bin", "rb"))){ fread(&k_up.input, sizeof(int), 1, file); fread(&k_down.input, sizeof(int), 1, file); fread(&k_left.input, sizeof(int), 1, file); fread(&k_right.input, sizeof(int), 1, file); fread(&k_attack.input, sizeof(int), 1, file); fread(&k_menu.input, sizeof(int), 1, file); fread(&k_pause.input, sizeof(int), 1, file); fread(&k_accept.input, sizeof(int), 1, file); fread(&k_decline.input, sizeof(int), 1, file); fread(&k_delete.input, sizeof(int), 1, file); fclose(file); } //screenShot = false; tickCount=0; initRecipes(); defineTables(); while (aptMainLoop()) { ++tickCount; hidScanInput(); tickKeys(hidKeysHeld(),hidKeysDown()); //if (quitGame || hidKeysHeld() & KEY_SELECT) break; if (quitGame) break; //if (hidKeysDown() & (KEY_L | KEY_R)) screenShot = true; // else screenShot = false; if(initGame > 0) setupGame(initGame == 1 ? true : false); if(currentMenu == 0){ tick(); sprintf(fpsstr, " FPS: %.0f, X:%d, Y:%d, E:%d", sf2d_get_fps(),player.x, player.y,eManager.lastSlot[currentLevel]); sf2d_start_frame(GFX_TOP, GFX_LEFT); if(currentLevel == 0){ sf2d_draw_texture_part_scale(minimap[1],(-xscr/3)-256,(-yscr/3)-32,0,0,128,128,12.5,7.5); sf2d_draw_rectangle(0,0,400,240, 0xDFDFDFAF); } offsetX = xscr;offsetY = yscr; renderBackground(xscr,yscr); renderEntities(player.x, player.y, &eManager); renderPlayer(); offsetX = 0;offsetY = 0; renderItemWithText(player.p.activeItem, 10, 205); // drawText(debugText,2,208); drawText(fpsstr,2,225); sf2d_end_frame(); sf2d_start_frame(GFX_BOTTOM, GFX_LEFT); if(currentLevel == 0 && airWizardHealthDisplay > 0){ sprintf(bossHealthText, "BOSS: %.0f%%", ((float)airWizardHealthDisplay/2000.0)*100); drawText(bossHealthText,2,225); } renderGui(); sf2d_draw_texture(minimap[currentLevel], 192, 112);//y:56 sf2d_end_frame(); } else{ tickMenu(currentMenu); renderMenu(currentMenu,xscr,yscr); } sf2d_swapbuffers(); } freeRecipes(); sf2d_free_texture(icons); sf2d_free_texture(minimap[0]); sf2d_free_texture(minimap[1]); sf2d_free_texture(minimap[2]); sf2d_free_texture(minimap[3]); sf2d_free_texture(minimap[4]); freeSounds(); csndExit(); sf2d_fini(); return 0; }
int Application::run(Application* self) { m_self = self; if (!framework::Utils::exists("data/gui")) { Logger::toLog("Error: could not find gui directory. Probably working directory has not been set correctly (especially if you are running from IDE).\n"); return EXIT_FAILURE; } init(); m_isRunning = true; glfwSetErrorCallback(&Application::errorCallback); if (!glfwInit()) { Logger::toLog("Error: glfwInit failed"); return EXIT_FAILURE; } glfwWindowHint(GLFW_VISIBLE, GL_TRUE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_RED_BITS, 8); glfwWindowHint(GLFW_GREEN_BITS, 8); glfwWindowHint(GLFW_BLUE_BITS, 8); glfwWindowHint(GLFW_ALPHA_BITS, 0); glfwWindowHint(GLFW_DEPTH_BITS, 32); glfwWindowHint(GLFW_STENCIL_BITS, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, m_info.majorVersion); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, m_info.minorVersion); #ifdef _DEBUG glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); #endif glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_SAMPLES, m_info.samples); glfwWindowHint(GLFW_STEREO, m_info.flags.stereo ? GL_TRUE : GL_FALSE); // create window m_window = glfwCreateWindow(m_info.windowWidth, m_info.windowHeight, m_info.title.c_str(), m_info.flags.fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL); if (!m_window) { glfwTerminate(); return EXIT_FAILURE; } glfwSetWindowSizeCallback(m_window, &Application::_setWindowSize); glfwSetKeyCallback(m_window, &Application::_onKey); glfwSetCharCallback(m_window, &Application::_onChar); glfwSetMouseButtonCallback(m_window, &Application::_onMouse); glfwSetCursorPosCallback(m_window, &Application::_onCursor); glfwSetScrollCallback(m_window, &Application::_onScroll); glfwSetInputMode(m_window, GLFW_CURSOR, m_info.flags.cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); // center position GLFWmonitor* primary = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(primary); int posx = (mode->width - m_info.windowWidth) >> 1; int posy = (mode->height - m_info.windowHeight) >> 1; glfwSetWindowPos(m_window, posx, posy); // set vsync glfwMakeContextCurrent(m_window); glfwSwapInterval((int)m_info.flags.vsync); // init GL3w gl3wInit(); std::vector<int> multisamplingLevels; if (!checkOpenGLVersion() || !checkDeviceCapabilities(multisamplingLevels)) { glfwDestroyWindow(m_window); glfwTerminate(); return EXIT_FAILURE; } #ifdef _DEBUG Logger::toLogWithFormat("Video adapter: %s - %s, OpenGL: %s\n", (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER), (const char*)glGetString(GL_VERSION)); #endif if (m_info.flags.debug) { if (gl3wIsSupported(4, 3)) { glDebugMessageCallback(debugCallback, this); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); } else if (IsExtensionSupported("GL_ARB_debug_output")) { glDebugMessageCallbackARB(debugCallback, this); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); } } initGui(); if (!StandardGpuPrograms::init()) { glfwDestroyWindow(m_window); glfwTerminate(); return EXIT_FAILURE; } initAxes(); startup(m_rootWindow); do { glfwMakeContextCurrent(m_window); Texture::beginFrame(); if (fabs(m_lastTime) < 1e-7) { render(0); Texture::endFrame(); renderGui(0); m_lastTime = glfwGetTime(); } else { double curTime = glfwGetTime(); double delta = curTime - m_lastTime; // fps counter measureFps(delta); // rendering render(delta); Texture::endFrame(); renderGui(delta); m_lastTime = curTime; } glfwSwapBuffers(m_window); glfwPollEvents(); if (glfwWindowShouldClose(m_window)) { m_isRunning = GL_FALSE; } m_isRunning &= (glfwGetKey(m_window, GLFW_KEY_ESCAPE) == GLFW_RELEASE); } while(m_isRunning); shutdown(); destroyAllDestroyable(); destroyGui(); glfwDestroyWindow(m_window); glfwTerminate(); return EXIT_SUCCESS; }