Skip to content

Commit

Permalink
libgui: Various fixed
Browse files Browse the repository at this point in the history
Return loaded image contents, and perform GLWindow initialization
with audience notifications.
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 371395b commit d4d359c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
66 changes: 52 additions & 14 deletions doomsday/libs/gui/src/glwindow.cpp
Expand Up @@ -22,6 +22,8 @@
#include "de/GuiApp"
#include "de/GLTimer"
#include "de/ElapsedTimer"
#include "de/EventLoop"
#include "de/CoreEvent"

//#include <QElapsedTimer>
//#include <QImage>
Expand Down Expand Up @@ -51,24 +53,30 @@ DE_PIMPL(GLWindow)
LoopCallback mainCall;
GLFramebuffer backing; // Represents QOpenGLWindow's framebuffer.
WindowEventHandler *handler = nullptr; ///< Event handler.
bool initialized = false;
bool readyPending = false;
bool readyNotified = false;
bool paintPending = false;
Size currentSize;
double pixelRatio = 0.0;

uint frameCount = 0;
float fps = 0;
uint frameCount = 0;
float fps = 0;

std::unique_ptr<GLTimer> timer;
Id totalFrameTimeQueryId;

Impl(Public *i) : Base(i)
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
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_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

window = SDL_CreateWindow("GLWindow",
SDL_WINDOWPOS_UNDEFINED,
Expand Down Expand Up @@ -152,8 +160,11 @@ DE_PIMPL(GLWindow)
#endif
#endif

LIBGUI_ASSERT_GL_OK();

// Everybody can perform GL init now.
DE_FOR_PUBLIC_AUDIENCE2(Init, i) i->windowInit(self());
DE_FOR_PUBLIC_AUDIENCE2(Resize, i) i->windowResized(self());

readyNotified = true;

Expand Down Expand Up @@ -241,7 +252,7 @@ DE_PIMPL(GLWindow)
SDL_GL_GetDrawableSize(window, &pw, &ph);
debug("[GLWindow] Drawable size is %dx%d pixels", pw, ph);

pendingSize = Size(pw, ph);
auto pendingSize = Size(pw, ph); // * qApp->devicePixelRatio();

// Only react if this is actually a resize.
if (currentSize != pendingSize)
Expand Down Expand Up @@ -326,12 +337,6 @@ void GLWindow::doneCurrent()
SDL_GL_MakeCurrent(d->window, nullptr);
}

void GLWindow::update()
{
/// @todo Schedule a redraw event.

}

void GLWindow::show()
{
SDL_ShowWindow(d->window);
Expand Down Expand Up @@ -616,7 +621,13 @@ void GLWindow::handleSDLEvent(const void *ptr)
case SDL_WINDOWEVENT:
switch (event->window.event)
{
case SDL_WINDOWEVENT_EXPOSED: break;
case SDL_WINDOWEVENT_EXPOSED:
if (!d->initialized)
{
initializeGL();
paintGL();
}
break;

case SDL_WINDOWEVENT_MOVED:
DE_FOR_AUDIENCE2(Move, i)
Expand Down Expand Up @@ -646,16 +657,40 @@ void GLWindow::handleSDLEvent(const void *ptr)
}
}

void GLWindow::update()
{
if (!d->paintPending)
{
d->paintPending = true;
EventLoop::get()->postEvent(new CoreEvent(Event::Callback, [this]() {
makeCurrent();
paintGL();
doneCurrent();
}));
}
}

void GLWindow::initializeGL()
{
if (!d->initialized)
{
LOG_AS("GLWindow");
LOGDEV_GL_NOTE("Initializing OpenGL window");

d->initialized = true;
d->glInit();

int w, h;
SDL_GL_GetDrawableSize(d->window, &w, &h);
debug("initializeGL: %i x %i", w, h);

d->currentSize = Size(w, h);
}
}

void GLWindow::paintGL()
{
d->paintPending = false;
GLFramebuffer::setDefaultFramebuffer(0); //defaultFramebufferObject());

// Do not proceed with painting until after the application has completed
Expand All @@ -670,6 +705,7 @@ void GLWindow::paintGL()
d->mainCall.enqueue([this]() { d->notifyReady(); });
}
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(d->window);
return;
}

Expand Down Expand Up @@ -720,6 +756,8 @@ void GLWindow::paintGL()
// d->timerQueryPending = true;
// }
//#endif

SDL_GL_SwapWindow(d->window);
}

void GLWindow::windowAboutToClose()
Expand Down
1 change: 1 addition & 0 deletions doomsday/libs/gui/src/graphics/image.cpp
Expand Up @@ -1207,6 +1207,7 @@ Image Image::fromData(Block const &data, String const &formatHint)
num == 2 ? LuminanceAlpha_88 : Luminance_8);
img.d->pixels = Block(pixels, num * w * h); // makes an extra copy...
stbi_image_free(pixels);
return img;
}
}

Expand Down

0 comments on commit d4d359c

Please sign in to comment.