// graphics.cpp: implementation of the graphics class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "graphics.h"
#include "texture.h"
#include "quad.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Graphics::Graphics()
{
m_wglSwapIntervalEXT = NULL;
m_wglGetSwapIntervalEXT = NULL;
m_hRC = NULL;
m_hdc = NULL;
m_width = 320;
m_height = 240;
#ifdef FREEIMAGE_LIB
FreeImage_Initialise();
#endif
}
Graphics::~Graphics()
{
#ifdef FREEIMAGE_LIB
FreeImage_DeInitialise();
#endif
release();
}
void Graphics::release()
{
if (m_hRC) {
if (!wglMakeCurrent(NULL, NULL)) TRACE("Release Of DC And RC Failed.");
if (!wglDeleteContext(m_hRC)) TRACE("Release Rendering Context Failed.");
m_hRC = NULL;
}
}
// 设置HDC的像素格式
int Graphics::setPixelFormat(HDC hdc)
{
m_hdc = hdc;
static PIXELFORMATDESCRIPTOR pfd=//pfd 告诉窗口我们所希望的东东
{
sizeof(PIXELFORMATDESCRIPTOR),//上诉格式描述符的大小
1, // 版本号
PFD_DRAW_TO_WINDOW |// 格式必须支持窗口
PFD_SUPPORT_OPENGL |// 格式必须支持OpenGL
PFD_DOUBLEBUFFER, // 必须支持双缓冲
PFD_TYPE_RGBA,// 申请 RGBA 格式
32, // 选定色彩深度
0, 0, 0, 0, 0, 0, // 忽略的色彩位
0,// 无Alpha缓存
0,// 忽略Shift Bit
0,// 无聚集缓存
0, 0, 0, 0, // 忽略聚集位
16, // 16位 Z-缓存 (深度缓存)
0,// 无模板缓存
0,// 无辅助缓存
PFD_MAIN_PLANE, // 主绘图层
0,// 保留
0, 0, 0 // 忽略层遮罩
};
int iPixelFormat;
// 为设备描述表得到最匹配的像素格式
if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
TRACE("ChoosePixelFormat Failed");
return 1;
}
// 设置最匹配的像素格式为当前的像素格式
if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
TRACE("SetPixelFormat Failed");
return 1;
}
if (!(m_hRC=wglCreateContext(hdc))) {
TRACE("Can’t Create A GL Rendering Context.");
return 1;
}
if (!wglMakeCurrent(hdc,m_hRC)) {
TRACE("Can’t Activate The GL Rendering Context.");
return 1;
}
return 0;
}
bool Graphics::initVSync()
{
char* extensions = (char*)glGetString(GL_EXTENSIONS);
if (strstr(extensions,"WGL_EXT_swap_control")) {
m_wglSwapIntervalEXT = (PFNWGLEXTSWAPCONTROLPROC)wglGetProcAddress("wglSwapIntervalEXT");
m_wglGetSwapIntervalEXT = (PFNWGLEXTGETSWAPINTERVALPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
return true;
}
return false;
}
bool Graphics::isVSyncEnabled()
{
if (m_wglGetSwapIntervalEXT == NULL) return true;
return (m_wglGetSwapIntervalEXT() > 0);
}
void Graphics::setVSyncState(bool enable)
{
if (m_wglSwapIntervalEXT == NULL) return;
if (enable)
m_wglSwapIntervalEXT(1);
else
m_wglSwapIntervalEXT(0);
}
bool Graphics::init()
{
bool isOk = initVSync();
if (isOk) {
// 关闭垂直同步
setVSyncState(false);
}
glShadeModel(GL_SMOOTH); // GL_SMOOTH, GL_FLAT
glClearColor(0.65f,0.65f,0.65f,1.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// blend
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// texture
glEnable(GL_TEXTURE_2D);
return true;
}
GLvoid Graphics::resizeScene(GLsizei width, GLsizei height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
m_width = width;
m_height = height;
}
void Graphics::setClearColor(float red, float green, float blue)
{
glClearColor(red, green, blue, 1.0f);
}
//int Graphics::renderScene()
//{
//
// return 0;
//}
void Graphics::beginScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}
void Graphics::endScene()
{
if (m_hdc != NULL) {
::SwapBuffers(m_hdc);
}
}
void Graphics::drawLine(float x1, float y1, float x2, float y2, const stColor& color)
{
glDisable(GL_BLEND);
glLoadIdentity();
glColor4f(color.r, color.g, color.b, color.a);
glBegin(GL_LINES);
glVertex3f(x1, y1, 0);
glVertex3f(x2, y2, 0);
glEnd();
glEnable(GL_BLEND);
}
void Graphics::drawRectFrame(float x1, float y1, float x2, float y2, const stColor& color)
{
glDisable(GL_BLEND);
glLoadIdentity();
glColor4f(color.r, color.g, color.b, color.a);
glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, 0);
glVertex3f(x2, y1, 0);
glVertex3f(x2, y2, 0);
glVertex3f(x1, y2, 0);
glEnd();
glEnable(GL_BLEND);
}
没有合适的资源?快使用搜索试试~ 我知道了~
使用opengl渲染2D游戏的底层代码
共15个文件
h:8个
cpp:5个
dll:1个
4星 · 超过85%的资源 需积分: 35 66 下载量 112 浏览量
2011-03-02
11:06:15
上传
评论 4
收藏 740KB RAR 举报
温馨提示
使用opengl渲染2D游戏的底层代码,稍微贵了点,但是绝对有价值^_^,原创首发
资源推荐
资源详情
资源评论
收起资源包目录
glsdk.rar (15个子文件)
glsdk
quad.cpp 2KB
sprite.h 976B
singleton.h 655B
stdafx.h 303B
texture.cpp 3KB
quad.h 2KB
graphics.cpp 5KB
stdafx.cpp 141B
graphics.h 2KB
misc.h 389B
texture.h 852B
FreeImage
FreeImage.h 52KB
FreeImage.lib 60KB
FreeImage.dll 2.29MB
sprite.cpp 2KB
共 15 条
- 1
crackgame
- 粉丝: 9
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页