root/yuvplayer/win/yuvplayer/OpenGLView.cpp

Revision 88, 4.5 kB (checked in by aqua, 4 months ago)

Remove some memory leak

Line 
1// OpenGLView.cpp : implementation file
2//
3
4#include "stdafx.h"
5#include "yuvplayer.h"
6#include "OpenGLView.h"
7
8#include <gl/gl.h>
9
10// COpenGLView
11
12IMPLEMENT_DYNCREATE(COpenGLView, CView)
13
14COpenGLView::COpenGLView()
15{
16        loaded = FALSE;
17
18        t_width = 0;
19        t_height = 0;
20
21        ratio = 1.0;
22}
23
24COpenGLView::~COpenGLView()
25{
26}
27
28BEGIN_MESSAGE_MAP(COpenGLView, CView)
29        ON_WM_CREATE()
30        ON_WM_SIZE()
31        ON_WM_DESTROY()
32        ON_WM_ERASEBKGND()
33END_MESSAGE_MAP()
34
35
36// COpenGLView drawing
37
38void COpenGLView::OnDraw(CDC* pDC)
39{
40        CDocument* pDoc = GetDocument();
41        HDC dc = ::GetDC(m_hWnd);
42
43        // TODO: add draw code here     
44        glClear(GL_COLOR_BUFFER_BIT);           // clear screen and depth buffer
45
46        if( loaded ){
47                glBindTexture( GL_TEXTURE_2D, texture);
48                glBegin(GL_QUADS);
49                        glTexCoord2f( 0.f, 0.f );
50                        glVertex3i( 0, 0, 0);
51
52                        glTexCoord2f( 0.f, 1.f );
53                        glVertex3i( 0, t_height, 0);
54
55                        glTexCoord2f( 1.f, 1.f );
56                        glVertex3i( t_width, t_height, 0);
57
58                        glTexCoord2f( 1.f, 0.f );
59                        glVertex3i( t_width, 0, 0);
60                glEnd();
61        }
62        SwapBuffers( dc );
63
64        ::ReleaseDC( m_hWnd, dc );
65
66}
67
68
69// COpenGLView diagnostics
70
71#ifdef _DEBUG
72void COpenGLView::AssertValid() const
73{
74        CView::AssertValid();
75}
76
77#ifndef _WIN32_WCE
78void COpenGLView::Dump(CDumpContext& dc) const
79{
80        CView::Dump(dc);
81}
82#endif
83#endif //_DEBUG
84
85
86// COpenGLView message handlers
87BOOL COpenGLView::PreCreateWindow(CREATESTRUCT& cs)
88
89{
90
91       // TODO: Modify the Window class or styles here by modifying
92       cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC; 
93
94       return CView::PreCreateWindow(cs);
95
96} 
97int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
98{
99        if (CView::OnCreate(lpCreateStruct) == -1)
100                return -1;
101
102        // TODO:  Add your specialized creation code here
103        int nPixelFormat;                                       // our pixel format index
104
105        static PIXELFORMATDESCRIPTOR pfd = {
106                sizeof(PIXELFORMATDESCRIPTOR),  // size of structure
107                1,                                                              // default version
108                PFD_DRAW_TO_WINDOW |                    // window drawing support
109                PFD_SUPPORT_OPENGL |                    // OpenGL support
110                PFD_DOUBLEBUFFER,                               // double buffering support
111                PFD_TYPE_RGBA,                                  // RGBA color mode
112                32,                                                             // 32 bit color mode
113                0, 0, 0, 0, 0, 0,                               // ignore color bits, non-palettized mode
114                0,                                                              // no alpha buffer
115                0,                                                              // ignore shift bit
116                0,                                                              // no accumulation buffer
117                0, 0, 0, 0,                                             // ignore accumulation bits
118                0,                                                              // no z-buffer size
119                0,                                                              // no stencil buffer
120                0,                                                              // no auxiliary buffer
121                PFD_MAIN_PLANE,                                 // main drawing plane
122                0,                                                              // reserved
123                0, 0, 0 };                                              // layer masks ignored
124
125        HDC hdc = ::GetDC(m_hWnd);
126
127        nPixelFormat = ChoosePixelFormat(hdc, &pfd);
128        BOOL success = SetPixelFormat(hdc, nPixelFormat, &pfd);
129        m_hRC = wglCreateContext(hdc);
130
131        wglMakeCurrent(hdc, m_hRC);
132
133        glDisable(GL_DEPTH_TEST);
134        glEnable(GL_TEXTURE_2D);
135
136        glGenTextures( 1, &texture );
137        glBindTexture(GL_TEXTURE_2D, texture );
138
139        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
140        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
141
142        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
143        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
144
145        glClearColor(0,0,0,0);
146
147        return 0;
148}
149
150void COpenGLView::OnSize(UINT nType, int cx, int cy)
151{
152        CView::OnSize(nType, cx, cy);
153
154        // TODO: Add your message handler code here
155        glViewport( 0, 0, cx, cy );
156       
157    glMatrixMode(GL_PROJECTION);        // set projection matrix current matrix
158        glLoadIdentity();
159        glOrtho( 0, cx, cy, 0, 0, 100);
160
161        glMatrixMode(GL_MODELVIEW);
162        glLoadIdentity();
163
164        glTranslatef( 0, 0, -10.0f );
165        glScalef( ratio, ratio, 1.f );
166
167}
168
169void COpenGLView::OnDestroy()
170{
171        CView::OnDestroy();
172
173        // TODO: Add your message handler code here
174        wglDeleteContext(m_hRC);
175}
176
177void COpenGLView::SetParam(int width, int height, float ratio)
178{
179        for( t_width = 2  ; t_width  < width  ; t_width  *= 2 );
180        for( t_height = 2 ; t_height < height ; t_height *= 2 );
181
182        this->ratio = ratio;
183}
184
185void COpenGLView::LoadTexture(unsigned char* rgba)
186{
187
188        glBindTexture(GL_TEXTURE_2D, texture );
189        if( loaded )
190                glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, t_width, t_height, GL_RGBA, GL_UNSIGNED_BYTE, rgba );
191        else{
192                glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba );
193                loaded = TRUE;
194        }
195        Invalidate(NULL);
196}
197
198BOOL COpenGLView::OnEraseBkgnd(CDC* pDC)
199{
200        // TODO: Add your message handler code here and/or call default
201
202        return TRUE;
203        //return CView::OnEraseBkgnd(pDC);
204}
Note: See TracBrowser for help on using the browser.