SDL OpenGL窗口立即关闭

时间:2014-02-07 00:23:34

标签: c++ opengl sdl

所以我刚开始玩sdl并且我在一个单独的类中工作正常但由于某种原因,当我将事物分成单独的类时,显示打开一个insta关闭。一个想法?

主类标题

#pragma once
#include <SDL.h>
#include <glew.h>
#include <iostream>

#include "Input.h"
#include "Display.h"
#include "RenderingEngine.h"
#include "PhysicsEngine.h"

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

    /* Engine settings & Engine Controlls*/
    void start();
    void stop();
    void pause(bool value);
    void run();

private:

    /* Loop Controllers */
    bool running;
    bool paused;


    /* Engine Initialisation */
    void initSDL();

    RenderingEngine render_core  = RenderingEngine(4, 2);
    PhysicsEngine   physics_core = PhysicsEngine();
    Display     display      = Display("game engine", 900, 900, 900, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    Input input;


};

主类

#include "Main.h"

Main::Main()
{
   initSDL();
   start();
}


Main::~Main()
{

}

void Main::initSDL()
{
    SDL_Init(SDL_INIT_EVERYTHING);
}

void Main::start()
{
    if (running) return;
        running = true;
        run();
}

void Main::stop()
{
if (!running) return;
     running = false;
     exit(0);
}   

void Main::pause(bool value)
{
    paused = value;
}

void Main::run()
{

  while (running)
  {

     if (!paused)
     {

     }
    render_core.render();
    display.swapBackBuffer();
    input.update();
   }
 }

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

  Main engine;

  return 0;
}

显示标题

#pragma once
#include <iostream>
#include <SDL.h>

class Display
{   
public:
    Display(const char* name, int x, int y, int w, int h, Uint32 flags);
    ~Display();

    void swapBackBuffer();

private:

    int x;
    int y;
    int w;
    int h;

    const char* name;

    Uint32 flags;
    SDL_Window *window;
    SDL_GLContext opengl;

};

展示班级

#include "Display.h"

Display::Display(const char* n, int x, int y, int w, int h, Uint32 f)
{
    this->x = x;
    this->y = y;
    this->w = w;
    this->h = h;
    this->name = name;
    this->flags = flags;

    this->window = SDL_CreateWindow(n, x, y, w, h, f);
    this->opengl = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, opengl);
    printf("Display: initialised\n\n");
}


Display::~Display()
{
    SDL_GL_DeleteContext(opengl);
    SDL_DestroyWindow(window);
    printf("Display: destroyed\n\n");
}

void Display::swapBackBuffer()
{
    SDL_GL_SwapWindow(window);
}

Render Engine class ....标题

中没有任何重要内容
#include "RenderingEngine.h"


RenderingEngine::RenderingEngine(int major_version, int minor_version)
{
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major_version);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor_version);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
    setClearColour(0, 0, 0, 1);
    printf("Rendering Engine:: initialised\n\n");
}


RenderingEngine::~RenderingEngine()
{
    printf("Rendering Engine:: destroyed\n");
}

void RenderingEngine::setClearColour(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
}

void RenderingEngine::clearScreen()
{
    glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
}

void RenderingEngine::render()
{
    clearScreen();
}

输入类

   #include "Input.h"

Input::Input()
{
    printf("Input:: initialised\n");
}


Input::~Input()
{
    printf("Input:: destroyed\n");
}

void Input::setMouseVisabilityTo(bool value)
{
    if (value) SDL_ShowCursor(1);
    else       SDL_ShowCursor(0);
}

int Input::getMouseX()
{
    return mouseX;
}

int Input::getMouseY()
{
    return mouseY;
}

void Input::update()
{
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {

        case SDL_QUIT:

            break;

        case SDL_KEYDOWN:
            keyboard[event.key.keysym.sym] = true;
            break;

        case SDL_KEYUP:
            keyboard[event.key.keysym.sym] = false;
            break;

        case SDL_MOUSEBUTTONDOWN:
            mouse[event.button.button] = true;
            break;

        case SDL_MOUSEBUTTONUP:
            mouse[event.button.button] = false;
            break;

        case SDL_MOUSEWHEEL:

            break;

        case SDL_MOUSEMOTION:
            mouseX = event.button.x;
            mouseY = event.button.y;
            break;
        }   
    }   
}

我知道有很多文件,所以非常感谢帮助,这一直困扰我一段时间

我编辑的main.h文件

#include "Display.h"
#include "RenderingEngine.h"
#include "PhysicsEngine.h"

class Main
{
public:
    Main() :
        render_core(4, 2),
        display("game engine", 900, 900, 900, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL),
        physics_core(),
        input()
    {
        running = false;
        paused = false;
        initSDL();
        start();
    }


    ~Main();

    /* Engine settings & Engine Controlls*/
    void start();
    void stop();
    void pause(bool value);
    void run();

private:

    /* Loop Controllers */
    bool running;
    bool paused;


    /* Engine Initialisation */
    void initSDL();

    RenderingEngine render_core;
    PhysicsEngine   physics_core;
    Display     display;

    Input input;

1 个答案:

答案 0 :(得分:2)

这是一个奇怪的C ++版本(例如 C ++ 11),你可以在同一个语句中声明和初始化一个非静态成员变量吗?

你不应该做那种事情,构建和初始化渲染上下文与其他软件相关的顺序非常重要。 这是构造函数的用途。

假设你实际上是故意( ab )在这里使用C ++ 11,你的窗口会立即关闭,因为这句话:

Display     display = Display("game engine", 900, 900, 900, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

构造一个Display对象,然后复制该对象并将其分配给display。在此语句“返回”之后,原始对象将被销毁。

看看Display的析构函数是做什么的:

Display::~Display()
{
    SDL_GL_DeleteContext(opengl);
    SDL_DestroyWindow(window);
    printf("Display: destroyed\n\n");
}

长话短说,不要以这种方式初始化你的成员。在C ++ 11出现之前,构造函数非常精细,让生活变得更加困难。

请考虑以下 构造函数

Main () : render_core  (4, 2),
          display      ("game engine", 900, 900, 900, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL),
          physics_core ()
{
  initSDL ();
  start   ();
}

...

private:
  RenderingEngine render_core;
  PhysicsEngine   physics_core;
  Display         display;

此解决方案在构造Main时初始化所有成员,没有任何复制分配,并且与更广泛的C ++编译器兼容。此代码中没有临时Display对象意味着您的窗口不会被创建,然后立即被销毁。