void SpriteRenderer::RenderText(const SpriteFont& font, const wchar* text, const Float4x4& transform, const Float4& color) { D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer RenderText"); size_t length = wcslen(text); Float4x4 textTransform; uint64 numCharsToDraw = std::min(length, MaxBatchSize); uint64 currentDraw = 0; for(uint64 i = 0; i < numCharsToDraw; ++i) { wchar character = text[i]; if(character == ' ') textTransform._41 += font.SpaceWidth(); else if(character == '\n') { textTransform._42 += font.CharHeight(); textTransform._41 = 0; } else { SpriteFont::CharDesc desc = font.GetCharDescriptor(character); textDrawData[currentDraw].Transform = textTransform * transform; textDrawData[currentDraw].Color = color; textDrawData[currentDraw].DrawRect.x = desc.X; textDrawData[currentDraw].DrawRect.y = desc.Y; textDrawData[currentDraw].DrawRect.z = desc.Width; textDrawData[currentDraw].DrawRect.w = desc.Height; currentDraw++; textTransform._41 += desc.Width + 1; } } // Submit a batch RenderBatch(font.SRView(), textDrawData, currentDraw); D3DPERF_EndEvent(); if(length > numCharsToDraw) RenderText(font, text + numCharsToDraw, textTransform, color); }
void SpriteRenderer::RenderText(const SpriteFont& font, const WCHAR* text, const XMMATRIX& transform, const XMFLOAT4& color) { D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer RenderText"); size_t length = wcslen(text); XMMATRIX textTransform = XMMatrixIdentity(); UINT64 numCharsToDraw = min(length, MaxBatchSize); UINT64 currentDraw = 0; for (UINT64 i = 0; i < numCharsToDraw; ++i) { WCHAR character = text[i]; if(character == ' ') textTransform._41 += font.SpaceWidth(); else if(character == '\n') { textTransform._42 += font.CharHeight(); textTransform._41 = 0; } else { SpriteFont::CharDesc desc = font.GetCharDescriptor(character); textDrawData[currentDraw].Transform = XMMatrixMultiply(textTransform, transform); textDrawData[currentDraw].Color = color; textDrawData[currentDraw].DrawRect.x = desc.X; textDrawData[currentDraw].DrawRect.y = desc.Y; textDrawData[currentDraw].DrawRect.z = desc.Width; textDrawData[currentDraw].DrawRect.w = desc.Height; currentDraw++; textTransform._41 += desc.Width + 1; } } // Submit a batch RenderBatch(font.SRView(), textDrawData, currentDraw); D3DPERF_EndEvent(); if(length > numCharsToDraw) RenderText(font, text + numCharsToDraw, textTransform, color); }
/// @summary Submits a single frame to the GPU for rendering. Runs once per /// application tick at a variable timestep. /// @param currentTime The current absolute time, in seconds. This represents /// the time at which the current tick started. /// @param elapsedTime The time elapsed since the previous tick, in seconds. /// @param t A value in [0, 1] indicating how far into the current simulation /// step we are at the time the frame is generated. /// @param width The width of the default framebuffer, in pixels. /// @param height The height of the default framebuffer, in pixels. static void render(double currentTime, double elapsedTime, double t, int width, int height) { UNUSED_ARG(currentTime); UNUSED_ARG(elapsedTime); UNUSED_ARG(t); DisplayManager *dm = gDisplayManager; SpriteBatch *batch = dm->GetBatch(); SpriteFont *font = dm->GetFont(); float rgba[]= {1.0f, 0.0f, 0.0f, 1.0f}; dm->SetViewport(width, height); dm->Clear(0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0); dm->BeginFrame(); batch->SetBlendModeAlpha(); font->Draw("Hello, world!", 0, 0, 1, rgba, 5.0f, 5.0f, batch); gEntityManager->Draw(currentTime, elapsedTime, dm); dm->EndFrame(); }
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA); // single is default glutInitWindowSize(500, 500); // default is 300 x 300 glutInitWindowPosition(0, 0); glutCreateWindow("Second OpenGL Program"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutIdleFunc(idle); font.load("Font.bmp"); init(); glutMainLoop(); return 1; }
void display(void) { // glFlush (); stringstream ss,sr,st,sd,phy,AI,slow,frame1; //for three different font on the screen. string radh; /*if(fog_status==true) { fog_enable(); } else { fog_disable(); } */ float depth=0.9; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); // which matrix glLoadIdentity(); // initialize the selected matrix Vector3 subvelo(sub.vxs,0,sub.vzs); Vector3 unit_vect; Vector3 look(sub.xPosition,sub.yPosition,(sub.zPosition)); Vector3 c(sub.xPosition,sub.yPosition,sub.zPosition+40); float speed= subvelo.getNorm(); if(speed!=0) { unit_vect=subvelo.getNormalized(); unit_vect=unit_vect*40; c= look - unit_vect; q= look + unit_vect; } float deltaX= 40*sin((sub.angle_sub)*0.0174532925); float deltaZ= 40*cos((sub.angle_sub)*0.0174532925); Vector3 des(deltaX,0,deltaZ); c=look-des; q=c+des; //cout<<c.x<<" "<<c.y<<" "<<c.z<<endl; gluLookAt( //camera will move if submarine moves, z position of eye will remain fixed so that submarine remain constant c.x, c.y, c.z, // x,y,z of eye q.x, q.y, q.z, // x,y,z of look at point 0.0, 1.0, 0.0 ); // x,y,z of up vector //cout<<"asaf"<<q.x<<" "<<q.y<<" "<<q.z<<endl; fis.floor_surface(); ss << "X: " <<sub.xPosition<<" Y: "<<sub.yPosition<<" z: "<<sub.zPosition; radh=ss.str(); if(sub.yPosition>1000) { sub.skybox(sub); } sub.depth_background(); //background color according to depth //position and shape of submarine // add more stuff here to draw other models fis.fish_position_color(spiky_obj); //this will position and color the fish if(fis.is_diamond()==1) { whale_model(); } sub.submarine(submarine_obj); // detect submarine is close enough to fish or not if(fis.is_finish()) //whether 10 fish is catched or no { //If catched then game over char store; SpriteFont::setUp2DView(640, 480); font.draw("Game Over, You Win", 150,150,SpriteFont::BOLD); //game over SpriteFont::unsetUp2DView(); int total=fis.no_fish(); int score=fis.my_score(); string sco; st<<"Score: "<<score; sco=st.str(); sr << "Total_fish: " <<total; string rad=sr.str(); SpriteFont::setUp2DView(640, 480); font.draw(radh, 350,30,SpriteFont::BOLD); font.draw(rad,30,30,SpriteFont::BOLD); font.draw(sco,30,450); SpriteFont::unsetUp2DView(); glFlush(); glutSwapBuffers(); sleep(5); //wait for 5 seconds and then exit exit(1); } if(fis.is_diamond()==1) { SpriteFont::setUp2DView(640, 480); //font.draw("Bonus!!! Dont worry about the depth", 20,50,SpriteFont::BOLD); SpriteFont::unsetUp2DView(); } int total=fis.no_fish(); int score=fis.my_score(); string sco; string physics; string AI_delta; string slow_AI; string frame_rate; AI<<"AI_delta_time: "<<AI_delta_time<<" sec"; phy<<"Physics_delta_time: "<<physics_delta_time<<" sec"; slow<<"Slow_AI_update: "<<ai_slow_value<<" sec"; frame1<<"frame_rate: "<<frame<<" fps"; physics=phy.str(); AI_delta=AI.str(); slow_AI=slow.str(); frame_rate=frame1.str(); st<<"Score: "<<score; sco=st.str(); sr << "Total_fish: " <<total; string rad=sr.str(); SpriteFont::setUp2DView(640, 480); font.draw(radh, 300,20,SpriteFont::BOLD); font.draw(rad,30,20,SpriteFont::BOLD); font.draw(sco,30,450); //font.draw(physics,30,410); //font.draw(AI_delta,350,410); //font.draw(slow_AI,200,380); //font.draw(frame_rate,200,50); SpriteFont::unsetUp2DView(); static int shouldPlaySound = 1; //glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); // glClearColor(0,0,0,1); fis.background_sound(); glFlush(); glutSwapBuffers(); // display it, double buffering }
Common::Error CryoEngine::run() { // Initialize graphics using following: initGraphics(320, 200, false); // Create debugger console. It requires GFX to be initialized _console = new CryoConsole(this); // Additional setup. //debug("CryoEngine::init\n"); Common::Event event; Common::EventManager *eventMan = _system->getEventManager(); _resMan = new ResourceManager(isCD()); // Show something Sprite *s = new Sprite("intds.hsq", this); s->setPalette(); s->drawFrame(0, 0, 92); delete s; SpriteFont *sf = new SpriteFont("generic.hsq", this); sf->drawText("DUNE TEST", 100, 50); delete sf; Common::String charFile = isCD() ? "dnchar.bin" : "dunechar.hsq"; FixedFont *f = new FixedFont(charFile, this); f->drawText("DUNE TEST", 100, 120, 10); delete f; /*Game *g = new Game(_system); g->loadProtection(); delete g;*/ // Update the screen so that its contents can be shown _system->updateScreen(); // Your main even loop should be (invoked from) here. //debug("CryoEngine::go: Hello, World!\n"); while (!shouldQuit()) { // Open the debugger window, if requested while (eventMan->pollEvent(event)) { if (event.kbd.hasFlags(Common::KBD_CTRL) && event.kbd.keycode == Common::KEYCODE_d) { _console->attach(); _console->onFrame(); } } // TODO: Do something... _system->delayMillis(10); } // This test will show up if -d1 and --debugflags=example are specified on the commandline //debugC(1, kQuuxDebugExample, "Example debug call"); // This test will show up if --debugflags=example or --debugflags=example2 or both of them and -d3 are specified on the commandline //debugC(3, kQuuxDebugExample | kQuuxDebugExample2, "Example debug call two"); return Common::kNoError; }
SpriteFont* FontLoader::LoadContent(const std::string& assetFile) { using namespace std; cout << "Loading Texture: " << assetFile << " . . . "; auto pBinReader = new BinaryReader(); //Prevent memory leaks pBinReader->Open(assetFile); if (!pBinReader->Exists()) { delete pBinReader; std::cout << " . . . FAILED!\n[ERROR] " << "SpriteFont::Load > Failed to read the assetFile!\nPath: " << assetFile << std::endl; return nullptr; } bool valid = false; if (pBinReader->Read<char>() == 'B') { if (pBinReader->Read<char>() == 'M') { if (pBinReader->Read<char>() == 'F') { valid = true; } } } if (!valid) { std::cout << " . . . FAILED!\n[ERROR] " << "Font file header invalid!" << std::endl; return nullptr; } if (pBinReader->Read<char>() < 3) { std::cout << " . . . FAILED!\n[ERROR] " << "Font version invalid!" << std::endl; return nullptr; } SpriteFont* pFont = new SpriteFont(); //********** // BLOCK 0 * //********** pBinReader->Read<char>(); auto Block0Size = pBinReader->Read<int>(); int pos = pBinReader->GetBufferPosition(); pFont->m_FontSize = pBinReader->Read<short>(); pBinReader->SetBufferPosition(pos + 14); std::string fn; char cur = pBinReader->Read<char>(); while (cur != '\0') { fn += cur; cur = pBinReader->Read<char>(); } pFont->m_FontName = fn; pBinReader->SetBufferPosition(pos + Block0Size); //********** // BLOCK 1 * //********** pBinReader->Read<char>(); auto Block1Size = pBinReader->Read<int>(); pos = pBinReader->GetBufferPosition(); pBinReader->SetBufferPosition(pos + 4); pFont->m_TextureWidth = pBinReader->Read<unsigned short>(); pFont->m_TextureHeight = pBinReader->Read<unsigned short>(); auto pagecount = pBinReader->Read<unsigned short>(); if (pagecount > 1)std::cout << "[ERROR] " << "SpriteFont::Load > SpriteFont(.fnt): Only one texture per font allowed" << std::endl; pBinReader->SetBufferPosition(pos + Block1Size); //********** // BLOCK 2 * //********** pBinReader->Read<char>(); auto Block2Size = pBinReader->Read<int>(); pos = pBinReader->GetBufferPosition(); std::string pn; cur = pBinReader->Read<char>(); while (cur != '\0') { pn += cur; cur = pBinReader->Read<char>(); } if (pn.size() == 0)std::cout << "[ERROR] " << "SpriteFont::Load > SpriteFont(.fnt): Invalid Font Sprite [Empty]" << std::endl; auto filepath = assetFile.substr(0, assetFile.rfind('/') + 1); std::cout << std::endl << "\t"; pFont->m_pTexture = ContentManager::Load<TextureData>(filepath + pn); pBinReader->SetBufferPosition(pos + Block2Size); //********** // BLOCK 3 * //********** pBinReader->Read<char>(); auto Block3Size = pBinReader->Read<int>(); pos = pBinReader->GetBufferPosition(); auto numChars = Block3Size / 20; pFont->m_CharacterCount = numChars; for (int i = 0; i < numChars; i++) { auto posChar = pBinReader->GetBufferPosition(); auto charId = (wchar_t)(pBinReader->Read<unsigned int>()); if (!(pFont->IsCharValid(charId))) { std::cout << "[WARNING] " << "SpriteFont::Load > SpriteFont(.fnt): Invalid Character" << std::endl; pBinReader->SetBufferPosition(posChar + 20); } else { auto metric = &(pFont->GetMetric(charId)); metric->IsValid = true; metric->Character = charId; auto xPos = pBinReader->Read<unsigned short>(); auto yPos = pBinReader->Read<unsigned short>(); metric->Width = pBinReader->Read<unsigned short>(); metric->Height = pBinReader->Read<unsigned short>(); metric->OffsetX = pBinReader->Read<short>(); metric->OffsetY = pBinReader->Read<short>(); metric->AdvanceX = pBinReader->Read<short>(); metric->Page = pBinReader->Read<unsigned char>(); auto chan = pBinReader->Read<unsigned char>(); switch (chan) { case 1: metric->Channel = 2; break; case 2: metric->Channel = 1; break; case 4: metric->Channel = 0; break; case 8: metric->Channel = 3; break; default: metric->Channel = 4; break; } metric->TexCoord = glm::vec2((float)xPos / (float)pFont->m_TextureWidth , (float)yPos / (float)pFont->m_TextureHeight); pBinReader->SetBufferPosition(posChar + 20); } } delete pBinReader; cout << " . . . SUCCESS!" << endl; return pFont; }
bool PauseMenuView::Draw(PauseMenu* menu) { if (menu->isActive()) { SpriteBatch* spriteBatch = game->getGraphics()->getSpriteBatch(); SpriteFont* spriteFontBig = game->getGraphics()->getSpriteFontBig(); SpriteFont* spriteFontNormal = game->getGraphics()->getSpriteFontNormal(); spriteBatch->Begin(); // From the original 800x600 positions to 1366x768 // Can be refactored to use inheritance and be dynamic to whatever the screen dimensions are float resolutionWidthOffset = 283.0f; float resolutionHeightOffset = 84.0f; //font->DrawString(sprites, L"AVALANCHE", XMFLOAT2(278, 85), Colors::Purple); spriteFontBig->DrawString(spriteBatch, L"AVALANCHE", XMFLOAT2(MENU_TITLE_X, MENU_TITLE_Y), Colors::Purple); switch(menu->getPausedPlayer()) { case 0: spriteFontNormal->DrawString(spriteBatch, L"PLAYER 1 PAUSED", XMFLOAT2(228.0f + resolutionWidthOffset, 155.0f + resolutionHeightOffset), Colors::Purple); break; case 1: spriteFontNormal->DrawString(spriteBatch, L"PLAYER 2 PAUSED", XMFLOAT2(228.0f + resolutionWidthOffset, 155.0f + resolutionHeightOffset), Colors::Purple); break; case 2: spriteFontNormal->DrawString(spriteBatch, L"PLAYER 3 PAUSED", XMFLOAT2(228.0f + resolutionWidthOffset, 155.0f + resolutionHeightOffset), Colors::Purple); break; case 3: spriteFontNormal->DrawString(spriteBatch, L"PLAYER 4 PAUSED", XMFLOAT2(228.0f + resolutionWidthOffset, 155.0f + resolutionHeightOffset), Colors::Purple); break; } spriteFontNormal->DrawString(spriteBatch, L"resume game", XMFLOAT2(260.0f + resolutionWidthOffset, 213.0f + resolutionHeightOffset), Colors::Silver); spriteFontNormal->DrawString(spriteBatch, L"how to play", XMFLOAT2(275.0f + resolutionWidthOffset, 263.0f + resolutionHeightOffset), Colors::Silver); spriteFontNormal->DrawString(spriteBatch, L"exit to menu", XMFLOAT2(265.0f + resolutionWidthOffset, 313.0f + resolutionHeightOffset), Colors::Silver); switch(menu->getCurrentSelection()) { case 0: spriteFontNormal->DrawString(spriteBatch, L"resume game", XMFLOAT2(260.0f + resolutionWidthOffset, 213.0f + resolutionHeightOffset), Colors::Purple); break; case 1: spriteFontNormal->DrawString(spriteBatch, L"how to play", XMFLOAT2(275.0f + resolutionWidthOffset, 263.0f + resolutionHeightOffset), Colors::Purple); break; case 2: spriteFontNormal->DrawString(spriteBatch, L"exit to menu", XMFLOAT2(265.0f + resolutionWidthOffset, 313.0f + resolutionHeightOffset), Colors::Purple); break; } spriteBatch->End(); game->getGraphics()->getD3D()->getSwapChain()->Present(0,0); } return true; }