将 SDL_Event* 传入方法

Passing in an SDL_Event* into a method

我想对 SDL2 中的文本输入进行计时,使其不以每秒 1000 次按键的速度向其发送垃圾邮件,而不是大约 33kps 的标准。

main.cpp

#include <iostream>
#include <SDL2/SDL.h>


#include "main.h"

void Main::Init()
{
    std::cout << "Main Init called\n";

    SDL_Init(SDL_INIT_VIDEO);

    Get_Instance().window = SDL_CreateWindow("Program", 0, 30, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
    Get_Instance().renderer = SDL_CreateRenderer(Get_Instance().window, -1, SDL_RENDERER_ACCELERATED  );

    Get_Instance().running = true;

}



void Main::Free()
{

    SDL_DestroyRenderer(Get_Instance().renderer);
    SDL_DestroyWindow(Get_Instance().window);


    SDL_Quit();

    std::cout << "Main Free called\n";

}



void Main::Mainloop()
{

    Get_Instance(); ///To initialize constructor

    if (Get_Instance().mainloopInstanceBlocker == 'C')
    {
        Get_Instance().mainloopInstanceBlocker = 'B';   ///Begins at I (initialized), then C (constructed) then B (began)
                                        ///It works as it begins as constructed, then does the main loop, after set to B, won't enter again.


        Get_Instance().Init();

        SDL_Event event;
        SDL_StartTextInput();



        while (Get_Instance().running)
        {

            ///Poll events
            SDL_PollEvent(&event);


            ///To quit program
            if ( event.type == SDL_QUIT ){
                Get_Instance().running = false;
                break;
            }



            ///Clear display to color
            SDL_SetRenderDrawColor(Get_Instance().renderer, 0,255,0,255);
            SDL_RenderClear(Get_Instance().renderer);



            Get_Instance().m_Main_Loop.Mainloop( Get_Instance().window, Get_Instance().renderer, &event );


            SDL_RenderPresent(Get_Instance().renderer);
        }



        SDL_StopTextInput();


        Get_Instance().Free();


    }
}




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

    Main::Mainloop();

    return 0;
}

main.h

#ifndef MAIN_H
#define MAIN_H

#include "main_loop.h"



class Main
{
public:
    Main(const Main&) = delete;
    Main(Main&&) = delete;
    Main& operator=(const Main&) = delete;
    Main& operator=(Main&&) = delete;


    static void Mainloop();




private:
    Main()
    {
        std::cout << "Main constructor called\n";
        mainloopInstanceBlocker = 'C';
    }

    static Main& Get_Instance()
    {
        static Main instance;
        return instance;
    }


    static void Init();
    static void Free();



    Main_Loop m_Main_Loop;

    SDL_Window* window = nullptr;
    SDL_Renderer* renderer = nullptr;

    bool running = false;
    char mainloopInstanceBlocker = 'I';



};








#endif // MAIN_H

main_loop.h

#ifndef MAIN_LOOP_H
#define MAIN_LOOP_H


#include <iostream>
#include <string>
#include <SDL2/SDL.h>


class Main_Loop
{
public:
    Main_Loop();
    ~Main_Loop();


    void Mainloop(SDL_Window* window, SDL_Renderer* renderer, SDL_Event* event);




};




#endif // MAIN_LOOP_H

main_loop.cpp

#include "main_loop.h"




Main_Loop::Main_Loop()
{

}


void Main_Loop::Mainloop(SDL_Window* window, SDL_Renderer* renderer, SDL_Event* event)
{
    if (event->type == SDL_TEXTINPUT)
    {
        std::cout << event->text.text << std::endl;
    }


}





Main_Loop::~Main_Loop()
{

}

你的问题是

        SDL_PollEvent(&event);

SDL_PollEvent returns 如果队列中没有更多事件,则为 0,但在这种情况下,它不会将任何内容写入传递的事件结构。由于您不再检查 PollEvent 的 return 值,您可以对上次事件留下的陈旧数据进行操作。至少做一些像

if(!SDL_PollEvent(&event)) event.type = 0;

您的 Mainloop 函数有问题 - 这是否意味着每帧只能有一个事件?如果单帧中发生了多个事件,你会怎么做?我建议将事件处理和渲染分开 - 即使事件处理函数处理整个事件队列并通过修改数据对事件做出反应,并分离仅进行渲染的渲染函数。