Note that there are some explanatory texts on larger screens.

plurals
  1. POGDI is not rendering, what am I doing wrong?
    text
    copied!<p>I am trying to make a basic framework for graphics with GDI to make some minigames with. But GDI is not rendering anything at all, I just get a black client area, and I have no idea what I'm doing wrong.</p> <p>The following code is inside a game loop that is running constantly:</p> <pre><code>//render double buffered with GDI HDC frontBuffer = GetDC(m_hMainWnd); HDC backBuffer; HBITMAP bitmap; HBITMAP oldBitmap; backBuffer = CreateCompatibleDC(frontBuffer); bitmap = CreateCompatibleBitmap(frontBuffer, m_ClientWidth, m_ClientHeight); oldBitmap = (HBITMAP)SelectObject(backBuffer, bitmap); GDI-&gt;StartDrawing(backBuffer, m_ClientWidth, m_ClientHeight); //this basically selects pens and brushes etc Render(dRenderTime); //here I render some stuff GDI-&gt;StopDrawing(backBuffer); //selects old pens and brushes back //blit backbuffer to frontbuffer BitBlt(frontBuffer, 0, 0, m_ClientWidth, m_ClientHeight, backBuffer, 0, 0, SRCCOPY); SelectObject(backBuffer, oldBitmap); DeleteObject(bitmap); DeleteDC(backBuffer); ReleaseDC(m_hMainWnd, frontBuffer); } </code></pre> <p>What am I doing wrong here? Sorry if it's some stupid mistake, I am not good at all with windows programming.</p> <p>EDIT: additional code, as requested:</p> <pre><code>gdi.h #pragma once #include &lt;Windows.h&gt; #include "Macros.h" #include "Transform.h" #include "Text.h" #define GDI gdi::getInstance() class gdi { private: HPEN m_OldPen; HPEN m_Pen; HBRUSH m_OldBrush; HBRUSH m_Brush; HDC m_hdc; int m_DcWidth; int m_DcHeight; COLORREF m_Color; int m_LineWidth; DBlib::float3x3 m_Transform; gdi(); void Transform(int&amp; x_out, int&amp; y_out, const DBlib::float2&amp; p) const; void Transform_NDC_To_WC(int&amp; x_out, int&amp; y_out, const DBlib::float2&amp; p) const; void Transfrom_WC_To_NDC(DBlib::float2&amp; p_out, int x, int y) const; public: ~gdi(); static gdi* getInstance(); void StartDrawing(HDC hdc, int dcwidth, int dcheight); void StopDrawing(HDC hdc); void SetColor(const DBlib::float3&amp; col); void SetLineWidth(int width); void SetTransform(const DBlib::float3x3&amp; transform); void DrawText(const DBlib::float2&amp; p1, const std::tstring&amp; s); void DrawLine(const DBlib::float2&amp; p1, const DBlib::float2&amp; p2); void DrawPolygon(const DBlib::float2* p, int size); void FillPolygon(const DBlib::float2* p, int size); }; </code></pre> <p>GDI.cpp</p> <pre><code>#include "gdi.h" gdi* gdi::getInstance() { static gdi instance; return &amp;instance; } gdi::gdi() { m_hdc = NULL; m_OldPen = NULL; m_OldBrush = NULL; m_LineWidth = 1; m_Color = RGB(0,0,0); m_DcWidth = -1; m_DcHeight = -1; m_Transform.set_identity(); m_Pen = CreatePen(PS_SOLID,1,RGB(0,0,0)); m_Brush = CreateSolidBrush(RGB(255,255,255)); } gdi::~gdi() { if(m_Pen) DeleteObject(m_Pen); if(m_Brush) DeleteObject(m_Brush); } void gdi::StartDrawing(HDC hdc, int dcwidth, int dcheight) { m_hdc = hdc; m_DcWidth = dcwidth; m_DcHeight = dcheight; m_OldPen = (HPEN)SelectObject(hdc, m_Pen); m_OldBrush = (HBRUSH)SelectObject(hdc, m_Brush); } void gdi::StopDrawing(HDC hdc) { SelectObject(hdc, m_OldPen); SelectObject(hdc, m_OldBrush); m_hdc = NULL; m_DcWidth = -1; m_DcHeight = -1; m_OldPen = NULL; m_OldBrush = NULL; } void gdi::SetColor(const DBlib::float3&amp; col) { int r = static_cast&lt;int&gt;(DBlib::clamp(col.x*255.0f, 0.0f, 255.0f)); int g = static_cast&lt;int&gt;(DBlib::clamp(col.y*255.0f, 0.0f, 255.0f)); int b = static_cast&lt;int&gt;(DBlib::clamp(col.z*255.0f, 0.0f, 255.0f)); m_Color = RGB(r,g,b); SetTextColor(m_hdc, m_Color); if(m_Pen) DeleteObject(m_Pen); if(m_Brush) DeleteObject(m_Brush); m_Pen= CreatePen(PS_SOLID, m_LineWidth, m_Color); m_Brush= CreateSolidBrush(m_Color); } void gdi::SetLineWidth(int width) { m_LineWidth = width; if(m_Pen) DeleteObject(m_Pen); m_Pen= CreatePen(PS_SOLID, m_LineWidth, m_Color); } void gdi::SetTransform(const DBlib::float3x3&amp; transform) { m_Transform = transform; } void gdi::Transform_NDC_To_WC(int&amp; x_out, int&amp; y_out, const DBlib::float2&amp; p) const { x_out = static_cast&lt;int&gt;((p.x+1.0f)*(static_cast&lt;float&gt;(m_DcWidth)/2.0f)); y_out = m_DcHeight - static_cast&lt;int&gt;((p.y+1.0f)*(static_cast&lt;float&gt;(m_DcHeight)/2.0f)); } void gdi::Transfrom_WC_To_NDC(DBlib::float2&amp; p_out, int x, int y) const { p_out.x = static_cast&lt;float&gt;(x)*2.0f/static_cast&lt;float&gt;(m_DcWidth) - 1.0f; p_out.y = -(static_cast&lt;float&gt;(y)*2.0f/static_cast&lt;float&gt;(m_DcHeight) - 1.0f); } void gdi::Transform(int&amp; x_out, int&amp; y_out, const DBlib::float2&amp; p) const { Transform_NDC_To_WC(x_out, y_out, p*m_Transform); } void gdi::DrawText(const DBlib::float2&amp; pos, const std::tstring&amp; s) { int x,y; Transform(x,y,pos); TextOut(m_hdc, x, y, s.c_str(), (int)s.size()); } void gdi::DrawLine(const DBlib::float2&amp; p1, const DBlib::float2&amp; p2) { int x1,y1,x2,y2; Transform(x1,y1,p1); Transform(x2,y2,p2); MoveToEx(m_hdc, x1, y1, NULL); LineTo(m_hdc, x2, y2); } void gdi::DrawPolygon(const DBlib::float2* p, int size) { int* x = new int[size]; int* y = new int[size]; for(int i=0; i&lt;size; ++i) { Transform(x[i],y[i],*(p+i)); } for(int i=0; i&lt;size; ++i) { MoveToEx(m_hdc, x[i], y[i], NULL); LineTo(m_hdc, x[(i+1)%size], y[(i+1)%size]); } delete[] x; delete[] y; } void gdi::FillPolygon(const DBlib::float2* p, int size) { int* x = new int[size]; int* y = new int[size]; POINT* pts = new POINT[size]; for(int i=0; i&lt;size; ++i) { Transform(x[i],y[i],*(p+i)); pts[i].x = static_cast&lt;LONG&gt;(x[i]); pts[i].y = static_cast&lt;LONG&gt;(y[i]); } Polygon(m_hdc, pts, size); delete[] x; delete[] y; delete[] pts; } </code></pre> <p>App.cpp - Render method definition</p> <pre><code>void App::Render(float dTime) { GDI-&gt;SetColor(DBlib::float3(1.0f,1.0f,1.0f)); GDI-&gt;SetLineWidth(50); GDI-&gt;DrawLine(DBlib::float2(-1.0f,-1.0f), DBlib::float2(1.0f,1.0f)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload