VPN termux about China.net

Sunday 25 August 2024

Console SDL2 .ttf requaied to run this projekt

 

 rubik-black.font font.ttf

#include <SDL.h>

#include <SDL_ttf.h>

#include <string>

#include <vector>

#include <stdio.h>

#include <stdlib.h>

#include <SDL.h>

#include <SDL_ttf.h>

#include <string>

#include <string>

#include "SDL2/SDL.h"

#include <stdio.h>

#include <stdlib.h>

#include <SDL2/SDL.h>

#include <SDL_image.h>

#include <stdio.h>

#define MAX_FILE_SIZE 1024 


const int SCREEN_WIDTH = 640;

const int SCREEN_HEIGHT = 480;

#define MAX_FILE_SIZE 1024


char* run(const char* cmd) {

    char *echo = (char*)malloc(MAX_FILE_SIZE);

    if (!echo) {

        perror("malloc");

        return NULL;

    }

    

    FILE *fp = popen(cmd, "r");

    if (fp == NULL) {

        perror("popen");

        free(echo);

        return NULL;

    }


    size_t len = fread(echo, 1, MAX_FILE_SIZE - 1, fp);

    echo[len] = '\0';


    pclose(fp);

    return echo;

}


int main(int argc, char* args[]) {

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {

        printf("SDL nie mogło się zainicjalizować! SDL_Error: %s\n", SDL_GetError());

        return -1;

    }


    if (TTF_Init() == -1) {

        printf("SDL_ttf nie mogło się zainicjalizować! TTF_Error: %s\n", TTF_GetError());

        SDL_Quit();

        return -1;

    }


    SDL_Window* window = SDL_CreateWindow("SDL2 Text Input/Output", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    if (window == nullptr) {

        printf("Okno nie mogło zostać utworzone! SDL_Error: %s\n", SDL_GetError());

        TTF_Quit();

        SDL_Quit();

        return -1;

    }


    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    if (renderer == nullptr) {

        printf("Renderer nie mógł zostać utworzony! SDL_Error: %s\n", SDL_GetError());

        SDL_DestroyWindow(window);

        TTF_Quit();

        SDL_Quit();

        return -1;

    }


    TTF_Font* font = TTF_OpenFont("./Rubik-Black.ttf", 24);

    if (font == nullptr) {

        printf("Nie udało się załadować czcionki! TTF_Error: %s\n", TTF_GetError());

        SDL_DestroyRenderer(renderer);

        SDL_DestroyWindow(window);

        TTF_Quit();

        SDL_Quit();

        return -1;

    }


    SDL_StartTextInput();

    char inputText[100] = {0};

    std::vector<std::string> outputLines;


    bool quit = false;

    SDL_Event e;


    while (!quit) {

        while (SDL_PollEvent(&e) != 0) {

            if (e.type == SDL_QUIT) {

                quit = true;

            } else if (e.type == SDL_TEXTINPUT) {

                strcat(inputText, e.text.text);

            } else if (e.type == SDL_KEYDOWN) {

                if (e.key.keysym.sym == SDLK_BACKSPACE && strlen(inputText) > 0) {

                    inputText[strlen(inputText) - 1] = '\0';

                } else if (e.key.keysym.sym == SDLK_RETURN) {

                    char *result = run(inputText);

                    if (result != NULL) {

                        outputLines.clear(); // Wyczyść poprzednie linie

                        char *line = strtok(result, "\n");

                        /*c++ std*/

                        while (line != NULL) {

                            outputLines.push_back(std::string(line));

                            line = strtok(NULL, "\n");

                        }

                        free(result);

                    } else {

                        outputLines.push_back("Błąd wykonania polecenia");

                    }

                    inputText[0] = '\0';

                }

            }

        }


        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

        SDL_RenderClear(renderer);


        SDL_Color textColor = { 0, 0, 0, 255 };

        int y = 10;

        for (const auto& line : outputLines) {

            SDL_Surface* textSurface = TTF_RenderText_Solid(font, line.c_str(), textColor);

            SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);

            int textWidth = textSurface->w;

            int textHeight = textSurface->h;

            SDL_Rect renderQuad = { 10, y, textWidth, textHeight };

            SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad);

            y += textHeight + 5; // Przesuń w dół dla kolejnej linii

            SDL_DestroyTexture(textTexture);

            SDL_FreeSurface(textSurface);

        }


        // Renderowanie wprowadzanego tekstu

        if (inputText[0] != '\0') {

            SDL_Surface* textSurface2 = TTF_RenderText_Solid(font, inputText, textColor);

            SDL_Texture* textTexture2 = SDL_CreateTextureFromSurface(renderer, textSurface2);

            int textWidth2 = textSurface2->w;

            int textHeight2 = textSurface2->h;

            SDL_Rect renderQuad2 = { 10, y, textWidth2, textHeight2 };

            SDL_RenderCopy(renderer, textTexture2, nullptr, &renderQuad2);

            SDL_DestroyTexture(textTexture2);

            SDL_FreeSurface(textSurface2);

        }


        SDL_RenderPresent(renderer);

    }


    SDL_StopTextInput();

    TTF_CloseFont(font);

    SDL_DestroyRenderer(renderer);

    SDL_DestroyWindow(window);

    TTF_Quit();

    SDL_Quit();


    return 0;

}


No comments:

Post a Comment