void Sprite::Render(RenderTarget&, Renderer& renderer) const { // Get the sprite size float width = static_cast<float>(mySubRect.Width); float height = static_cast<float>(mySubRect.Height); // Check if the texture is valid, and calculate the texture coordinates FloatRect coords; if (myTexture) coords = myTexture->GetTexCoords(mySubRect); // Compute the texture coordinates float left = coords.Left; float top = coords.Top; float right = coords.Left + coords.Width; float bottom = coords.Top + coords.Height; if (myIsFlippedX) std::swap(left, right); if (myIsFlippedY) std::swap(top, bottom); // Bind the texture renderer.SetTexture(myTexture); // Draw the sprite's geometry renderer.Begin(Renderer::TriangleStrip); renderer.AddVertex(0, 0, left, top); renderer.AddVertex(width, 0, right, top); renderer.AddVertex(0, height, left, bottom); renderer.AddVertex(width, height, right, bottom); renderer.End(); }
void AnimatedSprite::Render(RenderTarget&, Renderer& renderer) const { // Get the sprite size float width = mSubRectSize.x; float height = mSubRectSize.y; // Check if the image is valid, and calculate the texture coordinates FloatRect coords; IntRect subrect(mSubRectOffset.x + mSubRectSize.x * mFrameNumber, mSubRectOffset.y, mSubRectSize.x, mSubRectSize.y); if (GetImage()) coords = GetImage()->GetTexCoords(subrect); // Compute the texture coordinates float left = coords.Left; float top = coords.Top; float right = coords.Left + coords.Width; float bottom = coords.Top + coords.Height; // Bind the texture renderer.SetTexture(GetImage()); // Draw the sprite's geometry renderer.Begin(Renderer::TriangleStrip); renderer.AddVertex(0, 0, left, top); renderer.AddVertex(width, 0, right, top); renderer.AddVertex(0, height, left, bottom); renderer.AddVertex(width, height, right, bottom); renderer.End(); }
int wmain() { #pragma region Other Stuff // Hide cursor SDL_ShowCursor(0); gameState = PLAY; //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //SDL Renderer r = Renderer(SCREEN_WIDTH, SCREEN_HEIGHT); AudioManager::GetInstance()->Init(); AudioManager::GetInstance()->LoadMedia(); AudioManager::GetInstance()->PlaySoundEffect(1); SDL_Point ePos; ePos.x = 100; ePos.y = 100; Enemy en = Enemy(200, 200, 62 / 3, 77 / 3, r, 0); Enemy en1 = Enemy(400, 100, 62 / 3, 77 / 3, r, 0); Level lvl = Level(r); // MENU SDL_Point bPos; bPos.x = 0; bPos.y = 0; Menu background = Menu(bPos, SCREEN_WIDTH, SCREEN_HEIGHT, r, 0, "background"); SDL_Point pPos; pPos.x = SCREEN_WIDTH / 3; pPos.y = SCREEN_HEIGHT / 4; Menu playBtn = Menu(pPos, 263, 44, r, 0, "playBtn"); SDL_Point e2Pos; e2Pos.x = (SCREEN_WIDTH / 2) - 50; e2Pos.y = (SCREEN_HEIGHT / 2) - 20; Menu exitBtn = Menu(e2Pos, 111, 45, r, 0, "exitBtn"); // Player SDL_Point playerPos; playerPos.x = SCREEN_WIDTH / 2 - 25; playerPos.y = SCREEN_HEIGHT / 2 - 10; Player player = Player(playerPos, 44, 32, r, 0); // THREADS \\ // Run the thread //Player * p = (); // pointer to object void * pv = &player; // pointer to void Player * p2 = static_cast<Player *>(pv); // pointer to the same object int data; SDL_Thread* threadID = SDL_CreateThread(renderThreadFunction, "RenderThread", (void*)p2); // THREADS \\ #pragma endregion #pragma region SDL STUFF bool quit = false; float oldTime = SDL_GetTicks(); float delta = 0.f; float newTime = 0.f; SDL_Event e; fpsTimer.start(); while (!quit) { while (SDL_PollEvent(&e) != 0) { InputManager::GetInstance()->UpdatePolledEvents(e); } if (e.button.button == SDL_SCANCODE_ESCAPE) quit = true; // THREADS \\ // THREADS \\ //controls gameState added in game menu feature r.Begin(); switch (gameState) { case MENU: //detect button click if (e.type == SDL_MOUSEBUTTONDOWN) { //If the left mouse button was pressed if (e.button.button == SDL_BUTTON_LEFT) { //Get the mouse offsets int mouse_x = e.button.x; int mouse_y = e.button.y; if (playBtn.IsClicked(mouse_x, mouse_y)) { gameState = PLAY; } else if (exitBtn.IsClicked(mouse_x, mouse_y)) { quit = true; } } }// End detect button click background.Draw(r); playBtn.Draw(r); exitBtn.Draw(r); break; case PLAY: newTime = SDL_GetTicks(); delta = newTime - oldTime; oldTime = newTime; en.Update(delta); lvl.Draw(r); en.Draw(r); en1.Update(delta); en1.Draw(r); en.Draw(r); player.Update(r, delta); player.Draw(r); //calculateFPS(); break; } InputManager::GetInstance()->UpdateState(); r.End(); } //Remove timer in case the call back was not called SDL_WaitThread(threadID, NULL); return EXIT_SUCCESS; }
void Text::Render(RenderTarget&, Renderer& renderer) const { // No text or not font: nothing to render if (!myFont || myString.IsEmpty()) return; // Bind the font texture const Image& texture = myFont->GetImage(myCharacterSize); renderer.SetTexture(&texture); // Computes values related to the text style bool bold = (myStyle & Bold) != 0; bool underlined = (myStyle & Underlined) != 0; float italicCoeff = (myStyle & Italic) ? 0.208f : 0.f; // 12 degrees float underlineOffset = myCharacterSize * 0.1f; float underlineThickness = myCharacterSize * (bold ? 0.1f : 0.07f); FloatRect underlineCoords = texture.GetTexCoords(IntRect(1, 1, 1, 1)); float underlineLeft = underlineCoords.Left; float underlineTop = underlineCoords.Top; float underlineRight = underlineCoords.Left + underlineCoords.Width; float underlineBottom = underlineCoords.Top + underlineCoords.Height; // Initialize the rendering coordinates float space = static_cast<float>(myFont->GetGlyph(L' ', myCharacterSize, bold).Advance); float lineSpacing = static_cast<float>(myFont->GetLineSpacing(myCharacterSize)); float x = 0.f; float y = static_cast<float>(myCharacterSize); // Note: // Here we use a Begin/End pair for each quad because // the font's texture may change in a call to GetGlyph // Draw one quad for each character Uint32 prevChar = 0; for (std::size_t i = 0; i < myString.GetSize(); ++i) { Uint32 curChar = myString[i]; // Apply the kerning offset x += static_cast<float>(myFont->GetKerning(prevChar, curChar, myCharacterSize)); prevChar = curChar; // If we're using the underlined style and there's a new line, draw a line if (underlined && (curChar == L'\n')) { float top = y + underlineOffset; float bottom = top + underlineThickness; renderer.Begin(Renderer::QuadList); renderer.AddVertex(0, top, underlineLeft, underlineTop); renderer.AddVertex(x, top, underlineRight, underlineTop); renderer.AddVertex(x, bottom, underlineRight, underlineBottom); renderer.AddVertex(0, bottom, underlineLeft, underlineBottom); renderer.End(); } // Handle special characters switch (curChar) { case L' ' : x += space; continue; case L'\t' : x += space * 4; continue; case L'\n' : y += lineSpacing; x = 0; continue; case L'\v' : y += lineSpacing * 4; continue; } // Extract the current glyph's description const Glyph& glyph = myFont->GetGlyph(curChar, myCharacterSize, bold); int advance = glyph.Advance; const IntRect& bounds = glyph.Bounds; const FloatRect& coords = texture.GetTexCoords(glyph.SubRect); int boundsRight = bounds.Left + bounds.Width; int boundsBottom = bounds.Top + bounds.Height; float coordsRight = coords.Left + coords.Width; float coordsBottom = coords.Top + coords.Height; // Draw a textured quad for the current character renderer.Begin(Renderer::QuadList); renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Top, y + bounds.Top, coords.Left, coords.Top); renderer.AddVertex(x + boundsRight - italicCoeff * bounds.Top, y + bounds.Top, coordsRight, coords.Top); renderer.AddVertex(x + boundsRight - italicCoeff * boundsBottom, y + boundsBottom, coordsRight, coordsBottom); renderer.AddVertex(x + bounds.Left - italicCoeff * boundsBottom, y + boundsBottom, coords.Left, coordsBottom); renderer.End(); // Advance to the next character x += advance; } // If we're using the underlined style, add the last line if (underlined) { float top = y + underlineOffset; float bottom = top + underlineThickness; renderer.Begin(Renderer::QuadList); renderer.AddVertex(0, top, underlineLeft, underlineTop); renderer.AddVertex(x, top, underlineRight, underlineTop); renderer.AddVertex(x, bottom, underlineRight, underlineBottom); renderer.AddVertex(0, bottom, underlineLeft, underlineBottom); renderer.End(); } }