GTK: Change the OpenGL texture size when changing scaler size. Thanks to fernandotcl for the patch.

This commit is contained in:
bgk 2011-02-20 09:02:46 +00:00
parent de6db92249
commit 2009faab00
5 changed files with 40 additions and 20 deletions

View File

@ -29,7 +29,7 @@ template<typename T> T max( T x, T y ) { return x > y ? x : y; }
ScreenAreaGl::ScreenAreaGl(int _iWidth, int _iHeight, int _iScale) :
ScreenArea(_iWidth, _iHeight, _iScale),
m_uiScreenTexture(0),
m_iTextureSize(256)
m_iTextureSize(0)
{
Glib::RefPtr<Gdk::GL::Config> glconfig;
@ -46,6 +46,23 @@ ScreenAreaGl::ScreenAreaGl(int _iWidth, int _iHeight, int _iScale) :
vUpdateSize();
}
void ScreenAreaGl::vUpdateTexture()
{
// Calculate the new texture size as a the smallest working power of two
// TODO: Support the ARB_texture_rectangle extension
int iExpX = 0, iExpY = 0;
for (int i = m_iScaledWidth; i; i >>= 1, ++iExpX);
for (int i = m_iScaledHeight; i; i >>= 1, ++iExpY);
int iNewTextureSize = 1 << max(iExpX, iExpY);
// Notify the system if the texture size changed
if (iNewTextureSize != m_iTextureSize) {
m_iTextureSize = iNewTextureSize;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iNewTextureSize, iNewTextureSize,
0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
}
}
void ScreenAreaGl::on_realize()
{
Gtk::DrawingArea::on_realize();
@ -60,8 +77,6 @@ void ScreenAreaGl::on_realize()
if (glIsTexture(m_uiScreenTexture))
glDeleteTextures(1, &m_uiScreenTexture);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
@ -76,19 +91,7 @@ void ScreenAreaGl::on_realize()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Calculate texture size as a the smallest working power of two
float n1 = log10((float)m_iScaledWidth ) / log10( 2.0f);
float n2 = log10((float)m_iScaledHeight ) / log10( 2.0f);
float n = (n1 > n2)? n1 : n2;
// round up
if (((float)((int)n)) != n)
n = ((float)((int)n)) + 1.0f;
m_iTextureSize = (int)pow(2.0f, n);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_iTextureSize, m_iTextureSize, 0,
GL_BGRA, GL_UNSIGNED_BYTE, NULL);
vUpdateTexture();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
@ -171,4 +174,18 @@ bool ScreenAreaGl::on_expose_event(GdkEventExpose * _pstEvent)
return true;
}
void ScreenAreaGl::vOnSizeUpdated()
{
if (!is_realized())
return;
Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();
if (!glwindow->gl_begin(get_gl_context()))
return;
vUpdateTexture();
glwindow->gl_end();
}
} // namespace VBA

View File

@ -42,9 +42,9 @@ protected:
private:
GLuint m_uiScreenTexture;
int m_iTextureSize;
/* double m_dAreaTop;
double m_dAreaLeft;
double m_dScaleFactor;*/
void vUpdateTexture();
void vOnSizeUpdated();
};
} // namespace VBA

View File

@ -224,6 +224,8 @@ void ScreenArea::vUpdateSize()
memset(m_puiPixels, 0, (m_iScaledWidth + 1) * m_iScaledHeight * sizeof(u32));
memset(m_puiDelta, 255, (m_iWidth + 2) * (m_iHeight + 2) * sizeof(u32));
vOnSizeUpdated();
set_size_request(m_iScale * m_iWidth, m_iScale * m_iHeight);
}

View File

@ -48,6 +48,7 @@ protected:
virtual bool on_leave_notify_event(GdkEventCrossing * _pstEvent);
virtual bool on_configure_event(GdkEventConfigure * event);
virtual bool bOnCursorTimeout();
virtual void vOnSizeUpdated() {}
int m_iWidth;
int m_iHeight;

View File

@ -109,7 +109,7 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">gtk-media-pause</property>
<property name="label" translatable="yes">Pause</property>
<property name="use_underline">True</property>
<accelerator key="P" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>