SDL_PollEvent导致我的窗口冻结

时间:2019-04-25 14:28:42

标签: c++ windows sdl

我能够找到 SDL_PollEvent 函数导致我的窗口无响应/冻结。当我注释掉它时,它工作正常,但是当我声明它时,它导致我的窗口冻结。我不确定这是功能故障还是代码中的某些原因导致它按原样运行。

这是导致冻结事件的代码块。

while (SDL_PollEvent(&m_event))
{
    switch (m_event.type)
    {
        case SDL_QUIT:
        return false;
    }
}

调用堆栈:

C++ SDL_Project.exe!Screen_namespace::Screen::ProcessEvents() Line 57   

C++  SDL_Project.exe!main() Line 41 

C++ [External Code] kernel32.dll![Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll] Unknown

所有文件:

Main.cpp

#define SDL_MAIN_HANDLED

#include <iostream>
#include <math.h>
#include "SDL.h"
#include "Screen.h"


int main()
{


    Screen_namespace::Screen screen;

    if (screen.init() == false)
        std::cout << "Error initializing SDL" << std::endl;


    while (true)
    {

        int elapsed = SDL_GetTicks();
        unsigned char red = (1 + sin(elapsed * 0.001) * 128);
        unsigned char green = (1 + sin(elapsed * 0.002) * 128);
        unsigned char blue = (1 + sin(elapsed * 0.003) * 128);


        for (int x = 0; x < screen.WINDOW_WIDTH; x++)
        {
            for (int y = 0; y < screen.WINDOW_HEIGHT; y++) {
                screen.setPixel(x, y, red, green, blue);
            }
        }

        //screen.setPixel(400, 300, 255, 255, 255);

        //Draw to the screen
        screen.update();


        //Check for messages/events.
        if (screen.ProcessEvents() == false)
        {
            break;
        }

    }
    screen.close();

    return 0;
}

Screen.h

#pragma once

#include "SDL.h"

namespace Screen_namespace {
    class Screen
    {
    public:
        const int WINDOW_WIDTH = 800;
        const int WINDOW_HEIGHT = 600;

        Screen();

        bool init();
        bool ProcessEvents();
        void close();
        void update();
        void setPixel(int x, int y, Uint32 red, Uint32 green, Uint32 blue);

    private:
        SDL_Window* m_window;
        SDL_Renderer* m_renderer;
        SDL_Texture* m_texture;
        Uint32* m_buffer;
        SDL_Event m_event;
    };
}

Screen.cpp

#include "Screen.h"

namespace Screen_namespace {

    Screen::Screen()
        : m_window(NULL), m_renderer(NULL), m_texture(NULL), m_buffer(NULL)
    {
    }

    bool Screen::init()
    {
        const int window_width = 800;
        const int window_height = 600;

        SDL_Init(SDL_INIT_VIDEO);

        m_window = SDL_CreateWindow("Particle Fire Explosion", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);

        m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC);
        m_texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, WINDOW_WIDTH, WINDOW_HEIGHT);

        m_buffer = new Uint32[WINDOW_WIDTH * WINDOW_HEIGHT]();

        SDL_memset(m_buffer, 0, WINDOW_WIDTH * WINDOW_HEIGHT * sizeof(Uint32));

        return true;
    }

    void Screen::setPixel(int x, int y, Uint32 red, Uint32 green, Uint32 blue)
    {
        Uint32 color = 0;

        color += red;
        color <<= 8;
        color += green;
        color <<= 8;
        color += blue;
        color <<= 8;
        color += 0xff;

        m_buffer[(y * WINDOW_WIDTH) + x] = color;

    }



    void Screen::update()
    {
        SDL_UpdateTexture(m_texture, NULL, m_buffer, WINDOW_WIDTH * sizeof(Uint32));
        SDL_RenderClear(m_renderer);
        SDL_RenderCopy(m_renderer, m_texture, NULL, NULL);
        SDL_RenderPresent(m_renderer);
    }

    bool Screen::ProcessEvents()
    {
        while (SDL_PollEvent(&m_event))
        {
            switch (m_event.type)
            {
            case SDL_QUIT:
                return false;
            }
        }
        return true;
    }

    void Screen::close()
    {
        delete[] m_buffer;
        SDL_DestroyTexture(m_texture);
        SDL_DestroyRenderer(m_renderer);
        SDL_DestroyWindow(m_window);
        SDL_Quit();
    }
}

0 个答案:

没有答案