@DOBUTSU

Как можно оптимизировать код?

Суть проблеммы в том что программа очень сильно лагает
#include <iostream>
#include <string>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>

SDL_Window* SDL_WindowWindow = nullptr;
SDL_Renderer* SDL_RendererRenderer = nullptr;
SDL_Event SDL_EventEvent;

float floatFPS = 0;

int main(void) {
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) > 0) {
        std::cerr << SDL_GetError() << std::endl;
    }

    if (IMG_Init(IMG_INIT_PNG) > 0) {
        std::cerr << IMG_GetError() << std::endl;
    }

    if (TTF_Init() > 0) {
        std::cerr << TTF_GetError() << std::endl;
    }

    SDL_WindowWindow = SDL_CreateWindow("Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    SDL_RendererRenderer = SDL_CreateRenderer(SDL_WindowWindow, -1, SDL_RENDERER_ACCELERATED);

    while (SDL_WindowWindow != nullptr && SDL_RendererRenderer != nullptr) {
        int intPerformanceCounterStart = SDL_GetPerformanceCounter();

        while (SDL_PollEvent(&SDL_EventEvent) != 0) {
            switch (SDL_EventEvent.type) {
                case SDL_QUIT: SDL_WindowWindow = nullptr; break;
            }
        }

        SDL_SetRenderDrawColor(SDL_RendererRenderer, 57, 57, 57, 255);
        SDL_RenderClear(SDL_RendererRenderer);

        {
            for (int intFor = 0; intFor < 100; intFor = intFor + 1) {
                SDL_Surface* SDL_SurfaceSurface = IMG_Load("test.png");
                if (SDL_SurfaceSurface == nullptr) {
                    std::cerr << IMG_GetError() << std::endl;
                }

                SDL_Texture* SDL_TextureTexture = SDL_CreateTextureFromSurface(SDL_RendererRenderer, SDL_SurfaceSurface);
                if (SDL_TextureTexture == nullptr) {
                    std::cerr << SDL_GetError() << std::endl;
                } SDL_FreeSurface(SDL_SurfaceSurface);

                SDL_Rect SDL_RectRectSource = {0, 0, 100, 100};
                SDL_Rect SDL_RectRect = {15 + (SDL_RectRectSource.w * intFor), 15, SDL_RectRectSource.w, SDL_RectRectSource.h};
                SDL_RenderCopy(SDL_RendererRenderer, SDL_TextureTexture, &SDL_RectRectSource, &SDL_RectRect);
                SDL_DestroyTexture(SDL_TextureTexture);
            }
        }
        {
            TTF_Font* TTF_FontFont = TTF_OpenFont("font.ttf", 25);
            if (TTF_FontFont == nullptr) {
                std::cerr << TTF_GetError() << std::endl;
            }

            std::string stringText = "FPS: " + std::to_string((int)(1.0f / floatFPS));
            SDL_Color SDL_ColorColor = {240, 255, 255, 255};

            SDL_Surface* SDL_SurfaceSurface = TTF_RenderText_Solid(TTF_FontFont, stringText.c_str(), SDL_ColorColor);
            if (SDL_SurfaceSurface == nullptr) {
                std::cerr << TTF_GetError() << std::endl;
            } TTF_CloseFont(TTF_FontFont);

            SDL_Texture* SDL_TextureTexture = SDL_CreateTextureFromSurface(SDL_RendererRenderer, SDL_SurfaceSurface);
            if (SDL_TextureTexture == nullptr) {
                std::cerr << SDL_GetError() << std::endl;
            } SDL_FreeSurface(SDL_SurfaceSurface);
            
            SDL_Rect SDL_RectRect = {15, 15, SDL_SurfaceSurface->w, SDL_SurfaceSurface->h};
            SDL_RenderCopy(SDL_RendererRenderer, SDL_TextureTexture, nullptr, &SDL_RectRect);
            SDL_DestroyTexture(SDL_TextureTexture);
        }

        SDL_RenderPresent(SDL_RendererRenderer);

        int intPerformanceCounterEnd = SDL_GetPerformanceCounter();
        floatFPS = (intPerformanceCounterEnd - intPerformanceCounterStart) / (float)SDL_GetPerformanceFrequency();
    }

    SDL_DestroyRenderer(SDL_RendererRenderer);
    SDL_DestroyWindow(SDL_WindowWindow);
    IMG_Quit();
    TTF_Quit();
    SDL_Quit();
    return 0;
}
  • Вопрос задан
  • 154 просмотра
Решения вопроса 1
@DOBUTSU Автор вопроса
int main(void) {
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) > 0) {
        std::cerr << SDL_GetError() << std::endl;
    }

    if (IMG_Init(IMG_INIT_PNG) > 0) {
        std::cerr << IMG_GetError() << std::endl;
    }

    if (TTF_Init() > 0) {
        std::cerr << TTF_GetError() << std::endl;
    }

    TTF_Font* TTF_FontFont = TTF_OpenFont("font.ttf", 25);
    if (TTF_FontFont == nullptr) {
        std::cerr << TTF_GetError() << std::endl;
    }

    ...
    TTF_CloseFont(TTF_FontFont);
    SDL_DestroyRenderer(SDL_RendererRenderer);
    SDL_DestroyWindow(SDL_WindowWindow);
    IMG_Quit();
    TTF_Quit();
    SDL_Quit();
    return 0;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы