Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////// HEADER
  2.  
  3. #ifndef DISPLAY_H
  4. #define DISPLAY_H
  5.  
  6. #include <QOpenGLWidget>
  7. #include <QOpenGLFunctions>
  8.  
  9. class Display : public QOpenGLWidget
  10. {
  11. public:
  12.     Display(QWidget* parent);
  13.     ~Display();
  14.  
  15.     void render();
  16.     static QSize displaySize() { return QSize(displayWidth, displayHeight); }
  17.  
  18.     void doTest();
  19.  
  20. protected:
  21.     void initializeGL() override;
  22.     void paintGL() override;
  23.     void resizeGL(int width, int height) override;
  24.  
  25. private:
  26.     static const quint32 displayWidth = 160;
  27.     static const quint32 displayHeight = 144;
  28.  
  29.     void setPixelColor(quint32 x, quint32 y, quint32 color);
  30.  
  31.     QColor clearColor;
  32.  
  33.     quint32* textureBuffer;
  34.     GLuint texture;
  35. };
  36.  
  37. #endif // DISPLAY_H
  38.  
  39. //////////////// SRC
  40.  
  41. #include "display.h"
  42.  
  43. Display::Display(QWidget* parent)
  44.     : QOpenGLWidget(parent), clearColor(Qt::black)
  45. {
  46.     textureBuffer = new quint32[displayWidth*displayHeight];
  47.  
  48.     setMinimumSize(displayWidth, displayHeight);
  49. }
  50.  
  51. Display::~Display()
  52. {
  53.     delete[] textureBuffer;
  54. }
  55.  
  56. void Display::initializeGL()
  57. {
  58.     glDisable(GL_DEPTH_TEST);
  59.     glDisable(GL_ALPHA_TEST);
  60.     glDisable(GL_STENCIL_TEST);
  61.  
  62.     glEnable(GL_TEXTURE_2D);
  63.     glEnable(GL_DITHER);
  64.  
  65.     glGenTextures(1, &texture);
  66.     glBindTexture(GL_TEXTURE_2D, texture);
  67.     glPixelStorei(GL_UNPACK_ROW_LENGTH, displayWidth);
  68.  
  69.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  70.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  71.  
  72.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, displayWidth, displayHeight, 0,
  73.                  GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, textureBuffer);
  74. }
  75.  
  76. void Display::paintGL()
  77. {
  78.     glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), 1.0f);
  79.  
  80.     glViewport(0, 0, width(), height());
  81.  
  82.     glMatrixMode(GL_PROJECTION);
  83.     glLoadIdentity();
  84.     glOrtho(0, width(), 0, height(), -1, 1);
  85.  
  86.     glMatrixMode(GL_MODELVIEW);
  87.     glLoadIdentity();
  88.  
  89.  
  90.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  91.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  92.     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, displayWidth, displayHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, textureBuffer);
  93.  
  94.  
  95.     glBindTexture(GL_TEXTURE_2D, texture);
  96.     glBegin(GL_TRIANGLE_STRIP);
  97.  
  98.     glTexCoord2f(0.0, 1.0);
  99.     glVertex3i(0, 0, 0);
  100.  
  101.     glTexCoord2f(1.0, 1.0);
  102.     glVertex3i(width(), 0, 0);
  103.  
  104.     glTexCoord2f(0.0, 0.0);
  105.     glVertex3i(0, height(), 0);
  106.  
  107.     glTexCoord2f(1.0, 0.0);
  108.     glVertex3i(width(), height(), 0);
  109.  
  110.     glEnd();
  111. }
  112.  
  113. void Display::resizeGL(int width, int height)
  114. {
  115.     Q_UNUSED(width);
  116.     Q_UNUSED(height);
  117.  
  118.     // Do some aspect ratio stuff here eventually...
  119. }
  120.  
  121.  
  122. void Display::setPixelColor(quint32 x, quint32 y, quint32 color)
  123. {
  124.     quint32 idx = (x + y* displayWidth);
  125.     textureBuffer[idx] = color;
  126. }
  127.  
  128. #include "mmu.h"
  129. void Display::doTest()
  130. {
  131.     for (uint i = 0; i < displayWidth; i++)
  132.         setPixelColor(i/8, i%8, MMU::vram[i]);
  133.     update();
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement